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