Fix various warnings with clang build, and adjust cmake clang warnings flags
[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                 remove_cc_flag("-Werror")
504         endif()
505
506         if(MSVC)
507                 # TODO
508         endif()
509
510 endmacro()
511
512 # note, we can only append flags on a single file so we need to negate the options.
513 # at the moment we cant shut up ffmpeg deprecations, so use this, but will
514 # probably add more removals here.
515 macro(remove_strict_flags_file
516         filenames)
517
518         foreach(_SOURCE ${ARGV})
519
520                 if(CMAKE_COMPILER_IS_GNUCC)
521                         set_source_files_properties(${_SOURCE}
522                                 PROPERTIES
523                                         COMPILE_FLAGS "${CC_REMOVE_STRICT_FLAGS}"
524                         )
525                 endif()
526
527                 if(MSVC)
528                         # TODO
529                 endif()
530
531         endforeach()
532
533         unset(_SOURCE)
534
535 endmacro()
536
537
538 macro(ADD_CHECK_C_COMPILER_FLAG
539         _CFLAGS
540         _CACHE_VAR
541         _FLAG)
542
543         include(CheckCCompilerFlag)
544
545         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
546         if(${_CACHE_VAR})
547                 # message(STATUS "Using CFLAG: ${_FLAG}")
548                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}")
549         else()
550                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
551         endif()
552 endmacro()
553
554 macro(ADD_CHECK_CXX_COMPILER_FLAG
555         _CXXFLAGS
556         _CACHE_VAR
557         _FLAG)
558
559         include(CheckCXXCompilerFlag)
560
561         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
562         if(${_CACHE_VAR})
563                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
564                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}")
565         else()
566                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
567         endif()
568 endmacro()
569
570 macro(get_blender_version)
571         # So cmake depends on BKE_blender.h, beware of inf-loops!
572         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
573
574         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
575
576         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
577         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
578         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
579         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
580
581         if(NOT ${_out_version} MATCHES "[0-9]+")
582                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
583         endif()
584
585         if(NOT ${_out_subversion} MATCHES "[0-9]+")
586                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
587         endif()
588
589         # clumsy regex, only single char are ok but it could be unset
590
591         string(LENGTH "${_out_version_char}" _out_version_char_len)
592         if(NOT _out_version_char_len EQUAL 1)
593                 set(_out_version_char "")
594         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
595                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
596         endif()
597
598         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
599                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
600         endif()
601
602         math(EXPR BLENDER_VERSION_MAJOR "${_out_version} / 100")
603         math(EXPR BLENDER_VERSION_MINOR "${_out_version} % 100")
604         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
605
606         set(BLENDER_SUBVERSION ${_out_subversion})
607         set(BLENDER_VERSION_CHAR ${_out_version_char})
608         set(BLENDER_VERSION_CYCLE ${_out_version_cycle})
609
610         # for packaging, alpha to numbers
611         string(COMPARE EQUAL "${BLENDER_VERSION_CHAR}" "" _out_version_char_empty)
612         if(${_out_version_char_empty})
613                 set(BLENDER_VERSION_CHAR_INDEX "0")
614         else()
615                 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)
616                 list(FIND _char_ls ${BLENDER_VERSION_CHAR} _out_version_char_index)
617                 math(EXPR BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index} + 1")
618                 unset(_char_ls)
619                 unset(_out_version_char_index)
620         endif()
621
622         unset(_out_subversion)
623         unset(_out_version_char)
624         unset(_out_version_char_empty)
625         unset(_out_version_cycle)
626
627         # message(STATUS "Version (Internal): ${BLENDER_VERSION}.${BLENDER_SUBVERSION}, Version (external): ${BLENDER_VERSION}${BLENDER_VERSION_CHAR}-${BLENDER_VERSION_CYCLE}")
628 endmacro()
629
630
631 # hacks to override initial project settings
632 # these macros must be called directly before/after project(Blender)
633 macro(blender_project_hack_pre)
634         # ----------------
635         # MINGW HACK START
636         # ignore system set flag, use our own
637         # must be before project(...)
638         # if the user wants to add their own its ok after first run.
639         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
640                 set(_reset_standard_libraries OFF)
641         else()
642                 set(_reset_standard_libraries ON)
643         endif()
644
645         # ------------------
646         # GCC -O3 HACK START
647         # needed because O3 can cause problems but
648         # allow the builder to set O3 manually after.
649         if(DEFINED CMAKE_C_FLAGS_RELEASE)
650                 set(_reset_standard_cflags_rel OFF)
651         else()
652                 set(_reset_standard_cflags_rel ON)
653         endif()
654         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
655                 set(_reset_standard_cxxflags_rel OFF)
656         else()
657                 set(_reset_standard_cxxflags_rel ON)
658         endif()
659 endmacro()
660
661
662 macro(blender_project_hack_post)
663         # --------------
664         # MINGW HACK END
665         if (_reset_standard_libraries)
666                 # Must come after projecINCt(...)
667                 #
668                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
669                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
670                 # with a meaningless stack trace. by overriding this flag we ensure we only
671                 # have libs we define and that cmake & scons builds match.
672                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
673                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
674                 mark_as_advanced(
675                         CMAKE_C_STANDARD_LIBRARIES
676                         CMAKE_CXX_STANDARD_LIBRARIES
677                 )
678         endif()
679         unset(_reset_standard_libraries)
680
681
682         # ----------------
683         # GCC -O3 HACK END
684         if(_reset_standard_cflags_rel)
685                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
686                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
687                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
688         endif()
689
690         if(_reset_standard_cxxflags_rel)
691                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
692                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
693                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
694         endif()
695
696         unset(_reset_standard_cflags_rel)
697         unset(_reset_standard_cxxflags_rel)
698
699         # ------------------------------------------------------------------
700         # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
701         if(CMAKE_COMPILER_IS_GNUCC)
702                 if(NOT DARWIN)
703                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
704                 endif()
705         endif()
706
707 endmacro()
708
709 # pair of macros to allow libraries to be specify files to install, but to
710 # only install them at the end so the directories don't get cleared with
711 # the files in them. used by cycles to install addon.
712 macro(delayed_install
713         base
714         files
715         destination)
716
717         foreach(f ${files})
718                 if(IS_ABSOLUTE ${f})
719                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${f})
720                 else()
721                         set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_FILES ${base}/${f})
722                 endif()
723                 set_property(GLOBAL APPEND PROPERTY DELAYED_INSTALL_DESTINATIONS ${destination})
724         endforeach()
725 endmacro()
726
727 # note this is a function instead of a macro so that ${BUILD_TYPE} in targetdir
728 # does not get expanded in calling but is preserved
729 function(delayed_do_install
730         targetdir)
731
732         get_property(files GLOBAL PROPERTY DELAYED_INSTALL_FILES)
733         get_property(destinations GLOBAL PROPERTY DELAYED_INSTALL_DESTINATIONS)
734
735         if(files)
736                 list(LENGTH files n)
737                 math(EXPR n "${n}-1")
738
739                 foreach(i RANGE ${n})
740                         list(GET files ${i} f)
741                         list(GET destinations ${i} d)
742                         install(FILES ${f} DESTINATION ${targetdir}/${d})
743                 endforeach()
744         endif()
745 endfunction()
746
747 macro(set_lib_path
748                 lvar
749                 lproj)
750
751         if(MSVC10)
752                 set(${lvar} ${LIBDIR}/${lproj}/vc2010)
753         else()
754                 set(${lvar} ${LIBDIR}/${lproj})
755         endif()
756 endmacro()
757
758
759 macro(data_to_c
760       file_from file_to
761       list_to_add)
762
763         list(APPEND ${list_to_add} ${file_to})
764
765         get_filename_component(_file_to_path ${file_to} PATH)
766
767         add_custom_command(
768                 OUTPUT ${file_to}
769                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
770                 COMMAND ${CMAKE_BINARY_DIR}/bin/${CMAKE_CFG_INTDIR}/datatoc ${file_from} ${file_to}
771                 DEPENDS ${file_from} datatoc)
772         unset(_file_to_path)
773 endmacro()
774
775
776 # same as above but generates the var name and output automatic.
777 macro(data_to_c_simple
778       file_from
779       list_to_add)
780
781         # remove ../'s
782         get_filename_component(_file_from ${CMAKE_CURRENT_SOURCE_DIR}/${file_from}   REALPATH)
783         get_filename_component(_file_to   ${CMAKE_CURRENT_BINARY_DIR}/${file_from}.c REALPATH)
784
785         list(APPEND ${list_to_add} ${_file_to})
786
787         get_filename_component(_file_to_path ${_file_to} PATH)
788
789         add_custom_command(
790                 OUTPUT  ${_file_to}
791                 COMMAND ${CMAKE_COMMAND} -E make_directory ${_file_to_path}
792                 COMMAND ${CMAKE_BINARY_DIR}/bin/${CMAKE_CFG_INTDIR}/datatoc ${_file_from} ${_file_to}
793                 DEPENDS ${_file_from} datatoc)
794
795         unset(_file_from)
796         unset(_file_to)
797         unset(_file_to_path)
798 endmacro()