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