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