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