Fix El Topo:
[blender.git] / build_files / cmake / macros.cmake
1 # -*- mode: cmake; indent-tabs-mode: t; -*-
2
3
4 # foo_bar.spam --> foo_barMySuffix.spam
5 macro(file_suffix
6         file_name_new file_name file_suffix
7         )
8
9         get_filename_component(_file_name_PATH ${file_name} PATH)
10         get_filename_component(_file_name_NAME_WE ${file_name} NAME_WE)
11         get_filename_component(_file_name_EXT ${file_name} EXT)
12         set(${file_name_new} "${_file_name_PATH}/${_file_name_NAME_WE}${file_suffix}${_file_name_EXT}")
13
14         unset(_file_name_PATH)
15         unset(_file_name_NAME_WE)
16         unset(_file_name_EXT)
17 endmacro()
18
19 # useful for adding debug suffix to library lists:
20 # /somepath/foo.lib --> /somepath/foo_d.lib
21 macro(file_list_suffix
22         fp_list_new fp_list fn_suffix
23         )
24
25         # incase of empty list
26         set(_fp)
27         set(_fp_suffixed)
28
29         set(fp_list_new)
30
31         foreach(_fp ${fp_list})
32                 file_suffix(_fp_suffixed "${_fp}" "${fn_suffix}")
33                 list(APPEND "${fp_list_new}" "${_fp_suffixed}")
34         endforeach()
35
36         unset(_fp)
37         unset(_fp_suffixed)
38
39 endmacro()
40
41
42 macro(target_link_libraries_optimized TARGET LIBS)
43         foreach(_LIB ${LIBS})
44                 target_link_libraries(${TARGET} optimized "${_LIB}")
45         endforeach()
46         unset(_LIB)
47 endmacro()
48
49 macro(target_link_libraries_debug TARGET LIBS)
50         foreach(_LIB ${LIBS})
51                 target_link_libraries(${TARGET} debug "${_LIB}")
52         endforeach()
53         unset(_LIB)
54 endmacro()
55
56 # Nicer makefiles with -I/1/foo/ instead of -I/1/2/3/../../foo/
57 # use it instead of include_directories()
58 macro(blender_include_dirs
59         includes)
60         set(_ALL_INCS "")
61         foreach(_INC ${ARGV})
62                 get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
63                 list(APPEND _ALL_INCS ${_ABS_INC})
64                 # for checking for invalid includes, disable for regular use
65                 ##if(NOT EXISTS "${_ABS_INC}/")
66                 ##      message(FATAL_ERROR "Include not found: ${_ABS_INC}/")
67                 ##endif()
68         endforeach()
69         include_directories(${_ALL_INCS})
70         unset(_INC)
71         unset(_ABS_INC)
72         unset(_ALL_INCS)
73 endmacro()
74
75 macro(blender_include_dirs_sys
76         includes)
77         set(_ALL_INCS "")
78         foreach(_INC ${ARGV})
79                 get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
80                 list(APPEND _ALL_INCS ${_ABS_INC})
81                 ##if(NOT EXISTS "${_ABS_INC}/")
82                 ##      message(FATAL_ERROR "Include not found: ${_ABS_INC}/")
83                 ##endif()
84         endforeach()
85         include_directories(SYSTEM ${_ALL_INCS})
86         unset(_INC)
87         unset(_ABS_INC)
88         unset(_ALL_INCS)
89 endmacro()
90
91 macro(blender_source_group
92         sources)
93
94         # Group by location on disk
95         source_group("Source Files" FILES CMakeLists.txt)
96
97         foreach(_SRC ${sources})
98                 get_filename_component(_SRC_EXT ${_SRC} EXT)
99                 if((${_SRC_EXT} MATCHES ".h") OR (${_SRC_EXT} MATCHES ".hpp"))
100                         source_group("Header Files" FILES ${_SRC})
101                 else()
102                         source_group("Source Files" FILES ${_SRC})
103                 endif()
104         endforeach()
105
106         unset(_SRC)
107         unset(_SRC_EXT)
108 endmacro()
109
110
111 # only MSVC uses SOURCE_GROUP
112 macro(blender_add_lib_nolist
113         name
114         sources
115         includes
116         includes_sys)
117
118         # message(STATUS "Configuring library ${name}")
119
120         # include_directories(${includes})
121         # include_directories(SYSTEM ${includes_sys})
122         blender_include_dirs("${includes}")
123         blender_include_dirs_sys("${includes_sys}")
124
125         add_library(${name} ${sources})
126
127         # works fine without having the includes
128         # listed is helpful for IDE's (QtCreator/MSVC)
129         blender_source_group("${sources}")
130
131 endmacro()
132
133
134 macro(blender_add_lib
135         name
136         sources
137         includes
138         includes_sys)
139
140         blender_add_lib_nolist(${name} "${sources}" "${includes}" "${includes_sys}")
141
142         set_property(GLOBAL APPEND PROPERTY BLENDER_LINK_LIBS ${name})
143 endmacro()
144
145
146 macro(SETUP_LIBDIRS)
147         link_directories(${JPEG_LIBPATH} ${PNG_LIBPATH} ${ZLIB_LIBPATH} ${FREETYPE_LIBPATH})
148
149         if(WITH_PYTHON)  #  AND NOT WITH_PYTHON_MODULE  # WIN32 needs
150                 link_directories(${PYTHON_LIBPATH})
151         endif()
152         if(WITH_INTERNATIONAL)
153                 link_directories(${ICONV_LIBPATH})
154                 link_directories(${GETTEXT_LIBPATH})
155         endif()
156         if(WITH_SDL)
157                 link_directories(${SDL_LIBPATH})
158         endif()
159         if(WITH_CODEC_FFMPEG)
160                 link_directories(${FFMPEG_LIBPATH})
161         endif()
162         if(WITH_IMAGE_OPENEXR)
163                 link_directories(${OPENEXR_LIBPATH})
164         endif()
165         if(WITH_IMAGE_TIFF)
166                 link_directories(${TIFF_LIBPATH})
167         endif()
168         if(WITH_BOOST)
169                 link_directories(${BOOST_LIBPATH})
170         endif()
171         if(WITH_OPENIMAGEIO)
172                 link_directories(${OPENIMAGEIO_LIBPATH})
173         endif()
174         if(WITH_IMAGE_OPENJPEG AND UNIX AND NOT APPLE)
175                 link_directories(${OPENJPEG_LIBPATH})
176         endif()
177         if(WITH_CODEC_QUICKTIME)
178                 link_directories(${QUICKTIME_LIBPATH})
179         endif()
180         if(WITH_OPENAL)
181                 link_directories(${OPENAL_LIBPATH})
182         endif()
183         if(WITH_JACK)
184                 link_directories(${JACK_LIBPATH})
185         endif()
186         if(WITH_CODEC_SNDFILE)
187                 link_directories(${SNDFILE_LIBPATH})
188         endif()
189         if(WITH_FFTW3)
190                 link_directories(${FFTW3_LIBPATH})
191         endif()
192         if(WITH_OPENCOLLADA)
193                 link_directories(${OPENCOLLADA_LIBPATH})
194                 link_directories(${PCRE_LIBPATH})
195                 link_directories(${EXPAT_LIBPATH})
196         endif()
197         if(WITH_MEM_JEMALLOC)
198                 link_directories(${JEMALLOC_LIBPATH})
199         endif()
200
201         if(WIN32 AND NOT UNIX)
202                 link_directories(${PTHREADS_LIBPATH})
203         endif()
204 endmacro()
205
206 macro(setup_liblinks
207         target)
208
209         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}")
210         set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}")
211
212         target_link_libraries(${target}
213                         ${OPENGL_gl_LIBRARY}
214                         ${OPENGL_glu_LIBRARY}
215                         ${PNG_LIBRARIES}
216                         ${ZLIB_LIBRARIES}
217                         ${FREETYPE_LIBRARY}
218                         ${LAPACK_LIBRARIES})
219
220         # since we are using the local libs for python when compiling msvc projects, we need to add _d when compiling debug versions
221         if(WITH_PYTHON)  # AND NOT WITH_PYTHON_MODULE  # WIN32 needs
222                 target_link_libraries(${target} ${PYTHON_LINKFLAGS})
223
224                 if(WIN32 AND NOT UNIX)
225                         file_list_suffix(PYTHON_LIBRARIES_DEBUG "${PYTHON_LIBRARIES}" "_d")
226                         target_link_libraries_debug(${target} "${PYTHON_LIBRARIES_DEBUG}")
227                         target_link_libraries_optimized(${target} "${PYTHON_LIBRARIES}")
228                         unset(PYTHON_LIBRARIES_DEBUG)
229                 else()
230                         target_link_libraries(${target} ${PYTHON_LIBRARIES})
231                 endif()
232         endif()
233
234         if(NOT WITH_BUILTIN_GLEW)
235                 target_link_libraries(${target} ${GLEW_LIBRARY})
236         endif()
237
238         if(WITH_INTERNATIONAL)
239                 target_link_libraries(${target} ${GETTEXT_LIBRARIES})
240
241                 if(WIN32 AND NOT UNIX)
242                         target_link_libraries(${target} ${ICONV_LIBRARIES})
243                 endif()
244         endif()
245
246         if(WITH_OPENAL)
247                 target_link_libraries(${target} ${OPENAL_LIBRARY})
248         endif()
249         if(WITH_FFTW3)
250                 target_link_libraries(${target} ${FFTW3_LIBRARIES})
251         endif()
252         if(WITH_JACK)
253                 target_link_libraries(${target} ${JACK_LIBRARIES})
254         endif()
255         if(WITH_CODEC_SNDFILE)
256                 target_link_libraries(${target} ${SNDFILE_LIBRARIES})
257         endif()
258         if(WITH_SDL)
259                 target_link_libraries(${target} ${SDL_LIBRARY})
260         endif()
261         if(WITH_CODEC_QUICKTIME)
262                 target_link_libraries(${target} ${QUICKTIME_LIBRARIES})
263         endif()
264         if(WITH_IMAGE_TIFF)
265                 target_link_libraries(${target} ${TIFF_LIBRARY})
266         endif()
267         if(WITH_OPENIMAGEIO)
268                 target_link_libraries(${target} ${OPENIMAGEIO_LIBRARIES})
269         endif()
270         if(WITH_BOOST)
271                 target_link_libraries(${target} ${BOOST_LIBRARIES})
272         endif()
273         target_link_libraries(${target} ${JPEG_LIBRARIES})
274         if(WITH_IMAGE_OPENEXR)
275                 if(WIN32 AND NOT UNIX AND NOT CMAKE_COMPILER_IS_GNUCC)
276                         file_list_suffix(OPENEXR_LIBRARIES_DEBUG "${OPENEXR_LIBRARIES}" "_d")
277                         target_link_libraries_debug(${target} "${OPENEXR_LIBRARIES_DEBUG}")
278                         target_link_libraries_optimized(${target} "${OPENEXR_LIBRARIES}")
279                         unset(OPENEXR_LIBRARIES_DEBUG)
280                 else()
281                         target_link_libraries(${target} ${OPENEXR_LIBRARIES})
282                 endif()
283         endif()
284         if(WITH_IMAGE_OPENJPEG AND UNIX AND NOT APPLE)
285                 target_link_libraries(${target} ${OPENJPEG_LIBRARIES})
286         endif()
287         if(WITH_CODEC_FFMPEG)
288
289                 # Strange!, without this ffmpeg gives linking errors (on linux)
290                 # even though its linked above
291                 target_link_libraries(${target} ${OPENGL_glu_LIBRARY})
292
293                 target_link_libraries(${target} ${FFMPEG_LIBRARIES})
294         endif()
295         if(WITH_OPENCOLLADA)
296                 if(WIN32 AND NOT UNIX)
297                         file_list_suffix(OPENCOLLADA_LIBRARIES_DEBUG "${OPENCOLLADA_LIBRARIES}" "_d")
298                         target_link_libraries_debug(${target} "${OPENCOLLADA_LIBRARIES_DEBUG}")
299                         target_link_libraries_optimized(${target} "${OPENCOLLADA_LIBRARIES}")
300                         unset(OPENCOLLADA_LIBRARIES_DEBUG)
301
302                         file_list_suffix(PCRE_LIBRARIES_DEBUG "${PCRE_LIBRARIES}" "_d")
303                         target_link_libraries_debug(${target} "${PCRE_LIBRARIES_DEBUG}")
304                         target_link_libraries_optimized(${target} "${PCRE_LIBRARIES}")
305                         unset(PCRE_LIBRARIES_DEBUG)
306
307                         if(EXPAT_LIB)
308                                 file_list_suffix(EXPAT_LIB_DEBUG "${EXPAT_LIB}" "_d")
309                                 target_link_libraries_debug(${target} "${EXPAT_LIB_DEBUG}")
310                                 target_link_libraries_optimized(${target} "${EXPAT_LIB}")
311                                 unset(EXPAT_LIB_DEBUG)
312                         endif()
313                 else()
314                         target_link_libraries(${target}
315                                         ${OPENCOLLADA_LIBRARIES}
316                                         ${PCRE_LIBRARIES}
317                                         ${XML2_LIBRARIES}
318                                         ${EXPAT_LIB})
319                 endif()
320         endif()
321         if(WITH_MEM_JEMALLOC)
322                 target_link_libraries(${target} ${JEMALLOC_LIBRARIES})
323         endif()
324         if(WITH_INPUT_NDOF)
325                 target_link_libraries(${target} ${NDOF_LIBRARIES})
326         endif()
327
328         if(WIN32 AND NOT UNIX)
329                 target_link_libraries(${target} ${PTHREADS_LIBRARIES})
330         endif()
331
332         target_link_libraries(${target} ${PLATFORM_LINKLIBS})
333 endmacro()
334
335 macro(TEST_SSE_SUPPORT
336         _sse_flags
337         _sse2_flags)
338
339         include(CheckCSourceRuns)
340
341         # message(STATUS "Detecting SSE support")
342         if(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
343                 set(${_sse_flags} "-msse")
344                 set(${_sse2_flags} "-msse2")
345         elseif(MSVC)
346                 set(${_sse_flags} "/arch:SSE")
347                 set(${_sse2_flags} "/arch:SSE2")
348         elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
349                 set(${_sse_flags} "")  # icc defaults to -msse
350                 set(${_sse2_flags} "-msse2")
351         else()
352                 message(WARNING "SSE flags for this compiler: '${CMAKE_C_COMPILER_ID}' not known")
353                 set(${_sse_flags})
354                 set(${_sse2_flags})
355         endif()
356
357         set(CMAKE_REQUIRED_FLAGS "${${_sse_flags}} ${${_sse2_flags}}")
358
359         if(NOT DEFINED SUPPORT_SSE_BUILD)
360                 # result cached
361                 check_c_source_runs("
362                         #include <xmmintrin.h>
363                         int main(void) { __m128 v = _mm_setzero_ps(); return 0; }"
364                 SUPPORT_SSE_BUILD)
365
366                 if(SUPPORT_SSE_BUILD)
367                         message(STATUS "SSE Support: detected.")
368                 else()
369                         message(STATUS "SSE Support: missing.")
370                 endif()
371         endif()
372
373         if(NOT DEFINED SUPPORT_SSE2_BUILD)
374                 # result cached
375                 check_c_source_runs("
376                         #include <emmintrin.h>
377                         int main(void) { __m128d v = _mm_setzero_pd(); return 0; }"
378                 SUPPORT_SSE2_BUILD)
379
380                 if(SUPPORT_SSE2_BUILD)
381                         message(STATUS "SSE2 Support: detected.")
382                 else()
383                         message(STATUS "SSE2 Support: missing.")
384                 endif()
385         endif()
386
387         unset(CMAKE_REQUIRED_FLAGS)
388 endmacro()
389
390 # when we have warnings as errors applied globally this
391 # needs to be removed for some external libs which we dont maintain.
392
393 # utility macro
394 macro(remove_cc_flag
395         flag)
396
397         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
398         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
399         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
400         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
401         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
402
403         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
404         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
405         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
406         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
407         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
408
409 endmacro()
410
411 macro(add_cc_flag
412         flag)
413
414         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
415         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
416 endmacro()
417
418 macro(remove_strict_flags)
419
420         if(CMAKE_COMPILER_IS_GNUCC)
421                 remove_cc_flag("-Wstrict-prototypes")
422                 remove_cc_flag("-Wunused-parameter")
423                 remove_cc_flag("-Wwrite-strings")
424                 remove_cc_flag("-Wundef")
425                 remove_cc_flag("-Wshadow")
426                 remove_cc_flag("-Werror=[^ ]+")
427                 remove_cc_flag("-Werror")
428
429                 # negate flags implied by '-Wall'
430                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
431         endif()
432
433         if(MSVC)
434                 # TODO
435         endif()
436
437 endmacro()
438
439 # note, we can only append flags on a single file so we need to negate the options.
440 # at the moment we cant shut up ffmpeg deprecations, so use this, but will
441 # probably add more removals here.
442 macro(remove_strict_flags_file
443         filenames)
444
445         foreach(_SOURCE ${ARGV})
446
447                 if(CMAKE_COMPILER_IS_GNUCC)
448                         set_source_files_properties(${_SOURCE}
449                                 PROPERTIES
450                                         COMPILE_FLAGS "${CC_REMOVE_STRICT_FLAGS}"
451                         )
452                 endif()
453
454                 if(MSVC)
455                         # TODO
456                 endif()
457
458         endforeach()    
459
460         unset(_SOURCE)
461
462 endmacro()
463
464
465 macro(ADD_CHECK_C_COMPILER_FLAG
466         _CFLAGS
467         _CACHE_VAR
468         _FLAG)
469
470         include(CheckCCompilerFlag)
471
472         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
473         if(${_CACHE_VAR})
474                 # message(STATUS "Using CFLAG: ${_FLAG}")
475                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}")
476         else()
477                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
478         endif()
479 endmacro()
480
481 macro(ADD_CHECK_CXX_COMPILER_FLAG
482         _CXXFLAGS
483         _CACHE_VAR
484         _FLAG)
485
486         include(CheckCXXCompilerFlag)
487
488         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
489         if(${_CACHE_VAR})
490                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
491                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}")
492         else()
493                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
494         endif()
495 endmacro()
496
497 macro(get_blender_version)
498         # So cmake depends on BKE_blender.h, beware of inf-loops!
499         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
500
501         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
502
503         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
504         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
505         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
506         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
507
508         if(NOT ${_out_version} MATCHES "[0-9]+")
509                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
510         endif()
511
512         if(NOT ${_out_subversion} MATCHES "[0-9]+")
513                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
514         endif()
515
516         # clumsy regex, only single char are ok but it could be unset
517
518         string(LENGTH "${_out_version_char}" _out_version_char_len)
519         if(NOT _out_version_char_len EQUAL 1)
520                 set(_out_version_char "")
521         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
522                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
523         endif()
524
525         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
526                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
527         endif()
528
529         math(EXPR BLENDER_VERSION_MAJOR "${_out_version} / 100")
530         math(EXPR BLENDER_VERSION_MINOR "${_out_version} % 100")
531         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
532
533         set(BLENDER_SUBVERSION ${_out_subversion})
534         set(BLENDER_VERSION_CHAR ${_out_version_char})
535         set(BLENDER_VERSION_CYCLE ${_out_version_cycle})
536
537         # for packaging, alpha to numbers
538         string(COMPARE EQUAL "${BLENDER_VERSION_CHAR}" "" _out_version_char_empty)
539         if(${_out_version_char_empty})
540                 set(BLENDER_VERSION_CHAR_INDEX "0")
541         else()
542                 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)
543                 list(FIND _char_ls ${BLENDER_VERSION_CHAR} _out_version_char_index)
544                 math(EXPR BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index} + 1")
545                 unset(_char_ls)
546                 unset(_out_version_char_index)
547         endif()
548
549         unset(_out_subversion)
550         unset(_out_version_char)
551         unset(_out_version_char_empty)
552         unset(_out_version_cycle)
553
554         # message(STATUS "Version (Internal): ${BLENDER_VERSION}.${BLENDER_SUBVERSION}, Version (external): ${BLENDER_VERSION}${BLENDER_VERSION_CHAR}-${BLENDER_VERSION_CYCLE}")
555 endmacro()
556
557
558 # hacks to override initial project settings
559 # these macros must be called directly before/after project(Blender)
560 macro(blender_project_hack_pre)
561         # ----------------
562         # MINGW HACK START
563         # ignore system set flag, use our own
564         # must be before project(...)
565         # if the user wants to add their own its ok after first run.
566         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
567                 set(_reset_standard_libraries OFF)
568         else()
569                 set(_reset_standard_libraries ON)
570         endif()
571
572         # ------------------
573         # GCC -O3 HACK START
574         # needed because O3 can cause problems but
575         # allow the builder to set O3 manually after.
576         if(DEFINED CMAKE_C_FLAGS_RELEASE)
577                 set(_reset_standard_cflags_rel OFF)
578         else()
579                 set(_reset_standard_cflags_rel ON)
580         endif()
581         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
582                 set(_reset_standard_cxxflags_rel OFF)
583         else()
584                 set(_reset_standard_cxxflags_rel ON)
585         endif()
586 endmacro()
587
588
589 macro(blender_project_hack_post)
590         # --------------
591         # MINGW HACK END
592         if (_reset_standard_libraries)
593                 # Must come after project(...)
594                 #
595                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
596                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
597                 # with a meaningless stack trace. by overriding this flag we ensure we only
598                 # have libs we define and that cmake & scons builds match.
599                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
600                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
601                 mark_as_advanced(
602                         CMAKE_C_STANDARD_LIBRARIES
603                         CMAKE_CXX_STANDARD_LIBRARIES
604                 )
605         endif()
606         unset(_reset_standard_libraries)
607
608
609         # ----------------
610         # GCC -O3 HACK END
611         if(_reset_standard_cflags_rel)
612                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
613                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
614                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
615         endif()
616
617         if(_reset_standard_cxxflags_rel)
618                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
619                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
620                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
621         endif()
622
623         unset(_reset_standard_cflags_rel)
624         unset(_reset_standard_cxxflags_rel)
625
626         # ------------------------------------------------------------------
627         # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
628         if(CMAKE_COMPILER_IS_GNUCC)
629                 if(NOT DARWIN)
630                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
631                 endif()
632         endif()
633
634 endmacro()
635
636 # pair of macros to allow libraries to be specify files to install, but to
637 # only install them at the end so the directories don't get cleared with
638 # the files in them. used by cycles to install addon.
639 macro(delayed_install
640         base
641         files
642         destination)
643
644         foreach(f ${files})
645                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${base}/${f})
646                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_DESTINATIONS ${destination})
647         endforeach()
648 endmacro()
649
650 # note this is a function instead of a macro so that ${BUILD_TYPE} in targetdir
651 # does not get expanded in calling but is preserved
652 function(delayed_do_install
653         targetdir)
654
655         get_property(files GLOBAL PROPERTY DELAYED_INSTALL_FILES)
656         get_property(destinations GLOBAL PROPERTY DELAYED_INSTALL_DESTINATIONS)
657
658         if(files)
659                 list(LENGTH files n)
660                 math(EXPR n "${n}-1")
661
662                 foreach(i RANGE ${n})
663                         list(GET files ${i} f)
664                         list(GET destinations ${i} d)
665                         install(FILES ${f} DESTINATION ${targetdir}/${d})
666                 endforeach()
667         endif()
668 endfunction()
669
670 macro(set_lib_path
671                 lvar
672                 lproj)
673
674         
675         if(MSVC10 AND EXISTS ${LIBDIR}/vc2010/${lproj})
676                 set(${lvar} ${LIBDIR}/vc2010/${lproj})
677         else()
678                 set(${lvar} ${LIBDIR}/${lproj})
679         endif()
680
681
682 endmacro()