Merging r50484 through r50501 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(WITH_CYCLES_OSL)
377                 target_link_libraries(${target} ${OSL_LIBRARIES})
378         endif()
379         if(WIN32 AND NOT UNIX)
380                 target_link_libraries(${target} ${PTHREADS_LIBRARIES})
381         endif()
382
383         target_link_libraries(${target} ${PLATFORM_LINKLIBS})
384 endmacro()
385
386 macro(TEST_SSE_SUPPORT
387         _sse_flags
388         _sse2_flags)
389
390         include(CheckCSourceRuns)
391
392         # message(STATUS "Detecting SSE support")
393         if(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
394                 set(${_sse_flags} "-msse")
395                 set(${_sse2_flags} "-msse2")
396         elseif(MSVC)
397                 set(${_sse_flags} "/arch:SSE")
398                 set(${_sse2_flags} "/arch:SSE2")
399         elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
400                 set(${_sse_flags} "")  # icc defaults to -msse
401                 set(${_sse2_flags} "-msse2")
402         else()
403                 message(WARNING "SSE flags for this compiler: '${CMAKE_C_COMPILER_ID}' not known")
404                 set(${_sse_flags})
405                 set(${_sse2_flags})
406         endif()
407
408         set(CMAKE_REQUIRED_FLAGS "${${_sse_flags}} ${${_sse2_flags}}")
409
410         if(NOT DEFINED SUPPORT_SSE_BUILD)
411                 # result cached
412                 check_c_source_runs("
413                         #include <xmmintrin.h>
414                         int main(void) { __m128 v = _mm_setzero_ps(); return 0; }"
415                 SUPPORT_SSE_BUILD)
416
417                 if(SUPPORT_SSE_BUILD)
418                         message(STATUS "SSE Support: detected.")
419                 else()
420                         message(STATUS "SSE Support: missing.")
421                 endif()
422         endif()
423
424         if(NOT DEFINED SUPPORT_SSE2_BUILD)
425                 # result cached
426                 check_c_source_runs("
427                         #include <emmintrin.h>
428                         int main(void) { __m128d v = _mm_setzero_pd(); return 0; }"
429                 SUPPORT_SSE2_BUILD)
430
431                 if(SUPPORT_SSE2_BUILD)
432                         message(STATUS "SSE2 Support: detected.")
433                 else()
434                         message(STATUS "SSE2 Support: missing.")
435                 endif()
436         endif()
437
438         unset(CMAKE_REQUIRED_FLAGS)
439 endmacro()
440
441 # when we have warnings as errors applied globally this
442 # needs to be removed for some external libs which we dont maintain.
443
444 # utility macro
445 macro(remove_cc_flag
446         flag)
447
448         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
449         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
450         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
451         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
452         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
453
454         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
455         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
456         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
457         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
458         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
459
460 endmacro()
461
462 macro(add_cc_flag
463         flag)
464
465         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
466         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
467 endmacro()
468
469 macro(remove_strict_flags)
470
471         if(CMAKE_COMPILER_IS_GNUCC)
472                 remove_cc_flag("-Wstrict-prototypes")
473                 remove_cc_flag("-Wunused-parameter")
474                 remove_cc_flag("-Wwrite-strings")
475                 remove_cc_flag("-Wundef")
476                 remove_cc_flag("-Wshadow")
477                 remove_cc_flag("-Werror=[^ ]+")
478                 remove_cc_flag("-Werror")
479
480                 # negate flags implied by '-Wall'
481                 add_cc_flag("${CC_REMOVE_STRICT_FLAGS}")
482         endif()
483
484         if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
485                 remove_cc_flag("-Wunused-parameter")
486                 remove_cc_flag("-Wunused-variable")
487                 remove_cc_flag("-Werror")
488         endif()
489
490         if(MSVC)
491                 # TODO
492         endif()
493
494 endmacro()
495
496 # note, we can only append flags on a single file so we need to negate the options.
497 # at the moment we cant shut up ffmpeg deprecations, so use this, but will
498 # probably add more removals here.
499 macro(remove_strict_flags_file
500         filenames)
501
502         foreach(_SOURCE ${ARGV})
503
504                 if(CMAKE_COMPILER_IS_GNUCC)
505                         set_source_files_properties(${_SOURCE}
506                                 PROPERTIES
507                                         COMPILE_FLAGS "${CC_REMOVE_STRICT_FLAGS}"
508                         )
509                 endif()
510
511                 if(MSVC)
512                         # TODO
513                 endif()
514
515         endforeach()    
516
517         unset(_SOURCE)
518
519 endmacro()
520
521
522 macro(ADD_CHECK_C_COMPILER_FLAG
523         _CFLAGS
524         _CACHE_VAR
525         _FLAG)
526
527         include(CheckCCompilerFlag)
528
529         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
530         if(${_CACHE_VAR})
531                 # message(STATUS "Using CFLAG: ${_FLAG}")
532                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}")
533         else()
534                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
535         endif()
536 endmacro()
537
538 macro(ADD_CHECK_CXX_COMPILER_FLAG
539         _CXXFLAGS
540         _CACHE_VAR
541         _FLAG)
542
543         include(CheckCXXCompilerFlag)
544
545         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
546         if(${_CACHE_VAR})
547                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
548                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}")
549         else()
550                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
551         endif()
552 endmacro()
553
554 macro(get_blender_version)
555         # So cmake depends on BKE_blender.h, beware of inf-loops!
556         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
557
558         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
559
560         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
561         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
562         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
563         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
564
565         if(NOT ${_out_version} MATCHES "[0-9]+")
566                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
567         endif()
568
569         if(NOT ${_out_subversion} MATCHES "[0-9]+")
570                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
571         endif()
572
573         # clumsy regex, only single char are ok but it could be unset
574
575         string(LENGTH "${_out_version_char}" _out_version_char_len)
576         if(NOT _out_version_char_len EQUAL 1)
577                 set(_out_version_char "")
578         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
579                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
580         endif()
581
582         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
583                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
584         endif()
585
586         math(EXPR BLENDER_VERSION_MAJOR "${_out_version} / 100")
587         math(EXPR BLENDER_VERSION_MINOR "${_out_version} % 100")
588         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
589
590         set(BLENDER_SUBVERSION ${_out_subversion})
591         set(BLENDER_VERSION_CHAR ${_out_version_char})
592         set(BLENDER_VERSION_CYCLE ${_out_version_cycle})
593
594         # for packaging, alpha to numbers
595         string(COMPARE EQUAL "${BLENDER_VERSION_CHAR}" "" _out_version_char_empty)
596         if(${_out_version_char_empty})
597                 set(BLENDER_VERSION_CHAR_INDEX "0")
598         else()
599                 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)
600                 list(FIND _char_ls ${BLENDER_VERSION_CHAR} _out_version_char_index)
601                 math(EXPR BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index} + 1")
602                 unset(_char_ls)
603                 unset(_out_version_char_index)
604         endif()
605
606         unset(_out_subversion)
607         unset(_out_version_char)
608         unset(_out_version_char_empty)
609         unset(_out_version_cycle)
610
611         # message(STATUS "Version (Internal): ${BLENDER_VERSION}.${BLENDER_SUBVERSION}, Version (external): ${BLENDER_VERSION}${BLENDER_VERSION_CHAR}-${BLENDER_VERSION_CYCLE}")
612 endmacro()
613
614
615 # hacks to override initial project settings
616 # these macros must be called directly before/after project(Blender)
617 macro(blender_project_hack_pre)
618         # ----------------
619         # MINGW HACK START
620         # ignore system set flag, use our own
621         # must be before project(...)
622         # if the user wants to add their own its ok after first run.
623         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
624                 set(_reset_standard_libraries OFF)
625         else()
626                 set(_reset_standard_libraries ON)
627         endif()
628
629         # ------------------
630         # GCC -O3 HACK START
631         # needed because O3 can cause problems but
632         # allow the builder to set O3 manually after.
633         if(DEFINED CMAKE_C_FLAGS_RELEASE)
634                 set(_reset_standard_cflags_rel OFF)
635         else()
636                 set(_reset_standard_cflags_rel ON)
637         endif()
638         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
639                 set(_reset_standard_cxxflags_rel OFF)
640         else()
641                 set(_reset_standard_cxxflags_rel ON)
642         endif()
643 endmacro()
644
645
646 macro(blender_project_hack_post)
647         # --------------
648         # MINGW HACK END
649         if (_reset_standard_libraries)
650                 # Must come after projecINCt(...)
651                 #
652                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
653                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
654                 # with a meaningless stack trace. by overriding this flag we ensure we only
655                 # have libs we define and that cmake & scons builds match.
656                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
657                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
658                 mark_as_advanced(
659                         CMAKE_C_STANDARD_LIBRARIES
660                         CMAKE_CXX_STANDARD_LIBRARIES
661                 )
662         endif()
663         unset(_reset_standard_libraries)
664
665
666         # ----------------
667         # GCC -O3 HACK END
668         if(_reset_standard_cflags_rel)
669                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
670                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
671                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
672         endif()
673
674         if(_reset_standard_cxxflags_rel)
675                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
676                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
677                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
678         endif()
679
680         unset(_reset_standard_cflags_rel)
681         unset(_reset_standard_cxxflags_rel)
682
683         # ------------------------------------------------------------------
684         # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
685         if(CMAKE_COMPILER_IS_GNUCC)
686                 if(NOT DARWIN)
687                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
688                 endif()
689         endif()
690
691 endmacro()
692
693 # pair of macros to allow libraries to be specify files to install, but to
694 # only install them at the end so the directories don't get cleared with
695 # the files in them. used by cycles to install addon.
696 macro(delayed_install
697         base
698         files
699         destination)
700
701         foreach(f ${files})
702                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${base}/${f})
703                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_DESTINATIONS ${destination})
704         endforeach()
705 endmacro()
706
707 # note this is a function instead of a macro so that ${BUILD_TYPE} in targetdir
708 # does not get expanded in calling but is preserved
709 function(delayed_do_install
710         targetdir)
711
712         get_property(files GLOBAL PROPERTY DELAYED_INSTALL_FILES)
713         get_property(destinations GLOBAL PROPERTY DELAYED_INSTALL_DESTINATIONS)
714
715         if(files)
716                 list(LENGTH files n)
717                 math(EXPR n "${n}-1")
718
719                 foreach(i RANGE ${n})
720                         list(GET files ${i} f)
721                         list(GET destinations ${i} d)
722                         install(FILES ${f} DESTINATION ${targetdir}/${d})
723                 endforeach()
724         endif()
725 endfunction()
726
727 macro(set_lib_path
728                 lvar
729                 lproj)
730
731         if(MSVC10)
732                 set(${lvar} ${LIBDIR}/${lproj}/vc2010)
733         else()
734                 set(${lvar} ${LIBDIR}/${lproj})
735         endif()
736 endmacro()
737
738
739 macro(data_to_c
740       file_from file_to
741       list_to_add)
742
743         list(APPEND ${list_to_add} ${file_to})
744
745         get_filename_component(_file_to_path ${file_to} PATH)
746
747         add_custom_command(
748                 OUTPUT ${file_to}
749                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
750                 COMMAND ${CMAKE_BINARY_DIR}/bin/${CMAKE_CFG_INTDIR}/datatoc ${file_from} ${file_to}
751                 DEPENDS ${file_from} datatoc)
752         unset(_file_to_path)
753 endmacro()
754
755
756 # same as above but generates the var name and output automatic.
757 macro(data_to_c_simple
758       file_from
759       list_to_add)
760
761         # remove ../'s
762         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from}   REALPATH)
763         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${file_from}.c REALPATH)
764
765         list(APPEND ${list_to_add} ${_file_to})
766
767         get_filename_component(_file_to_path ${_file_to} PATH)
768
769         add_custom_command(
770                 OUTPUT  ${_file_to}
771                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
772                 COMMAND ${CMAKE_BINARY_DIR}/bin/${CMAKE_CFG_INTDIR}/datatoc ${_file_from} ${_file_to}
773                 DEPENDS ${_file_from} datatoc)
774
775         unset(_file_from)
776         unset(_file_to)
777         unset(_file_to_path)
778 endmacro()