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