9237f01a950aefce8cca1f7a05faf90d0273b49c
[blender-staging.git] / build_files / cmake / macros.cmake
1
2 IF(MSVC)
3         # only MSVC uses SOURCE_GROUP
4         MACRO(BLENDERLIB_NOLIST
5                 name
6                 sources
7                 includes)
8
9                 MESSAGE(STATUS "Configuring library ${name}")
10
11                 # Gather all headers
12                 FILE(GLOB_RECURSE INC_ALL *.h)
13                          
14                 INCLUDE_DIRECTORIES(${includes})
15                 ADD_LIBRARY(${name} ${INC_ALL} ${sources})
16
17                 # Group by location on disk
18                 SOURCE_GROUP(Files FILES CMakeLists.txt)
19                 SET(ALL_FILES ${sources} ${INC_ALL})
20                 FOREACH(SRC ${ALL_FILES})
21                         STRING(REGEX REPLACE ${CMAKE_CURRENT_SOURCE_DIR} "Files" REL_DIR "${SRC}")
22                         STRING(REGEX REPLACE "[\\\\/][^\\\\/]*$" "" REL_DIR "${REL_DIR}")
23                         STRING(REGEX REPLACE "^[\\\\/]" "" REL_DIR "${REL_DIR}")
24                         IF(REL_DIR)
25                                 SOURCE_GROUP(${REL_DIR} FILES ${SRC})
26                         ELSE(REL_DIR)
27                                 SOURCE_GROUP(Files FILES ${SRC})
28                         ENDIF(REL_DIR)
29                 ENDFOREACH(SRC)
30         ENDMACRO(BLENDERLIB_NOLIST)
31 ELSE(MSVC)
32
33         MACRO(BLENDERLIB_NOLIST
34                 name
35                 sources
36                 includes)
37
38                 MESSAGE(STATUS "Configuring library ${name}")
39                 INCLUDE_DIRECTORIES(${includes})
40                 ADD_LIBRARY(${name} ${sources})
41         ENDMACRO(BLENDERLIB_NOLIST)
42 ENDIF(MSVC)
43
44 MACRO(BLENDERLIB
45         name
46         sources
47         includes)
48
49         BLENDERLIB_NOLIST(${name} "${sources}" "${includes}")
50
51         # Add to blender's list of libraries
52         FILE(APPEND ${CMAKE_BINARY_DIR}/cmake_blender_libs.txt "${name};")
53 ENDMACRO(BLENDERLIB)
54
55 MACRO(SETUP_LIBDIRS)
56         # see "cmake --help-policy CMP0003"
57         if(COMMAND cmake_policy)
58                 CMAKE_POLICY(SET CMP0003 NEW)
59         endif(COMMAND cmake_policy)
60         
61         LINK_DIRECTORIES(${JPEG_LIBPATH} ${PNG_LIBPATH} ${ZLIB_LIBPATH} ${FREETYPE_LIBPATH} ${LIBSAMPLERATE_LIBPATH})
62         
63         IF(WITH_PYTHON)
64                 LINK_DIRECTORIES(${PYTHON_LIBPATH})
65         ENDIF(WITH_PYTHON)
66         IF(WITH_INTERNATIONAL)
67                 LINK_DIRECTORIES(${ICONV_LIBPATH})
68                 LINK_DIRECTORIES(${GETTEXT_LIBPATH})
69         ENDIF(WITH_INTERNATIONAL)
70         IF(WITH_SDL)
71                 LINK_DIRECTORIES(${SDL_LIBPATH})
72         ENDIF(WITH_SDL)
73         IF(WITH_FFMPEG)
74                 LINK_DIRECTORIES(${FFMPEG_LIBPATH})
75         ENDIF(WITH_FFMPEG)
76         IF(WITH_IMAGE_OPENEXR)
77                 LINK_DIRECTORIES(${OPENEXR_LIBPATH})
78         ENDIF(WITH_IMAGE_OPENEXR)
79         IF(WITH_IMAGE_TIFF)
80                 LINK_DIRECTORIES(${TIFF_LIBPATH})
81         ENDIF(WITH_IMAGE_TIFF)
82         IF(WITH_LCMS)
83                 LINK_DIRECTORIES(${LCMS_LIBPATH})
84         ENDIF(WITH_LCMS)
85         IF(WITH_QUICKTIME)
86                 LINK_DIRECTORIES(${QUICKTIME_LIBPATH})
87         ENDIF(WITH_QUICKTIME)
88         IF(WITH_OPENAL)
89                 LINK_DIRECTORIES(${OPENAL_LIBPATH})
90         ENDIF(WITH_OPENAL)
91         IF(WITH_JACK)
92                 LINK_DIRECTORIES(${JACK_LIBPATH})
93         ENDIF(WITH_JACK)
94         IF(WITH_SNDFILE)
95                 LINK_DIRECTORIES(${SNDFILE_LIBPATH})
96         ENDIF(WITH_SNDFILE)
97         IF(WITH_FFTW3)
98                 LINK_DIRECTORIES(${FFTW3_LIBPATH})
99         ENDIF(WITH_FFTW3)
100         IF(WITH_OPENCOLLADA)
101                 LINK_DIRECTORIES(${OPENCOLLADA_LIBPATH})
102                 LINK_DIRECTORIES(${PCRE_LIBPATH})
103                 LINK_DIRECTORIES(${EXPAT_LIBPATH})
104         ENDIF(WITH_OPENCOLLADA)
105
106         IF(WIN32)
107                 LINK_DIRECTORIES(${PTHREADS_LIBPATH})
108         ENDIF(WIN32)
109 ENDMACRO(SETUP_LIBDIRS)
110
111 MACRO(SETUP_LIBLINKS
112         target)
113         SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS} ")
114
115         TARGET_LINK_LIBRARIES(${target} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY} ${JPEG_LIBRARY} ${PNG_LIBRARIES} ${ZLIB_LIBRARIES} ${LLIBS})
116
117         # since we are using the local libs for python when compiling msvc projects, we need to add _d when compiling debug versions
118         IF(WITH_PYTHON)
119                 TARGET_LINK_LIBRARIES(${target} ${PYTHON_LINKFLAGS})
120
121                 IF(WIN32)
122                         TARGET_LINK_LIBRARIES(${target} debug ${PYTHON_LIB}_d)
123                         TARGET_LINK_LIBRARIES(${target} optimized ${PYTHON_LIB})
124                 ELSE(WIN32)
125                         TARGET_LINK_LIBRARIES(${target} ${PYTHON_LIB})
126                 ENDIF(WIN32)
127         ENDIF(WITH_PYTHON)
128
129         TARGET_LINK_LIBRARIES(${target} ${OPENGL_glu_LIBRARY} ${JPEG_LIB} ${PNG_LIB} ${ZLIB_LIB})
130         TARGET_LINK_LIBRARIES(${target} ${FREETYPE_LIBRARY} ${LIBSAMPLERATE_LIB})
131
132         IF(WITH_INTERNATIONAL)
133                 TARGET_LINK_LIBRARIES(${target} ${GETTEXT_LIB})
134
135                 IF(WIN32)
136                         TARGET_LINK_LIBRARIES(${target} ${ICONV_LIB})
137                 ENDIF(WIN32)
138         ENDIF(WITH_INTERNATIONAL)
139
140         IF(WITH_OPENAL)
141                 TARGET_LINK_LIBRARIES(${target} ${OPENAL_LIBRARY})
142         ENDIF(WITH_OPENAL)
143         IF(WITH_FFTW3)  
144                 TARGET_LINK_LIBRARIES(${target} ${FFTW3_LIB})
145         ENDIF(WITH_FFTW3)
146         IF(WITH_JACK)
147                 TARGET_LINK_LIBRARIES(${target} ${JACK_LIB})
148         ENDIF(WITH_JACK)
149         IF(WITH_SNDFILE)
150                 TARGET_LINK_LIBRARIES(${target} ${SNDFILE_LIB})
151         ENDIF(WITH_SNDFILE)
152         IF(WITH_SDL)
153                 TARGET_LINK_LIBRARIES(${target} ${SDL_LIBRARY})
154         ENDIF(WITH_SDL)
155         IF(WITH_QUICKTIME)
156                 TARGET_LINK_LIBRARIES(${target} ${QUICKTIME_LIB})
157         ENDIF(WITH_QUICKTIME)
158         IF(WITH_IMAGE_TIFF)
159                 TARGET_LINK_LIBRARIES(${target} ${TIFF_LIBRARY})
160         ENDIF(WITH_IMAGE_TIFF)
161         IF(WITH_IMAGE_OPENEXR)
162                 IF(WIN32)
163                         FOREACH(loop_var ${OPENEXR_LIB})
164                                 TARGET_LINK_LIBRARIES(${target} debug ${loop_var}_d)
165                                 TARGET_LINK_LIBRARIES(${target} optimized ${loop_var})
166                         ENDFOREACH(loop_var)
167                 ELSE(WIN32)
168                         TARGET_LINK_LIBRARIES(${target} ${OPENEXR_LIB})
169                 ENDIF(WIN32)
170         ENDIF(WITH_IMAGE_OPENEXR)
171         IF(WITH_LCMS)
172                 TARGET_LINK_LIBRARIES(${target} ${LCMS_LIBRARY})
173         ENDIF(WITH_LCMS)
174         IF(WITH_FFMPEG)
175                 TARGET_LINK_LIBRARIES(${target} ${FFMPEG_LIB})
176         ENDIF(WITH_FFMPEG)
177         IF(WITH_OPENCOLLADA)
178                 IF(WIN32)
179                         FOREACH(loop_var ${OPENCOLLADA_LIB})
180                                 TARGET_LINK_LIBRARIES(${target} debug ${loop_var}_d)
181                                 TARGET_LINK_LIBRARIES(${target} optimized ${loop_var})
182                         ENDFOREACH(loop_var)
183                         TARGET_LINK_LIBRARIES(${target} debug ${PCRE_LIB}_d)
184                         TARGET_LINK_LIBRARIES(${target} optimized ${PCRE_LIB})
185                         IF(EXPAT_LIB)
186                                 TARGET_LINK_LIBRARIES(${target} debug ${EXPAT_LIB}_d)
187                                 TARGET_LINK_LIBRARIES(${target} optimized ${EXPAT_LIB})
188                         ENDIF(EXPAT_LIB)
189                 ELSE(WIN32)
190                         TARGET_LINK_LIBRARIES(${target} ${OPENCOLLADA_LIB})
191                         TARGET_LINK_LIBRARIES(${target} ${PCRE_LIB})
192                         TARGET_LINK_LIBRARIES(${target} ${EXPAT_LIB})
193                 ENDIF(WIN32)
194         ENDIF(WITH_OPENCOLLADA)
195         IF(WITH_LCMS)
196                 IF(WIN32)
197                         TARGET_LINK_LIBRARIES(${target} debug ${LCMS_LIB}_d)
198                         TARGET_LINK_LIBRARIES(${target} optimized ${LCMS_LIB})
199                 ENDIF(WIN32)
200         ENDIF(WITH_LCMS)
201         IF(WIN32)
202                 TARGET_LINK_LIBRARIES(${target} ${PTHREADS_LIB})
203         ENDIF(WIN32)
204 ENDMACRO(SETUP_LIBLINKS)
205
206 MACRO(TEST_SSE_SUPPORT)
207         INCLUDE(CheckCSourceRuns)
208
209         MESSAGE(STATUS "Detecting SSE support")
210         IF(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
211                 SET(CMAKE_REQUIRED_FLAGS "-msse -msse2")
212         ELSEIF(MSVC)
213                 SET(CMAKE_REQUIRED_FLAGS "/arch:SSE2") # TODO, SSE 1 ?
214         ENDIF()
215
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         CHECK_C_SOURCE_RUNS("
222                 #include <emmintrin.h>
223                 int main() { __m128d v = _mm_setzero_pd(); return 0; }"
224         SUPPORT_SSE2_BUILD)
225         MESSAGE(STATUS "Detecting SSE support")
226
227         IF(SUPPORT_SSE_BUILD)
228                 MESSAGE(STATUS "   ...SSE support found.")
229         ELSE(SUPPORT_SSE_BUILD)
230                 MESSAGE(STATUS "   ...SSE support missing.")
231         ENDIF(SUPPORT_SSE_BUILD)
232
233         IF(SUPPORT_SSE2_BUILD)
234                 MESSAGE(STATUS "   ...SSE2 support found.")
235         ELSE(SUPPORT_SSE2_BUILD)
236                 MESSAGE(STATUS "   ...SSE2 support missing.")
237         ENDIF(SUPPORT_SSE2_BUILD)
238
239 ENDMACRO(TEST_SSE_SUPPORT)
240
241
242 MACRO(GET_BLENDER_VERSION)
243         FILE(READ ${CMAKE_SOURCE_DIR}/source/blender/blenkernel/BKE_blender.h CONTENT)
244         STRING(REGEX REPLACE "\n" ";" CONTENT "${CONTENT}")
245         STRING(REGEX REPLACE "\t" ";" CONTENT "${CONTENT}")
246         STRING(REGEX REPLACE " " ";" CONTENT "${CONTENT}")
247
248         FOREACH(ITEM ${CONTENT})
249                 IF(LASTITEM MATCHES "BLENDER_VERSION")
250                         MATH(EXPR BLENDER_VERSION_MAJOR "${ITEM} / 100")
251                         MATH(EXPR BLENDER_VERSION_MINOR "${ITEM} % 100")
252                         SET(BLENDER_VERSION "${BLENDER_VERSION_MAJOR}.${BLENDER_VERSION_MINOR}")
253                 ENDIF(LASTITEM MATCHES "BLENDER_VERSION")
254                 
255                 IF(LASTITEM MATCHES "BLENDER_SUBVERSION")
256                         SET(BLENDER_SUBVERSION ${ITEM})
257                 ENDIF(LASTITEM MATCHES "BLENDER_SUBVERSION")
258                 
259                 IF(LASTITEM MATCHES "BLENDER_MINVERSION")
260                         MATH(EXPR BLENDER_MINVERSION_MAJOR "${ITEM} / 100")
261                         MATH(EXPR BLENDER_MINVERSION_MINOR "${ITEM} % 100")
262                         SET(BLENDER_MINVERSION "${BLENDER_MINVERSION_MAJOR}.${BLENDER_MINVERSION_MINOR}")
263                 ENDIF(LASTITEM MATCHES "BLENDER_MINVERSION")
264                 
265                 IF(LASTITEM MATCHES "BLENDER_MINSUBVERSION")
266                         SET(BLENDER_MINSUBVERSION ${ITEM})
267                 ENDIF(LASTITEM MATCHES "BLENDER_MINSUBVERSION")
268
269                 SET(LASTITEM ${ITEM})
270         ENDFOREACH(ITEM ${CONTENT})
271         
272         MESSAGE(STATUS "Version major: ${BLENDER_VERSION_MAJOR}, Version minor: ${BLENDER_VERSION_MINOR}, Subversion: ${BLENDER_SUBVERSION}, Version: ${BLENDER_VERSION}")
273         MESSAGE(STATUS "Minversion major: ${BLENDER_MINVERSION_MAJOR}, Minversion minor: ${BLENDER_MINVERSION_MINOR}, MinSubversion: ${BLENDER_MINSUBVERSION}, Minversion: ${BLENDER_MINVERSION}")
274 ENDMACRO(GET_BLENDER_VERSION)