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