fix for cmake flag detection, was not caching the resulting variable so the test...
[blender.git] / build_files / cmake / macros.cmake
1
2 # only MSVC uses SOURCE_GROUP
3 macro(blenderlib_nolist
4         name
5         sources
6         includes)
7
8         # message(STATUS "Configuring library ${name}")
9
10         include_directories(${includes})
11         add_library(${name} ${sources})
12
13         # Group by location on disk
14         source_group("Source Files" FILES CMakeLists.txt)
15         foreach(SRC ${sources})
16                 get_filename_component(SRC_EXT ${SRC} EXT)
17                 if(${SRC_EXT} MATCHES ".h" OR ${SRC_EXT} MATCHES ".hpp") 
18                         source_group("Header Files" FILES ${SRC})
19                 else()
20                         source_group("Source Files" FILES ${SRC})
21                 endif()
22         endforeach()
23 endmacro()
24
25 #       # works fine but having the includes listed is helpful for IDE's (QtCreator/MSVC)
26 #       macro(blenderlib_nolist
27 #               name
28 #               sources
29 #               includes)
30 #
31 #               message(STATUS "Configuring library ${name}")
32 #               include_directories(${includes})
33 #               add_library(${name} ${sources})
34 #       endmacro()
35
36
37 macro(blenderlib
38         name
39         sources
40         includes)
41
42         blenderlib_nolist(${name} "${sources}" "${includes}")
43
44         set_property(GLOBAL APPEND PROPERTY BLENDER_LINK_LIBS ${name})
45
46 endmacro()
47
48 macro(SETUP_LIBDIRS)
49         # see "cmake --help-policy CMP0003"
50         if(COMMAND cmake_policy)
51                 cmake_policy(SET CMP0003 NEW)
52         endif()
53         
54         link_directories(${JPEG_LIBPATH} ${PNG_LIBPATH} ${ZLIB_LIBPATH} ${FREETYPE_LIBPATH})
55
56         if(WITH_PYTHON)
57                 link_directories(${PYTHON_LIBPATH})
58         endif()
59         if(WITH_INTERNATIONAL)
60                 link_directories(${ICONV_LIBPATH})
61                 link_directories(${GETTEXT_LIBPATH})
62         endif()
63         if(WITH_SDL)
64                 link_directories(${SDL_LIBPATH})
65         endif()
66         if(WITH_CODEC_FFMPEG)
67                 link_directories(${FFMPEG_LIBPATH})
68         endif()
69         if(WITH_IMAGE_OPENEXR)
70                 link_directories(${OPENEXR_LIBPATH})
71         endif()
72         if(WITH_IMAGE_TIFF)
73                 link_directories(${TIFF_LIBPATH})
74         endif()
75         if(WITH_LCMS)
76                 link_directories(${LCMS_LIBPATH})
77         endif()
78         if(WITH_CODEC_QUICKTIME)
79                 link_directories(${QUICKTIME_LIBPATH})
80         endif()
81         if(WITH_OPENAL)
82                 link_directories(${OPENAL_LIBPATH})
83         endif()
84         if(WITH_JACK)
85                 link_directories(${JACK_LIBPATH})
86         endif()
87         if(WITH_CODEC_SNDFILE)
88                 link_directories(${SNDFILE_LIBPATH})
89         endif()
90         if(WITH_SAMPLERATE)
91                 link_directories(${LIBSAMPLERATE_LIBPATH})
92         endif()
93         if(WITH_FFTW3)
94                 link_directories(${FFTW3_LIBPATH})
95         endif()
96         if(WITH_OPENCOLLADA)
97                 link_directories(${OPENCOLLADA_LIBPATH})
98                 link_directories(${PCRE_LIBPATH})
99                 link_directories(${EXPAT_LIBPATH})
100         endif()
101
102         if(WIN32 AND NOT UNIX)
103                 link_directories(${PTHREADS_LIBPATH})
104         endif()
105 endmacro()
106
107 macro(setup_liblinks
108         target)
109         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS} ")
110
111         target_link_libraries(${target} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} ${JPEG_LIBRARY} ${PNG_LIBRARIES} ${ZLIB_LIBRARIES} ${LLIBS})
112
113         # since we are using the local libs for python when compiling msvc projects, we need to add _d when compiling debug versions
114         if(WITH_PYTHON)
115                 target_link_libraries(${target} ${PYTHON_LINKFLAGS})
116
117                 if(WIN32 AND NOT UNIX)
118                         target_link_libraries(${target} debug ${PYTHON_LIB}_d)
119                         target_link_libraries(${target} optimized ${PYTHON_LIB})
120                 else()
121                         target_link_libraries(${target} ${PYTHON_LIB})
122                 endif()
123         endif()
124
125         target_link_libraries(${target} ${OPENGL_glu_LIBRARY} ${JPEG_LIB} ${PNG_LIB} ${ZLIB_LIB})
126         target_link_libraries(${target} ${FREETYPE_LIBRARY})
127
128         if(WITH_INTERNATIONAL)
129                 target_link_libraries(${target} ${GETTEXT_LIB})
130
131                 if(WIN32 AND NOT UNIX)
132                         target_link_libraries(${target} ${ICONV_LIB})
133                 endif()
134         endif()
135
136         if(WITH_OPENAL)
137                 target_link_libraries(${target} ${OPENAL_LIBRARY})
138         endif()
139         if(WITH_FFTW3)  
140                 target_link_libraries(${target} ${FFTW3_LIB})
141         endif()
142         if(WITH_JACK)
143                 target_link_libraries(${target} ${JACK_LIB})
144         endif()
145         if(WITH_CODEC_SNDFILE)
146                 target_link_libraries(${target} ${SNDFILE_LIB})
147         endif()
148         if(WITH_SAMPLERATE)
149                 target_link_libraries(${target} ${LIBSAMPLERATE_LIB})
150         endif() 
151         if(WITH_SDL)
152                 target_link_libraries(${target} ${SDL_LIBRARY})
153         endif()
154         if(WITH_CODEC_QUICKTIME)
155                 target_link_libraries(${target} ${QUICKTIME_LIB})
156         endif()
157         if(WITH_IMAGE_TIFF)
158                 target_link_libraries(${target} ${TIFF_LIBRARY})
159         endif()
160         if(WITH_IMAGE_OPENEXR)
161                 if(WIN32 AND NOT UNIX)
162                         foreach(loop_var ${OPENEXR_LIB})
163                                 target_link_libraries(${target} debug ${loop_var}_d)
164                                 target_link_libraries(${target} optimized ${loop_var})
165                         endforeach()
166                 else()
167                         target_link_libraries(${target} ${OPENEXR_LIB})
168                 endif()
169         endif()
170         if(WITH_LCMS)
171                 target_link_libraries(${target} ${LCMS_LIBRARY})
172         endif()
173         if(WITH_CODEC_FFMPEG)
174                 target_link_libraries(${target} ${FFMPEG_LIB})
175         endif()
176         if(WITH_OPENCOLLADA)
177                 if(WIN32 AND NOT UNIX)
178                         foreach(loop_var ${OPENCOLLADA_LIB})
179                                 target_link_libraries(${target} debug ${loop_var}_d)
180                                 target_link_libraries(${target} optimized ${loop_var})
181                         endforeach()
182                         target_link_libraries(${target} debug ${PCRE_LIB}_d)
183                         target_link_libraries(${target} optimized ${PCRE_LIB})
184                         if(EXPAT_LIB)
185                                 target_link_libraries(${target} debug ${EXPAT_LIB}_d)
186                                 target_link_libraries(${target} optimized ${EXPAT_LIB})
187                         endif()
188                 else()
189                         target_link_libraries(${target} ${OPENCOLLADA_LIB})
190                         target_link_libraries(${target} ${PCRE_LIB})
191                         target_link_libraries(${target} ${EXPAT_LIB})
192                 endif()
193         endif()
194         if(WITH_LCMS)
195                 if(WIN32 AND NOT UNIX)
196                         target_link_libraries(${target} debug ${LCMS_LIB}_d)
197                         target_link_libraries(${target} optimized ${LCMS_LIB})
198                 endif()
199         endif()
200         if(WIN32 AND NOT UNIX)
201                 target_link_libraries(${target} ${PTHREADS_LIB})
202         endif()
203 endmacro()
204
205 macro(TEST_SSE_SUPPORT)
206         include(CheckCSourceRuns)
207
208         # message(STATUS "Detecting SSE support")
209         if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
210                 set(CMAKE_REQUIRED_FLAGS "-msse -msse2")
211         elseif(MSVC)
212                 set(CMAKE_REQUIRED_FLAGS "/arch:SSE2") # TODO, SSE 1 ?
213         endif()
214
215         if(NOT DEFINED ${SUPPORT_SSE_BUILD})
216                 check_c_source_runs("
217                         #include <xmmintrin.h>
218                         int main() { __m128 v = _mm_setzero_ps(); return 0; }"
219                 SUPPORT_SSE_BUILD)
220                 
221                 if(SUPPORT_SSE_BUILD)
222                         message(STATUS "SSE Support: detected.")
223                 else()
224                         message(STATUS "SSE Support: missing.")
225                 endif()
226                 set(${SUPPORT_SSE_BUILD} ${SUPPORT_SSE_BUILD} CACHE INTERNAL "SSE Test")
227         endif() 
228
229         if(NOT DEFINED ${SUPPORT_SSE2_BUILD})
230                 check_c_source_runs("
231                         #include <emmintrin.h>
232                         int main() { __m128d v = _mm_setzero_pd(); return 0; }"
233                 SUPPORT_SSE2_BUILD)
234
235                 if(SUPPORT_SSE2_BUILD)
236                         message(STATUS "SSE2 Support: detected.")
237                 else()
238                         message(STATUS "SSE2 Support: missing.")
239                 endif() 
240                 set(${SUPPORT_SSE2_BUILD} ${SUPPORT_SSE2_BUILD} CACHE INTERNAL "SSE2 Test")
241         endif()
242
243 endmacro()
244
245 # when we have warnings as errors applied globally this
246 # needs to be removed for some external libs which we dont maintain.
247
248 # utility macro
249 macro(_remove_strict_flags
250         flag)
251         
252         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
253         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
254         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
255         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
256         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
257
258         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
259         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
260         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
261         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
262         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
263
264 endmacro()
265
266 macro(remove_strict_flags)
267
268         if(CMAKE_COMPILER_IS_GNUCC)
269                 _remove_strict_flags("-Wstrict-prototypes")
270                 _remove_strict_flags("-Wunused-parameter")
271                 _remove_strict_flags("-Wwrite-strings")
272                 _remove_strict_flags("-Wshadow")
273                 _remove_strict_flags("-Werror=[^ ]+")
274                 _remove_strict_flags("-Werror")
275         endif()
276
277         if(MSVC)
278                 # TODO
279         endif()
280
281 endmacro()
282
283
284 # XXX, until cmake 2.8.4 is released.
285 INCLUDE(CheckCSourceCompiles)
286 MACRO (CHECK_C_COMPILER_FLAG__INTERNAL _FLAG _RESULT)
287    SET(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}")
288    SET(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}")
289    CHECK_C_SOURCE_COMPILES("int main(void) { return 0;}" ${_RESULT}
290      # Some compilers do not fail with a bad flag
291      FAIL_REGEX "unrecognized .*option"                     # GNU
292      FAIL_REGEX "ignoring unknown option"                   # MSVC
293      FAIL_REGEX "[Uu]nknown option"                         # HP
294      FAIL_REGEX "[Ww]arning: [Oo]ption"                     # SunPro
295      FAIL_REGEX "command option .* is not recognized"       # XL
296      )
297    SET (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}")
298 ENDMACRO (CHECK_C_COMPILER_FLAG__INTERNAL)
299 # XXX, end duplicate code.
300
301 macro(ADD_CHECK_C_COMPILER_FLAG
302         _CFLAGS
303         _CACHE_VAR
304         _FLAG)
305
306         # include(CheckCCompilerFlag)
307
308         CHECK_C_COMPILER_FLAG__INTERNAL("${_FLAG}" "${_CACHE_VAR}")
309         if(${_CACHE_VAR})
310                 # message(STATUS "Using CFLAG: ${_FLAG}")
311                 set(${_CFLAGS} "${${_CFLAGS}} ${_FLAG}")
312         else()
313                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
314         endif()
315 endmacro()
316
317 macro(ADD_CHECK_CXX_COMPILER_FLAG
318         _CXXFLAGS
319         _CACHE_VAR
320         _FLAG)
321
322         include(CheckCXXCompilerFlag)
323
324         CHECK_CXX_COMPILER_FLAG("${_FLAG}" "${_CACHE_VAR}")
325         if(${_CACHE_VAR})
326                 # message(STATUS "Using CXXFLAG: ${_FLAG}")
327                 set(${_CXXFLAGS} "${${_CXXFLAGS}} ${_FLAG}")
328         else()
329                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
330         endif()
331 endmacro()
332
333 macro(get_blender_version)
334         file(READ ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h CONTENT)
335         string(REGEX REPLACE "\n" ";" CONTENT "${CONTENT}")
336         string(REGEX REPLACE "\t" ";" CONTENT "${CONTENT}")
337         string(REGEX REPLACE " " ";" CONTENT "${CONTENT}")
338
339         foreach(ITEM ${CONTENT})
340                 if(LASTITEM MATCHES "BLENDER_VERSION")
341                         MATH(EXPR BLENDER_VERSION_MAJOR "${ITEM} / 100")
342                         MATH(EXPR BLENDER_VERSION_MINOR "${ITEM} % 100")
343                         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
344                 endif()
345                 
346                 if(LASTITEM MATCHES "BLENDER_SUBVERSION")
347                         set(BLENDER_SUBVERSION ${ITEM})
348                 endif()
349                 
350                 if(LASTITEM MATCHES "BLENDER_MINVERSION")
351                         MATH(EXPR BLENDER_MINVERSION_MAJOR "${ITEM} / 100")
352                         MATH(EXPR BLENDER_MINVERSION_MINOR "${ITEM} % 100")
353                         set(BLENDER_MINVERSION "${BLENDER_MINVERSION_MAJOR}.${BLENDER_MINVERSION_MINOR}")
354                 endif()
355                 
356                 if(LASTITEM MATCHES "BLENDER_MINSUBVERSION")
357                         set(BLENDER_MINSUBVERSION ${ITEM})
358                 endif()
359
360                 set(LASTITEM ${ITEM})
361         endforeach()
362         
363         # message(STATUS "Version major: ${BLENDER_VERSION_MAJOR}, Version minor: ${BLENDER_VERSION_MINOR}, Subversion: ${BLENDER_SUBVERSION}, Version: ${BLENDER_VERSION}")
364         # message(STATUS "Minversion major: ${BLENDER_MINVERSION_MAJOR}, Minversion minor: ${BLENDER_MINVERSION_MINOR}, MinSubversion: ${BLENDER_MINSUBVERSION}, Minversion: ${BLENDER_MINVERSION}")
365 endmacro()