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