Cleanup: stdbool no longer optional, remove checks
[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 # Only print message if running CMake first time
803 macro(message_first_run)
804         if(FIRST_RUN)
805                 message(${ARGV})
806         endif()
807 endmacro()
808
809 macro(TEST_UNORDERED_MAP_SUPPORT)
810         # - Detect unordered_map availability
811         # Test if a valid implementation of unordered_map exists
812         # and define the include path
813         # This module defines
814         #  HAVE_UNORDERED_MAP, whether unordered_map implementation was found
815         #
816         #  HAVE_STD_UNORDERED_MAP_HEADER, <unordered_map.h> was found
817         #  HAVE_UNORDERED_MAP_IN_STD_NAMESPACE, unordered_map is in namespace std
818         #  HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE, unordered_map is in namespace std::tr1
819         #
820         #  UNORDERED_MAP_INCLUDE_PREFIX, include path prefix for unordered_map, if found
821         #  UNORDERED_MAP_NAMESPACE, namespace for unordered_map, if found
822
823         include(CheckIncludeFileCXX)
824         CHECK_INCLUDE_FILE_CXX("unordered_map" HAVE_STD_UNORDERED_MAP_HEADER)
825         if(HAVE_STD_UNORDERED_MAP_HEADER)
826                 # Even so we've found unordered_map header file it doesn't
827                 # mean unordered_map and unordered_set will be declared in
828                 # std namespace.
829                 #
830                 # Namely, MSVC 2008 have unordered_map header which declares
831                 # unordered_map class in std::tr1 namespace. In order to support
832                 # this, we do extra check to see which exactly namespace is
833                 # to be used.
834
835                 include(CheckCXXSourceCompiles)
836                 CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
837                                           int main() {
838                                             std::unordered_map<int, int> map;
839                                             return 0;
840                                           }"
841                                           HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
842                 if(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
843                         message_first_run(STATUS "Found unordered_map/set in std namespace.")
844
845                         set(HAVE_UNORDERED_MAP "TRUE")
846                         set(UNORDERED_MAP_INCLUDE_PREFIX "")
847                         set(UNORDERED_MAP_NAMESPACE "std")
848                 else()
849                         CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
850                                                   int main() {
851                                                     std::tr1::unordered_map<int, int> map;
852                                                     return 0;
853                                                   }"
854                                                   HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
855                         if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
856                                 message_first_run(STATUS "Found unordered_map/set in std::tr1 namespace.")
857
858                                 set(HAVE_UNORDERED_MAP "TRUE")
859                                 set(UNORDERED_MAP_INCLUDE_PREFIX "")
860                                 set(UNORDERED_MAP_NAMESPACE "std::tr1")
861                         else()
862                                 message_first_run(STATUS "Found <unordered_map> but cannot find either std::unordered_map "
863                                                   "or std::tr1::unordered_map.")
864                         endif()
865                 endif()
866         else()
867                 CHECK_INCLUDE_FILE_CXX("tr1/unordered_map" HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
868                 if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
869                         message_first_run(STATUS "Found unordered_map/set in std::tr1 namespace.")
870
871                         set(HAVE_UNORDERED_MAP "TRUE")
872                         set(UNORDERED_MAP_INCLUDE_PREFIX "tr1")
873                         set(UNORDERED_MAP_NAMESPACE "std::tr1")
874                 else()
875                         message_first_run(STATUS "Unable to find <unordered_map> or <tr1/unordered_map>. ")
876                 endif()
877         endif()
878 endmacro()
879
880 macro(TEST_SHARED_PTR_SUPPORT)
881         # This check are coming from Ceres library.
882         #
883         # Find shared pointer header and namespace.
884         #
885         # This module defines the following variables:
886         #
887         # SHARED_PTR_FOUND: TRUE if shared_ptr found.
888         # SHARED_PTR_TR1_MEMORY_HEADER: True if <tr1/memory> header is to be used
889         # for the shared_ptr object, otherwise use <memory>.
890         # SHARED_PTR_TR1_NAMESPACE: TRUE if shared_ptr is defined in std::tr1 namespace,
891         # otherwise it's assumed to be defined in std namespace.
892
893         include(CheckIncludeFileCXX)
894         set(SHARED_PTR_FOUND FALSE)
895         CHECK_INCLUDE_FILE_CXX(memory HAVE_STD_MEMORY_HEADER)
896         if(HAVE_STD_MEMORY_HEADER)
897                 # Finding the memory header doesn't mean that shared_ptr is in std
898                 # namespace.
899                 #
900                 # In particular, MSVC 2008 has shared_ptr declared in std::tr1.  In
901                 # order to support this, we do an extra check to see which namespace
902                 # should be used.
903                 include(CheckCXXSourceCompiles)
904                 CHECK_CXX_SOURCE_COMPILES("#include <memory>
905                                            int main() {
906                                              std::shared_ptr<int> int_ptr;
907                                              return 0;
908                                            }"
909                                           HAVE_SHARED_PTR_IN_STD_NAMESPACE)
910
911                 if(HAVE_SHARED_PTR_IN_STD_NAMESPACE)
912                         message_first_run("-- Found shared_ptr in std namespace using <memory> header.")
913                         set(SHARED_PTR_FOUND TRUE)
914                 else()
915                         CHECK_CXX_SOURCE_COMPILES("#include <memory>
916                                                    int main() {
917                                                    std::tr1::shared_ptr<int> int_ptr;
918                                                    return 0;
919                                                    }"
920                                                   HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
921                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
922                                 message_first_run("-- Found shared_ptr in std::tr1 namespace using <memory> header.")
923                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
924                                 set(SHARED_PTR_FOUND TRUE)
925                         endif()
926                 endif()
927         endif()
928
929         if(NOT SHARED_PTR_FOUND)
930                 # Further, gcc defines shared_ptr in std::tr1 namespace and
931                 # <tr1/memory> is to be included for this. And what makes things
932                 # even more tricky is that gcc does have <memory> header, so
933                 # all the checks above wouldn't find shared_ptr.
934                 CHECK_INCLUDE_FILE_CXX("tr1/memory" HAVE_TR1_MEMORY_HEADER)
935                 if(HAVE_TR1_MEMORY_HEADER)
936                         CHECK_CXX_SOURCE_COMPILES("#include <tr1/memory>
937                                                    int main() {
938                                                    std::tr1::shared_ptr<int> int_ptr;
939                                                    return 0;
940                                                    }"
941                                                    HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
942                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
943                                 message_first_run("-- Found shared_ptr in std::tr1 namespace using <tr1/memory> header.")
944                                 set(SHARED_PTR_TR1_MEMORY_HEADER TRUE)
945                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
946                                 set(SHARED_PTR_FOUND TRUE)
947                         endif()
948                 endif()
949         endif()
950 endmacro()
951
952 # when we have warnings as errors applied globally this
953 # needs to be removed for some external libs which we dont maintain.
954
955 # utility macro
956 macro(remove_cc_flag
957         _flag)
958
959         foreach(flag ${ARGV})
960                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
961                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
962                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
963                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
964                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
965
966                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
967                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
968                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
969                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
970                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
971         endforeach()
972         unset(flag)
973
974 endmacro()
975
976 macro(add_cc_flag
977         flag)
978
979         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
980         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
981 endmacro()
982
983 macro(remove_strict_flags)
984
985         if(CMAKE_COMPILER_IS_GNUCC)
986                 remove_cc_flag(
987                         "-Wstrict-prototypes"
988                         "-Wmissing-prototypes"
989                         "-Wmissing-format-attribute"
990                         "-Wunused-local-typedefs"
991                         "-Wunused-macros"
992                         "-Wunused-parameter"
993                         "-Wwrite-strings"
994                         "-Wredundant-decls"
995                         "-Wundef"
996                         "-Wshadow"
997                         "-Wdouble-promotion"
998                         "-Wold-style-definition"
999                         "-Werror=[^ ]+"
1000                         "-Werror"
1001                 )
1002
1003                 # negate flags implied by '-Wall'
1004                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1005         endif()
1006
1007         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1008                 remove_cc_flag(
1009                         "-Wunused-parameter"
1010                         "-Wunused-variable"
1011                         "-Werror=[^ ]+"
1012                         "-Werror"
1013                 )
1014
1015                 # negate flags implied by '-Wall'
1016                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1017         endif()
1018
1019         if(MSVC)
1020                 # TODO
1021         endif()
1022
1023 endmacro()
1024
1025 macro(remove_extra_strict_flags)
1026         if(CMAKE_COMPILER_IS_GNUCC)
1027                 remove_cc_flag(
1028                         "-Wunused-parameter"
1029                 )
1030         endif()
1031
1032         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1033                 remove_cc_flag(
1034                         "-Wunused-parameter"
1035                 )
1036         endif()
1037
1038         if(MSVC)
1039                 # TODO
1040         endif()
1041 endmacro()
1042
1043 # note, we can only append flags on a single file so we need to negate the options.
1044 # at the moment we cant shut up ffmpeg deprecations, so use this, but will
1045 # probably add more removals here.
1046 macro(remove_strict_flags_file
1047         filenames)
1048
1049         foreach(_SOURCE ${ARGV})
1050
1051                 if(CMAKE_COMPILER_IS_GNUCC OR
1052                   (CMAKE_C_COMPILER_ID MATCHES "Clang"))
1053
1054                         set_source_files_properties(${_SOURCE}
1055                                 PROPERTIES
1056                                         COMPILE_FLAGS "${CC_REMOVE_STRICT_FLAGS}"
1057                         )
1058                 endif()
1059
1060                 if(MSVC)
1061                         # TODO
1062                 endif()
1063
1064         endforeach()
1065
1066         unset(_SOURCE)
1067
1068 endmacro()
1069
1070
1071 function(ADD_CHECK_C_COMPILER_FLAG
1072         _CFLAGS
1073         _CACHE_VAR
1074         _FLAG
1075         )
1076
1077         include(CheckCCompilerFlag)
1078
1079         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1080         if(${_CACHE_VAR})
1081                 # message(STATUS "Using CFLAG: ${_FLAG}")
1082                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}" PARENT_SCOPE)
1083         else()
1084                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
1085         endif()
1086 endfunction()
1087
1088 function(ADD_CHECK_CXX_COMPILER_FLAG
1089         _CXXFLAGS
1090         _CACHE_VAR
1091         _FLAG
1092         )
1093
1094         include(CheckCXXCompilerFlag)
1095
1096         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1097         if(${_CACHE_VAR})
1098                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
1099                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}" PARENT_SCOPE)
1100         else()
1101                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
1102         endif()
1103 endfunction()
1104
1105 function(get_blender_version)
1106         # extracts header vars and defines them in the parent scope:
1107         #
1108         # - BLENDER_VERSION (major.minor)
1109         # - BLENDER_VERSION_MAJOR
1110         # - BLENDER_VERSION_MINOR
1111         # - BLENDER_SUBVERSION (used for internal versioning mainly)
1112         # - BLENDER_VERSION_CHAR (a, b, c, ...or empty string)
1113         # - BLENDER_VERSION_CYCLE (alpha, beta, rc, release)
1114
1115         # So cmake depends on BKE_blender.h, beware of inf-loops!
1116         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h
1117                        ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
1118
1119         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
1120
1121         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
1122         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
1123         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
1124         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
1125
1126         if(NOT ${_out_version} MATCHES "[0-9]+")
1127                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
1128         endif()
1129
1130         if(NOT ${_out_subversion} MATCHES "[0-9]+")
1131                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
1132         endif()
1133
1134         # clumsy regex, only single char are ok but it could be unset
1135
1136         string(LENGTH "${_out_version_char}" _out_version_char_len)
1137         if(NOT _out_version_char_len EQUAL 1)
1138                 set(_out_version_char "")
1139         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
1140                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
1141         endif()
1142
1143         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
1144                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
1145         endif()
1146
1147         math(EXPR _out_version_major "${_out_version} / 100")
1148         math(EXPR _out_version_minor "${_out_version} % 100")
1149
1150         # for packaging, alpha to numbers
1151         string(COMPARE EQUAL "${_out_version_char}" "" _out_version_char_empty)
1152         if(${_out_version_char_empty})
1153                 set(_out_version_char_index "0")
1154         else()
1155                 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)
1156                 list(FIND _char_ls ${_out_version_char} _out_version_char_index)
1157                 math(EXPR _out_version_char_index "${_out_version_char_index} + 1")
1158         endif()
1159
1160         # output vars
1161         set(BLENDER_VERSION "${_out_version_major}.${_out_version_minor}" PARENT_SCOPE)
1162         set(BLENDER_VERSION_MAJOR "${_out_version_major}" PARENT_SCOPE)
1163         set(BLENDER_VERSION_MINOR "${_out_version_minor}" PARENT_SCOPE)
1164         set(BLENDER_SUBVERSION "${_out_subversion}" PARENT_SCOPE)
1165         set(BLENDER_VERSION_CHAR "${_out_version_char}" PARENT_SCOPE)
1166         set(BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index}" PARENT_SCOPE)
1167         set(BLENDER_VERSION_CYCLE "${_out_version_cycle}" PARENT_SCOPE)
1168
1169 endfunction()
1170
1171
1172 # hacks to override initial project settings
1173 # these macros must be called directly before/after project(Blender)
1174 macro(blender_project_hack_pre)
1175         # ----------------
1176         # MINGW HACK START
1177         # ignore system set flag, use our own
1178         # must be before project(...)
1179         # if the user wants to add their own its ok after first run.
1180         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
1181                 set(_reset_standard_libraries OFF)
1182         else()
1183                 set(_reset_standard_libraries ON)
1184         endif()
1185
1186         # ------------------
1187         # GCC -O3 HACK START
1188         # needed because O3 can cause problems but
1189         # allow the builder to set O3 manually after.
1190         if(DEFINED CMAKE_C_FLAGS_RELEASE)
1191                 set(_reset_standard_cflags_rel OFF)
1192         else()
1193                 set(_reset_standard_cflags_rel ON)
1194         endif()
1195         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
1196                 set(_reset_standard_cxxflags_rel OFF)
1197         else()
1198                 set(_reset_standard_cxxflags_rel ON)
1199         endif()
1200 endmacro()
1201
1202
1203 macro(blender_project_hack_post)
1204         # --------------
1205         # MINGW HACK END
1206         if(_reset_standard_libraries)
1207                 # Must come after projecINCt(...)
1208                 #
1209                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
1210                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
1211                 # with a meaningless stack trace. by overriding this flag we ensure we only
1212                 # have libs we define.
1213                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1214                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1215                 mark_as_advanced(
1216                         CMAKE_C_STANDARD_LIBRARIES
1217                         CMAKE_CXX_STANDARD_LIBRARIES
1218                 )
1219         endif()
1220         unset(_reset_standard_libraries)
1221
1222
1223         # ----------------
1224         # GCC -O3 HACK END
1225         if(_reset_standard_cflags_rel)
1226                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
1227                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1228                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
1229         endif()
1230
1231         if(_reset_standard_cxxflags_rel)
1232                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
1233                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1234                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
1235         endif()
1236
1237         unset(_reset_standard_cflags_rel)
1238         unset(_reset_standard_cxxflags_rel)
1239
1240         # ------------------------------------------------------------------
1241         # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
1242         if(CMAKE_COMPILER_IS_GNUCC)
1243                 if(NOT DARWIN)
1244                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
1245                 endif()
1246         endif()
1247
1248 endmacro()
1249
1250 # pair of macros to allow libraries to be specify files to install, but to
1251 # only install them at the end so the directories don't get cleared with
1252 # the files in them. used by cycles to install addon.
1253 function(delayed_install
1254         base
1255         files
1256         destination)
1257
1258         foreach(f ${files})
1259                 if(IS_ABSOLUTE ${f})
1260                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${f})
1261                 else()
1262                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${base}/${f})
1263                 endif()
1264                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_DESTINATIONS ${destination})
1265         endforeach()
1266 endfunction()
1267
1268 # note this is a function instead of a macro so that ${BUILD_TYPE} in targetdir
1269 # does not get expanded in calling but is preserved
1270 function(delayed_do_install
1271         targetdir)
1272
1273         get_property(files GLOBAL PROPERTY DELAYED_INSTALL_FILES)
1274         get_property(destinations GLOBAL PROPERTY DELAYED_INSTALL_DESTINATIONS)
1275
1276         if(files)
1277                 list(LENGTH files n)
1278                 math(EXPR n "${n}-1")
1279
1280                 foreach(i RANGE ${n})
1281                         list(GET files ${i} f)
1282                         list(GET destinations ${i} d)
1283                         install(FILES ${f} DESTINATION ${targetdir}/${d})
1284                 endforeach()
1285         endif()
1286 endfunction()
1287
1288
1289 function(data_to_c
1290         file_from file_to
1291         list_to_add
1292         )
1293
1294         list(APPEND ${list_to_add} ${file_to})
1295         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1296
1297         get_filename_component(_file_to_path ${file_to} PATH)
1298
1299         add_custom_command(
1300                 OUTPUT ${file_to}
1301                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1302                 COMMAND "$<TARGET_FILE:datatoc>" ${file_from} ${file_to}
1303                 DEPENDS ${file_from} datatoc)
1304
1305         set_source_files_properties(${file_to} PROPERTIES GENERATED TRUE)
1306 endfunction()
1307
1308
1309 # same as above but generates the var name and output automatic.
1310 function(data_to_c_simple
1311         file_from
1312         list_to_add
1313         )
1314
1315         # remove ../'s
1316         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from}   REALPATH)
1317         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${file_from}.c REALPATH)
1318
1319         list(APPEND ${list_to_add} ${_file_to})
1320         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1321
1322         get_filename_component(_file_to_path ${_file_to} PATH)
1323
1324         add_custom_command(
1325                 OUTPUT  ${_file_to}
1326                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1327                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1328                 DEPENDS ${_file_from} datatoc)
1329
1330         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1331 endfunction()
1332
1333 # macro for converting pixmap directory to a png and then a c file
1334 function(data_to_c_simple_icons
1335         path_from
1336         list_to_add
1337         )
1338
1339         # Conversion steps
1340         #  path_from  ->  _file_from  ->  _file_to
1341         #  foo/*.dat  ->  foo.png     ->  foo.png.c
1342
1343         get_filename_component(_path_from_abs ${path_from} ABSOLUTE)
1344         # remove ../'s
1345         get_filename_component(_file_from ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png   REALPATH)
1346         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png.c REALPATH)
1347
1348         list(APPEND ${list_to_add} ${_file_to})
1349         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1350
1351         get_filename_component(_file_to_path ${_file_to} PATH)
1352
1353         # ideally we wouldn't glob, but storing all names for all pixmaps is a bit heavy
1354         file(GLOB _icon_files "${path_from}/*.dat")
1355
1356         add_custom_command(
1357                 OUTPUT  ${_file_from} ${_file_to}
1358                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1359                 #COMMAND python3 ${CMAKE_SOURCE_DIR}/source/blender/datatoc/datatoc_icon.py ${_path_from_abs} ${_file_from}
1360                 COMMAND "$<TARGET_FILE:datatoc_icon>" ${_path_from_abs} ${_file_from}
1361                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1362                 DEPENDS
1363                         ${_icon_files}
1364                         datatoc_icon
1365                         datatoc
1366                         # could be an arg but for now we only create icons depending on UI_icons.h
1367                         ${CMAKE_SOURCE_DIR}/source/blender/editors/include/UI_icons.h
1368                 )
1369
1370         set_source_files_properties(${_file_from} ${_file_to} PROPERTIES GENERATED TRUE)
1371 endfunction()
1372
1373 # XXX Not used for now...
1374 function(svg_to_png
1375         file_from
1376         file_to
1377         dpi
1378         list_to_add
1379         )
1380
1381         # remove ../'s
1382         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from} REALPATH)
1383         get_filename_component(_file_to   ${CMAKE_CURRENT_SOURCE_DIR}/${file_to}   REALPATH)
1384
1385         list(APPEND ${list_to_add} ${_file_to})
1386         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1387
1388         find_program(INKSCAPE_EXE inkscape)
1389         mark_as_advanced(INKSCAPE_EXE)
1390
1391         if(INKSCAPE_EXE)
1392                 if(APPLE)
1393                         # in OS X app bundle, the binary is a shim that doesn't take any
1394                         # command line arguments, replace it with the actual binary
1395                         string(REPLACE "MacOS/Inkscape" "Resources/bin/inkscape" INKSCAPE_REAL_EXE ${INKSCAPE_EXE})
1396                         if(EXISTS "${INKSCAPE_REAL_EXE}")
1397                                 set(INKSCAPE_EXE ${INKSCAPE_REAL_EXE})
1398                         endif()
1399                 endif()
1400
1401                 add_custom_command(
1402                         OUTPUT  ${_file_to}
1403                         COMMAND ${INKSCAPE_EXE} ${_file_from} --export-dpi=${dpi}  --without-gui --export-png=${_file_to}
1404                         DEPENDS ${_file_from} ${INKSCAPE_EXE}
1405                 )
1406         else()
1407                 message(WARNING "Inkscape not found, could not re-generate ${_file_to} from ${_file_from}!")
1408         endif()
1409 endfunction()
1410
1411 function(msgfmt_simple
1412         file_from
1413         list_to_add
1414         )
1415
1416         # remove ../'s
1417         get_filename_component(_file_from_we ${file_from} NAME_WE)
1418
1419         get_filename_component(_file_from ${file_from} REALPATH)
1420         get_filename_component(_file_to ${CMAKE_CURRENT_BINARY_DIR}/${_file_from_we}.mo REALPATH)
1421
1422         list(APPEND ${list_to_add} ${_file_to})
1423         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1424
1425         get_filename_component(_file_to_path ${_file_to} PATH)
1426
1427         add_custom_command(
1428                 OUTPUT  ${_file_to}
1429                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1430                 COMMAND "$<TARGET_FILE:msgfmt>" ${_file_from} ${_file_to}
1431                 DEPENDS msgfmt ${_file_from})
1432
1433         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1434 endfunction()
1435
1436 function(find_python_package
1437         package
1438         )
1439
1440         string(TOUPPER ${package} _upper_package)
1441
1442         # set but invalid
1443         if((NOT ${PYTHON_${_upper_package}_PATH} STREQUAL "") AND
1444            (NOT ${PYTHON_${_upper_package}_PATH} MATCHES NOTFOUND))
1445 #               if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}/${package}")
1446 #                       message(WARNING "PYTHON_${_upper_package}_PATH is invalid, ${package} not found in '${PYTHON_${_upper_package}_PATH}' "
1447 #                                       "WITH_PYTHON_INSTALL_${_upper_package} option will be ignored when installing python")
1448 #                       set(WITH_PYTHON_INSTALL${_upper_package} OFF)
1449 #               endif()
1450         # not set, so initialize
1451         else()
1452                 string(REPLACE "." ";" _PY_VER_SPLIT "${PYTHON_VERSION}")
1453                 list(GET _PY_VER_SPLIT 0 _PY_VER_MAJOR)
1454
1455                 # re-cache
1456                 unset(PYTHON_${_upper_package}_PATH CACHE)
1457                 find_path(PYTHON_${_upper_package}_PATH
1458                   NAMES
1459                     ${package}
1460                   HINTS
1461                     "${PYTHON_LIBPATH}/python${PYTHON_VERSION}/"
1462                     "${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/"
1463                   PATH_SUFFIXES
1464                     site-packages
1465                     dist-packages
1466                    NO_DEFAULT_PATH
1467                 )
1468
1469                  if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}")
1470                         message(WARNING "'${package}' path could not be found in:\n"
1471                                         "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/site-packages/${package}', "
1472                                         "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/site-packages/${package}', "
1473                                         "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/dist-packages/${package}', "
1474                                         "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/dist-packages/${package}', "
1475                                         "WITH_PYTHON_INSTALL_${_upper_package} option will be ignored when installing python")
1476                         set(WITH_PYTHON_INSTALL_${_upper_package} OFF PARENT_SCOPE)
1477                 else()
1478                         message(STATUS "${package} found at '${PYTHON_${_upper_package}_PATH}'")
1479                 endif()
1480           endif()
1481 endfunction()
1482
1483 # like Python's 'print(dir())'
1484 function(print_all_vars)
1485         get_cmake_property(_vars VARIABLES)
1486         foreach(_var ${_vars})
1487                 message("${_var}=${${_var}}")
1488         endforeach()
1489 endfunction()