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