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