OpenVDB: Get rid of hardcoded TBB variables and enabled for Linux buildbot
[blender-staging.git] / build_files / cmake / macros.cmake
1 # ***** BEGIN GPL LICENSE BLOCK *****
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # The Original Code is Copyright (C) 2006, Blender Foundation
18 # All rights reserved.
19 #
20 # The Original Code is: all of this file.
21 #
22 # Contributor(s): Jacques Beaurain.
23 #
24 # ***** END GPL LICENSE BLOCK *****
25
26 macro(list_insert_after
27         list_id item_check item_add
28         )
29         set(_index)
30         list(FIND "${list_id}" "${item_check}" _index)
31         if("${_index}" MATCHES "-1")
32                 message(FATAL_ERROR "'${list_id}' doesn't contain '${item_check}'")
33         endif()
34         math(EXPR _index "${_index} + 1")
35         list(INSERT ${list_id} "${_index}" ${item_add})
36         unset(_index)
37 endmacro()
38
39 macro(list_insert_before
40         list_id item_check item_add
41         )
42         set(_index)
43         list(FIND "${list_id}" "${item_check}" _index)
44         if("${_index}" MATCHES "-1")
45                 message(FATAL_ERROR "'${list_id}' doesn't contain '${item_check}'")
46         endif()
47         list(INSERT ${list_id} "${_index}" ${item_add})
48         unset(_index)
49 endmacro()
50
51 function(list_assert_duplicates
52         list_id
53         )
54         
55         # message(STATUS "list data: ${list_id}")
56
57         list(LENGTH list_id _len_before)
58         list(REMOVE_DUPLICATES list_id)
59         list(LENGTH list_id _len_after)
60         # message(STATUS "list size ${_len_before} -> ${_len_after}")
61         if(NOT _len_before EQUAL _len_after)
62                 message(FATAL_ERROR "duplicate found in list which should not contain duplicates: ${list_id}")
63         endif()
64         unset(_len_before)
65         unset(_len_after)
66 endfunction()
67
68
69 # foo_bar.spam --> foo_barMySuffix.spam
70 macro(file_suffix
71         file_name_new file_name file_suffix
72         )
73
74         get_filename_component(_file_name_PATH ${file_name} PATH)
75         get_filename_component(_file_name_NAME_WE ${file_name} NAME_WE)
76         get_filename_component(_file_name_EXT ${file_name} EXT)
77         set(${file_name_new} "${_file_name_PATH}/${_file_name_NAME_WE}${file_suffix}${_file_name_EXT}")
78
79         unset(_file_name_PATH)
80         unset(_file_name_NAME_WE)
81         unset(_file_name_EXT)
82 endmacro()
83
84 # useful for adding debug suffix to library lists:
85 # /somepath/foo.lib --> /somepath/foo_d.lib
86 macro(file_list_suffix
87         fp_list_new fp_list fn_suffix
88         )
89
90         # incase of empty list
91         set(_fp)
92         set(_fp_suffixed)
93
94         set(fp_list_new)
95
96         foreach(_fp ${fp_list})
97                 file_suffix(_fp_suffixed "${_fp}" "${fn_suffix}")
98                 list(APPEND "${fp_list_new}" "${_fp_suffixed}")
99         endforeach()
100
101         unset(_fp)
102         unset(_fp_suffixed)
103
104 endmacro()
105
106 if(UNIX AND NOT APPLE)
107         macro(find_package_static)
108                 set(_cmake_find_library_suffixes_back ${CMAKE_FIND_LIBRARY_SUFFIXES})
109                 set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
110                 find_package(${ARGV})
111                 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_cmake_find_library_suffixes_back})
112                 unset(_cmake_find_library_suffixes_back)
113         endmacro()
114
115         macro(find_library_static)
116                 set(_cmake_find_library_suffixes_back ${CMAKE_FIND_LIBRARY_SUFFIXES})
117                 set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
118                 find_library(${ARGV})
119                 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_cmake_find_library_suffixes_back})
120                 unset(_cmake_find_library_suffixes_back)
121         endmacro()
122 endif()
123
124 function(target_link_libraries_optimized
125         TARGET
126         LIBS
127         )
128
129         foreach(_LIB ${LIBS})
130                 target_link_libraries(${TARGET} optimized "${_LIB}")
131         endforeach()
132 endfunction()
133
134 function(target_link_libraries_debug
135         TARGET
136         LIBS
137         )
138
139         foreach(_LIB ${LIBS})
140                 target_link_libraries(${TARGET} debug "${_LIB}")
141         endforeach()
142 endfunction()
143
144 function(target_link_libraries_decoupled
145         target
146         libraries_var
147         )
148
149         if(NOT MSVC)
150                 target_link_libraries(${target} ${${libraries_var}})
151         else()
152                 # For MSVC we link to different libraries depending whether
153                 # release or debug target is being built.
154                 file_list_suffix(_libraries_debug "${${libraries_var}}" "_d")
155                 target_link_libraries_debug(${target} "${_libraries_debug}")
156                 target_link_libraries_optimized(${target} "${${libraries_var}}")
157         endif()
158 endfunction()
159
160 # Nicer makefiles with -I/1/foo/ instead of -I/1/2/3/../../foo/
161 # use it instead of include_directories()
162 function(blender_include_dirs
163         includes
164         )
165
166         set(_ALL_INCS "")
167         foreach(_INC ${ARGV})
168                 get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
169                 list(APPEND _ALL_INCS ${_ABS_INC})
170                 # for checking for invalid includes, disable for regular use
171                 ##if(NOT EXISTS "${_ABS_INC}/")
172                 ##      message(FATAL_ERROR "Include not found: ${_ABS_INC}/")
173                 ##endif()
174         endforeach()
175         include_directories(${_ALL_INCS})
176 endfunction()
177
178 function(blender_include_dirs_sys
179         includes
180         )
181
182         set(_ALL_INCS "")
183         foreach(_INC ${ARGV})
184                 get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
185                 list(APPEND _ALL_INCS ${_ABS_INC})
186                 ##if(NOT EXISTS "${_ABS_INC}/")
187                 ##      message(FATAL_ERROR "Include not found: ${_ABS_INC}/")
188                 ##endif()
189         endforeach()
190         include_directories(SYSTEM ${_ALL_INCS})
191 endfunction()
192
193 function(blender_source_group
194         sources
195         )
196
197         # Group by location on disk
198         source_group("Source Files" FILES CMakeLists.txt)
199
200         foreach(_SRC ${sources})
201                 get_filename_component(_SRC_EXT ${_SRC} EXT)
202                 if((${_SRC_EXT} MATCHES ".h") OR
203                    (${_SRC_EXT} MATCHES ".hpp") OR
204                    (${_SRC_EXT} MATCHES ".hh"))
205
206                         set(GROUP_ID "Header Files")
207                 else()
208                         set(GROUP_ID "Source Files")
209                 endif()
210                 source_group("${GROUP_ID}" FILES ${_SRC})
211         endforeach()
212 endfunction()
213
214
215 # only MSVC uses SOURCE_GROUP
216 function(blender_add_lib_nolist
217         name
218         sources
219         includes
220         includes_sys
221         )
222
223         # message(STATUS "Configuring library ${name}")
224
225         # include_directories(${includes})
226         # include_directories(SYSTEM ${includes_sys})
227         blender_include_dirs("${includes}")
228         blender_include_dirs_sys("${includes_sys}")
229
230         add_library(${name} ${sources})
231
232         # works fine without having the includes
233         # listed is helpful for IDE's (QtCreator/MSVC)
234         blender_source_group("${sources}")
235
236         list_assert_duplicates("${sources}")
237         list_assert_duplicates("${includes}")
238         # Not for system includes because they can resolve to the same path
239         # list_assert_duplicates("${includes_sys}")
240
241 endfunction()
242
243
244 function(blender_add_lib
245         name
246         sources
247         includes
248         includes_sys
249         )
250
251         blender_add_lib_nolist(${name} "${sources}" "${includes}" "${includes_sys}")
252
253         set_property(GLOBAL APPEND PROPERTY BLENDER_LINK_LIBS ${name})
254 endfunction()
255
256
257 function(SETUP_LIBDIRS)
258
259         link_directories(${JPEG_LIBPATH} ${PNG_LIBPATH} ${ZLIB_LIBPATH} ${FREETYPE_LIBPATH})
260
261         if(WITH_PYTHON)  #  AND NOT WITH_PYTHON_MODULE  # WIN32 needs
262                 link_directories(${PYTHON_LIBPATH})
263         endif()
264         if(WITH_SDL AND NOT WITH_SDL_DYNLOAD)
265                 link_directories(${SDL_LIBPATH})
266         endif()
267         if(WITH_CODEC_FFMPEG)
268                 link_directories(${FFMPEG_LIBPATH})
269         endif()
270         if(WITH_IMAGE_OPENEXR)
271                 link_directories(${OPENEXR_LIBPATH})
272         endif()
273         if(WITH_IMAGE_TIFF)
274                 link_directories(${TIFF_LIBPATH})
275         endif()
276         if(WITH_BOOST)
277                 link_directories(${BOOST_LIBPATH})
278         endif()
279         if(WITH_OPENIMAGEIO)
280                 link_directories(${OPENIMAGEIO_LIBPATH})
281         endif()
282         if(WITH_OPENCOLORIO)
283                 link_directories(${OPENCOLORIO_LIBPATH})
284         endif()
285         if(WITH_OPENVDB)
286                 link_directories(${OPENVDB_LIBPATH})
287         endif()
288         if(WITH_IMAGE_OPENJPEG AND WITH_SYSTEM_OPENJPEG)
289                 link_directories(${OPENJPEG_LIBPATH})
290         endif()
291         if(WITH_CODEC_QUICKTIME)
292                 link_directories(${QUICKTIME_LIBPATH})
293         endif()
294         if(WITH_OPENAL)
295                 link_directories(${OPENAL_LIBPATH})
296         endif()
297         if(WITH_JACK AND NOT WITH_JACK_DYNLOAD)
298                 link_directories(${JACK_LIBPATH})
299         endif()
300         if(WITH_CODEC_SNDFILE)
301                 link_directories(${SNDFILE_LIBPATH})
302         endif()
303         if(WITH_FFTW3)
304                 link_directories(${FFTW3_LIBPATH})
305         endif()
306         if(WITH_OPENCOLLADA)
307                 link_directories(${OPENCOLLADA_LIBPATH})
308                 link_directories(${PCRE_LIBPATH})
309                 link_directories(${EXPAT_LIBPATH})
310         endif()
311         if(WITH_LLVM)
312                 link_directories(${LLVM_LIBPATH})
313         endif()
314         if(WITH_MEM_JEMALLOC)
315                 link_directories(${JEMALLOC_LIBPATH})
316         endif()
317
318         if(WIN32 AND NOT UNIX)
319                 link_directories(${PTHREADS_LIBPATH})
320         endif()
321 endfunction()
322
323 function(setup_liblinks
324         target
325         )
326
327         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
328         set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
329
330         set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
331         set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
332
333         set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
334         set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
335
336         target_link_libraries(
337                 ${target}
338                 ${PNG_LIBRARIES}
339                 ${ZLIB_LIBRARIES}
340                 ${FREETYPE_LIBRARY}
341         )
342
343         # since we are using the local libs for python when compiling msvc projects, we need to add _d when compiling debug versions
344         if(WITH_PYTHON)  # AND NOT WITH_PYTHON_MODULE  # WIN32 needs
345                 target_link_libraries(${target} ${PYTHON_LINKFLAGS})
346
347                 if(WIN32 AND NOT UNIX)
348                         file_list_suffix(PYTHON_LIBRARIES_DEBUG "${PYTHON_LIBRARIES}" "_d")
349                         target_link_libraries_debug(${target} "${PYTHON_LIBRARIES_DEBUG}")
350                         target_link_libraries_optimized(${target} "${PYTHON_LIBRARIES}")
351                         unset(PYTHON_LIBRARIES_DEBUG)
352                 else()
353                         target_link_libraries(${target} ${PYTHON_LIBRARIES})
354                 endif()
355         endif()
356
357         if(WITH_LZO AND WITH_SYSTEM_LZO)
358                 target_link_libraries(${target} ${LZO_LIBRARIES})
359         endif()
360         if(WITH_SYSTEM_GLEW)
361                 target_link_libraries(${target} ${BLENDER_GLEW_LIBRARIES})
362         endif()
363         if(WITH_BULLET AND WITH_SYSTEM_BULLET)
364                 target_link_libraries(${target} ${BULLET_LIBRARIES})
365         endif()
366         if(WITH_AUDASPACE AND WITH_SYSTEM_AUDASPACE)
367                 target_link_libraries(${target} ${AUDASPACE_C_LIBRARIES} ${AUDASPACE_PY_LIBRARIES})
368         endif()
369         if(WITH_OPENAL)
370                 target_link_libraries(${target} ${OPENAL_LIBRARY})
371         endif()
372         if(WITH_FFTW3)
373                 target_link_libraries(${target} ${FFTW3_LIBRARIES})
374         endif()
375         if(WITH_JACK AND NOT WITH_JACK_DYNLOAD)
376                 target_link_libraries(${target} ${JACK_LIBRARIES})
377         endif()
378         if(WITH_CODEC_SNDFILE)
379                 target_link_libraries(${target} ${SNDFILE_LIBRARIES})
380         endif()
381         if(WITH_SDL AND NOT WITH_SDL_DYNLOAD)
382                 target_link_libraries(${target} ${SDL_LIBRARY})
383         endif()
384         if(WITH_CODEC_QUICKTIME)
385                 target_link_libraries(${target} ${QUICKTIME_LIBRARIES})
386         endif()
387         if(WITH_IMAGE_TIFF)
388                 target_link_libraries(${target} ${TIFF_LIBRARY})
389         endif()
390         if(WITH_OPENIMAGEIO)
391                 target_link_libraries(${target} ${OPENIMAGEIO_LIBRARIES})
392         endif()
393         if(WITH_OPENCOLORIO)
394                 target_link_libraries(${target} ${OPENCOLORIO_LIBRARIES})
395         endif()
396         if(WITH_OPENSUBDIV)
397                 if(WIN32 AND NOT UNIX)
398                         file_list_suffix(OPENSUBDIV_LIBRARIES_DEBUG "${OPENSUBDIV_LIBRARIES}" "_d")
399                         target_link_libraries_debug(${target} "${OPENSUBDIV_LIBRARIES_DEBUG}")
400                         target_link_libraries_optimized(${target} "${OPENSUBDIV_LIBRARIES}")
401                         unset(OPENSUBDIV_LIBRARIES_DEBUG)
402                 else()
403                         target_link_libraries(${target} ${OPENSUBDIV_LIBRARIES})
404                 endif()
405         endif()
406         if(WITH_OPENVDB)
407                 target_link_libraries(${target} ${OPENVDB_LIBRARIES} ${TBB_LIBRARIES})
408         endif()
409         if(WITH_CYCLES_OSL)
410                 target_link_libraries(${target} ${OSL_LIBRARIES})
411         endif()
412         if(WITH_BOOST)
413                 target_link_libraries(${target} ${BOOST_LIBRARIES})
414                 if(Boost_USE_STATIC_LIBS AND Boost_USE_ICU)
415                         target_link_libraries(${target} ${ICU_LIBRARIES})
416                 endif()
417         endif()
418         target_link_libraries(${target} ${JPEG_LIBRARIES})
419         if(WITH_IMAGE_OPENEXR)
420                 if(WIN32 AND NOT UNIX AND NOT CMAKE_COMPILER_IS_GNUCC)
421                         file_list_suffix(OPENEXR_LIBRARIES_DEBUG "${OPENEXR_LIBRARIES}" "_d")
422                         target_link_libraries_debug(${target} "${OPENEXR_LIBRARIES_DEBUG}")
423                         target_link_libraries_optimized(${target} "${OPENEXR_LIBRARIES}")
424                         unset(OPENEXR_LIBRARIES_DEBUG)
425                 else()
426                         target_link_libraries(${target} ${OPENEXR_LIBRARIES})
427                 endif()
428         endif()
429         if(WITH_IMAGE_OPENJPEG AND WITH_SYSTEM_OPENJPEG)
430                 target_link_libraries(${target} ${OPENJPEG_LIBRARIES})
431         endif()
432         if(WITH_CODEC_FFMPEG)
433                 target_link_libraries(${target} ${FFMPEG_LIBRARIES})
434         endif()
435         if(WITH_OPENCOLLADA)
436                 if(WIN32 AND NOT UNIX)
437                         file_list_suffix(OPENCOLLADA_LIBRARIES_DEBUG "${OPENCOLLADA_LIBRARIES}" "_d")
438                         target_link_libraries_debug(${target} "${OPENCOLLADA_LIBRARIES_DEBUG}")
439                         target_link_libraries_optimized(${target} "${OPENCOLLADA_LIBRARIES}")
440                         unset(OPENCOLLADA_LIBRARIES_DEBUG)
441
442                         file_list_suffix(PCRE_LIBRARIES_DEBUG "${PCRE_LIBRARIES}" "_d")
443                         target_link_libraries_debug(${target} "${PCRE_LIBRARIES_DEBUG}")
444                         target_link_libraries_optimized(${target} "${PCRE_LIBRARIES}")
445                         unset(PCRE_LIBRARIES_DEBUG)
446
447                         if(EXPAT_LIB)
448                                 file_list_suffix(EXPAT_LIB_DEBUG "${EXPAT_LIB}" "_d")
449                                 target_link_libraries_debug(${target} "${EXPAT_LIB_DEBUG}")
450                                 target_link_libraries_optimized(${target} "${EXPAT_LIB}")
451                                 unset(EXPAT_LIB_DEBUG)
452                         endif()
453                 else()
454                         target_link_libraries(
455                                 ${target}
456                                 ${OPENCOLLADA_LIBRARIES}
457                                 ${PCRE_LIBRARIES}
458                                 ${XML2_LIBRARIES}
459                                 ${EXPAT_LIB}
460                         )
461                 endif()
462         endif()
463         if(WITH_MEM_JEMALLOC)
464                 target_link_libraries(${target} ${JEMALLOC_LIBRARIES})
465         endif()
466         if(WITH_INPUT_NDOF)
467                 target_link_libraries(${target} ${NDOF_LIBRARIES})
468         endif()
469         if(WITH_MOD_CLOTH_ELTOPO)
470                 target_link_libraries(${target} ${LAPACK_LIBRARIES})
471         endif()
472         if(WITH_LLVM)
473                 target_link_libraries(${target} ${LLVM_LIBRARY})
474         endif()
475         if(WIN32 AND NOT UNIX)
476                 target_link_libraries(${target} ${PTHREADS_LIBRARIES})
477         endif()
478         if(UNIX AND NOT APPLE)
479                 if(WITH_OPENMP_STATIC)
480                         target_link_libraries(${target} ${OpenMP_LIBRARIES})
481                 endif()
482         endif()
483
484         # We put CLEW and CUEW here because OPENSUBDIV_LIBRARIES dpeends on them..
485         if(WITH_CYCLES OR WITH_COMPOSITOR OR WITH_OPENSUBDIV)
486                 target_link_libraries(${target} "extern_clew")
487                 if(WITH_CUDA_DYNLOAD)
488                         target_link_libraries(${target} "extern_cuew")
489                 else()
490                         target_link_libraries(${target} ${CUDA_CUDA_LIBRARY})
491                 endif()
492         endif()
493
494         #system libraries with no dependencies such as platform link libs or opengl should go last
495         target_link_libraries(${target}
496                         ${BLENDER_GL_LIBRARIES})
497
498         target_link_libraries(${target} ${PLATFORM_LINKLIBS} ${CMAKE_DL_LIBS})
499 endfunction()
500
501
502 function(SETUP_BLENDER_SORTED_LIBS)
503
504         get_property(BLENDER_LINK_LIBS GLOBAL PROPERTY BLENDER_LINK_LIBS)
505
506         list(APPEND BLENDER_LINK_LIBS
507                 bf_windowmanager
508                 bf_render
509         )
510
511         if(WITH_MOD_FLUID)
512                 list(APPEND BLENDER_LINK_LIBS bf_intern_elbeem)
513         endif()
514
515         if(WITH_CYCLES)
516                 list(APPEND BLENDER_LINK_LIBS
517                         cycles_render
518                         cycles_bvh
519                         cycles_device
520                         cycles_kernel
521                         cycles_util
522                         cycles_subd)
523                 if(WITH_CYCLES_OSL)
524                         list(APPEND BLENDER_LINK_LIBS cycles_kernel_osl)
525                 endif()
526         endif()
527
528         # Sort libraries
529         set(BLENDER_SORTED_LIBS
530                 bf_windowmanager
531
532                 bf_editor_space_api
533                 bf_editor_space_action
534                 bf_editor_space_buttons
535                 bf_editor_space_console
536                 bf_editor_space_file
537                 bf_editor_space_graph
538                 bf_editor_space_image
539                 bf_editor_space_info
540                 bf_editor_space_logic
541                 bf_editor_space_nla
542                 bf_editor_space_node
543                 bf_editor_space_outliner
544                 bf_editor_space_script
545                 bf_editor_space_sequencer
546                 bf_editor_space_text
547                 bf_editor_space_time
548                 bf_editor_space_userpref
549                 bf_editor_space_view3d
550                 bf_editor_space_clip
551
552                 bf_editor_transform
553                 bf_editor_util
554                 bf_editor_uvedit
555                 bf_editor_curve
556                 bf_editor_gpencil
557                 bf_editor_interface
558                 bf_editor_mesh
559                 bf_editor_metaball
560                 bf_editor_object
561                 bf_editor_armature
562                 bf_editor_physics
563                 bf_editor_render
564                 bf_editor_screen
565                 bf_editor_sculpt_paint
566                 bf_editor_sound
567                 bf_editor_animation
568                 bf_editor_datafiles
569                 bf_editor_mask
570                 bf_editor_io
571
572                 bf_render
573                 bf_python
574                 bf_python_ext
575                 bf_python_mathutils
576                 bf_python_bmesh
577                 bf_freestyle
578                 bf_ikplugin
579                 bf_modifiers
580                 bf_bmesh
581                 bf_gpu
582                 bf_blenkernel
583                 bf_physics
584                 bf_nodes
585                 bf_rna
586                 bf_blenloader
587                 bf_imbuf
588                 bf_blenlib
589                 bf_depsgraph
590                 bf_intern_ghost
591                 bf_intern_string
592                 bf_avi
593                 bf_imbuf_cineon
594                 bf_imbuf_openexr
595                 bf_imbuf_openimageio
596                 bf_imbuf_dds
597                 bf_collada
598                 bf_intern_elbeem
599                 bf_intern_memutil
600                 bf_intern_guardedalloc
601                 bf_intern_ctr
602                 bf_intern_utfconv
603                 ge_blen_routines
604                 ge_converter
605                 ge_phys_dummy
606                 ge_phys_bullet
607                 bf_intern_smoke
608                 extern_lzma
609                 ge_logic_ketsji
610                 extern_recastnavigation
611                 ge_logic
612                 ge_rasterizer
613                 ge_oglrasterizer
614                 ge_logic_expressions
615                 ge_scenegraph
616                 ge_logic_network
617                 ge_logic_ngnetwork
618                 ge_logic_loopbacknetwork
619                 bf_intern_moto
620                 extern_openjpeg
621                 extern_redcode
622                 ge_videotex
623                 bf_dna
624                 bf_blenfont
625                 bf_blentranslation
626                 bf_intern_audaspace
627                 bf_intern_mikktspace
628                 bf_intern_dualcon
629                 bf_intern_cycles
630                 cycles_render
631                 cycles_bvh
632                 cycles_device
633                 cycles_kernel
634                 cycles_util
635                 cycles_subd
636                 bf_intern_opencolorio
637                 bf_intern_eigen
638                 extern_rangetree
639                 extern_wcwidth
640                 bf_intern_libmv
641                 extern_glog
642                 extern_gflags
643                 extern_sdlew
644
645                 bf_intern_glew_mx
646         )
647
648         if(WITH_COMPOSITOR)
649                 # added for opencl compositor
650                 list_insert_before(BLENDER_SORTED_LIBS "bf_blenkernel" "bf_compositor")
651                 list_insert_after(BLENDER_SORTED_LIBS "bf_compositor" "bf_intern_opencl")
652         endif()
653
654         if(WITH_LIBMV)
655                 list(APPEND BLENDER_SORTED_LIBS extern_ceres)
656         endif()
657
658         if(WITH_MOD_CLOTH_ELTOPO)
659                 list(APPEND BLENDER_SORTED_LIBS extern_eltopo)
660         endif()
661
662         if(NOT WITH_SYSTEM_LZO)
663                 list(APPEND BLENDER_SORTED_LIBS extern_minilzo)
664         endif()
665
666         if(NOT WITH_SYSTEM_GLEW)
667                 list(APPEND BLENDER_SORTED_LIBS ${BLENDER_GLEW_LIBRARIES})
668         endif()
669
670         if(WITH_BINRELOC)
671                 list(APPEND BLENDER_SORTED_LIBS extern_binreloc)
672         endif()
673
674         if(WITH_CXX_GUARDEDALLOC)
675                 list(APPEND BLENDER_SORTED_LIBS bf_intern_guardedalloc_cpp)
676         endif()
677
678         if(WITH_IK_SOLVER)
679                 list_insert_after(BLENDER_SORTED_LIBS "bf_intern_elbeem" "bf_intern_iksolver")
680         endif()
681
682         if(WITH_IK_ITASC)
683                 list(APPEND BLENDER_SORTED_LIBS bf_intern_itasc)
684         endif()
685
686         if(WITH_CODEC_QUICKTIME)
687                 list(APPEND BLENDER_SORTED_LIBS bf_quicktime)
688         endif()
689
690         if(WITH_INPUT_NDOF)
691                 list(APPEND BLENDER_SORTED_LIBS bf_intern_ghostndof3dconnexion)
692         endif()
693         
694         if(WITH_MOD_BOOLEAN)
695                 list(APPEND BLENDER_SORTED_LIBS extern_carve)
696         endif()
697
698         if(WITH_GHOST_XDND)
699                 list(APPEND BLENDER_SORTED_LIBS extern_xdnd)
700         endif()
701
702         if(WITH_CYCLES_OSL)
703                 list_insert_after(BLENDER_SORTED_LIBS "cycles_kernel" "cycles_kernel_osl")
704         endif()
705
706         if(WITH_INTERNATIONAL)
707                 list(APPEND BLENDER_SORTED_LIBS bf_intern_locale)
708         endif()
709
710         if(WITH_BULLET)
711                 list_insert_after(BLENDER_SORTED_LIBS "bf_blenkernel" "bf_intern_rigidbody")
712         endif()
713
714         if(WITH_BULLET AND NOT WITH_SYSTEM_BULLET)
715                 list_insert_after(BLENDER_SORTED_LIBS "ge_logic_ngnetwork" "extern_bullet")
716         endif()
717
718         if(WITH_OPENSUBDIV)
719                 list(APPEND BLENDER_SORTED_LIBS bf_intern_opensubdiv)
720         endif()
721
722         if(WITH_OPENVDB)
723                 list(APPEND BLENDER_SORTED_LIBS bf_intern_openvdb)
724         endif()
725
726         foreach(SORTLIB ${BLENDER_SORTED_LIBS})
727                 set(REMLIB ${SORTLIB})
728                 foreach(SEARCHLIB ${BLENDER_LINK_LIBS})
729                         if(${SEARCHLIB} STREQUAL ${SORTLIB})
730                                 set(REMLIB "")
731                         endif()
732                 endforeach()
733                 if(REMLIB)
734                         # message(STATUS "Removing library ${REMLIB} from blender linking because: not configured")
735                         list(APPEND REM_MSG ${REMLIB})
736                         list(REMOVE_ITEM BLENDER_SORTED_LIBS ${REMLIB})
737                 endif()
738         endforeach()
739         if(REM_MSG)
740                 list(SORT REM_MSG)
741                 message(STATUS "Blender Skipping: (${REM_MSG})")
742         endif()
743
744
745         set(BLENDER_SORTED_LIBS ${BLENDER_SORTED_LIBS} PARENT_SCOPE)
746
747         # for top-level tests
748         set_property(GLOBAL PROPERTY BLENDER_SORTED_LIBS_PROP ${BLENDER_SORTED_LIBS})
749 endfunction()
750
751 macro(TEST_SSE_SUPPORT
752         _sse_flags
753         _sse2_flags)
754
755         include(CheckCSourceRuns)
756
757         # message(STATUS "Detecting SSE support")
758         if(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
759                 set(${_sse_flags} "-msse")
760                 set(${_sse2_flags} "-msse2")
761         elseif(MSVC)
762                 # x86_64 has this auto enabled
763                 if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
764                         set(${_sse_flags} "")
765                         set(${_sse2_flags} "")
766                 else()
767                         set(${_sse_flags} "/arch:SSE")
768                         set(${_sse2_flags} "/arch:SSE2")
769                 endif()
770         elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
771                 set(${_sse_flags} "")  # icc defaults to -msse
772                 set(${_sse2_flags} "-msse2")
773         else()
774                 message(WARNING "SSE flags for this compiler: '${CMAKE_C_COMPILER_ID}' not known")
775                 set(${_sse_flags})
776                 set(${_sse2_flags})
777         endif()
778
779         set(CMAKE_REQUIRED_FLAGS "${${_sse_flags}} ${${_sse2_flags}}")
780
781         if(NOT DEFINED SUPPORT_SSE_BUILD)
782                 # result cached
783                 check_c_source_runs("
784                         #include <xmmintrin.h>
785                         int main(void) { __m128 v = _mm_setzero_ps(); return 0; }"
786                 SUPPORT_SSE_BUILD)
787
788                 if(SUPPORT_SSE_BUILD)
789                         message(STATUS "SSE Support: detected.")
790                 else()
791                         message(STATUS "SSE Support: missing.")
792                 endif()
793         endif()
794
795         if(NOT DEFINED SUPPORT_SSE2_BUILD)
796                 # result cached
797                 check_c_source_runs("
798                         #include <emmintrin.h>
799                         int main(void) { __m128d v = _mm_setzero_pd(); return 0; }"
800                 SUPPORT_SSE2_BUILD)
801
802                 if(SUPPORT_SSE2_BUILD)
803                         message(STATUS "SSE2 Support: detected.")
804                 else()
805                         message(STATUS "SSE2 Support: missing.")
806                 endif()
807         endif()
808
809         unset(CMAKE_REQUIRED_FLAGS)
810 endmacro()
811
812 # Only print message if running CMake first time
813 macro(message_first_run)
814         if(FIRST_RUN)
815                 message(${ARGV})
816         endif()
817 endmacro()
818
819 macro(TEST_UNORDERED_MAP_SUPPORT)
820         # - Detect unordered_map availability
821         # Test if a valid implementation of unordered_map exists
822         # and define the include path
823         # This module defines
824         #  HAVE_UNORDERED_MAP, whether unordered_map implementation was found
825         #
826         #  HAVE_STD_UNORDERED_MAP_HEADER, <unordered_map.h> was found
827         #  HAVE_UNORDERED_MAP_IN_STD_NAMESPACE, unordered_map is in namespace std
828         #  HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE, unordered_map is in namespace std::tr1
829         #
830         #  UNORDERED_MAP_INCLUDE_PREFIX, include path prefix for unordered_map, if found
831         #  UNORDERED_MAP_NAMESPACE, namespace for unordered_map, if found
832
833         include(CheckIncludeFileCXX)
834         CHECK_INCLUDE_FILE_CXX("unordered_map" HAVE_STD_UNORDERED_MAP_HEADER)
835         if(HAVE_STD_UNORDERED_MAP_HEADER)
836                 # Even so we've found unordered_map header file it doesn't
837                 # mean unordered_map and unordered_set will be declared in
838                 # std namespace.
839                 #
840                 # Namely, MSVC 2008 have unordered_map header which declares
841                 # unordered_map class in std::tr1 namespace. In order to support
842                 # this, we do extra check to see which exactly namespace is
843                 # to be used.
844
845                 include(CheckCXXSourceCompiles)
846                 CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
847                                           int main() {
848                                             std::unordered_map<int, int> map;
849                                             return 0;
850                                           }"
851                                           HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
852                 if(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
853                         message_first_run(STATUS "Found unordered_map/set in std namespace.")
854
855                         set(HAVE_UNORDERED_MAP "TRUE")
856                         set(UNORDERED_MAP_INCLUDE_PREFIX "")
857                         set(UNORDERED_MAP_NAMESPACE "std")
858                 else()
859                         CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
860                                                   int main() {
861                                                     std::tr1::unordered_map<int, int> map;
862                                                     return 0;
863                                                   }"
864                                                   HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
865                         if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
866                                 message_first_run(STATUS "Found unordered_map/set in std::tr1 namespace.")
867
868                                 set(HAVE_UNORDERED_MAP "TRUE")
869                                 set(UNORDERED_MAP_INCLUDE_PREFIX "")
870                                 set(UNORDERED_MAP_NAMESPACE "std::tr1")
871                         else()
872                                 message_first_run(STATUS "Found <unordered_map> but cannot find either std::unordered_map "
873                                                   "or std::tr1::unordered_map.")
874                         endif()
875                 endif()
876         else()
877                 CHECK_INCLUDE_FILE_CXX("tr1/unordered_map" HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
878                 if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
879                         message_first_run(STATUS "Found unordered_map/set in std::tr1 namespace.")
880
881                         set(HAVE_UNORDERED_MAP "TRUE")
882                         set(UNORDERED_MAP_INCLUDE_PREFIX "tr1")
883                         set(UNORDERED_MAP_NAMESPACE "std::tr1")
884                 else()
885                         message_first_run(STATUS "Unable to find <unordered_map> or <tr1/unordered_map>. ")
886                 endif()
887         endif()
888 endmacro()
889
890 macro(TEST_SHARED_PTR_SUPPORT)
891         # This check are coming from Ceres library.
892         #
893         # Find shared pointer header and namespace.
894         #
895         # This module defines the following variables:
896         #
897         # SHARED_PTR_FOUND: TRUE if shared_ptr found.
898         # SHARED_PTR_TR1_MEMORY_HEADER: True if <tr1/memory> header is to be used
899         # for the shared_ptr object, otherwise use <memory>.
900         # SHARED_PTR_TR1_NAMESPACE: TRUE if shared_ptr is defined in std::tr1 namespace,
901         # otherwise it's assumed to be defined in std namespace.
902
903         include(CheckIncludeFileCXX)
904         set(SHARED_PTR_FOUND FALSE)
905         CHECK_INCLUDE_FILE_CXX(memory HAVE_STD_MEMORY_HEADER)
906         if(HAVE_STD_MEMORY_HEADER)
907                 # Finding the memory header doesn't mean that shared_ptr is in std
908                 # namespace.
909                 #
910                 # In particular, MSVC 2008 has shared_ptr declared in std::tr1.  In
911                 # order to support this, we do an extra check to see which namespace
912                 # should be used.
913                 include(CheckCXXSourceCompiles)
914                 CHECK_CXX_SOURCE_COMPILES("#include <memory>
915                                            int main() {
916                                              std::shared_ptr<int> int_ptr;
917                                              return 0;
918                                            }"
919                                           HAVE_SHARED_PTR_IN_STD_NAMESPACE)
920
921                 if(HAVE_SHARED_PTR_IN_STD_NAMESPACE)
922                         message_first_run("-- Found shared_ptr in std namespace using <memory> header.")
923                         set(SHARED_PTR_FOUND TRUE)
924                 else()
925                         CHECK_CXX_SOURCE_COMPILES("#include <memory>
926                                                    int main() {
927                                                    std::tr1::shared_ptr<int> int_ptr;
928                                                    return 0;
929                                                    }"
930                                                   HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
931                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
932                                 message_first_run("-- Found shared_ptr in std::tr1 namespace using <memory> header.")
933                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
934                                 set(SHARED_PTR_FOUND TRUE)
935                         endif()
936                 endif()
937         endif()
938
939         if(NOT SHARED_PTR_FOUND)
940                 # Further, gcc defines shared_ptr in std::tr1 namespace and
941                 # <tr1/memory> is to be included for this. And what makes things
942                 # even more tricky is that gcc does have <memory> header, so
943                 # all the checks above wouldn't find shared_ptr.
944                 CHECK_INCLUDE_FILE_CXX("tr1/memory" HAVE_TR1_MEMORY_HEADER)
945                 if(HAVE_TR1_MEMORY_HEADER)
946                         CHECK_CXX_SOURCE_COMPILES("#include <tr1/memory>
947                                                    int main() {
948                                                    std::tr1::shared_ptr<int> int_ptr;
949                                                    return 0;
950                                                    }"
951                                                    HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
952                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
953                                 message_first_run("-- Found shared_ptr in std::tr1 namespace using <tr1/memory> header.")
954                                 set(SHARED_PTR_TR1_MEMORY_HEADER TRUE)
955                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
956                                 set(SHARED_PTR_FOUND TRUE)
957                         endif()
958                 endif()
959         endif()
960 endmacro()
961
962 # when we have warnings as errors applied globally this
963 # needs to be removed for some external libs which we dont maintain.
964
965 # utility macro
966 macro(remove_cc_flag
967         _flag)
968
969         foreach(flag ${ARGV})
970                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
971                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
972                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
973                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
974                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
975
976                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
977                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
978                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
979                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
980                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
981         endforeach()
982         unset(flag)
983
984 endmacro()
985
986 macro(add_cc_flag
987         flag)
988
989         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
990         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
991 endmacro()
992
993 macro(remove_strict_flags)
994
995         if(CMAKE_COMPILER_IS_GNUCC)
996                 remove_cc_flag(
997                         "-Wstrict-prototypes"
998                         "-Wmissing-prototypes"
999                         "-Wmissing-format-attribute"
1000                         "-Wunused-local-typedefs"
1001                         "-Wunused-macros"
1002                         "-Wunused-parameter"
1003                         "-Wwrite-strings"
1004                         "-Wredundant-decls"
1005                         "-Wundef"
1006                         "-Wshadow"
1007                         "-Wdouble-promotion"
1008                         "-Wold-style-definition"
1009                         "-Werror=[^ ]+"
1010                         "-Werror"
1011                 )
1012
1013                 # negate flags implied by '-Wall'
1014                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1015         endif()
1016
1017         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1018                 remove_cc_flag(
1019                         "-Wunused-parameter"
1020                         "-Wunused-variable"
1021                         "-Werror=[^ ]+"
1022                         "-Werror"
1023                 )
1024
1025                 # negate flags implied by '-Wall'
1026                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1027         endif()
1028
1029         if(MSVC)
1030                 # TODO
1031         endif()
1032
1033 endmacro()
1034
1035 macro(remove_extra_strict_flags)
1036         if(CMAKE_COMPILER_IS_GNUCC)
1037                 remove_cc_flag(
1038                         "-Wunused-parameter"
1039                 )
1040         endif()
1041
1042         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1043                 remove_cc_flag(
1044                         "-Wunused-parameter"
1045                 )
1046         endif()
1047
1048         if(MSVC)
1049                 # TODO
1050         endif()
1051 endmacro()
1052
1053 # note, we can only append flags on a single file so we need to negate the options.
1054 # at the moment we cant shut up ffmpeg deprecations, so use this, but will
1055 # probably add more removals here.
1056 macro(remove_strict_flags_file
1057         filenames)
1058
1059         foreach(_SOURCE ${ARGV})
1060
1061                 if(CMAKE_COMPILER_IS_GNUCC OR
1062                   (CMAKE_C_COMPILER_ID MATCHES "Clang"))
1063
1064                         set_source_files_properties(${_SOURCE}
1065                                 PROPERTIES
1066                                         COMPILE_FLAGS "${CC_REMOVE_STRICT_FLAGS}"
1067                         )
1068                 endif()
1069
1070                 if(MSVC)
1071                         # TODO
1072                 endif()
1073
1074         endforeach()
1075
1076         unset(_SOURCE)
1077
1078 endmacro()
1079
1080
1081 function(ADD_CHECK_C_COMPILER_FLAG
1082         _CFLAGS
1083         _CACHE_VAR
1084         _FLAG
1085         )
1086
1087         include(CheckCCompilerFlag)
1088
1089         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1090         if(${_CACHE_VAR})
1091                 # message(STATUS "Using CFLAG: ${_FLAG}")
1092                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}" PARENT_SCOPE)
1093         else()
1094                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
1095         endif()
1096 endfunction()
1097
1098 function(ADD_CHECK_CXX_COMPILER_FLAG
1099         _CXXFLAGS
1100         _CACHE_VAR
1101         _FLAG
1102         )
1103
1104         include(CheckCXXCompilerFlag)
1105
1106         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1107         if(${_CACHE_VAR})
1108                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
1109                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}" PARENT_SCOPE)
1110         else()
1111                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
1112         endif()
1113 endfunction()
1114
1115 function(get_blender_version)
1116         # extracts header vars and defines them in the parent scope:
1117         #
1118         # - BLENDER_VERSION (major.minor)
1119         # - BLENDER_VERSION_MAJOR
1120         # - BLENDER_VERSION_MINOR
1121         # - BLENDER_SUBVERSION (used for internal versioning mainly)
1122         # - BLENDER_VERSION_CHAR (a, b, c, ...or empty string)
1123         # - BLENDER_VERSION_CYCLE (alpha, beta, rc, release)
1124
1125         # So cmake depends on BKE_blender.h, beware of inf-loops!
1126         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h
1127                        ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
1128
1129         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
1130
1131         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
1132         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
1133         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
1134         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
1135
1136         if(NOT ${_out_version} MATCHES "[0-9]+")
1137                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
1138         endif()
1139
1140         if(NOT ${_out_subversion} MATCHES "[0-9]+")
1141                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
1142         endif()
1143
1144         # clumsy regex, only single char are ok but it could be unset
1145
1146         string(LENGTH "${_out_version_char}" _out_version_char_len)
1147         if(NOT _out_version_char_len EQUAL 1)
1148                 set(_out_version_char "")
1149         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
1150                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
1151         endif()
1152
1153         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
1154                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
1155         endif()
1156
1157         math(EXPR _out_version_major "${_out_version} / 100")
1158         math(EXPR _out_version_minor "${_out_version} % 100")
1159
1160         # for packaging, alpha to numbers
1161         string(COMPARE EQUAL "${_out_version_char}" "" _out_version_char_empty)
1162         if(${_out_version_char_empty})
1163                 set(_out_version_char_index "0")
1164         else()
1165                 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)
1166                 list(FIND _char_ls ${_out_version_char} _out_version_char_index)
1167                 math(EXPR _out_version_char_index "${_out_version_char_index} + 1")
1168         endif()
1169
1170         # output vars
1171         set(BLENDER_VERSION "${_out_version_major}.${_out_version_minor}" PARENT_SCOPE)
1172         set(BLENDER_VERSION_MAJOR "${_out_version_major}" PARENT_SCOPE)
1173         set(BLENDER_VERSION_MINOR "${_out_version_minor}" PARENT_SCOPE)
1174         set(BLENDER_SUBVERSION "${_out_subversion}" PARENT_SCOPE)
1175         set(BLENDER_VERSION_CHAR "${_out_version_char}" PARENT_SCOPE)
1176         set(BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index}" PARENT_SCOPE)
1177         set(BLENDER_VERSION_CYCLE "${_out_version_cycle}" PARENT_SCOPE)
1178
1179 endfunction()
1180
1181
1182 # hacks to override initial project settings
1183 # these macros must be called directly before/after project(Blender)
1184 macro(blender_project_hack_pre)
1185         # ----------------
1186         # MINGW HACK START
1187         # ignore system set flag, use our own
1188         # must be before project(...)
1189         # if the user wants to add their own its ok after first run.
1190         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
1191                 set(_reset_standard_libraries OFF)
1192         else()
1193                 set(_reset_standard_libraries ON)
1194         endif()
1195
1196         # ------------------
1197         # GCC -O3 HACK START
1198         # needed because O3 can cause problems but
1199         # allow the builder to set O3 manually after.
1200         if(DEFINED CMAKE_C_FLAGS_RELEASE)
1201                 set(_reset_standard_cflags_rel OFF)
1202         else()
1203                 set(_reset_standard_cflags_rel ON)
1204         endif()
1205         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
1206                 set(_reset_standard_cxxflags_rel OFF)
1207         else()
1208                 set(_reset_standard_cxxflags_rel ON)
1209         endif()
1210 endmacro()
1211
1212
1213 macro(blender_project_hack_post)
1214         # --------------
1215         # MINGW HACK END
1216         if(_reset_standard_libraries)
1217                 # Must come after projecINCt(...)
1218                 #
1219                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
1220                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
1221                 # with a meaningless stack trace. by overriding this flag we ensure we only
1222                 # have libs we define.
1223                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1224                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1225                 mark_as_advanced(
1226                         CMAKE_C_STANDARD_LIBRARIES
1227                         CMAKE_CXX_STANDARD_LIBRARIES
1228                 )
1229         endif()
1230         unset(_reset_standard_libraries)
1231
1232
1233         # ----------------
1234         # GCC -O3 HACK END
1235         if(_reset_standard_cflags_rel)
1236                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
1237                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1238                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
1239         endif()
1240
1241         if(_reset_standard_cxxflags_rel)
1242                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
1243                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1244                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
1245         endif()
1246
1247         unset(_reset_standard_cflags_rel)
1248         unset(_reset_standard_cxxflags_rel)
1249
1250         # ------------------------------------------------------------------
1251         # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
1252         if(CMAKE_COMPILER_IS_GNUCC)
1253                 if(NOT DARWIN)
1254                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
1255                 endif()
1256         endif()
1257
1258 endmacro()
1259
1260 # pair of macros to allow libraries to be specify files to install, but to
1261 # only install them at the end so the directories don't get cleared with
1262 # the files in them. used by cycles to install addon.
1263 function(delayed_install
1264         base
1265         files
1266         destination)
1267
1268         foreach(f ${files})
1269                 if(IS_ABSOLUTE ${f})
1270                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${f})
1271                 else()
1272                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${base}/${f})
1273                 endif()
1274                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_DESTINATIONS ${destination})
1275         endforeach()
1276 endfunction()
1277
1278 # note this is a function instead of a macro so that ${BUILD_TYPE} in targetdir
1279 # does not get expanded in calling but is preserved
1280 function(delayed_do_install
1281         targetdir)
1282
1283         get_property(files GLOBAL PROPERTY DELAYED_INSTALL_FILES)
1284         get_property(destinations GLOBAL PROPERTY DELAYED_INSTALL_DESTINATIONS)
1285
1286         if(files)
1287                 list(LENGTH files n)
1288                 math(EXPR n "${n}-1")
1289
1290                 foreach(i RANGE ${n})
1291                         list(GET files ${i} f)
1292                         list(GET destinations ${i} d)
1293                         install(FILES ${f} DESTINATION ${targetdir}/${d})
1294                 endforeach()
1295         endif()
1296 endfunction()
1297
1298
1299 function(data_to_c
1300         file_from file_to
1301         list_to_add
1302         )
1303
1304         list(APPEND ${list_to_add} ${file_to})
1305         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1306
1307         get_filename_component(_file_to_path ${file_to} PATH)
1308
1309         add_custom_command(
1310                 OUTPUT ${file_to}
1311                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1312                 COMMAND "$<TARGET_FILE:datatoc>" ${file_from} ${file_to}
1313                 DEPENDS ${file_from} datatoc)
1314
1315         set_source_files_properties(${file_to} PROPERTIES GENERATED TRUE)
1316 endfunction()
1317
1318
1319 # same as above but generates the var name and output automatic.
1320 function(data_to_c_simple
1321         file_from
1322         list_to_add
1323         )
1324
1325         # remove ../'s
1326         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from}   REALPATH)
1327         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${file_from}.c REALPATH)
1328
1329         list(APPEND ${list_to_add} ${_file_to})
1330         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1331
1332         get_filename_component(_file_to_path ${_file_to} PATH)
1333
1334         add_custom_command(
1335                 OUTPUT  ${_file_to}
1336                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1337                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1338                 DEPENDS ${_file_from} datatoc)
1339
1340         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1341 endfunction()
1342
1343 # macro for converting pixmap directory to a png and then a c file
1344 function(data_to_c_simple_icons
1345         path_from
1346         list_to_add
1347         )
1348
1349         # Conversion steps
1350         #  path_from  ->  _file_from  ->  _file_to
1351         #  foo/*.dat  ->  foo.png     ->  foo.png.c
1352
1353         get_filename_component(_path_from_abs ${path_from} ABSOLUTE)
1354         # remove ../'s
1355         get_filename_component(_file_from ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png   REALPATH)
1356         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png.c REALPATH)
1357
1358         list(APPEND ${list_to_add} ${_file_to})
1359         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1360
1361         get_filename_component(_file_to_path ${_file_to} PATH)
1362
1363         # ideally we wouldn't glob, but storing all names for all pixmaps is a bit heavy
1364         file(GLOB _icon_files "${path_from}/*.dat")
1365
1366         add_custom_command(
1367                 OUTPUT  ${_file_from} ${_file_to}
1368                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1369                 #COMMAND python3 ${CMAKE_SOURCE_DIR}/source/blender/datatoc/datatoc_icon.py ${_path_from_abs} ${_file_from}
1370                 COMMAND "$<TARGET_FILE:datatoc_icon>" ${_path_from_abs} ${_file_from}
1371                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1372                 DEPENDS
1373                         ${_icon_files}
1374                         datatoc_icon
1375                         datatoc
1376                         # could be an arg but for now we only create icons depending on UI_icons.h
1377                         ${CMAKE_SOURCE_DIR}/source/blender/editors/include/UI_icons.h
1378                 )
1379
1380         set_source_files_properties(${_file_from} ${_file_to} PROPERTIES GENERATED TRUE)
1381 endfunction()
1382
1383 # XXX Not used for now...
1384 function(svg_to_png
1385         file_from
1386         file_to
1387         dpi
1388         list_to_add
1389         )
1390
1391         # remove ../'s
1392         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from} REALPATH)
1393         get_filename_component(_file_to   ${CMAKE_CURRENT_SOURCE_DIR}/${file_to}   REALPATH)
1394
1395         list(APPEND ${list_to_add} ${_file_to})
1396         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1397
1398         find_program(INKSCAPE_EXE inkscape)
1399         mark_as_advanced(INKSCAPE_EXE)
1400
1401         if(INKSCAPE_EXE)
1402                 if(APPLE)
1403                         # in OS X app bundle, the binary is a shim that doesn't take any
1404                         # command line arguments, replace it with the actual binary
1405                         string(REPLACE "MacOS/Inkscape" "Resources/bin/inkscape" INKSCAPE_REAL_EXE ${INKSCAPE_EXE})
1406                         if(EXISTS "${INKSCAPE_REAL_EXE}")
1407                                 set(INKSCAPE_EXE ${INKSCAPE_REAL_EXE})
1408                         endif()
1409                 endif()
1410
1411                 add_custom_command(
1412                         OUTPUT  ${_file_to}
1413                         COMMAND ${INKSCAPE_EXE} ${_file_from} --export-dpi=${dpi}  --without-gui --export-png=${_file_to}
1414                         DEPENDS ${_file_from} ${INKSCAPE_EXE}
1415                 )
1416         else()
1417                 message(WARNING "Inkscape not found, could not re-generate ${_file_to} from ${_file_from}!")
1418         endif()
1419 endfunction()
1420
1421 function(msgfmt_simple
1422         file_from
1423         list_to_add
1424         )
1425
1426         # remove ../'s
1427         get_filename_component(_file_from_we ${file_from} NAME_WE)
1428
1429         get_filename_component(_file_from ${file_from} REALPATH)
1430         get_filename_component(_file_to ${CMAKE_CURRENT_BINARY_DIR}/${_file_from_we}.mo REALPATH)
1431
1432         list(APPEND ${list_to_add} ${_file_to})
1433         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1434
1435         get_filename_component(_file_to_path ${_file_to} PATH)
1436
1437         add_custom_command(
1438                 OUTPUT  ${_file_to}
1439                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1440                 COMMAND "$<TARGET_FILE:msgfmt>" ${_file_from} ${_file_to}
1441                 DEPENDS msgfmt ${_file_from})
1442
1443         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1444 endfunction()
1445
1446 function(find_python_package
1447         package
1448         )
1449
1450         string(TOUPPER ${package} _upper_package)
1451
1452         # set but invalid
1453         if((NOT ${PYTHON_${_upper_package}_PATH} STREQUAL "") AND
1454            (NOT ${PYTHON_${_upper_package}_PATH} MATCHES NOTFOUND))
1455 #               if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}/${package}")
1456 #                       message(WARNING "PYTHON_${_upper_package}_PATH is invalid, ${package} not found in '${PYTHON_${_upper_package}_PATH}' "
1457 #                                       "WITH_PYTHON_INSTALL_${_upper_package} option will be ignored when installing python")
1458 #                       set(WITH_PYTHON_INSTALL${_upper_package} OFF)
1459 #               endif()
1460         # not set, so initialize
1461         else()
1462                 string(REPLACE "." ";" _PY_VER_SPLIT "${PYTHON_VERSION}")
1463                 list(GET _PY_VER_SPLIT 0 _PY_VER_MAJOR)
1464
1465                 # re-cache
1466                 unset(PYTHON_${_upper_package}_PATH CACHE)
1467                 find_path(PYTHON_${_upper_package}_PATH
1468                   NAMES
1469                     ${package}
1470                   HINTS
1471                     "${PYTHON_LIBPATH}/python${PYTHON_VERSION}/"
1472                     "${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/"
1473                   PATH_SUFFIXES
1474                     site-packages
1475                     dist-packages
1476                    NO_DEFAULT_PATH
1477                 )
1478
1479                  if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}")
1480                         message(WARNING
1481                                 "Python package '${package}' path could not be found in:\n"
1482                                 "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/site-packages/${package}', "
1483                                 "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/site-packages/${package}', "
1484                                 "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/dist-packages/${package}', "
1485                                 "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/dist-packages/${package}', "
1486                                 "\n"
1487                                 "The 'WITH_PYTHON_INSTALL_${_upper_package}' option will be ignored when installing Python.\n"
1488                                 "The build will be usable, only add-ons that depend on this package won't be functional."
1489                         )
1490                         set(WITH_PYTHON_INSTALL_${_upper_package} OFF PARENT_SCOPE)
1491                 else()
1492                         message(STATUS "${package} found at '${PYTHON_${_upper_package}_PATH}'")
1493                 endif()
1494         endif()
1495 endfunction()
1496
1497 # like Python's 'print(dir())'
1498 function(print_all_vars)
1499         get_cmake_property(_vars VARIABLES)
1500         foreach(_var ${_vars})
1501                 message("${_var}=${${_var}}")
1502         endforeach()
1503 endfunction()