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