e21d949135474b7fadbc77a5e0c2ec904fa30395
[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 # only MSVC uses SOURCE_GROUP
17 macro(blenderlib_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(blenderlib_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(blenderlib
51         name
52         sources
53         includes)
54
55         blenderlib_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)
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_LCMS)
89                 link_directories(${LCMS_LIBPATH})
90         endif()
91         if(WITH_CODEC_QUICKTIME)
92                 link_directories(${QUICKTIME_LIBPATH})
93         endif()
94         if(WITH_OPENAL)
95                 link_directories(${OPENAL_LIBPATH})
96         endif()
97         if(WITH_JACK)
98                 link_directories(${JACK_LIBPATH})
99         endif()
100         if(WITH_CODEC_SNDFILE)
101                 link_directories(${SNDFILE_LIBPATH})
102         endif()
103         if(WITH_SAMPLERATE)
104                 link_directories(${LIBSAMPLERATE_LIBPATH})
105         endif()
106         if(WITH_FFTW3)
107                 link_directories(${FFTW3_LIBPATH})
108         endif()
109         if(WITH_OPENCOLLADA)
110                 link_directories(${OPENCOLLADA_LIBPATH})
111                 link_directories(${PCRE_LIBPATH})
112                 link_directories(${EXPAT_LIBPATH})
113         endif()
114
115         if(WIN32 AND NOT UNIX)
116                 link_directories(${PTHREADS_LIBPATH})
117         endif()
118 endmacro()
119
120 macro(setup_liblinks
121         target)
122         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS} ")
123
124         target_link_libraries(${target} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} ${JPEG_LIBRARY} ${PNG_LIBRARIES} ${ZLIB_LIBRARIES} ${LLIBS})
125
126         # since we are using the local libs for python when compiling msvc projects, we need to add _d when compiling debug versions
127         if(WITH_PYTHON)
128                 target_link_libraries(${target} ${PYTHON_LINKFLAGS})
129
130                 if(WIN32 AND NOT UNIX)
131                         target_link_libraries(${target} debug ${PYTHON_LIB}_d)
132                         target_link_libraries(${target} optimized ${PYTHON_LIB})
133                 else()
134                         target_link_libraries(${target} ${PYTHON_LIB})
135                 endif()
136         endif()
137
138         target_link_libraries(${target} ${OPENGL_glu_LIBRARY} ${JPEG_LIB} ${PNG_LIB} ${ZLIB_LIB})
139         target_link_libraries(${target} ${FREETYPE_LIBRARY})
140
141         if(WITH_INTERNATIONAL)
142                 target_link_libraries(${target} ${GETTEXT_LIB})
143
144                 if(WIN32 AND NOT UNIX)
145                         target_link_libraries(${target} ${ICONV_LIB})
146                 endif()
147         endif()
148
149         if(WITH_OPENAL)
150                 target_link_libraries(${target} ${OPENAL_LIBRARY})
151         endif()
152         if(WITH_FFTW3)
153                 target_link_libraries(${target} ${FFTW3_LIB})
154         endif()
155         if(WITH_JACK)
156                 target_link_libraries(${target} ${JACK_LIB})
157         endif()
158         if(WITH_CODEC_SNDFILE)
159                 target_link_libraries(${target} ${SNDFILE_LIB})
160         endif()
161         if(WITH_SAMPLERATE)
162                 target_link_libraries(${target} ${LIBSAMPLERATE_LIB})
163         endif()
164         if(WITH_SDL)
165                 target_link_libraries(${target} ${SDL_LIBRARY})
166         endif()
167         if(WITH_CODEC_QUICKTIME)
168                 target_link_libraries(${target} ${QUICKTIME_LIB})
169         endif()
170         if(WITH_IMAGE_TIFF)
171                 target_link_libraries(${target} ${TIFF_LIBRARY})
172         endif()
173         if(WITH_IMAGE_OPENEXR)
174                 if(WIN32 AND NOT UNIX)
175                         foreach(loop_var ${OPENEXR_LIB})
176                                 target_link_libraries(${target} debug ${loop_var}_d)
177                                 target_link_libraries(${target} optimized ${loop_var})
178                         endforeach()
179                 else()
180                         target_link_libraries(${target} ${OPENEXR_LIB})
181                 endif()
182         endif()
183         if(WITH_LCMS)
184                 target_link_libraries(${target} ${LCMS_LIBRARY})
185         endif()
186         if(WITH_CODEC_FFMPEG)
187                 target_link_libraries(${target} ${FFMPEG_LIB})
188         endif()
189         if(WITH_OPENCOLLADA)
190                 if(WIN32 AND NOT UNIX)
191                         foreach(loop_var ${OPENCOLLADA_LIB})
192                                 target_link_libraries(${target} debug ${loop_var}_d)
193                                 target_link_libraries(${target} optimized ${loop_var})
194                         endforeach()
195                         target_link_libraries(${target} debug ${PCRE_LIB}_d)
196                         target_link_libraries(${target} optimized ${PCRE_LIB})
197                         if(EXPAT_LIB)
198                                 target_link_libraries(${target} debug ${EXPAT_LIB}_d)
199                                 target_link_libraries(${target} optimized ${EXPAT_LIB})
200                         endif()
201                 else()
202                         target_link_libraries(${target} ${OPENCOLLADA_LIB})
203                         target_link_libraries(${target} ${PCRE_LIB})
204                         target_link_libraries(${target} ${EXPAT_LIB})
205                 endif()
206         endif()
207         if(WITH_LCMS)
208                 if(WIN32 AND NOT UNIX)
209                         target_link_libraries(${target} debug ${LCMS_LIB}_d)
210                         target_link_libraries(${target} optimized ${LCMS_LIB})
211                 endif()
212         endif()
213         if(WIN32 AND NOT UNIX)
214                 target_link_libraries(${target} ${PTHREADS_LIB})
215         endif()
216 endmacro()
217
218 macro(TEST_SSE_SUPPORT)
219         include(CheckCSourceRuns)
220
221         # message(STATUS "Detecting SSE support")
222         if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
223                 set(CMAKE_REQUIRED_FLAGS "-msse -msse2")
224         elseif(MSVC)
225                 set(CMAKE_REQUIRED_FLAGS "/arch:SSE2") # TODO, SSE 1 ?
226         endif()
227
228         if(NOT DEFINED ${SUPPORT_SSE_BUILD})
229                 check_c_source_runs("
230                         #include <xmmintrin.h>
231                         int main() { __m128 v = _mm_setzero_ps(); return 0; }"
232                 SUPPORT_SSE_BUILD)
233                 
234                 if(SUPPORT_SSE_BUILD)
235                         message(STATUS "SSE Support: detected.")
236                 else()
237                         message(STATUS "SSE Support: missing.")
238                 endif()
239                 set(${SUPPORT_SSE_BUILD} ${SUPPORT_SSE_BUILD} CACHE INTERNAL "SSE Test")
240         endif() 
241
242         if(NOT DEFINED ${SUPPORT_SSE2_BUILD})
243                 check_c_source_runs("
244                         #include <emmintrin.h>
245                         int main() { __m128d v = _mm_setzero_pd(); return 0; }"
246                 SUPPORT_SSE2_BUILD)
247
248                 if(SUPPORT_SSE2_BUILD)
249                         message(STATUS "SSE2 Support: detected.")
250                 else()
251                         message(STATUS "SSE2 Support: missing.")
252                 endif() 
253                 set(${SUPPORT_SSE2_BUILD} ${SUPPORT_SSE2_BUILD} CACHE INTERNAL "SSE2 Test")
254         endif()
255
256 endmacro()
257
258 # when we have warnings as errors applied globally this
259 # needs to be removed for some external libs which we dont maintain.
260
261 # utility macro
262 macro(_remove_strict_flags
263         flag)
264
265         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
266         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
267         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
268         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
269         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
270
271         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
272         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
273         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
274         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
275         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
276
277 endmacro()
278
279 macro(remove_strict_flags)
280
281         if(CMAKE_COMPILER_IS_GNUCC)
282                 _remove_strict_flags("-Wstrict-prototypes")
283                 _remove_strict_flags("-Wunused-parameter")
284                 _remove_strict_flags("-Wwrite-strings")
285                 _remove_strict_flags("-Wshadow")
286                 _remove_strict_flags("-Werror=[^ ]+")
287                 _remove_strict_flags("-Werror")
288         endif()
289
290         if(MSVC)
291                 # TODO
292         endif()
293
294 endmacro()
295
296
297 # XXX, until cmake 2.8.4 is released.
298 INCLUDE(CheckCSourceCompiles)
299 MACRO (CHECK_C_COMPILER_FLAG__INTERNAL _FLAG _RESULT)
300    SET(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}")
301    SET(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}")
302    CHECK_C_SOURCE_COMPILES("int main(void) { return 0;}" ${_RESULT}
303      # Some compilers do not fail with a bad flag
304      FAIL_REGEX "unrecognized .*option"                     # GNU
305      FAIL_REGEX "ignoring unknown option"                   # MSVC
306      FAIL_REGEX "[Uu]nknown option"                         # HP
307      FAIL_REGEX "[Ww]arning: [Oo]ption"                     # SunPro
308      FAIL_REGEX "command option .* is not recognized"       # XL
309      )
310    SET (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}")
311 ENDMACRO (CHECK_C_COMPILER_FLAG__INTERNAL)
312 # XXX, end duplicate code.
313
314 macro(ADD_CHECK_C_COMPILER_FLAG
315         _CFLAGS
316         _CACHE_VAR
317         _FLAG)
318
319         # include(CheckCCompilerFlag)
320
321         CHECK_C_COMPILER_FLAG__INTERNAL("${_FLAG}" "${_CACHE_VAR}")
322         if(${_CACHE_VAR})
323                 # message(STATUS "Using CFLAG: ${_FLAG}")
324                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}")
325         else()
326                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
327         endif()
328 endmacro()
329
330 macro(ADD_CHECK_CXX_COMPILER_FLAG
331         _CXXFLAGS
332         _CACHE_VAR
333         _FLAG)
334
335         include(CheckCXXCompilerFlag)
336
337         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
338         if(${_CACHE_VAR})
339                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
340                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}")
341         else()
342                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
343         endif()
344 endmacro()
345
346 macro(get_blender_version)
347         file(READ ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h CONTENT)
348         string(REGEX REPLACE "\n" ";" CONTENT "${CONTENT}")
349         string(REGEX REPLACE "\t" ";" CONTENT "${CONTENT}")
350         string(REGEX REPLACE " " ";" CONTENT "${CONTENT}")
351
352         foreach(ITEM ${CONTENT})
353                 if(LASTITEM MATCHES "BLENDER_VERSION")
354                         MATH(EXPR BLENDER_VERSION_MAJOR "${ITEM} / 100")
355                         MATH(EXPR BLENDER_VERSION_MINOR "${ITEM} % 100")
356                         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
357                 endif()
358
359                 if(LASTITEM MATCHES "BLENDER_SUBVERSION")
360                         set(BLENDER_SUBVERSION ${ITEM})
361                 endif()
362
363                 if(LASTITEM MATCHES "BLENDER_MINVERSION")
364                         MATH(EXPR BLENDER_MINVERSION_MAJOR "${ITEM} / 100")
365                         MATH(EXPR BLENDER_MINVERSION_MINOR "${ITEM} % 100")
366                         set(BLENDER_MINVERSION "${BLENDER_MINVERSION_MAJOR}.${BLENDER_MINVERSION_MINOR}")
367                 endif()
368
369                 if(LASTITEM MATCHES "BLENDER_MINSUBVERSION")
370                         set(BLENDER_MINSUBVERSION ${ITEM})
371                 endif()
372
373                 set(LASTITEM ${ITEM})
374         endforeach()
375
376         # message(STATUS "Version major: ${BLENDER_VERSION_MAJOR}, Version minor: ${BLENDER_VERSION_MINOR}, Subversion: ${BLENDER_SUBVERSION}, Version: ${BLENDER_VERSION}")
377         # message(STATUS "Minversion major: ${BLENDER_MINVERSION_MAJOR}, Minversion minor: ${BLENDER_MINVERSION_MINOR}, MinSubversion: ${BLENDER_MINSUBVERSION}, Minversion: ${BLENDER_MINVERSION}")
378 endmacro()