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