Fix Cycles gtests build on macOS.
[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
13 # Build Flags
14 # todo: this code could be refactored a bit to avoid duplication
15 # note: CXX_HAS_SSE is needed in case passing SSE flags fails altogether (gcc-arm)
16
17 if(WITH_CYCLES_NATIVE_ONLY)
18         set(CXX_HAS_SSE FALSE)
19         set(CXX_HAS_AVX FALSE)
20         set(CXX_HAS_AVX2 FALSE)
21         add_definitions(
22                 -DWITH_KERNEL_NATIVE
23         )
24         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
25         set(CYCLES_KERNEL_FLAGS "-march=native")
26 elseif(NOT WITH_CPU_SSE)
27         set(CXX_HAS_SSE FALSE)
28         set(CXX_HAS_AVX FALSE)
29         set(CXX_HAS_AVX2 FALSE)
30 elseif(WIN32 AND MSVC)
31         set(CXX_HAS_SSE TRUE)
32         set(CXX_HAS_AVX TRUE)
33         set(CXX_HAS_AVX2 TRUE)
34
35         # /arch:AVX for VC2012 and above
36         if(NOT MSVC_VERSION LESS 1700)
37                 set(CYCLES_AVX_ARCH_FLAGS "/arch:AVX")
38                 set(CYCLES_AVX2_ARCH_FLAGS "/arch:AVX /arch:AVX2")
39         elseif(NOT CMAKE_CL_64)
40                 set(CYCLES_AVX_ARCH_FLAGS "/arch:SSE2")
41                 set(CYCLES_AVX2_ARCH_FLAGS "/arch:SSE2")
42         endif()
43
44         # Unlike GCC/clang we still use fast math, because there is no fine
45         # grained control and the speedup we get here is too big to ignore.
46         set(CYCLES_KERNEL_FLAGS "/fp:fast -D_CRT_SECURE_NO_WARNINGS /GS-")
47
48         # there is no /arch:SSE3, but intrinsics are available anyway
49         if(CMAKE_CL_64)
50                 set(CYCLES_SSE2_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS}")
51                 set(CYCLES_SSE3_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS}")
52                 set(CYCLES_SSE41_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS}")
53                 set(CYCLES_AVX_KERNEL_FLAGS "${CYCLES_AVX_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
54                 set(CYCLES_AVX2_KERNEL_FLAGS "${CYCLES_AVX2_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
55         else()
56                 set(CYCLES_SSE2_KERNEL_FLAGS "/arch:SSE2 ${CYCLES_KERNEL_FLAGS}")
57                 set(CYCLES_SSE3_KERNEL_FLAGS "/arch:SSE2 ${CYCLES_KERNEL_FLAGS}")
58                 set(CYCLES_SSE41_KERNEL_FLAGS "/arch:SSE2 ${CYCLES_KERNEL_FLAGS}")
59                 set(CYCLES_AVX_KERNEL_FLAGS "${CYCLES_AVX_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
60                 set(CYCLES_AVX2_KERNEL_FLAGS "${CYCLES_AVX2_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
61         endif()
62
63         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CYCLES_KERNEL_FLAGS}")
64         set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ox")
65         set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /Ox")
66         set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /Ox")
67 elseif(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
68         check_cxx_compiler_flag(-msse CXX_HAS_SSE)
69         check_cxx_compiler_flag(-mavx CXX_HAS_AVX)
70         check_cxx_compiler_flag(-mavx2 CXX_HAS_AVX2)
71
72         # Assume no signal trapping for better code generation.
73         set(CYCLES_KERNEL_FLAGS "-fno-trapping-math")
74         # Avoid overhead of setting errno for NaNs.
75         set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-math-errno")
76         # Let compiler optimize 0.0 - x without worrying about signed zeros.
77         set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-signed-zeros")
78
79         if(CMAKE_COMPILER_IS_GNUCC)
80                 # Assume no signal trapping for better code generation.
81                 set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-signaling-nans")
82                 # Assume a fixed rounding mode for better constant folding.
83                 set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-rounding-math")
84         endif()
85
86         if(CXX_HAS_SSE)
87                 if(CMAKE_COMPILER_IS_GNUCC)
88                         set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -mfpmath=sse")
89                 endif()
90
91                 set(CYCLES_SSE2_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -msse -msse2")
92                 set(CYCLES_SSE3_KERNEL_FLAGS "${CYCLES_SSE2_KERNEL_FLAGS} -msse3 -mssse3")
93                 set(CYCLES_SSE41_KERNEL_FLAGS "${CYCLES_SSE3_KERNEL_FLAGS} -msse4.1")
94                 if(CXX_HAS_AVX)
95                         set(CYCLES_AVX_KERNEL_FLAGS "${CYCLES_SSE41_KERNEL_FLAGS} -mavx")
96                 endif()
97                 if(CXX_HAS_AVX2)
98                         set(CYCLES_AVX2_KERNEL_FLAGS "${CYCLES_SSE41_KERNEL_FLAGS} -mavx -mavx2 -mfma -mlzcnt -mbmi -mbmi2 -mf16c")
99                 endif()
100         endif()
101
102         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CYCLES_KERNEL_FLAGS}")
103 endif()
104
105 if(CXX_HAS_SSE)
106         add_definitions(
107                 -DWITH_KERNEL_SSE2
108                 -DWITH_KERNEL_SSE3
109                 -DWITH_KERNEL_SSE41
110         )
111 endif()
112
113 if(CXX_HAS_AVX)
114         add_definitions(-DWITH_KERNEL_AVX)
115 endif()
116
117 if(CXX_HAS_AVX2)
118         add_definitions(-DWITH_KERNEL_AVX2)
119 endif()
120
121 if(WITH_CYCLES_OSL)
122         if(WIN32 AND MSVC)
123                 set(RTTI_DISABLE_FLAGS "/GR- -DBOOST_NO_RTTI -DBOOST_NO_TYPEID")
124         elseif(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
125                 set(RTTI_DISABLE_FLAGS "-fno-rtti -DBOOST_NO_RTTI -DBOOST_NO_TYPEID")
126         endif()
127 endif()
128
129 # Definitions and Includes
130
131 add_definitions(
132         ${BOOST_DEFINITIONS}
133         ${OPENIMAGEIO_DEFINITIONS}
134 )
135
136 add_definitions(
137         -DCCL_NAMESPACE_BEGIN=namespace\ ccl\ {
138         -DCCL_NAMESPACE_END=}
139 )
140
141 if(WITH_CYCLES_STANDALONE_GUI)
142         add_definitions(-DWITH_CYCLES_STANDALONE_GUI)
143 endif()
144
145 if(WITH_CYCLES_PTEX)
146         add_definitions(-DWITH_PTEX)
147 endif()
148
149 if(WITH_CYCLES_OSL)
150         add_definitions(-DWITH_OSL)
151         add_definitions(-DOSL_STATIC_LIBRARY)
152         include_directories(
153                 SYSTEM
154                 ${OSL_INCLUDE_DIR}
155         )
156 endif()
157
158 if(WITH_CYCLES_OPENSUBDIV)
159         add_definitions(-DWITH_OPENSUBDIV)
160         include_directories(
161                 SYSTEM
162                 ${OPENSUBDIV_INCLUDE_DIR}
163         )
164 endif()
165
166 set(WITH_CYCLES_DEVICE_OPENCL TRUE)
167 set(WITH_CYCLES_DEVICE_CUDA TRUE)
168 set(WITH_CYCLES_DEVICE_MULTI TRUE)
169
170 if(CYCLES_STANDALONE_REPOSITORY)
171         TEST_UNORDERED_MAP_SUPPORT()
172 endif()
173 if(WITH_CXX11)
174         add_definitions(-DCYCLES_STD_UNORDERED_MAP)
175 elseif(HAVE_STD_UNORDERED_MAP_HEADER)
176         if(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
177                 add_definitions(-DCYCLES_STD_UNORDERED_MAP)
178         else()
179                 if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
180                         add_definitions(-DCYCLES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
181                 else()
182                         add_definitions(-DCYCLES_NO_UNORDERED_MAP)
183                         message(STATUS "Replacing unordered_map/set with map/set (warning: slower!)")
184                 endif()
185         endif()
186 else()
187         if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
188                 add_definitions(-DCYCLES_TR1_UNORDERED_MAP)
189         else()
190                 add_definitions(-DCYCLES_NO_UNORDERED_MAP)
191                 message(STATUS "Replacing unordered_map/set with map/set (warning: slower!)")
192         endif()
193 endif()
194
195 # Logging capabilities using GLog library.
196 if(WITH_CYCLES_LOGGING)
197         add_definitions(-DWITH_CYCLES_LOGGING)
198         add_definitions(${GLOG_DEFINES})
199         add_definitions(-DCYCLES_GFLAGS_NAMESPACE=${GFLAGS_NAMESPACE})
200         include_directories(
201                 SYSTEM
202                 ${GLOG_INCLUDE_DIRS}
203                 ${GFLAGS_INCLUDE_DIRS}
204         )
205 endif()
206
207 # Debugging capabilities (debug passes etc).
208 if(WITH_CYCLES_DEBUG)
209         add_definitions(-DWITH_CYCLES_DEBUG)
210 endif()
211
212 include_directories(
213         SYSTEM
214         ${BOOST_INCLUDE_DIR}
215         ${OPENIMAGEIO_INCLUDE_DIRS}
216         ${OPENIMAGEIO_INCLUDE_DIRS}/OpenImageIO
217         ${OPENEXR_INCLUDE_DIR}
218         ${OPENEXR_INCLUDE_DIRS}
219         ${PUGIXML_INCLUDE_DIR}
220 )
221
222 if(CYCLES_STANDALONE_REPOSITORY)
223         include_directories(../third_party/atomic)
224 else()
225         include_directories(../atomic)
226 endif()
227
228 # Warnings
229 if(CMAKE_COMPILER_IS_GNUCXX)
230         ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS _has_cxxflag_float_conversion "-Werror=float-conversion")
231         ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS _has_cxxflag_double_promotion "-Werror=double-promotion")
232         ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS _has_no_error_unused_macros "-Wno-error=unused-macros")
233         unset(_has_cxxflag_float_conversion)
234         unset(_has_cxxflag_double_promotion)
235         unset(_has_no_error_unused_macros)
236 endif()
237
238
239 # Subdirectories
240
241 if(WITH_CYCLES_BLENDER)
242         add_definitions(-DWITH_BLENDER_GUARDEDALLOC)
243         add_subdirectory(blender)
244 endif()
245
246 if(WITH_CYCLES_NETWORK)
247         add_definitions(-DWITH_NETWORK)
248 endif()
249
250 if(WITH_CYCLES_STANDALONE OR WITH_CYCLES_NETWORK)
251         add_subdirectory(app)
252 endif()
253
254 add_subdirectory(bvh)
255 add_subdirectory(device)
256 add_subdirectory(doc)
257 add_subdirectory(graph)
258 add_subdirectory(kernel)
259 add_subdirectory(render)
260 add_subdirectory(subd)
261 add_subdirectory(util)
262
263 # TODO(sergey): Make this to work with standalone repository.
264 if(WITH_GTESTS)
265         add_subdirectory(test)
266 endif()
267
268 if(NOT WITH_BLENDER AND WITH_CYCLES_STANDALONE)
269         delayed_do_install(${CMAKE_BINARY_DIR}/bin)
270 endif()
271