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