Add option for CMake to build libredcode, also use CODEC prefix for ffmpeg, quicktime...
[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(SRC)
23 ENDMACRO(BLENDERLIB_NOLIST)
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(BLENDERLIB_NOLIST)
35
36
37 MACRO(BLENDERLIB
38         name
39         sources
40         includes)
41
42         BLENDERLIB_NOLIST(${name} "${sources}" "${includes}")
43
44         # Add to blender's list of libraries
45         FILE(APPEND ${CMAKE_BINARY_DIR}/cmake_blender_libs.txt "${name};")
46 ENDMACRO(BLENDERLIB)
47
48 MACRO(SETUP_LIBDIRS)
49         # see "cmake --help-policy CMP0003"
50         if(COMMAND cmake_policy)
51                 CMAKE_POLICY(SET CMP0003 NEW)
52         endif(COMMAND cmake_policy)
53         
54         LINK_DIRECTORIES(${JPEG_LIBPATH} ${PNG_LIBPATH} ${ZLIB_LIBPATH} ${FREETYPE_LIBPATH})
55
56         IF(WITH_PYTHON)
57                 LINK_DIRECTORIES(${PYTHON_LIBPATH})
58         ENDIF(WITH_PYTHON)
59         IF(WITH_INTERNATIONAL)
60                 LINK_DIRECTORIES(${ICONV_LIBPATH})
61                 LINK_DIRECTORIES(${GETTEXT_LIBPATH})
62         ENDIF(WITH_INTERNATIONAL)
63         IF(WITH_SDL)
64                 LINK_DIRECTORIES(${SDL_LIBPATH})
65         ENDIF(WITH_SDL)
66         IF(WITH_CODEC_FFMPEG)
67                 LINK_DIRECTORIES(${FFMPEG_LIBPATH})
68         ENDIF(WITH_CODEC_FFMPEG)
69         IF(WITH_IMAGE_OPENEXR)
70                 LINK_DIRECTORIES(${OPENEXR_LIBPATH})
71         ENDIF(WITH_IMAGE_OPENEXR)
72         IF(WITH_IMAGE_TIFF)
73                 LINK_DIRECTORIES(${TIFF_LIBPATH})
74         ENDIF(WITH_IMAGE_TIFF)
75         IF(WITH_LCMS)
76                 LINK_DIRECTORIES(${LCMS_LIBPATH})
77         ENDIF(WITH_LCMS)
78         IF(WITH_CODEC_QUICKTIME)
79                 LINK_DIRECTORIES(${QUICKTIME_LIBPATH})
80         ENDIF(WITH_CODEC_QUICKTIME)
81         IF(WITH_OPENAL)
82                 LINK_DIRECTORIES(${OPENAL_LIBPATH})
83         ENDIF(WITH_OPENAL)
84         IF(WITH_JACK)
85                 LINK_DIRECTORIES(${JACK_LIBPATH})
86         ENDIF(WITH_JACK)
87         IF(WITH_CODEC_SNDFILE)
88                 LINK_DIRECTORIES(${SNDFILE_LIBPATH})
89         ENDIF(WITH_CODEC_SNDFILE)
90         IF(WITH_SAMPLERATE)
91                 LINK_DIRECTORIES(${LIBSAMPLERATE_LIBPATH})
92         ENDIF(WITH_SAMPLERATE)
93         IF(WITH_FFTW3)
94                 LINK_DIRECTORIES(${FFTW3_LIBPATH})
95         ENDIF(WITH_FFTW3)
96         IF(WITH_OPENCOLLADA)
97                 LINK_DIRECTORIES(${OPENCOLLADA_LIBPATH})
98                 LINK_DIRECTORIES(${PCRE_LIBPATH})
99                 LINK_DIRECTORIES(${EXPAT_LIBPATH})
100         ENDIF(WITH_OPENCOLLADA)
101
102         IF(WIN32 AND NOT UNIX)
103                 LINK_DIRECTORIES(${PTHREADS_LIBPATH})
104         ENDIF(WIN32 AND NOT UNIX)
105 ENDMACRO(SETUP_LIBDIRS)
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(WIN32 AND NOT UNIX)
121                         TARGET_LINK_LIBRARIES(${target} ${PYTHON_LIB})
122                 ENDIF(WIN32 AND NOT UNIX)
123         ENDIF(WITH_PYTHON)
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(WIN32 AND NOT UNIX)
134         ENDIF(WITH_INTERNATIONAL)
135
136         IF(WITH_OPENAL)
137                 TARGET_LINK_LIBRARIES(${target} ${OPENAL_LIBRARY})
138         ENDIF(WITH_OPENAL)
139         IF(WITH_FFTW3)  
140                 TARGET_LINK_LIBRARIES(${target} ${FFTW3_LIB})
141         ENDIF(WITH_FFTW3)
142         IF(WITH_JACK)
143                 TARGET_LINK_LIBRARIES(${target} ${JACK_LIB})
144         ENDIF(WITH_JACK)
145         IF(WITH_CODEC_SNDFILE)
146                 TARGET_LINK_LIBRARIES(${target} ${SNDFILE_LIB})
147         ENDIF(WITH_CODEC_SNDFILE)
148         IF(WITH_SAMPLERATE)
149                 TARGET_LINK_LIBRARIES(${target} ${LIBSAMPLERATE_LIB})
150         ENDIF(WITH_SAMPLERATE)  
151         IF(WITH_SDL)
152                 TARGET_LINK_LIBRARIES(${target} ${SDL_LIBRARY})
153         ENDIF(WITH_SDL)
154         IF(WITH_CODEC_QUICKTIME)
155                 TARGET_LINK_LIBRARIES(${target} ${QUICKTIME_LIB})
156         ENDIF(WITH_CODEC_QUICKTIME)
157         IF(WITH_IMAGE_TIFF)
158                 TARGET_LINK_LIBRARIES(${target} ${TIFF_LIBRARY})
159         ENDIF(WITH_IMAGE_TIFF)
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(loop_var)
166                 ELSE(WIN32 AND NOT UNIX)
167                         TARGET_LINK_LIBRARIES(${target} ${OPENEXR_LIB})
168                 ENDIF(WIN32 AND NOT UNIX)
169         ENDIF(WITH_IMAGE_OPENEXR)
170         IF(WITH_LCMS)
171                 TARGET_LINK_LIBRARIES(${target} ${LCMS_LIBRARY})
172         ENDIF(WITH_LCMS)
173         IF(WITH_CODEC_FFMPEG)
174                 TARGET_LINK_LIBRARIES(${target} ${FFMPEG_LIB})
175         ENDIF(WITH_CODEC_FFMPEG)
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(loop_var)
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(EXPAT_LIB)
188                 ELSE(WIN32 AND NOT UNIX)
189                         TARGET_LINK_LIBRARIES(${target} ${OPENCOLLADA_LIB})
190                         TARGET_LINK_LIBRARIES(${target} ${PCRE_LIB})
191                         TARGET_LINK_LIBRARIES(${target} ${EXPAT_LIB})
192                 ENDIF(WIN32 AND NOT UNIX)
193         ENDIF(WITH_OPENCOLLADA)
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(WIN32 AND NOT UNIX)
199         ENDIF(WITH_LCMS)
200         IF(WIN32 AND NOT UNIX)
201                 TARGET_LINK_LIBRARIES(${target} ${PTHREADS_LIB})
202         ENDIF(WIN32 AND NOT UNIX)
203 ENDMACRO(SETUP_LIBLINKS)
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(SUPPORT_SSE_BUILD)
229                 MESSAGE(STATUS "   ...SSE support missing.")
230         ENDIF(SUPPORT_SSE_BUILD)
231
232         IF(SUPPORT_SSE2_BUILD)
233                 MESSAGE(STATUS "   ...SSE2 support found.")
234         ELSE(SUPPORT_SSE2_BUILD)
235                 MESSAGE(STATUS "   ...SSE2 support missing.")
236         ENDIF(SUPPORT_SSE2_BUILD)
237
238 ENDMACRO(TEST_SSE_SUPPORT)
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(_REMOVE_STRICT_FLAGS)
260
261 MACRO(REMOVE_STRICT_FLAGS)
262
263         IF(CMAKE_COMPILER_IS_GNUCC)
264                 _REMOVE_STRICT_FLAGS("-Wunused-parameter")
265                 _REMOVE_STRICT_FLAGS("-Werror=[^ ]+")
266                 _REMOVE_STRICT_FLAGS("-Werror")
267         ENDIF(CMAKE_COMPILER_IS_GNUCC)
268
269         IF(MSVC)
270                 # TODO
271         ENDIF(MSVC)
272
273 ENDMACRO(REMOVE_STRICT_FLAGS)
274
275
276 MACRO(GET_BLENDER_VERSION)
277         FILE(READ ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h CONTENT)
278         STRING(REGEX REPLACE "\n" ";" CONTENT "${CONTENT}")
279         STRING(REGEX REPLACE "\t" ";" CONTENT "${CONTENT}")
280         STRING(REGEX REPLACE " " ";" CONTENT "${CONTENT}")
281
282         FOREACH(ITEM ${CONTENT})
283                 IF(LASTITEM MATCHES "BLENDER_VERSION")
284                         MATH(EXPR BLENDER_VERSION_MAJOR "${ITEM} / 100")
285                         MATH(EXPR BLENDER_VERSION_MINOR "${ITEM} % 100")
286                         SET(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
287                 ENDIF(LASTITEM MATCHES "BLENDER_VERSION")
288                 
289                 IF(LASTITEM MATCHES "BLENDER_SUBVERSION")
290                         SET(BLENDER_SUBVERSION ${ITEM})
291                 ENDIF(LASTITEM MATCHES "BLENDER_SUBVERSION")
292                 
293                 IF(LASTITEM MATCHES "BLENDER_MINVERSION")
294                         MATH(EXPR BLENDER_MINVERSION_MAJOR "${ITEM} / 100")
295                         MATH(EXPR BLENDER_MINVERSION_MINOR "${ITEM} % 100")
296                         SET(BLENDER_MINVERSION "${BLENDER_MINVERSION_MAJOR}.${BLENDER_MINVERSION_MINOR}")
297                 ENDIF(LASTITEM MATCHES "BLENDER_MINVERSION")
298                 
299                 IF(LASTITEM MATCHES "BLENDER_MINSUBVERSION")
300                         SET(BLENDER_MINSUBVERSION ${ITEM})
301                 ENDIF(LASTITEM MATCHES "BLENDER_MINSUBVERSION")
302
303                 SET(LASTITEM ${ITEM})
304         ENDFOREACH(ITEM ${CONTENT})
305         
306         MESSAGE(STATUS "Version major: ${BLENDER_VERSION_MAJOR}, Version minor: ${BLENDER_VERSION_MINOR}, Subversion: ${BLENDER_SUBVERSION}, Version: ${BLENDER_VERSION}")
307         MESSAGE(STATUS "Minversion major: ${BLENDER_MINVERSION_MAJOR}, Minversion minor: ${BLENDER_MINVERSION_MINOR}, MinSubversion: ${BLENDER_MINSUBVERSION}, Minversion: ${BLENDER_MINVERSION}")
308 ENDMACRO(GET_BLENDER_VERSION)