Incompatible usage of the Collada transparency value
[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 # ***** END GPL LICENSE BLOCK *****
20
21 macro(list_insert_after
22   list_id item_check item_add
23   )
24   set(_index)
25   list(FIND "${list_id}" "${item_check}" _index)
26   if("${_index}" MATCHES "-1")
27     message(FATAL_ERROR "'${list_id}' doesn't contain '${item_check}'")
28   endif()
29   math(EXPR _index "${_index} + 1")
30   list(INSERT ${list_id} "${_index}" ${item_add})
31   unset(_index)
32 endmacro()
33
34 macro(list_insert_before
35   list_id item_check item_add
36   )
37   set(_index)
38   list(FIND "${list_id}" "${item_check}" _index)
39   if("${_index}" MATCHES "-1")
40     message(FATAL_ERROR "'${list_id}' doesn't contain '${item_check}'")
41   endif()
42   list(INSERT ${list_id} "${_index}" ${item_add})
43   unset(_index)
44 endmacro()
45
46 function(list_assert_duplicates
47   list_id
48   )
49
50   # message(STATUS "list data: ${list_id}")
51
52   list(LENGTH list_id _len_before)
53   list(REMOVE_DUPLICATES list_id)
54   list(LENGTH list_id _len_after)
55   # message(STATUS "list size ${_len_before} -> ${_len_after}")
56   if(NOT _len_before EQUAL _len_after)
57     message(FATAL_ERROR "duplicate found in list which should not contain duplicates: ${list_id}")
58   endif()
59   unset(_len_before)
60   unset(_len_after)
61 endfunction()
62
63
64 # foo_bar.spam --> foo_barMySuffix.spam
65 macro(file_suffix
66   file_name_new file_name file_suffix
67   )
68
69   get_filename_component(_file_name_PATH ${file_name} PATH)
70   get_filename_component(_file_name_NAME_WE ${file_name} NAME_WE)
71   get_filename_component(_file_name_EXT ${file_name} EXT)
72   set(${file_name_new} "${_file_name_PATH}/${_file_name_NAME_WE}${file_suffix}${_file_name_EXT}")
73
74   unset(_file_name_PATH)
75   unset(_file_name_NAME_WE)
76   unset(_file_name_EXT)
77 endmacro()
78
79 # useful for adding debug suffix to library lists:
80 # /somepath/foo.lib --> /somepath/foo_d.lib
81 macro(file_list_suffix
82   fp_list_new fp_list fn_suffix
83   )
84
85   # incase of empty list
86   set(_fp)
87   set(_fp_suffixed)
88
89   set(fp_list_new)
90
91   foreach(_fp ${fp_list})
92     file_suffix(_fp_suffixed "${_fp}" "${fn_suffix}")
93     list(APPEND "${fp_list_new}" "${_fp_suffixed}")
94   endforeach()
95
96   unset(_fp)
97   unset(_fp_suffixed)
98
99 endmacro()
100
101 if(UNIX AND NOT APPLE)
102   macro(find_package_static)
103     set(_cmake_find_library_suffixes_back ${CMAKE_FIND_LIBRARY_SUFFIXES})
104     set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
105     find_package(${ARGV})
106     set(CMAKE_FIND_LIBRARY_SUFFIXES ${_cmake_find_library_suffixes_back})
107     unset(_cmake_find_library_suffixes_back)
108   endmacro()
109
110   macro(find_library_static)
111     set(_cmake_find_library_suffixes_back ${CMAKE_FIND_LIBRARY_SUFFIXES})
112     set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
113     find_library(${ARGV})
114     set(CMAKE_FIND_LIBRARY_SUFFIXES ${_cmake_find_library_suffixes_back})
115     unset(_cmake_find_library_suffixes_back)
116   endmacro()
117 endif()
118
119 function(target_link_libraries_optimized
120   TARGET
121   LIBS
122   )
123
124   foreach(_LIB ${LIBS})
125     target_link_libraries(${TARGET} optimized "${_LIB}")
126   endforeach()
127 endfunction()
128
129 function(target_link_libraries_debug
130   TARGET
131   LIBS
132   )
133
134   foreach(_LIB ${LIBS})
135     target_link_libraries(${TARGET} debug "${_LIB}")
136   endforeach()
137 endfunction()
138
139 # Nicer makefiles with -I/1/foo/ instead of -I/1/2/3/../../foo/
140 # use it instead of include_directories()
141 function(blender_include_dirs
142   includes
143   )
144
145   set(_ALL_INCS "")
146   foreach(_INC ${ARGV})
147     get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
148     list(APPEND _ALL_INCS ${_ABS_INC})
149     # for checking for invalid includes, disable for regular use
150     ##if(NOT EXISTS "${_ABS_INC}/")
151     ##  message(FATAL_ERROR "Include not found: ${_ABS_INC}/")
152     ##endif()
153   endforeach()
154   include_directories(${_ALL_INCS})
155 endfunction()
156
157 function(blender_include_dirs_sys
158   includes
159   )
160
161   set(_ALL_INCS "")
162   foreach(_INC ${ARGV})
163     get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
164     list(APPEND _ALL_INCS ${_ABS_INC})
165     ##if(NOT EXISTS "${_ABS_INC}/")
166     ##  message(FATAL_ERROR "Include not found: ${_ABS_INC}/")
167     ##endif()
168   endforeach()
169   include_directories(SYSTEM ${_ALL_INCS})
170 endfunction()
171
172 function(blender_source_group
173   sources
174   )
175
176   #if enabled, use the sources directories as filters.
177   if(WINDOWS_USE_VISUAL_STUDIO_SOURCE_FOLDERS)
178     foreach(_SRC ${sources})
179       # remove ../'s
180       get_filename_component(_SRC_DIR ${_SRC} REALPATH)
181       get_filename_component(_SRC_DIR ${_SRC_DIR} DIRECTORY)
182       string(FIND ${_SRC_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/" _pos)
183       if(NOT _pos EQUAL -1)
184         string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" GROUP_ID ${_SRC_DIR})
185         string(REPLACE "/" "\\" GROUP_ID ${GROUP_ID})
186         source_group("${GROUP_ID}" FILES ${_SRC})
187       endif()
188       unset(_pos)
189     endforeach()
190   else()
191     # Group by location on disk
192     source_group("Source Files" FILES CMakeLists.txt)
193     foreach(_SRC ${sources})
194       get_filename_component(_SRC_EXT ${_SRC} EXT)
195       if((${_SRC_EXT} MATCHES ".h") OR
196          (${_SRC_EXT} MATCHES ".hpp") OR
197          (${_SRC_EXT} MATCHES ".hh"))
198
199         set(GROUP_ID "Header Files")
200       elseif(${_SRC_EXT} MATCHES ".glsl$")
201         set(GROUP_ID "Shaders")
202       else()
203         set(GROUP_ID "Source Files")
204       endif()
205       source_group("${GROUP_ID}" FILES ${_SRC})
206     endforeach()
207   endif()
208 endfunction()
209
210
211 # Support per-target CMake flags
212 # Read from: CMAKE_C_FLAGS_**** (made upper case) when set.
213 #
214 # 'name' should always match the target name,
215 # use this macro before add_library or add_executable.
216 #
217 # Optionally takes an arg passed to set(), eg PARENT_SCOPE.
218 macro(add_cc_flags_custom_test
219   name
220   )
221
222   string(TOUPPER ${name} _name_upper)
223   if(DEFINED CMAKE_C_FLAGS_${_name_upper})
224     message(STATUS "Using custom CFLAGS: CMAKE_C_FLAGS_${_name_upper} in \"${CMAKE_CURRENT_SOURCE_DIR}\"")
225     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${_name_upper}}" ${ARGV1})
226   endif()
227   if(DEFINED CMAKE_CXX_FLAGS_${_name_upper})
228     message(STATUS "Using custom CXXFLAGS: CMAKE_CXX_FLAGS_${_name_upper} in \"${CMAKE_CURRENT_SOURCE_DIR}\"")
229     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${_name_upper}}" ${ARGV1})
230   endif()
231   unset(_name_upper)
232
233 endmacro()
234
235
236 # only MSVC uses SOURCE_GROUP
237 function(blender_add_lib__impl
238   name
239   sources
240   includes
241   includes_sys
242   library_deps
243   )
244
245   # message(STATUS "Configuring library ${name}")
246
247   # include_directories(${includes})
248   # include_directories(SYSTEM ${includes_sys})
249   blender_include_dirs("${includes}")
250   blender_include_dirs_sys("${includes_sys}")
251
252   add_library(${name} ${sources})
253
254   if(NOT "${library_deps}" STREQUAL "")
255     target_link_libraries(${name} INTERFACE "${library_deps}")
256   endif()
257
258   # works fine without having the includes
259   # listed is helpful for IDE's (QtCreator/MSVC)
260   blender_source_group("${sources}")
261
262   #if enabled, set the FOLDER property for visual studio projects
263   if(WINDOWS_USE_VISUAL_STUDIO_PROJECT_FOLDERS)
264     get_filename_component(FolderDir ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
265     string(REPLACE ${CMAKE_SOURCE_DIR} "" FolderDir ${FolderDir})
266     set_target_properties(${name} PROPERTIES FOLDER ${FolderDir})
267   endif()
268
269   list_assert_duplicates("${sources}")
270   list_assert_duplicates("${includes}")
271   # Not for system includes because they can resolve to the same path
272   # list_assert_duplicates("${includes_sys}")
273
274 endfunction()
275
276
277 function(blender_add_lib_nolist
278   name
279   sources
280   includes
281   includes_sys
282   library_deps
283   )
284
285   add_cc_flags_custom_test(${name} PARENT_SCOPE)
286
287   blender_add_lib__impl(${name} "${sources}" "${includes}" "${includes_sys}" "${library_deps}")
288 endfunction()
289
290 function(blender_add_lib
291   name
292   sources
293   includes
294   includes_sys
295   library_deps
296   )
297
298   add_cc_flags_custom_test(${name} PARENT_SCOPE)
299
300   blender_add_lib__impl(${name} "${sources}" "${includes}" "${includes_sys}" "${library_deps}")
301
302   set_property(GLOBAL APPEND PROPERTY BLENDER_LINK_LIBS ${name})
303 endfunction()
304
305 # Ninja only: assign 'heavy pool' to some targets that are especially RAM-consuming to build.
306 function(setup_heavy_lib_pool)
307   if(WITH_NINJA_POOL_JOBS AND NINJA_MAX_NUM_PARALLEL_COMPILE_HEAVY_JOBS)
308     if(WITH_CYCLES)
309       list(APPEND _HEAVY_LIBS "cycles_device" "cycles_kernel")
310     endif()
311     if(WITH_LIBMV)
312       list(APPEND _HEAVY_LIBS "bf_intern_libmv")
313     endif()
314     if(WITH_OPENVDB)
315       list(APPEND _HEAVY_LIBS "bf_intern_openvdb")
316     endif()
317
318     foreach(TARGET ${_HEAVY_LIBS})
319       if(TARGET ${TARGET})
320         set_property(TARGET ${TARGET} PROPERTY JOB_POOL_COMPILE compile_heavy_job_pool)
321       endif()
322     endforeach()
323   endif()
324 endfunction()
325
326 function(SETUP_LIBDIRS)
327
328   # NOTE: For all new libraries, use absolute library paths.
329   # This should eventually be phased out.
330
331   if(NOT MSVC)
332     link_directories(${JPEG_LIBPATH} ${PNG_LIBPATH} ${ZLIB_LIBPATH} ${FREETYPE_LIBPATH})
333
334     if(WITH_PYTHON)  #  AND NOT WITH_PYTHON_MODULE  # WIN32 needs
335       link_directories(${PYTHON_LIBPATH})
336     endif()
337     if(WITH_SDL AND NOT WITH_SDL_DYNLOAD)
338       link_directories(${SDL_LIBPATH})
339     endif()
340     if(WITH_CODEC_FFMPEG)
341       link_directories(${FFMPEG_LIBPATH})
342     endif()
343     if(WITH_IMAGE_OPENEXR)
344       link_directories(${OPENEXR_LIBPATH})
345     endif()
346     if(WITH_IMAGE_TIFF)
347       link_directories(${TIFF_LIBPATH})
348     endif()
349     if(WITH_BOOST)
350       link_directories(${BOOST_LIBPATH})
351     endif()
352     if(WITH_OPENIMAGEIO)
353       link_directories(${OPENIMAGEIO_LIBPATH})
354     endif()
355     if(WITH_OPENCOLORIO)
356       link_directories(${OPENCOLORIO_LIBPATH})
357     endif()
358     if(WITH_OPENVDB)
359       link_directories(${OPENVDB_LIBPATH})
360     endif()
361     if(WITH_OPENAL)
362       link_directories(${OPENAL_LIBPATH})
363     endif()
364     if(WITH_JACK AND NOT WITH_JACK_DYNLOAD)
365       link_directories(${JACK_LIBPATH})
366     endif()
367     if(WITH_CODEC_SNDFILE)
368       link_directories(${LIBSNDFILE_LIBPATH})
369     endif()
370     if(WITH_FFTW3)
371       link_directories(${FFTW3_LIBPATH})
372     endif()
373     if(WITH_OPENCOLLADA)
374       link_directories(${OPENCOLLADA_LIBPATH})
375       ## Never set
376       # link_directories(${PCRE_LIBPATH})
377       # link_directories(${EXPAT_LIBPATH})
378     endif()
379     if(WITH_LLVM)
380       link_directories(${LLVM_LIBPATH})
381     endif()
382
383     if(WITH_ALEMBIC)
384       link_directories(${ALEMBIC_LIBPATH})
385       link_directories(${HDF5_LIBPATH})
386     endif()
387
388     if(WIN32 AND NOT UNIX)
389       link_directories(${PTHREADS_LIBPATH})
390     endif()
391   endif()
392 endfunction()
393
394 macro(setup_platform_linker_flags)
395   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}")
396   set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}")
397 endmacro()
398
399 function(setup_liblinks
400   target
401   )
402
403   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
404   set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
405
406   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
407   set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
408
409   set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
410   set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
411
412   # jemalloc must be early in the list, to be before pthread (see T57998)
413   if(WITH_MEM_JEMALLOC)
414     target_link_libraries(${target} ${JEMALLOC_LIBRARIES})
415   endif()
416
417   target_link_libraries(
418     ${target}
419     ${PNG_LIBRARIES}
420     ${FREETYPE_LIBRARY}
421   )
422
423
424   if(WITH_PYTHON)
425     target_link_libraries(${target} ${PYTHON_LINKFLAGS})
426     target_link_libraries(${target} ${PYTHON_LIBRARIES})
427   endif()
428
429   if(WITH_LZO AND WITH_SYSTEM_LZO)
430     target_link_libraries(${target} ${LZO_LIBRARIES})
431   endif()
432   if(WITH_SYSTEM_GLEW)
433     target_link_libraries(${target} ${BLENDER_GLEW_LIBRARIES})
434   endif()
435   if(WITH_BULLET AND WITH_SYSTEM_BULLET)
436     target_link_libraries(${target} ${BULLET_LIBRARIES})
437   endif()
438   if(WITH_AUDASPACE AND WITH_SYSTEM_AUDASPACE)
439     target_link_libraries(${target} ${AUDASPACE_C_LIBRARIES} ${AUDASPACE_PY_LIBRARIES})
440   endif()
441   if(WITH_OPENAL)
442     target_link_libraries(${target} ${OPENAL_LIBRARY})
443   endif()
444   if(WITH_FFTW3)
445     target_link_libraries(${target} ${FFTW3_LIBRARIES})
446   endif()
447   if(WITH_JACK AND NOT WITH_JACK_DYNLOAD)
448     target_link_libraries(${target} ${JACK_LIBRARIES})
449   endif()
450   if(WITH_CODEC_SNDFILE)
451     target_link_libraries(${target} ${LIBSNDFILE_LIBRARIES})
452   endif()
453   if(WITH_SDL AND NOT WITH_SDL_DYNLOAD)
454     target_link_libraries(${target} ${SDL_LIBRARY})
455   endif()
456   if(WITH_CYCLES_OSL)
457     target_link_libraries(${target} ${OSL_LIBRARIES})
458   endif()
459   if(WITH_OPENVDB)
460     target_link_libraries(${target} ${OPENVDB_LIBRARIES} ${TBB_LIBRARIES} ${BLOSC_LIBRARIES})
461   endif()
462   if(WITH_OPENIMAGEIO)
463     target_link_libraries(${target} ${OPENIMAGEIO_LIBRARIES})
464   endif()
465   if(WITH_OPENCOLORIO)
466     target_link_libraries(${target} ${OPENCOLORIO_LIBRARIES})
467   endif()
468   if(WITH_OPENSUBDIV)
469       target_link_libraries(${target} ${OPENSUBDIV_LIBRARIES})
470   endif()
471   if(WITH_CYCLES_EMBREE)
472     target_link_libraries(${target} ${EMBREE_LIBRARIES})
473   endif()
474   if(WITH_BOOST)
475     target_link_libraries(${target} ${BOOST_LIBRARIES})
476     if(Boost_USE_STATIC_LIBS AND Boost_USE_ICU)
477       target_link_libraries(${target} ${ICU_LIBRARIES})
478     endif()
479   endif()
480   target_link_libraries(${target} ${JPEG_LIBRARIES})
481   if(WITH_ALEMBIC)
482     target_link_libraries(${target} ${ALEMBIC_LIBRARIES} ${HDF5_LIBRARIES})
483   endif()
484   if(WITH_IMAGE_TIFF)
485     target_link_libraries(${target} ${TIFF_LIBRARY})
486   endif()
487   if(WITH_IMAGE_OPENEXR)
488     target_link_libraries(${target} ${OPENEXR_LIBRARIES})
489   endif()
490   if(WITH_IMAGE_OPENJPEG)
491     target_link_libraries(${target} ${OPENJPEG_LIBRARIES})
492   endif()
493   if(WITH_CODEC_FFMPEG)
494     target_link_libraries(${target} ${FFMPEG_LIBRARIES})
495   endif()
496   if(WITH_OPENCOLLADA)
497     if(WIN32 AND NOT UNIX)
498       file_list_suffix(OPENCOLLADA_LIBRARIES_DEBUG "${OPENCOLLADA_LIBRARIES}" "_d")
499       target_link_libraries_debug(${target} "${OPENCOLLADA_LIBRARIES_DEBUG}")
500       target_link_libraries_optimized(${target} "${OPENCOLLADA_LIBRARIES}")
501       unset(OPENCOLLADA_LIBRARIES_DEBUG)
502
503       file_list_suffix(PCRE_LIBRARIES_DEBUG "${PCRE_LIBRARIES}" "_d")
504       target_link_libraries_debug(${target} "${PCRE_LIBRARIES_DEBUG}")
505       target_link_libraries_optimized(${target} "${PCRE_LIBRARIES}")
506       unset(PCRE_LIBRARIES_DEBUG)
507
508       if(EXPAT_LIB)
509         file_list_suffix(EXPAT_LIB_DEBUG "${EXPAT_LIB}" "_d")
510         target_link_libraries_debug(${target} "${EXPAT_LIB_DEBUG}")
511         target_link_libraries_optimized(${target} "${EXPAT_LIB}")
512         unset(EXPAT_LIB_DEBUG)
513       endif()
514     else()
515       target_link_libraries(
516         ${target}
517         ${OPENCOLLADA_LIBRARIES}
518         ${PCRE_LIBRARIES}
519         ${XML2_LIBRARIES}
520         ${EXPAT_LIB}
521       )
522     endif()
523   endif()
524   if(WITH_MOD_CLOTH_ELTOPO)
525     target_link_libraries(${target} ${LAPACK_LIBRARIES})
526   endif()
527   if(WITH_LLVM)
528     target_link_libraries(${target} ${LLVM_LIBRARY})
529   endif()
530   if(WIN32 AND NOT UNIX)
531     target_link_libraries(${target} ${PTHREADS_LIBRARIES})
532   endif()
533   if(UNIX AND NOT APPLE)
534     if(WITH_OPENMP_STATIC)
535       target_link_libraries(${target} ${OpenMP_LIBRARIES})
536     endif()
537     if(WITH_INPUT_NDOF)
538       target_link_libraries(${target} ${NDOF_LIBRARIES})
539     endif()
540   endif()
541   if(WITH_SYSTEM_GLOG)
542     target_link_libraries(${target} ${GLOG_LIBRARIES})
543   endif()
544   if(WITH_SYSTEM_GFLAGS)
545     target_link_libraries(${target} ${GFLAGS_LIBRARIES})
546   endif()
547
548   # We put CLEW and CUEW here because OPENSUBDIV_LIBRARIES depends on them..
549   if(WITH_CYCLES OR WITH_COMPOSITOR OR WITH_OPENSUBDIV)
550     target_link_libraries(${target} "extern_clew")
551     if(WITH_CUDA_DYNLOAD)
552       target_link_libraries(${target} "extern_cuew")
553     else()
554       target_link_libraries(${target} ${CUDA_CUDA_LIBRARY})
555     endif()
556   endif()
557
558   target_link_libraries(
559     ${target}
560     ${ZLIB_LIBRARIES}
561   )
562
563   #system libraries with no dependencies such as platform link libs or opengl should go last
564   target_link_libraries(${target}
565       ${BLENDER_GL_LIBRARIES})
566
567   #target_link_libraries(${target} ${PLATFORM_LINKLIBS} ${CMAKE_DL_LIBS})
568   target_link_libraries(${target} ${PLATFORM_LINKLIBS})
569 endfunction()
570
571 macro(TEST_SSE_SUPPORT
572   _sse_flags
573   _sse2_flags)
574
575   include(CheckCSourceRuns)
576
577   # message(STATUS "Detecting SSE support")
578   if(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
579     set(${_sse_flags} "-msse")
580     set(${_sse2_flags} "-msse2")
581   elseif(MSVC)
582     # x86_64 has this auto enabled
583     if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
584       set(${_sse_flags} "")
585       set(${_sse2_flags} "")
586     else()
587       set(${_sse_flags} "/arch:SSE")
588       set(${_sse2_flags} "/arch:SSE2")
589     endif()
590   elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
591     set(${_sse_flags} "")  # icc defaults to -msse
592     set(${_sse2_flags} "")  # icc defaults to -msse2
593   else()
594     message(WARNING "SSE flags for this compiler: '${CMAKE_C_COMPILER_ID}' not known")
595     set(${_sse_flags})
596     set(${_sse2_flags})
597   endif()
598
599   set(CMAKE_REQUIRED_FLAGS "${${_sse_flags}} ${${_sse2_flags}}")
600
601   if(NOT DEFINED SUPPORT_SSE_BUILD)
602     # result cached
603     check_c_source_runs("
604       #include <xmmintrin.h>
605       int main(void) { __m128 v = _mm_setzero_ps(); return 0; }"
606     SUPPORT_SSE_BUILD)
607
608     if(SUPPORT_SSE_BUILD)
609       message(STATUS "SSE Support: detected.")
610     else()
611       message(STATUS "SSE Support: missing.")
612     endif()
613   endif()
614
615   if(NOT DEFINED SUPPORT_SSE2_BUILD)
616     # result cached
617     check_c_source_runs("
618       #include <emmintrin.h>
619       int main(void) { __m128d v = _mm_setzero_pd(); return 0; }"
620     SUPPORT_SSE2_BUILD)
621
622     if(SUPPORT_SSE2_BUILD)
623       message(STATUS "SSE2 Support: detected.")
624     else()
625       message(STATUS "SSE2 Support: missing.")
626     endif()
627   endif()
628
629   unset(CMAKE_REQUIRED_FLAGS)
630 endmacro()
631
632 # Only print message if running CMake first time
633 macro(message_first_run)
634   if(FIRST_RUN)
635     message(${ARGV})
636   endif()
637 endmacro()
638
639 # when we have warnings as errors applied globally this
640 # needs to be removed for some external libs which we dont maintain.
641
642 # utility macro
643 macro(remove_cc_flag
644   _flag)
645
646   foreach(flag ${ARGV})
647     string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
648     string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
649     string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
650     string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
651     string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
652
653     string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
654     string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
655     string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
656     string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
657     string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
658   endforeach()
659   unset(flag)
660
661 endmacro()
662
663 macro(add_c_flag
664   flag)
665
666   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
667   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
668 endmacro()
669
670 macro(add_cxx_flag
671   flag)
672
673   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
674 endmacro()
675
676 macro(remove_strict_flags)
677
678   if(CMAKE_COMPILER_IS_GNUCC)
679     remove_cc_flag(
680       "-Wstrict-prototypes"
681       "-Wmissing-prototypes"
682       "-Wmissing-declarations"
683       "-Wmissing-format-attribute"
684       "-Wunused-local-typedefs"
685       "-Wunused-macros"
686       "-Wunused-parameter"
687       "-Wwrite-strings"
688       "-Wredundant-decls"
689       "-Wundef"
690       "-Wshadow"
691       "-Wdouble-promotion"
692       "-Wold-style-definition"
693       "-Werror=[^ ]+"
694       "-Werror"
695     )
696
697     # negate flags implied by '-Wall'
698     add_c_flag("${C_REMOVE_STRICT_FLAGS}")
699     add_cxx_flag("${CXX_REMOVE_STRICT_FLAGS}")
700   endif()
701
702   if(CMAKE_C_COMPILER_ID MATCHES "Clang")
703     remove_cc_flag(
704       "-Wunused-parameter"
705       "-Wunused-variable"
706       "-Werror=[^ ]+"
707       "-Werror"
708     )
709
710     # negate flags implied by '-Wall'
711     add_c_flag("${C_REMOVE_STRICT_FLAGS}")
712     add_cxx_flag("${CXX_REMOVE_STRICT_FLAGS}")
713   endif()
714
715   if(MSVC)
716     remove_cc_flag(/w34189) # Restore warn C4189 (unused variable) back to w4 
717   endif()
718
719 endmacro()
720
721 macro(remove_extra_strict_flags)
722   if(CMAKE_COMPILER_IS_GNUCC)
723     remove_cc_flag(
724       "-Wunused-parameter"
725     )
726   endif()
727
728   if(CMAKE_C_COMPILER_ID MATCHES "Clang")
729     remove_cc_flag(
730       "-Wunused-parameter"
731     )
732   endif()
733
734   if(MSVC)
735     # TODO
736   endif()
737 endmacro()
738
739 # note, we can only append flags on a single file so we need to negate the options.
740 # at the moment we cant shut up ffmpeg deprecations, so use this, but will
741 # probably add more removals here.
742 macro(remove_strict_c_flags_file
743   filenames)
744   foreach(_SOURCE ${ARGV})
745     if(CMAKE_COMPILER_IS_GNUCC OR
746        (CMAKE_C_COMPILER_ID MATCHES "Clang"))
747       set_source_files_properties(${_SOURCE}
748         PROPERTIES
749           COMPILE_FLAGS "${C_REMOVE_STRICT_FLAGS}"
750       )
751     endif()
752     if(MSVC)
753       # TODO
754     endif()
755   endforeach()
756   unset(_SOURCE)
757 endmacro()
758
759 macro(remove_strict_cxx_flags_file
760   filenames)
761   remove_strict_c_flags_file(${filenames} ${ARHV})
762   foreach(_SOURCE ${ARGV})
763     if(CMAKE_COMPILER_IS_GNUCC OR
764        (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
765       set_source_files_properties(${_SOURCE}
766         PROPERTIES
767           COMPILE_FLAGS "${CXX_REMOVE_STRICT_FLAGS}"
768       )
769     endif()
770     if(MSVC)
771       # TODO
772     endif()
773   endforeach()
774   unset(_SOURCE)
775 endmacro()
776
777 # External libs may need 'signed char' to be default.
778 macro(remove_cc_flag_unsigned_char)
779   if(CMAKE_COMPILER_IS_GNUCC OR
780      (CMAKE_C_COMPILER_ID MATCHES "Clang") OR
781      (CMAKE_C_COMPILER_ID MATCHES "Intel"))
782     remove_cc_flag("-funsigned-char")
783   elseif(MSVC)
784     remove_cc_flag("/J")
785   else()
786     message(WARNING
787       "Compiler '${CMAKE_C_COMPILER_ID}' failed to disable 'unsigned char' flag."
788       "Build files need updating."
789     )
790   endif()
791 endmacro()
792
793 function(ADD_CHECK_C_COMPILER_FLAG
794   _CFLAGS
795   _CACHE_VAR
796   _FLAG
797   )
798
799   include(CheckCCompilerFlag)
800
801   CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
802   if(${_CACHE_VAR})
803     # message(STATUS "Using CFLAG: ${_FLAG}")
804     set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}" PARENT_SCOPE)
805   else()
806     message(STATUS "Unsupported CFLAG: ${_FLAG}")
807   endif()
808 endfunction()
809
810 function(ADD_CHECK_CXX_COMPILER_FLAG
811   _CXXFLAGS
812   _CACHE_VAR
813   _FLAG
814   )
815
816   include(CheckCXXCompilerFlag)
817
818   CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
819   if(${_CACHE_VAR})
820     # message(STATUS "Using CXXFLAG: ${_FLAG}")
821     set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}" PARENT_SCOPE)
822   else()
823     message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
824   endif()
825 endfunction()
826
827 function(get_blender_version)
828   # extracts header vars and defines them in the parent scope:
829   #
830   # - BLENDER_VERSION (major.minor)
831   # - BLENDER_VERSION_MAJOR
832   # - BLENDER_VERSION_MINOR
833   # - BLENDER_SUBVERSION (used for internal versioning mainly)
834   # - BLENDER_VERSION_CHAR (a, b, c, ...or empty string)
835   # - BLENDER_VERSION_CYCLE (alpha, beta, rc, release)
836
837   # So cmake depends on BKE_blender.h, beware of inf-loops!
838   CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender_version.h
839                  ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender_version.h.done)
840
841   file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender_version.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
842
843   string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
844   string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
845   string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
846   string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
847
848   if(NOT ${_out_version} MATCHES "[0-9]+")
849     message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
850   endif()
851
852   if(NOT ${_out_subversion} MATCHES "[0-9]+")
853     message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
854   endif()
855
856   # clumsy regex, only single char are ok but it could be unset
857
858   string(LENGTH "${_out_version_char}" _out_version_char_len)
859   if(NOT _out_version_char_len EQUAL 1)
860     set(_out_version_char "")
861   elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
862     message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
863   endif()
864
865   if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
866     message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
867   endif()
868
869   math(EXPR _out_version_major "${_out_version} / 100")
870   math(EXPR _out_version_minor "${_out_version} % 100")
871
872   # for packaging, alpha to numbers
873   string(COMPARE EQUAL "${_out_version_char}" "" _out_version_char_empty)
874   if(${_out_version_char_empty})
875     set(_out_version_char_index "0")
876   else()
877     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)
878     list(FIND _char_ls ${_out_version_char} _out_version_char_index)
879     math(EXPR _out_version_char_index "${_out_version_char_index} + 1")
880   endif()
881
882   # output vars
883   set(BLENDER_VERSION "${_out_version_major}.${_out_version_minor}" PARENT_SCOPE)
884   set(BLENDER_VERSION_MAJOR "${_out_version_major}" PARENT_SCOPE)
885   set(BLENDER_VERSION_MINOR "${_out_version_minor}" PARENT_SCOPE)
886   set(BLENDER_SUBVERSION "${_out_subversion}" PARENT_SCOPE)
887   set(BLENDER_VERSION_CHAR "${_out_version_char}" PARENT_SCOPE)
888   set(BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index}" PARENT_SCOPE)
889   set(BLENDER_VERSION_CYCLE "${_out_version_cycle}" PARENT_SCOPE)
890
891 endfunction()
892
893
894 # hacks to override initial project settings
895 # these macros must be called directly before/after project(Blender)
896 macro(blender_project_hack_pre)
897   # ------------------
898   # GCC -O3 HACK START
899   # needed because O3 can cause problems but
900   # allow the builder to set O3 manually after.
901   if(DEFINED CMAKE_C_FLAGS_RELEASE)
902     set(_reset_standard_cflags_rel OFF)
903   else()
904     set(_reset_standard_cflags_rel ON)
905   endif()
906   if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
907     set(_reset_standard_cxxflags_rel OFF)
908   else()
909     set(_reset_standard_cxxflags_rel ON)
910   endif()
911 endmacro()
912
913
914 macro(blender_project_hack_post)
915   # ----------------
916   # GCC -O3 HACK END
917   if(_reset_standard_cflags_rel)
918     string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
919     set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
920     mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
921   endif()
922
923   if(_reset_standard_cxxflags_rel)
924     string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
925     set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
926     mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
927   endif()
928
929   unset(_reset_standard_cflags_rel)
930   unset(_reset_standard_cxxflags_rel)
931
932   # ------------------------------------------------------------------
933   # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
934   if(CMAKE_COMPILER_IS_GNUCC)
935     if(NOT DARWIN)
936       set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
937     endif()
938   endif()
939
940 endmacro()
941
942 # pair of macros to allow libraries to be specify files to install, but to
943 # only install them at the end so the directories don't get cleared with
944 # the files in them. used by cycles to install addon.
945 function(delayed_install
946   base
947   files
948   destination)
949
950   foreach(f ${files})
951     if(IS_ABSOLUTE ${f})
952       set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${f})
953     else()
954       set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${base}/${f})
955     endif()
956     set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_DESTINATIONS ${destination})
957   endforeach()
958   unset(f)
959 endfunction()
960
961 # note this is a function instead of a macro so that ${BUILD_TYPE} in targetdir
962 # does not get expanded in calling but is preserved
963 function(delayed_do_install
964   targetdir)
965
966   get_property(files GLOBAL PROPERTY DELAYED_INSTALL_FILES)
967   get_property(destinations GLOBAL PROPERTY DELAYED_INSTALL_DESTINATIONS)
968
969   if(files)
970     list(LENGTH files n)
971     math(EXPR n "${n}-1")
972
973     foreach(i RANGE ${n})
974       list(GET files ${i} f)
975       list(GET destinations ${i} d)
976       if(NOT IS_ABSOLUTE ${d})
977         install(FILES ${f} DESTINATION ${targetdir}/${d})
978       else()
979         install(FILES ${f} DESTINATION ${d})
980       endif()
981     endforeach()
982   endif()
983 endfunction()
984
985
986 function(data_to_c
987   file_from file_to
988   list_to_add
989   )
990
991   list(APPEND ${list_to_add} ${file_to})
992   set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
993
994   get_filename_component(_file_to_path ${file_to} PATH)
995
996   add_custom_command(
997     OUTPUT ${file_to}
998     COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
999     COMMAND "$<TARGET_FILE:datatoc>" ${file_from} ${file_to}
1000     DEPENDS ${file_from} datatoc)
1001
1002   set_source_files_properties(${file_to} PROPERTIES GENERATED TRUE)
1003 endfunction()
1004
1005
1006 # same as above but generates the var name and output automatic.
1007 function(data_to_c_simple
1008   file_from
1009   list_to_add
1010   )
1011
1012   # remove ../'s
1013   get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from}   REALPATH)
1014   get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${file_from}.c REALPATH)
1015
1016   list(APPEND ${list_to_add} ${_file_to})
1017   source_group(Generated FILES ${_file_to})
1018   list(APPEND ${list_to_add} ${file_from})
1019   set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1020
1021   get_filename_component(_file_to_path ${_file_to} PATH)
1022
1023   add_custom_command(
1024     OUTPUT  ${_file_to}
1025     COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1026     COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1027     DEPENDS ${_file_from} datatoc)
1028
1029   set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1030 endfunction()
1031
1032 # macro for converting pixmap directory to a png and then a c file
1033 function(data_to_c_simple_icons
1034   path_from icon_prefix icon_names
1035   list_to_add
1036   )
1037
1038   # Conversion steps
1039   #  path_from  ->  _file_from  ->  _file_to
1040   #  foo/*.dat  ->  foo.png     ->  foo.png.c
1041
1042   get_filename_component(_path_from_abs ${path_from} ABSOLUTE)
1043   # remove ../'s
1044   get_filename_component(_file_from ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png   REALPATH)
1045   get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png.c REALPATH)
1046
1047   list(APPEND ${list_to_add} ${_file_to})
1048   set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1049
1050   get_filename_component(_file_to_path ${_file_to} PATH)
1051
1052   # Construct a list of absolute paths from input
1053   set(_icon_files)
1054   foreach(_var ${icon_names})
1055     list(APPEND _icon_files "${_path_from_abs}/${icon_prefix}${_var}.dat")
1056   endforeach()
1057
1058   add_custom_command(
1059     OUTPUT  ${_file_from} ${_file_to}
1060     COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1061     #COMMAND python3 ${CMAKE_SOURCE_DIR}/source/blender/datatoc/datatoc_icon.py ${_path_from_abs} ${_file_from}
1062     COMMAND "$<TARGET_FILE:datatoc_icon>" ${_path_from_abs} ${_file_from}
1063     COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1064     DEPENDS
1065       ${_icon_files}
1066       datatoc_icon
1067       datatoc
1068       # could be an arg but for now we only create icons depending on UI_icons.h
1069       ${CMAKE_SOURCE_DIR}/source/blender/editors/include/UI_icons.h
1070     )
1071
1072   set_source_files_properties(${_file_from} ${_file_to} PROPERTIES GENERATED TRUE)
1073 endfunction()
1074
1075 # XXX Not used for now...
1076 function(svg_to_png
1077   file_from
1078   file_to
1079   dpi
1080   list_to_add
1081   )
1082
1083   # remove ../'s
1084   get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from} REALPATH)
1085   get_filename_component(_file_to   ${CMAKE_CURRENT_SOURCE_DIR}/${file_to}   REALPATH)
1086
1087   list(APPEND ${list_to_add} ${_file_to})
1088   set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1089
1090   find_program(INKSCAPE_EXE inkscape)
1091   mark_as_advanced(INKSCAPE_EXE)
1092
1093   if(INKSCAPE_EXE)
1094     if(APPLE)
1095       # in OS X app bundle, the binary is a shim that doesn't take any
1096       # command line arguments, replace it with the actual binary
1097       string(REPLACE "MacOS/Inkscape" "Resources/bin/inkscape" INKSCAPE_REAL_EXE ${INKSCAPE_EXE})
1098       if(EXISTS "${INKSCAPE_REAL_EXE}")
1099         set(INKSCAPE_EXE ${INKSCAPE_REAL_EXE})
1100       endif()
1101     endif()
1102
1103     add_custom_command(
1104       OUTPUT  ${_file_to}
1105       COMMAND ${INKSCAPE_EXE} ${_file_from} --export-dpi=${dpi}  --without-gui --export-png=${_file_to}
1106       DEPENDS ${_file_from} ${INKSCAPE_EXE}
1107     )
1108   else()
1109     message(WARNING "Inkscape not found, could not re-generate ${_file_to} from ${_file_from}!")
1110   endif()
1111 endfunction()
1112
1113 function(msgfmt_simple
1114   file_from
1115   list_to_add
1116   )
1117
1118   # remove ../'s
1119   get_filename_component(_file_from_we ${file_from} NAME_WE)
1120
1121   get_filename_component(_file_from ${file_from} REALPATH)
1122   get_filename_component(_file_to ${CMAKE_CURRENT_BINARY_DIR}/${_file_from_we}.mo REALPATH)
1123
1124   list(APPEND ${list_to_add} ${_file_to})
1125   set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1126
1127   get_filename_component(_file_to_path ${_file_to} PATH)
1128
1129   add_custom_command(
1130     OUTPUT  ${_file_to}
1131     COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1132     COMMAND "$<TARGET_FILE:msgfmt>" ${_file_from} ${_file_to}
1133     DEPENDS msgfmt ${_file_from})
1134
1135   set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1136 endfunction()
1137
1138 function(find_python_package
1139   package
1140   )
1141
1142   string(TOUPPER ${package} _upper_package)
1143
1144   # set but invalid
1145   if((NOT ${PYTHON_${_upper_package}_PATH} STREQUAL "") AND
1146      (NOT ${PYTHON_${_upper_package}_PATH} MATCHES NOTFOUND))
1147 #       if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}/${package}")
1148 #           message(WARNING "PYTHON_${_upper_package}_PATH is invalid, ${package} not found in '${PYTHON_${_upper_package}_PATH}' "
1149 #                           "WITH_PYTHON_INSTALL_${_upper_package} option will be ignored when installing python")
1150 #           set(WITH_PYTHON_INSTALL${_upper_package} OFF)
1151 #       endif()
1152   # not set, so initialize
1153   else()
1154     string(REPLACE "." ";" _PY_VER_SPLIT "${PYTHON_VERSION}")
1155     list(GET _PY_VER_SPLIT 0 _PY_VER_MAJOR)
1156
1157     # re-cache
1158     unset(PYTHON_${_upper_package}_PATH CACHE)
1159     find_path(PYTHON_${_upper_package}_PATH
1160       NAMES
1161         ${package}
1162       HINTS
1163         "${PYTHON_LIBPATH}/"
1164         "${PYTHON_LIBPATH}/python${PYTHON_VERSION}/"
1165         "${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/"
1166       PATH_SUFFIXES
1167         site-packages
1168         dist-packages
1169         vendor-packages
1170        NO_DEFAULT_PATH
1171     )
1172
1173     if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}")
1174       message(WARNING
1175         "Python package '${package}' path could not be found in:\n"
1176         "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/site-packages/${package}', "
1177         "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/site-packages/${package}', "
1178         "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/dist-packages/${package}', "
1179         "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/dist-packages/${package}', "
1180         "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/vendor-packages/${package}', "
1181         "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/vendor-packages/${package}', "
1182         "\n"
1183         "The 'WITH_PYTHON_INSTALL_${_upper_package}' option will be ignored when installing Python.\n"
1184         "The build will be usable, only add-ons that depend on this package won't be functional."
1185       )
1186       set(WITH_PYTHON_INSTALL_${_upper_package} OFF PARENT_SCOPE)
1187     else()
1188       message(STATUS "${package} found at '${PYTHON_${_upper_package}_PATH}'")
1189     endif()
1190   endif()
1191 endfunction()
1192
1193 # like Python's 'print(dir())'
1194 function(print_all_vars)
1195   get_cmake_property(_vars VARIABLES)
1196   foreach(_var ${_vars})
1197     message("${_var}=${${_var}}")
1198   endforeach()
1199 endfunction()
1200
1201 macro(openmp_delayload
1202   projectname
1203   )
1204     if(MSVC)
1205       if(WITH_OPENMP)
1206         if(MSVC_VERSION EQUAL 1800)
1207           set(OPENMP_DLL_NAME "vcomp120")
1208         else()
1209           set(OPENMP_DLL_NAME "vcomp140")
1210         endif()
1211         SET_TARGET_PROPERTIES(${projectname} PROPERTIES LINK_FLAGS_RELEASE "/DELAYLOAD:${OPENMP_DLL_NAME}.dll delayimp.lib")
1212         SET_TARGET_PROPERTIES(${projectname} PROPERTIES LINK_FLAGS_DEBUG "/DELAYLOAD:${OPENMP_DLL_NAME}d.dll delayimp.lib")
1213         SET_TARGET_PROPERTIES(${projectname} PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/DELAYLOAD:${OPENMP_DLL_NAME}.dll delayimp.lib")
1214         SET_TARGET_PROPERTIES(${projectname} PROPERTIES LINK_FLAGS_MINSIZEREL "/DELAYLOAD:${OPENMP_DLL_NAME}.dll delayimp.lib")
1215       endif()
1216     endif()
1217 endmacro()
1218
1219 macro(WINDOWS_SIGN_TARGET target)
1220   if(WITH_WINDOWS_CODESIGN)
1221     if(!SIGNTOOL_EXE)
1222       error("Codesigning is enabled, but signtool is not found")
1223     else()
1224       if(WINDOWS_CODESIGN_PFX_PASSWORD)
1225         set(CODESIGNPASSWORD /p ${WINDOWS_CODESIGN_PFX_PASSWORD})
1226       else()
1227         if($ENV{PFXPASSWORD})
1228           set(CODESIGNPASSWORD /p $ENV{PFXPASSWORD})
1229         else()
1230           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.")
1231         endif()
1232       endif()
1233       add_custom_command(TARGET ${target}
1234         POST_BUILD
1235         COMMAND ${SIGNTOOL_EXE} sign /f ${WINDOWS_CODESIGN_PFX} ${CODESIGNPASSWORD} $<TARGET_FILE:${target}>
1236         VERBATIM
1237       )
1238     endif()
1239   endif()
1240 endmacro()
1241
1242 macro(blender_precompile_headers target cpp header)
1243   if(MSVC)
1244     # get the name for the pch output file
1245     get_filename_component( pchbase ${cpp} NAME_WE )
1246     set( pchfinal "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${pchbase}.pch" )
1247
1248     # mark the cpp as the one outputting the pch
1249     set_property(SOURCE ${cpp} APPEND PROPERTY OBJECT_OUTPUTS "${pchfinal}")
1250
1251     # get all sources for the target
1252     get_target_property(sources ${target} SOURCES)
1253
1254     # make all sources depend on the pch to enforce the build order
1255     foreach(src ${sources})
1256       set_property(SOURCE ${src} APPEND PROPERTY OBJECT_DEPENDS "${pchfinal}")
1257     endforeach()
1258
1259     target_sources(${target} PRIVATE ${cpp} ${header})
1260     set_target_properties(${target} PROPERTIES COMPILE_FLAGS "/Yu${header} /Fp${pchfinal} /FI${header}")
1261     set_source_files_properties(${cpp} PROPERTIES COMPILE_FLAGS "/Yc${header} /Fp${pchfinal}")
1262   endif()
1263 endmacro()