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