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