CMake: decouple WITH_CYCLES_OPENSUBDIV from WITH_OPENSUBDIV, and enable on OS X.
[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 if(NOT DEFINED LIBDIR)
27         set(LIBDIR ${CMAKE_SOURCE_DIR}/../lib/darwin-9.x.universal)
28 else()
29         message(STATUS "Using pre-compiled LIBDIR: ${LIBDIR}")
30 endif()
31 if(NOT EXISTS "${LIBDIR}/")
32         message(FATAL_ERROR "Mac OSX requires pre-compiled libs at: '${LIBDIR}'")
33 endif()
34
35 if(WITH_OPENAL)
36         find_package(OpenAL)
37         if(OPENAL_FOUND)
38                 set(WITH_OPENAL ON)
39                 set(OPENAL_INCLUDE_DIR "${LIBDIR}/openal/include")
40         else()
41                 set(WITH_OPENAL OFF)
42         endif()
43 endif()
44
45 if(WITH_ALEMBIC)
46         set(ALEMBIC ${LIBDIR}/alembic)
47         set(ALEMBIC_INCLUDE_DIR ${ALEMBIC}/include)
48         set(ALEMBIC_INCLUDE_DIRS ${ALEMBIC_INCLUDE_DIR})
49         set(ALEMBIC_LIBPATH ${ALEMBIC}/lib)
50         set(ALEMBIC_LIBRARIES Alembic)
51 endif()
52
53 if(WITH_OPENSUBDIV OR WITH_CYCLES_OPENSUBDIV)
54         set(OPENSUBDIV ${LIBDIR}/opensubdiv)
55         set(OPENSUBDIV_LIBPATH ${OPENSUBDIV}/lib)
56         find_library(OSD_LIB_CPU NAMES osdCPU PATHS ${OPENSUBDIV_LIBPATH})
57         find_library(OSD_LIB_GPU NAMES osdGPU PATHS ${OPENSUBDIV_LIBPATH})
58         set(OPENSUBDIV_INCLUDE_DIR ${OPENSUBDIV}/include)
59         set(OPENSUBDIV_INCLUDE_DIRS ${OPENSUBDIV_INCLUDE_DIR})
60         list(APPEND OPENSUBDIV_LIBRARIES ${OSD_LIB_CPU} ${OSD_LIB_GPU})
61 endif()
62
63 if(WITH_JACK)
64         find_library(JACK_FRAMEWORK
65                 NAMES jackmp
66         )
67         set(JACK_INCLUDE_DIRS ${JACK_FRAMEWORK}/headers)
68         if(NOT JACK_FRAMEWORK)
69                 set(WITH_JACK OFF)
70         endif()
71 endif()
72
73 if(WITH_CODEC_SNDFILE)
74         set(SNDFILE ${LIBDIR}/sndfile)
75         set(SNDFILE_INCLUDE_DIRS ${SNDFILE}/include)
76         set(SNDFILE_LIBRARIES sndfile FLAC ogg vorbis vorbisenc)
77         set(SNDFILE_LIBPATH ${SNDFILE}/lib ${FFMPEG}/lib)  # TODO, deprecate
78 endif()
79
80 if(WITH_PYTHON)
81         # we use precompiled libraries for py 3.5 and up by default
82         set(PYTHON_VERSION 3.5)
83         if(NOT WITH_PYTHON_MODULE AND NOT WITH_PYTHON_FRAMEWORK)
84                 # normally cached but not since we include them with blender
85                 set(PYTHON_INCLUDE_DIR "${LIBDIR}/python/include/python${PYTHON_VERSION}m")
86                 set(PYTHON_EXECUTABLE "${LIBDIR}/python/bin/python${PYTHON_VERSION}m")
87                 set(PYTHON_LIBRARY python${PYTHON_VERSION}m)
88                 set(PYTHON_LIBPATH "${LIBDIR}/python/lib/python${PYTHON_VERSION}")
89                 # set(PYTHON_LINKFLAGS "-u _PyMac_Error")  # won't  build with this enabled
90         else()
91                 # module must be compiled against Python framework
92                 set(_py_framework "/Library/Frameworks/Python.framework/Versions/${PYTHON_VERSION}")
93
94                 set(PYTHON_INCLUDE_DIR "${_py_framework}/include/python${PYTHON_VERSION}m")
95                 set(PYTHON_EXECUTABLE "${_py_framework}/bin/python${PYTHON_VERSION}m")
96                 set(PYTHON_LIBPATH "${_py_framework}/lib/python${PYTHON_VERSION}/config-${PYTHON_VERSION}m")
97                 #set(PYTHON_LIBRARY python${PYTHON_VERSION})
98                 #set(PYTHON_LINKFLAGS "-u _PyMac_Error -framework Python")  # won't  build with this enabled
99
100                 unset(_py_framework)
101         endif()
102
103         # uncached vars
104         set(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
105         set(PYTHON_LIBRARIES  "${PYTHON_LIBRARY}")
106
107         if(NOT EXISTS "${PYTHON_EXECUTABLE}")
108                 message(FATAL_ERROR "Python executable missing: ${PYTHON_EXECUTABLE}")
109         endif()
110 endif()
111
112 if(WITH_FFTW3)
113         set(FFTW3 ${LIBDIR}/fftw3)
114         set(FFTW3_INCLUDE_DIRS ${FFTW3}/include)
115         set(FFTW3_LIBRARIES fftw3)
116         set(FFTW3_LIBPATH ${FFTW3}/lib)
117 endif()
118
119 set(PNG_LIBRARIES png)
120 set(JPEG_LIBRARIES jpeg)
121
122 set(ZLIB /usr)
123 set(ZLIB_INCLUDE_DIRS "${ZLIB}/include")
124 set(ZLIB_LIBRARIES z bz2)
125
126 set(FREETYPE ${LIBDIR}/freetype)
127 set(FREETYPE_INCLUDE_DIRS ${FREETYPE}/include ${FREETYPE}/include/freetype2)
128 set(FREETYPE_LIBPATH ${FREETYPE}/lib)
129 set(FREETYPE_LIBRARY freetype)
130
131 if(WITH_IMAGE_OPENEXR)
132         set(OPENEXR ${LIBDIR}/openexr)
133         set(OPENEXR_INCLUDE_DIR ${OPENEXR}/include)
134         set(OPENEXR_INCLUDE_DIRS ${OPENEXR_INCLUDE_DIR} ${OPENEXR}/include/OpenEXR)
135         set(OPENEXR_LIBRARIES Iex Half IlmImf Imath IlmThread)
136         set(OPENEXR_LIBPATH ${OPENEXR}/lib)
137 endif()
138
139 if(WITH_CODEC_FFMPEG)
140         set(FFMPEG ${LIBDIR}/ffmpeg)
141         set(FFMPEG_INCLUDE_DIRS ${FFMPEG}/include)
142         set(FFMPEG_LIBRARIES
143                 avcodec avdevice avformat avutil
144                 mp3lame swscale x264 xvidcore theora theoradec theoraenc vorbis vorbisenc vorbisfile ogg
145         )
146         set(FFMPEG_LIBPATH ${FFMPEG}/lib)
147 endif()
148
149 find_library(SYSTEMSTUBS_LIBRARY
150         NAMES
151         SystemStubs
152         PATHS
153 )
154 mark_as_advanced(SYSTEMSTUBS_LIBRARY)
155 if(SYSTEMSTUBS_LIBRARY)
156         list(APPEND PLATFORM_LINKLIBS SystemStubs)
157 endif()
158
159 set(PLATFORM_CFLAGS "-pipe -funsigned-char")
160 set(PLATFORM_LINKFLAGS
161         "-fexceptions -framework CoreServices -framework Foundation -framework IOKit -framework AppKit -framework Cocoa -framework Carbon -framework AudioUnit -framework AudioToolbox -framework CoreAudio"
162 )
163 if(WITH_CODEC_QUICKTIME)
164         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -framework QTKit")
165         if(CMAKE_OSX_ARCHITECTURES MATCHES i386)
166                 set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -framework QuickTime")
167                 # libSDL still needs 32bit carbon quicktime
168         endif()
169 endif()
170
171 if(WITH_CXX11)
172         list(APPEND PLATFORM_LINKLIBS c++)
173 else()
174         list(APPEND PLATFORM_LINKLIBS stdc++)
175 endif()
176
177 if(WITH_JACK)
178         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -F/Library/Frameworks -weak_framework jackmp")
179 endif()
180
181 if(WITH_PYTHON_MODULE OR WITH_PYTHON_FRAMEWORK)
182         # force cmake to link right framework
183         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} /Library/Frameworks/Python.framework/Versions/${PYTHON_VERSION}/Python")
184 endif()
185
186 if(WITH_OPENCOLLADA)
187         set(OPENCOLLADA ${LIBDIR}/opencollada)
188
189         set(OPENCOLLADA_INCLUDE_DIRS
190                 ${LIBDIR}/opencollada/include/COLLADAStreamWriter
191                 ${LIBDIR}/opencollada/include/COLLADABaseUtils
192                 ${LIBDIR}/opencollada/include/COLLADAFramework
193                 ${LIBDIR}/opencollada/include/COLLADASaxFrameworkLoader
194                 ${LIBDIR}/opencollada/include/GeneratedSaxParser
195         )
196
197         set(OPENCOLLADA_LIBPATH ${OPENCOLLADA}/lib)
198         set(OPENCOLLADA_LIBRARIES
199                 OpenCOLLADASaxFrameworkLoader
200                 -lOpenCOLLADAFramework
201                 -lOpenCOLLADABaseUtils
202                 -lOpenCOLLADAStreamWriter
203                 -lMathMLSolver
204                 -lGeneratedSaxParser
205                 -lxml2 -lbuffer -lftoa
206         )
207         # Use UTF functions from collada if LLVM is not enabled
208         if(NOT WITH_LLVM)
209                 list(APPEND OPENCOLLADA_LIBRARIES -lUTF)
210         endif()
211         # pcre is bundled with openCollada
212         #set(PCRE ${LIBDIR}/pcre)
213         #set(PCRE_LIBPATH ${PCRE}/lib)
214         set(PCRE_LIBRARIES pcre)
215         #libxml2 is used
216         #set(EXPAT ${LIBDIR}/expat)
217         #set(EXPAT_LIBPATH ${EXPAT}/lib)
218         set(EXPAT_LIB)
219 endif()
220
221 if(WITH_SDL)
222         set(SDL ${LIBDIR}/sdl)
223         set(SDL_INCLUDE_DIR ${SDL}/include)
224         set(SDL_LIBRARY SDL2)
225         set(SDL_LIBPATH ${SDL}/lib)
226         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -lazy_framework ForceFeedback")
227 endif()
228
229 set(PNG "${LIBDIR}/png")
230 set(PNG_INCLUDE_DIRS "${PNG}/include")
231 set(PNG_LIBPATH ${PNG}/lib)
232
233 set(JPEG "${LIBDIR}/jpeg")
234 set(JPEG_INCLUDE_DIR "${JPEG}/include")
235 set(JPEG_LIBPATH ${JPEG}/lib)
236
237 if(WITH_IMAGE_TIFF)
238         set(TIFF ${LIBDIR}/tiff)
239         set(TIFF_INCLUDE_DIR ${TIFF}/include)
240         set(TIFF_LIBRARY tiff)
241         set(TIFF_LIBPATH ${TIFF}/lib)
242 endif()
243
244 if(WITH_BOOST)
245         set(BOOST ${LIBDIR}/boost)
246         set(BOOST_INCLUDE_DIR ${BOOST}/include)
247         set(BOOST_LIBRARIES
248                 boost_date_time-mt
249                 boost_filesystem-mt
250                 boost_regex-mt
251                 boost_system-mt
252                 boost_thread-mt
253                 boost_wave-mt
254         )
255         if(WITH_INTERNATIONAL)
256                 list(APPEND BOOST_LIBRARIES boost_locale-mt)
257         endif()
258         if(WITH_CYCLES_NETWORK)
259                 list(APPEND BOOST_LIBRARIES boost_serialization-mt)
260         endif()
261         if(WITH_OPENVDB)
262                 list(APPEND BOOST_LIBRARIES boost_iostreams-mt)
263         endif()
264         set(BOOST_LIBPATH ${BOOST}/lib)
265         set(BOOST_DEFINITIONS)
266 endif()
267
268 if(WITH_INTERNATIONAL OR WITH_CODEC_FFMPEG)
269         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -liconv") # boost_locale and ffmpeg needs it !
270 endif()
271
272 if(WITH_OPENIMAGEIO)
273         set(OPENIMAGEIO ${LIBDIR}/openimageio)
274         set(OPENIMAGEIO_INCLUDE_DIRS ${OPENIMAGEIO}/include)
275         set(OPENIMAGEIO_LIBRARIES
276                 ${OPENIMAGEIO}/lib/libOpenImageIO.a
277                 ${PNG_LIBRARIES}
278                 ${JPEG_LIBRARIES}
279                 ${TIFF_LIBRARY}
280                 ${OPENEXR_LIBRARIES}
281                 ${ZLIB_LIBRARIES}
282         )
283         set(OPENIMAGEIO_LIBPATH
284                 ${OPENIMAGEIO}/lib
285                 ${JPEG_LIBPATH}
286                 ${PNG_LIBPATH}
287                 ${TIFF_LIBPATH}
288                 ${OPENEXR_LIBPATH}
289                 ${ZLIB_LIBPATH}
290         )
291         set(OPENIMAGEIO_DEFINITIONS "-DOIIO_STATIC_BUILD")
292         set(OPENIMAGEIO_IDIFF "${LIBDIR}/openimageio/bin/idiff")
293 endif()
294
295 if(WITH_OPENCOLORIO)
296         set(OPENCOLORIO ${LIBDIR}/opencolorio)
297         set(OPENCOLORIO_INCLUDE_DIRS ${OPENCOLORIO}/include)
298         set(OPENCOLORIO_LIBRARIES OpenColorIO tinyxml yaml-cpp)
299         set(OPENCOLORIO_LIBPATH ${OPENCOLORIO}/lib)
300 endif()
301
302 if(WITH_OPENVDB)
303         set(OPENVDB ${LIBDIR}/openvdb)
304         set(OPENVDB_INCLUDE_DIRS ${OPENVDB}/include)
305         set(TBB_INCLUDE_DIRS ${LIBDIR}/tbb/include)
306         set(TBB_LIBRARIES ${LIBDIR}/tbb/lib/libtbb.a)
307         set(OPENVDB_LIBRARIES openvdb blosc ${TBB_LIBRARIES})
308         set(OPENVDB_LIBPATH ${LIBDIR}/openvdb/lib)
309         set(OPENVDB_DEFINITIONS)
310 endif()
311
312 if(WITH_LLVM)
313         set(LLVM_ROOT_DIR ${LIBDIR}/llvm CACHE PATH     "Path to the LLVM installation")
314         set(LLVM_VERSION "3.4" CACHE STRING     "Version of LLVM to use")
315         if(EXISTS "${LLVM_ROOT_DIR}/bin/llvm-config")
316                 set(LLVM_CONFIG "${LLVM_ROOT_DIR}/bin/llvm-config")
317         else()
318                 set(LLVM_CONFIG llvm-config)
319         endif()
320         execute_process(COMMAND ${LLVM_CONFIG} --version
321                         OUTPUT_VARIABLE LLVM_VERSION
322                         OUTPUT_STRIP_TRAILING_WHITESPACE)
323         execute_process(COMMAND ${LLVM_CONFIG} --prefix
324                         OUTPUT_VARIABLE LLVM_ROOT_DIR
325                         OUTPUT_STRIP_TRAILING_WHITESPACE)
326         execute_process(COMMAND ${LLVM_CONFIG} --libdir
327                         OUTPUT_VARIABLE LLVM_LIBPATH
328                         OUTPUT_STRIP_TRAILING_WHITESPACE)
329         find_library(LLVM_LIBRARY
330                       NAMES LLVMAnalysis # first of a whole bunch of libs to get
331                       PATHS ${LLVM_LIBPATH})
332
333         if(LLVM_LIBRARY AND LLVM_ROOT_DIR AND LLVM_LIBPATH)
334                 if(LLVM_STATIC)
335                         # if static LLVM libraries were requested, use llvm-config to generate
336                         # the list of what libraries we need, and substitute that in the right
337                         # way for LLVM_LIBRARY.
338                         execute_process(COMMAND ${LLVM_CONFIG} --libfiles
339                                         OUTPUT_VARIABLE LLVM_LIBRARY
340                                         OUTPUT_STRIP_TRAILING_WHITESPACE)
341                         string(REPLACE " " ";" LLVM_LIBRARY ${LLVM_LIBRARY})
342                 else()
343                         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -lLLVM-3.4")
344                 endif()
345         else()
346                 message(FATAL_ERROR "LLVM not found.")
347         endif()
348 endif()
349
350 if(WITH_CYCLES_OSL)
351         set(CYCLES_OSL ${LIBDIR}/osl CACHE PATH "Path to OpenShadingLanguage installation")
352
353         find_library(OSL_LIB_EXEC NAMES oslexec PATHS ${CYCLES_OSL}/lib)
354         find_library(OSL_LIB_COMP NAMES oslcomp PATHS ${CYCLES_OSL}/lib)
355         find_library(OSL_LIB_QUERY NAMES oslquery PATHS ${CYCLES_OSL}/lib)
356         # WARNING! depends on correct order of OSL libs linking
357         list(APPEND OSL_LIBRARIES ${OSL_LIB_COMP} -force_load ${OSL_LIB_EXEC} ${OSL_LIB_QUERY})
358         find_path(OSL_INCLUDE_DIR OSL/oslclosure.h PATHS ${CYCLES_OSL}/include)
359         find_program(OSL_COMPILER NAMES oslc PATHS ${CYCLES_OSL}/bin)
360
361         if(OSL_INCLUDE_DIR AND OSL_LIBRARIES AND OSL_COMPILER)
362                 set(OSL_FOUND TRUE)
363         else()
364                 message(STATUS "OSL not found")
365                 set(WITH_CYCLES_OSL OFF)
366         endif()
367 endif()
368
369 if(WITH_OPENMP)
370         execute_process(COMMAND ${CMAKE_C_COMPILER} --version OUTPUT_VARIABLE COMPILER_VENDOR)
371         string(SUBSTRING "${COMPILER_VENDOR}" 0 5 VENDOR_NAME) # truncate output
372         if(${VENDOR_NAME} MATCHES "Apple") # Apple does not support OpenMP reliable with gcc and not with clang
373                 set(WITH_OPENMP OFF)
374         else() # vanilla gcc or clang_omp support OpenMP
375                 message(STATUS "Using special OpenMP enabled compiler !") # letting find_package(OpenMP) module work for gcc
376                 if(CMAKE_C_COMPILER_ID MATCHES "Clang") # clang-omp in darwin libs
377                         set(OPENMP_FOUND ON)
378                         set(OpenMP_C_FLAGS "-fopenmp" CACHE STRING "C compiler flags for OpenMP parallization" FORCE)
379                         set(OpenMP_CXX_FLAGS "-fopenmp" CACHE STRING "C++ compiler flags for OpenMP parallization" FORCE)
380                         include_directories(${LIBDIR}/openmp/include)
381                         link_directories(${LIBDIR}/openmp/lib)
382                         # This is a workaround for our helperbinaries ( datatoc, masgfmt, ... ),
383                         # They are linked also to omp lib, so we need it in builddir for runtime exexcution,
384                         # TODO: remove all unneeded dependencies from these
385
386                         # for intermediate binaries, in respect to lib ID
387                         execute_process(
388                                 COMMAND ditto -arch ${CMAKE_OSX_ARCHITECTURES}
389                                 ${LIBDIR}/openmp/lib/libiomp5.dylib
390                                 ${CMAKE_BINARY_DIR}/Resources/lib/libiomp5.dylib)
391                 endif()
392         endif()
393 endif()
394
395 set(EXETYPE MACOSX_BUNDLE)
396
397 set(CMAKE_C_FLAGS_DEBUG "-fno-strict-aliasing -g")
398 set(CMAKE_CXX_FLAGS_DEBUG "-fno-strict-aliasing -g")
399 if(CMAKE_OSX_ARCHITECTURES MATCHES "x86_64" OR CMAKE_OSX_ARCHITECTURES MATCHES "i386")
400         set(CMAKE_CXX_FLAGS_RELEASE "-O2 -mdynamic-no-pic -msse -msse2 -msse3 -mssse3")
401         set(CMAKE_C_FLAGS_RELEASE "-O2 -mdynamic-no-pic  -msse -msse2 -msse3 -mssse3")
402         if(NOT CMAKE_C_COMPILER_ID MATCHES "Clang")
403                 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -ftree-vectorize  -fvariable-expansion-in-unroller")
404                 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -ftree-vectorize  -fvariable-expansion-in-unroller")
405         endif()
406 else()
407         set(CMAKE_C_FLAGS_RELEASE "-mdynamic-no-pic -fno-strict-aliasing")
408         set(CMAKE_CXX_FLAGS_RELEASE "-mdynamic-no-pic -fno-strict-aliasing")
409 endif()
410
411 if(${XCODE_VERSION} VERSION_EQUAL 5 OR ${XCODE_VERSION} VERSION_GREATER 5)
412         # Xcode 5 is always using CLANG, which has too low template depth of 128 for libmv
413         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftemplate-depth=1024")
414 endif()
415 # Get rid of eventually clashes, we export some symbols explicite as local
416 set(PLATFORM_LINKFLAGS
417         "${PLATFORM_LINKFLAGS} -Xlinker -unexported_symbols_list -Xlinker ${CMAKE_SOURCE_DIR}/source/creator/osx_locals.map"
418 )
419
420 if(WITH_CXX11)
421         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
422         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -stdlib=libc++")
423 endif()
424
425 # Suppress ranlib "has no symbols" warnings (workaround for T48250)
426 set(CMAKE_C_ARCHIVE_CREATE   "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
427 set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
428 set(CMAKE_C_ARCHIVE_FINISH   "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
429 set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")