a246a81a545a0edf6cf8aeea50d9632e36ac23d6
[blender-staging.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         check_c_source_runs("
216                 #include <xmmintrin.h>
217                 int main() { __m128 v = _mm_setzero_ps(); return 0; }"
218         SUPPORT_SSE_BUILD)
219
220         check_c_source_runs("
221                 #include <emmintrin.h>
222                 int main() { __m128d v = _mm_setzero_pd(); return 0; }"
223         SUPPORT_SSE2_BUILD)
224         message(STATUS "Detecting SSE support")
225
226         if(SUPPORT_SSE_BUILD)
227                 message(STATUS "   ...SSE support found.")
228         else()
229                 message(STATUS "   ...SSE support missing.")
230         endif()
231
232         if(SUPPORT_SSE2_BUILD)
233                 message(STATUS "   ...SSE2 support found.")
234         else()
235                 message(STATUS "   ...SSE2 support missing.")
236         endif()
237
238 endmacro()
239
240 # when we have warnings as errors applied globally this
241 # needs to be removed for some external libs which we dont maintain.
242
243 # utility macro
244 macro(_remove_strict_flags
245         flag)
246         
247         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
248         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
249         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
250         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL}")
251         string(REGEX REPLACE ${flag} "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
252
253         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
254         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
255         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
256         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
257         string(REGEX REPLACE ${flag} "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
258
259 endmacro()
260
261 macro(remove_strict_flags)
262
263         if(CMAKE_COMPILER_IS_GNUCC)
264                 _remove_strict_flags("-Wstrict-prototypes")
265                 _remove_strict_flags("-Wunused-parameter")
266                 _remove_strict_flags("-Wwrite-strings")
267                 _remove_strict_flags("-Wshadow")
268                 _remove_strict_flags("-Werror=[^ ]+")
269                 _remove_strict_flags("-Werror")
270         endif()
271
272         if(MSVC)
273                 # TODO
274         endif()
275
276 endmacro()
277
278 macro(ADD_CHECK_C_COMPILER_FLAG
279         _CFLAGS
280         _FLAG)
281
282         include(CheckCCompilerFlag)
283
284         CHECK_C_COMPILER_FLAG(${_FLAG} SUPPORT_WALL)
285
286         if(SUPPORT_WALL)
287                 message(STATUS "Using CFLAG: ${_FLAG}")
288                 set(_CFLAGS "${_CFLAGS} ${_FLAG}")
289         else()
290                 message(STATUS "Unsupported CFLAG: ${_FLAG}")
291         endif()
292 endmacro()
293
294 macro(ADD_CHECK_CXX_COMPILER_FLAG
295         _CXXFLAGS
296         _FLAG)
297
298         include(CheckCXXCompilerFlag)
299
300         CHECK_CXX_COMPILER_FLAG(${_FLAG} SUPPORT_WALL)
301
302         if(SUPPORT_WALL)
303                 message(STATUS "Using CXXFLAG: ${_FLAG}")
304                 set(_CFLAGS "${_CXXFLAGS} ${_FLAG}")
305         else()
306                 message(STATUS "Unsupported CXXFLAG: ${_FLAG}")
307         endif()
308 endmacro()
309
310 macro(get_blender_version)
311         file(READ ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h CONTENT)
312         string(REGEX REPLACE "\n" ";" CONTENT "${CONTENT}")
313         string(REGEX REPLACE "\t" ";" CONTENT "${CONTENT}")
314         string(REGEX REPLACE " " ";" CONTENT "${CONTENT}")
315
316         foreach(ITEM ${CONTENT})
317                 if(LASTITEM MATCHES "BLENDER_VERSION")
318                         MATH(EXPR BLENDER_VERSION_MAJOR "${ITEM} / 100")
319                         MATH(EXPR BLENDER_VERSION_MINOR "${ITEM} % 100")
320                         set(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
321                 endif()
322                 
323                 if(LASTITEM MATCHES "BLENDER_SUBVERSION")
324                         set(BLENDER_SUBVERSION ${ITEM})
325                 endif()
326                 
327                 if(LASTITEM MATCHES "BLENDER_MINVERSION")
328                         MATH(EXPR BLENDER_MINVERSION_MAJOR "${ITEM} / 100")
329                         MATH(EXPR BLENDER_MINVERSION_MINOR "${ITEM} % 100")
330                         set(BLENDER_MINVERSION "${BLENDER_MINVERSION_MAJOR}.${BLENDER_MINVERSION_MINOR}")
331                 endif()
332                 
333                 if(LASTITEM MATCHES "BLENDER_MINSUBVERSION")
334                         set(BLENDER_MINSUBVERSION ${ITEM})
335                 endif()
336
337                 set(LASTITEM ${ITEM})
338         endforeach()
339         
340         message(STATUS "Version major: ${BLENDER_VERSION_MAJOR}, Version minor: ${BLENDER_VERSION_MINOR}, Subversion: ${BLENDER_SUBVERSION}, Version: ${BLENDER_VERSION}")
341         message(STATUS "Minversion major: ${BLENDER_MINVERSION_MAJOR}, Minversion minor: ${BLENDER_MINVERSION_MINOR}, MinSubversion: ${BLENDER_MINSUBVERSION}, Minversion: ${BLENDER_MINVERSION}")
342 endmacro()