74958a8731c7b246ea716eccfbcf8449523ea53f
[blender.git] / build_files / cmake / platform / platform_unix.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 any *nix system including Linux and Unix.
25
26 # Detect precompiled library directory
27 set(LIBDIR_NAME ${CMAKE_SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR})
28 string(TOLOWER ${LIBDIR_NAME} LIBDIR_NAME)
29 set(LIBDIR ${CMAKE_SOURCE_DIR}/../lib/${LIBDIR_NAME})
30
31 if(EXISTS ${LIBDIR})
32         file(GLOB LIB_SUBDIRS ${LIBDIR}/*)
33         set(CMAKE_PREFIX_PATH ${LIB_SUBDIRS})
34         set(WITH_STATIC_LIBS ON)
35         set(WITH_OPENMP_STATIC ON)
36 endif()
37
38 # Wrapper to prefer static libraries
39 macro(find_package_wrapper)
40         if(WITH_STATIC_LIBS)
41                 find_package_static(${ARGV})
42         else()
43                 find_package(${ARGV})
44         endif()
45 endmacro()
46
47 find_package_wrapper(JPEG REQUIRED)
48 find_package_wrapper(PNG REQUIRED)
49 find_package_wrapper(ZLIB REQUIRED)
50 find_package_wrapper(Freetype REQUIRED)
51
52 if(WITH_LZO AND WITH_SYSTEM_LZO)
53         find_package_wrapper(LZO)
54         if(NOT LZO_FOUND)
55                 message(FATAL_ERROR "Failed finding system LZO version!")
56         endif()
57 endif()
58
59 if(WITH_SYSTEM_EIGEN3)
60         find_package_wrapper(Eigen3)
61         if(NOT EIGEN3_FOUND)
62                 message(FATAL_ERROR "Failed finding system Eigen3 version!")
63         endif()
64 endif()
65 # else values are set below for all platforms
66
67 if(WITH_PYTHON)
68         # No way to set py35, remove for now.
69         # find_package(PythonLibs)
70
71         # Use our own instead, since without py is such a rare case,
72         # require this package
73         # XXX Linking errors with debian static python :/
74 #               find_package_wrapper(PythonLibsUnix REQUIRED)
75         find_package(PythonLibsUnix REQUIRED)
76 endif()
77
78 if(WITH_IMAGE_OPENEXR)
79         find_package_wrapper(OpenEXR)  # our own module
80         if(NOT OPENEXR_FOUND)
81                 set(WITH_IMAGE_OPENEXR OFF)
82         endif()
83 endif()
84
85 if(WITH_IMAGE_OPENJPEG)
86         find_package_wrapper(OpenJPEG)
87         if(NOT OPENJPEG_FOUND)
88                 set(WITH_IMAGE_OPENJPEG OFF)
89         endif()
90 endif()
91
92 if(WITH_IMAGE_TIFF)
93         # XXX Linking errors with debian static tiff :/
94 #               find_package_wrapper(TIFF)
95         find_package(TIFF)
96         if(NOT TIFF_FOUND)
97                 set(WITH_IMAGE_TIFF OFF)
98         endif()
99 endif()
100
101 # Audio IO
102 if(WITH_SYSTEM_AUDASPACE)
103         find_package_wrapper(Audaspace)
104         if(NOT AUDASPACE_FOUND OR NOT AUDASPACE_C_FOUND)
105                 message(FATAL_ERROR "Audaspace external library not found!")
106         endif()
107 endif()
108
109 if(WITH_OPENAL)
110         find_package_wrapper(OpenAL)
111         if(NOT OPENAL_FOUND)
112                 set(WITH_OPENAL OFF)
113         endif()
114 endif()
115
116 if(WITH_SDL)
117         if(WITH_SDL_DYNLOAD)
118                 set(SDL_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/extern/sdlew/include/SDL2")
119                 set(SDL_LIBRARY)
120         else()
121                 find_package_wrapper(SDL2)
122                 if(SDL2_FOUND)
123                         # Use same names for both versions of SDL until we move to 2.x.
124                         set(SDL_INCLUDE_DIR "${SDL2_INCLUDE_DIR}")
125                         set(SDL_LIBRARY "${SDL2_LIBRARY}")
126                         set(SDL_FOUND "${SDL2_FOUND}")
127                 else()
128                         find_package_wrapper(SDL)
129                 endif()
130                 mark_as_advanced(
131                         SDL_INCLUDE_DIR
132                         SDL_LIBRARY
133                 )
134                 # unset(SDLMAIN_LIBRARY CACHE)
135                 if(NOT SDL_FOUND)
136                         set(WITH_SDL OFF)
137                 endif()
138         endif()
139 endif()
140
141 if(WITH_JACK)
142         find_package_wrapper(Jack)
143         if(NOT JACK_FOUND)
144                 set(WITH_JACK OFF)
145         endif()
146 endif()
147
148 # Codecs
149 if(WITH_CODEC_SNDFILE)
150         find_package_wrapper(SndFile)
151         if(NOT SNDFILE_FOUND)
152                 set(WITH_CODEC_SNDFILE OFF)
153         endif()
154 endif()
155
156 if(WITH_CODEC_FFMPEG)
157         if(EXISTS ${LIBDIR})
158                 # For precompiled lib directory, all ffmpeg dependencies are in the same folder
159                 file(GLOB ffmpeg_libs ${LIBDIR}/ffmpeg/lib/*.a ${LIBDIR}/sndfile/lib/*.a)
160                 set(FFMPEG ${LIBDIR}/ffmpeg CACHE PATH "FFMPEG Directory")
161                 set(FFMPEG_LIBRARIES ${ffmpeg_libs} ${ffmpeg_libs} CACHE STRING "FFMPEG Libraries")
162         else()
163                 set(FFMPEG /usr CACHE PATH "FFMPEG Directory")
164                 set(FFMPEG_LIBRARIES avformat avcodec avutil avdevice swscale CACHE STRING "FFMPEG Libraries")
165         endif()
166
167         mark_as_advanced(FFMPEG)
168
169         # lame, but until we have proper find module for ffmpeg
170         set(FFMPEG_INCLUDE_DIRS ${FFMPEG}/include)
171         if(EXISTS "${FFMPEG}/include/ffmpeg/")
172                 list(APPEND FFMPEG_INCLUDE_DIRS "${FFMPEG}/include/ffmpeg")
173         endif()
174         # end lameness
175
176         mark_as_advanced(FFMPEG_LIBRARIES)
177         set(FFMPEG_LIBPATH ${FFMPEG}/lib)
178 endif()
179
180 if(WITH_FFTW3)
181         find_package_wrapper(Fftw3)
182         if(NOT FFTW3_FOUND)
183                 set(WITH_FFTW3 OFF)
184         endif()
185 endif()
186
187 if(WITH_OPENCOLLADA)
188         find_package_wrapper(OpenCOLLADA)
189         if(OPENCOLLADA_FOUND)
190                 find_package_wrapper(XML2)
191                 find_package_wrapper(PCRE)
192         else()
193                 set(WITH_OPENCOLLADA OFF)
194         endif()
195 endif()
196
197 if(WITH_MEM_JEMALLOC)
198         find_package_wrapper(JeMalloc)
199         if(NOT JEMALLOC_FOUND)
200                 set(WITH_MEM_JEMALLOC OFF)
201         endif()
202 endif()
203
204 if(WITH_INPUT_NDOF)
205         find_package_wrapper(Spacenav)
206         if(SPACENAV_FOUND)
207                 # use generic names within blenders buildsystem.
208                 set(NDOF_INCLUDE_DIRS ${SPACENAV_INCLUDE_DIRS})
209                 set(NDOF_LIBRARIES ${SPACENAV_LIBRARIES})
210         else()
211                 set(WITH_INPUT_NDOF OFF)
212         endif()
213 endif()
214
215 if(WITH_CYCLES_OSL)
216         set(CYCLES_OSL ${LIBDIR}/osl CACHE PATH "Path to OpenShadingLanguage installation")
217         if(NOT OSL_ROOT)
218                 set(OSL_ROOT ${CYCLES_OSL})
219         endif()
220         find_package_wrapper(OpenShadingLanguage)
221         if(OSL_FOUND)
222                 if(${OSL_LIBRARY_VERSION_MAJOR} EQUAL "1" AND ${OSL_LIBRARY_VERSION_MINOR} LESS "6")
223                         # Note: --whole-archive is needed to force loading of all symbols in liboslexec,
224                         # otherwise LLVM is missing the osl_allocate_closure_component function
225                         set(OSL_LIBRARIES
226                                 ${OSL_OSLCOMP_LIBRARY}
227                                 -Wl,--whole-archive ${OSL_OSLEXEC_LIBRARY}
228                                 -Wl,--no-whole-archive ${OSL_OSLQUERY_LIBRARY}
229                         )
230                 endif()
231         else()
232                 message(STATUS "OSL not found, disabling it from Cycles")
233                 set(WITH_CYCLES_OSL OFF)
234         endif()
235 endif()
236
237 if(WITH_OPENVDB)
238         find_package_wrapper(OpenVDB)
239         find_package_wrapper(TBB)
240         find_package_wrapper(Blosc)
241         if(NOT OPENVDB_FOUND OR NOT TBB_FOUND)
242                 set(WITH_OPENVDB OFF)
243                 set(WITH_OPENVDB_BLOSC OFF)
244                 message(STATUS "OpenVDB not found, disabling it")
245         elseif(NOT BLOSC_FOUND)
246                 set(WITH_OPENVDB_BLOSC OFF)
247                 message(STATUS "Blosc not found, disabling it")
248         endif()
249 endif()
250
251 if(WITH_ALEMBIC)
252         find_package_wrapper(Alembic)
253
254         if(WITH_ALEMBIC_HDF5)
255                 set(HDF5_ROOT_DIR ${LIBDIR}/hdf5)
256                 find_package_wrapper(HDF5)
257         endif()
258
259         if(NOT ALEMBIC_FOUND OR (WITH_ALEMBIC_HDF5 AND NOT HDF5_FOUND))
260                 set(WITH_ALEMBIC OFF)
261                 set(WITH_ALEMBIC_HDF5 OFF)
262         endif()
263 endif()
264
265 if(WITH_BOOST)
266         # uses in build instructions to override include and library variables
267         if(NOT BOOST_CUSTOM)
268                 if(WITH_STATIC_LIBS)
269                         set(Boost_USE_STATIC_LIBS ON)
270                 endif()
271                 set(Boost_USE_MULTITHREADED ON)
272                 set(__boost_packages filesystem regex thread date_time)
273                 if(WITH_CYCLES_OSL)
274                         if(NOT (${OSL_LIBRARY_VERSION_MAJOR} EQUAL "1" AND ${OSL_LIBRARY_VERSION_MINOR} LESS "6"))
275                                 list(APPEND __boost_packages wave)
276                         else()
277                         endif()
278                 endif()
279                 if(WITH_INTERNATIONAL)
280                         list(APPEND __boost_packages locale)
281                 endif()
282                 if(WITH_CYCLES_NETWORK)
283                         list(APPEND __boost_packages serialization)
284                 endif()
285                 if(WITH_OPENVDB)
286                         list(APPEND __boost_packages iostreams)
287                 endif()
288                 list(APPEND __boost_packages system)
289                 find_package(Boost 1.48 COMPONENTS ${__boost_packages})
290                 if(NOT Boost_FOUND)
291                         # try to find non-multithreaded if -mt not found, this flag
292                         # doesn't matter for us, it has nothing to do with thread
293                         # safety, but keep it to not disturb build setups
294                         set(Boost_USE_MULTITHREADED OFF)
295                         find_package(Boost 1.48 COMPONENTS ${__boost_packages})
296                 endif()
297                 unset(__boost_packages)
298                 if(Boost_USE_STATIC_LIBS AND WITH_BOOST_ICU)
299                         find_package(IcuLinux)
300                 endif()
301                 mark_as_advanced(Boost_DIR)  # why doesnt boost do this?
302         endif()
303
304         set(BOOST_INCLUDE_DIR ${Boost_INCLUDE_DIRS})
305         set(BOOST_LIBRARIES ${Boost_LIBRARIES})
306         set(BOOST_LIBPATH ${Boost_LIBRARY_DIRS})
307         set(BOOST_DEFINITIONS "-DBOOST_ALL_NO_LIB")
308 endif()
309
310 if(WITH_OPENIMAGEIO)
311         find_package_wrapper(OpenImageIO)
312         if(NOT OPENIMAGEIO_PUGIXML_FOUND AND WITH_CYCLES_STANDALONE)
313                 find_package_wrapper(PugiXML)
314         else()
315                 set(PUGIXML_INCLUDE_DIR "${OPENIMAGEIO_INCLUDE_DIR/OpenImageIO}")
316                 set(PUGIXML_LIBRARIES "")
317         endif()
318
319         set(OPENIMAGEIO_LIBRARIES
320                 ${OPENIMAGEIO_LIBRARIES}
321                 ${PNG_LIBRARIES}
322                 ${JPEG_LIBRARIES}
323                 ${ZLIB_LIBRARIES}
324                 ${BOOST_LIBRARIES}
325         )
326         set(OPENIMAGEIO_LIBPATH)  # TODO, remove and reference the absolute path everywhere
327         set(OPENIMAGEIO_DEFINITIONS "")
328
329         if(WITH_IMAGE_TIFF)
330                 list(APPEND OPENIMAGEIO_LIBRARIES "${TIFF_LIBRARY}")
331         endif()
332         if(WITH_IMAGE_OPENEXR)
333                 list(APPEND OPENIMAGEIO_LIBRARIES "${OPENEXR_LIBRARIES}")
334         endif()
335
336         if(NOT OPENIMAGEIO_FOUND)
337                 set(WITH_OPENIMAGEIO OFF)
338                 message(STATUS "OpenImageIO not found, disabling WITH_CYCLES")
339         endif()
340 endif()
341
342 if(WITH_OPENCOLORIO)
343         find_package_wrapper(OpenColorIO)
344
345         set(OPENCOLORIO_LIBRARIES ${OPENCOLORIO_LIBRARIES})
346         set(OPENCOLORIO_LIBPATH)  # TODO, remove and reference the absolute path everywhere
347         set(OPENCOLORIO_DEFINITIONS)
348
349         if(NOT OPENCOLORIO_FOUND)
350                 set(WITH_OPENCOLORIO OFF)
351                 message(STATUS "OpenColorIO not found")
352         endif()
353 endif()
354
355 if(WITH_LLVM)
356         if(EXISTS ${LIBDIR})
357                 set(LLVM_STATIC ON)
358         endif()
359
360         find_package_wrapper(LLVM)
361
362         # Symbol conflicts with same UTF library used by OpenCollada
363         if(EXISTS ${LIBDIR})
364                 if(WITH_OPENCOLLADA AND (${LLVM_VERSION} VERSION_LESS "4.0.0"))
365                         list(REMOVE_ITEM OPENCOLLADA_LIBRARIES ${OPENCOLLADA_UTF_LIBRARY})
366                 endif()
367         endif()
368
369         if(NOT LLVM_FOUND)
370                 set(WITH_LLVM OFF)
371                 message(STATUS "LLVM not found")
372         endif()
373 endif()
374
375 if(WITH_LLVM OR WITH_SDL_DYNLOAD)
376         # Fix for conflict with Mesa llvmpipe
377         set(PLATFORM_LINKFLAGS
378                 "${PLATFORM_LINKFLAGS} -Wl,--version-script='${CMAKE_SOURCE_DIR}/source/creator/blender.map'"
379         )
380 endif()
381
382 if(WITH_OPENSUBDIV OR WITH_CYCLES_OPENSUBDIV)
383         find_package_wrapper(OpenSubdiv)
384
385         set(OPENSUBDIV_LIBRARIES ${OPENSUBDIV_LIBRARIES})
386         set(OPENSUBDIV_LIBPATH)  # TODO, remove and reference the absolute path everywhere
387
388         if(NOT OPENSUBDIV_FOUND)
389                 set(WITH_OPENSUBDIV OFF)
390                 set(WITH_CYCLES_OPENSUBDIV OFF)
391                 message(STATUS "OpenSubdiv not found")
392         endif()
393 endif()
394
395 # OpenSuse needs lutil, ArchLinux not, for now keep, can avoid by using --as-needed
396 if(HAIKU)
397         list(APPEND PLATFORM_LINKLIBS -lnetwork)
398 else()
399         list(APPEND PLATFORM_LINKLIBS -lutil -lc -lm)
400 endif()
401
402 find_package(Threads REQUIRED)
403 list(APPEND PLATFORM_LINKLIBS ${CMAKE_THREAD_LIBS_INIT})
404 # used by other platforms
405 set(PTHREADS_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
406
407 if(CMAKE_DL_LIBS)
408         list(APPEND PLATFORM_LINKLIBS ${CMAKE_DL_LIBS})
409 endif()
410
411 if(CMAKE_SYSTEM_NAME MATCHES "Linux")
412         if(NOT WITH_PYTHON_MODULE)
413                 # binreloc is linux only
414                 set(BINRELOC_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/extern/binreloc/include)
415                 set(WITH_BINRELOC ON)
416         endif()
417 endif()
418
419 # lfs on glibc, all compilers should use
420 add_definitions(-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE)
421
422 # GNU Compiler
423 if(CMAKE_COMPILER_IS_GNUCC)
424         set(PLATFORM_CFLAGS "-pipe -fPIC -funsigned-char -fno-strict-aliasing")
425
426         if(WITH_LINKER_GOLD)
427                 execute_process(
428                         COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version
429                         ERROR_QUIET OUTPUT_VARIABLE LD_VERSION)
430                 if("${LD_VERSION}" MATCHES "GNU gold")
431                         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fuse-ld=gold")
432                         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fuse-ld=gold")
433                 else()
434                         message(STATUS "GNU gold linker isn't available, using the default system linker.")
435                 endif()
436                 unset(LD_VERSION)
437         endif()
438
439 # CLang is the same as GCC for now.
440 elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
441         set(PLATFORM_CFLAGS "-pipe -fPIC -funsigned-char -fno-strict-aliasing")
442 # Intel C++ Compiler
443 elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
444         # think these next two are broken
445         find_program(XIAR xiar)
446         if(XIAR)
447                 set(CMAKE_AR "${XIAR}")
448         endif()
449         mark_as_advanced(XIAR)
450
451         find_program(XILD xild)
452         if(XILD)
453                 set(CMAKE_LINKER "${XILD}")
454         endif()
455         mark_as_advanced(XILD)
456
457         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fp-model precise -prec_div -parallel")
458         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fp-model precise -prec_div -parallel")
459
460         # set(PLATFORM_CFLAGS "${PLATFORM_CFLAGS} -diag-enable sc3")
461         set(PLATFORM_CFLAGS "-pipe -fPIC -funsigned-char -fno-strict-aliasing")
462         set(PLATFORM_LINKFLAGS "${PLATFORM_LINKFLAGS} -static-intel")
463 endif()