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