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