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