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