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