rename cmake include/libraries to conform with suggested cmake names
[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 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(${SAMPLERATE_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}
154                         ${OPENGL_gl_LIBRARY}
155                         ${OPENGL_glu_LIBRARY}
156                         ${JPEG_LIBRARIES}
157                         ${PNG_LIBRARIES}
158                         ${ZLIB_LIBRARIES}
159                         ${LLIBS})
160
161         # since we are using the local libs for python when compiling msvc projects, we need to add _d when compiling debug versions
162         if(WITH_PYTHON)  # AND NOT WITH_PYTHON_MODULE  # WIN32 needs
163                 target_link_libraries(${target} ${PYTHON_LINKFLAGS})
164
165                 if(WIN32 AND NOT UNIX)
166                         target_link_libraries(${target}
167                                         debug ${PYTHON_LIBRARY}_d
168                                         optimized ${PYTHON_LIBRARY})
169                 else()
170                         target_link_libraries(${target} ${PYTHON_LIBRARY})
171                 endif()
172         endif()
173
174         if(NOT WITH_BUILTIN_GLEW)
175                 target_link_libraries(${target} ${GLEW_LIBRARY})
176         endif()
177
178         target_link_libraries(${target}
179                         ${OPENGL_glu_LIBRARY}
180                         ${JPEG_LIBRARIES}
181                         ${PNG_LIBRARIES}
182                         ${ZLIB_LIBRARIES}
183                         ${FREETYPE_LIBRARY})
184
185         if(WITH_INTERNATIONAL)
186                 target_link_libraries(${target} ${GETTEXT_LIB})
187
188                 if(WIN32 AND NOT UNIX)
189                         target_link_libraries(${target} ${ICONV_LIBRARIES})
190                 endif()
191         endif()
192
193         if(WITH_OPENAL)
194                 target_link_libraries(${target} ${OPENAL_LIBRARY})
195         endif()
196         if(WITH_FFTW3)
197                 target_link_libraries(${target} ${FFTW3_LIBRARIES})
198         endif()
199         if(WITH_JACK)
200                 target_link_libraries(${target} ${JACK_LIBRARIES})
201         endif()
202         if(WITH_CODEC_SNDFILE)
203                 target_link_libraries(${target} ${SNDFILE_LIBRARIES})
204         endif()
205         if(WITH_SAMPLERATE)
206                 target_link_libraries(${target} ${SAMPLERATE_LIBRARIES})
207         endif()
208         if(WITH_SDL)
209                 target_link_libraries(${target} ${SDL_LIBRARY})
210         endif()
211         if(WITH_CODEC_QUICKTIME)
212                 target_link_libraries(${target} ${QUICKTIME_LIBRARIES})
213         endif()
214         if(WITH_IMAGE_TIFF)
215                 target_link_libraries(${target} ${TIFF_LIBRARY})
216         endif()
217         if(WITH_IMAGE_OPENEXR)
218                 if(WIN32 AND NOT UNIX)
219                         foreach(_LOOP_VAR ${OPENEXR_LIBRARIES})
220                                 string(REGEX REPLACE ".lib$" "_d.lib" _LOOP_VAR_DEBUG ${_LOOP_VAR})
221                                 target_link_libraries(${target}
222                                                 debug ${_LOOP_VAR_DEBUG}
223                                                 optimized ${_LOOP_VAR})
224                         endforeach()
225                         unset(_LOOP_VAR)
226                         unset(_LOOP_VAR_DEBUG)
227                 else()
228                         target_link_libraries(${target} ${OPENEXR_LIBRARIES})
229                 endif()
230         endif()
231         if(WITH_IMAGE_OPENJPEG AND UNIX AND NOT APPLE)
232                 target_link_libraries(${target} ${OPENJPEG_LIB})
233         endif()
234         if(WITH_CODEC_FFMPEG)
235                 target_link_libraries(${target} ${FFMPEG_LIBRARIES})
236         endif()
237         if(WITH_OPENCOLLADA)
238                 if(WIN32 AND NOT UNIX)
239                         foreach(_LOOP_VAR ${OPENCOLLADA_LIBRARIES})
240                                 target_link_libraries(${target}
241                                                 debug ${_LOOP_VAR}_d
242                                                 optimized ${_LOOP_VAR})
243                         endforeach()
244                         unset(_LOOP_VAR)
245                         target_link_libraries(${target}
246                                         debug ${PCRE_LIB}_d
247                                         optimized ${PCRE_LIB})
248                         if(EXPAT_LIB)
249                                 target_link_libraries(${target}
250                                                 debug ${EXPAT_LIB}_d
251                                                 optimized ${EXPAT_LIB})
252                         endif()
253                 else()
254                         target_link_libraries(${target}
255                                         ${OPENCOLLADA_LIBRARIES}
256                                         ${PCRE_LIB}
257                                         ${EXPAT_LIB})
258                 endif()
259         endif()
260         if(WITH_MEM_JEMALLOC)
261                 target_link_libraries(${target} ${JEMALLOC_LIBRARIES})
262         endif()
263         if(WIN32 AND NOT UNIX)
264                 target_link_libraries(${target} ${PTHREADS_LIBRARIES})
265         endif()
266 endmacro()
267
268 macro(TEST_SSE_SUPPORT)
269         include(CheckCSourceRuns)
270
271         # message(STATUS "Detecting SSE support")
272         if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
273                 set(CMAKE_REQUIRED_FLAGS "-msse -msse2")
274         elseif(MSVC)
275                 set(CMAKE_REQUIRED_FLAGS "/arch:SSE2") # TODO, SSE 1 ?
276         endif()
277
278         if(NOT DEFINED ${SUPPORT_SSE_BUILD})
279                 check_c_source_runs("
280                         #include <xmmintrin.h>
281                         int main() { __m128 v = _mm_setzero_ps(); return 0; }"
282                 SUPPORT_SSE_BUILD)
283                 
284                 if(SUPPORT_SSE_BUILD)
285                         message(STATUS "SSE Support: detected.")
286                 else()
287                         message(STATUS "SSE Support: missing.")
288                 endif()
289                 set(${SUPPORT_SSE_BUILD} ${SUPPORT_SSE_BUILD} CACHE INTERNAL "SSE Test")
290         endif() 
291
292         if(NOT DEFINED ${SUPPORT_SSE2_BUILD})
293                 check_c_source_runs("
294                         #include <emmintrin.h>
295                         int main() { __m128d v = _mm_setzero_pd(); return 0; }"
296                 SUPPORT_SSE2_BUILD)
297
298                 if(SUPPORT_SSE2_BUILD)
299                         message(STATUS "SSE2 Support: detected.")
300                 else()
301                         message(STATUS "SSE2 Support: missing.")
302                 endif() 
303                 set(${SUPPORT_SSE2_BUILD} ${SUPPORT_SSE2_BUILD} CACHE INTERNAL "SSE2 Test")
304         endif()
305
306 endmacro()
307
308 # when we have warnings as errors applied globally this
309 # needs to be removed for some external libs which we dont maintain.
310
311 # utility macro
312 macro(remove_flag
313         flag)
314
315         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
316         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
317         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
318         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
319         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
320
321         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
322         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
323         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
324         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
325         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
326
327 endmacro()
328
329 macro(remove_strict_flags)
330
331         if(CMAKE_COMPILER_IS_GNUCC)
332                 remove_flag("-Wstrict-prototypes")
333                 remove_flag("-Wunused-parameter")
334                 remove_flag("-Wwrite-strings")
335                 remove_flag("-Wshadow")
336                 remove_flag("-Werror=[^ ]+")
337                 remove_flag("-Werror")
338         endif()
339
340         if(MSVC)
341                 # TODO
342         endif()
343
344 endmacro()
345
346 macro(ADD_CHECK_C_COMPILER_FLAG
347         _CFLAGS
348         _CACHE_VAR
349         _FLAG)
350
351         include(CheckCCompilerFlag)
352
353         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
354         if(${_CACHE_VAR})
355                 # message(STATUS "Using CFLAG: ${_FLAG}")
356                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}")
357         else()
358                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
359         endif()
360 endmacro()
361
362 macro(ADD_CHECK_CXX_COMPILER_FLAG
363         _CXXFLAGS
364         _CACHE_VAR
365         _FLAG)
366
367         include(CheckCXXCompilerFlag)
368
369         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
370         if(${_CACHE_VAR})
371                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
372                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}")
373         else()
374                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
375         endif()
376 endmacro()
377
378 macro(get_blender_version)
379         # So cmake depends on BKE_blender.h, beware of inf-loops!
380         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
381
382         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
383
384         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
385         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
386         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
387         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
388
389         if(NOT ${_out_version} MATCHES "[0-9]+")
390                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
391         endif()
392
393         if(NOT ${_out_subversion} MATCHES "[0-9]+")
394                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
395         endif()
396
397         # clumsy regex, only single char are ok but it could be unset
398
399         string(LENGTH "${_out_version_char}" _out_version_char_len)
400         if(NOT _out_version_char_len EQUAL 1)
401                 set(_out_version_char "")
402         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
403                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
404         endif()
405
406         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
407                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
408         endif()
409
410         math(EXPR BLENDER_VERSION_MAJOR "${_out_version} / 100")
411         math(EXPR BLENDER_VERSION_MINOR "${_out_version} % 100")
412         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
413
414         set(BLENDER_SUBVERSION ${_out_subversion})
415         set(BLENDER_VERSION_CHAR ${_out_version_char})
416         set(BLENDER_VERSION_CYCLE ${_out_version_cycle})
417
418         # for packaging, alpha to numbers
419         string(COMPARE EQUAL "${BLENDER_VERSION_CHAR}" "" _out_version_char_empty)
420         if(${_out_version_char_empty})
421                 set(BLENDER_VERSION_CHAR_INDEX "0")
422         else()
423                 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)
424                 list(FIND _char_ls ${BLENDER_VERSION_CHAR} _out_version_char_index)
425                 math(EXPR BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index} + 1")
426                 unset(_char_ls)
427                 unset(_out_version_char_index)
428         endif()
429
430         unset(_out_subversion)
431         unset(_out_version_char)
432         unset(_out_version_char_empty)
433         unset(_out_version_cycle)
434
435         # message(STATUS "Version (Internal): ${BLENDER_VERSION}.${BLENDER_SUBVERSION}, Version (external): ${BLENDER_VERSION}${BLENDER_VERSION_CHAR}-${BLENDER_VERSION_CYCLE}")
436 endmacro()
437
438
439 # hacks to override initial project settings
440 # these macros must be called directly before/after project(Blender) 
441 macro(blender_project_hack_pre)
442         # ----------------
443         # MINGW HACK START
444         # ignore system set flag, use our own
445         # must be before project(...)
446         # if the user wants to add their own its ok after first run.
447         if(DEFINED CMAKE_C_STANDARD_LIBRARIES)
448                 set(_reset_standard_libraries OFF)
449         else()
450                 set(_reset_standard_libraries ON)
451         endif()
452
453         # ------------------
454         # GCC -O3 HACK START
455         # needed because O3 can cause problems but
456         # allow the builder to set O3 manually after.
457         if(DEFINED CMAKE_C_FLAGS_RELEASE)
458                 set(_reset_standard_cflags_rel OFF)
459         else()
460                 set(_reset_standard_cflags_rel ON)
461         endif()
462         if(DEFINED CMAKE_CXX_FLAGS_RELEASE)
463                 set(_reset_standard_cxxflags_rel OFF)
464         else()
465                 set(_reset_standard_cxxflags_rel ON)
466         endif()
467 endmacro()
468
469
470 macro(blender_project_hack_post)
471         # --------------
472         # MINGW HACK END
473         if (_reset_standard_libraries)
474                 # Must come after project(...)
475                 #
476                 # MINGW workaround for -ladvapi32 being included which surprisingly causes
477                 # string formatting of floats, eg: printf("%.*f", 3, value). to crash blender
478                 # with a meaningless stack trace. by overriding this flag we ensure we only
479                 # have libs we define and that cmake & scons builds match.
480                 set(CMAKE_C_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
481                 set(CMAKE_CXX_STANDARD_LIBRARIES "" CACHE STRING "" FORCE)
482                 mark_as_advanced(CMAKE_C_STANDARD_LIBRARIES)
483                 mark_as_advanced(CMAKE_CXX_STANDARD_LIBRARIES)
484         endif()
485         unset(_reset_standard_libraries)
486
487
488         # ----------------
489         # GCC -O3 HACK END
490         if(_reset_standard_cflags_rel)
491                 string(REGEX REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
492                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}" CACHE STRING "" FORCE)
493                 mark_as_advanced(CMAKE_C_FLAGS_RELEASE)
494         endif()
495
496         if(_reset_standard_cxxflags_rel)
497                 string(REGEX REPLACE "-O3" "-O2" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
498                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
499                 mark_as_advanced(CMAKE_CXX_FLAGS_RELEASE)
500         endif()
501
502         unset(_reset_standard_cflags_rel)
503         unset(_reset_standard_cxxflags_rel)
504
505         # ------------------------------------------------------------------
506         # workaround for omission in cmake 2.8.4's GNU.cmake, fixed in 2.8.5
507         if(CMAKE_COMPILER_IS_GNUCC)
508                 if(NOT DARWIN)
509                         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
510                 endif()
511         endif()
512
513 endmacro()