Cleanup: use enum constant for DNA comparison
[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                 if(WITH_CUDA_DYNLOAD)
482                         target_link_libraries(${target} "extern_cuew")
483                 else()
484                         target_link_libraries(${target} ${CUDA_CUDA_LIBRARY})
485                 endif()
486         endif()
487
488         #system libraries with no dependencies such as platform link libs or opengl should go last
489         target_link_libraries(${target}
490                         ${BLENDER_GL_LIBRARIES})
491
492         target_link_libraries(${target} ${PLATFORM_LINKLIBS} ${CMAKE_DL_LIBS})
493 endfunction()
494
495
496 function(SETUP_BLENDER_SORTED_LIBS)
497
498         get_property(BLENDER_LINK_LIBS GLOBAL PROPERTY BLENDER_LINK_LIBS)
499
500         list(APPEND BLENDER_LINK_LIBS
501                 bf_windowmanager
502                 bf_render
503         )
504
505         if(WITH_MOD_FLUID)
506                 list(APPEND BLENDER_LINK_LIBS bf_intern_elbeem)
507         endif()
508
509         if(WITH_CYCLES)
510                 list(APPEND BLENDER_LINK_LIBS
511                         cycles_render
512                         cycles_bvh
513                         cycles_device
514                         cycles_kernel
515                         cycles_util
516                         cycles_subd)
517                 if(WITH_CYCLES_OSL)
518                         list(APPEND BLENDER_LINK_LIBS cycles_kernel_osl)
519                 endif()
520         endif()
521
522         # Sort libraries
523         set(BLENDER_SORTED_LIBS
524                 bf_windowmanager
525
526                 bf_editor_space_api
527                 bf_editor_space_action
528                 bf_editor_space_buttons
529                 bf_editor_space_console
530                 bf_editor_space_file
531                 bf_editor_space_graph
532                 bf_editor_space_image
533                 bf_editor_space_info
534                 bf_editor_space_logic
535                 bf_editor_space_nla
536                 bf_editor_space_node
537                 bf_editor_space_outliner
538                 bf_editor_space_script
539                 bf_editor_space_sequencer
540                 bf_editor_space_text
541                 bf_editor_space_time
542                 bf_editor_space_userpref
543                 bf_editor_space_view3d
544                 bf_editor_space_clip
545
546                 bf_editor_transform
547                 bf_editor_util
548                 bf_editor_uvedit
549                 bf_editor_curve
550                 bf_editor_gpencil
551                 bf_editor_interface
552                 bf_editor_mesh
553                 bf_editor_metaball
554                 bf_editor_object
555                 bf_editor_armature
556                 bf_editor_physics
557                 bf_editor_render
558                 bf_editor_screen
559                 bf_editor_sculpt_paint
560                 bf_editor_sound
561                 bf_editor_animation
562                 bf_editor_datafiles
563                 bf_editor_mask
564                 bf_editor_io
565
566                 bf_render
567                 bf_python
568                 bf_python_ext
569                 bf_python_mathutils
570                 bf_python_bmesh
571                 bf_freestyle
572                 bf_ikplugin
573                 bf_modifiers
574                 bf_bmesh
575                 bf_gpu
576                 bf_blenkernel
577                 bf_physics
578                 bf_nodes
579                 bf_rna
580                 bf_blenloader
581                 bf_imbuf
582                 bf_blenlib
583                 bf_depsgraph
584                 bf_intern_ghost
585                 bf_intern_string
586                 bf_avi
587                 bf_imbuf_cineon
588                 bf_imbuf_openexr
589                 bf_imbuf_openimageio
590                 bf_imbuf_dds
591                 bf_collada
592                 bf_intern_elbeem
593                 bf_intern_memutil
594                 bf_intern_guardedalloc
595                 bf_intern_ctr
596                 bf_intern_utfconv
597                 ge_blen_routines
598                 ge_converter
599                 ge_phys_dummy
600                 ge_phys_bullet
601                 bf_intern_smoke
602                 extern_lzma
603                 ge_logic_ketsji
604                 extern_recastnavigation
605                 ge_logic
606                 ge_rasterizer
607                 ge_oglrasterizer
608                 ge_logic_expressions
609                 ge_scenegraph
610                 ge_logic_network
611                 ge_logic_ngnetwork
612                 ge_logic_loopbacknetwork
613                 bf_intern_moto
614                 extern_openjpeg
615                 extern_redcode
616                 ge_videotex
617                 bf_dna
618                 bf_blenfont
619                 bf_blentranslation
620                 bf_intern_audaspace
621                 bf_intern_mikktspace
622                 bf_intern_dualcon
623                 bf_intern_cycles
624                 cycles_render
625                 cycles_bvh
626                 cycles_device
627                 cycles_kernel
628                 cycles_util
629                 cycles_subd
630                 bf_intern_opencolorio
631                 bf_intern_eigen
632                 extern_rangetree
633                 extern_wcwidth
634                 bf_intern_libmv
635                 extern_glog
636                 extern_gflags
637                 extern_sdlew
638
639                 bf_intern_glew_mx
640         )
641
642         if(WITH_COMPOSITOR)
643                 # added for opencl compositor
644                 list_insert_before(BLENDER_SORTED_LIBS "bf_blenkernel" "bf_compositor")
645                 list_insert_after(BLENDER_SORTED_LIBS "bf_compositor" "bf_intern_opencl")
646         endif()
647
648         if(WITH_LIBMV)
649                 list(APPEND BLENDER_SORTED_LIBS extern_ceres)
650         endif()
651
652         if(WITH_MOD_CLOTH_ELTOPO)
653                 list(APPEND BLENDER_SORTED_LIBS extern_eltopo)
654         endif()
655
656         if(NOT WITH_SYSTEM_LZO)
657                 list(APPEND BLENDER_SORTED_LIBS extern_minilzo)
658         endif()
659
660         if(NOT WITH_SYSTEM_GLEW)
661                 list(APPEND BLENDER_SORTED_LIBS ${BLENDER_GLEW_LIBRARIES})
662         endif()
663
664         if(WITH_BINRELOC)
665                 list(APPEND BLENDER_SORTED_LIBS extern_binreloc)
666         endif()
667
668         if(WITH_CXX_GUARDEDALLOC)
669                 list(APPEND BLENDER_SORTED_LIBS bf_intern_guardedalloc_cpp)
670         endif()
671
672         if(WITH_IK_SOLVER)
673                 list_insert_after(BLENDER_SORTED_LIBS "bf_intern_elbeem" "bf_intern_iksolver")
674         endif()
675
676         if(WITH_IK_ITASC)
677                 list(APPEND BLENDER_SORTED_LIBS bf_intern_itasc)
678         endif()
679
680         if(WITH_CODEC_QUICKTIME)
681                 list(APPEND BLENDER_SORTED_LIBS bf_quicktime)
682         endif()
683
684         if(WITH_INPUT_NDOF)
685                 list(APPEND BLENDER_SORTED_LIBS bf_intern_ghostndof3dconnexion)
686         endif()
687         
688         if(WITH_MOD_BOOLEAN)
689                 list(APPEND BLENDER_SORTED_LIBS extern_carve)
690         endif()
691
692         if(WITH_GHOST_XDND)
693                 list(APPEND BLENDER_SORTED_LIBS extern_xdnd)
694         endif()
695
696         if(WITH_CYCLES_OSL)
697                 list_insert_after(BLENDER_SORTED_LIBS "cycles_kernel" "cycles_kernel_osl")
698         endif()
699
700         if(WITH_INTERNATIONAL)
701                 list(APPEND BLENDER_SORTED_LIBS bf_intern_locale)
702         endif()
703
704         if(WITH_BULLET)
705                 list_insert_after(BLENDER_SORTED_LIBS "bf_blenkernel" "bf_intern_rigidbody")
706         endif()
707
708         if(WITH_BULLET AND NOT WITH_SYSTEM_BULLET)
709                 list_insert_after(BLENDER_SORTED_LIBS "ge_logic_ngnetwork" "extern_bullet")
710         endif()
711
712         if(WITH_OPENSUBDIV)
713                 list(APPEND BLENDER_SORTED_LIBS bf_intern_opensubdiv)
714         endif()
715
716         foreach(SORTLIB ${BLENDER_SORTED_LIBS})
717                 set(REMLIB ${SORTLIB})
718                 foreach(SEARCHLIB ${BLENDER_LINK_LIBS})
719                         if(${SEARCHLIB} STREQUAL ${SORTLIB})
720                                 set(REMLIB "")
721                         endif()
722                 endforeach()
723                 if(REMLIB)
724                         # message(STATUS "Removing library ${REMLIB} from blender linking because: not configured")
725                         list(APPEND REM_MSG ${REMLIB})
726                         list(REMOVE_ITEM BLENDER_SORTED_LIBS ${REMLIB})
727                 endif()
728         endforeach()
729         if(REM_MSG)
730                 list(SORT REM_MSG)
731                 message(STATUS "Blender Skipping: (${REM_MSG})")
732         endif()
733
734
735         set(BLENDER_SORTED_LIBS ${BLENDER_SORTED_LIBS} PARENT_SCOPE)
736
737         # for top-level tests
738         set_property(GLOBAL PROPERTY BLENDER_SORTED_LIBS_PROP ${BLENDER_SORTED_LIBS})
739 endfunction()
740
741 macro(TEST_SSE_SUPPORT
742         _sse_flags
743         _sse2_flags)
744
745         include(CheckCSourceRuns)
746
747         # message(STATUS "Detecting SSE support")
748         if(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
749                 set(${_sse_flags} "-msse")
750                 set(${_sse2_flags} "-msse2")
751         elseif(MSVC)
752                 # x86_64 has this auto enabled
753                 if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
754                         set(${_sse_flags} "")
755                         set(${_sse2_flags} "")
756                 else()
757                         set(${_sse_flags} "/arch:SSE")
758                         set(${_sse2_flags} "/arch:SSE2")
759                 endif()
760         elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
761                 set(${_sse_flags} "")  # icc defaults to -msse
762                 set(${_sse2_flags} "-msse2")
763         else()
764                 message(WARNING "SSE flags for this compiler: '${CMAKE_C_COMPILER_ID}' not known")
765                 set(${_sse_flags})
766                 set(${_sse2_flags})
767         endif()
768
769         set(CMAKE_REQUIRED_FLAGS "${${_sse_flags}} ${${_sse2_flags}}")
770
771         if(NOT DEFINED SUPPORT_SSE_BUILD)
772                 # result cached
773                 check_c_source_runs("
774                         #include <xmmintrin.h>
775                         int main(void) { __m128 v = _mm_setzero_ps(); return 0; }"
776                 SUPPORT_SSE_BUILD)
777
778                 if(SUPPORT_SSE_BUILD)
779                         message(STATUS "SSE Support: detected.")
780                 else()
781                         message(STATUS "SSE Support: missing.")
782                 endif()
783         endif()
784
785         if(NOT DEFINED SUPPORT_SSE2_BUILD)
786                 # result cached
787                 check_c_source_runs("
788                         #include <emmintrin.h>
789                         int main(void) { __m128d v = _mm_setzero_pd(); return 0; }"
790                 SUPPORT_SSE2_BUILD)
791
792                 if(SUPPORT_SSE2_BUILD)
793                         message(STATUS "SSE2 Support: detected.")
794                 else()
795                         message(STATUS "SSE2 Support: missing.")
796                 endif()
797         endif()
798
799         unset(CMAKE_REQUIRED_FLAGS)
800 endmacro()
801
802 macro(TEST_STDBOOL_SUPPORT)
803         include(CheckCSourceRuns)
804
805         # This program will compile correctly if and only if
806         # this C compiler supports C99 stdbool.
807         check_c_source_runs("
808                 #include <stdbool.h>
809                 int main(void) { return (int)false; }"
810         HAVE_STDBOOL_H)
811 endmacro()
812
813 # Only print message if running CMake first time
814 macro(message_first_run)
815         if(FIRST_RUN)
816                 message(${ARGV})
817         endif()
818 endmacro()
819
820 macro(TEST_UNORDERED_MAP_SUPPORT)
821         # - Detect unordered_map availability
822         # Test if a valid implementation of unordered_map exists
823         # and define the include path
824         # This module defines
825         #  HAVE_UNORDERED_MAP, whether unordered_map implementation was found
826         #
827         #  HAVE_STD_UNORDERED_MAP_HEADER, <unordered_map.h> was found
828         #  HAVE_UNORDERED_MAP_IN_STD_NAMESPACE, unordered_map is in namespace std
829         #  HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE, unordered_map is in namespace std::tr1
830         #
831         #  UNORDERED_MAP_INCLUDE_PREFIX, include path prefix for unordered_map, if found
832         #  UNORDERED_MAP_NAMESPACE, namespace for unordered_map, if found
833
834         include(CheckIncludeFileCXX)
835         CHECK_INCLUDE_FILE_CXX("unordered_map" HAVE_STD_UNORDERED_MAP_HEADER)
836         if(HAVE_STD_UNORDERED_MAP_HEADER)
837                 # Even so we've found unordered_map header file it doesn't
838                 # mean unordered_map and unordered_set will be declared in
839                 # std namespace.
840                 #
841                 # Namely, MSVC 2008 have unordered_map header which declares
842                 # unordered_map class in std::tr1 namespace. In order to support
843                 # this, we do extra check to see which exactly namespace is
844                 # to be used.
845
846                 include(CheckCXXSourceCompiles)
847                 CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
848                                           int main() {
849                                             std::unordered_map<int, int> map;
850                                             return 0;
851                                           }"
852                                           HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
853                 if(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
854                         message_first_run(STATUS "Found unordered_map/set in std namespace.")
855
856                         set(HAVE_UNORDERED_MAP "TRUE")
857                         set(UNORDERED_MAP_INCLUDE_PREFIX "")
858                         set(UNORDERED_MAP_NAMESPACE "std")
859                 else()
860                         CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
861                                                   int main() {
862                                                     std::tr1::unordered_map<int, int> map;
863                                                     return 0;
864                                                   }"
865                                                   HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
866                         if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
867                                 message_first_run(STATUS "Found unordered_map/set in std::tr1 namespace.")
868
869                                 set(HAVE_UNORDERED_MAP "TRUE")
870                                 set(UNORDERED_MAP_INCLUDE_PREFIX "")
871                                 set(UNORDERED_MAP_NAMESPACE "std::tr1")
872                         else()
873                                 message_first_run(STATUS "Found <unordered_map> but cannot find either std::unordered_map "
874                                                   "or std::tr1::unordered_map.")
875                         endif()
876                 endif()
877         else()
878                 CHECK_INCLUDE_FILE_CXX("tr1/unordered_map" HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
879                 if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
880                         message_first_run(STATUS "Found unordered_map/set in std::tr1 namespace.")
881
882                         set(HAVE_UNORDERED_MAP "TRUE")
883                         set(UNORDERED_MAP_INCLUDE_PREFIX "tr1")
884                         set(UNORDERED_MAP_NAMESPACE "std::tr1")
885                 else()
886                         message_first_run(STATUS "Unable to find <unordered_map> or <tr1/unordered_map>. ")
887                 endif()
888         endif()
889 endmacro()
890
891 macro(TEST_SHARED_PTR_SUPPORT)
892         # This check are coming from Ceres library.
893         #
894         # Find shared pointer header and namespace.
895         #
896         # This module defines the following variables:
897         #
898         # SHARED_PTR_FOUND: TRUE if shared_ptr found.
899         # SHARED_PTR_TR1_MEMORY_HEADER: True if <tr1/memory> header is to be used
900         # for the shared_ptr object, otherwise use <memory>.
901         # SHARED_PTR_TR1_NAMESPACE: TRUE if shared_ptr is defined in std::tr1 namespace,
902         # otherwise it's assumed to be defined in std namespace.
903
904         include(CheckIncludeFileCXX)
905         set(SHARED_PTR_FOUND FALSE)
906         CHECK_INCLUDE_FILE_CXX(memory HAVE_STD_MEMORY_HEADER)
907         if(HAVE_STD_MEMORY_HEADER)
908                 # Finding the memory header doesn't mean that shared_ptr is in std
909                 # namespace.
910                 #
911                 # In particular, MSVC 2008 has shared_ptr declared in std::tr1.  In
912                 # order to support this, we do an extra check to see which namespace
913                 # should be used.
914                 include(CheckCXXSourceCompiles)
915                 CHECK_CXX_SOURCE_COMPILES("#include <memory>
916                                            int main() {
917                                              std::shared_ptr<int> int_ptr;
918                                              return 0;
919                                            }"
920                                           HAVE_SHARED_PTR_IN_STD_NAMESPACE)
921
922                 if(HAVE_SHARED_PTR_IN_STD_NAMESPACE)
923                         message_first_run("-- Found shared_ptr in std namespace using <memory> header.")
924                         set(SHARED_PTR_FOUND TRUE)
925                 else()
926                         CHECK_CXX_SOURCE_COMPILES("#include <memory>
927                                                    int main() {
928                                                    std::tr1::shared_ptr<int> int_ptr;
929                                                    return 0;
930                                                    }"
931                                                   HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
932                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
933                                 message_first_run("-- Found shared_ptr in std::tr1 namespace using <memory> header.")
934                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
935                                 set(SHARED_PTR_FOUND TRUE)
936                         endif()
937                 endif()
938         endif()
939
940         if(NOT SHARED_PTR_FOUND)
941                 # Further, gcc defines shared_ptr in std::tr1 namespace and
942                 # <tr1/memory> is to be included for this. And what makes things
943                 # even more tricky is that gcc does have <memory> header, so
944                 # all the checks above wouldn't find shared_ptr.
945                 CHECK_INCLUDE_FILE_CXX("tr1/memory" HAVE_TR1_MEMORY_HEADER)
946                 if(HAVE_TR1_MEMORY_HEADER)
947                         CHECK_CXX_SOURCE_COMPILES("#include <tr1/memory>
948                                                    int main() {
949                                                    std::tr1::shared_ptr<int> int_ptr;
950                                                    return 0;
951                                                    }"
952                                                    HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
953                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
954                                 message_first_run("-- Found shared_ptr in std::tr1 namespace using <tr1/memory> header.")
955                                 set(SHARED_PTR_TR1_MEMORY_HEADER TRUE)
956                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
957                                 set(SHARED_PTR_FOUND TRUE)
958                         endif()
959                 endif()
960         endif()
961 endmacro()
962
963 # when we have warnings as errors applied globally this
964 # needs to be removed for some external libs which we dont maintain.
965
966 # utility macro
967 macro(remove_cc_flag
968         _flag)
969
970         foreach(flag ${ARGV})
971                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
972                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
973                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
974                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
975                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
976
977                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
978                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
979                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
980                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
981                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
982         endforeach()
983         unset(flag)
984
985 endmacro()
986
987 macro(add_cc_flag
988         flag)
989
990         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
991         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
992 endmacro()
993
994 macro(remove_strict_flags)
995
996         if(CMAKE_COMPILER_IS_GNUCC)
997                 remove_cc_flag(
998                         "-Wstrict-prototypes"
999                         "-Wmissing-prototypes"
1000                         "-Wmissing-format-attribute"
1001                         "-Wunused-local-typedefs"
1002                         "-Wunused-macros"
1003                         "-Wunused-parameter"
1004                         "-Wwrite-strings"
1005                         "-Wredundant-decls"
1006                         "-Wundef"
1007                         "-Wshadow"
1008                         "-Wdouble-promotion"
1009                         "-Wold-style-definition"
1010                         "-Werror=[^ ]+"
1011                         "-Werror"
1012                 )
1013
1014                 # negate flags implied by '-Wall'
1015                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1016         endif()
1017
1018         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1019                 remove_cc_flag(
1020                         "-Wunused-parameter"
1021                         "-Wunused-variable"
1022                         "-Werror=[^ ]+"
1023                         "-Werror"
1024                 )
1025
1026                 # negate flags implied by '-Wall'
1027                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1028         endif()
1029
1030         if(MSVC)
1031                 # TODO
1032         endif()
1033
1034 endmacro()
1035
1036 macro(remove_extra_strict_flags)
1037         if(CMAKE_COMPILER_IS_GNUCC)
1038                 remove_cc_flag(
1039                         "-Wunused-parameter"
1040                 )
1041         endif()
1042
1043         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1044                 remove_cc_flag(
1045                         "-Wunused-parameter"
1046                 )
1047         endif()
1048
1049         if(MSVC)
1050                 # TODO
1051         endif()
1052 endmacro()
1053
1054 # note, we can only append flags on a single file so we need to negate the options.
1055 # at the moment we cant shut up ffmpeg deprecations, so use this, but will
1056 # probably add more removals here.
1057 macro(remove_strict_flags_file
1058         filenames)
1059
1060         foreach(_SOURCE ${ARGV})
1061
1062                 if(CMAKE_COMPILER_IS_GNUCC OR
1063                   (CMAKE_C_COMPILER_ID MATCHES "Clang"))
1064
1065                         set_source_files_properties(${_SOURCE}
1066                                 PROPERTIES
1067                                         COMPILE_FLAGS "${CC_REMOVE_STRICT_FLAGS}"
1068                         )
1069                 endif()
1070
1071                 if(MSVC)
1072                         # TODO
1073                 endif()
1074
1075         endforeach()
1076
1077         unset(_SOURCE)
1078
1079 endmacro()
1080
1081
1082 function(ADD_CHECK_C_COMPILER_FLAG
1083         _CFLAGS
1084         _CACHE_VAR
1085         _FLAG
1086         )
1087
1088         include(CheckCCompilerFlag)
1089
1090         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1091         if(${_CACHE_VAR})
1092                 # message(STATUS "Using CFLAG: ${_FLAG}")
1093                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}" PARENT_SCOPE)
1094         else()
1095                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
1096         endif()
1097 endfunction()
1098
1099 function(ADD_CHECK_CXX_COMPILER_FLAG
1100         _CXXFLAGS
1101         _CACHE_VAR
1102         _FLAG
1103         )
1104
1105         include(CheckCXXCompilerFlag)
1106
1107         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1108         if(${_CACHE_VAR})
1109                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
1110                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}" PARENT_SCOPE)
1111         else()
1112                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
1113         endif()
1114 endfunction()
1115
1116 function(get_blender_version)
1117         # extracts header vars and defines them in the parent scope:
1118         #
1119         # - BLENDER_VERSION (major.minor)
1120         # - BLENDER_VERSION_MAJOR
1121         # - BLENDER_VERSION_MINOR
1122         # - BLENDER_SUBVERSION (used for internal versioning mainly)
1123         # - BLENDER_VERSION_CHAR (a, b, c, ...or empty string)
1124         # - BLENDER_VERSION_CYCLE (alpha, beta, rc, release)
1125
1126         # So cmake depends on BKE_blender.h, beware of inf-loops!
1127         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h
1128                        ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
1129
1130         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
1131
1132         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
1133         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
1134         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
1135         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
1136
1137         if(NOT ${_out_version} MATCHES "[0-9]+")
1138                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
1139         endif()
1140
1141         if(NOT ${_out_subversion} MATCHES "[0-9]+")
1142                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
1143         endif()
1144
1145         # clumsy regex, only single char are ok but it could be unset
1146
1147         string(LENGTH "${_out_version_char}" _out_version_char_len)
1148         if(NOT _out_version_char_len EQUAL 1)
1149                 set(_out_version_char "")
1150         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
1151                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
1152         endif()
1153
1154         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
1155                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
1156         endif()
1157
1158         math(EXPR _out_version_major "${_out_version} / 100")
1159         math(EXPR _out_version_minor "${_out_version} % 100")
1160
1161         # for packaging, alpha to numbers
1162         string(COMPARE EQUAL "${_out_version_char}" "" _out_version_char_empty)
1163         if(${_out_version_char_empty})
1164                 set(_out_version_char_index "0")
1165         else()
1166                 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)
1167                 list(FIND _char_ls ${_out_version_char} _out_version_char_index)
1168                 math(EXPR _out_version_char_index "${_out_version_char_index} + 1")
1169         endif()
1170
1171         # output vars
1172         set(BLENDER_VERSION "${_out_version_major}.${_out_version_minor}" PARENT_SCOPE)
1173         set(BLENDER_VERSION_MAJOR "${_out_version_major}" PARENT_SCOPE)
1174         set(BLENDER_VERSION_MINOR "${_out_version_minor}" PARENT_SCOPE)
1175         set(BLENDER_SUBVERSION "${_out_subversion}" PARENT_SCOPE)
1176         set(BLENDER_VERSION_CHAR "${_out_version_char}" PARENT_SCOPE)
1177         set(BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index}" PARENT_SCOPE)
1178         set(BLENDER_VERSION_CYCLE "${_out_version_cycle}" PARENT_SCOPE)
1179
1180 endfunction()
1181
1182
1183 # hacks to override initial project settings
1184 # these macros must be called directly before/after project(Blender)
1185 macro(blender_project_hack_pre)
1186         # ----------------
1187         # MINGW HACK START
1188         # ignore system set flag, use our own
1189         # must be before project(...)
1190         # if the user wants to add their own its ok after first run.
1191         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
1192                 set(_reset_standard_libraries OFF)
1193         else()
1194                 set(_reset_standard_libraries ON)
1195         endif()
1196
1197         # ------------------
1198         # GCC -O3 HACK START
1199         # needed because O3 can cause problems but
1200         # allow the builder to set O3 manually after.
1201         if(DEFINED CMAKE_C_FLAGS_RELEASE)
1202                 set(_reset_standard_cflags_rel OFF)
1203         else()
1204                 set(_reset_standard_cflags_rel ON)
1205         endif()
1206         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
1207                 set(_reset_standard_cxxflags_rel OFF)
1208         else()
1209                 set(_reset_standard_cxxflags_rel ON)
1210         endif()
1211 endmacro()
1212
1213
1214 macro(blender_project_hack_post)
1215         # --------------
1216         # MINGW HACK END
1217         if(_reset_standard_libraries)
1218                 # Must come after projecINCt(...)
1219                 #
1220                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
1221                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
1222                 # with a meaningless stack trace. by overriding this flag we ensure we only
1223                 # have libs we define.
1224                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1225                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1226                 mark_as_advanced(
1227                         CMAKE_C_STANDARD_LIBRARIES
1228                         CMAKE_CXX_STANDARD_LIBRARIES
1229                 )
1230         endif()
1231         unset(_reset_standard_libraries)
1232
1233
1234         # ----------------
1235         # GCC -O3 HACK END
1236         if(_reset_standard_cflags_rel)
1237                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
1238                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1239                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
1240         endif()
1241
1242         if(_reset_standard_cxxflags_rel)
1243                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
1244                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1245                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
1246         endif()
1247
1248         unset(_reset_standard_cflags_rel)
1249         unset(_reset_standard_cxxflags_rel)
1250
1251         # ------------------------------------------------------------------
1252         # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
1253         if(CMAKE_COMPILER_IS_GNUCC)
1254                 if(NOT DARWIN)
1255                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
1256                 endif()
1257         endif()
1258
1259 endmacro()
1260
1261 # pair of macros to allow libraries to be specify files to install, but to
1262 # only install them at the end so the directories don't get cleared with
1263 # the files in them. used by cycles to install addon.
1264 function(delayed_install
1265         base
1266         files
1267         destination)
1268
1269         foreach(f ${files})
1270                 if(IS_ABSOLUTE ${f})
1271                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${f})
1272                 else()
1273                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${base}/${f})
1274                 endif()
1275                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_DESTINATIONS ${destination})
1276         endforeach()
1277 endfunction()
1278
1279 # note this is a function instead of a macro so that ${BUILD_TYPE} in targetdir
1280 # does not get expanded in calling but is preserved
1281 function(delayed_do_install
1282         targetdir)
1283
1284         get_property(files GLOBAL PROPERTY DELAYED_INSTALL_FILES)
1285         get_property(destinations GLOBAL PROPERTY DELAYED_INSTALL_DESTINATIONS)
1286
1287         if(files)
1288                 list(LENGTH files n)
1289                 math(EXPR n "${n}-1")
1290
1291                 foreach(i RANGE ${n})
1292                         list(GET files ${i} f)
1293                         list(GET destinations ${i} d)
1294                         install(FILES ${f} DESTINATION ${targetdir}/${d})
1295                 endforeach()
1296         endif()
1297 endfunction()
1298
1299
1300 function(data_to_c
1301         file_from file_to
1302         list_to_add
1303         )
1304
1305         list(APPEND ${list_to_add} ${file_to})
1306         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1307
1308         get_filename_component(_file_to_path ${file_to} PATH)
1309
1310         add_custom_command(
1311                 OUTPUT ${file_to}
1312                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1313                 COMMAND "$<TARGET_FILE:datatoc>" ${file_from} ${file_to}
1314                 DEPENDS ${file_from} datatoc)
1315
1316         set_source_files_properties(${file_to} PROPERTIES GENERATED TRUE)
1317 endfunction()
1318
1319
1320 # same as above but generates the var name and output automatic.
1321 function(data_to_c_simple
1322         file_from
1323         list_to_add
1324         )
1325
1326         # remove ../'s
1327         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from}   REALPATH)
1328         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${file_from}.c REALPATH)
1329
1330         list(APPEND ${list_to_add} ${_file_to})
1331         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1332
1333         get_filename_component(_file_to_path ${_file_to} PATH)
1334
1335         add_custom_command(
1336                 OUTPUT  ${_file_to}
1337                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1338                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1339                 DEPENDS ${_file_from} datatoc)
1340
1341         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1342 endfunction()
1343
1344 # macro for converting pixmap directory to a png and then a c file
1345 function(data_to_c_simple_icons
1346         path_from
1347         list_to_add
1348         )
1349
1350         # Conversion steps
1351         #  path_from  ->  _file_from  ->  _file_to
1352         #  foo/*.dat  ->  foo.png     ->  foo.png.c
1353
1354         get_filename_component(_path_from_abs ${path_from} ABSOLUTE)
1355         # remove ../'s
1356         get_filename_component(_file_from ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png   REALPATH)
1357         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png.c REALPATH)
1358
1359         list(APPEND ${list_to_add} ${_file_to})
1360         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1361
1362         get_filename_component(_file_to_path ${_file_to} PATH)
1363
1364         # ideally we wouldn't glob, but storing all names for all pixmaps is a bit heavy
1365         file(GLOB _icon_files "${path_from}/*.dat")
1366
1367         add_custom_command(
1368                 OUTPUT  ${_file_from} ${_file_to}
1369                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1370                 #COMMAND python3 ${CMAKE_SOURCE_DIR}/source/blender/datatoc/datatoc_icon.py ${_path_from_abs} ${_file_from}
1371                 COMMAND "$<TARGET_FILE:datatoc_icon>" ${_path_from_abs} ${_file_from}
1372                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1373                 DEPENDS
1374                         ${_icon_files}
1375                         datatoc_icon
1376                         datatoc
1377                         # could be an arg but for now we only create icons depending on UI_icons.h
1378                         ${CMAKE_SOURCE_DIR}/source/blender/editors/include/UI_icons.h
1379                 )
1380
1381         set_source_files_properties(${_file_from} ${_file_to} PROPERTIES GENERATED TRUE)
1382 endfunction()
1383
1384 # XXX Not used for now...
1385 function(svg_to_png
1386         file_from
1387         file_to
1388         dpi
1389         list_to_add
1390         )
1391
1392         # remove ../'s
1393         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from} REALPATH)
1394         get_filename_component(_file_to   ${CMAKE_CURRENT_SOURCE_DIR}/${file_to}   REALPATH)
1395
1396         list(APPEND ${list_to_add} ${_file_to})
1397         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1398
1399         find_program(INKSCAPE_EXE inkscape)
1400         mark_as_advanced(INKSCAPE_EXE)
1401
1402         if(INKSCAPE_EXE)
1403                 if(APPLE)
1404                         # in OS X app bundle, the binary is a shim that doesn't take any
1405                         # command line arguments, replace it with the actual binary
1406                         string(REPLACE "MacOS/Inkscape" "Resources/bin/inkscape" INKSCAPE_REAL_EXE ${INKSCAPE_EXE})
1407                         if(EXISTS "${INKSCAPE_REAL_EXE}")
1408                                 set(INKSCAPE_EXE ${INKSCAPE_REAL_EXE})
1409                         endif()
1410                 endif()
1411
1412                 add_custom_command(
1413                         OUTPUT  ${_file_to}
1414                         COMMAND ${INKSCAPE_EXE} ${_file_from} --export-dpi=${dpi}  --without-gui --export-png=${_file_to}
1415                         DEPENDS ${_file_from} ${INKSCAPE_EXE}
1416                 )
1417         else()
1418                 message(WARNING "Inkscape not found, could not re-generate ${_file_to} from ${_file_from}!")
1419         endif()
1420 endfunction()
1421
1422 function(msgfmt_simple
1423         file_from
1424         list_to_add
1425         )
1426
1427         # remove ../'s
1428         get_filename_component(_file_from_we ${file_from} NAME_WE)
1429
1430         get_filename_component(_file_from ${file_from} REALPATH)
1431         get_filename_component(_file_to ${CMAKE_CURRENT_BINARY_DIR}/${_file_from_we}.mo REALPATH)
1432
1433         list(APPEND ${list_to_add} ${_file_to})
1434         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1435
1436         get_filename_component(_file_to_path ${_file_to} PATH)
1437
1438         add_custom_command(
1439                 OUTPUT  ${_file_to}
1440                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1441                 COMMAND "$<TARGET_FILE:msgfmt>" ${_file_from} ${_file_to}
1442                 DEPENDS msgfmt ${_file_from})
1443
1444         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1445 endfunction()
1446
1447 function(find_python_package
1448         package
1449         )
1450
1451         string(TOUPPER ${package} _upper_package)
1452
1453         # set but invalid
1454         if((NOT ${PYTHON_${_upper_package}_PATH} STREQUAL "") AND
1455            (NOT ${PYTHON_${_upper_package}_PATH} MATCHES NOTFOUND))
1456 #               if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}/${package}")
1457 #                       message(WARNING "PYTHON_${_upper_package}_PATH is invalid, ${package} not found in '${PYTHON_${_upper_package}_PATH}' "
1458 #                                       "WITH_PYTHON_INSTALL_${_upper_package} option will be ignored when installing python")
1459 #                       set(WITH_PYTHON_INSTALL${_upper_package} OFF)
1460 #               endif()
1461         # not set, so initialize
1462         else()
1463                 string(REPLACE "." ";" _PY_VER_SPLIT "${PYTHON_VERSION}")
1464                 list(GET _PY_VER_SPLIT 0 _PY_VER_MAJOR)
1465
1466                 # re-cache
1467                 unset(PYTHON_${_upper_package}_PATH CACHE)
1468                 find_path(PYTHON_${_upper_package}_PATH
1469                   NAMES
1470                     ${package}
1471                   HINTS
1472                     "${PYTHON_LIBPATH}/python${PYTHON_VERSION}/"
1473                     "${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/"
1474                   PATH_SUFFIXES
1475                     site-packages
1476                     dist-packages
1477                    NO_DEFAULT_PATH
1478                 )
1479
1480                  if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}")
1481                         message(WARNING "'${package}' path could not be found in:\n"
1482                                         "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/site-packages/${package}', "
1483                                         "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/site-packages/${package}', "
1484                                         "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/dist-packages/${package}', "
1485                                         "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/dist-packages/${package}', "
1486                                         "WITH_PYTHON_INSTALL_${_upper_package} option will be ignored when installing python")
1487                         set(WITH_PYTHON_INSTALL_${_upper_package} OFF PARENT_SCOPE)
1488                 else()
1489                         message(STATUS "${package} found at '${PYTHON_${_upper_package}_PATH}'")
1490                 endif()
1491           endif()
1492 endfunction()
1493
1494 # like Python's 'print(dir())'
1495 function(print_all_vars)
1496         get_cmake_property(_vars VARIABLES)
1497         foreach(_var ${_vars})
1498                 message("${_var}=${${_var}}")
1499         endforeach()
1500 endfunction()