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