LCMS code removed, was an experiment but never finished.
[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_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         if(WITH_MEM_JEMALLOC)
115                 link_directories(${JEMALLOC_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_CODEC_FFMPEG)
194                 target_link_libraries(${target} ${FFMPEG_LIB})
195         endif()
196         if(WITH_OPENCOLLADA)
197                 if(WIN32 AND NOT UNIX)
198                         foreach(loop_var ${OPENCOLLADA_LIB})
199                                 target_link_libraries(${target} debug ${loop_var}_d)
200                                 target_link_libraries(${target} optimized ${loop_var})
201                         endforeach()
202                         target_link_libraries(${target} debug ${PCRE_LIB}_d)
203                         target_link_libraries(${target} optimized ${PCRE_LIB})
204                         if(EXPAT_LIB)
205                                 target_link_libraries(${target} debug ${EXPAT_LIB}_d)
206                                 target_link_libraries(${target} optimized ${EXPAT_LIB})
207                         endif()
208                 else()
209                         target_link_libraries(${target} ${OPENCOLLADA_LIB})
210                         target_link_libraries(${target} ${PCRE_LIB})
211                         target_link_libraries(${target} ${EXPAT_LIB})
212                 endif()
213         endif()
214         if(WITH_MEM_JEMALLOC)
215                 target_link_libraries(${target} ${JEMALLOC_LIBRARY})
216         endif()
217         if(WIN32 AND NOT UNIX)
218                 target_link_libraries(${target} ${PTHREADS_LIB})
219         endif()
220 endmacro()
221
222 macro(TEST_SSE_SUPPORT)
223         include(CheckCSourceRuns)
224
225         # message(STATUS "Detecting SSE support")
226         if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
227                 set(CMAKE_REQUIRED_FLAGS "-msse -msse2")
228         elseif(MSVC)
229                 set(CMAKE_REQUIRED_FLAGS "/arch:SSE2") # TODO, SSE 1 ?
230         endif()
231
232         if(NOT DEFINED ${SUPPORT_SSE_BUILD})
233                 check_c_source_runs("
234                         #include <xmmintrin.h>
235                         int main() { __m128 v = _mm_setzero_ps(); return 0; }"
236                 SUPPORT_SSE_BUILD)
237                 
238                 if(SUPPORT_SSE_BUILD)
239                         message(STATUS "SSE Support: detected.")
240                 else()
241                         message(STATUS "SSE Support: missing.")
242                 endif()
243                 set(${SUPPORT_SSE_BUILD} ${SUPPORT_SSE_BUILD} CACHE INTERNAL "SSE Test")
244         endif() 
245
246         if(NOT DEFINED ${SUPPORT_SSE2_BUILD})
247                 check_c_source_runs("
248                         #include <emmintrin.h>
249                         int main() { __m128d v = _mm_setzero_pd(); return 0; }"
250                 SUPPORT_SSE2_BUILD)
251
252                 if(SUPPORT_SSE2_BUILD)
253                         message(STATUS "SSE2 Support: detected.")
254                 else()
255                         message(STATUS "SSE2 Support: missing.")
256                 endif() 
257                 set(${SUPPORT_SSE2_BUILD} ${SUPPORT_SSE2_BUILD} CACHE INTERNAL "SSE2 Test")
258         endif()
259
260 endmacro()
261
262 # when we have warnings as errors applied globally this
263 # needs to be removed for some external libs which we dont maintain.
264
265 # utility macro
266 macro(remove_flag
267         flag)
268
269         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
270         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
271         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
272         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
273         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
274
275         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
276         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
277         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
278         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
279         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
280
281 endmacro()
282
283 macro(remove_strict_flags)
284
285         if(CMAKE_COMPILER_IS_GNUCC)
286                 remove_flag("-Wstrict-prototypes")
287                 remove_flag("-Wunused-parameter")
288                 remove_flag("-Wwrite-strings")
289                 remove_flag("-Wshadow")
290                 remove_flag("-Werror=[^ ]+")
291                 remove_flag("-Werror")
292         endif()
293
294         if(MSVC)
295                 # TODO
296         endif()
297
298 endmacro()
299
300 macro(ADD_CHECK_C_COMPILER_FLAG
301         _CFLAGS
302         _CACHE_VAR
303         _FLAG)
304
305         include(CheckCCompilerFlag)
306
307         CHECK_C_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
308         if(${_CACHE_VAR})
309                 # message(STATUS "Using CFLAG: ${_FLAG}")
310                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}")
311         else()
312                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
313         endif()
314 endmacro()
315
316 macro(ADD_CHECK_CXX_COMPILER_FLAG
317         _CXXFLAGS
318         _CACHE_VAR
319         _FLAG)
320
321         include(CheckCXXCompilerFlag)
322
323         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
324         if(${_CACHE_VAR})
325                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
326                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}")
327         else()
328                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
329         endif()
330 endmacro()
331
332 macro(get_blender_version)
333         # So cmake depends on BKE_blender.h, beware of inf-loops!
334         CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h ${CMAKE_BINARY_DIR}/source/blender/blenkernel/BKE_blender.h.done)
335
336         file(STRINGS ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h _contents REGEX "^#define[ \t]+BLENDER_.*$")
337
338         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION[ \t]+([0-9]+).*" "\\1" _out_version "${_contents}")
339         string(REGEX REPLACE ".*#define[ \t]+BLENDER_SUBVERSION[ \t]+([0-9]+).*" "\\1" _out_subversion "${_contents}")
340         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CHAR[ \t]+([a-z]+).*" "\\1" _out_version_char "${_contents}")
341         string(REGEX REPLACE ".*#define[ \t]+BLENDER_VERSION_CYCLE[ \t]+([a-z]+).*" "\\1" _out_version_cycle "${_contents}")
342
343         if(NOT ${_out_version} MATCHES "[0-9]+")
344                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION")
345         endif()
346
347         if(NOT ${_out_subversion} MATCHES "[0-9]+")
348                 message(FATAL_ERROR "Version parsing failed for BLENDER_SUBVERSION")
349         endif()
350
351         # clumsy regex, only single char are ok but it could be unset
352
353         string(LENGTH "${_out_version_char}" _out_version_char_len)
354         if(NOT _out_version_char_len EQUAL 1)
355                 set(_out_version_char "")
356         elseif(NOT ${_out_version_char} MATCHES "[a-z]+")
357                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CHAR")
358         endif()
359
360         if(NOT ${_out_version_cycle} MATCHES "[a-z]+")
361                 message(FATAL_ERROR "Version parsing failed for BLENDER_VERSION_CYCLE")
362         endif()
363
364         math(EXPR BLENDER_VERSION_MAJOR "${_out_version} / 100")
365         math(EXPR BLENDER_VERSION_MINOR "${_out_version} % 100")
366         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
367
368         set(BLENDER_SUBVERSION ${_out_subversion})
369         set(BLENDER_VERSION_CHAR ${_out_version_char})
370         set(BLENDER_VERSION_CYCLE ${_out_version_cycle})
371
372         # for packaging, alpha to numbers
373         string(COMPARE EQUAL "${BLENDER_VERSION_CHAR}" "" _out_version_char_empty)
374         if(${_out_version_char_empty})
375                 set(BLENDER_VERSION_CHAR_INDEX "0")
376         else()
377                 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)
378                 list(FIND _char_ls ${BLENDER_VERSION_CHAR} _out_version_char_index)
379                 math(EXPR BLENDER_VERSION_CHAR_INDEX "${_out_version_char_index} + 1")
380                 unset(_char_ls)
381                 unset(_out_version_char_index)
382         endif()
383
384         unset(_out_subversion)
385         unset(_out_version_char)
386         unset(_out_version_char_empty)
387         unset(_out_version_cycle)
388
389         # message(STATUS "Version (Internal): ${BLENDER_VERSION}.${BLENDER_SUBVERSION}, Version (external): ${BLENDER_VERSION}${BLENDER_VERSION_CHAR}-${BLENDER_VERSION_CYCLE}")
390 endmacro()