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