merge from trunk r37405
[blender.git] / build_files / cmake / macros.cmake
1 # -*- mode: cmake; indent-tabs-mode: t; -*-
2 # $Id$
3
4 # Nicer makefiles with -I/1/foo/ instead of -I/1/2/3/../../foo/
5 # use it instead of include_directories()
6 macro(blender_include_dirs
7         includes)
8         set(_ALL_INCS "")
9         foreach(_INC ${ARGV})
10                 get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
11                 list(APPEND _ALL_INCS ${_ABS_INC})
12         endforeach()
13         include_directories(${_ALL_INCS})
14         unset(_INC)
15         unset(_ABS_INC)
16         unset(_ALL_INCS)
17 endmacro()
18
19         set(_ALL_INCS "")
20         foreach(_INC ${ARGV})
21                 get_filename_component(_ABS_INC ${_INC} ABSOLUTE)
22                 list(APPEND _ALL_INCS ${_ABS_INC})
23         include_directories(SYSTEM ${_ALL_INCS})
24         unset(_INC)
25         unset(_ABS_INC)
26         unset(_ALL_INCS)
27
28 macro(blender_source_group
29         sources)
30
31         # Group by location on disk
32         source_group("Source Files" FILES CMakeLists.txt)
33
34         foreach(_SRC ${sources})
35                 get_filename_component(_SRC_EXT ${_SRC} EXT)
36                 if(${_SRC_EXT} MATCHES ".h" OR ${_SRC_EXT} MATCHES ".hpp")
37                         source_group("Header Files" FILES ${_SRC})
38                 else()
39                         source_group("Source Files" FILES ${_SRC})
40                 endif()
41         endforeach()
42
43         unset(_SRC)
44         unset(_SRC_EXT)
45 endmacro()
46
47
48 # only MSVC uses SOURCE_GROUP
49 macro(blender_add_lib_nolist
50         name
51         sources
52         includes)
53
54         # message(STATUS "Configuring library ${name}")
55
56         # include_directories(${includes})
57         blender_include_dirs("${includes}")
58
59         add_library(${name} ${sources})
60
61         # works fine without having the includes
62         # listed is helpful for IDE's (QtCreator/MSVC)
63         blender_source_group("${sources}")
64
65 endmacro()
66
67
68 macro(blender_add_lib
69         name
70         sources
71         includes)
72
73         blender_add_lib_nolist(${name} "${sources}" "${includes}")
74
75         set_property(GLOBAL APPEND PROPERTY BLENDER_LINK_LIBS ${name})
76 endmacro()
77
78
79 macro(SETUP_LIBDIRS)
80         # see "cmake --help-policy CMP0003"
81         if(COMMAND cmake_policy)
82                 cmake_policy(SET CMP0003 NEW)
83         endif()
84
85         link_directories(${JPEG_LIBPATH} ${PNG_LIBPATH} ${ZLIB_LIBPATH} ${FREETYPE_LIBPATH})
86
87         if(WITH_PYTHON)  #  AND NOT WITH_PYTHON_MODULE  # WIN32 needs
88                 link_directories(${PYTHON_LIBPATH})
89         endif()
90         if(WITH_INTERNATIONAL)
91                 link_directories(${ICONV_LIBPATH})
92                 link_directories(${GETTEXT_LIBPATH})
93         endif()
94         if(WITH_SDL)
95                 link_directories(${SDL_LIBPATH})
96         endif()
97         if(WITH_CODEC_FFMPEG)
98                 link_directories(${FFMPEG_LIBPATH})
99         endif()
100         if(WITH_IMAGE_OPENEXR)
101                 link_directories(${OPENEXR_LIBPATH})
102         endif()
103         if(WITH_IMAGE_TIFF)
104                 link_directories(${TIFF_LIBPATH})
105         endif()
106         if(WITH_IMAGE_OPENJPEG AND UNIX AND NOT APPLE)
107                 link_directories(${OPENJPEG_LIBPATH})
108         endif()
109         if(WITH_CODEC_QUICKTIME)
110                 link_directories(${QUICKTIME_LIBPATH})
111         endif()
112         if(WITH_OPENAL)
113                 link_directories(${OPENAL_LIBPATH})
114         endif()
115         if(WITH_JACK)
116                 link_directories(${JACK_LIBPATH})
117         endif()
118         if(WITH_CODEC_SNDFILE)
119                 link_directories(${SNDFILE_LIBPATH})
120         endif()
121         if(WITH_SAMPLERATE)
122                 link_directories(${LIBSAMPLERATE_LIBPATH})
123         endif()
124         if(WITH_FFTW3)
125                 link_directories(${FFTW3_LIBPATH})
126         endif()
127         if(WITH_OPENCOLLADA)
128                 link_directories(${OPENCOLLADA_LIBPATH})
129                 link_directories(${PCRE_LIBPATH})
130                 link_directories(${EXPAT_LIBPATH})
131         endif()
132         if(WITH_MEM_JEMALLOC)
133                 link_directories(${JEMALLOC_LIBPATH})
134         endif()
135
136         if(WIN32 AND NOT UNIX)
137                 link_directories(${PTHREADS_LIBPATH})
138         endif()
139 endmacro()
140
141 macro(setup_liblinks
142         target)
143         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS} ")
144
145         target_link_libraries(${target} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} ${JPEG_LIBRARIES} ${PNG_LIBRARIES} ${ZLIB_LIBRARIES} ${LLIBS})
146
147         # since we are using the local libs for python when compiling msvc projects, we need to add _d when compiling debug versions
148         if(WITH_PYTHON)  # AND NOT WITH_PYTHON_MODULE  # WIN32 needs
149                 target_link_libraries(${target} ${PYTHON_LINKFLAGS})
150
151                 if(WIN32 AND NOT UNIX)
152                         target_link_libraries(${target} debug ${PYTHON_LIBRARY}_d)
153                         target_link_libraries(${target} optimized ${PYTHON_LIBRARY})
154                 else()
155                         target_link_libraries(${target} ${PYTHON_LIBRARY})
156                 endif()
157         endif()
158
159         if(NOT WITH_BUILTIN_GLEW)
160                 target_link_libraries(${target} ${GLEW_LIBRARY})
161         endif()
162
163         target_link_libraries(${target} ${OPENGL_glu_LIBRARY} ${JPEG_LIBRARIES} ${PNG_LIBRARIES} ${ZLIB_LIBRARIES})
164         target_link_libraries(${target} ${FREETYPE_LIBRARY})
165
166         if(WITH_INTERNATIONAL)
167                 target_link_libraries(${target} ${GETTEXT_LIB})
168
169                 if(WIN32 AND NOT UNIX)
170                         target_link_libraries(${target} ${ICONV_LIB})
171                 endif()
172         endif()
173
174         if(WITH_OPENAL)
175                 target_link_libraries(${target} ${OPENAL_LIBRARY})
176         endif()
177         if(WITH_FFTW3)
178                 target_link_libraries(${target} ${FFTW3_LIB})
179         endif()
180         if(WITH_JACK)
181                 target_link_libraries(${target} ${JACK_LIB})
182         endif()
183         if(WITH_CODEC_SNDFILE)
184                 target_link_libraries(${target} ${SNDFILE_LIB})
185         endif()
186         if(WITH_SAMPLERATE)
187                 target_link_libraries(${target} ${LIBSAMPLERATE_LIB})
188         endif()
189         if(WITH_SDL)
190                 target_link_libraries(${target} ${SDL_LIBRARY})
191         endif()
192         if(WITH_CODEC_QUICKTIME)
193                 target_link_libraries(${target} ${QUICKTIME_LIB})
194         endif()
195         if(WITH_IMAGE_TIFF)
196                 target_link_libraries(${target} ${TIFF_LIBRARY})
197         endif()
198         if(WITH_IMAGE_OPENEXR)
199                 if(WIN32 AND NOT UNIX)
200                         foreach(_LOOP_VAR ${OPENEXR_LIB})
201                                 target_link_libraries(${target} debug ${_LOOP_VAR}_d)
202                                 target_link_libraries(${target} optimized ${_LOOP_VAR})
203                         endforeach()
204                         unset(_LOOP_VAR)
205                 else()
206                         target_link_libraries(${target} ${OPENEXR_LIB})
207                 endif()
208         endif()
209         if(WITH_IMAGE_OPENJPEG AND UNIX AND NOT APPLE)
210                 target_link_libraries(${target} ${OPENJPEG_LIB})
211         endif()
212         if(WITH_CODEC_FFMPEG)
213                 target_link_libraries(${target} ${FFMPEG_LIB})
214         endif()
215         if(WITH_OPENCOLLADA)
216                 if(WIN32 AND NOT UNIX)
217                         foreach(_LOOP_VAR ${OPENCOLLADA_LIB})
218                                 target_link_libraries(${target} debug ${_LOOP_VAR}_d)
219                                 target_link_libraries(${target} optimized ${_LOOP_VAR})
220                         endforeach()
221                         unset(_LOOP_VAR)
222                         target_link_libraries(${target} debug ${PCRE_LIB}_d)
223                         target_link_libraries(${target} optimized ${PCRE_LIB})
224                         if(EXPAT_LIB)
225                                 target_link_libraries(${target} debug ${EXPAT_LIB}_d)
226                                 target_link_libraries(${target} optimized ${EXPAT_LIB})
227                         endif()
228                 else()
229                         target_link_libraries(${target} ${OPENCOLLADA_LIB})
230                         target_link_libraries(${target} ${PCRE_LIB})
231                         target_link_libraries(${target} ${EXPAT_LIB})
232                 endif()
233         endif()
234         if(WITH_MEM_JEMALLOC)
235                 target_link_libraries(${target} ${JEMALLOC_LIBRARY})
236         endif()
237         if(WIN32 AND NOT UNIX)
238                 target_link_libraries(${target} ${PTHREADS_LIB})
239         endif()
240 endmacro()
241
242 macro(TEST_SSE_SUPPORT)
243         include(CheckCSourceRuns)
244
245         # message(STATUS "Detecting SSE support")
246         if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
247                 set(CMAKE_REQUIRED_FLAGS "-msse -msse2")
248         elseif(MSVC)
249                 set(CMAKE_REQUIRED_FLAGS "/arch:SSE2") # TODO, SSE 1 ?
250         endif()
251
252         if(NOT DEFINED ${SUPPORT_SSE_BUILD})
253                 check_c_source_runs("
254                         #include <xmmintrin.h>
255                         int main() { __m128 v = _mm_setzero_ps(); return 0; }"
256                 SUPPORT_SSE_BUILD)
257                 
258                 if(SUPPORT_SSE_BUILD)
259                         message(STATUS "SSE Support: detected.")
260                 else()
261                         message(STATUS "SSE Support: missing.")
262                 endif()
263                 set(${SUPPORT_SSE_BUILD} ${SUPPORT_SSE_BUILD} CACHE INTERNAL "SSE Test")
264         endif() 
265
266         if(NOT DEFINED ${SUPPORT_SSE2_BUILD})
267                 check_c_source_runs("
268                         #include <emmintrin.h>
269                         int main() { __m128d v = _mm_setzero_pd(); return 0; }"
270                 SUPPORT_SSE2_BUILD)
271
272                 if(SUPPORT_SSE2_BUILD)
273                         message(STATUS "SSE2 Support: detected.")
274                 else()
275                         message(STATUS "SSE2 Support: missing.")
276                 endif() 
277                 set(${SUPPORT_SSE2_BUILD} ${SUPPORT_SSE2_BUILD} CACHE INTERNAL "SSE2 Test")
278         endif()
279
280 endmacro()
281
282 # when we have warnings as errors applied globally this
283 # needs to be removed for some external libs which we dont maintain.
284
285 # utility macro
286 macro(remove_flag
287         flag)
288
289         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
290         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
291         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
292         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
293         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
294
295         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
296         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
297         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
298         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
299         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
300
301 endmacro()
302
303 macro(remove_strict_flags)
304
305         if(CMAKE_COMPILER_IS_GNUCC)
306                 remove_flag("-Wstrict-prototypes")
307                 remove_flag("-Wunused-parameter")
308                 remove_flag("-Wwrite-strings")
309                 remove_flag("-Wshadow")
310                 remove_flag("-Werror=[^ ]+")
311                 remove_flag("-Werror")
312         endif()
313
314         if(MSVC)
315                 # TODO
316         endif()
317
318 endmacro()
319
320 macro(ADD_CHECK_C_COMPILER_FLAG
321         _CFLAGS
322         _CACHE_VAR
323         _FLAG)
324
325         include(CheckCCompilerFlag)
326
327         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
328         if(${_CACHE_VAR})
329                 # message(STATUS "Using CFLAG: ${_FLAG}")
330                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}")
331         else()
332                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
333         endif()
334 endmacro()
335
336 macro(ADD_CHECK_CXX_COMPILER_FLAG
337         _CXXFLAGS
338         _CACHE_VAR
339         _FLAG)
340
341         include(CheckCXXCompilerFlag)
342
343         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
344         if(${_CACHE_VAR})
345                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
346                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}")
347         else()
348                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
349         endif()
350 endmacro()
351
352 macro(get_blender_version)
353         # So cmake depends on BKE_blender.h, beware of inf-loops!
354         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
355
356         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
357
358         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
359         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
360         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
361         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
362
363         if(NOT ${_out_version} MATCHES "[0-9]+")
364                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
365         endif()
366
367         if(NOT ${_out_subversion} MATCHES "[0-9]+")
368                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
369         endif()
370
371         # clumsy regex, only single char are ok but it could be unset
372
373         string(LENGTH "${_out_version_char}" _out_version_char_len)
374         if(NOT _out_version_char_len EQUAL 1)
375                 set(_out_version_char "")
376         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
377                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
378         endif()
379
380         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
381                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
382         endif()
383
384         math(EXPR BLENDER_VERSION_MAJOR "${_out_version} / 100")
385         math(EXPR BLENDER_VERSION_MINOR "${_out_version} % 100")
386         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
387
388         set(BLENDER_SUBVERSION ${_out_subversion})
389         set(BLENDER_VERSION_CHAR ${_out_version_char})
390         set(BLENDER_VERSION_CYCLE ${_out_version_cycle})
391
392         # for packaging, alpha to numbers
393         string(COMPARE EQUAL "${BLENDER_VERSION_CHAR}" "" _out_version_char_empty)
394         if(${_out_version_char_empty})
395                 set(BLENDER_VERSION_CHAR_INDEX "0")
396         else()
397                 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 q y z)
398                 list(FIND _char_ls ${BLENDER_VERSION_CHAR} _out_version_char_index)
399                 math(EXPR BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index} + 1")
400                 unset(_char_ls)
401                 unset(_out_version_char_index)
402         endif()
403
404         unset(_out_subversion)
405         unset(_out_version_char)
406         unset(_out_version_char_empty)
407         unset(_out_version_cycle)
408
409         # message(STATUS "Version (Internal): ${BLENDER_VERSION}.${BLENDER_SUBVERSION}, Version (external): ${BLENDER_VERSION}${BLENDER_VERSION_CHAR}-${BLENDER_VERSION_CYCLE}")
410 endmacro()
411
412
413 # hacks to override initial project settings
414 # these macros must be called directly before/after project(Blender) 
415 macro(blender_project_hack_pre)
416         # ----------------
417         # MINGW HACK START
418         # ignore system set flag, use our own
419         # must be before project(...)
420         # if the user wants to add their own its ok after first run.
421         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
422                 set(_reset_standard_libraries OFF)
423         else()
424                 set(_reset_standard_libraries ON)
425         endif()
426
427         # ------------------
428         # GCC -O3 HACK START
429         # needed because O3 can cause problems but
430         # allow the builder to set O3 manually after.
431         if(DEFINED CMAKE_C_FLAGS_RELEASE)
432                 set(_reset_standard_cflags_rel OFF)
433         else()
434                 set(_reset_standard_cflags_rel ON)
435         endif()
436         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
437                 set(_reset_standard_cxxflags_rel OFF)
438         else()
439                 set(_reset_standard_cxxflags_rel ON)
440         endif()
441 endmacro()
442
443
444 macro(blender_project_hack_post)
445         # --------------
446         # MINGW HACK END
447         if (_reset_standard_libraries)
448                 # Must come after project(...)
449                 #
450                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
451                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
452                 # with a meaningless stack trace. by overriding this flag we ensure we only
453                 # have libs we define and that cmake & scons builds match.
454                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
455                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
456                 mark_as_advanced(CMAKE_C_STANDARD_LIBRARIES)
457                 mark_as_advanced(CMAKE_CXX_STANDARD_LIBRARIES)
458         endif()
459         unset(_reset_standard_libraries)
460
461
462         # ----------------
463         # GCC -O3 HACK END
464         if(_reset_standard_cflags_rel)
465                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
466                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
467                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
468         endif()
469
470         if(_reset_standard_cxxflags_rel)
471                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
472                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
473                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
474         endif()
475
476         unset(_reset_standard_cflags_rel)
477         unset(_reset_standard_cxxflags_rel)
478
479         # --------------------------------------------------
480         # workaround for omission in cmake 2.8.4's GNU.cmake
481         if(CMAKE_COMPILER_IS_GNUCC)
482                 if(NOT DARWIN)
483                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
484                 endif()
485         endif()
486
487 endmacro()