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