patch from IRIE Shinsuke, use systems openjpeg on *nix.
[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)
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)
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
303 # XXX, until cmake 2.8.4 is released.
304 INCLUDE(CheckCSourceCompiles)
305 MACRO (CHECK_C_COMPILER_FLAG__INTERNAL _FLAG _RESULT)
306    SET(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}")
307    SET(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}")
308    CHECK_C_SOURCE_COMPILES("int main(void) { return 0;}" ${_RESULT}
309      # Some compilers do not fail with a bad flag
310      FAIL_REGEX "unrecognized .*option"                     # GNU
311      FAIL_REGEX "ignoring unknown option"                   # MSVC
312      FAIL_REGEX "[Uu]nknown option"                         # HP
313      FAIL_REGEX "[Ww]arning: [Oo]ption"                     # SunPro
314      FAIL_REGEX "command option .* is not recognized"       # XL
315      )
316    SET (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}")
317 ENDMACRO (CHECK_C_COMPILER_FLAG__INTERNAL)
318 # XXX, end duplicate code.
319
320 macro(ADD_CHECK_C_COMPILER_FLAG
321         _CFLAGS
322         _CACHE_VAR
323         _FLAG)
324
325         # include(CheckCCompilerFlag)
326
327         CHECK_C_COMPILER_FLAG__INTERNAL("${_FLAG}" "${_CACHE_VAR}")
328         if(${_CACHE_VAR})
329                 # message(STATUS "Using CFLAG: ${_FLAG}")
330                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}")
331         else()
332                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
333         endif()
334 endmacro()
335
336 macro(ADD_CHECK_CXX_COMPILER_FLAG
337         _CXXFLAGS
338         _CACHE_VAR
339         _FLAG)
340
341         include(CheckCXXCompilerFlag)
342
343         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
344         if(${_CACHE_VAR})
345                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
346                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}")
347         else()
348                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
349         endif()
350 endmacro()
351
352 macro(get_blender_version)
353         file(READ ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h CONTENT)
354         string(REGEX REPLACE "\n" ";" CONTENT "${CONTENT}")
355         string(REGEX REPLACE "\t" ";" CONTENT "${CONTENT}")
356         string(REGEX REPLACE " " ";" CONTENT "${CONTENT}")
357
358         foreach(ITEM ${CONTENT})
359                 if(LASTITEM MATCHES "BLENDER_VERSION")
360                         MATH(EXPR BLENDER_VERSION_MAJOR "${ITEM} / 100")
361                         MATH(EXPR BLENDER_VERSION_MINOR "${ITEM} % 100")
362                         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
363                 endif()
364
365                 if(LASTITEM MATCHES "BLENDER_SUBVERSION")
366                         set(BLENDER_SUBVERSION ${ITEM})
367                 endif()
368
369                 if(LASTITEM MATCHES "BLENDER_MINVERSION")
370                         MATH(EXPR BLENDER_MINVERSION_MAJOR "${ITEM} / 100")
371                         MATH(EXPR BLENDER_MINVERSION_MINOR "${ITEM} % 100")
372                         set(BLENDER_MINVERSION "${BLENDER_MINVERSION_MAJOR}.${BLENDER_MINVERSION_MINOR}")
373                 endif()
374
375                 if(LASTITEM MATCHES "BLENDER_MINSUBVERSION")
376                         set(BLENDER_MINSUBVERSION ${ITEM})
377                 endif()
378
379                 set(LASTITEM ${ITEM})
380         endforeach()
381
382         # message(STATUS "Version major: ${BLENDER_VERSION_MAJOR}, Version minor: ${BLENDER_VERSION_MINOR}, Subversion: ${BLENDER_SUBVERSION}, Version: ${BLENDER_VERSION}")
383         # message(STATUS "Minversion major: ${BLENDER_MINVERSION_MAJOR}, Minversion minor: ${BLENDER_MINVERSION_MINOR}, MinSubversion: ${BLENDER_MINSUBVERSION}, Minversion: ${BLENDER_MINVERSION}")
384 endmacro()