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