1 # Copyright 2022 The Dawn & Tint Authors
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are met:
6 # 1. Redistributions of source code must retain the above copyright notice, this
7 # list of conditions and the following disclaimer.
9 # 2. Redistributions in binary form must reproduce the above copyright notice,
10 # this list of conditions and the following disclaimer in the documentation
11 # and/or other materials provided with the distribution.
13 # 3. Neither the name of the copyright holder nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 cmake_minimum_required(VERSION 3.10.2)
30 # When upgrading to CMake 3.11 we can remove DAWN_PLACEHOLDER_FILE because source-less add_library
32 # When upgrading to CMake 3.12 we should add CONFIGURE_DEPENDS to DawnGenerator to rerun CMake in
33 # case any of the generator files changes. We should also remove the CACHE "" FORCE stuff to
34 # override options in third_party dependencies. We can also add the HOMEPAGE_URL
35 # entry to the project `HOMEPAGE_URL "https://dawn.googlesource.com/dawn"`
39 DESCRIPTION "Dawn, a WebGPU implementation"
44 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
46 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
47 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
48 set(CMAKE_CXX_STANDARD 17)
49 set(CMAKE_DEBUG_POSTFIX "")
51 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
52 message(STATUS "No build type selected, default to Debug")
53 set(CMAKE_BUILD_TYPE "Debug")
56 set(DAWN_BUILD_GEN_DIR "${Dawn_BINARY_DIR}/gen")
57 set(DAWN_GENERATOR_DIR "${Dawn_SOURCE_DIR}/generator")
58 set(DAWN_SRC_DIR "${Dawn_SOURCE_DIR}/src")
59 set(DAWN_INCLUDE_DIR "${Dawn_SOURCE_DIR}/include")
60 set(DAWN_TEMPLATE_DIR "${DAWN_GENERATOR_DIR}/templates")
62 set(DAWN_PLACEHOLDER_FILE "${DAWN_SRC_DIR}/Placeholder.cpp")
64 ################################################################################
65 # Configuration options
66 ################################################################################
68 # option_if_not_defined(name description default)
70 # option(name description default)
71 # If a variable is not already defined with the given name, otherwise the
72 # function does nothing.
73 # Simplifies customization by projects that use Dawn as a dependency.
74 function (option_if_not_defined name description default)
75 if(NOT DEFINED ${name})
76 option(${name} ${description} ${default})
80 # set_if_not_defined(name value description)
82 # set(${name} ${value} CACHE STRING ${description})
83 # If a variable is not already defined with the given name, otherwise the
84 # function does nothing.
85 # Simplifies customization by projects that use Dawn as a dependency.
86 function (set_if_not_defined name value description)
87 if(NOT DEFINED ${name})
88 set(${name} ${value} CACHE STRING ${description})
92 function (install_if_enabled target)
93 if(NOT DAWN_ENABLE_INSTALL)
97 install(TARGETS ${target}
98 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
99 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
100 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
103 get_target_property(targetHeaders ${target} INTERFACE_SOURCES)
104 if (NOT targetHeaders)
108 foreach(headerFile ${targetHeaders})
109 # Starting from CMake 3.20 there is the cmake_path command that could simplify this code.
110 # Compute the install subdirectory for the header by stripping out the path to
111 # the include / gen/include directory...
112 string(FIND "${headerFile}" "${DAWN_INCLUDE_DIR}" foundIndex)
113 if (foundIndex EQUAL 0)
114 string(LENGTH "${DAWN_INCLUDE_DIR}/" lengthToRemove)
116 string(FIND "${headerFile}" "${DAWN_BUILD_GEN_DIR}/include/" foundIndex)
117 if (foundIndex EQUAL 0)
118 string(LENGTH "${DAWN_BUILD_GEN_DIR}/include/" lengthToRemove)
120 string(SUBSTRING "${headerFile}" "${lengthToRemove}" -1 headerRelDir)
122 # ... then remove everything after the last /
123 string(FIND "${headerRelDir}" "/" foundIndex REVERSE)
124 string(SUBSTRING "${headerRelDir}" 0 ${foundIndex} headerRelDir)
126 install(FILES "${headerFile}" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${headerRelDir})
131 # Default values for the backend-enabling options
132 set(ENABLE_D3D11 OFF)
133 set(ENABLE_D3D12 OFF)
134 set(ENABLE_METAL OFF)
135 set(ENABLE_OPENGLES OFF)
136 set(ENABLE_DESKTOP_GL OFF)
137 set(ENABLE_VULKAN OFF)
140 set(USE_WINDOWS_UI OFF)
141 set(BUILD_SAMPLES OFF)
142 set(TARGET_MACOS OFF)
145 set(USE_WINDOWS_UI ON)
147 if (NOT WINDOWS_STORE)
148 # Enable Vulkan in win32 compilation only
149 # since UWP only supports d3d
150 set(ENABLE_VULKAN ON)
154 if(CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "MacOS")
158 set(ENABLE_VULKAN ON)
159 set(ENABLE_OPENGLES ON)
161 set(ENABLE_OPENGLES ON)
162 set(ENABLE_DESKTOP_GL ON)
163 set(ENABLE_VULKAN ON)
167 # GLFW is not supported in UWP
168 set(DAWN_SUPPORTS_GLFW_FOR_WINDOWING OFF)
170 if ((WIN32 AND NOT WINDOWS_STORE) OR (UNIX AND NOT ANDROID))
171 set(DAWN_SUPPORTS_GLFW_FOR_WINDOWING ON)
174 # Current examples are depend on GLFW
175 if (DAWN_SUPPORTS_GLFW_FOR_WINDOWING)
176 set(BUILD_SAMPLES ON)
179 option_if_not_defined(DAWN_ENABLE_ASAN "Enable address sanitizer" OFF)
180 option_if_not_defined(DAWN_ENABLE_INSTALL "Enable install step for Dawn libraries" OFF)
181 option_if_not_defined(DAWN_ENABLE_TSAN "Enable thread sanitizer" OFF)
182 option_if_not_defined(DAWN_ENABLE_MSAN "Enable memory sanitizer" OFF)
183 option_if_not_defined(DAWN_ENABLE_UBSAN "Enable undefined behaviour sanitizer" OFF)
185 option_if_not_defined(DAWN_ENABLE_D3D11 "Enable compilation of the D3D11 backend" ${ENABLE_D3D11})
186 option_if_not_defined(DAWN_ENABLE_D3D12 "Enable compilation of the D3D12 backend" ${ENABLE_D3D12})
187 option_if_not_defined(DAWN_ENABLE_METAL "Enable compilation of the Metal backend" ${ENABLE_METAL})
188 option_if_not_defined(DAWN_ENABLE_NULL "Enable compilation of the Null backend" ON)
189 option_if_not_defined(DAWN_ENABLE_DESKTOP_GL "Enable compilation of the OpenGL backend" ${ENABLE_DESKTOP_GL})
190 option_if_not_defined(DAWN_ENABLE_OPENGLES "Enable compilation of the OpenGL ES backend" ${ENABLE_OPENGLES})
191 option_if_not_defined(DAWN_ENABLE_VULKAN "Enable compilation of the Vulkan backend" ${ENABLE_VULKAN})
193 option_if_not_defined(DAWN_ALWAYS_ASSERT "Enable assertions on all build types" OFF)
194 option_if_not_defined(DAWN_USE_WAYLAND "Enable support for Wayland surface" ${USE_WAYLAND})
195 option_if_not_defined(DAWN_USE_X11 "Enable support for X11 surface" ${USE_X11})
196 option_if_not_defined(DAWN_USE_GLFW "Enable compilation of the GLFW windowing utils" ${DAWN_SUPPORTS_GLFW_FOR_WINDOWING})
197 option_if_not_defined(DAWN_USE_WINDOWS_UI "Enable support for Windows UI surface" ${USE_WINDOWS_UI})
198 option_if_not_defined(DAWN_TARGET_MACOS "Manually link Apple core frameworks" ${TARGET_MACOS})
200 option_if_not_defined(DAWN_BUILD_SAMPLES "Enables building Dawn's samples" ${BUILD_SAMPLES})
201 option_if_not_defined(DAWN_BUILD_NODE_BINDINGS "Enables building Dawn's NodeJS bindings" OFF)
202 option_if_not_defined(DAWN_ENABLE_SWIFTSHADER "Enables building Swiftshader as part of the build and Vulkan adapter discovery" OFF)
203 option_if_not_defined(DAWN_BUILD_BENCHMARKS "Build Dawn benchmarks" OFF)
205 option_if_not_defined(DAWN_ENABLE_PIC "Build with Position-Independent-Code enabled" OFF)
207 option_if_not_defined(DAWN_EMIT_COVERAGE "Emit code coverage information" OFF)
208 set_if_not_defined(LLVM_SOURCE_DIR "${Dawn_LLVM_SOURCE_DIR}" "Directory to an LLVM source checkout. Required to build turbo-cov")
210 if (DAWN_ENABLE_OPENGLES OR DAWN_ENABLE_DESKTOP_GL)
211 set(TINT_DEFAULT_GLSL ON)
213 set(TINT_DEFAULT_GLSL OFF)
216 option_if_not_defined(TINT_ENABLE_INSTALL "Enable install step for Tint libraries" OFF)
217 option_if_not_defined(TINT_BUILD_CMD_TOOLS "Build the Tint command line tools" ON)
218 option_if_not_defined(TINT_BUILD_DOCS "Build documentation" ON)
219 option_if_not_defined(TINT_DOCS_WARN_AS_ERROR "When building documentation, treat warnings as errors" OFF)
221 if (NOT DAWN_USE_GLFW AND (TINT_BUILD_CMD_TOOLS OR DAWN_BUILD_SAMPLES))
222 message(SEND_ERROR "Dawn samples require GLFW")
225 option_if_not_defined(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" ${DAWN_ENABLE_VULKAN})
226 option_if_not_defined(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
227 option_if_not_defined(TINT_BUILD_GLSL_WRITER "Build the GLSL output writer" ${TINT_DEFAULT_GLSL})
228 option_if_not_defined(TINT_BUILD_GLSL_VALIDATOR "Build the GLSL output validator" ON)
229 option_if_not_defined(TINT_BUILD_HLSL_WRITER "Build the HLSL output writer" ${DAWN_ENABLE_D3D12})
230 option_if_not_defined(TINT_BUILD_MSL_WRITER "Build the MSL output writer" ${DAWN_ENABLE_METAL})
231 option_if_not_defined(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ${DAWN_ENABLE_VULKAN})
232 option_if_not_defined(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
234 option_if_not_defined(TINT_BUILD_SYNTAX_TREE_WRITER "Build the syntax tree writer" OFF)
236 option_if_not_defined(TINT_BUILD_FUZZERS "Build fuzzers" OFF)
237 option_if_not_defined(TINT_BUILD_AST_FUZZER "Build AST fuzzer" OFF)
238 option_if_not_defined(TINT_BUILD_REGEX_FUZZER "Build regex fuzzer" OFF)
239 option_if_not_defined(TINT_BUILD_BENCHMARKS "Build Tint benchmarks" OFF)
240 option_if_not_defined(TINT_BUILD_TESTS "Build tests" ON)
241 option_if_not_defined(TINT_BUILD_AS_OTHER_OS "Override OS detection to force building of *_other.cc files" OFF)
243 set_if_not_defined(TINT_EXTERNAL_BENCHMARK_CORPUS_DIR "" "Directory that holds a corpus of external shaders to benchmark.")
245 option_if_not_defined(TINT_ENABLE_BREAK_IN_DEBUGGER "Enable tint::debugger::Break()" OFF)
246 option_if_not_defined(TINT_CHECK_CHROMIUM_STYLE "Check for [chromium-style] issues during build" OFF)
247 option_if_not_defined(TINT_RANDOMIZE_HASHES "Randomize the hash seed value to detect non-deterministic output" OFF)
249 # Recommended setting for compability with future abseil releases.
250 set(ABSL_PROPAGATE_CXX_STD ON)
252 set_if_not_defined(DAWN_THIRD_PARTY_DIR "${Dawn_SOURCE_DIR}/third_party" "Directory in which to find third-party dependencies.")
253 set_if_not_defined(DAWN_VULKAN_DEPS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps" "Directory in which to find vulkan-deps")
255 set_if_not_defined(DAWN_ABSEIL_DIR "${DAWN_THIRD_PARTY_DIR}/abseil-cpp" "Directory in which to find Abseil")
256 set_if_not_defined(DAWN_GLFW_DIR "${DAWN_THIRD_PARTY_DIR}/glfw" "Directory in which to find GLFW")
257 set_if_not_defined(DAWN_JINJA2_DIR "${DAWN_THIRD_PARTY_DIR}/jinja2" "Directory in which to find Jinja2")
258 set_if_not_defined(DAWN_MARKUPSAFE_DIR "${DAWN_THIRD_PARTY_DIR}/markupsafe" "Directory in which to find MarkupSafe")
259 set_if_not_defined(DAWN_KHRONOS_DIR "${DAWN_THIRD_PARTY_DIR}/khronos" "Directory in which to find Khronos GL headers")
260 set_if_not_defined(DAWN_SWIFTSHADER_DIR "${DAWN_THIRD_PARTY_DIR}/swiftshader" "Directory in which to find swiftshader")
262 set_if_not_defined(DAWN_SPIRV_TOOLS_DIR "${DAWN_VULKAN_DEPS_DIR}/spirv-tools/src" "Directory in which to find SPIRV-Tools")
263 set_if_not_defined(DAWN_SPIRV_HEADERS_DIR "${DAWN_VULKAN_DEPS_DIR}/spirv-headers/src" "Directory in which to find SPIRV-Headers")
264 set_if_not_defined(DAWN_VULKAN_HEADERS_DIR "${DAWN_VULKAN_DEPS_DIR}/vulkan-headers/src" "Directory in which to find Vulkan-Headers")
265 set_if_not_defined(DAWN_VULKAN_UTILITY_LIBRARIES_DIR "${DAWN_VULKAN_DEPS_DIR}/vulkan-utility-libraries/src" "Directory in which to find Vulkan-Utility-Libraries")
267 # Dependencies for DAWN_BUILD_NODE_BINDINGS
268 set_if_not_defined(NODE_ADDON_API_DIR "${DAWN_THIRD_PARTY_DIR}/node-addon-api" "Directory in which to find node-addon-api")
269 set_if_not_defined(NODE_API_HEADERS_DIR "${DAWN_THIRD_PARTY_DIR}/node-api-headers" "Directory in which to find node-api-headers")
270 set_if_not_defined(WEBGPU_IDL_PATH "${DAWN_THIRD_PARTY_DIR}/gpuweb/webgpu.idl" "Path to the webgpu.idl definition file")
271 set_if_not_defined(GO_EXECUTABLE "go" "Golang executable for running the IDL generator")
273 option_if_not_defined(DAWN_FETCH_DEPENDENCIES "Use fetch_dawn_dependencies.py as an alternative to using depot_tools" OFF)
275 # Much of the backend code is shared among desktop OpenGL and OpenGL ES
276 if (${DAWN_ENABLE_DESKTOP_GL} OR ${DAWN_ENABLE_OPENGLES})
277 set(DAWN_ENABLE_OPENGL ON)
281 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
284 if (${TINT_BUILD_AST_FUZZER})
285 message(STATUS "TINT_BUILD_AST_FUZZER is ON - setting
287 TINT_BUILD_WGSL_READER
288 TINT_BUILD_WGSL_WRITER
289 TINT_BUILD_SPV_WRITER
290 TINT_BUILD_MSL_WRITER
291 TINT_BUILD_GLSL_WRITER
292 TINT_BUILD_HLSL_WRITER to ON")
293 set(TINT_BUILD_FUZZERS ON CACHE BOOL "Build tint fuzzers" FORCE)
294 set(TINT_BUILD_WGSL_READER ON CACHE BOOL "Build WGSL reader" FORCE)
295 set(TINT_BUILD_WGSL_WRITER ON CACHE BOOL "Build WGSL writer" FORCE)
296 set(TINT_BUILD_SPV_WRITER ON CACHE BOOL "Build SPIR-V writer" FORCE)
297 set(TINT_BUILD_MSL_WRITER ON CACHE BOOL "Build MSL writer" FORCE)
298 set(TINT_BUILD_GLSL_WRITER ON CACHE BOOL "Build GLSL writer" FORCE)
299 set(TINT_BUILD_HLSL_WRITER ON CACHE BOOL "Build HLSL writer" FORCE)
302 if (${TINT_BUILD_REGEX_FUZZER})
303 message(STATUS "TINT_BUILD_REGEX_FUZZER is ON - setting
305 TINT_BUILD_WGSL_READER
306 TINT_BUILD_WGSL_WRITER
307 TINT_BUILD_SPV_WRITER
308 TINT_BUILD_MSL_WRITER
309 TINT_BUILD_GLSL_WRITER
310 TINT_BUILD_HLSL_WRITER to ON")
311 set(TINT_BUILD_FUZZERS ON CACHE BOOL "Build tint fuzzers" FORCE)
312 set(TINT_BUILD_WGSL_READER ON CACHE BOOL "Build WGSL reader" FORCE)
313 set(TINT_BUILD_WGSL_WRITER ON CACHE BOOL "Build WGSL writer" FORCE)
314 set(TINT_BUILD_SPV_WRITER ON CACHE BOOL "Build SPIR-V writer" FORCE)
315 set(TINT_BUILD_MSL_WRITER ON CACHE BOOL "Build MSL writer" FORCE)
316 set(TINT_BUILD_GLSL_WRITER ON CACHE BOOL "Build GLSL writer" FORCE)
317 set(TINT_BUILD_HLSL_WRITER ON CACHE BOOL "Build HLSL writer" FORCE)
320 message(STATUS "Dawn build D3D11 backend: ${DAWN_ENABLE_D3D11}")
321 message(STATUS "Dawn build D3D12 backend: ${DAWN_ENABLE_D3D12}")
322 message(STATUS "Dawn build Metal backend: ${DAWN_ENABLE_METAL}")
323 message(STATUS "Dawn build Vulkan backend: ${DAWN_ENABLE_VULKAN}")
324 message(STATUS "Dawn build OpenGL backend: ${DAWN_ENABLE_DESKTOP_GL}")
325 message(STATUS "Dawn build OpenGL ES backend: ${DAWN_ENABLE_OPENGLES}")
326 message(STATUS "Dawn build Null backend: ${DAWN_ENABLE_NULL}")
328 message(STATUS "Dawn build with asserts in all configurations: ${DAWN_ALWAYS_ASSERT}")
329 message(STATUS "Dawn build Wayland support: ${DAWN_USE_WAYLAND}")
330 message(STATUS "Dawn build X11 support: ${DAWN_USE_X11}")
331 message(STATUS "Dawn build GLFW support: ${DAWN_USE_GLFW}")
332 message(STATUS "Dawn build Windows UI support: ${DAWN_USE_WINDOWS_UI}")
334 message(STATUS "Dawn build samples: ${DAWN_BUILD_SAMPLES}")
335 message(STATUS "Dawn build Node bindings: ${DAWN_BUILD_NODE_BINDINGS}")
336 message(STATUS "Dawn build Swiftshader: ${DAWN_ENABLE_SWIFTSHADER}")
337 message(STATUS "Dawn build benchmarks: ${DAWN_BUILD_BENCHMARKS}")
339 message(STATUS "Dawn build PIC: ${DAWN_ENABLE_PIC}")
341 message(STATUS "Dawn build with ASAN: ${DAWN_ENABLE_ASAN}")
342 message(STATUS "Dawn build with TSAN: ${DAWN_ENABLE_TSAN}")
343 message(STATUS "Dawn build with MSAN: ${DAWN_ENABLE_MSAN}")
344 message(STATUS "Dawn build with UBSAN: ${DAWN_ENABLE_UBSAN}")
346 message(STATUS "Tint build command line executable tools: ${TINT_BUILD_CMD_TOOLS}")
347 message(STATUS "Tint build docs: ${TINT_BUILD_DOCS}")
348 message(STATUS "Tint build docs with warn as error: ${TINT_DOCS_WARN_AS_ERROR}")
349 message(STATUS "Tint build SPIR-V reader: ${TINT_BUILD_SPV_READER}")
350 message(STATUS "Tint build WGSL reader: ${TINT_BUILD_WGSL_READER}")
351 message(STATUS "Tint build GLSL writer: ${TINT_BUILD_GLSL_WRITER}")
352 message(STATUS "Tint build GLSL validator: ${TINT_BUILD_GLSL_VALIDATOR}")
353 message(STATUS "Tint build HLSL writer: ${TINT_BUILD_HLSL_WRITER}")
354 message(STATUS "Tint build MSL writer: ${TINT_BUILD_MSL_WRITER}")
355 message(STATUS "Tint build SPIR-V writer: ${TINT_BUILD_SPV_WRITER}")
356 message(STATUS "Tint build WGSL writer: ${TINT_BUILD_WGSL_WRITER}")
357 message(STATUS "Tint build Syntax Tree writer: ${TINT_BUILD_SYNTAX_TREE_WRITER}")
358 message(STATUS "Tint build fuzzers: ${TINT_BUILD_FUZZERS}")
359 message(STATUS "Tint build AST fuzzer: ${TINT_BUILD_AST_FUZZER}")
360 message(STATUS "Tint build regex fuzzer: ${TINT_BUILD_REGEX_FUZZER}")
361 message(STATUS "Tint build benchmarks: ${TINT_BUILD_BENCHMARKS}")
362 message(STATUS "Tint build tests: ${TINT_BUILD_TESTS}")
363 message(STATUS "Tint build checking [chromium-style]: ${TINT_CHECK_CHROMIUM_STYLE}")
364 message(STATUS "Tint external benchmark corpus dir: ${TINT_EXTERNAL_BENCHMARK_CORPUS_DIR}")
367 if (NOT ${TINT_LIB_FUZZING_ENGINE_LINK_OPTIONS} STREQUAL "")
368 message(STATUS "Using provided LIB_FUZZING_ENGINE options: ${TINT_LIB_FUZZING_ENGINE_LINK_OPTIONS}")
371 message(STATUS "Using python3")
372 find_package(PythonInterp 3 REQUIRED)
374 ################################################################################
375 # common_compile_options - sets compiler and linker options common for dawn and
376 # tint on the given target
377 ################################################################################
378 function(common_compile_options TARGET)
379 if (COMPILER_IS_LIKE_GNU)
380 target_compile_options(${TARGET} PRIVATE
384 -Wno-deprecated-builtins
385 -Wno-unknown-warning-option
388 if (${DAWN_ENABLE_MSAN})
389 target_compile_options(${TARGET} PUBLIC -fsanitize=memory)
390 target_link_options(${TARGET} PUBLIC -fsanitize=memory)
391 elseif (${DAWN_ENABLE_ASAN})
392 target_compile_options(${TARGET} PUBLIC -fsanitize=address)
393 target_link_options(${TARGET} PUBLIC -fsanitize=address)
394 elseif (${DAWN_ENABLE_TSAN})
395 target_compile_options(${TARGET} PUBLIC -fsanitize=thread)
396 target_link_options(${TARGET} PUBLIC -fsanitize=thread)
397 elseif (${DAWN_ENABLE_UBSAN})
398 target_compile_options(${TARGET} PUBLIC -fsanitize=undefined -fsanitize=float-divide-by-zero)
399 target_link_options(${TARGET} PUBLIC -fsanitize=undefined -fsanitize=float-divide-by-zero)
401 endif(COMPILER_IS_LIKE_GNU)
404 target_compile_options(${TARGET} PUBLIC /utf-8)
407 if (DAWN_EMIT_COVERAGE)
408 target_compile_definitions(${TARGET} PRIVATE "DAWN_EMIT_COVERAGE")
409 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
410 target_compile_options(${TARGET} PRIVATE "--coverage")
411 target_link_options(${TARGET} PRIVATE "gcov")
412 elseif(COMPILER_IS_CLANG OR COMPILER_IS_CLANG_CL)
413 target_compile_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
414 target_link_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
416 message(FATAL_ERROR "Coverage generation not supported for the ${CMAKE_CXX_COMPILER_ID} toolchain")
418 endif(DAWN_EMIT_COVERAGE)
421 if (${DAWN_ENABLE_TSAN})
422 add_compile_options(-stdlib=libc++)
423 add_link_options(-stdlib=libc++)
426 ################################################################################
427 # Dawn's public and internal "configs"
428 ################################################################################
430 set(IS_DEBUG_BUILD 0)
431 string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
432 if ((NOT ${build_type} STREQUAL "RELEASE") AND (NOT ${build_type} STREQUAL "RELWITHDEBINFO"))
433 set(IS_DEBUG_BUILD 1)
436 # The public config contains only the include paths for the Dawn headers.
437 add_library(dawn_public_config INTERFACE)
438 target_include_directories(dawn_public_config INTERFACE
439 "${DAWN_INCLUDE_DIR}"
440 "${DAWN_BUILD_GEN_DIR}/include"
443 # The internal config conatins additional path but includes the dawn_public_config include paths
444 add_library(dawn_internal_config INTERFACE)
445 target_include_directories(dawn_internal_config INTERFACE
447 "${DAWN_BUILD_GEN_DIR}/src"
449 target_link_libraries(dawn_internal_config INTERFACE dawn_public_config)
451 # Compile definitions for the internal config
452 if (DAWN_ALWAYS_ASSERT OR IS_DEBUG_BUILD)
453 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_ASSERTS")
455 if (DAWN_ENABLE_D3D11)
456 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_D3D11")
458 if (DAWN_ENABLE_D3D12)
459 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_D3D12")
461 if (DAWN_ENABLE_METAL)
462 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_METAL")
464 if (DAWN_ENABLE_NULL)
465 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_NULL")
467 if (DAWN_ENABLE_DESKTOP_GL)
468 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_DESKTOP_GL")
470 if (DAWN_ENABLE_OPENGLES)
471 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_OPENGLES")
473 if (DAWN_ENABLE_OPENGL)
474 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_OPENGL")
476 if (DAWN_ENABLE_VULKAN)
477 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_VULKAN")
479 if (DAWN_USE_WAYLAND)
480 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_USE_WAYLAND")
483 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_USE_X11")
485 if (DAWN_USE_WINDOWS_UI)
486 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_USE_WINDOWS_UI")
489 target_compile_definitions(dawn_internal_config INTERFACE "NOMINMAX" "WIN32_LEAN_AND_MEAN")
492 set(CMAKE_CXX_STANDARD "17")
494 ################################################################################
496 ################################################################################
498 set(TINT_LIB_FUZZING_ENGINE_LINK_OPTIONS "" CACHE STRING "Used by OSS-Fuzz to control, via link options, which fuzzing engine should be used")
500 set(TINT_ROOT_SOURCE_DIR ${PROJECT_SOURCE_DIR})
501 set(TINT_SPIRV_HEADERS_DIR ${DAWN_SPIRV_HEADERS_DIR})
502 set(TINT_SPIRV_TOOLS_DIR ${DAWN_SPIRV_TOOLS_DIR})
504 # CMake < 3.15 sets /W3 in CMAKE_CXX_FLAGS. Remove it if it's there.
505 # See https://gitlab.kitware.com/cmake/cmake/-/issues/18317
507 if (CMAKE_CXX_FLAGS MATCHES "/W3")
508 string(REPLACE "/W3" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
512 if (${TINT_CHECK_CHROMIUM_STYLE})
513 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -add-plugin -Xclang find-bad-constructs")
516 if((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND (CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))
517 set(COMPILER_IS_CLANG_CL TRUE)
520 if((CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") OR
521 ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND
522 (NOT COMPILER_IS_CLANG_CL)))
523 set(COMPILER_IS_CLANG TRUE)
526 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
527 set(COMPILER_IS_GNU TRUE)
530 if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR COMPILER_IS_CLANG)
531 set(COMPILER_IS_LIKE_GNU TRUE)
534 # Enable msbuild multiprocessor builds
535 if (MSVC AND NOT COMPILER_IS_CLANG_CL)
536 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
539 if(${TINT_BUILD_DOCS})
540 find_package(Doxygen)
542 set(DOXYGEN_WARN_AS_ERROR NO)
543 if(TINT_DOCS_WARN_AS_ERROR)
544 set(DOXYGEN_WARN_AS_ERROR YES)
547 set(DOXYGEN_WARN_FORMAT "$file:$line: $text")
549 set(DOXYGEN_WARN_FORMAT "$file($line): $text")
552 add_custom_target(tint-docs ALL
553 COMMAND ${CMAKE_COMMAND}
555 "DOXYGEN_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR}/docs"
556 "DOXYGEN_WARN_AS_ERROR=${DOXYGEN_WARN_AS_ERROR}"
557 "DOXYGEN_WARN_FORMAT=${DOXYGEN_WARN_FORMAT}"
558 ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
559 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
560 COMMENT "Generating API documentation"
563 message("Doxygen not found. Skipping documentation")
567 ################################################################################
568 # Run on all subdirectories
569 ################################################################################
571 add_subdirectory(third_party)
573 # TODO(crbug.com/tint/455): Tint does not currently build with CMake when
574 # BUILD_SHARED_LIBS=1, so always build it as static for now.
575 set(BUILD_SHARED_LIBS_SAVED ${BUILD_SHARED_LIBS})
576 set(BUILD_SHARED_LIBS 0)
577 add_subdirectory(src/tint)
578 set(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_SAVED})
580 add_subdirectory(generator)
581 add_subdirectory(src/dawn)
583 ################################################################################
585 ################################################################################
586 add_custom_target(tint-lint
588 WORKING_DIRECTORY ${TINT_ROOT_SOURCE_DIR}
589 COMMENT "Running linter"
592 add_custom_target(tint-format
593 COMMAND ./tools/format
594 WORKING_DIRECTORY ${TINT_ROOT_SOURCE_DIR}
595 COMMENT "Running formatter"
598 if (DAWN_EMIT_COVERAGE)
599 add_subdirectory(tools/src/cmd/turbo-cov)
601 # The tint-generate-coverage target generates a lcov.info file at the project
602 # root, holding the code coverage for all the tint_unitests.
603 # This can be used by tools such as VSCode's Coverage Gutters extension to
604 # visualize code coverage in the editor.
605 get_filename_component(CLANG_BIN_DIR ${CMAKE_C_COMPILER} DIRECTORY)
606 set(PATH_WITH_CLANG "${CLANG_BIN_DIR}:$ENV{PATH}")
607 add_custom_target(tint-generate-coverage
608 COMMAND ${CMAKE_COMMAND} -E env PATH=${PATH_WITH_CLANG} ./tools/tint-generate-coverage $<TARGET_FILE:tint_unittests>
609 DEPENDS tint_unittests
610 WORKING_DIRECTORY ${TINT_ROOT_SOURCE_DIR}
611 COMMENT "Generating tint coverage data"