ClangFormat: apply to source, most of intern
[blender.git] / intern / cycles / CMakeLists.txt
1 # Standalone or with Blender
2 if(NOT WITH_BLENDER AND WITH_CYCLES_STANDALONE)
3   set(CYCLES_INSTALL_PATH "")
4 else()
5   set(WITH_CYCLES_BLENDER ON)
6   # WINDOWS_PYTHON_DEBUG needs to write into the user addons folder since it will
7   # be started with --env-system-scripts pointing to the release folder, which will
8   # lack the cycles addon, and we don't want to write into it.
9   if(NOT WINDOWS_PYTHON_DEBUG)
10     set(CYCLES_INSTALL_PATH "scripts/addons/cycles")
11   else()
12     set(CYCLES_INSTALL_PATH "$ENV{appdata}/blender foundation/blender/${BLENDER_VERSION}/scripts/addons/cycles")
13   endif()
14 endif()
15
16 # External Libraries
17
18 include(cmake/external_libs.cmake)
19 include(cmake/macros.cmake)
20
21 # Build Flags
22 # todo: this code could be refactored a bit to avoid duplication
23 # note: CXX_HAS_SSE is needed in case passing SSE flags fails altogether (gcc-arm)
24
25 if(WITH_CYCLES_NATIVE_ONLY)
26   set(CXX_HAS_SSE FALSE)
27   set(CXX_HAS_AVX FALSE)
28   set(CXX_HAS_AVX2 FALSE)
29   add_definitions(
30     -DWITH_KERNEL_NATIVE
31   )
32
33   if(NOT MSVC)
34     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
35     set(CYCLES_KERNEL_FLAGS "-march=native")
36   endif()
37 elseif(NOT WITH_CPU_SSE)
38   set(CXX_HAS_SSE FALSE)
39   set(CXX_HAS_AVX FALSE)
40   set(CXX_HAS_AVX2 FALSE)
41 elseif(WIN32 AND MSVC AND NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
42   set(CXX_HAS_SSE TRUE)
43   set(CXX_HAS_AVX TRUE)
44   set(CXX_HAS_AVX2 TRUE)
45
46   # /arch:AVX for VC2012 and above
47   if(NOT MSVC_VERSION LESS 1700)
48     set(CYCLES_AVX_ARCH_FLAGS "/arch:AVX")
49     set(CYCLES_AVX2_ARCH_FLAGS "/arch:AVX /arch:AVX2")
50   elseif(NOT CMAKE_CL_64)
51     set(CYCLES_AVX_ARCH_FLAGS "/arch:SSE2")
52     set(CYCLES_AVX2_ARCH_FLAGS "/arch:SSE2")
53   endif()
54
55   # Unlike GCC/clang we still use fast math, because there is no fine
56   # grained control and the speedup we get here is too big to ignore.
57   set(CYCLES_KERNEL_FLAGS "/fp:fast -D_CRT_SECURE_NO_WARNINGS /GS-")
58
59   # there is no /arch:SSE3, but intrinsics are available anyway
60   if(CMAKE_CL_64)
61     set(CYCLES_SSE2_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS}")
62     set(CYCLES_SSE3_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS}")
63     set(CYCLES_SSE41_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS}")
64     set(CYCLES_AVX_KERNEL_FLAGS "${CYCLES_AVX_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
65     set(CYCLES_AVX2_KERNEL_FLAGS "${CYCLES_AVX2_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
66   else()
67     set(CYCLES_SSE2_KERNEL_FLAGS "/arch:SSE2 ${CYCLES_KERNEL_FLAGS}")
68     set(CYCLES_SSE3_KERNEL_FLAGS "/arch:SSE2 ${CYCLES_KERNEL_FLAGS}")
69     set(CYCLES_SSE41_KERNEL_FLAGS "/arch:SSE2 ${CYCLES_KERNEL_FLAGS}")
70     set(CYCLES_AVX_KERNEL_FLAGS "${CYCLES_AVX_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
71     set(CYCLES_AVX2_KERNEL_FLAGS "${CYCLES_AVX2_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
72   endif()
73
74   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CYCLES_KERNEL_FLAGS}")
75   set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ox")
76   set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /Ox")
77   set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /Ox")
78 elseif(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
79   check_cxx_compiler_flag(-msse CXX_HAS_SSE)
80   check_cxx_compiler_flag(-mavx CXX_HAS_AVX)
81   check_cxx_compiler_flag(-mavx2 CXX_HAS_AVX2)
82
83   # Assume no signal trapping for better code generation.
84   set(CYCLES_KERNEL_FLAGS "-fno-trapping-math")
85   # Avoid overhead of setting errno for NaNs.
86   set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-math-errno")
87   # Let compiler optimize 0.0 - x without worrying about signed zeros.
88   set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-signed-zeros")
89
90   if(CMAKE_COMPILER_IS_GNUCC)
91     # Assume no signal trapping for better code generation.
92     set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-signaling-nans")
93     # Assume a fixed rounding mode for better constant folding.
94     set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-rounding-math")
95   endif()
96
97   if(CXX_HAS_SSE)
98     if(CMAKE_COMPILER_IS_GNUCC)
99       set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -mfpmath=sse")
100     endif()
101
102     set(CYCLES_SSE2_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -msse -msse2")
103     set(CYCLES_SSE3_KERNEL_FLAGS "${CYCLES_SSE2_KERNEL_FLAGS} -msse3 -mssse3")
104     set(CYCLES_SSE41_KERNEL_FLAGS "${CYCLES_SSE3_KERNEL_FLAGS} -msse4.1")
105     if(CXX_HAS_AVX)
106       set(CYCLES_AVX_KERNEL_FLAGS "${CYCLES_SSE41_KERNEL_FLAGS} -mavx")
107     endif()
108     if(CXX_HAS_AVX2)
109       set(CYCLES_AVX2_KERNEL_FLAGS "${CYCLES_SSE41_KERNEL_FLAGS} -mavx -mavx2 -mfma -mlzcnt -mbmi -mbmi2 -mf16c")
110     endif()
111   endif()
112
113   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CYCLES_KERNEL_FLAGS}")
114 elseif(WIN32 AND CMAKE_CXX_COMPILER_ID MATCHES "Intel")
115   check_cxx_compiler_flag(/QxSSE2 CXX_HAS_SSE)
116   check_cxx_compiler_flag(/arch:AVX CXX_HAS_AVX)
117   check_cxx_compiler_flag(/QxCORE-AVX2 CXX_HAS_AVX2)
118
119   if(CXX_HAS_SSE)
120     set(CYCLES_SSE2_KERNEL_FLAGS "/QxSSE2")
121     set(CYCLES_SSE3_KERNEL_FLAGS "/QxSSSE3")
122     set(CYCLES_SSE41_KERNEL_FLAGS "/QxSSE4.1")
123
124     if(CXX_HAS_AVX)
125       set(CYCLES_AVX_KERNEL_FLAGS "/arch:AVX")
126     endif()
127
128     if(CXX_HAS_AVX2)
129       set(CYCLES_AVX2_KERNEL_FLAGS "/QxCORE-AVX2")
130     endif()
131   endif()
132 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
133   if(APPLE)
134     # ICC does not support SSE2 flag on MacOSX
135     check_cxx_compiler_flag(-xssse3 CXX_HAS_SSE)
136   else()
137     check_cxx_compiler_flag(-xsse2 CXX_HAS_SSE)
138   endif()
139
140   check_cxx_compiler_flag(-xavx CXX_HAS_AVX)
141   check_cxx_compiler_flag(-xcore-avx2 CXX_HAS_AVX2)
142
143   if(CXX_HAS_SSE)
144     if(APPLE)
145       # ICC does not support SSE2 flag on MacOSX
146       set(CYCLES_SSE2_KERNEL_FLAGS "-xssse3")
147     else()
148       set(CYCLES_SSE2_KERNEL_FLAGS "-xsse2")
149     endif()
150
151     set(CYCLES_SSE3_KERNEL_FLAGS "-xssse3")
152     set(CYCLES_SSE41_KERNEL_FLAGS "-xsse4.1")
153
154     if(CXX_HAS_AVX)
155       set(CYCLES_AVX_KERNEL_FLAGS "-xavx")
156     endif()
157
158     if(CXX_HAS_AVX2)
159       set(CYCLES_AVX2_KERNEL_FLAGS "-xcore-avx2")
160     endif()
161   endif()
162 endif()
163
164 if(CXX_HAS_SSE)
165   add_definitions(
166     -DWITH_KERNEL_SSE2
167     -DWITH_KERNEL_SSE3
168     -DWITH_KERNEL_SSE41
169   )
170 endif()
171
172 if(CXX_HAS_AVX)
173   add_definitions(-DWITH_KERNEL_AVX)
174 endif()
175
176 if(CXX_HAS_AVX2)
177   add_definitions(-DWITH_KERNEL_AVX2)
178 endif()
179
180 if(WITH_CYCLES_OSL)
181   if(WIN32 AND MSVC)
182     set(RTTI_DISABLE_FLAGS "/GR- -DBOOST_NO_RTTI -DBOOST_NO_TYPEID")
183   elseif(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
184     set(RTTI_DISABLE_FLAGS "-fno-rtti -DBOOST_NO_RTTI -DBOOST_NO_TYPEID")
185   endif()
186 endif()
187
188 # Definitions and Includes
189
190 add_definitions(
191   ${BOOST_DEFINITIONS}
192   ${OPENIMAGEIO_DEFINITIONS}
193 )
194
195 add_definitions(
196   -DCCL_NAMESPACE_BEGIN=namespace\ ccl\ {
197   -DCCL_NAMESPACE_END=}
198 )
199
200 if(WITH_CYCLES_STANDALONE_GUI)
201   add_definitions(-DWITH_CYCLES_STANDALONE_GUI)
202 endif()
203
204 if(WITH_CYCLES_PTEX)
205   add_definitions(-DWITH_PTEX)
206 endif()
207
208 if(WITH_CYCLES_OSL)
209   add_definitions(-DWITH_OSL)
210   #osl 1.9.x
211   add_definitions(-DOSL_STATIC_BUILD)
212   #pre 1.9
213   add_definitions(-DOSL_STATIC_LIBRARY)
214   include_directories(
215     SYSTEM
216     ${OSL_INCLUDE_DIR}
217   )
218 endif()
219
220 if(WITH_CYCLES_EMBREE)
221   add_definitions(-DWITH_EMBREE)
222   add_definitions(-DEMBREE_STATIC_LIB)
223   include_directories(
224     SYSTEM
225     ${EMBREE_INCLUDE_DIRS}
226   )
227 endif()
228
229 if(WITH_OPENSUBDIV)
230   add_definitions(-DWITH_OPENSUBDIV)
231   include_directories(
232     SYSTEM
233     ${OPENSUBDIV_INCLUDE_DIR}
234   )
235 endif()
236
237 if(WITH_CYCLES_STANDALONE)
238   set(WITH_CYCLES_DEVICE_OPENCL TRUE)
239   set(WITH_CYCLES_DEVICE_CUDA TRUE)
240   # Experimental and unfinished.
241   set(WITH_CYCLES_NETWORK FALSE)
242 endif()
243 # TODO(sergey): Consider removing it, only causes confusion in interface.
244 set(WITH_CYCLES_DEVICE_MULTI TRUE)
245
246 # Logging capabilities using GLog library.
247 if(WITH_CYCLES_LOGGING)
248   add_definitions(-DWITH_CYCLES_LOGGING)
249   add_definitions(${GLOG_DEFINES})
250   add_definitions(-DCYCLES_GFLAGS_NAMESPACE=${GFLAGS_NAMESPACE})
251   include_directories(
252     SYSTEM
253     ${GLOG_INCLUDE_DIRS}
254     ${GFLAGS_INCLUDE_DIRS}
255   )
256 endif()
257
258 # Debugging capabilities (debug passes etc).
259 if(WITH_CYCLES_DEBUG)
260   add_definitions(-DWITH_CYCLES_DEBUG)
261 endif()
262
263 if(NOT OPENIMAGEIO_PUGIXML_FOUND)
264   add_definitions(-DWITH_SYSTEM_PUGIXML)
265 endif()
266
267 include_directories(
268   SYSTEM
269   ${BOOST_INCLUDE_DIR}
270   ${OPENIMAGEIO_INCLUDE_DIRS}
271   ${OPENIMAGEIO_INCLUDE_DIRS}/OpenImageIO
272   ${OPENEXR_INCLUDE_DIR}
273   ${OPENEXR_INCLUDE_DIRS}
274   ${PUGIXML_INCLUDE_DIR}
275 )
276
277 if(CYCLES_STANDALONE_REPOSITORY)
278   include_directories(../third_party/atomic)
279 else()
280   include_directories(../atomic)
281 endif()
282
283 # Warnings
284 if(CMAKE_COMPILER_IS_GNUCXX)
285   ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS _has_cxxflag_float_conversion "-Werror=float-conversion")
286   ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS _has_cxxflag_double_promotion "-Werror=double-promotion")
287   ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS _has_no_error_unused_macros "-Wno-error=unused-macros")
288   unset(_has_cxxflag_float_conversion)
289   unset(_has_cxxflag_double_promotion)
290   unset(_has_no_error_unused_macros)
291 endif()
292
293 if(WITH_CYCLES_CUDA_BINARIES AND (NOT WITH_CYCLES_CUBIN_COMPILER))
294   if(MSVC)
295     set(MAX_MSVC 1800)
296     if(${CUDA_VERSION} EQUAL "8.0")
297       set(MAX_MSVC 1900)
298     elseif(${CUDA_VERSION} EQUAL "9.0")
299       set(MAX_MSVC 1910)
300     elseif(${CUDA_VERSION} EQUAL "9.1")
301       set(MAX_MSVC 1911)
302     elseif(${CUDA_VERSION} EQUAL "10.0")
303       set(MAX_MSVC 1999)
304     elseif(${CUDA_VERSION} EQUAL "10.1")
305       set(MAX_MSVC 1999)
306     endif()
307     if(NOT MSVC_VERSION LESS ${MAX_MSVC} OR CMAKE_C_COMPILER_ID MATCHES "Clang")
308       message(STATUS "nvcc not supported for this compiler version, using cycles_cubin_cc instead.")
309       set(WITH_CYCLES_CUBIN_COMPILER ON)
310     endif()
311     unset(MAX_MSVC)
312   elseif(APPLE)
313     if(NOT (${XCODE_VERSION} VERSION_LESS 10.0))
314       message(STATUS "nvcc not supported for this compiler version, using cycles_cubin_cc instead.")
315       set(WITH_CYCLES_CUBIN_COMPILER ON)
316     endif()
317   endif()
318 endif()
319
320 # NVRTC gives wrong rendering result in CUDA 10.0, so we must use NVCC.
321 if(WITH_CYCLES_CUDA_BINARIES AND WITH_CYCLES_CUBIN_COMPILER)
322   if(NOT (${CUDA_VERSION} VERSION_LESS 10.0))
323     message(STATUS "cycles_cubin_cc not supported for CUDA 10.0+, using nvcc instead.")
324     set(WITH_CYCLES_CUBIN_COMPILER OFF)
325   endif()
326 endif()
327
328 # Subdirectories
329
330 if(WITH_CYCLES_BLENDER)
331   add_definitions(-DWITH_BLENDER_GUARDEDALLOC)
332   add_subdirectory(blender)
333 endif()
334
335 if(WITH_CYCLES_NETWORK)
336   add_definitions(-DWITH_NETWORK)
337 endif()
338
339 if(WITH_OPENCOLORIO)
340   add_definitions(-DWITH_OCIO)
341   include_directories(
342     SYSTEM
343     ${OPENCOLORIO_INCLUDE_DIRS}
344   )
345 endif()
346
347 if(WITH_CYCLES_STANDALONE OR WITH_CYCLES_NETWORK OR WITH_CYCLES_CUBIN_COMPILER)
348   add_subdirectory(app)
349 endif()
350
351 add_subdirectory(bvh)
352 add_subdirectory(device)
353 add_subdirectory(doc)
354 add_subdirectory(graph)
355 add_subdirectory(kernel)
356 add_subdirectory(render)
357 add_subdirectory(subd)
358 add_subdirectory(util)
359
360 # TODO(sergey): Make this to work with standalone repository.
361 if(WITH_GTESTS)
362   add_subdirectory(test)
363 endif()
364
365 if(NOT WITH_BLENDER AND WITH_CYCLES_STANDALONE)
366   delayed_do_install(${CMAKE_BINARY_DIR}/bin)
367 endif()