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