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