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