Merging r44109 through r44128 from trunk into soc-2011-tomato
[blender-staging.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                         ${JPEG_LIBRARIES}
216                         ${PNG_LIBRARIES}
217                         ${ZLIB_LIBRARIES}
218                         ${FREETYPE_LIBRARY}
219                         ${PLATFORM_LINKLIBS})
220
221         # since we are using the local libs for python when compiling msvc projects, we need to add _d when compiling debug versions
222         if(WITH_PYTHON)  # AND NOT WITH_PYTHON_MODULE  # WIN32 needs
223                 target_link_libraries(${target} ${PYTHON_LINKFLAGS})
224
225                 if(WIN32 AND NOT UNIX)
226                         file_list_suffix(PYTHON_LIBRARIES_DEBUG "${PYTHON_LIBRARIES}" "_d")
227                         target_link_libraries_debug(${target} "${PYTHON_LIBRARIES_DEBUG}")
228                         target_link_libraries_optimized(${target} "${PYTHON_LIBRARIES}")
229                         unset(PYTHON_LIBRARIES_DEBUG)
230                 else()
231                         target_link_libraries(${target} ${PYTHON_LIBRARIES})
232                 endif()
233         endif()
234
235         if(NOT WITH_BUILTIN_GLEW)
236                 target_link_libraries(${target} ${GLEW_LIBRARY})
237         endif()
238
239         if(WITH_INTERNATIONAL)
240                 target_link_libraries(${target} ${GETTEXT_LIBRARIES})
241
242                 if(WIN32 AND NOT UNIX)
243                         target_link_libraries(${target} ${ICONV_LIBRARIES})
244                 endif()
245         endif()
246
247         if(WITH_OPENAL)
248                 target_link_libraries(${target} ${OPENAL_LIBRARY})
249         endif()
250         if(WITH_FFTW3)
251                 target_link_libraries(${target} ${FFTW3_LIBRARIES})
252         endif()
253         if(WITH_JACK)
254                 target_link_libraries(${target} ${JACK_LIBRARIES})
255         endif()
256         if(WITH_CODEC_SNDFILE)
257                 target_link_libraries(${target} ${SNDFILE_LIBRARIES})
258         endif()
259         if(WITH_SDL)
260                 target_link_libraries(${target} ${SDL_LIBRARY})
261         endif()
262         if(WITH_CODEC_QUICKTIME)
263                 target_link_libraries(${target} ${QUICKTIME_LIBRARIES})
264         endif()
265         if(WITH_IMAGE_TIFF)
266                 target_link_libraries(${target} ${TIFF_LIBRARY})
267         endif()
268         if(WITH_OPENIMAGEIO)
269                 target_link_libraries(${target} ${OPENIMAGEIO_LIBRARIES})
270         endif()
271         if(WITH_BOOST)
272                 target_link_libraries(${target} ${BOOST_LIBRARIES})
273         endif()
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 endmacro()
332
333 macro(TEST_SSE_SUPPORT
334         _sse_flags
335         _sse2_flags)
336
337         include(CheckCSourceRuns)
338
339         # message(STATUS "Detecting SSE support")
340         if(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
341                 set(${_sse_flags} "-msse")
342                 set(${_sse2_flags} "-msse2")
343         elseif(MSVC)
344                 set(${_sse_flags} "/arch:SSE")
345                 set(${_sse2_flags} "/arch:SSE2")
346         elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
347                 set(${_sse_flags} "")  # icc defaults to -msse
348                 set(${_sse2_flags} "-msse2")
349         else()
350                 message(WARNING "SSE flags for this compiler: '${CMAKE_C_COMPILER_ID}' not known")
351                 set(${_sse_flags})
352                 set(${_sse2_flags})
353         endif()
354
355         set(CMAKE_REQUIRED_FLAGS "${${_sse_flags}} ${${_sse2_flags}}")
356
357         if(NOT DEFINED SUPPORT_SSE_BUILD)
358                 # result cached
359                 check_c_source_runs("
360                         #include <xmmintrin.h>
361                         int main(void) { __m128 v = _mm_setzero_ps(); return 0; }"
362                 SUPPORT_SSE_BUILD)
363
364                 if(SUPPORT_SSE_BUILD)
365                         message(STATUS "SSE Support: detected.")
366                 else()
367                         message(STATUS "SSE Support: missing.")
368                 endif()
369         endif()
370
371         if(NOT DEFINED SUPPORT_SSE2_BUILD)
372                 # result cached
373                 check_c_source_runs("
374                         #include <emmintrin.h>
375                         int main(void) { __m128d v = _mm_setzero_pd(); return 0; }"
376                 SUPPORT_SSE2_BUILD)
377
378                 if(SUPPORT_SSE2_BUILD)
379                         message(STATUS "SSE2 Support: detected.")
380                 else()
381                         message(STATUS "SSE2 Support: missing.")
382                 endif()
383         endif()
384
385         unset(CMAKE_REQUIRED_FLAGS)
386 endmacro()
387
388 # when we have warnings as errors applied globally this
389 # needs to be removed for some external libs which we dont maintain.
390
391 # utility macro
392 macro(remove_cc_flag
393         flag)
394
395         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
396         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
397         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
398         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
399         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
400
401         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
402         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
403         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
404         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
405         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
406
407 endmacro()
408
409 macro(add_cc_flag
410         flag)
411
412         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
413         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
414 endmacro()
415
416 macro(remove_strict_flags)
417
418         if(CMAKE_COMPILER_IS_GNUCC)
419                 remove_cc_flag("-Wstrict-prototypes")
420                 remove_cc_flag("-Wunused-parameter")
421                 remove_cc_flag("-Wwrite-strings")
422                 remove_cc_flag("-Wundef")
423                 remove_cc_flag("-Wshadow")
424                 remove_cc_flag("-Werror=[^ ]+")
425                 remove_cc_flag("-Werror")
426
427                 # negate flags implied by '-Wall'
428                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
429         endif()
430
431         if(MSVC)
432                 # TODO
433         endif()
434
435 endmacro()
436
437 # note, we can only append flags on a single file so we need to negate the options.
438 # at the moment we cant shut up ffmpeg deprecations, so use this, but will
439 # probably add more removals here.
440 macro(remove_strict_flags_file
441         filenames)
442
443         foreach(_SOURCE ${ARGV})
444
445                 if(CMAKE_COMPILER_IS_GNUCC)
446                         set_source_files_properties(${_SOURCE}
447                                 PROPERTIES
448                                         COMPILE_FLAGS "${CC_REMOVE_STRICT_FLAGS}"
449                         )
450                 endif()
451
452                 if(MSVC)
453                         # TODO
454                 endif()
455
456         endforeach()    
457
458         unset(_SOURCE)
459
460 endmacro()
461
462
463 macro(ADD_CHECK_C_COMPILER_FLAG
464         _CFLAGS
465         _CACHE_VAR
466         _FLAG)
467
468         include(CheckCCompilerFlag)
469
470         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
471         if(${_CACHE_VAR})
472                 # message(STATUS "Using CFLAG: ${_FLAG}")
473                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}")
474         else()
475                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
476         endif()
477 endmacro()
478
479 macro(ADD_CHECK_CXX_COMPILER_FLAG
480         _CXXFLAGS
481         _CACHE_VAR
482         _FLAG)
483
484         include(CheckCXXCompilerFlag)
485
486         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
487         if(${_CACHE_VAR})
488                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
489                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}")
490         else()
491                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
492         endif()
493 endmacro()
494
495 macro(get_blender_version)
496         # So cmake depends on BKE_blender.h, beware of inf-loops!
497         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
498
499         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
500
501         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
502         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
503         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
504         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
505
506         if(NOT ${_out_version} MATCHES "[0-9]+")
507                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
508         endif()
509
510         if(NOT ${_out_subversion} MATCHES "[0-9]+")
511                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
512         endif()
513
514         # clumsy regex, only single char are ok but it could be unset
515
516         string(LENGTH "${_out_version_char}" _out_version_char_len)
517         if(NOT _out_version_char_len EQUAL 1)
518                 set(_out_version_char "")
519         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
520                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
521         endif()
522
523         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
524                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
525         endif()
526
527         math(EXPR BLENDER_VERSION_MAJOR "${_out_version} / 100")
528         math(EXPR BLENDER_VERSION_MINOR "${_out_version} % 100")
529         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
530
531         set(BLENDER_SUBVERSION ${_out_subversion})
532         set(BLENDER_VERSION_CHAR ${_out_version_char})
533         set(BLENDER_VERSION_CYCLE ${_out_version_cycle})
534
535         # for packaging, alpha to numbers
536         string(COMPARE EQUAL "${BLENDER_VERSION_CHAR}" "" _out_version_char_empty)
537         if(${_out_version_char_empty})
538                 set(BLENDER_VERSION_CHAR_INDEX "0")
539         else()
540                 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)
541                 list(FIND _char_ls ${BLENDER_VERSION_CHAR} _out_version_char_index)
542                 math(EXPR BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index} + 1")
543                 unset(_char_ls)
544                 unset(_out_version_char_index)
545         endif()
546
547         unset(_out_subversion)
548         unset(_out_version_char)
549         unset(_out_version_char_empty)
550         unset(_out_version_cycle)
551
552         # message(STATUS "Version (Internal): ${BLENDER_VERSION}.${BLENDER_SUBVERSION}, Version (external): ${BLENDER_VERSION}${BLENDER_VERSION_CHAR}-${BLENDER_VERSION_CYCLE}")
553 endmacro()
554
555
556 # hacks to override initial project settings
557 # these macros must be called directly before/after project(Blender)
558 macro(blender_project_hack_pre)
559         # ----------------
560         # MINGW HACK START
561         # ignore system set flag, use our own
562         # must be before project(...)
563         # if the user wants to add their own its ok after first run.
564         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
565                 set(_reset_standard_libraries OFF)
566         else()
567                 set(_reset_standard_libraries ON)
568         endif()
569
570         # ------------------
571         # GCC -O3 HACK START
572         # needed because O3 can cause problems but
573         # allow the builder to set O3 manually after.
574         if(DEFINED CMAKE_C_FLAGS_RELEASE)
575                 set(_reset_standard_cflags_rel OFF)
576         else()
577                 set(_reset_standard_cflags_rel ON)
578         endif()
579         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
580                 set(_reset_standard_cxxflags_rel OFF)
581         else()
582                 set(_reset_standard_cxxflags_rel ON)
583         endif()
584 endmacro()
585
586
587 macro(blender_project_hack_post)
588         # --------------
589         # MINGW HACK END
590         if (_reset_standard_libraries)
591                 # Must come after project(...)
592                 #
593                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
594                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
595                 # with a meaningless stack trace. by overriding this flag we ensure we only
596                 # have libs we define and that cmake & scons builds match.
597                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
598                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
599                 mark_as_advanced(
600                         CMAKE_C_STANDARD_LIBRARIES
601                         CMAKE_CXX_STANDARD_LIBRARIES
602                 )
603         endif()
604         unset(_reset_standard_libraries)
605
606
607         # ----------------
608         # GCC -O3 HACK END
609         if(_reset_standard_cflags_rel)
610                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
611                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
612                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
613         endif()
614
615         if(_reset_standard_cxxflags_rel)
616                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
617                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
618                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
619         endif()
620
621         unset(_reset_standard_cflags_rel)
622         unset(_reset_standard_cxxflags_rel)
623
624         # ------------------------------------------------------------------
625         # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
626         if(CMAKE_COMPILER_IS_GNUCC)
627                 if(NOT DARWIN)
628                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
629                 endif()
630         endif()
631
632 endmacro()
633
634 # pair of macros to allow libraries to be specify files to install, but to
635 # only install them at the end so the directories don't get cleared with
636 # the files in them. used by cycles to install addon.
637 macro(delayed_install
638         base
639         files
640         destination)
641
642         foreach(f ${files})
643                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${base}/${f})
644                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_DESTINATIONS ${destination})
645         endforeach()
646 endmacro()
647
648 # note this is a function instead of a macro so that ${BUILD_TYPE} in targetdir
649 # does not get expanded in calling but is preserved
650 function(delayed_do_install
651         targetdir)
652
653         get_property(files GLOBAL PROPERTY DELAYED_INSTALL_FILES)
654         get_property(destinations GLOBAL PROPERTY DELAYED_INSTALL_DESTINATIONS)
655
656         if(files)
657                 list(LENGTH files n)
658                 math(EXPR n "${n}-1")
659
660                 foreach(i RANGE ${n})
661                         list(GET files ${i} f)
662                         list(GET destinations ${i} d)
663                         install(FILES ${f} DESTINATION ${targetdir}/${d})
664                 endforeach()
665         endif()
666 endfunction()
667
668 macro(set_lib_path
669                 lvar
670                 lproj)
671
672         
673         if(MSVC10 AND EXISTS ${LIBDIR}/vc2010/${lproj})
674                 set(${lvar} ${LIBDIR}/vc2010/${lproj})
675         else()
676                 set(${lvar} ${LIBDIR}/${lproj})
677         endif()
678
679
680 endmacro()