Fixup for fix for OSX build using a build folder name with spaces
[blender.git] / build_files / cmake / platform / platform_apple.cmake
1 # ***** BEGIN GPL LICENSE BLOCK *****
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # The Original Code is Copyright (C) 2016, Blender Foundation
18 # All rights reserved.
19 #
20 # Contributor(s): Sergey Sharybin.
21 #
22 # ***** END GPL LICENSE BLOCK *****
23
24 # Libraries configuration for Apple.
25
26 macro(find_package_wrapper)
27 # do nothing, just satisfy the macro
28 endmacro()
29
30 if(NOT DEFINED LIBDIR)
31         set(LIBDIR ${CMAKE_SOURCE_DIR}/../lib/darwin)
32         # Prefer lib directory paths
33         file(GLOB LIB_SUBDIRS ${LIBDIR}/*)
34         set(CMAKE_PREFIX_PATH ${LIB_SUBDIRS})
35 else()
36         message(STATUS "Using pre-compiled LIBDIR: ${LIBDIR}")
37 endif()
38 if(NOT EXISTS "${LIBDIR}/")
39         message(FATAL_ERROR "Mac OSX requires pre-compiled libs at: '${LIBDIR}'")
40 endif()
41
42 if(WITH_OPENAL)
43         find_package(OpenAL)
44         if(OPENAL_FOUND)
45                 set(WITH_OPENAL ON)
46         else()
47                 set(WITH_OPENAL OFF)
48         endif()
49 endif()
50
51 if(WITH_ALEMBIC)
52         set(ALEMBIC ${LIBDIR}/alembic)
53         set(ALEMBIC_INCLUDE_DIR ${ALEMBIC}/include)
54         set(ALEMBIC_INCLUDE_DIRS ${ALEMBIC_INCLUDE_DIR})
55         set(ALEMBIC_LIBPATH ${ALEMBIC}/lib)
56         set(ALEMBIC_LIBRARIES Alembic)
57         set(ALEMBIC_FOUND ON)
58 endif()
59
60 if(WITH_OPENSUBDIV)
61         set(OPENSUBDIV ${LIBDIR}/opensubdiv)
62         set(OPENSUBDIV_LIBPATH ${OPENSUBDIV}/lib)
63         find_library(OSD_LIB_CPU NAMES osdCPU PATHS ${OPENSUBDIV_LIBPATH})
64         find_library(OSD_LIB_GPU NAMES osdGPU PATHS ${OPENSUBDIV_LIBPATH})
65         set(OPENSUBDIV_INCLUDE_DIR ${OPENSUBDIV}/include)
66         set(OPENSUBDIV_INCLUDE_DIRS ${OPENSUBDIV_INCLUDE_DIR})
67         list(APPEND OPENSUBDIV_LIBRARIES ${OSD_LIB_CPU} ${OSD_LIB_GPU})
68 endif()
69
70 if(WITH_JACK)
71         find_library(JACK_FRAMEWORK
72                 NAMES jackmp
73         )
74         set(JACK_INCLUDE_DIRS ${JACK_FRAMEWORK}/headers)
75         if(NOT JACK_FRAMEWORK)
76                 set(WITH_JACK OFF)
77         endif()
78 endif()
79
80 if(WITH_CODEC_SNDFILE)
81         set(SNDFILE ${LIBDIR}/sndfile)
82         set(SNDFILE_INCLUDE_DIRS ${SNDFILE}/include)
83         set(SNDFILE_LIBRARIES sndfile FLAC ogg vorbis vorbisenc)
84         set(SNDFILE_LIBPATH ${SNDFILE}/lib ${LIBDIR}/ffmpeg/lib)  # TODO, deprecate
85 endif()
86
87 if(WITH_PYTHON)
88         # we use precompiled libraries for py 3.7 and up by default
89         set(PYTHON_VERSION 3.7)
90         if(NOT WITH_PYTHON_MODULE AND NOT WITH_PYTHON_FRAMEWORK)
91                 # normally cached but not since we include them with blender
92                 set(PYTHON_INCLUDE_DIR "${LIBDIR}/python/include/python${PYTHON_VERSION}m")
93                 set(PYTHON_EXECUTABLE "${LIBDIR}/python/bin/python${PYTHON_VERSION}m")
94                 set(PYTHON_LIBRARY ${LIBDIR}/python/lib/libpython${PYTHON_VERSION}m.a)
95                 set(PYTHON_LIBPATH "${LIBDIR}/python/lib/python${PYTHON_VERSION}")
96                 # set(PYTHON_LINKFLAGS "-u _PyMac_Error")  # won't  build with this enabled
97         else()
98                 # module must be compiled against Python framework
99                 set(_py_framework "/Library/Frameworks/Python.framework/Versions/${PYTHON_VERSION}")
100
101                 set(PYTHON_INCLUDE_DIR "${_py_framework}/include/python${PYTHON_VERSION}m")
102                 set(PYTHON_EXECUTABLE "${_py_framework}/bin/python${PYTHON_VERSION}m")
103                 set(PYTHON_LIBPATH "${_py_framework}/lib/python${PYTHON_VERSION}/config-${PYTHON_VERSION}m")
104                 #set(PYTHON_LIBRARY python${PYTHON_VERSION})
105                 #set(PYTHON_LINKFLAGS "-u _PyMac_Error -framework Python")  # won't  build with this enabled
106
107                 unset(_py_framework)
108         endif()
109
110         # uncached vars
111         set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
112         set(PYTHON_LIBRARIES  "${PYTHON_LIBRARY}")
113
114         # needed for Audaspace, numpy is installed into python site-packages
115         set(NUMPY_INCLUDE_DIRS "${PYTHON_LIBPATH}/site-packages/numpy/core/include")
116
117         if(NOT EXISTS "${PYTHON_EXECUTABLE}")
118                 message(FATAL_ERROR "Python executable missing: ${PYTHON_EXECUTABLE}")
119         endif()
120 endif()
121
122 if(WITH_FFTW3)
123         set(FFTW3 ${LIBDIR}/fftw3)
124         set(FFTW3_INCLUDE_DIRS ${FFTW3}/include)
125         set(FFTW3_LIBRARIES fftw3)
126         set(FFTW3_LIBPATH ${FFTW3}/lib)
127 endif()
128
129 set(PNG_LIBRARIES png)
130 set(JPEG_LIBRARIES jpeg)
131
132 set(ZLIB /usr)
133 set(ZLIB_INCLUDE_DIRS "${ZLIB}/include")
134 set(ZLIB_LIBRARIES z bz2)
135
136 set(FREETYPE ${LIBDIR}/freetype)
137 set(FREETYPE_INCLUDE_DIRS ${FREETYPE}/include ${FREETYPE}/include/freetype2)
138 set(FREETYPE_LIBPATH ${FREETYPE}/lib)
139 set(FREETYPE_LIBRARY freetype)
140
141 if(WITH_IMAGE_OPENEXR)
142         set(OPENEXR ${LIBDIR}/openexr)
143         set(OPENEXR_INCLUDE_DIR ${OPENEXR}/include)
144         set(OPENEXR_INCLUDE_DIRS ${OPENEXR_INCLUDE_DIR} ${OPENEXR}/include/OpenEXR)
145         set(OPENEXR_LIBRARIES
146                 Iex
147                 Half
148                 IlmImf
149                 Imath
150                 IlmThread)
151         set(OPENEXR_LIBPATH ${OPENEXR}/lib)
152 endif()
153
154 if(WITH_CODEC_FFMPEG)
155         set(FFMPEG ${LIBDIR}/ffmpeg)
156         set(FFMPEG_INCLUDE_DIRS ${FFMPEG}/include)
157         set(FFMPEG_LIBRARIES
158                 avcodec avdevice avformat avutil
159                 mp3lame swscale x264 xvidcore
160                 theora theoradec theoraenc
161                 vorbis vorbisenc vorbisfile ogg
162                 vpx swresample)
163         set(FFMPEG_LIBPATH ${FFMPEG}/lib)
164 endif()
165
166 if(WITH_IMAGE_OPENJPEG OR WITH_CODEC_FFMPEG)
167         # use openjpeg from libdir that is linked into ffmpeg
168         set(OPENJPEG ${LIBDIR}/openjpeg)
169         set(OPENJPEG_INCLUDE_DIRS ${OPENJPEG}/include)
170         set(OPENJPEG_LIBRARIES ${OPENJPEG}/lib/libopenjp2.a)
171 endif()
172
173 find_library(SYSTEMSTUBS_LIBRARY
174         NAMES
175         SystemStubs
176         PATHS
177 )
178 mark_as_advanced(SYSTEMSTUBS_LIBRARY)
179 if(SYSTEMSTUBS_LIBRARY)
180         list(APPEND PLATFORM_LINKLIBS SystemStubs)
181 endif()
182
183 set(PLATFORM_CFLAGS "-pipe -funsigned-char")
184 set(PLATFORM_LINKFLAGS
185         "-fexceptions -framework CoreServices -framework Foundation -framework IOKit -framework AppKit -framework Cocoa -framework Carbon -framework AudioUnit -framework AudioToolbox -framework CoreAudio"
186 )
187
188 list(APPEND PLATFORM_LINKLIBS c++)
189
190 if(WITH_JACK)
191         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -F/Library/Frameworks -weak_framework jackmp")
192 endif()
193
194 if(WITH_PYTHON_MODULE OR WITH_PYTHON_FRAMEWORK)
195         # force cmake to link right framework
196         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} /Library/Frameworks/Python.framework/Versions/${PYTHON_VERSION}/Python")
197 endif()
198
199 if(WITH_OPENCOLLADA)
200         set(OPENCOLLADA ${LIBDIR}/opencollada)
201
202         set(OPENCOLLADA_INCLUDE_DIRS
203                 ${LIBDIR}/opencollada/include/COLLADAStreamWriter
204                 ${LIBDIR}/opencollada/include/COLLADABaseUtils
205                 ${LIBDIR}/opencollada/include/COLLADAFramework
206                 ${LIBDIR}/opencollada/include/COLLADASaxFrameworkLoader
207                 ${LIBDIR}/opencollada/include/GeneratedSaxParser
208         )
209
210         set(OPENCOLLADA_LIBPATH ${OPENCOLLADA}/lib)
211         set(OPENCOLLADA_LIBRARIES
212                 OpenCOLLADASaxFrameworkLoader
213                 -lOpenCOLLADAFramework
214                 -lOpenCOLLADABaseUtils
215                 -lOpenCOLLADAStreamWriter
216                 -lMathMLSolver
217                 -lGeneratedSaxParser
218                 -lbuffer -lftoa -lUTF
219                 ${OPENCOLLADA_LIBPATH}/libxml2.a
220         )
221         # PCRE is bundled with openCollada
222         #set(PCRE ${LIBDIR}/pcre)
223         #set(PCRE_LIBPATH ${PCRE}/lib)
224         set(PCRE_LIBRARIES pcre)
225         # libxml2 is used
226         #set(EXPAT ${LIBDIR}/expat)
227         #set(EXPAT_LIBPATH ${EXPAT}/lib)
228         set(EXPAT_LIB)
229 endif()
230
231 if(WITH_SDL)
232         set(SDL ${LIBDIR}/sdl)
233         set(SDL_INCLUDE_DIR ${SDL}/include)
234         set(SDL_LIBRARY SDL2)
235         set(SDL_LIBPATH ${SDL}/lib)
236         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -framework ForceFeedback")
237 endif()
238
239 set(PNG "${LIBDIR}/png")
240 set(PNG_INCLUDE_DIRS "${PNG}/include")
241 set(PNG_LIBPATH ${PNG}/lib)
242
243 set(JPEG "${LIBDIR}/jpeg")
244 set(JPEG_INCLUDE_DIR "${JPEG}/include")
245 set(JPEG_LIBPATH ${JPEG}/lib)
246
247 if(WITH_IMAGE_TIFF)
248         set(TIFF ${LIBDIR}/tiff)
249         set(TIFF_INCLUDE_DIR ${TIFF}/include)
250         set(TIFF_LIBRARY tiff)
251         set(TIFF_LIBPATH ${TIFF}/lib)
252 endif()
253
254 if(WITH_BOOST)
255         set(BOOST ${LIBDIR}/boost)
256         set(BOOST_INCLUDE_DIR ${BOOST}/include)
257         set(BOOST_POSTFIX)
258         set(BOOST_LIBRARIES
259                 boost_date_time${BOOST_POSTFIX}
260                 boost_filesystem${BOOST_POSTFIX}
261                 boost_regex${BOOST_POSTFIX}
262                 boost_system${BOOST_POSTFIX}
263                 boost_thread${BOOST_POSTFIX}
264                 boost_wave${BOOST_POSTFIX}
265         )
266         if(WITH_INTERNATIONAL)
267                 list(APPEND BOOST_LIBRARIES boost_locale${BOOST_POSTFIX})
268         endif()
269         if(WITH_CYCLES_NETWORK)
270                 list(APPEND BOOST_LIBRARIES boost_serialization${BOOST_POSTFIX})
271         endif()
272         if(WITH_OPENVDB)
273                 list(APPEND BOOST_LIBRARIES boost_iostreams${BOOST_POSTFIX})
274         endif()
275         set(BOOST_LIBPATH ${BOOST}/lib)
276         set(BOOST_DEFINITIONS)
277 endif()
278
279 if(WITH_INTERNATIONAL OR WITH_CODEC_FFMPEG)
280         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -liconv") # boost_locale and ffmpeg needs it !
281 endif()
282
283 if(WITH_OPENIMAGEIO)
284         set(OPENIMAGEIO ${LIBDIR}/openimageio)
285         set(OPENIMAGEIO_INCLUDE_DIRS ${OPENIMAGEIO}/include)
286         set(OPENIMAGEIO_LIBRARIES
287                 ${OPENIMAGEIO}/lib/libOpenImageIO.a
288                 ${PNG_LIBRARIES}
289                 ${JPEG_LIBRARIES}
290                 ${TIFF_LIBRARY}
291                 ${OPENEXR_LIBRARIES}
292                 ${OPENJPEG_LIBRARIES}
293                 ${ZLIB_LIBRARIES}
294         )
295         set(OPENIMAGEIO_LIBPATH
296                 ${OPENIMAGEIO}/lib
297                 ${JPEG_LIBPATH}
298                 ${PNG_LIBPATH}
299                 ${TIFF_LIBPATH}
300                 ${OPENEXR_LIBPATH}
301                 ${ZLIB_LIBPATH}
302         )
303         set(OPENIMAGEIO_DEFINITIONS "-DOIIO_STATIC_BUILD")
304         set(OPENIMAGEIO_IDIFF "${LIBDIR}/openimageio/bin/idiff")
305 endif()
306
307 if(WITH_OPENCOLORIO)
308         set(OPENCOLORIO ${LIBDIR}/opencolorio)
309         set(OPENCOLORIO_INCLUDE_DIRS ${OPENCOLORIO}/include)
310         set(OPENCOLORIO_LIBRARIES OpenColorIO tinyxml yaml-cpp)
311         set(OPENCOLORIO_LIBPATH ${OPENCOLORIO}/lib)
312 endif()
313
314 if(WITH_OPENVDB)
315         set(OPENVDB ${LIBDIR}/openvdb)
316         set(OPENVDB_INCLUDE_DIRS ${OPENVDB}/include)
317         set(TBB_INCLUDE_DIRS ${LIBDIR}/tbb/include)
318         set(TBB_LIBRARIES ${LIBDIR}/tbb/lib/libtbb.a)
319         set(OPENVDB_LIBRARIES openvdb blosc ${TBB_LIBRARIES})
320         set(OPENVDB_LIBPATH ${LIBDIR}/openvdb/lib)
321         set(OPENVDB_DEFINITIONS)
322 endif()
323
324 if(WITH_LLVM)
325         set(LLVM_ROOT_DIR ${LIBDIR}/llvm)
326         set(LLVM_VERSION 3.4)
327         if(EXISTS "${LLVM_ROOT_DIR}/bin/llvm-config")
328                 set(LLVM_CONFIG "${LLVM_ROOT_DIR}/bin/llvm-config")
329         else()
330                 set(LLVM_CONFIG llvm-config)
331         endif()
332         execute_process(COMMAND ${LLVM_CONFIG} --version
333                         OUTPUT_VARIABLE LLVM_VERSION
334                         OUTPUT_STRIP_TRAILING_WHITESPACE)
335         execute_process(COMMAND ${LLVM_CONFIG} --prefix
336                         OUTPUT_VARIABLE LLVM_ROOT_DIR
337                         OUTPUT_STRIP_TRAILING_WHITESPACE)
338         execute_process(COMMAND ${LLVM_CONFIG} --libdir
339                         OUTPUT_VARIABLE LLVM_LIBPATH
340                         OUTPUT_STRIP_TRAILING_WHITESPACE)
341         find_library(LLVM_LIBRARY
342                       NAMES LLVMAnalysis # first of a whole bunch of libs to get
343                       PATHS ${LLVM_LIBPATH})
344
345         if(LLVM_LIBRARY AND LLVM_ROOT_DIR AND LLVM_LIBPATH)
346                 if(LLVM_STATIC)
347                         # if static LLVM libraries were requested, use llvm-config to generate
348                         # the list of what libraries we need, and substitute that in the right
349                         # way for LLVM_LIBRARY.
350                         execute_process(COMMAND ${LLVM_CONFIG} --libfiles
351                                         OUTPUT_VARIABLE LLVM_LIBRARY
352                                         OUTPUT_STRIP_TRAILING_WHITESPACE)
353                         string(REPLACE ".a /" ".a;/" LLVM_LIBRARY ${LLVM_LIBRARY})
354                 else()
355                         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -lLLVM-3.4")
356                 endif()
357         else()
358                 message(FATAL_ERROR "LLVM not found.")
359         endif()
360 endif()
361
362 if(WITH_CYCLES_OSL)
363         set(CYCLES_OSL ${LIBDIR}/osl)
364
365         find_library(OSL_LIB_EXEC NAMES oslexec PATHS ${CYCLES_OSL}/lib)
366         find_library(OSL_LIB_COMP NAMES oslcomp PATHS ${CYCLES_OSL}/lib)
367         find_library(OSL_LIB_QUERY NAMES oslquery PATHS ${CYCLES_OSL}/lib)
368         # WARNING! depends on correct order of OSL libs linking
369         list(APPEND OSL_LIBRARIES ${OSL_LIB_COMP} -force_load ${OSL_LIB_EXEC} ${OSL_LIB_QUERY})
370         find_path(OSL_INCLUDE_DIR OSL/oslclosure.h PATHS ${CYCLES_OSL}/include)
371         find_program(OSL_COMPILER NAMES oslc PATHS ${CYCLES_OSL}/bin)
372
373         if(OSL_INCLUDE_DIR AND OSL_LIBRARIES AND OSL_COMPILER)
374                 set(OSL_FOUND TRUE)
375         else()
376                 message(STATUS "OSL not found")
377                 set(WITH_CYCLES_OSL OFF)
378         endif()
379 endif()
380
381 if(WITH_CYCLES_EMBREE)
382         find_package(Embree 3.2.4 REQUIRED)
383         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -Xlinker -stack_size -Xlinker 0x100000")
384 endif()
385
386 # CMake FindOpenMP doesn't know about AppleClang before 3.12, so provide custom flags.
387 if(WITH_OPENMP)
388         if(CMAKE_C_COMPILER_ID MATCHES "AppleClang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0")
389                 # Use OpenMP from our precompiled libraries.
390                 message(STATUS "Using ${LIBDIR}/openmp for OpenMP")
391                 set(OPENMP_CUSTOM ON)
392                 set(OPENMP_FOUND ON)
393                 set(OpenMP_C_FLAGS "-Xclang -fopenmp -I'${LIBDIR}/openmp/include'")
394                 set(OpenMP_CXX_FLAGS "-Xclang -fopenmp -I'${LIBDIR}/openmp/include'")
395                 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L'${LIBDIR}/openmp/lib' -lomp")
396
397                 # Copy libomp.dylib to allow executables like datatoc to work.
398                 execute_process(
399                         COMMAND mkdir -p ${CMAKE_BINARY_DIR}/Resources/lib
400                         COMMAND cp -p ${LIBDIR}/openmp/lib/libomp.dylib ${CMAKE_BINARY_DIR}/Resources/lib/libomp.dylib
401                 )
402         endif()
403 endif()
404
405 set(EXETYPE MACOSX_BUNDLE)
406
407 set(CMAKE_C_FLAGS_DEBUG "-fno-strict-aliasing -g")
408 set(CMAKE_CXX_FLAGS_DEBUG "-fno-strict-aliasing -g")
409 if(CMAKE_OSX_ARCHITECTURES MATCHES "x86_64" OR CMAKE_OSX_ARCHITECTURES MATCHES "i386")
410         set(CMAKE_CXX_FLAGS_RELEASE "-O2 -mdynamic-no-pic -msse -msse2 -msse3 -mssse3")
411         set(CMAKE_C_FLAGS_RELEASE "-O2 -mdynamic-no-pic  -msse -msse2 -msse3 -mssse3")
412         if(NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
413                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -ftree-vectorize  -fvariable-expansion-in-unroller")
414                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -ftree-vectorize  -fvariable-expansion-in-unroller")
415         endif()
416 else()
417         set(CMAKE_C_FLAGS_RELEASE "-mdynamic-no-pic -fno-strict-aliasing")
418         set(CMAKE_CXX_FLAGS_RELEASE "-mdynamic-no-pic -fno-strict-aliasing")
419 endif()
420
421 if(${XCODE_VERSION} VERSION_EQUAL 5 OR ${XCODE_VERSION} VERSION_GREATER 5)
422         # Xcode 5 is always using CLANG, which has too low template depth of 128 for libmv
423         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftemplate-depth=1024")
424 endif()
425 # Get rid of eventually clashes, we export some symbols explicitly as local
426 set(PLATFORM_LINKFLAGS
427         "${PLATFORM_LINKFLAGS} -Xlinker -unexported_symbols_list -Xlinker '${CMAKE_SOURCE_DIR}/source/creator/osx_locals.map'"
428 )
429
430 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
431 set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -stdlib=libc++")
432
433 # Suppress ranlib "has no symbols" warnings (workaround for T48250)
434 set(CMAKE_C_ARCHIVE_CREATE   "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
435 set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
436 set(CMAKE_C_ARCHIVE_FINISH   "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
437 set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")