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