patch [#26235] debian/watch: Update for change of versioning
[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(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)
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
118         if(WIN32 AND NOT UNIX)
119                 link_directories(${PTHREADS_LIBPATH})
120         endif()
121 endmacro()
122
123 macro(setup_liblinks
124         target)
125         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS} ")
126
127         target_link_libraries(${target} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} ${JPEG_LIBRARIES} ${PNG_LIBRARIES} ${ZLIB_LIBRARIES} ${LLIBS})
128
129         # since we are using the local libs for python when compiling msvc projects, we need to add _d when compiling debug versions
130         if(WITH_PYTHON AND NOT WITH_PYTHON_MODULE)
131                 target_link_libraries(${target} ${PYTHON_LINKFLAGS})
132
133                 if(WIN32 AND NOT UNIX)
134                         target_link_libraries(${target} debug ${PYTHON_LIBRARY}_d)
135                         target_link_libraries(${target} optimized ${PYTHON_LIBRARY})
136                 else()
137                         target_link_libraries(${target} ${PYTHON_LIBRARY})
138                 endif()
139         endif()
140
141         target_link_libraries(${target} ${OPENGL_glu_LIBRARY} ${JPEG_LIBRARIES} ${PNG_LIBRARIES} ${ZLIB_LIBRARIES})
142         target_link_libraries(${target} ${FREETYPE_LIBRARY})
143
144         if(WITH_INTERNATIONAL)
145                 target_link_libraries(${target} ${GETTEXT_LIB})
146
147                 if(WIN32 AND NOT UNIX)
148                         target_link_libraries(${target} ${ICONV_LIB})
149                 endif()
150         endif()
151
152         if(WITH_OPENAL)
153                 target_link_libraries(${target} ${OPENAL_LIBRARY})
154         endif()
155         if(WITH_FFTW3)
156                 target_link_libraries(${target} ${FFTW3_LIB})
157         endif()
158         if(WITH_JACK)
159                 target_link_libraries(${target} ${JACK_LIB})
160         endif()
161         if(WITH_CODEC_SNDFILE)
162                 target_link_libraries(${target} ${SNDFILE_LIB})
163         endif()
164         if(WITH_SAMPLERATE)
165                 target_link_libraries(${target} ${LIBSAMPLERATE_LIB})
166         endif()
167         if(WITH_SDL)
168                 target_link_libraries(${target} ${SDL_LIBRARY})
169         endif()
170         if(WITH_CODEC_QUICKTIME)
171                 target_link_libraries(${target} ${QUICKTIME_LIB})
172         endif()
173         if(WITH_IMAGE_TIFF)
174                 target_link_libraries(${target} ${TIFF_LIBRARY})
175         endif()
176         if(WITH_IMAGE_OPENEXR)
177                 if(WIN32 AND NOT UNIX)
178                         foreach(loop_var ${OPENEXR_LIB})
179                                 target_link_libraries(${target} debug ${loop_var}_d)
180                                 target_link_libraries(${target} optimized ${loop_var})
181                         endforeach()
182                 else()
183                         target_link_libraries(${target} ${OPENEXR_LIB})
184                 endif()
185         endif()
186         if(WITH_IMAGE_OPENJPEG AND UNIX AND NOT APPLE)
187                 target_link_libraries(${target} ${OPENJPEG_LIB})
188         endif()
189         if(WITH_LCMS)
190                 target_link_libraries(${target} ${LCMS_LIBRARY})
191         endif()
192         if(WITH_CODEC_FFMPEG)
193                 target_link_libraries(${target} ${FFMPEG_LIB})
194         endif()
195         if(WITH_OPENCOLLADA)
196                 if(WIN32 AND NOT UNIX)
197                         foreach(loop_var ${OPENCOLLADA_LIB})
198                                 target_link_libraries(${target} debug ${loop_var}_d)
199                                 target_link_libraries(${target} optimized ${loop_var})
200                         endforeach()
201                         target_link_libraries(${target} debug ${PCRE_LIB}_d)
202                         target_link_libraries(${target} optimized ${PCRE_LIB})
203                         if(EXPAT_LIB)
204                                 target_link_libraries(${target} debug ${EXPAT_LIB}_d)
205                                 target_link_libraries(${target} optimized ${EXPAT_LIB})
206                         endif()
207                 else()
208                         target_link_libraries(${target} ${OPENCOLLADA_LIB})
209                         target_link_libraries(${target} ${PCRE_LIB})
210                         target_link_libraries(${target} ${EXPAT_LIB})
211                 endif()
212         endif()
213         if(WITH_LCMS)
214                 if(WIN32 AND NOT UNIX)
215                         target_link_libraries(${target} debug ${LCMS_LIB}_d)
216                         target_link_libraries(${target} optimized ${LCMS_LIB})
217                 endif()
218         endif()
219         if(WIN32 AND NOT UNIX)
220                 target_link_libraries(${target} ${PTHREADS_LIB})
221         endif()
222 endmacro()
223
224 macro(TEST_SSE_SUPPORT)
225         include(CheckCSourceRuns)
226
227         # message(STATUS "Detecting SSE support")
228         if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
229                 set(CMAKE_REQUIRED_FLAGS "-msse -msse2")
230         elseif(MSVC)
231                 set(CMAKE_REQUIRED_FLAGS "/arch:SSE2") # TODO, SSE 1 ?
232         endif()
233
234         if(NOT DEFINED ${SUPPORT_SSE_BUILD})
235                 check_c_source_runs("
236                         #include <xmmintrin.h>
237                         int main() { __m128 v = _mm_setzero_ps(); return 0; }"
238                 SUPPORT_SSE_BUILD)
239                 
240                 if(SUPPORT_SSE_BUILD)
241                         message(STATUS "SSE Support: detected.")
242                 else()
243                         message(STATUS "SSE Support: missing.")
244                 endif()
245                 set(${SUPPORT_SSE_BUILD} ${SUPPORT_SSE_BUILD} CACHE INTERNAL "SSE Test")
246         endif() 
247
248         if(NOT DEFINED ${SUPPORT_SSE2_BUILD})
249                 check_c_source_runs("
250                         #include <emmintrin.h>
251                         int main() { __m128d v = _mm_setzero_pd(); return 0; }"
252                 SUPPORT_SSE2_BUILD)
253
254                 if(SUPPORT_SSE2_BUILD)
255                         message(STATUS "SSE2 Support: detected.")
256                 else()
257                         message(STATUS "SSE2 Support: missing.")
258                 endif() 
259                 set(${SUPPORT_SSE2_BUILD} ${SUPPORT_SSE2_BUILD} CACHE INTERNAL "SSE2 Test")
260         endif()
261
262 endmacro()
263
264 # when we have warnings as errors applied globally this
265 # needs to be removed for some external libs which we dont maintain.
266
267 # utility macro
268 macro(_remove_strict_flags
269         flag)
270
271         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
272         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
273         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
274         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
275         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
276
277         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
278         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
279         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
280         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
281         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
282
283 endmacro()
284
285 macro(remove_strict_flags)
286
287         if(CMAKE_COMPILER_IS_GNUCC)
288                 _remove_strict_flags("-Wstrict-prototypes")
289                 _remove_strict_flags("-Wunused-parameter")
290                 _remove_strict_flags("-Wwrite-strings")
291                 _remove_strict_flags("-Wshadow")
292                 _remove_strict_flags("-Werror=[^ ]+")
293                 _remove_strict_flags("-Werror")
294         endif()
295
296         if(MSVC)
297                 # TODO
298         endif()
299
300 endmacro()
301
302 macro(ADD_CHECK_C_COMPILER_FLAG
303         _CFLAGS
304         _CACHE_VAR
305         _FLAG)
306
307         include(CheckCCompilerFlag)
308
309         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
310         if(${_CACHE_VAR})
311                 # message(STATUS "Using CFLAG: ${_FLAG}")
312                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}")
313         else()
314                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
315         endif()
316 endmacro()
317
318 macro(ADD_CHECK_CXX_COMPILER_FLAG
319         _CXXFLAGS
320         _CACHE_VAR
321         _FLAG)
322
323         include(CheckCXXCompilerFlag)
324
325         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
326         if(${_CACHE_VAR})
327                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
328                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}")
329         else()
330                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
331         endif()
332 endmacro()
333
334 macro(get_blender_version)
335         # So cmake depends on BKE_blender.h, beware of inf-loops!
336         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
337
338         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
339
340         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
341         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
342         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
343         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
344
345         if(NOT ${_out_version} MATCHES "[0-9]+")
346                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
347         endif()
348
349         if(NOT ${_out_subversion} MATCHES "[0-9]+")
350                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
351         endif()
352
353         if(NOT ${_out_version_char} MATCHES "[a-z]+")
354                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
355         endif()
356
357         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
358                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
359         endif()
360
361         MATH(EXPR BLENDER_VERSION_MAJOR "${_out_version} / 100")
362         MATH(EXPR BLENDER_VERSION_MINOR "${_out_version} % 100")
363         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
364
365         set(BLENDER_SUBVERSION ${_out_subversion})
366         set(BLENDER_VERSION_CHAR ${_out_version_char})
367         set(BLENDER_VERSION_CYCLE ${_out_version_cycle})
368
369         # for packaging, alpha to numbers
370         if(${BLENDER_VERSION_CHAR})
371                 set(BLENDER_VERSION_CHAR_INDEX "0")
372         else()
373                 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)
374                 list(FIND _char_ls ${BLENDER_VERSION_CHAR} _out_version_char_index)
375                 MATH(EXPR BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index} + 1")
376                 unset(_char_ls)
377                 unset(_out_version_char_index)
378         endif()
379
380         unset(_out_subversion)
381         unset(_out_version_char)
382         unset(_out_version_cycle)
383
384         # message(STATUS "Version (Internal): ${BLENDER_VERSION}.${BLENDER_SUBVERSION}, Version (external): ${BLENDER_VERSION}${BLENDER_VERSION_CHAR}-${BLENDER_VERSION_CYCLE}")
385 endmacro()