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