blob: 62d3a935fa88c4ac3aa0067e73743fd57689581e [file] [log] [blame]
Ryan Harrisone87ac762022-04-06 15:37:27 -04001# Copyright 2022 The Dawn & Tint Authors
Corentin Wallez7fe6efb2020-02-05 17:16:05 +00002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Dan Sinclair6e581892020-03-02 15:47:43 -050015cmake_minimum_required(VERSION 3.10.2)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000016
dan sinclairfb5a4922022-04-19 22:25:45 +000017# When upgrading to CMake 3.11 we can remove DAWN_PLACEHOLDER_FILE because source-less add_library
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000018# becomes available.
19# When upgrading to CMake 3.12 we should add CONFIGURE_DEPENDS to DawnGenerator to rerun CMake in
Corentin Wallez42450c62020-04-10 17:04:31 +000020# case any of the generator files changes. We should also remove the CACHE "" FORCE stuff to
dan sinclair1877c272020-10-20 19:46:21 +000021# override options in third_party dependencies. We can also add the HOMEPAGE_URL
22# entry to the project `HOMEPAGE_URL "https://dawn.googlesource.com/dawn"`
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000023
24project(
25 Dawn
26 DESCRIPTION "Dawn, a WebGPU implementation"
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000027 LANGUAGES C CXX
28)
Dan Sinclair6e581892020-03-02 15:47:43 -050029enable_testing()
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000030
31set_property(GLOBAL PROPERTY USE_FOLDERS ON)
32
Dan Sinclair6e581892020-03-02 15:47:43 -050033set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
34set(CMAKE_POSITION_INDEPENDENT_CODE ON)
Ben Clayton7b0686a2022-01-06 09:23:11 +000035set(CMAKE_CXX_STANDARD 17)
Dan Sinclair6e581892020-03-02 15:47:43 -050036set(CMAKE_DEBUG_POSTFIX "")
37
38if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
39 message(STATUS "No build type selected, default to Debug")
40 set(CMAKE_BUILD_TYPE "Debug")
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000041endif()
42
43set(DAWN_BUILD_GEN_DIR "${Dawn_BINARY_DIR}/gen")
44set(DAWN_GENERATOR_DIR "${Dawn_SOURCE_DIR}/generator")
45set(DAWN_SRC_DIR "${Dawn_SOURCE_DIR}/src")
Ben Clayton9fb7a512022-02-04 18:18:18 +000046set(DAWN_INCLUDE_DIR "${Dawn_SOURCE_DIR}/include")
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000047set(DAWN_TEMPLATE_DIR "${DAWN_GENERATOR_DIR}/templates")
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000048
dan sinclairfb5a4922022-04-19 22:25:45 +000049set(DAWN_PLACEHOLDER_FILE "${DAWN_SRC_DIR}/Placeholder.cpp")
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000050
51################################################################################
52# Configuration options
53################################################################################
54
Ben Clayton30eeac72021-09-24 10:38:18 +000055# option_if_not_defined(name description default)
56# Behaves like:
57# option(name description default)
58# If a variable is not already defined with the given name, otherwise the
59# function does nothing.
60# Simplifies customization by projects that use Dawn as a dependency.
61function (option_if_not_defined name description default)
62 if(NOT DEFINED ${name})
63 option(${name} ${description} ${default})
64 endif()
65endfunction()
66
67# set_if_not_defined(name value description)
68# Behaves like:
69# set(${name} ${value} CACHE STRING ${description})
70# If a variable is not already defined with the given name, otherwise the
71# function does nothing.
72# Simplifies customization by projects that use Dawn as a dependency.
73function (set_if_not_defined name value description)
74 if(NOT DEFINED ${name})
75 set(${name} ${value} CACHE STRING ${description})
76 endif()
77endfunction()
78
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000079# Default values for the backend-enabling options
80set(ENABLE_D3D12 OFF)
81set(ENABLE_METAL OFF)
Sergey Karchevsky3a75b1c2021-06-30 15:06:43 +000082set(ENABLE_OPENGLES OFF)
83set(ENABLE_DESKTOP_GL OFF)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000084set(ENABLE_VULKAN OFF)
Corentin Wallez2e22d922022-06-01 09:30:50 +000085set(USE_WAYLAND OFF)
Corentin Wallezd353ca02020-02-18 02:12:35 +000086set(USE_X11 OFF)
Ben Claytona6750752022-02-04 18:35:55 +000087set(BUILD_SAMPLES OFF)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000088if (WIN32)
89 set(ENABLE_D3D12 ON)
陈俊嘉610de1d2021-06-28 08:19:28 +000090 if (NOT WINDOWS_STORE)
陈俊嘉b2bc57a2021-06-24 07:00:16 +000091 # Enable Vulkan in win32 compilation only
92 # since UWP only supports d3d
93 set(ENABLE_VULKAN ON)
94 endif()
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000095elseif(APPLE)
96 set(ENABLE_METAL ON)
Alexander Vestinf2556ab2022-03-25 13:18:46 +000097elseif(ANDROID)
98 set(ENABLE_VULKAN ON)
99 set(ENABLE_OPENGLES ON)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000100elseif(UNIX)
Sergey Karchevsky3a75b1c2021-06-30 15:06:43 +0000101 set(ENABLE_OPENGLES ON)
102 set(ENABLE_DESKTOP_GL ON)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000103 set(ENABLE_VULKAN ON)
Corentin Wallezd353ca02020-02-18 02:12:35 +0000104 set(USE_X11 ON)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000105endif()
106
陈俊嘉b2bc57a2021-06-24 07:00:16 +0000107# GLFW is not supported in UWP
Ryan Harrisone87ac762022-04-06 15:37:27 -0400108if ((WIN32 AND NOT WINDOWS_STORE) OR UNIX AND NOT ANDROID)
陈俊嘉b2bc57a2021-06-24 07:00:16 +0000109 set(DAWN_SUPPORTS_GLFW_FOR_WINDOWING ON)
110endif()
111
112# Current examples are depend on GLFW
113if (DAWN_SUPPORTS_GLFW_FOR_WINDOWING)
Ben Claytona6750752022-02-04 18:35:55 +0000114 set(BUILD_SAMPLES ON)
陈俊嘉b2bc57a2021-06-24 07:00:16 +0000115endif()
116
Ben Clayton96e245e2022-04-08 18:08:36 +0000117option_if_not_defined(DAWN_ENABLE_MSAN "Enable memory sanitizer" OFF)
118option_if_not_defined(DAWN_ENABLE_ASAN "Enable address sanitizer" OFF)
119option_if_not_defined(DAWN_ENABLE_UBSAN "Enable undefined behaviour sanitizer" OFF)
120
Ben Clayton30eeac72021-09-24 10:38:18 +0000121option_if_not_defined(DAWN_ENABLE_D3D12 "Enable compilation of the D3D12 backend" ${ENABLE_D3D12})
122option_if_not_defined(DAWN_ENABLE_METAL "Enable compilation of the Metal backend" ${ENABLE_METAL})
123option_if_not_defined(DAWN_ENABLE_NULL "Enable compilation of the Null backend" ON)
124option_if_not_defined(DAWN_ENABLE_DESKTOP_GL "Enable compilation of the OpenGL backend" ${ENABLE_DESKTOP_GL})
125option_if_not_defined(DAWN_ENABLE_OPENGLES "Enable compilation of the OpenGL ES backend" ${ENABLE_OPENGLES})
126option_if_not_defined(DAWN_ENABLE_VULKAN "Enable compilation of the Vulkan backend" ${ENABLE_VULKAN})
dan sinclair194c1772022-06-21 17:54:03 +0000127
Ben Clayton30eeac72021-09-24 10:38:18 +0000128option_if_not_defined(DAWN_ALWAYS_ASSERT "Enable assertions on all build types" OFF)
Corentin Wallez2e22d922022-06-01 09:30:50 +0000129option_if_not_defined(DAWN_USE_WAYLAND "Enable support for Wayland surface" ${USE_WAYLAND})
Ben Clayton30eeac72021-09-24 10:38:18 +0000130option_if_not_defined(DAWN_USE_X11 "Enable support for X11 surface" ${USE_X11})
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000131
Ben Claytona6750752022-02-04 18:35:55 +0000132option_if_not_defined(DAWN_BUILD_SAMPLES "Enables building Dawn's samples" ${BUILD_SAMPLES})
Ben Claytonaffb7a32021-09-28 11:14:42 +0000133option_if_not_defined(DAWN_BUILD_NODE_BINDINGS "Enables building Dawn's NodeJS bindings" OFF)
Corentin Wallezbe352ea2022-04-11 16:48:43 +0000134option_if_not_defined(DAWN_ENABLE_SWIFTSHADER "Enables building Swiftshader as part of the build and Vulkan adapter discovery" OFF)
Ben Claytonaffb7a32021-09-28 11:14:42 +0000135
136option_if_not_defined(DAWN_ENABLE_PIC "Build with Position-Independent-Code enabled" OFF)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000137
dan sinclair194c1772022-06-21 17:54:03 +0000138if (DAWN_ENABLE_OPENGLES OR DAWN_ENABLE_DESKTOP_GL)
139 set(TINT_DEFAULT_GLSL ON)
140else()
141 set(TINT_DEFAULT_GLSL OFF)
142endif()
143
144option_if_not_defined(TINT_BUILD_SAMPLES "Build samples" ${DAWN_BUILD_SAMPLES})
145option_if_not_defined(TINT_BUILD_DOCS "Build documentation" ON)
146option_if_not_defined(TINT_DOCS_WARN_AS_ERROR "When building documentation, treat warnings as errors" OFF)
147
148option_if_not_defined(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" ${DAWN_ENABLE_VULKAN})
149option_if_not_defined(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
150option_if_not_defined(TINT_BUILD_GLSL_WRITER "Build the GLSL output writer" ${TINT_DEFAULT_GLSL})
151option_if_not_defined(TINT_BUILD_HLSL_WRITER "Build the HLSL output writer" ${DAWN_ENABLE_D3D12})
152option_if_not_defined(TINT_BUILD_MSL_WRITER "Build the MSL output writer" ${DAWN_ENABLE_METAL})
153option_if_not_defined(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ${DAWN_ENABLE_VULKAN})
154option_if_not_defined(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
155
156option_if_not_defined(TINT_BUILD_FUZZERS "Build fuzzers" OFF)
157option_if_not_defined(TINT_BUILD_SPIRV_TOOLS_FUZZER "Build SPIRV-Tools fuzzer" OFF)
158option_if_not_defined(TINT_BUILD_AST_FUZZER "Build AST fuzzer" OFF)
159option_if_not_defined(TINT_BUILD_REGEX_FUZZER "Build regex fuzzer" OFF)
160option_if_not_defined(TINT_BUILD_BENCHMARKS "Build benchmarks" OFF)
161option_if_not_defined(TINT_BUILD_TESTS "Build tests" ON)
162option_if_not_defined(TINT_BUILD_AS_OTHER_OS "Override OS detection to force building of *_other.cc files" OFF)
163option_if_not_defined(TINT_BUILD_REMOTE_COMPILE "Build the remote-compile tool for validating shaders on a remote machine" OFF)
164
165option_if_not_defined(TINT_ENABLE_BREAK_IN_DEBUGGER "Enable tint::debugger::Break()" OFF)
166option_if_not_defined(TINT_EMIT_COVERAGE "Emit code coverage information" OFF)
167option_if_not_defined(TINT_CHECK_CHROMIUM_STYLE "Check for [chromium-style] issues during build" OFF)
168option_if_not_defined(TINT_SYMBOL_STORE_DEBUG_NAME "Enable storing of name in tint::ast::Symbol to help debugging the AST" OFF)
Corentin Walleze5570872020-10-20 14:26:10 +0000169
Brandon Jonesa04663c2021-09-23 20:36:03 +0000170# Recommended setting for compability with future abseil releases.
171set(ABSL_PROPAGATE_CXX_STD ON)
Brandon Jonesa04663c2021-09-23 20:36:03 +0000172
dan sinclair194c1772022-06-21 17:54:03 +0000173set_if_not_defined(DAWN_THIRD_PARTY_DIR "${Dawn_SOURCE_DIR}/third_party" "Directory in which to find third-party dependencies.")
Ben Clayton30eeac72021-09-24 10:38:18 +0000174set_if_not_defined(DAWN_ABSEIL_DIR "${DAWN_THIRD_PARTY_DIR}/abseil-cpp" "Directory in which to find Abseil")
175set_if_not_defined(DAWN_GLFW_DIR "${DAWN_THIRD_PARTY_DIR}/glfw" "Directory in which to find GLFW")
Ben Clayton30eeac72021-09-24 10:38:18 +0000176set_if_not_defined(DAWN_JINJA2_DIR "${DAWN_THIRD_PARTY_DIR}/jinja2" "Directory in which to find Jinja2")
Stephen White94349492022-06-29 15:29:41 +0000177set_if_not_defined(DAWN_KHRONOS_DIR "${DAWN_THIRD_PARTY_DIR}/khronos" "Directory in which to find Khronos GL headers")
Ben Clayton30eeac72021-09-24 10:38:18 +0000178set_if_not_defined(DAWN_SPIRV_HEADERS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-headers/src" "Directory in which to find SPIRV-Headers")
179set_if_not_defined(DAWN_SPIRV_TOOLS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-tools/src" "Directory in which to find SPIRV-Tools")
Corentin Wallezbe352ea2022-04-11 16:48:43 +0000180set_if_not_defined(DAWN_SWIFTSHADER_DIR "${DAWN_THIRD_PARTY_DIR}/swiftshader" "Directory in which to find swiftshader")
Ryan Harrisone87ac762022-04-06 15:37:27 -0400181set_if_not_defined(DAWN_TINT_DIR "${Dawn_SOURCE_DIR}" "Directory in which to find Tint")
Loko Kung23d09c62022-04-09 00:10:08 +0000182set_if_not_defined(DAWN_VULKAN_DEPS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps" "Directory in which to find vulkan-deps")
183set_if_not_defined(DAWN_VULKAN_HEADERS_DIR "${DAWN_VULKAN_DEPS_DIR}/vulkan-headers/src" "Directory in which to find Vulkan-Headers")
Loko Kung83a5d522022-04-13 19:14:46 +0000184set_if_not_defined(DAWN_VULKAN_TOOLS_DIR "${DAWN_VULKAN_DEPS_DIR}/vulkan-tools/src" "Directory in which to find Vulkan-Tools")
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000185
Ben Claytonaffb7a32021-09-28 11:14:42 +0000186# Dependencies for DAWN_BUILD_NODE_BINDINGS
187set_if_not_defined(NODE_ADDON_API_DIR "${DAWN_THIRD_PARTY_DIR}/node-addon-api" "Directory in which to find node-addon-api")
Ben Clayton72e3ba62021-09-30 18:04:01 +0000188set_if_not_defined(NODE_API_HEADERS_DIR "${DAWN_THIRD_PARTY_DIR}/node-api-headers" "Directory in which to find node-api-headers")
Ben Claytonaffb7a32021-09-28 11:14:42 +0000189set_if_not_defined(WEBGPU_IDL_PATH "${DAWN_THIRD_PARTY_DIR}/gpuweb/webgpu.idl" "Path to the webgpu.idl definition file")
Austin Eng4948c812021-10-15 14:28:32 +0000190set_if_not_defined(GO_EXECUTABLE "go" "Golang executable for running the IDL generator")
Ben Claytonaffb7a32021-09-28 11:14:42 +0000191
Sergey Karchevsky3a75b1c2021-06-30 15:06:43 +0000192# Much of the backend code is shared among desktop OpenGL and OpenGL ES
193if (${DAWN_ENABLE_DESKTOP_GL} OR ${DAWN_ENABLE_OPENGLES})
194 set(DAWN_ENABLE_OPENGL ON)
195endif()
196
Ben Claytonaffb7a32021-09-28 11:14:42 +0000197if(DAWN_ENABLE_PIC)
198 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
199endif()
200
dan sinclair194c1772022-06-21 17:54:03 +0000201if (${TINT_BUILD_SPIRV_TOOLS_FUZZER})
202 message(STATUS "TINT_BUILD_SPIRV_TOOLS_FUZZER is ON - setting
203 TINT_BUILD_FUZZERS
204 TINT_BUILD_SPV_READER
205 TINT_BUILD_SPV_WRITER
206 TINT_BUILD_WGSL_READER
207 TINT_BUILD_WGSL_WRITER
208 TINT_BUILD_GLSL_WRITER
209 TINT_BUILD_HLSL_WRITER
210 TINT_BUILD_MSL_WRITER to ON")
211 set(TINT_BUILD_FUZZERS ON CACHE BOOL "Build tint fuzzers" FORCE)
212 set(TINT_BUILD_SPV_READER ON CACHE BOOL "Build SPIR-V reader" FORCE)
213 set(TINT_BUILD_SPV_WRITER ON CACHE BOOL "Build SPIR-V writer" FORCE)
214 set(TINT_BUILD_WGSL_READER ON CACHE BOOL "Build WGSL reader" FORCE)
215 set(TINT_BUILD_WGSL_WRITER ON CACHE BOOL "Build WGSL writer" FORCE)
216 set(TINT_BUILD_GLSL_WRITER ON CACHE BOOL "Build HLSL writer" FORCE)
217 set(TINT_BUILD_HLSL_WRITER ON CACHE BOOL "Build HLSL writer" FORCE)
218 set(TINT_BUILD_MSL_WRITER ON CACHE BOOL "Build MSL writer" FORCE)
219endif()
220
221if (${TINT_BUILD_AST_FUZZER})
222 message(STATUS "TINT_BUILD_AST_FUZZER is ON - setting
223 TINT_BUILD_FUZZERS
224 TINT_BUILD_WGSL_READER
225 TINT_BUILD_WGSL_WRITER
226 TINT_BUILD_SPV_WRITER
227 TINT_BUILD_MSL_WRITER
228 TINT_BUILD_GLSL_WRITER
229 TINT_BUILD_HLSL_WRITER to ON")
230 set(TINT_BUILD_FUZZERS ON CACHE BOOL "Build tint fuzzers" FORCE)
231 set(TINT_BUILD_WGSL_READER ON CACHE BOOL "Build WGSL reader" FORCE)
232 set(TINT_BUILD_WGSL_WRITER ON CACHE BOOL "Build WGSL writer" FORCE)
233 set(TINT_BUILD_SPV_WRITER ON CACHE BOOL "Build SPIR-V writer" FORCE)
234 set(TINT_BUILD_MSL_WRITER ON CACHE BOOL "Build MSL writer" FORCE)
235 set(TINT_BUILD_GLSL_WRITER ON CACHE BOOL "Build GLSL writer" FORCE)
236 set(TINT_BUILD_HLSL_WRITER ON CACHE BOOL "Build HLSL writer" FORCE)
237endif()
238
239if (${TINT_BUILD_REGEX_FUZZER})
240 message(STATUS "TINT_BUILD_REGEX_FUZZER is ON - setting
241 TINT_BUILD_FUZZERS
242 TINT_BUILD_WGSL_READER
243 TINT_BUILD_WGSL_WRITER
244 TINT_BUILD_SPV_WRITER
245 TINT_BUILD_MSL_WRITER
246 TINT_BUILD_GLSL_WRITER
247 TINT_BUILD_HLSL_WRITER to ON")
248 set(TINT_BUILD_FUZZERS ON CACHE BOOL "Build tint fuzzers" FORCE)
249 set(TINT_BUILD_WGSL_READER ON CACHE BOOL "Build WGSL reader" FORCE)
250 set(TINT_BUILD_WGSL_WRITER ON CACHE BOOL "Build WGSL writer" FORCE)
251 set(TINT_BUILD_SPV_WRITER ON CACHE BOOL "Build SPIR-V writer" FORCE)
252 set(TINT_BUILD_MSL_WRITER ON CACHE BOOL "Build MSL writer" FORCE)
253 set(TINT_BUILD_GLSL_WRITER ON CACHE BOOL "Build GLSL writer" FORCE)
254 set(TINT_BUILD_HLSL_WRITER ON CACHE BOOL "Build HLSL writer" FORCE)
255endif()
256
257message(STATUS "Dawn build D3D12 backend: ${DAWN_ENABLE_D3D12}")
258message(STATUS "Dawn build Metal backend: ${DAWN_ENABLE_METAL}")
259message(STATUS "Dawn build Vulkan backend: ${DAWN_ENABLE_VULKAN}")
260message(STATUS "Dawn build OpenGL backend: ${DAWN_ENABLE_DESKTOP_GL}")
261message(STATUS "Dawn build OpenGL ES backend: ${DAWN_ENABLE_OPENGLES}")
262message(STATUS "Dawn build Null backend: ${DAWN_ENABLE_NULL}")
263
264message(STATUS "Dawn build with asserts in all configurations: ${DAWN_ALWAYS_ASSERT}")
265message(STATUS "Dawn build Wayland support: ${DAWN_USE_WAYLAND}")
266message(STATUS "Dawn build X11 support: ${DAWN_USE_X11}")
267
268message(STATUS "Dawn build samples: ${DAWN_BUILD_SAMPLES}")
269message(STATUS "Dawn build Node bindings: ${DAWN_BUILD_NODE_BINDINGS}")
270message(STATUS "Dawn build Swiftshader: ${DAWN_ENABLE_SWIFTSHADER}")
271
272message(STATUS "Dawn build PIC: ${DAWN_ENABLE_PIC}")
273
274message(STATUS "Dawn build with ASAN: ${DAWN_ENABLE_ASAN}")
275message(STATUS "Dawn build with MSAN: ${DAWN_ENABLE_MSAN}")
276message(STATUS "Dawn build with UBSAN: ${DAWN_ENABLE_UBSAN}")
277
278message(STATUS "Tint build samples: ${TINT_BUILD_SAMPLES}")
279message(STATUS "Tint build docs: ${TINT_BUILD_DOCS}")
280message(STATUS "Tint build docs with warn as error: ${TINT_DOCS_WARN_AS_ERROR}")
281message(STATUS "Tint build SPIR-V reader: ${TINT_BUILD_SPV_READER}")
282message(STATUS "Tint build WGSL reader: ${TINT_BUILD_WGSL_READER}")
283message(STATUS "Tint build GLSL writer: ${TINT_BUILD_GLSL_WRITER}")
284message(STATUS "Tint build HLSL writer: ${TINT_BUILD_HLSL_WRITER}")
285message(STATUS "Tint build MSL writer: ${TINT_BUILD_MSL_WRITER}")
286message(STATUS "Tint build SPIR-V writer: ${TINT_BUILD_SPV_WRITER}")
287message(STATUS "Tint build WGSL writer: ${TINT_BUILD_WGSL_WRITER}")
288message(STATUS "Tint build fuzzers: ${TINT_BUILD_FUZZERS}")
289message(STATUS "Tint build SPIRV-Tools fuzzer: ${TINT_BUILD_SPIRV_TOOLS_FUZZER}")
290message(STATUS "Tint build AST fuzzer: ${TINT_BUILD_AST_FUZZER}")
291message(STATUS "Tint build regex fuzzer: ${TINT_BUILD_REGEX_FUZZER}")
292message(STATUS "Tint build benchmarks: ${TINT_BUILD_BENCHMARKS}")
293message(STATUS "Tint build tests: ${TINT_BUILD_TESTS}")
294message(STATUS "Tint build checking [chromium-style]: ${TINT_CHECK_CHROMIUM_STYLE}")
295message(STATUS "Tint build remote-compile tool: ${TINT_BUILD_REMOTE_COMPILE}")
296
297if (NOT ${TINT_LIB_FUZZING_ENGINE_LINK_OPTIONS} STREQUAL "")
298 message(STATUS "Using provided LIB_FUZZING_ENGINE options: ${TINT_LIB_FUZZING_ENGINE_LINK_OPTIONS}")
299endif()
300
301message(STATUS "Using python3")
302find_package(PythonInterp 3 REQUIRED)
303
304
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000305################################################################################
Ben Clayton96e245e2022-04-08 18:08:36 +0000306# common_compile_options - sets compiler and linker options common for dawn and
307# tint on the given target
308################################################################################
309function(common_compile_options TARGET)
310 if (COMPILER_IS_LIKE_GNU)
311 target_compile_options(${TARGET} PRIVATE
312 -fno-exceptions
313 -fno-rtti
Corentin Wallez2b3dcf42022-05-16 09:16:41 +0000314 -fvisibility-inlines-hidden
Ben Clayton96e245e2022-04-08 18:08:36 +0000315 )
316
317 if (${DAWN_ENABLE_MSAN})
318 target_compile_options(${TARGET} PRIVATE -fsanitize=memory)
319 target_link_options(${TARGET} PRIVATE -fsanitize=memory)
320 elseif (${DAWN_ENABLE_ASAN})
321 target_compile_options(${TARGET} PRIVATE -fsanitize=address)
322 target_link_options(${TARGET} PRIVATE -fsanitize=address)
323 elseif (${DAWN_ENABLE_UBSAN})
324 target_compile_options(${TARGET} PRIVATE -fsanitize=undefined)
325 target_link_options(${TARGET} PRIVATE -fsanitize=undefined)
326 endif()
327 endif(COMPILER_IS_LIKE_GNU)
328endfunction()
329
330################################################################################
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000331# Dawn's public and internal "configs"
332################################################################################
333
334# The public config contains only the include paths for the Dawn headers.
335add_library(dawn_public_config INTERFACE)
336target_include_directories(dawn_public_config INTERFACE
Ben Clayton9fb7a512022-02-04 18:18:18 +0000337 "${DAWN_INCLUDE_DIR}"
338 "${DAWN_BUILD_GEN_DIR}/include"
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000339)
340
341# The internal config conatins additional path but includes the dawn_public_config include paths
342add_library(dawn_internal_config INTERFACE)
343target_include_directories(dawn_internal_config INTERFACE
344 "${DAWN_SRC_DIR}"
345 "${DAWN_BUILD_GEN_DIR}/src"
346)
347target_link_libraries(dawn_internal_config INTERFACE dawn_public_config)
348
349# Compile definitions for the internal config
350if (DAWN_ALWAYS_ASSERT OR $<CONFIG:Debug>)
351 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_ASSERTS")
352endif()
353if (DAWN_ENABLE_D3D12)
354 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_D3D12")
355endif()
356if (DAWN_ENABLE_METAL)
357 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_METAL")
358endif()
359if (DAWN_ENABLE_NULL)
360 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_NULL")
361endif()
Sergey Karchevsky3a75b1c2021-06-30 15:06:43 +0000362if (DAWN_ENABLE_DESKTOP_GL)
363 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_DESKTOP_GL")
364endif()
365if (DAWN_ENABLE_OPENGLES)
366 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_OPENGLES")
367endif()
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000368if (DAWN_ENABLE_OPENGL)
369 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_OPENGL")
370endif()
371if (DAWN_ENABLE_VULKAN)
372 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_VULKAN")
373endif()
Corentin Wallez2e22d922022-06-01 09:30:50 +0000374if (DAWN_USE_WAYLAND)
375 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_USE_WAYLAND")
376endif()
Corentin Wallezd353ca02020-02-18 02:12:35 +0000377if (DAWN_USE_X11)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000378 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_USE_X11")
379endif()
Corentin Wallez33466972020-02-24 13:27:28 +0000380if (WIN32)
381 target_compile_definitions(dawn_internal_config INTERFACE "NOMINMAX" "WIN32_LEAN_AND_MEAN")
382endif()
383
Corentin Wallez7c8bc942022-01-05 09:26:46 +0000384set(CMAKE_CXX_STANDARD "17")
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000385
386################################################################################
Ryan Harrisone87ac762022-04-06 15:37:27 -0400387# Tint
388################################################################################
389
Alastair Donaldson6a1eb452021-09-02 23:49:25 +0000390set(TINT_LIB_FUZZING_ENGINE_LINK_OPTIONS "" CACHE STRING "Used by OSS-Fuzz to control, via link options, which fuzzing engine should be used")
391
Ben Clayton54cfa0b2021-07-08 19:35:53 +0000392set(TINT_ROOT_SOURCE_DIR ${PROJECT_SOURCE_DIR})
393
Antonio Maioranod6009722021-03-17 13:32:54 +0000394# CMake < 3.15 sets /W3 in CMAKE_CXX_FLAGS. Remove it if it's there.
395# See https://gitlab.kitware.com/cmake/cmake/-/issues/18317
396if (MSVC)
Vasyl Teliman0b3611b2021-06-24 18:10:46 +0000397 if (CMAKE_CXX_FLAGS MATCHES "/W3")
398 string(REPLACE "/W3" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
399 endif()
Antonio Maioranod6009722021-03-17 13:32:54 +0000400endif()
401
Ryan Harrison563d3e92020-04-28 19:27:38 +0000402if (${TINT_CHECK_CHROMIUM_STYLE})
403 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -add-plugin -Xclang find-bad-constructs")
404endif()
405
dan sinclair4b71b9e2020-03-18 14:08:48 +0000406if (${TINT_BUILD_SPV_READER})
Ryan Harrisone87ac762022-04-06 15:37:27 -0400407 include_directories("${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-tools/include")
Dan Sinclair6e581892020-03-02 15:47:43 -0500408endif()
409
Antonio Maiorano88d3d2e2021-03-23 20:51:09 +0000410if((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND (CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))
411 set(COMPILER_IS_CLANG_CL TRUE)
412endif()
413
414if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR
415 (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") OR
416 ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND
417 (NOT COMPILER_IS_CLANG_CL)))
Dan Sinclair6e581892020-03-02 15:47:43 -0500418 set(COMPILER_IS_LIKE_GNU TRUE)
419endif()
420
Antonio Maiorano88d3d2e2021-03-23 20:51:09 +0000421# Enable msbuild multiprocessor builds
422if (MSVC AND NOT COMPILER_IS_CLANG_CL)
423 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
424endif()
425
Ben Clayton7687ec12021-04-16 14:43:34 +0000426set(TINT_OS_CC_SUFFIX "other")
427if (NOT TINT_BUILD_AS_OTHER_OS)
428 if(UNIX OR APPLE)
429 set(TINT_OS_CC_SUFFIX "posix")
430 set(TINT_OS_CC_SUFFIX "posix")
431 elseif(WIN32)
432 set(TINT_OS_CC_SUFFIX "windows")
433 set(TINT_OS_CC_SUFFIX "windows")
434 endif()
435endif()
436
David Neto5b46d712020-06-26 22:29:27 +0000437if(${TINT_BUILD_DOCS})
438 find_package(Doxygen)
439 if(DOXYGEN_FOUND)
Antonio Maiorano6241f962021-06-25 14:00:36 +0000440 set(DOXYGEN_WARN_AS_ERROR NO)
441 if(TINT_DOCS_WARN_AS_ERROR)
442 set(DOXYGEN_WARN_AS_ERROR YES)
443 endif()
Antonio Maioranoe25a8fc2021-06-25 14:53:06 +0000444
445 set(DOXYGEN_WARN_FORMAT "$file:$line: $text")
446 if (MSVC)
447 set(DOXYGEN_WARN_FORMAT "$file($line): $text")
448 endif()
449
David Neto5b46d712020-06-26 22:29:27 +0000450 add_custom_target(tint-docs ALL
Ben Clayton4ace8222021-05-12 08:15:41 +0000451 COMMAND ${CMAKE_COMMAND}
Antonio Maiorano6241f962021-06-25 14:00:36 +0000452 -E env
453 "DOXYGEN_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR}/docs"
454 "DOXYGEN_WARN_AS_ERROR=${DOXYGEN_WARN_AS_ERROR}"
Antonio Maioranoe25a8fc2021-06-25 14:53:06 +0000455 "DOXYGEN_WARN_FORMAT=${DOXYGEN_WARN_FORMAT}"
Ben Clayton4ace8222021-05-12 08:15:41 +0000456 ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
David Neto5b46d712020-06-26 22:29:27 +0000457 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
458 COMMENT "Generating API documentation"
459 VERBATIM)
460 else()
461 message("Doxygen not found. Skipping documentation")
462 endif(DOXYGEN_FOUND)
dan sinclairb0391c6f2020-07-15 20:54:48 +0000463endif()
Dan Sinclair6e581892020-03-02 15:47:43 -0500464
Ben Claytonbe2362b2022-01-18 18:58:16 +0000465function(tint_core_compile_options TARGET)
Ben Clayton54cfa0b2021-07-08 19:35:53 +0000466 target_include_directories(${TARGET} PUBLIC "${TINT_ROOT_SOURCE_DIR}")
467 target_include_directories(${TARGET} PUBLIC "${TINT_ROOT_SOURCE_DIR}/include")
Dan Sinclair6e581892020-03-02 15:47:43 -0500468
dan sinclairf6fdcb12020-10-23 17:04:10 +0000469 if (${TINT_BUILD_SPV_READER} OR ${TINT_BUILD_SPV_WRITER})
470 target_include_directories(${TARGET} PUBLIC
Ryan Harrisone87ac762022-04-06 15:37:27 -0400471 "${DAWN_THIRD_PARTY_DIR}/spirv-headers/include")
dan sinclairf6fdcb12020-10-23 17:04:10 +0000472 endif()
473
Ben Claytonbe2362b2022-01-18 18:58:16 +0000474 target_compile_definitions(${TARGET} PUBLIC -DTINT_BUILD_SPV_READER=$<BOOL:${TINT_BUILD_SPV_READER}>)
475 target_compile_definitions(${TARGET} PUBLIC -DTINT_BUILD_WGSL_READER=$<BOOL:${TINT_BUILD_WGSL_READER}>)
476 target_compile_definitions(${TARGET} PUBLIC -DTINT_BUILD_GLSL_WRITER=$<BOOL:${TINT_BUILD_GLSL_WRITER}>)
477 target_compile_definitions(${TARGET} PUBLIC -DTINT_BUILD_HLSL_WRITER=$<BOOL:${TINT_BUILD_HLSL_WRITER}>)
478 target_compile_definitions(${TARGET} PUBLIC -DTINT_BUILD_MSL_WRITER=$<BOOL:${TINT_BUILD_MSL_WRITER}>)
479 target_compile_definitions(${TARGET} PUBLIC -DTINT_BUILD_SPV_WRITER=$<BOOL:${TINT_BUILD_SPV_WRITER}>)
480 target_compile_definitions(${TARGET} PUBLIC -DTINT_BUILD_WGSL_WRITER=$<BOOL:${TINT_BUILD_WGSL_WRITER}>)
481
Ben Clayton96e245e2022-04-08 18:08:36 +0000482 common_compile_options(${TARGET})
Ben Claytonbe2362b2022-01-18 18:58:16 +0000483
484 if (TINT_EMIT_COVERAGE)
485 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
486 target_compile_options(${TARGET} PRIVATE "--coverage")
487 target_link_options(${TARGET} PRIVATE "gcov")
488 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
489 target_compile_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
490 target_link_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
491 else()
492 message(FATAL_ERROR "Coverage generation not supported for the ${CMAKE_CXX_COMPILER_ID} toolchain")
493 endif()
494 endif(TINT_EMIT_COVERAGE)
495endfunction()
496
497function(tint_default_compile_options TARGET)
498 tint_core_compile_options(${TARGET})
dan sinclair37dbf992020-03-11 18:43:12 +0000499
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000500 set(COMMON_GNU_OPTIONS
501 -Wall
502 -Werror
503 -Wextra
504 -Wno-documentation-unknown-command
505 -Wno-padded
506 -Wno-switch-enum
507 -Wno-unknown-pragmas
508 )
509
510 set(COMMON_CLANG_OPTIONS
511 -Wno-c++98-compat
512 -Wno-c++98-compat-pedantic
513 -Wno-format-pedantic
514 -Wno-return-std-move-in-c++11
515 -Wno-unknown-warning-option
516 -Wno-undefined-var-template
517 -Wno-used-but-marked-unused
518 -Weverything
519 )
520
Ben Claytonbe2362b2022-01-18 18:58:16 +0000521 if (COMPILER_IS_LIKE_GNU)
Dan Sinclair6e581892020-03-02 15:47:43 -0500522 target_compile_options(${TARGET} PRIVATE
Dan Sinclair6e581892020-03-02 15:47:43 -0500523 -pedantic-errors
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000524 ${COMMON_GNU_OPTIONS}
Dan Sinclair6e581892020-03-02 15:47:43 -0500525 )
526
527 if (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") OR
528 ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang"))
529 target_compile_options(${TARGET} PRIVATE
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000530 ${COMMON_CLANG_OPTIONS}
Dan Sinclair6e581892020-03-02 15:47:43 -0500531 )
532 endif()
Ben Claytonbe2362b2022-01-18 18:58:16 +0000533 endif(COMPILER_IS_LIKE_GNU)
Ben Claytonadb10d62020-10-27 21:04:59 +0000534
Dan Sinclair6e581892020-03-02 15:47:43 -0500535 if (MSVC)
536 # Specify /EHs for exception handling.
537 target_compile_options(${TARGET} PRIVATE
538 /bigobj
539 /EHsc
Antonio Maioranoac39fb42021-03-16 15:05:33 +0000540 /W4
Dan Sinclair6e581892020-03-02 15:47:43 -0500541 /WX
542 /wd4068
Antonio Maioranod6009722021-03-17 13:32:54 +0000543 /wd4127
dan sinclair6ca26992020-05-04 18:58:24 +0000544 /wd4244
545 /wd4267
Antonio Maioranoe5476052021-03-23 14:03:58 +0000546 /wd4324
Antonio Maioranod6009722021-03-17 13:32:54 +0000547 /wd4458
Dan Sinclair6e581892020-03-02 15:47:43 -0500548 /wd4514
549 /wd4571
550 /wd4625
551 /wd4626
552 /wd4710
553 /wd4774
554 /wd4820
555 /wd5026
556 /wd5027
557 )
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000558
559 # When building with clang-cl on Windows, try to match our clang build
560 # options as much as possible.
561 if (COMPILER_IS_CLANG_CL)
562 target_compile_options(${TARGET} PRIVATE
563 ${COMMON_GNU_OPTIONS}
564 ${COMMON_CLANG_OPTIONS}
565 # Disable warnings that are usually disabled in downstream deps for
566 # gcc/clang, but aren't for clang-cl.
567 -Wno-global-constructors
568 -Wno-zero-as-null-pointer-constant
569 -Wno-shorten-64-to-32
Antonio Maioranoc5f2fe42021-12-20 21:39:35 +0000570 -Wno-shadow-field-in-constructor
571 -Wno-reserved-id-macro
Antonio Maioranob79f51e2022-02-04 23:24:43 +0000572 -Wno-language-extension-token
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000573 )
574 endif()
Dan Sinclair6e581892020-03-02 15:47:43 -0500575 endif()
576endfunction()
577
Ryan Harrisone87ac762022-04-06 15:37:27 -0400578################################################################################
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000579# Run on all subdirectories
580################################################################################
581
582add_subdirectory(third_party)
James Price8d9adb02022-05-22 00:41:53 +0000583
584# TODO(crbug.com/tint/455): Tint does not currently build with CMake when
585# BUILD_SHARED_LIBS=1, so always build it as static for now.
586set(BUILD_SHARED_LIBS_SAVED ${BUILD_SHARED_LIBS})
587set(BUILD_SHARED_LIBS 0)
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000588add_subdirectory(src/tint)
James Price8d9adb02022-05-22 00:41:53 +0000589set(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_SAVED})
590
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000591add_subdirectory(generator)
592add_subdirectory(src/dawn)
Ben Clayton7b778552022-02-04 18:59:15 +0000593
594################################################################################
595# Samples
596################################################################################
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000597
Antonio Maiorano0eaee0c2021-08-31 18:43:36 +0000598if (TINT_BUILD_SAMPLES)
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000599 add_subdirectory(src/tint/cmd)
Antonio Maiorano0eaee0c2021-08-31 18:43:36 +0000600endif()
Dan Sinclair6e581892020-03-02 15:47:43 -0500601
Antonio Maiorano0eaee0c2021-08-31 18:43:36 +0000602if (TINT_BUILD_FUZZERS)
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000603 add_subdirectory(src/tint/fuzzers)
Dan Sinclair6e581892020-03-02 15:47:43 -0500604endif()
605
dan sinclairb5950522020-03-19 13:03:35 +0000606add_custom_target(tint-lint
607 COMMAND ./tools/lint
Ben Clayton54cfa0b2021-07-08 19:35:53 +0000608 WORKING_DIRECTORY ${TINT_ROOT_SOURCE_DIR}
dan sinclairb5950522020-03-19 13:03:35 +0000609 COMMENT "Running linter"
610 VERBATIM)
611
612add_custom_target(tint-format
613 COMMAND ./tools/format
Ben Clayton54cfa0b2021-07-08 19:35:53 +0000614 WORKING_DIRECTORY ${TINT_ROOT_SOURCE_DIR}
dan sinclairb5950522020-03-19 13:03:35 +0000615 COMMENT "Running formatter"
616 VERBATIM)
Ben Claytonadb10d62020-10-27 21:04:59 +0000617
618
Antonio Maiorano0eaee0c2021-08-31 18:43:36 +0000619if (TINT_EMIT_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Ben Claytonadb10d62020-10-27 21:04:59 +0000620 # Generates a lcov.info file at the project root.
621 # This can be used by tools such as VSCode's Coverage Gutters extension to
622 # visualize code coverage in the editor.
623 get_filename_component(CLANG_BIN_DIR ${CMAKE_C_COMPILER} DIRECTORY)
624 set(PATH_WITH_CLANG "${CLANG_BIN_DIR}:$ENV{PATH}")
625 add_custom_target(tint-generate-coverage
626 COMMAND ${CMAKE_COMMAND} -E env PATH=${PATH_WITH_CLANG} ./tools/tint-generate-coverage $<TARGET_FILE:tint_unittests>
627 DEPENDS tint_unittests
Ben Clayton54cfa0b2021-07-08 19:35:53 +0000628 WORKING_DIRECTORY ${TINT_ROOT_SOURCE_DIR}
Ben Claytonadb10d62020-10-27 21:04:59 +0000629 COMMENT "Generating tint coverage data"
630 VERBATIM)
631endif()
Ben Clayton54cfa0b2021-07-08 19:35:53 +0000632
Antonio Maiorano0eaee0c2021-08-31 18:43:36 +0000633if (TINT_BUILD_REMOTE_COMPILE)
634 add_subdirectory(tools/src/cmd/remote-compile)
Ben Clayton54cfa0b2021-07-08 19:35:53 +0000635endif()