Cleanup: use boolean argument
[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 # ***** END GPL LICENSE BLOCK *****
20
21 # Libraries configuration for any *nix system including Linux and Unix.
22
23 # Detect precompiled library directory
24 if(NOT DEFINED LIBDIR)
25   # Path to a locally compiled libraries.
26   set(LIBDIR_NAME ${CMAKE_SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR})
27   string(TOLOWER ${LIBDIR_NAME} LIBDIR_NAME)
28   set(LIBDIR_NATIVE_ABI ${CMAKE_SOURCE_DIR}/../lib/${LIBDIR_NAME})
29
30   # Path to precompiled libraries with known CentOS 7 ABI.
31   set(LIBDIR_CENTOS7_ABI ${CMAKE_SOURCE_DIR}/../lib/linux_centos7_x86_64)
32
33   # Choose the best suitable libraries.
34   if(EXISTS ${LIBDIR_NATIVE_ABI})
35     set(LIBDIR ${LIBDIR_NATIVE_ABI})
36   elseif(EXISTS ${LIBDIR_CENTOS7_ABI})
37     set(LIBDIR ${LIBDIR_CENTOS7_ABI})
38     set(WITH_CXX11_ABI OFF)
39
40     if(CMAKE_COMPILER_IS_GNUCC AND
41        CMAKE_C_COMPILER_VERSION VERSION_LESS 9.3)
42       message(FATAL_ERROR "GCC version must be at least 9.3 for precompiled libraries, found ${CMAKE_C_COMPILER_VERSION}")
43     endif()
44   endif()
45
46   # Avoid namespace pollustion.
47   unset(LIBDIR_NATIVE_ABI)
48   unset(LIBDIR_CENTOS7_ABI)
49 endif()
50
51 if(EXISTS ${LIBDIR})
52   message(STATUS "Using pre-compiled LIBDIR: ${LIBDIR}")
53
54   file(GLOB LIB_SUBDIRS ${LIBDIR}/*)
55   # Ignore Mesa software OpenGL libraries, they are not intended to be
56   # linked against but to optionally override at runtime.
57   list(REMOVE_ITEM LIB_SUBDIRS ${LIBDIR}/mesa)
58   # NOTE: Make sure "proper" compiled zlib comes first before the one
59   # which is a part of OpenCollada. They have different ABI, and we
60   # do need to use the official one.
61   set(CMAKE_PREFIX_PATH ${LIBDIR}/zlib ${LIB_SUBDIRS})
62   set(WITH_STATIC_LIBS ON)
63   # OpenMP usually can't be statically linked into shared libraries,
64   # due to not being compiled with position independent code.
65   if(NOT WITH_PYTHON_MODULE)
66     set(WITH_OPENMP_STATIC ON)
67   endif()
68   set(Boost_NO_BOOST_CMAKE ON)
69   set(BOOST_ROOT ${LIBDIR}/boost)
70   set(BOOST_LIBRARYDIR ${LIBDIR}/boost/lib)
71   set(Boost_NO_SYSTEM_PATHS ON)
72   set(OPENEXR_ROOT_DIR ${LIBDIR}/openexr)
73   set(CLANG_ROOT_DIR ${LIBDIR}/llvm)
74 endif()
75
76 if(WITH_STATIC_LIBS)
77   string(APPEND CMAKE_EXE_LINKER_FLAGS " -static-libstdc++")
78 endif()
79
80 # Wrapper to prefer static libraries
81 macro(find_package_wrapper)
82   if(WITH_STATIC_LIBS)
83     find_package_static(${ARGV})
84   else()
85     find_package(${ARGV})
86   endif()
87 endmacro()
88
89 # ----------------------------------------------------------------------------
90 # Precompiled Libraries
91 #
92 # These are libraries that may be precompiled. For this we disable searching in
93 # the system directories so that we don't accidentally use them instead.
94
95 if(EXISTS ${LIBDIR})
96   without_system_libs_begin()
97 endif()
98
99 find_package_wrapper(JPEG REQUIRED)
100 find_package_wrapper(PNG REQUIRED)
101 find_package_wrapper(ZLIB REQUIRED)
102 find_package_wrapper(Freetype REQUIRED)
103
104 if(WITH_PYTHON)
105   # No way to set py35, remove for now.
106   # find_package(PythonLibs)
107
108   # Use our own instead, since without py is such a rare case,
109   # require this package
110   # XXX Linking errors with debian static python :/
111 #       find_package_wrapper(PythonLibsUnix REQUIRED)
112   find_package(PythonLibsUnix REQUIRED)
113 endif()
114
115 if(WITH_IMAGE_OPENEXR)
116   find_package_wrapper(OpenEXR)  # our own module
117   if(NOT OPENEXR_FOUND)
118     set(WITH_IMAGE_OPENEXR OFF)
119   endif()
120 endif()
121
122 if(WITH_IMAGE_OPENJPEG)
123   find_package_wrapper(OpenJPEG)
124   if(NOT OPENJPEG_FOUND)
125     set(WITH_IMAGE_OPENJPEG OFF)
126   endif()
127 endif()
128
129 if(WITH_IMAGE_TIFF)
130   # XXX Linking errors with debian static tiff :/
131 #       find_package_wrapper(TIFF)
132   find_package(TIFF)
133   if(NOT TIFF_FOUND)
134     set(WITH_IMAGE_TIFF OFF)
135   endif()
136 endif()
137
138 if(WITH_OPENAL)
139   find_package_wrapper(OpenAL)
140   if(NOT OPENAL_FOUND)
141     set(WITH_OPENAL OFF)
142   endif()
143 endif()
144
145 if(WITH_SDL)
146   if(WITH_SDL_DYNLOAD)
147     set(SDL_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/extern/sdlew/include/SDL2")
148     set(SDL_LIBRARY)
149   else()
150     find_package_wrapper(SDL2)
151     if(SDL2_FOUND)
152       # Use same names for both versions of SDL until we move to 2.x.
153       set(SDL_INCLUDE_DIR "${SDL2_INCLUDE_DIR}")
154       set(SDL_LIBRARY "${SDL2_LIBRARY}")
155       set(SDL_FOUND "${SDL2_FOUND}")
156     else()
157       find_package_wrapper(SDL)
158     endif()
159     mark_as_advanced(
160       SDL_INCLUDE_DIR
161       SDL_LIBRARY
162     )
163     # unset(SDLMAIN_LIBRARY CACHE)
164     if(NOT SDL_FOUND)
165       set(WITH_SDL OFF)
166     endif()
167   endif()
168 endif()
169
170 # Codecs
171 if(WITH_CODEC_SNDFILE)
172   find_package_wrapper(SndFile)
173   if(NOT SNDFILE_FOUND)
174     set(WITH_CODEC_SNDFILE OFF)
175   endif()
176 endif()
177
178 if(WITH_CODEC_FFMPEG)
179   if(EXISTS ${LIBDIR})
180     # For precompiled lib directory, all ffmpeg dependencies are in the same folder
181     file(GLOB ffmpeg_libs ${LIBDIR}/ffmpeg/lib/*.a ${LIBDIR}/sndfile/lib/*.a)
182     set(FFMPEG ${LIBDIR}/ffmpeg CACHE PATH "FFMPEG Directory")
183     set(FFMPEG_LIBRARIES ${ffmpeg_libs} ${ffmpeg_libs} CACHE STRING "FFMPEG Libraries")
184   else()
185     set(FFMPEG /usr CACHE PATH "FFMPEG Directory")
186     set(FFMPEG_LIBRARIES avformat avcodec avutil avdevice swscale CACHE STRING "FFMPEG Libraries")
187   endif()
188
189   mark_as_advanced(FFMPEG)
190
191   # lame, but until we have proper find module for ffmpeg
192   set(FFMPEG_INCLUDE_DIRS ${FFMPEG}/include)
193   if(EXISTS "${FFMPEG}/include/ffmpeg/")
194     list(APPEND FFMPEG_INCLUDE_DIRS "${FFMPEG}/include/ffmpeg")
195   endif()
196   # end lameness
197
198   mark_as_advanced(FFMPEG_LIBRARIES)
199   set(FFMPEG_LIBPATH ${FFMPEG}/lib)
200 endif()
201
202 if(WITH_FFTW3)
203   find_package_wrapper(Fftw3)
204   if(NOT FFTW3_FOUND)
205     set(WITH_FFTW3 OFF)
206   endif()
207 endif()
208
209 if(WITH_OPENCOLLADA)
210   find_package_wrapper(OpenCOLLADA)
211   if(OPENCOLLADA_FOUND)
212     if(WITH_STATIC_LIBS)
213       # PCRE is bundled with OpenCollada without headers, so can't use
214       # find_package reliably to detect it.
215       set(PCRE_LIBRARIES ${LIBDIR}/opencollada/lib/libpcre.a)
216     else()
217       find_package_wrapper(PCRE)
218     endif()
219     find_package_wrapper(XML2)
220   else()
221     set(WITH_OPENCOLLADA OFF)
222   endif()
223 endif()
224
225 if(WITH_MEM_JEMALLOC)
226   find_package_wrapper(JeMalloc)
227   if(NOT JEMALLOC_FOUND)
228     set(WITH_MEM_JEMALLOC OFF)
229   endif()
230 endif()
231
232 if(WITH_INPUT_NDOF)
233   find_package_wrapper(Spacenav)
234   if(SPACENAV_FOUND)
235     # use generic names within blenders buildsystem.
236     set(NDOF_INCLUDE_DIRS ${SPACENAV_INCLUDE_DIRS})
237     set(NDOF_LIBRARIES ${SPACENAV_LIBRARIES})
238   else()
239     set(WITH_INPUT_NDOF OFF)
240   endif()
241 endif()
242
243 if(WITH_CYCLES_OSL)
244   set(CYCLES_OSL ${LIBDIR}/osl CACHE PATH "Path to OpenShadingLanguage installation")
245   if(EXISTS ${CYCLES_OSL} AND NOT OSL_ROOT)
246     set(OSL_ROOT ${CYCLES_OSL})
247   endif()
248   find_package_wrapper(OSL)
249   if(OSL_FOUND)
250     if(${OSL_LIBRARY_VERSION_MAJOR} EQUAL "1" AND ${OSL_LIBRARY_VERSION_MINOR} LESS "6")
251       # Note: --whole-archive is needed to force loading of all symbols in liboslexec,
252       # otherwise LLVM is missing the osl_allocate_closure_component function
253       set(OSL_LIBRARIES
254         ${OSL_OSLCOMP_LIBRARY}
255         -Wl,--whole-archive ${OSL_OSLEXEC_LIBRARY}
256         -Wl,--no-whole-archive ${OSL_OSLQUERY_LIBRARY}
257       )
258     endif()
259   else()
260     message(STATUS "OSL not found, disabling it from Cycles")
261     set(WITH_CYCLES_OSL OFF)
262   endif()
263 endif()
264
265 if(WITH_OPENVDB)
266   find_package_wrapper(OpenVDB)
267   find_package_wrapper(Blosc)
268
269   if(NOT OPENVDB_FOUND)
270     set(WITH_OPENVDB OFF)
271     set(WITH_OPENVDB_BLOSC OFF)
272     message(STATUS "OpenVDB not found, disabling it")
273   elseif(NOT BLOSC_FOUND)
274     set(WITH_OPENVDB_BLOSC OFF)
275     message(STATUS "Blosc not found, disabling it for OpenVBD")
276   endif()
277 endif()
278
279 if(WITH_NANOVDB)
280   find_package_wrapper(NanoVDB)
281
282   if(NOT NANOVDB_FOUND)
283     set(WITH_NANOVDB OFF)
284     message(STATUS "NanoVDB not found, disabling it")
285   endif()
286 endif()
287
288 if(WITH_CPU_SIMD AND SUPPORT_NEON_BUILD)
289   find_package_wrapper(sse2neon)
290 endif()
291
292 if(WITH_ALEMBIC)
293   find_package_wrapper(Alembic)
294
295   if(NOT ALEMBIC_FOUND)
296     set(WITH_ALEMBIC OFF)
297   endif()
298 endif()
299
300 if(WITH_USD)
301   find_package_wrapper(USD)
302
303   if(NOT USD_FOUND)
304     set(WITH_USD OFF)
305   endif()
306 endif()
307
308 if(WITH_BOOST)
309   # uses in build instructions to override include and library variables
310   if(NOT BOOST_CUSTOM)
311     if(WITH_STATIC_LIBS)
312       set(Boost_USE_STATIC_LIBS ON)
313     endif()
314     set(Boost_USE_MULTITHREADED ON)
315     set(__boost_packages filesystem regex thread date_time)
316     if(WITH_CYCLES_OSL)
317       if(NOT (${OSL_LIBRARY_VERSION_MAJOR} EQUAL "1" AND ${OSL_LIBRARY_VERSION_MINOR} LESS "6"))
318         list(APPEND __boost_packages wave)
319       else()
320       endif()
321     endif()
322     if(WITH_INTERNATIONAL)
323       list(APPEND __boost_packages locale)
324     endif()
325     if(WITH_CYCLES_NETWORK)
326       list(APPEND __boost_packages serialization)
327     endif()
328     if(WITH_OPENVDB)
329       list(APPEND __boost_packages iostreams)
330     endif()
331     list(APPEND __boost_packages system)
332     find_package(Boost 1.48 COMPONENTS ${__boost_packages})
333     if(NOT Boost_FOUND)
334       # try to find non-multithreaded if -mt not found, this flag
335       # doesn't matter for us, it has nothing to do with thread
336       # safety, but keep it to not disturb build setups
337       set(Boost_USE_MULTITHREADED OFF)
338       find_package(Boost 1.48 COMPONENTS ${__boost_packages})
339     endif()
340     unset(__boost_packages)
341     if(Boost_USE_STATIC_LIBS AND WITH_BOOST_ICU)
342       find_package(IcuLinux)
343     endif()
344     mark_as_advanced(Boost_DIR)  # why doesn't boost do this?
345   endif()
346
347   set(BOOST_INCLUDE_DIR ${Boost_INCLUDE_DIRS})
348   set(BOOST_LIBRARIES ${Boost_LIBRARIES})
349   set(BOOST_LIBPATH ${Boost_LIBRARY_DIRS})
350   set(BOOST_DEFINITIONS "-DBOOST_ALL_NO_LIB")
351
352   if(Boost_USE_STATIC_LIBS AND WITH_BOOST_ICU)
353     find_package(IcuLinux)
354     list(APPEND BOOST_LIBRARIES ${ICU_LIBRARIES})
355   endif()
356 endif()
357
358 if(WITH_PUGIXML)
359   find_package_wrapper(PugiXML)
360
361   if(NOT PUGIXML_FOUND)
362     set(WITH_PUGIXML OFF)
363     message(STATUS "PugiXML not found, disabling WITH_PUGIXML")
364   endif()
365 endif()
366
367 if(WITH_OPENIMAGEIO)
368   find_package_wrapper(OpenImageIO)
369   set(OPENIMAGEIO_LIBRARIES
370     ${OPENIMAGEIO_LIBRARIES}
371     ${PNG_LIBRARIES}
372     ${JPEG_LIBRARIES}
373     ${ZLIB_LIBRARIES}
374     ${BOOST_LIBRARIES}
375   )
376   set(OPENIMAGEIO_LIBPATH)  # TODO, remove and reference the absolute path everywhere
377   set(OPENIMAGEIO_DEFINITIONS "")
378
379   if(WITH_IMAGE_TIFF)
380     list(APPEND OPENIMAGEIO_LIBRARIES "${TIFF_LIBRARY}")
381   endif()
382   if(WITH_IMAGE_OPENEXR)
383     list(APPEND OPENIMAGEIO_LIBRARIES "${OPENEXR_LIBRARIES}")
384   endif()
385
386   if(NOT OPENIMAGEIO_FOUND)
387     set(WITH_OPENIMAGEIO OFF)
388     message(STATUS "OpenImageIO not found, disabling WITH_CYCLES")
389   endif()
390 endif()
391
392 if(WITH_OPENCOLORIO)
393   find_package_wrapper(OpenColorIO 2.0.0)
394
395   set(OPENCOLORIO_LIBRARIES ${OPENCOLORIO_LIBRARIES})
396   set(OPENCOLORIO_LIBPATH)  # TODO, remove and reference the absolute path everywhere
397   set(OPENCOLORIO_DEFINITIONS)
398
399   if(NOT OPENCOLORIO_FOUND)
400     set(WITH_OPENCOLORIO OFF)
401     message(STATUS "OpenColorIO not found")
402   endif()
403 endif()
404
405 if(WITH_CYCLES_EMBREE)
406   find_package(Embree 3.8.0 REQUIRED)
407 endif()
408
409 if(WITH_OPENIMAGEDENOISE)
410   find_package_wrapper(OpenImageDenoise)
411
412   if(NOT OPENIMAGEDENOISE_FOUND)
413     set(WITH_OPENIMAGEDENOISE OFF)
414     message(STATUS "OpenImageDenoise not found")
415   endif()
416 endif()
417
418 if(WITH_LLVM)
419   if(EXISTS ${LIBDIR})
420     set(LLVM_STATIC ON)
421   endif()
422
423   find_package_wrapper(LLVM)
424   if(WITH_CLANG)
425     find_package_wrapper(Clang)
426   endif()
427   # Symbol conflicts with same UTF library used by OpenCollada
428   if(EXISTS ${LIBDIR})
429     if(WITH_OPENCOLLADA AND (${LLVM_VERSION} VERSION_LESS "4.0.0"))
430       list(REMOVE_ITEM OPENCOLLADA_LIBRARIES ${OPENCOLLADA_UTF_LIBRARY})
431     endif()
432   endif()
433
434   if(NOT LLVM_FOUND)
435     set(WITH_LLVM OFF)
436     set(WITH_CLANG OFF)
437     message(STATUS "LLVM not found")
438   else()
439     if(NOT CLANG_FOUND)
440       set(WITH_CLANG OFF)
441       message(STATUS "Clang not found")
442     endif()
443   endif()
444 endif()
445
446 if(WITH_OPENSUBDIV)
447   find_package_wrapper(OpenSubdiv)
448
449   set(OPENSUBDIV_LIBRARIES ${OPENSUBDIV_LIBRARIES})
450   set(OPENSUBDIV_LIBPATH)  # TODO, remove and reference the absolute path everywhere
451
452   if(NOT OPENSUBDIV_FOUND)
453     set(WITH_OPENSUBDIV OFF)
454     message(STATUS "OpenSubdiv not found")
455   endif()
456 endif()
457
458 if(WITH_TBB)
459   find_package_wrapper(TBB)
460 endif()
461
462 if(WITH_XR_OPENXR)
463   find_package(XR_OpenXR_SDK)
464   if(NOT XR_OPENXR_SDK_FOUND)
465     message(WARNING "OpenXR-SDK not found, disabling WITH_XR_OPENXR")
466     set(WITH_XR_OPENXR OFF)
467   endif()
468 endif()
469
470 if(WITH_GMP)
471   find_package_wrapper(GMP)
472   if(NOT GMP_FOUND)
473     message(WARNING "GMP not found, disabling WITH_GMP")
474     set(WITH_GMP OFF)
475   endif()
476 endif()
477
478 if(WITH_POTRACE)
479   find_package_wrapper(Potrace)
480   if(NOT POTRACE_FOUND)
481     message(WARNING "potrace not found, disabling WITH_POTRACE")
482     set(WITH_POTRACE OFF)
483   endif()
484 endif()
485
486 if(WITH_HARU)
487   find_package_wrapper(Haru)
488   if(NOT HARU_FOUND)
489     message(WARNING "Haru not found, disabling WITH_HARU")
490     set(WITH_HARU OFF)
491   endif()
492 endif()
493
494 if(EXISTS ${LIBDIR})
495   without_system_libs_end()
496 endif()
497
498 # ----------------------------------------------------------------------------
499 # Build and Link Flags
500
501 # OpenSuse needs lutil, ArchLinux not, for now keep, can avoid by using --as-needed
502 if(HAIKU)
503   list(APPEND PLATFORM_LINKLIBS -lnetwork)
504 else()
505   list(APPEND PLATFORM_LINKLIBS -lutil -lc -lm)
506 endif()
507
508 find_package(Threads REQUIRED)
509 list(APPEND PLATFORM_LINKLIBS ${CMAKE_THREAD_LIBS_INIT})
510 # used by other platforms
511 set(PTHREADS_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
512
513 if(CMAKE_DL_LIBS)
514   list(APPEND PLATFORM_LINKLIBS ${CMAKE_DL_LIBS})
515 endif()
516
517 if(CMAKE_SYSTEM_NAME MATCHES "Linux")
518   if(NOT WITH_PYTHON_MODULE)
519     # binreloc is linux only
520     set(BINRELOC_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/extern/binreloc/include)
521     set(WITH_BINRELOC ON)
522   endif()
523 endif()
524
525 # lfs on glibc, all compilers should use
526 add_definitions(-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE)
527
528 # ----------------------------------------------------------------------------
529 # System Libraries
530 #
531 # Keep last, so indirectly linked libraries don't override our own pre-compiled libs.
532
533 if(WITH_LZO AND WITH_SYSTEM_LZO)
534   find_package_wrapper(LZO)
535   if(NOT LZO_FOUND)
536     message(FATAL_ERROR "Failed finding system LZO version!")
537   endif()
538 endif()
539
540 if(WITH_SYSTEM_EIGEN3)
541   find_package_wrapper(Eigen3)
542   if(NOT EIGEN3_FOUND)
543     message(FATAL_ERROR "Failed finding system Eigen3 version!")
544   endif()
545 endif()
546
547 # Jack is intended to use the system library.
548 if(WITH_JACK)
549   find_package_wrapper(Jack)
550   if(NOT JACK_FOUND)
551     set(WITH_JACK OFF)
552   endif()
553 endif()
554
555 # Pulse is intended to use the system library.
556 if(WITH_PULSEAUDIO)
557   find_package_wrapper(Pulse)
558   if(NOT PULSE_FOUND)
559     set(WITH_PULSEAUDIO OFF)
560   endif()
561 endif()
562
563 # Audio IO
564 if(WITH_SYSTEM_AUDASPACE)
565   find_package_wrapper(Audaspace)
566   if(NOT AUDASPACE_FOUND OR NOT AUDASPACE_C_FOUND)
567     message(FATAL_ERROR "Audaspace external library not found!")
568   endif()
569 endif()
570
571 if(WITH_GHOST_WAYLAND)
572   find_package(PkgConfig)
573   pkg_check_modules(wayland-client REQUIRED wayland-client>=1.12)
574   pkg_check_modules(wayland-egl REQUIRED wayland-egl)
575   pkg_check_modules(wayland-scanner REQUIRED wayland-scanner)
576   pkg_check_modules(xkbcommon REQUIRED xkbcommon)
577   pkg_check_modules(wayland-cursor REQUIRED wayland-cursor)
578
579   set(WITH_GL_EGL ON)
580
581   if(WITH_GHOST_WAYLAND)
582     list(APPEND PLATFORM_LINKLIBS
583       ${wayland-client_LIBRARIES}
584       ${wayland-egl_LIBRARIES}
585       ${xkbcommon_LIBRARIES}
586       ${wayland-cursor_LIBRARIES}
587     )
588   endif()
589 endif()
590
591 if(WITH_GHOST_X11)
592   find_package(X11 REQUIRED)
593
594   find_path(X11_XF86keysym_INCLUDE_PATH X11/XF86keysym.h ${X11_INC_SEARCH_PATH})
595   mark_as_advanced(X11_XF86keysym_INCLUDE_PATH)
596
597   list(APPEND PLATFORM_LINKLIBS ${X11_X11_LIB})
598
599   if(WITH_X11_XINPUT)
600     if(X11_Xinput_LIB)
601       list(APPEND PLATFORM_LINKLIBS ${X11_Xinput_LIB})
602     else()
603       message(FATAL_ERROR "LibXi not found. Disable WITH_X11_XINPUT if you
604       want to build without tablet support")
605     endif()
606   endif()
607
608   if(WITH_X11_XF86VMODE)
609     # XXX, why doesn't cmake make this available?
610     find_library(X11_Xxf86vmode_LIB Xxf86vm   ${X11_LIB_SEARCH_PATH})
611     mark_as_advanced(X11_Xxf86vmode_LIB)
612     if(X11_Xxf86vmode_LIB)
613       list(APPEND PLATFORM_LINKLIBS ${X11_Xxf86vmode_LIB})
614     else()
615       message(FATAL_ERROR "libXxf86vm not found. Disable WITH_X11_XF86VMODE if you
616       want to build without")
617     endif()
618   endif()
619
620   if(WITH_X11_XFIXES)
621     if(X11_Xfixes_LIB)
622       list(APPEND PLATFORM_LINKLIBS ${X11_Xfixes_LIB})
623     else()
624       message(FATAL_ERROR "libXfixes not found. Disable WITH_X11_XFIXES if you
625       want to build without")
626     endif()
627   endif()
628
629   if(WITH_X11_ALPHA)
630     find_library(X11_Xrender_LIB Xrender  ${X11_LIB_SEARCH_PATH})
631     mark_as_advanced(X11_Xrender_LIB)
632     if(X11_Xrender_LIB)
633       list(APPEND PLATFORM_LINKLIBS ${X11_Xrender_LIB})
634     else()
635       message(FATAL_ERROR "libXrender not found. Disable WITH_X11_ALPHA if you
636       want to build without")
637     endif()
638   endif()
639
640 endif()
641
642 # ----------------------------------------------------------------------------
643 # Compilers
644
645 # GNU Compiler
646 if(CMAKE_COMPILER_IS_GNUCC)
647   # ffp-contract=off:
648   # Automatically turned on when building with "-march=native". This is
649   # explicitly turned off here as it will make floating point math give a bit
650   # different results. This will lead to automated test failures. So disable
651   # this until we support it. Seems to default to off in clang and the intel
652   # compiler.
653   set(PLATFORM_CFLAGS "-pipe -fPIC -funsigned-char -fno-strict-aliasing -ffp-contract=off")
654
655   # `maybe-uninitialized` is unreliable in release builds, but fine in debug builds.
656   set(GCC_EXTRA_FLAGS_RELEASE "-Wno-maybe-uninitialized")
657   set(CMAKE_C_FLAGS_RELEASE          "${GCC_EXTRA_FLAGS_RELEASE} ${CMAKE_C_FLAGS_RELEASE}")
658   set(CMAKE_C_FLAGS_RELWITHDEBINFO   "${GCC_EXTRA_FLAGS_RELEASE} ${CMAKE_C_FLAGS_RELWITHDEBINFO}")
659   set(CMAKE_CXX_FLAGS_RELEASE        "${GCC_EXTRA_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_RELEASE}")
660   string(PREPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO "${GCC_EXTRA_FLAGS_RELEASE} ")
661   unset(GCC_EXTRA_FLAGS_RELEASE)
662
663   if(WITH_LINKER_GOLD)
664     execute_process(
665       COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version
666       ERROR_QUIET OUTPUT_VARIABLE LD_VERSION)
667     if("${LD_VERSION}" MATCHES "GNU gold")
668       string(APPEND CMAKE_C_FLAGS " -fuse-ld=gold")
669       string(APPEND CMAKE_CXX_FLAGS " -fuse-ld=gold")
670     else()
671       message(STATUS "GNU gold linker isn't available, using the default system linker.")
672     endif()
673     unset(LD_VERSION)
674   endif()
675
676   if(WITH_LINKER_LLD)
677     execute_process(
678       COMMAND ${CMAKE_C_COMPILER} -fuse-ld=lld -Wl,--version
679       ERROR_QUIET OUTPUT_VARIABLE LD_VERSION)
680     if("${LD_VERSION}" MATCHES "LLD")
681       string(APPEND CMAKE_C_FLAGS " -fuse-ld=lld")
682       string(APPEND CMAKE_CXX_FLAGS " -fuse-ld=lld")
683     else()
684       message(STATUS "LLD linker isn't available, using the default system linker.")
685     endif()
686     unset(LD_VERSION)
687   endif()
688
689 # CLang is the same as GCC for now.
690 elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
691   set(PLATFORM_CFLAGS "-pipe -fPIC -funsigned-char -fno-strict-aliasing")
692 # Intel C++ Compiler
693 elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
694   # think these next two are broken
695   find_program(XIAR xiar)
696   if(XIAR)
697     set(CMAKE_AR "${XIAR}")
698   endif()
699   mark_as_advanced(XIAR)
700
701   find_program(XILD xild)
702   if(XILD)
703     set(CMAKE_LINKER "${XILD}")
704   endif()
705   mark_as_advanced(XILD)
706
707   string(APPEND CMAKE_C_FLAGS " -fp-model precise -prec_div -parallel")
708   string(APPEND CMAKE_CXX_FLAGS " -fp-model precise -prec_div -parallel")
709
710   # string(APPEND PLATFORM_CFLAGS " -diag-enable sc3")
711   set(PLATFORM_CFLAGS "-pipe -fPIC -funsigned-char -fno-strict-aliasing")
712   string(APPEND PLATFORM_LINKFLAGS " -static-intel")
713 endif()
714
715 # Avoid conflicts with Mesa llvmpipe, Luxrender, and other plug-ins that may
716 # use the same libraries as Blender with a different version or build options.
717 set(PLATFORM_LINKFLAGS
718   "${PLATFORM_LINKFLAGS} -Wl,--version-script='${CMAKE_SOURCE_DIR}/source/creator/blender.map'"
719 )
720
721 # Don't use position independent executable for portable install since file
722 # browsers can't properly detect blender as an executable then. Still enabled
723 # for non-portable installs as typically used by Linux distributions.
724 if(WITH_INSTALL_PORTABLE)
725   string(APPEND CMAKE_EXE_LINKER_FLAGS " -no-pie")
726 endif()
727
728 if(WITH_COMPILER_CCACHE)
729   find_program(CCACHE_PROGRAM ccache)
730   if(CCACHE_PROGRAM)
731     # Makefiles and ninja
732     set(CMAKE_C_COMPILER_LAUNCHER   "${CCACHE_PROGRAM}" CACHE STRING "" FORCE)
733     set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}" CACHE STRING "" FORCE)
734   else()
735     message(WARNING "Ccache NOT found, disabling WITH_COMPILER_CCACHE")
736     set(WITH_COMPILER_CCACHE OFF)
737   endif()
738 endif()