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