CMake: Remove mention of extern_redcode, it was removed
[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                 ge_videotex
622                 bf_dna
623                 bf_blenfont
624                 bf_blentranslation
625                 bf_intern_audaspace
626                 bf_intern_mikktspace
627                 bf_intern_dualcon
628                 bf_intern_cycles
629                 cycles_render
630                 cycles_bvh
631                 cycles_device
632                 cycles_kernel
633                 cycles_util
634                 cycles_subd
635                 bf_intern_opencolorio
636                 bf_intern_eigen
637                 extern_rangetree
638                 extern_wcwidth
639                 bf_intern_libmv
640                 extern_glog
641                 extern_gflags
642                 extern_sdlew
643
644                 bf_intern_glew_mx
645         )
646
647         if(WITH_COMPOSITOR)
648                 # added for opencl compositor
649                 list_insert_before(BLENDER_SORTED_LIBS "bf_blenkernel" "bf_compositor")
650                 list_insert_after(BLENDER_SORTED_LIBS "bf_compositor" "bf_intern_opencl")
651         endif()
652
653         if(WITH_LIBMV)
654                 list(APPEND BLENDER_SORTED_LIBS extern_ceres)
655         endif()
656
657         if(WITH_MOD_CLOTH_ELTOPO)
658                 list(APPEND BLENDER_SORTED_LIBS extern_eltopo)
659         endif()
660
661         if(NOT WITH_SYSTEM_LZO)
662                 list(APPEND BLENDER_SORTED_LIBS extern_minilzo)
663         endif()
664
665         if(NOT WITH_SYSTEM_GLEW)
666                 list(APPEND BLENDER_SORTED_LIBS ${BLENDER_GLEW_LIBRARIES})
667         endif()
668
669         if(WITH_BINRELOC)
670                 list(APPEND BLENDER_SORTED_LIBS extern_binreloc)
671         endif()
672
673         if(WITH_CXX_GUARDEDALLOC)
674                 list(APPEND BLENDER_SORTED_LIBS bf_intern_guardedalloc_cpp)
675         endif()
676
677         if(WITH_IK_SOLVER)
678                 list_insert_after(BLENDER_SORTED_LIBS "bf_intern_elbeem" "bf_intern_iksolver")
679         endif()
680
681         if(WITH_IK_ITASC)
682                 list(APPEND BLENDER_SORTED_LIBS bf_intern_itasc)
683         endif()
684
685         if(WITH_CODEC_QUICKTIME)
686                 list(APPEND BLENDER_SORTED_LIBS bf_quicktime)
687         endif()
688
689         if(WITH_INPUT_NDOF)
690                 list(APPEND BLENDER_SORTED_LIBS bf_intern_ghostndof3dconnexion)
691         endif()
692         
693         if(WITH_MOD_BOOLEAN)
694                 list(APPEND BLENDER_SORTED_LIBS extern_carve)
695         endif()
696
697         if(WITH_GHOST_XDND)
698                 list(APPEND BLENDER_SORTED_LIBS extern_xdnd)
699         endif()
700
701         if(WITH_CYCLES_OSL)
702                 list_insert_after(BLENDER_SORTED_LIBS "cycles_kernel" "cycles_kernel_osl")
703         endif()
704
705         if(WITH_INTERNATIONAL)
706                 list(APPEND BLENDER_SORTED_LIBS bf_intern_locale)
707         endif()
708
709         if(WITH_BULLET)
710                 list_insert_after(BLENDER_SORTED_LIBS "bf_blenkernel" "bf_intern_rigidbody")
711         endif()
712
713         if(WITH_BULLET AND NOT WITH_SYSTEM_BULLET)
714                 list_insert_after(BLENDER_SORTED_LIBS "ge_logic_ngnetwork" "extern_bullet")
715         endif()
716
717         if(WITH_OPENSUBDIV)
718                 list(APPEND BLENDER_SORTED_LIBS bf_intern_opensubdiv)
719         endif()
720
721         if(WITH_OPENVDB)
722                 list(APPEND BLENDER_SORTED_LIBS bf_intern_openvdb)
723         endif()
724
725         foreach(SORTLIB ${BLENDER_SORTED_LIBS})
726                 set(REMLIB ${SORTLIB})
727                 foreach(SEARCHLIB ${BLENDER_LINK_LIBS})
728                         if(${SEARCHLIB} STREQUAL ${SORTLIB})
729                                 set(REMLIB "")
730                         endif()
731                 endforeach()
732                 if(REMLIB)
733                         # message(STATUS "Removing library ${REMLIB} from blender linking because: not configured")
734                         list(APPEND REM_MSG ${REMLIB})
735                         list(REMOVE_ITEM BLENDER_SORTED_LIBS ${REMLIB})
736                 endif()
737         endforeach()
738         if(REM_MSG)
739                 list(SORT REM_MSG)
740                 message(STATUS "Blender Skipping: (${REM_MSG})")
741         endif()
742
743
744         set(BLENDER_SORTED_LIBS ${BLENDER_SORTED_LIBS} PARENT_SCOPE)
745
746         # for top-level tests
747         set_property(GLOBAL PROPERTY BLENDER_SORTED_LIBS_PROP ${BLENDER_SORTED_LIBS})
748 endfunction()
749
750 macro(TEST_SSE_SUPPORT
751         _sse_flags
752         _sse2_flags)
753
754         include(CheckCSourceRuns)
755
756         # message(STATUS "Detecting SSE support")
757         if(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
758                 set(${_sse_flags} "-msse")
759                 set(${_sse2_flags} "-msse2")
760         elseif(MSVC)
761                 # x86_64 has this auto enabled
762                 if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
763                         set(${_sse_flags} "")
764                         set(${_sse2_flags} "")
765                 else()
766                         set(${_sse_flags} "/arch:SSE")
767                         set(${_sse2_flags} "/arch:SSE2")
768                 endif()
769         elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
770                 set(${_sse_flags} "")  # icc defaults to -msse
771                 set(${_sse2_flags} "-msse2")
772         else()
773                 message(WARNING "SSE flags for this compiler: '${CMAKE_C_COMPILER_ID}' not known")
774                 set(${_sse_flags})
775                 set(${_sse2_flags})
776         endif()
777
778         set(CMAKE_REQUIRED_FLAGS "${${_sse_flags}} ${${_sse2_flags}}")
779
780         if(NOT DEFINED SUPPORT_SSE_BUILD)
781                 # result cached
782                 check_c_source_runs("
783                         #include <xmmintrin.h>
784                         int main(void) { __m128 v = _mm_setzero_ps(); return 0; }"
785                 SUPPORT_SSE_BUILD)
786
787                 if(SUPPORT_SSE_BUILD)
788                         message(STATUS "SSE Support: detected.")
789                 else()
790                         message(STATUS "SSE Support: missing.")
791                 endif()
792         endif()
793
794         if(NOT DEFINED SUPPORT_SSE2_BUILD)
795                 # result cached
796                 check_c_source_runs("
797                         #include <emmintrin.h>
798                         int main(void) { __m128d v = _mm_setzero_pd(); return 0; }"
799                 SUPPORT_SSE2_BUILD)
800
801                 if(SUPPORT_SSE2_BUILD)
802                         message(STATUS "SSE2 Support: detected.")
803                 else()
804                         message(STATUS "SSE2 Support: missing.")
805                 endif()
806         endif()
807
808         unset(CMAKE_REQUIRED_FLAGS)
809 endmacro()
810
811 # Only print message if running CMake first time
812 macro(message_first_run)
813         if(FIRST_RUN)
814                 message(${ARGV})
815         endif()
816 endmacro()
817
818 macro(TEST_UNORDERED_MAP_SUPPORT)
819         # - Detect unordered_map availability
820         # Test if a valid implementation of unordered_map exists
821         # and define the include path
822         # This module defines
823         #  HAVE_UNORDERED_MAP, whether unordered_map implementation was found
824         #
825         #  HAVE_STD_UNORDERED_MAP_HEADER, <unordered_map.h> was found
826         #  HAVE_UNORDERED_MAP_IN_STD_NAMESPACE, unordered_map is in namespace std
827         #  HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE, unordered_map is in namespace std::tr1
828         #
829         #  UNORDERED_MAP_INCLUDE_PREFIX, include path prefix for unordered_map, if found
830         #  UNORDERED_MAP_NAMESPACE, namespace for unordered_map, if found
831
832         include(CheckIncludeFileCXX)
833         CHECK_INCLUDE_FILE_CXX("unordered_map" HAVE_STD_UNORDERED_MAP_HEADER)
834         if(HAVE_STD_UNORDERED_MAP_HEADER)
835                 # Even so we've found unordered_map header file it doesn't
836                 # mean unordered_map and unordered_set will be declared in
837                 # std namespace.
838                 #
839                 # Namely, MSVC 2008 have unordered_map header which declares
840                 # unordered_map class in std::tr1 namespace. In order to support
841                 # this, we do extra check to see which exactly namespace is
842                 # to be used.
843
844                 include(CheckCXXSourceCompiles)
845                 CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
846                                           int main() {
847                                             std::unordered_map<int, int> map;
848                                             return 0;
849                                           }"
850                                           HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
851                 if(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
852                         message_first_run(STATUS "Found unordered_map/set in std namespace.")
853
854                         set(HAVE_UNORDERED_MAP "TRUE")
855                         set(UNORDERED_MAP_INCLUDE_PREFIX "")
856                         set(UNORDERED_MAP_NAMESPACE "std")
857                 else()
858                         CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
859                                                   int main() {
860                                                     std::tr1::unordered_map<int, int> map;
861                                                     return 0;
862                                                   }"
863                                                   HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
864                         if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
865                                 message_first_run(STATUS "Found unordered_map/set in std::tr1 namespace.")
866
867                                 set(HAVE_UNORDERED_MAP "TRUE")
868                                 set(UNORDERED_MAP_INCLUDE_PREFIX "")
869                                 set(UNORDERED_MAP_NAMESPACE "std::tr1")
870                         else()
871                                 message_first_run(STATUS "Found <unordered_map> but cannot find either std::unordered_map "
872                                                   "or std::tr1::unordered_map.")
873                         endif()
874                 endif()
875         else()
876                 CHECK_INCLUDE_FILE_CXX("tr1/unordered_map" HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
877                 if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
878                         message_first_run(STATUS "Found unordered_map/set in std::tr1 namespace.")
879
880                         set(HAVE_UNORDERED_MAP "TRUE")
881                         set(UNORDERED_MAP_INCLUDE_PREFIX "tr1")
882                         set(UNORDERED_MAP_NAMESPACE "std::tr1")
883                 else()
884                         message_first_run(STATUS "Unable to find <unordered_map> or <tr1/unordered_map>. ")
885                 endif()
886         endif()
887 endmacro()
888
889 macro(TEST_SHARED_PTR_SUPPORT)
890         # This check are coming from Ceres library.
891         #
892         # Find shared pointer header and namespace.
893         #
894         # This module defines the following variables:
895         #
896         # SHARED_PTR_FOUND: TRUE if shared_ptr found.
897         # SHARED_PTR_TR1_MEMORY_HEADER: True if <tr1/memory> header is to be used
898         # for the shared_ptr object, otherwise use <memory>.
899         # SHARED_PTR_TR1_NAMESPACE: TRUE if shared_ptr is defined in std::tr1 namespace,
900         # otherwise it's assumed to be defined in std namespace.
901
902         include(CheckIncludeFileCXX)
903         set(SHARED_PTR_FOUND FALSE)
904         CHECK_INCLUDE_FILE_CXX(memory HAVE_STD_MEMORY_HEADER)
905         if(HAVE_STD_MEMORY_HEADER)
906                 # Finding the memory header doesn't mean that shared_ptr is in std
907                 # namespace.
908                 #
909                 # In particular, MSVC 2008 has shared_ptr declared in std::tr1.  In
910                 # order to support this, we do an extra check to see which namespace
911                 # should be used.
912                 include(CheckCXXSourceCompiles)
913                 CHECK_CXX_SOURCE_COMPILES("#include <memory>
914                                            int main() {
915                                              std::shared_ptr<int> int_ptr;
916                                              return 0;
917                                            }"
918                                           HAVE_SHARED_PTR_IN_STD_NAMESPACE)
919
920                 if(HAVE_SHARED_PTR_IN_STD_NAMESPACE)
921                         message_first_run("-- Found shared_ptr in std namespace using <memory> header.")
922                         set(SHARED_PTR_FOUND TRUE)
923                 else()
924                         CHECK_CXX_SOURCE_COMPILES("#include <memory>
925                                                    int main() {
926                                                    std::tr1::shared_ptr<int> int_ptr;
927                                                    return 0;
928                                                    }"
929                                                   HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
930                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE)
931                                 message_first_run("-- Found shared_ptr in std::tr1 namespace using <memory> header.")
932                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
933                                 set(SHARED_PTR_FOUND TRUE)
934                         endif()
935                 endif()
936         endif()
937
938         if(NOT SHARED_PTR_FOUND)
939                 # Further, gcc defines shared_ptr in std::tr1 namespace and
940                 # <tr1/memory> is to be included for this. And what makes things
941                 # even more tricky is that gcc does have <memory> header, so
942                 # all the checks above wouldn't find shared_ptr.
943                 CHECK_INCLUDE_FILE_CXX("tr1/memory" HAVE_TR1_MEMORY_HEADER)
944                 if(HAVE_TR1_MEMORY_HEADER)
945                         CHECK_CXX_SOURCE_COMPILES("#include <tr1/memory>
946                                                    int main() {
947                                                    std::tr1::shared_ptr<int> int_ptr;
948                                                    return 0;
949                                                    }"
950                                                    HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
951                         if(HAVE_SHARED_PTR_IN_TR1_NAMESPACE_FROM_TR1_MEMORY_HEADER)
952                                 message_first_run("-- Found shared_ptr in std::tr1 namespace using <tr1/memory> header.")
953                                 set(SHARED_PTR_TR1_MEMORY_HEADER TRUE)
954                                 set(SHARED_PTR_TR1_NAMESPACE TRUE)
955                                 set(SHARED_PTR_FOUND TRUE)
956                         endif()
957                 endif()
958         endif()
959 endmacro()
960
961 # when we have warnings as errors applied globally this
962 # needs to be removed for some external libs which we dont maintain.
963
964 # utility macro
965 macro(remove_cc_flag
966         _flag)
967
968         foreach(flag ${ARGV})
969                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
970                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
971                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
972                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
973                 string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
974
975                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
976                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
977                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
978                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
979                 string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
980         endforeach()
981         unset(flag)
982
983 endmacro()
984
985 macro(add_cc_flag
986         flag)
987
988         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
989         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
990 endmacro()
991
992 macro(remove_strict_flags)
993
994         if(CMAKE_COMPILER_IS_GNUCC)
995                 remove_cc_flag(
996                         "-Wstrict-prototypes"
997                         "-Wmissing-prototypes"
998                         "-Wmissing-format-attribute"
999                         "-Wunused-local-typedefs"
1000                         "-Wunused-macros"
1001                         "-Wunused-parameter"
1002                         "-Wwrite-strings"
1003                         "-Wredundant-decls"
1004                         "-Wundef"
1005                         "-Wshadow"
1006                         "-Wdouble-promotion"
1007                         "-Wold-style-definition"
1008                         "-Werror=[^ ]+"
1009                         "-Werror"
1010                 )
1011
1012                 # negate flags implied by '-Wall'
1013                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1014         endif()
1015
1016         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1017                 remove_cc_flag(
1018                         "-Wunused-parameter"
1019                         "-Wunused-variable"
1020                         "-Werror=[^ ]+"
1021                         "-Werror"
1022                 )
1023
1024                 # negate flags implied by '-Wall'
1025                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
1026         endif()
1027
1028         if(MSVC)
1029                 # TODO
1030         endif()
1031
1032 endmacro()
1033
1034 macro(remove_extra_strict_flags)
1035         if(CMAKE_COMPILER_IS_GNUCC)
1036                 remove_cc_flag(
1037                         "-Wunused-parameter"
1038                 )
1039         endif()
1040
1041         if(CMAKE_C_COMPILER_ID MATCHES "Clang")
1042                 remove_cc_flag(
1043                         "-Wunused-parameter"
1044                 )
1045         endif()
1046
1047         if(MSVC)
1048                 # TODO
1049         endif()
1050 endmacro()
1051
1052 # note, we can only append flags on a single file so we need to negate the options.
1053 # at the moment we cant shut up ffmpeg deprecations, so use this, but will
1054 # probably add more removals here.
1055 macro(remove_strict_flags_file
1056         filenames)
1057
1058         foreach(_SOURCE ${ARGV})
1059
1060                 if(CMAKE_COMPILER_IS_GNUCC OR
1061                   (CMAKE_C_COMPILER_ID MATCHES "Clang"))
1062
1063                         set_source_files_properties(${_SOURCE}
1064                                 PROPERTIES
1065                                         COMPILE_FLAGS "${CC_REMOVE_STRICT_FLAGS}"
1066                         )
1067                 endif()
1068
1069                 if(MSVC)
1070                         # TODO
1071                 endif()
1072
1073         endforeach()
1074
1075         unset(_SOURCE)
1076
1077 endmacro()
1078
1079
1080 function(ADD_CHECK_C_COMPILER_FLAG
1081         _CFLAGS
1082         _CACHE_VAR
1083         _FLAG
1084         )
1085
1086         include(CheckCCompilerFlag)
1087
1088         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1089         if(${_CACHE_VAR})
1090                 # message(STATUS "Using CFLAG: ${_FLAG}")
1091                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}" PARENT_SCOPE)
1092         else()
1093                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
1094         endif()
1095 endfunction()
1096
1097 function(ADD_CHECK_CXX_COMPILER_FLAG
1098         _CXXFLAGS
1099         _CACHE_VAR
1100         _FLAG
1101         )
1102
1103         include(CheckCXXCompilerFlag)
1104
1105         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
1106         if(${_CACHE_VAR})
1107                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
1108                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}" PARENT_SCOPE)
1109         else()
1110                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
1111         endif()
1112 endfunction()
1113
1114 function(get_blender_version)
1115         # extracts header vars and defines them in the parent scope:
1116         #
1117         # - BLENDER_VERSION (major.minor)
1118         # - BLENDER_VERSION_MAJOR
1119         # - BLENDER_VERSION_MINOR
1120         # - BLENDER_SUBVERSION (used for internal versioning mainly)
1121         # - BLENDER_VERSION_CHAR (a, b, c, ...or empty string)
1122         # - BLENDER_VERSION_CYCLE (alpha, beta, rc, release)
1123
1124         # So cmake depends on BKE_blender.h, beware of inf-loops!
1125         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h
1126                        ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
1127
1128         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
1129
1130         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
1131         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
1132         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
1133         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
1134
1135         if(NOT ${_out_version} MATCHES "[0-9]+")
1136                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
1137         endif()
1138
1139         if(NOT ${_out_subversion} MATCHES "[0-9]+")
1140                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
1141         endif()
1142
1143         # clumsy regex, only single char are ok but it could be unset
1144
1145         string(LENGTH "${_out_version_char}" _out_version_char_len)
1146         if(NOT _out_version_char_len EQUAL 1)
1147                 set(_out_version_char "")
1148         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
1149                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
1150         endif()
1151
1152         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
1153                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
1154         endif()
1155
1156         math(EXPR _out_version_major "${_out_version} / 100")
1157         math(EXPR _out_version_minor "${_out_version} % 100")
1158
1159         # for packaging, alpha to numbers
1160         string(COMPARE EQUAL "${_out_version_char}" "" _out_version_char_empty)
1161         if(${_out_version_char_empty})
1162                 set(_out_version_char_index "0")
1163         else()
1164                 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)
1165                 list(FIND _char_ls ${_out_version_char} _out_version_char_index)
1166                 math(EXPR _out_version_char_index "${_out_version_char_index} + 1")
1167         endif()
1168
1169         # output vars
1170         set(BLENDER_VERSION "${_out_version_major}.${_out_version_minor}" PARENT_SCOPE)
1171         set(BLENDER_VERSION_MAJOR "${_out_version_major}" PARENT_SCOPE)
1172         set(BLENDER_VERSION_MINOR "${_out_version_minor}" PARENT_SCOPE)
1173         set(BLENDER_SUBVERSION "${_out_subversion}" PARENT_SCOPE)
1174         set(BLENDER_VERSION_CHAR "${_out_version_char}" PARENT_SCOPE)
1175         set(BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index}" PARENT_SCOPE)
1176         set(BLENDER_VERSION_CYCLE "${_out_version_cycle}" PARENT_SCOPE)
1177
1178 endfunction()
1179
1180
1181 # hacks to override initial project settings
1182 # these macros must be called directly before/after project(Blender)
1183 macro(blender_project_hack_pre)
1184         # ----------------
1185         # MINGW HACK START
1186         # ignore system set flag, use our own
1187         # must be before project(...)
1188         # if the user wants to add their own its ok after first run.
1189         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
1190                 set(_reset_standard_libraries OFF)
1191         else()
1192                 set(_reset_standard_libraries ON)
1193         endif()
1194
1195         # ------------------
1196         # GCC -O3 HACK START
1197         # needed because O3 can cause problems but
1198         # allow the builder to set O3 manually after.
1199         if(DEFINED CMAKE_C_FLAGS_RELEASE)
1200                 set(_reset_standard_cflags_rel OFF)
1201         else()
1202                 set(_reset_standard_cflags_rel ON)
1203         endif()
1204         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
1205                 set(_reset_standard_cxxflags_rel OFF)
1206         else()
1207                 set(_reset_standard_cxxflags_rel ON)
1208         endif()
1209 endmacro()
1210
1211
1212 macro(blender_project_hack_post)
1213         # --------------
1214         # MINGW HACK END
1215         if(_reset_standard_libraries)
1216                 # Must come after projecINCt(...)
1217                 #
1218                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
1219                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
1220                 # with a meaningless stack trace. by overriding this flag we ensure we only
1221                 # have libs we define.
1222                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1223                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
1224                 mark_as_advanced(
1225                         CMAKE_C_STANDARD_LIBRARIES
1226                         CMAKE_CXX_STANDARD_LIBRARIES
1227                 )
1228         endif()
1229         unset(_reset_standard_libraries)
1230
1231
1232         # ----------------
1233         # GCC -O3 HACK END
1234         if(_reset_standard_cflags_rel)
1235                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
1236                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1237                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
1238         endif()
1239
1240         if(_reset_standard_cxxflags_rel)
1241                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
1242                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
1243                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
1244         endif()
1245
1246         unset(_reset_standard_cflags_rel)
1247         unset(_reset_standard_cxxflags_rel)
1248
1249         # ------------------------------------------------------------------
1250         # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
1251         if(CMAKE_COMPILER_IS_GNUCC)
1252                 if(NOT DARWIN)
1253                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
1254                 endif()
1255         endif()
1256
1257 endmacro()
1258
1259 # pair of macros to allow libraries to be specify files to install, but to
1260 # only install them at the end so the directories don't get cleared with
1261 # the files in them. used by cycles to install addon.
1262 function(delayed_install
1263         base
1264         files
1265         destination)
1266
1267         foreach(f ${files})
1268                 if(IS_ABSOLUTE ${f})
1269                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${f})
1270                 else()
1271                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${base}/${f})
1272                 endif()
1273                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_DESTINATIONS ${destination})
1274         endforeach()
1275 endfunction()
1276
1277 # note this is a function instead of a macro so that ${BUILD_TYPE} in targetdir
1278 # does not get expanded in calling but is preserved
1279 function(delayed_do_install
1280         targetdir)
1281
1282         get_property(files GLOBAL PROPERTY DELAYED_INSTALL_FILES)
1283         get_property(destinations GLOBAL PROPERTY DELAYED_INSTALL_DESTINATIONS)
1284
1285         if(files)
1286                 list(LENGTH files n)
1287                 math(EXPR n "${n}-1")
1288
1289                 foreach(i RANGE ${n})
1290                         list(GET files ${i} f)
1291                         list(GET destinations ${i} d)
1292                         install(FILES ${f} DESTINATION ${targetdir}/${d})
1293                 endforeach()
1294         endif()
1295 endfunction()
1296
1297
1298 function(data_to_c
1299         file_from file_to
1300         list_to_add
1301         )
1302
1303         list(APPEND ${list_to_add} ${file_to})
1304         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1305
1306         get_filename_component(_file_to_path ${file_to} PATH)
1307
1308         add_custom_command(
1309                 OUTPUT ${file_to}
1310                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1311                 COMMAND "$<TARGET_FILE:datatoc>" ${file_from} ${file_to}
1312                 DEPENDS ${file_from} datatoc)
1313
1314         set_source_files_properties(${file_to} PROPERTIES GENERATED TRUE)
1315 endfunction()
1316
1317
1318 # same as above but generates the var name and output automatic.
1319 function(data_to_c_simple
1320         file_from
1321         list_to_add
1322         )
1323
1324         # remove ../'s
1325         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from}   REALPATH)
1326         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${file_from}.c REALPATH)
1327
1328         list(APPEND ${list_to_add} ${_file_to})
1329         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1330
1331         get_filename_component(_file_to_path ${_file_to} PATH)
1332
1333         add_custom_command(
1334                 OUTPUT  ${_file_to}
1335                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1336                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1337                 DEPENDS ${_file_from} datatoc)
1338
1339         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1340 endfunction()
1341
1342 # macro for converting pixmap directory to a png and then a c file
1343 function(data_to_c_simple_icons
1344         path_from
1345         list_to_add
1346         )
1347
1348         # Conversion steps
1349         #  path_from  ->  _file_from  ->  _file_to
1350         #  foo/*.dat  ->  foo.png     ->  foo.png.c
1351
1352         get_filename_component(_path_from_abs ${path_from} ABSOLUTE)
1353         # remove ../'s
1354         get_filename_component(_file_from ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png   REALPATH)
1355         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${path_from}.png.c REALPATH)
1356
1357         list(APPEND ${list_to_add} ${_file_to})
1358         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1359
1360         get_filename_component(_file_to_path ${_file_to} PATH)
1361
1362         # ideally we wouldn't glob, but storing all names for all pixmaps is a bit heavy
1363         file(GLOB _icon_files "${path_from}/*.dat")
1364
1365         add_custom_command(
1366                 OUTPUT  ${_file_from} ${_file_to}
1367                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1368                 #COMMAND python3 ${CMAKE_SOURCE_DIR}/source/blender/datatoc/datatoc_icon.py ${_path_from_abs} ${_file_from}
1369                 COMMAND "$<TARGET_FILE:datatoc_icon>" ${_path_from_abs} ${_file_from}
1370                 COMMAND "$<TARGET_FILE:datatoc>" ${_file_from} ${_file_to}
1371                 DEPENDS
1372                         ${_icon_files}
1373                         datatoc_icon
1374                         datatoc
1375                         # could be an arg but for now we only create icons depending on UI_icons.h
1376                         ${CMAKE_SOURCE_DIR}/source/blender/editors/include/UI_icons.h
1377                 )
1378
1379         set_source_files_properties(${_file_from} ${_file_to} PROPERTIES GENERATED TRUE)
1380 endfunction()
1381
1382 # XXX Not used for now...
1383 function(svg_to_png
1384         file_from
1385         file_to
1386         dpi
1387         list_to_add
1388         )
1389
1390         # remove ../'s
1391         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from} REALPATH)
1392         get_filename_component(_file_to   ${CMAKE_CURRENT_SOURCE_DIR}/${file_to}   REALPATH)
1393
1394         list(APPEND ${list_to_add} ${_file_to})
1395         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1396
1397         find_program(INKSCAPE_EXE inkscape)
1398         mark_as_advanced(INKSCAPE_EXE)
1399
1400         if(INKSCAPE_EXE)
1401                 if(APPLE)
1402                         # in OS X app bundle, the binary is a shim that doesn't take any
1403                         # command line arguments, replace it with the actual binary
1404                         string(REPLACE "MacOS/Inkscape" "Resources/bin/inkscape" INKSCAPE_REAL_EXE ${INKSCAPE_EXE})
1405                         if(EXISTS "${INKSCAPE_REAL_EXE}")
1406                                 set(INKSCAPE_EXE ${INKSCAPE_REAL_EXE})
1407                         endif()
1408                 endif()
1409
1410                 add_custom_command(
1411                         OUTPUT  ${_file_to}
1412                         COMMAND ${INKSCAPE_EXE} ${_file_from} --export-dpi=${dpi}  --without-gui --export-png=${_file_to}
1413                         DEPENDS ${_file_from} ${INKSCAPE_EXE}
1414                 )
1415         else()
1416                 message(WARNING "Inkscape not found, could not re-generate ${_file_to} from ${_file_from}!")
1417         endif()
1418 endfunction()
1419
1420 function(msgfmt_simple
1421         file_from
1422         list_to_add
1423         )
1424
1425         # remove ../'s
1426         get_filename_component(_file_from_we ${file_from} NAME_WE)
1427
1428         get_filename_component(_file_from ${file_from} REALPATH)
1429         get_filename_component(_file_to ${CMAKE_CURRENT_BINARY_DIR}/${_file_from_we}.mo REALPATH)
1430
1431         list(APPEND ${list_to_add} ${_file_to})
1432         set(${list_to_add} ${${list_to_add}} PARENT_SCOPE)
1433
1434         get_filename_component(_file_to_path ${_file_to} PATH)
1435
1436         add_custom_command(
1437                 OUTPUT  ${_file_to}
1438                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
1439                 COMMAND "$<TARGET_FILE:msgfmt>" ${_file_from} ${_file_to}
1440                 DEPENDS msgfmt ${_file_from})
1441
1442         set_source_files_properties(${_file_to} PROPERTIES GENERATED TRUE)
1443 endfunction()
1444
1445 function(find_python_package
1446         package
1447         )
1448
1449         string(TOUPPER ${package} _upper_package)
1450
1451         # set but invalid
1452         if((NOT ${PYTHON_${_upper_package}_PATH} STREQUAL "") AND
1453            (NOT ${PYTHON_${_upper_package}_PATH} MATCHES NOTFOUND))
1454 #               if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}/${package}")
1455 #                       message(WARNING "PYTHON_${_upper_package}_PATH is invalid, ${package} not found in '${PYTHON_${_upper_package}_PATH}' "
1456 #                                       "WITH_PYTHON_INSTALL_${_upper_package} option will be ignored when installing python")
1457 #                       set(WITH_PYTHON_INSTALL${_upper_package} OFF)
1458 #               endif()
1459         # not set, so initialize
1460         else()
1461                 string(REPLACE "." ";" _PY_VER_SPLIT "${PYTHON_VERSION}")
1462                 list(GET _PY_VER_SPLIT 0 _PY_VER_MAJOR)
1463
1464                 # re-cache
1465                 unset(PYTHON_${_upper_package}_PATH CACHE)
1466                 find_path(PYTHON_${_upper_package}_PATH
1467                   NAMES
1468                     ${package}
1469                   HINTS
1470                     "${PYTHON_LIBPATH}/python${PYTHON_VERSION}/"
1471                     "${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/"
1472                   PATH_SUFFIXES
1473                     site-packages
1474                     dist-packages
1475                    NO_DEFAULT_PATH
1476                 )
1477
1478                  if(NOT EXISTS "${PYTHON_${_upper_package}_PATH}")
1479                         message(WARNING
1480                                 "Python package '${package}' path could not be found in:\n"
1481                                 "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/site-packages/${package}', "
1482                                 "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/site-packages/${package}', "
1483                                 "'${PYTHON_LIBPATH}/python${PYTHON_VERSION}/dist-packages/${package}', "
1484                                 "'${PYTHON_LIBPATH}/python${_PY_VER_MAJOR}/dist-packages/${package}', "
1485                                 "\n"
1486                                 "The 'WITH_PYTHON_INSTALL_${_upper_package}' option will be ignored when installing Python.\n"
1487                                 "The build will be usable, only add-ons that depend on this package won't be functional."
1488                         )
1489                         set(WITH_PYTHON_INSTALL_${_upper_package} OFF PARENT_SCOPE)
1490                 else()
1491                         message(STATUS "${package} found at '${PYTHON_${_upper_package}_PATH}'")
1492                 endif()
1493         endif()
1494 endfunction()
1495
1496 # like Python's 'print(dir())'
1497 function(print_all_vars)
1498         get_cmake_property(_vars VARIABLES)
1499         foreach(_var ${_vars})
1500                 message("${_var}=${${_var}}")
1501         endforeach()
1502 endfunction()