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