Buildbot: Initial work to move linux build environment to CMake
[blender.git] / build_files / cmake / macros.cmake
1 # ***** BEGIN GPL LICENSE BLOCK *****
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # The Original Code is Copyright (C) 2006, Blender Foundation
18 # All rights reserved.
19 #
20 # The Original Code is: all of this file.
21 #
22 # Contributor(s): Jacques Beaurain.
23 #
24 # ***** END GPL LICENSE BLOCK *****
25
26 macro(list_insert_after
27         list_id item_check item_add
28         )
29         set(_index)
30         list(FIND "${list_id}" "${item_check}" _index)
31         if("${_index}" MATCHES "-1")
32                 message(FATAL_ERROR "'${list_id}' doesn't contain '${item_check}'")
33         endif()
34         math(EXPR _index "${_index} + 1")
35         list(INSERT ${list_id} "${_index}" ${item_add})
36         unset(_index)
37 endmacro()
38
39 macro(list_insert_before
40         list_id item_check item_add
41         )
42         set(_index)
43         list(FIND "${list_id}" "${item_check}" _index)
44         if("${_index}" MATCHES "-1")
45                 message(FATAL_ERROR "'${list_id}' doesn't contain '${item_check}'")
46         endif()
47         list(INSERT ${list_id} "${_index}" ${item_add})
48         unset(_index)
49 endmacro()
50
51 function(list_assert_duplicates
52         list_id
53         )
54         
55         # message(STATUS "list data: ${list_id}")
56
57         list(LENGTH list_id _len_before)
58         list(REMOVE_DUPLICATES list_id)
59         list(LENGTH list_id _len_after)
60         # message(STATUS "list size ${_len_before} -> ${_len_after}")
61         if(NOT _len_before EQUAL _len_after)
62                 message(FATAL_ERROR "duplicate found in list which should not contain duplicates: ${list_id}")
63         endif()
64         unset(_len_before)
65         unset(_len_after)
66 endfunction()
67
68
69 # foo_bar.spam --> foo_barMySuffix.spam
70 macro(file_suffix
71         file_name_new file_name file_suffix
72         )
73
74         get_filename_component(_file_name_PATH ${file_name} PATH)
75         get_filename_component(_file_name_NAME_WE ${file_name} NAME_WE)
76         get_filename_component(_file_name_EXT ${file_name} EXT)
77         set(${file_name_new} "${_file_name_PATH}/${_file_name_NAME_WE}${file_suffix}${_file_name_EXT}")
78
79         unset(_file_name_PATH)
80         unset(_file_name_NAME_WE)
81         unset(_file_name_EXT)
82 endmacro()
83
84 # useful for adding debug suffix to library lists:
85 # /somepath/foo.lib --> /somepath/foo_d.lib
86 macro(file_list_suffix
87         fp_list_new fp_list fn_suffix
88         )
89
90         # incase of empty list
91         set(_fp)
92         set(_fp_suffixed)
93
94         set(fp_list_new)
95
96         foreach(_fp ${fp_list})
97                 file_suffix(_fp_suffixed "${_fp}" "${fn_suffix}")
98                 list(APPEND "${fp_list_new}" "${_fp_suffixed}")
99         endforeach()
100
101         unset(_fp)
102         unset(_fp_suffixed)
103
104 endmacro()
105
106 if(UNIX AND NOT APPLE)
107         macro(find_package_static)
108                 set(_cmake_find_library_suffixes_back ${CMAKE_FIND_LIBRARY_SUFFIXES})
109                 set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
110                 find_package(${ARGV})
111                 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_cmake_find_library_suffixes_back})
112                 unset(_cmake_find_library_suffixes_back)
113         endmacro()
114
115         macro(find_library_static)
116                 set(_cmake_find_library_suffixes_back ${CMAKE_FIND_LIBRARY_SUFFIXES})
117                 set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
118                 find_library(${ARGV})
119                 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_cmake_find_library_suffixes_back})
120                 unset(_cmake_find_library_suffixes_back)
121         endmacro()
122 endif()
123
124 function(target_link_libraries_optimized
125         TARGET
126         LIBS
127         )
128
129         foreach(_LIB ${LIBS})
130                 target_link_libraries(${TARGET} optimized "${_LIB}")
131         endforeach()
132 endfunction()
133
134 function(target_link_libraries_debug
135         TARGET
136         LIBS
137         )
138
139         foreach(_LIB ${LIBS})
140                 target_link_libraries(${TARGET} debug "${_LIB}")
141         endforeach()
142 endfunction()
143
144 function(target_link_libraries_decoupled
145         target
146         libraries_var
147         )
148
149         if(NOT MSVC)
150                 target_link_libraries(${target} ${${libraries_var}})
151         else()
152                 # For MSVC we link to different libraries depending whether
153                 # release or debug target is being built.
154                 file_list_suffix(_libraries_debug "${${libraries_var}}" "_d")
155                 target_link_libraries_debug(${target} "${_libraries_debug}")
156                 target_link_libraries_optimized(${target} "${${libraries_var}}")
157         endif()
158 endfunction()
159
160 # Nicer makefiles with -I/1/foo/ instead of -I/1/2/3/../../foo/
161 # use it instead of include_directories()
162 function(blender_include_dirs
163         includes
164         )
165
166         set(_ALL_INCS "")
167         foreach(_INC ${ARGV})
168                 get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
169                 list(APPEND _ALL_INCS ${_ABS_INC})
170                 # for checking for invalid includes, disable for regular use
171                 ##if(NOT EXISTS "${_ABS_INC}/")
172                 ##      message(FATAL_ERROR "Include not found: ${_ABS_INC}/")
173                 ##endif()
174         endforeach()
175         include_directories(${_ALL_INCS})
176 endfunction()
177
178 function(blender_include_dirs_sys
179         includes
180         )
181
182         set(_ALL_INCS "")
183         foreach(_INC ${ARGV})
184                 get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
185                 list(APPEND _ALL_INCS ${_ABS_INC})
186                 ##if(NOT EXISTS "${_ABS_INC}/")
187                 ##      message(FATAL_ERROR "Include not found: ${_ABS_INC}/")
188                 ##endif()
189         endforeach()
190         include_directories(SYSTEM ${_ALL_INCS})
191 endfunction()
192
193 function(blender_source_group
194         sources
195         )
196
197         # Group by location on disk
198         source_group("Source Files" FILES CMakeLists.txt)
199
200         foreach(_SRC ${sources})
201                 get_filename_component(_SRC_EXT ${_SRC} EXT)
202                 if((${_SRC_EXT} MATCHES ".h") OR
203                    (${_SRC_EXT} MATCHES ".hpp") OR
204                    (${_SRC_EXT} MATCHES ".hh"))
205
206                         set(GROUP_ID "Header Files")
207                 else()
208                         set(GROUP_ID "Source Files")
209                 endif()
210                 source_group("${GROUP_ID}" FILES ${_SRC})
211         endforeach()
212 endfunction()
213
214
215 # only MSVC uses SOURCE_GROUP
216 function(blender_add_lib_nolist
217         name
218         sources
219         includes
220         includes_sys
221         )
222
223         # message(STATUS "Configuring library ${name}")
224
225         # include_directories(${includes})
226         # include_directories(SYSTEM ${includes_sys})
227         blender_include_dirs("${includes}")
228         blender_include_dirs_sys("${includes_sys}")
229
230         add_library(${name} ${sources})
231
232         # works fine without having the includes
233         # listed is helpful for IDE's (QtCreator/MSVC)
234         blender_source_group("${sources}")
235
236         list_assert_duplicates("${sources}")
237         list_assert_duplicates("${includes}")
238         # Not for system includes because they can resolve to the same path
239         # list_assert_duplicates("${includes_sys}")
240
241 endfunction()
242
243
244 function(blender_add_lib
245         name
246         sources
247         includes
248         includes_sys
249         )
250
251         blender_add_lib_nolist(${name} "${sources}" "${includes}" "${includes_sys}")
252
253         set_property(GLOBAL APPEND PROPERTY BLENDER_LINK_LIBS ${name})
254 endfunction()
255
256
257 function(SETUP_LIBDIRS)
258
259         link_directories(${JPEG_LIBPATH} ${PNG_LIBPATH} ${ZLIB_LIBPATH} ${FREETYPE_LIBPATH})
260
261         if(WITH_PYTHON)  #  AND NOT WITH_PYTHON_MODULE  # WIN32 needs
262                 link_directories(${PYTHON_LIBPATH})
263         endif()
264         if(WITH_SDL AND NOT WITH_SDL_DYNLOAD)
265                 link_directories(${SDL_LIBPATH})
266         endif()
267         if(WITH_CODEC_FFMPEG)
268                 link_directories(${FFMPEG_LIBPATH})
269         endif()
270         if(WITH_IMAGE_OPENEXR)
271                 link_directories(${OPENEXR_LIBPATH})
272         endif()
273         if(WITH_IMAGE_TIFF)
274                 link_directories(${TIFF_LIBPATH})
275         endif()
276         if(WITH_BOOST)
277                 link_directories(${BOOST_LIBPATH})
278         endif()
279         if(WITH_OPENIMAGEIO)
280                 link_directories(${OPENIMAGEIO_LIBPATH})
281         endif()
282         if(WITH_OPENCOLORIO)
283                 link_directories(${OPENCOLORIO_LIBPATH})
284         endif()
285         if(WITH_IMAGE_OPENJPEG AND WITH_SYSTEM_OPENJPEG)
286                 link_directories(${OPENJPEG_LIBPATH})
287         endif()
288         if(WITH_CODEC_QUICKTIME)
289                 link_directories(${QUICKTIME_LIBPATH})
290         endif()
291         if(WITH_OPENAL)
292                 link_directories(${OPENAL_LIBPATH})
293         endif()
294         if(WITH_JACK AND NOT WITH_JACK_DYNLOAD)
295                 link_directories(${JACK_LIBPATH})
296         endif()
297         if(WITH_CODEC_SNDFILE)
298                 link_directories(${SNDFILE_LIBPATH})
299         endif()
300         if(WITH_FFTW3)
301                 link_directories(${FFTW3_LIBPATH})
302         endif()
303         if(WITH_OPENCOLLADA)
304                 link_directories(${OPENCOLLADA_LIBPATH})
305                 link_directories(${PCRE_LIBPATH})
306                 link_directories(${EXPAT_LIBPATH})
307         endif()
308         if(WITH_LLVM)
309                 link_directories(${LLVM_LIBPATH})
310         endif()
311         if(WITH_MEM_JEMALLOC)
312                 link_directories(${JEMALLOC_LIBPATH})
313         endif()
314
315         if(WIN32 AND NOT UNIX)
316                 link_directories(${PTHREADS_LIBPATH})
317         endif()
318 endfunction()
319
320 function(setup_liblinks
321         target
322         )
323
324         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
325         set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
326
327         set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
328         set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
329
330         set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
331         set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
332
333         target_link_libraries(
334                 ${target}
335                 ${PNG_LIBRARIES}
336                 ${ZLIB_LIBRARIES}
337                 ${FREETYPE_LIBRARY}
338         )
339
340         # since we are using the local libs for python when compiling msvc projects, we need to add _d when compiling debug versions
341         if(WITH_PYTHON)  # AND NOT WITH_PYTHON_MODULE  # WIN32 needs
342                 target_link_libraries(${target} ${PYTHON_LINKFLAGS})
343
344                 if(WIN32 AND NOT UNIX)
345                         file_list_suffix(PYTHON_LIBRARIES_DEBUG "${PYTHON_LIBRARIES}" "_d")
346                         target_link_libraries_debug(${target} "${PYTHON_LIBRARIES_DEBUG}")
347                         target_link_libraries_optimized(${target} "${PYTHON_LIBRARIES}")
348                         unset(PYTHON_LIBRARIES_DEBUG)
349                 else()
350                         target_link_libraries(${target} ${PYTHON_LIBRARIES})
351                 endif()
352         endif()
353
354         if(WITH_LZO AND WITH_SYSTEM_LZO)
355                 target_link_libraries(${target} ${LZO_LIBRARIES})
356         endif()
357         if(WITH_SYSTEM_GLEW)
358                 target_link_libraries(${target} ${BLENDER_GLEW_LIBRARIES})
359         endif()
360         if(WITH_BULLET AND WITH_SYSTEM_BULLET)
361                 target_link_libraries(${target} ${BULLET_LIBRARIES})
362         endif()
363         if(WITH_AUDASPACE AND WITH_SYSTEM_AUDASPACE)
364                 target_link_libraries(${target} ${AUDASPACE_C_LIBRARIES} ${AUDASPACE_PY_LIBRARIES})
365         endif()
366         if(WITH_OPENAL)
367                 target_link_libraries(${target} ${OPENAL_LIBRARY})
368         endif()
369         if(WITH_FFTW3)
370                 target_link_libraries(${target} ${FFTW3_LIBRARIES})
371         endif()
372         if(WITH_JACK AND NOT WITH_JACK_DYNLOAD)
373                 target_link_libraries(${target} ${JACK_LIBRARIES})
374         endif()
375         if(WITH_CODEC_SNDFILE)
376                 target_link_libraries(${target} ${SNDFILE_LIBRARIES})
377         endif()
378         if(WITH_SDL AND NOT WITH_SDL_DYNLOAD)
379                 target_link_libraries(${target} ${SDL_LIBRARY})
380         endif()
381         if(WITH_CODEC_QUICKTIME)
382                 target_link_libraries(${target} ${QUICKTIME_LIBRARIES})
383         endif()
384         if(WITH_IMAGE_TIFF)
385                 target_link_libraries(${target} ${TIFF_LIBRARY})
386         endif()
387         if(WITH_OPENIMAGEIO)
388                 target_link_libraries(${target} ${OPENIMAGEIO_LIBRARIES})
389         endif()
390         if(WITH_OPENCOLORIO)
391                 target_link_libraries(${target} ${OPENCOLORIO_LIBRARIES})
392         endif()
393         if(WITH_OPENSUBDIV)
394                 if(WIN32 AND NOT UNIX)
395                         file_list_suffix(OPENSUBDIV_LIBRARIES_DEBUG "${OPENSUBDIV_LIBRARIES}" "_d")
396                         target_link_libraries_debug(${target} "${OPENSUBDIV_LIBRARIES_DEBUG}")
397                         target_link_libraries_optimized(${target} "${OPENSUBDIV_LIBRARIES}")
398                         unset(OPENSUBDIV_LIBRARIES_DEBUG)
399                 else()
400                         target_link_libraries(${target} ${OPENSUBDIV_LIBRARIES})
401                 endif()
402         endif()
403         if(WITH_CYCLES_OSL)
404                 target_link_libraries(${target} ${OSL_LIBRARIES})
405         endif()
406         if(WITH_BOOST)
407                 target_link_libraries(${target} ${BOOST_LIBRARIES})
408                 if(Boost_USE_STATIC_LIBS AND Boost_USE_ICU)
409                         target_link_libraries(${target} ${ICU_LIBRARIES})
410                 endif()
411         endif()
412         target_link_libraries(${target} ${JPEG_LIBRARIES})
413         if(WITH_IMAGE_OPENEXR)
414                 if(WIN32 AND NOT UNIX AND NOT CMAKE_COMPILER_IS_GNUCC)
415                         file_list_suffix(OPENEXR_LIBRARIES_DEBUG "${OPENEXR_LIBRARIES}" "_d")
416                         target_link_libraries_debug(${target} "${OPENEXR_LIBRARIES_DEBUG}")
417                         target_link_libraries_optimized(${target} "${OPENEXR_LIBRARIES}")
418                         unset(OPENEXR_LIBRARIES_DEBUG)
419                 else()
420                         target_link_libraries(${target} ${OPENEXR_LIBRARIES})
421                 endif()
422         endif()
423         if(WITH_IMAGE_OPENJPEG AND WITH_SYSTEM_OPENJPEG)
424                 target_link_libraries(${target} ${OPENJPEG_LIBRARIES})
425         endif()
426         if(WITH_CODEC_FFMPEG)
427                 target_link_libraries(${target} ${FFMPEG_LIBRARIES})
428         endif()
429         if(WITH_OPENCOLLADA)
430                 if(WIN32 AND NOT UNIX)
431                         file_list_suffix(OPENCOLLADA_LIBRARIES_DEBUG "${OPENCOLLADA_LIBRARIES}" "_d")
432                         target_link_libraries_debug(${target} "${OPENCOLLADA_LIBRARIES_DEBUG}")
433                         target_link_libraries_optimized(${target} "${OPENCOLLADA_LIBRARIES}")
434                         unset(OPENCOLLADA_LIBRARIES_DEBUG)
435
436                         file_list_suffix(PCRE_LIBRARIES_DEBUG "${PCRE_LIBRARIES}" "_d")
437                         target_link_libraries_debug(${target} "${PCRE_LIBRARIES_DEBUG}")
438                         target_link_libraries_optimized(${target} "${PCRE_LIBRARIES}")
439                         unset(PCRE_LIBRARIES_DEBUG)
440
441                         if(EXPAT_LIB)
442                                 file_list_suffix(EXPAT_LIB_DEBUG "${EXPAT_LIB}" "_d")
443                                 target_link_libraries_debug(${target} "${EXPAT_LIB_DEBUG}")
444                                 target_link_libraries_optimized(${target} "${EXPAT_LIB}")
445                                 unset(EXPAT_LIB_DEBUG)
446                         endif()
447                 else()
448                         target_link_libraries(
449                                 ${target}
450                                 ${OPENCOLLADA_LIBRARIES}
451                                 ${PCRE_LIBRARIES}
452                                 ${XML2_LIBRARIES}
453                                 ${EXPAT_LIB}
454                         )
455                 endif()
456         endif()
457         if(WITH_MEM_JEMALLOC)
458                 target_link_libraries(${target} ${JEMALLOC_LIBRARIES})
459         endif()
460         if(WITH_INPUT_NDOF)
461                 target_link_libraries(${target} ${NDOF_LIBRARIES})
462         endif()
463         if(WITH_MOD_CLOTH_ELTOPO)
464                 target_link_libraries(${target} ${LAPACK_LIBRARIES})
465         endif()
466         if(WITH_LLVM)
467                 target_link_libraries(${target} ${LLVM_LIBRARY})
468         endif()
469         if(WIN32 AND NOT UNIX)
470                 target_link_libraries(${target} ${PTHREADS_LIBRARIES})
471         endif()
472         if(UNIX AND NOT APPLE)
473                 if(WITH_OPENMP_STATIC)
474                         target_link_libraries(${target} ${OpenMP_LIBRARIES})
475                 endif()
476         endif()
477
478         # We put CLEW and CUEW here because OPENSUBDIV_LIBRARIES dpeends on them..
479         if(WITH_CYCLES OR WITH_COMPOSITOR OR WITH_OPENSUBDIV)
480                 target_link_libraries(${target} "extern_clew")
481                 target_link_libraries(${target} "extern_cuew")
482         endif()
483
484         #system libraries with no dependencies such as platform link libs or opengl should go last
485         target_link_libraries(${target}
486                         ${BLENDER_GL_LIBRARIES})
487
488         target_link_libraries(${target} ${PLATFORM_LINKLIBS} ${CMAKE_DL_LIBS})
489 endfunction()
490
491
492 function(SETUP_BLENDER_SORTED_LIBS)
493
494         get_property(BLENDER_LINK_LIBS GLOBAL PROPERTY BLENDER_LINK_LIBS)
495
496         list(APPEND BLENDER_LINK_LIBS
497                 bf_windowmanager
498                 bf_render
499         )
500
501         if(WITH_MOD_FLUID)
502                 list(APPEND BLENDER_LINK_LIBS bf_intern_elbeem)
503         endif()
504
505         if(WITH_CYCLES)
506                 list(APPEND BLENDER_LINK_LIBS
507                         cycles_render
508                         cycles_bvh
509                         cycles_device
510                         cycles_kernel
511                         cycles_util
512                         cycles_subd)
513                 if(WITH_CYCLES_OSL)
514                         list(APPEND BLENDER_LINK_LIBS cycles_kernel_osl)
515                 endif()
516         endif()
517
518         # Sort libraries
519         set(BLENDER_SORTED_LIBS
520                 bf_windowmanager
521
522                 bf_editor_space_api
523                 bf_editor_space_action
524                 bf_editor_space_buttons
525                 bf_editor_space_console
526                 bf_editor_space_file
527                 bf_editor_space_graph
528                 bf_editor_space_image
529                 bf_editor_space_info
530                 bf_editor_space_logic
531                 bf_editor_space_nla
532                 bf_editor_space_node
533                 bf_editor_space_outliner
534                 bf_editor_space_script
535                 bf_editor_space_sequencer
536                 bf_editor_space_text
537                 bf_editor_space_time
538                 bf_editor_space_userpref
539                 bf_editor_space_view3d
540                 bf_editor_space_clip
541
542                 bf_editor_transform
543                 bf_editor_util
544                 bf_editor_uvedit
545                 bf_editor_curve
546                 bf_editor_gpencil
547                 bf_editor_interface
548                 bf_editor_mesh
549                 bf_editor_metaball
550                 bf_editor_object
551                 bf_editor_armature
552                 bf_editor_physics
553                 bf_editor_render
554                 bf_editor_screen
555                 bf_editor_sculpt_paint
556                 bf_editor_sound
557                 bf_editor_animation
558                 bf_editor_datafiles
559                 bf_editor_mask
560                 bf_editor_io
561
562                 bf_render
563                 bf_python
564                 bf_python_ext
565                 bf_python_mathutils
566                 bf_python_bmesh
567                 bf_freestyle
568                 bf_ikplugin
569                 bf_modifiers
570                 bf_bmesh
571                 bf_gpu
572                 bf_blenkernel
573                 bf_physics
574                 bf_nodes
575                 bf_rna
576                 bf_blenloader
577                 bf_imbuf
578                 bf_blenlib
579                 bf_depsgraph
580                 bf_intern_ghost
581                 bf_intern_string
582                 bf_avi
583                 bf_imbuf_cineon
584                 bf_imbuf_openexr
585                 bf_imbuf_openimageio
586                 bf_imbuf_dds
587                 bf_collada
588                 bf_intern_elbeem
589                 bf_intern_memutil
590                 bf_intern_guardedalloc
591                 bf_intern_ctr
592                 bf_intern_utfconv
593                 ge_blen_routines
594                 ge_converter
595                 ge_phys_dummy
596                 ge_phys_bullet
597                 bf_intern_smoke
598                 extern_lzma
599                 extern_colamd
600                 ge_logic_ketsji
601                 extern_recastnavigation
602                 ge_logic
603                 ge_rasterizer
604                 ge_oglrasterizer
605                 ge_logic_expressions
606                 ge_scenegraph
607                 ge_logic_network
608                 ge_logic_ngnetwork
609                 ge_logic_loopbacknetwork
610                 bf_intern_moto
611                 extern_openjpeg
612                 extern_redcode
613                 ge_videotex
614                 bf_dna
615                 bf_blenfont
616                 bf_blentranslation
617                 bf_intern_audaspace
618                 bf_intern_mikktspace
619                 bf_intern_dualcon
620                 bf_intern_cycles
621                 cycles_render
622                 cycles_bvh
623                 cycles_device
624                 cycles_kernel
625                 cycles_util
626                 cycles_subd
627                 bf_intern_raskter
628                 bf_intern_opencolorio
629                 extern_rangetree
630                 extern_wcwidth
631                 extern_libmv
632                 extern_glog
633                 extern_sdlew
634                 extern_eigen3
635
636                 bf_intern_glew_mx
637         )
638
639         if(WITH_COMPOSITOR)
640                 # added for opencl compositor
641                 list_insert_before(BLENDER_SORTED_LIBS "bf_blenkernel" "bf_compositor")
642                 list_insert_after(BLENDER_SORTED_LIBS "bf_compositor" "bf_intern_opencl")
643         endif()
644
645         if(WITH_LIBMV)
646                 list(APPEND BLENDER_SORTED_LIBS extern_ceres)
647         endif()
648
649         if(WITH_MOD_CLOTH_ELTOPO)
650                 list(APPEND BLENDER_SORTED_LIBS extern_eltopo)
651         endif()
652
653         if(NOT WITH_SYSTEM_LZO)
654                 list(APPEND BLENDER_SORTED_LIBS extern_minilzo)
655         endif()
656
657         if(NOT WITH_SYSTEM_GLEW)
658                 list(APPEND BLENDER_SORTED_LIBS ${BLENDER_GLEW_LIBRARIES})
659         endif()
660
661         if(WITH_BINRELOC)
662                 list(APPEND BLENDER_SORTED_LIBS extern_binreloc)
663         endif()
664
665         if(WITH_CXX_GUARDEDALLOC)
666                 list(APPEND BLENDER_SORTED_LIBS bf_intern_guardedalloc_cpp)
667         endif()
668
669         if(WITH_IK_SOLVER)
670                 list_insert_after(BLENDER_SORTED_LIBS "bf_intern_elbeem" "bf_intern_iksolver")
671         endif()
672
673         if(WITH_IK_ITASC)
674                 list(APPEND BLENDER_SORTED_LIBS bf_intern_itasc)
675         endif()
676
677         if(WITH_CODEC_QUICKTIME)
678                 list(APPEND BLENDER_SORTED_LIBS bf_quicktime)
679         endif()
680
681         if(WITH_INPUT_NDOF)
682                 list(APPEND BLENDER_SORTED_LIBS bf_intern_ghostndof3dconnexion)
683         endif()
684         
685         if(WITH_MOD_BOOLEAN)
686                 list(APPEND BLENDER_SORTED_LIBS extern_carve)
687         endif()
688
689         if(WITH_GHOST_XDND)
690                 list(APPEND BLENDER_SORTED_LIBS extern_xdnd)
691         endif()
692
693         if(WITH_CYCLES_OSL)
694                 list_insert_after(BLENDER_SORTED_LIBS "cycles_kernel" "cycles_kernel_osl")
695         endif()
696
697         if(WITH_INTERNATIONAL)
698                 list(APPEND BLENDER_SORTED_LIBS bf_intern_locale)
699         endif()
700
701         if(WITH_OPENNL)
702                 list_insert_after(BLENDER_SORTED_LIBS "bf_render" "bf_intern_opennl")
703         endif()
704
705         if(WITH_BULLET)
706                 list_insert_after(BLENDER_SORTED_LIBS "bf_blenkernel" "bf_intern_rigidbody")
707         endif()
708
709         if(WITH_BULLET AND NOT WITH_SYSTEM_BULLET)
710                 list_insert_after(BLENDER_SORTED_LIBS "ge_logic_ngnetwork" "extern_bullet")
711         endif()
712
713         if(WITH_OPENSUBDIV)
714                 list(APPEND BLENDER_SORTED_LIBS bf_intern_opensubdiv)
715         endif()
716
717         foreach(SORTLIB ${BLENDER_SORTED_LIBS})
718                 set(REMLIB ${SORTLIB})
719                 foreach(SEARCHLIB ${BLENDER_LINK_LIBS})
720                         if(${SEARCHLIB} STREQUAL ${SORTLIB})
721                                 set(REMLIB "")
722                         endif()
723                 endforeach()
724                 if(REMLIB)
725                         # message(STATUS "Removing library ${REMLIB} from blender linking because: not configured")
726                         list(APPEND REM_MSG ${REMLIB})
727                         list(REMOVE_ITEM BLENDER_SORTED_LIBS ${REMLIB})
728                 endif()
729         endforeach()
730         if(REM_MSG)
731                 list(SORT REM_MSG)
732                 message(STATUS "Blender Skipping: (${REM_MSG})")
733         endif()
734
735
736         set(BLENDER_SORTED_LIBS ${BLENDER_SORTED_LIBS} PARENT_SCOPE)
737
738         # for top-level tests
739         set_property(GLOBAL PROPERTY BLENDER_SORTED_LIBS_PROP ${BLENDER_SORTED_LIBS})
740 endfunction()
741
742 macro(TEST_SSE_SUPPORT
743         _sse_flags
744         _sse2_flags)
745
746         include(CheckCSourceRuns)
747
748         # message(STATUS "Detecting SSE support")
749         if(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
750                 set(${_sse_flags} "-msse")
751                 set(${_sse2_flags} "-msse2")
752         elseif(MSVC)
753                 # x86_64 has this auto enabled
754                 if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
755                         set(${_sse_flags} "")
756                         set(${_sse2_flags} "")
757                 else()
758                         set(${_sse_flags} "/arch:SSE")
759                         set(${_sse2_flags} "/arch:SSE2")
760                 endif()
761         elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
762                 set(${_sse_flags} "")  # icc defaults to -msse
763                 set(${_sse2_flags} "-msse2")
764         else()
765                 message(WARNING "SSE flags for this compiler: '${CMAKE_C_COMPILER_ID}' not known")
766                 set(${_sse_flags})
767                 set(${_sse2_flags})
768         endif()
769
770         set(CMAKE_REQUIRED_FLAGS "${${_sse_flags}} ${${_sse2_flags}}")
771
772         if(NOT DEFINED SUPPORT_SSE_BUILD)
773                 # result cached
774                 check_c_source_runs("
775                         #include <xmmintrin.h>
776                         int main(void) { __m128 v = _mm_setzero_ps(); return 0; }"
777                 SUPPORT_SSE_BUILD)
778
779                 if(SUPPORT_SSE_BUILD)
780                         message(STATUS "SSE Support: detected.")
781                 else()
782                         message(STATUS "SSE Support: missing.")
783                 endif()
784         endif()
785
786         if(NOT DEFINED SUPPORT_SSE2_BUILD)
787                 # result cached
788                 check_c_source_runs("
789                         #include <emmintrin.h>
790                         int main(void) { __m128d v = _mm_setzero_pd(); return 0; }"
791                 SUPPORT_SSE2_BUILD)
792
793                 if(SUPPORT_SSE2_BUILD)
794                         message(STATUS "SSE2 Support: detected.")
795                 else()
796                         message(STATUS "SSE2 Support: missing.")
797                 endif()
798         endif()
799
800         unset(CMAKE_REQUIRED_FLAGS)
801 endmacro()
802
803 macro(TEST_STDBOOL_SUPPORT)
804         include(CheckCSourceRuns)
805
806         # This program will compile correctly if and only if
807         # this C compiler supports C99 stdbool.
808         check_c_source_runs("
809                 #include <stdbool.h>
810                 int main(void) { return (int)false; }"
811         HAVE_STDBOOL_H)
812 endmacro()
813
814 macro(TEST_UNORDERED_MAP_SUPPORT)
815         # - Detect unordered_map availability
816         # Test if a valid implementation of unordered_map exists
817         # and define the include path
818         # This module defines
819         #  HAVE_UNORDERED_MAP, whether unordered_map implementation was found
820         #  
821         #  HAVE_STD_UNORDERED_MAP_HEADER, <unordered_map.h> was found
822         #  HAVE_UNORDERED_MAP_IN_STD_NAMESPACE, unordered_map is in namespace std
823         #  HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE, unordered_map is in namespace std::tr1
824         #  
825         #  UNORDERED_MAP_INCLUDE_PREFIX, include path prefix for unordered_map, if found
826         #  UNORDERED_MAP_NAMESPACE, namespace for unordered_map, if found
827
828         include(CheckIncludeFileCXX)
829         CHECK_INCLUDE_FILE_CXX("unordered_map" HAVE_STD_UNORDERED_MAP_HEADER)
830         if(HAVE_STD_UNORDERED_MAP_HEADER)
831                 # Even so we've found unordered_map header file it doesn't
832                 # mean unordered_map and unordered_set will be declared in
833                 # std namespace.
834                 #
835                 # Namely, MSVC 2008 have unordered_map header which declares
836                 # unordered_map class in std::tr1 namespace. In order to support
837                 # this, we do extra check to see which exactly namespace is
838                 # to be used.
839
840                 include(CheckCXXSourceCompiles)
841                 CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
842                                           int main() {
843                                             std::unordered_map<int, int> map;
844                                             return 0;
845                                           }"
846                                           HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
847                 if(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
848                         message(STATUS "Found unordered_map/set in std namespace.")
849
850                         set(HAVE_UNORDERED_MAP "TRUE")
851                         set(UNORDERED_MAP_INCLUDE_PREFIX "")
852                         set(UNORDERED_MAP_NAMESPACE "std")
853                 else()
854                         CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
855                                                   int main() {
856                                                     std::tr1::unordered_map<int, int> map;
857                                                     return 0;
858                                                   }"
859                                                   HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
860                         if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
861                                 message(STATUS "Found unordered_map/set in std::tr1 namespace.")
862
863                                 set(HAVE_UNORDERED_MAP "TRUE")
864                                 set(UNORDERED_MAP_INCLUDE_PREFIX "")
865                                 set(UNORDERED_MAP_NAMESPACE "std::tr1")
866                         else()
867                                 message(STATUS "Found <unordered_map> but cannot find either std::unordered_map "
868                                         "or std::tr1::unordered_map.")
869                         endif()
870                 endif()
871         else()
872                 CHECK_INCLUDE_FILE_CXX("tr1/unordered_map" HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
873                 if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
874                         message(STATUS "Found unordered_map/set in std::tr1 namespace.")
875
876                         set(HAVE_UNORDERED_MAP "TRUE")
877                         set(UNORDERED_MAP_INCLUDE_PREFIX "tr1")
878                         set(UNORDERED_MAP_NAMESPACE "std::tr1")
879                 else()
880                         message(STATUS "Unable to find <unordered_map> or <tr1/unordered_map>. ")
881                 endif()
882         endif()
883 endmacro()
884
885 macro(TEST_SHARED_PTR_SUPPORT)
886         # This check are coming from Ceres library.
887         #
888         # Find shared pointer header and namespace.
889         #
890         # This module defines the following variables:
891         #
892         # SHARED_PTR_FOUND: TRUE if shared_ptr found.
893         # SHARED_PTR_TR1_MEMORY_HEADER: True if <tr1/memory> header is to be used
894         # for the shared_ptr object, otherwise use <memory>.
895         # SHARED_PTR_TR1_NAMESPACE: TRUE if shared_ptr is defined in std::tr1 namespace,
896         # otherwise it's assumed to be defined in std namespace.
897
898         include(CheckIncludeFileCXX)
899         set(SHARED_PTR_FOUND FALSE)
900         CHECK_INCLUDE_FILE_CXX(memory HAVE_STD_MEMORY_HEADER)
901         if(HAVE_STD_MEMORY_HEADER)
902                 # Finding the memory header doesn't mean that shared_ptr is in std
903                 # namespace.
904                 #
905                 # In particular, MSVC 2008 has shared_ptr declared in std::tr1.  In
906                 # order to support this, we do an extra check to see which namespace
907                 # should be used.
908                 include(CheckCXXSourceCompiles)
909                 CHECK_CXX_SOURCE_COMPILES("#include <memory>
910                                            int main() {
911                                              std::shared_ptr<int> int_ptr;
912                                              return 0;
913                                            }"
914                                           HAVE_SHARED_PTR_IN_STD_NAMESPACE)
915
916                 if(HAVE_SHARED_PTR_IN_STD_NAMESPACE)
917                         message("-- Found shared_ptr in std namespace using <memory> header.")
918                         set(SHARED_PTR_FOUND TRUE)
919                 else()
920                         CHECK_CXX_SOURCE_COMPILES("#include <memory>
921                                                    int main() {
922                                                    std::tr1::shared_ptr<int> int_ptr;
923                                                    return 0;
924                                                    }"
925                                                   HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
926                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
927                                 message("-- Found shared_ptr in std::tr1 namespace using <memory> header.")
928                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
929                                 set(SHARED_PTR_FOUND TRUE)
930                         endif()
931                 endif()
932         endif()
933
934         if(NOT SHARED_PTR_FOUND)
935                 # Further, gcc defines shared_ptr in std::tr1 namespace and
936                 # <tr1/memory> is to be included for this. And what makes things
937                 # even more tricky is that gcc does have <memory> header, so
938                 # all the checks above wouldn't find shared_ptr.
939                 CHECK_INCLUDE_FILE_CXX("tr1/memory" HAVE_TR1_MEMORY_HEADER)
940                 if(HAVE_TR1_MEMORY_HEADER)
941                         CHECK_CXX_SOURCE_COMPILES("#include <tr1/memory>
942                                                    int main() {
943                                                    std::tr1::shared_ptr<int> int_ptr;
944                                                    return 0;
945                                                    }"
946                                                    HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
947                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
948                                 message("-- Found shared_ptr in std::tr1 namespace using <tr1/memory> header.")
949                                 set(SHARED_PTR_TR1_MEMORY_HEADER TRUE)
950                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
951                                 set(SHARED_PTR_FOUND TRUE)
952                         endif()
953                 endif()
954         endif()
955 endmacro()
956
957 # when we have warnings as errors applied globally this
958 # needs to be removed for some external libs which we dont maintain.
959
960 # utility macro
961 macro(remove_cc_flag
962         _flag)
963
964         foreach(flag ${ARGV})
965                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
966                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
967                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
968                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
969                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
970
971                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
972                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
973                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
974                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
975                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
976         endforeach()
977         unset(flag)
978
979 endmacro()
980
981 macro(add_cc_flag
982         flag)
983
984         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
985         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
986 endmacro()
987
988 macro(remove_strict_flags)
989
990         if(CMAKE_COMPILER_IS_GNUCC)
991                 remove_cc_flag(
992                         "-Wstrict-prototypes"
993                         "-Wmissing-prototypes"
994                         "-Wmissing-format-attribute"
995                         "-Wunused-local-typedefs"
996                         "-Wunused-macros"
997                         "-Wunused-parameter"
998                         "-Wwrite-strings"
999                         "-Wredundant-decls"
1000                         "-Wundef"
1001                         "-Wshadow"
1002                         "-Wdouble-promotion"
1003                         "-Wold-style-definition"
1004                         "-Werror=[^ ]+"
1005                         "-Werror"
1006                 )
1007
1008                 # negate flags implied by '-Wall'
1009                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1010         endif()
1011
1012         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1013                 remove_cc_flag(
1014                         "-Wunused-parameter"
1015                         "-Wunused-variable"
1016                         "-Werror=[^ ]+"
1017                         "-Werror"
1018                 )
1019
1020                 # negate flags implied by '-Wall'
1021                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1022         endif()
1023
1024         if(MSVC)
1025                 # TODO
1026         endif()
1027
1028 endmacro()
1029
1030 macro(remove_extra_strict_flags)
1031         if(CMAKE_COMPILER_IS_GNUCC)
1032                 remove_cc_flag(
1033                         "-Wunused-parameter"
1034                 )
1035         endif()
1036
1037         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1038                 remove_cc_flag(
1039                         "-Wunused-parameter"
1040                 )
1041         endif()
1042
1043         if(MSVC)
1044                 # TODO
1045         endif()
1046 endmacro()
1047
1048 # note, we can only append flags on a single file so we need to negate the options.
1049 # at the moment we cant shut up ffmpeg deprecations, so use this, but will
1050 # probably add more removals here.
1051 macro(remove_strict_flags_file
1052         filenames)
1053
1054         foreach(_SOURCE ${ARGV})
1055
1056                 if(CMAKE_COMPILER_IS_GNUCC OR
1057                   (CMAKE_C_COMPILER_ID MATCHES "Clang"))
1058
1059                         set_source_files_properties(${_SOURCE}
1060                                 PROPERTIES
1061                                         COMPILE_FLAGS "${CC_REMOVE_STRICT_FLAGS}"
1062                         )
1063                 endif()
1064
1065                 if(MSVC)
1066                         # TODO
1067                 endif()
1068
1069         endforeach()
1070
1071         unset(_SOURCE)
1072
1073 endmacro()
1074
1075
1076 function(ADD_CHECK_C_COMPILER_FLAG
1077         _CFLAGS
1078         _CACHE_VAR
1079         _FLAG
1080         )
1081
1082         include(CheckCCompilerFlag)
1083
1084         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1085         if(${_CACHE_VAR})
1086                 # message(STATUS "Using CFLAG: ${_FLAG}")
1087                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}" PARENT_SCOPE)
1088         else()
1089                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
1090         endif()
1091 endfunction()
1092
1093 function(ADD_CHECK_CXX_COMPILER_FLAG
1094         _CXXFLAGS
1095         _CACHE_VAR
1096         _FLAG
1097         )
1098
1099         include(CheckCXXCompilerFlag)
1100
1101         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1102         if(${_CACHE_VAR})
1103                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
1104                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}" PARENT_SCOPE)
1105         else()
1106                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
1107         endif()
1108 endfunction()
1109
1110 function(get_blender_version)
1111         # extracts header vars and defines them in the parent scope:
1112         #
1113         # - BLENDER_VERSION (major.minor)
1114         # - BLENDER_VERSION_MAJOR
1115         # - BLENDER_VERSION_MINOR
1116         # - BLENDER_SUBVERSION (used for internal versioning mainly)
1117         # - BLENDER_VERSION_CHAR (a, b, c, ...or empty string)
1118         # - BLENDER_VERSION_CYCLE (alpha, beta, rc, release)
1119
1120         # So cmake depends on BKE_blender.h, beware of inf-loops!
1121         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h
1122                        ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
1123
1124         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
1125
1126         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
1127         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
1128         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
1129         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
1130
1131         if(NOT ${_out_version} MATCHES "[0-9]+")
1132                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
1133         endif()
1134
1135         if(NOT ${_out_subversion} MATCHES "[0-9]+")
1136                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
1137         endif()
1138
1139         # clumsy regex, only single char are ok but it could be unset
1140
1141         string(LENGTH "${_out_version_char}" _out_version_char_len)
1142         if(NOT _out_version_char_len EQUAL 1)
1143                 set(_out_version_char "")
1144         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
1145                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
1146         endif()
1147
1148         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
1149                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
1150         endif()
1151
1152         math(EXPR _out_version_major "${_out_version} / 100")
1153         math(EXPR _out_version_minor "${_out_version} % 100")
1154
1155         # for packaging, alpha to numbers
1156         string(COMPARE EQUAL "${_out_version_char}" "" _out_version_char_empty)
1157         if(${_out_version_char_empty})
1158                 set(_out_version_char_index "0")
1159         else()
1160                 set(_char_ls a b c d e f g h i j k l m n o p q r s t u v w x y z)
1161                 list(FIND _char_ls ${_out_version_char} _out_version_char_index)
1162                 math(EXPR _out_version_char_index "${_out_version_char_index} + 1")
1163         endif()
1164
1165         # output vars
1166         set(BLENDER_VERSION "${_out_version_major}.${_out_version_minor}" PARENT_SCOPE)
1167         set(BLENDER_VERSION_MAJOR "${_out_version_major}" PARENT_SCOPE)
1168         set(BLENDER_VERSION_MINOR "${_out_version_minor}" PARENT_SCOPE)
1169         set(BLENDER_SUBVERSION "${_out_subversion}" PARENT_SCOPE)
1170         set(BLENDER_VERSION_CHAR "${_out_version_char}" PARENT_SCOPE)
1171         set(BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index}" PARENT_SCOPE)
1172         set(BLENDER_VERSION_CYCLE "${_out_version_cycle}" PARENT_SCOPE)
1173
1174 endfunction()
1175
1176
1177 # hacks to override initial project settings
1178 # these macros must be called directly before/after project(Blender)
1179 macro(blender_project_hack_pre)
1180         # ----------------
1181         # MINGW HACK START
1182         # ignore system set flag, use our own
1183         # must be before project(...)
1184         # if the user wants to add their own its ok after first run.
1185         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
1186                 set(_reset_standard_libraries OFF)
1187         else()
1188                 set(_reset_standard_libraries ON)
1189         endif()
1190
1191         # ------------------
1192         # GCC -O3 HACK START
1193         # needed because O3 can cause problems but
1194         # allow the builder to set O3 manually after.
1195         if(DEFINED CMAKE_C_FLAGS_RELEASE)
1196                 set(_reset_standard_cflags_rel OFF)
1197         else()
1198                 set(_reset_standard_cflags_rel ON)
1199         endif()
1200         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
1201                 set(_reset_standard_cxxflags_rel OFF)
1202         else()
1203                 set(_reset_standard_cxxflags_rel ON)
1204         endif()
1205 endmacro()
1206
1207
1208 macro(blender_project_hack_post)
1209         # --------------
1210         # MINGW HACK END
1211         if(_reset_standard_libraries)
1212                 # Must come after projecINCt(...)
1213                 #
1214                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
1215                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
1216                 # with a meaningless stack trace. by overriding this flag we ensure we only
1217                 # have libs we define and that cmake & scons builds match.
1218                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1219                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1220                 mark_as_advanced(
1221                         CMAKE_C_STANDARD_LIBRARIES
1222                         CMAKE_CXX_STANDARD_LIBRARIES
1223                 )
1224         endif()
1225         unset(_reset_standard_libraries)
1226
1227
1228         # ----------------
1229         # GCC -O3 HACK END
1230         if(_reset_standard_cflags_rel)
1231                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
1232                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1233                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
1234         endif()
1235
1236         if(_reset_standard_cxxflags_rel)
1237                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
1238                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1239                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
1240         endif()
1241
1242         unset(_reset_standard_cflags_rel)
1243         unset(_reset_standard_cxxflags_rel)
1244
1245         # ------------------------------------------------------------------
1246         # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
1247         if(CMAKE_COMPILER_IS_GNUCC)
1248                 if(NOT DARWIN)
1249                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
1250                 endif()
1251         endif()
1252
1253 endmacro()
1254
1255 # pair of macros to allow libraries to be specify files to install, but to
1256 # only install them at the end so the directories don't get cleared with
1257 # the files in them. used by cycles to install addon.
1258 function(delayed_install
1259         base
1260         files
1261         destination)
1262
1263         foreach(f ${files})
1264                 if(IS_ABSOLUTE ${f})
1265                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${f})
1266                 else()
1267                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${base}/${f})
1268                 endif()
1269                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_DESTINATIONS ${destination})
1270         endforeach()
1271 endfunction()
1272
1273 # note this is a function instead of a macro so that ${BUILD_TYPE} in targetdir
1274 # does not get expanded in calling but is preserved
1275 function(delayed_do_install
1276         targetdir)
1277
1278         get_property(files GLOBAL PROPERTY DELAYED_INSTALL_FILES)
1279         get_property(destinations GLOBAL PROPERTY DELAYED_INSTALL_DESTINATIONS)
1280
1281         if(files)
1282                 list(LENGTH files n)
1283                 math(EXPR n "${n}-1")
1284
1285                 foreach(i RANGE ${n})
1286                         list(GET files ${i} f)
1287                         list(GET destinations ${i} d)
1288                         install(FILES ${f} DESTINATION ${targetdir}/${d})
1289                 endforeach()
1290         endif()
1291 endfunction()
1292
1293
1294 function(data_to_c
1295         file_from file_to
1296         list_to_add
1297         )
1298
1299         list(APPEND ${list_to_add} ${file_to})
1300         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1301
1302         get_filename_component(_file_to_path ${file_to} PATH)
1303
1304         add_custom_command(
1305                 OUTPUT ${file_to}
1306                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1307                 COMMAND "$<TARGET_FILE:datatoc>" ${file_from} ${file_to}
1308                 DEPENDS ${file_from} datatoc)
1309
1310         set_source_files_properties(${file_to} PROPERTIES GENERATED TRUE)
1311 endfunction()
1312
1313
1314 # same as above but generates the var name and output automatic.
1315 function(data_to_c_simple
1316         file_from
1317         list_to_add
1318         )
1319
1320         # remove ../'s
1321         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from}   REALPATH)
1322         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${file_from}.c REALPATH)
1323
1324         list(APPEND ${list_to_add} ${_file_to})
1325         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1326
1327         get_filename_component(_file_to_path ${_file_to} PATH)
1328
1329         add_custom_command(
1330                 OUTPUT  ${_file_to}
1331                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1332                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1333                 DEPENDS ${_file_from} datatoc)
1334
1335         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1336 endfunction()
1337
1338 # macro for converting pixmap directory to a png and then a c file
1339 function(data_to_c_simple_icons
1340         path_from
1341         list_to_add
1342         )
1343
1344         # Conversion steps
1345         #  path_from  ->  _file_from  ->  _file_to
1346         #  foo/*.dat  ->  foo.png     ->  foo.png.c
1347
1348         get_filename_component(_path_from_abs ${path_from} ABSOLUTE)
1349         # remove ../'s
1350         get_filename_component(_file_from ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png   REALPATH)
1351         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png.c REALPATH)
1352
1353         list(APPEND ${list_to_add} ${_file_to})
1354         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1355
1356         get_filename_component(_file_to_path ${_file_to} PATH)
1357
1358         # ideally we wouldn't glob, but storing all names for all pixmaps is a bit heavy
1359         file(GLOB _icon_files "${path_from}/*.dat")
1360
1361         add_custom_command(
1362                 OUTPUT  ${_file_from} ${_file_to}
1363                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1364                 #COMMAND python3 ${CMAKE_SOURCE_DIR}/source/blender/datatoc/datatoc_icon.py ${_path_from_abs} ${_file_from}
1365                 COMMAND "$<TARGET_FILE:datatoc_icon>" ${_path_from_abs} ${_file_from}
1366                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1367                 DEPENDS
1368                         ${_icon_files}
1369                         datatoc_icon
1370                         datatoc
1371                         # could be an arg but for now we only create icons depending on UI_icons.h
1372                         ${CMAKE_SOURCE_DIR}/source/blender/editors/include/UI_icons.h
1373                 )
1374
1375         set_source_files_properties(${_file_from} ${_file_to} PROPERTIES GENERATED TRUE)
1376 endfunction()
1377
1378 # XXX Not used for now...
1379 function(svg_to_png
1380         file_from
1381         file_to
1382         dpi
1383         list_to_add
1384         )
1385
1386         # remove ../'s
1387         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from} REALPATH)
1388         get_filename_component(_file_to   ${CMAKE_CURRENT_SOURCE_DIR}/${file_to}   REALPATH)
1389
1390         list(APPEND ${list_to_add} ${_file_to})
1391         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1392
1393         find_program(INKSCAPE_EXE inkscape)
1394         mark_as_advanced(INKSCAPE_EXE)
1395
1396         if(INKSCAPE_EXE)
1397                 if(APPLE)
1398                         # in OS X app bundle, the binary is a shim that doesn't take any
1399                         # command line arguments, replace it with the actual binary
1400                         string(REPLACE "MacOS/Inkscape" "Resources/bin/inkscape" INKSCAPE_REAL_EXE ${INKSCAPE_EXE})
1401                         if(EXISTS "${INKSCAPE_REAL_EXE}")
1402                                 set(INKSCAPE_EXE ${INKSCAPE_REAL_EXE})
1403                         endif()
1404                 endif()
1405
1406                 add_custom_command(
1407                         OUTPUT  ${_file_to}
1408                         COMMAND ${INKSCAPE_EXE} ${_file_from} --export-dpi=${dpi}  --without-gui --export-png=${_file_to}
1409                         DEPENDS ${_file_from} ${INKSCAPE_EXE}
1410                 )
1411         else()
1412                 message(WARNING "Inkscape not found, could not re-generate ${_file_to} from ${_file_from}!")
1413         endif()
1414 endfunction()
1415
1416 function(msgfmt_simple
1417         file_from
1418         list_to_add
1419         )
1420
1421         # remove ../'s
1422         get_filename_component(_file_from_we ${file_from} NAME_WE)
1423
1424         get_filename_component(_file_from ${file_from} REALPATH)
1425         get_filename_component(_file_to ${CMAKE_CURRENT_BINARY_DIR}/${_file_from_we}.mo REALPATH)
1426
1427         list(APPEND ${list_to_add} ${_file_to})
1428         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1429
1430         get_filename_component(_file_to_path ${_file_to} PATH)
1431
1432         add_custom_command(
1433                 OUTPUT  ${_file_to}
1434                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1435                 COMMAND "$<TARGET_FILE:msgfmt>" ${_file_from} ${_file_to}
1436                 DEPENDS msgfmt ${_file_from})
1437
1438         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1439 endfunction()
1440
1441 function(find_python_package
1442         package
1443         )
1444
1445         string(TOUPPER ${package} _upper_package)
1446
1447         # set but invalid
1448         if((NOT ${PYTHON_${_upper_package}_PATH} STREQUAL "") AND
1449            (NOT ${PYTHON_${_upper_package}_PATH} MATCHES NOTFOUND))
1450 #               if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}/${package}")
1451 #                       message(WARNING "PYTHON_${_upper_package}_PATH is invalid, ${package} not found in '${PYTHON_${_upper_package}_PATH}' "
1452 #                                       "WITH_PYTHON_INSTALL_${_upper_package} option will be ignored when installing python")
1453 #                       set(WITH_PYTHON_INSTALL${_upper_package} OFF)
1454 #               endif()
1455         # not set, so initialize
1456         else()
1457                 string(REPLACE "." ";" _PY_VER_SPLIT "${PYTHON_VERSION}")
1458                 list(GET _PY_VER_SPLIT 0 _PY_VER_MAJOR)
1459
1460                 # re-cache
1461                 unset(PYTHON_${_upper_package}_PATH CACHE)
1462                 find_path(PYTHON_${_upper_package}_PATH
1463                   NAMES
1464                     ${package}
1465                   HINTS
1466                     "${PYTHON_LIBPATH}/python${PYTHON_VERSION}/"
1467                     "${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/"
1468                   PATH_SUFFIXES
1469                     site-packages
1470                     dist-packages
1471                    NO_DEFAULT_PATH
1472                 )
1473
1474                  if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}")
1475                         message(WARNING "'${package}' path could not be found in:\n"
1476                                         "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/site-packages/${package}', "
1477                                         "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/site-packages/${package}', "
1478                                         "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/dist-packages/${package}', "
1479                                         "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/dist-packages/${package}', "
1480                                         "WITH_PYTHON_INSTALL_${_upper_package} option will be ignored when installing python")
1481                         set(WITH_PYTHON_INSTALL_${_upper_package} OFF PARENT_SCOPE)
1482                 else()
1483                         message(STATUS "${package} found at '${PYTHON_${_upper_package}_PATH}'")
1484                 endif()
1485           endif()
1486 endfunction()
1487
1488 # like Python's 'print(dir())'
1489 function(print_all_vars)
1490         get_cmake_property(_vars VARIABLES)
1491         foreach(_var ${_vars})
1492                 message("${_var}=${${_var}}")
1493         endforeach()
1494 endfunction()