blob: 324591520f0b2ae135b32a8eaf6421f7c68caec8 [file] [log] [blame]
Corentin Wallez7fe6efb2020-02-05 17:16:05 +00001# Copyright 2020 The Dawn Authors
2#
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
15cmake_minimum_required(VERSION 3.10)
16
17# When upgrading to CMake 3.11 we can remove DAWN_DUMMY_FILE because source-less add_library
18# 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)
29
30set_property(GLOBAL PROPERTY USE_FOLDERS ON)
31
32if(NOT CMAKE_BUILD_TYPE)
33 message(WARNING "CMAKE_BUILD_TYPE not set, forcing it to Debug")
34 set(CMAKE_BUILD_TYPE "Debug" CACHE STRING
35 "Build type (Debug, Release, RelWithDebInfo, MinSizeRel)" FORCE)
36endif()
37
38set(DAWN_BUILD_GEN_DIR "${Dawn_BINARY_DIR}/gen")
39set(DAWN_GENERATOR_DIR "${Dawn_SOURCE_DIR}/generator")
40set(DAWN_SRC_DIR "${Dawn_SOURCE_DIR}/src")
41set(DAWN_INCLUDE_DIR "${DAWN_SRC_DIR}/include")
42set(DAWN_TEMPLATE_DIR "${DAWN_GENERATOR_DIR}/templates")
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000043
44set(DAWN_DUMMY_FILE "${DAWN_SRC_DIR}/Dummy.cpp")
45
46################################################################################
47# Configuration options
48################################################################################
49
Ben Clayton30eeac72021-09-24 10:38:18 +000050# option_if_not_defined(name description default)
51# Behaves like:
52# option(name description default)
53# If a variable is not already defined with the given name, otherwise the
54# function does nothing.
55# Simplifies customization by projects that use Dawn as a dependency.
56function (option_if_not_defined name description default)
57 if(NOT DEFINED ${name})
58 option(${name} ${description} ${default})
59 endif()
60endfunction()
61
62# set_if_not_defined(name value description)
63# Behaves like:
64# set(${name} ${value} CACHE STRING ${description})
65# If a variable is not already defined with the given name, otherwise the
66# function does nothing.
67# Simplifies customization by projects that use Dawn as a dependency.
68function (set_if_not_defined name value description)
69 if(NOT DEFINED ${name})
70 set(${name} ${value} CACHE STRING ${description})
71 endif()
72endfunction()
73
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000074# Default values for the backend-enabling options
75set(ENABLE_D3D12 OFF)
76set(ENABLE_METAL OFF)
Sergey Karchevsky3a75b1c2021-06-30 15:06:43 +000077set(ENABLE_OPENGLES OFF)
78set(ENABLE_DESKTOP_GL OFF)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000079set(ENABLE_VULKAN OFF)
Corentin Wallezd353ca02020-02-18 02:12:35 +000080set(USE_X11 OFF)
陈俊嘉b2bc57a2021-06-24 07:00:16 +000081set(BUILD_EXAMPLE OFF)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000082if (WIN32)
83 set(ENABLE_D3D12 ON)
陈俊嘉610de1d2021-06-28 08:19:28 +000084 if (NOT WINDOWS_STORE)
陈俊嘉b2bc57a2021-06-24 07:00:16 +000085 # Enable Vulkan in win32 compilation only
86 # since UWP only supports d3d
87 set(ENABLE_VULKAN ON)
88 endif()
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000089elseif(APPLE)
90 set(ENABLE_METAL ON)
91elseif(UNIX)
Sergey Karchevsky3a75b1c2021-06-30 15:06:43 +000092 set(ENABLE_OPENGLES ON)
93 set(ENABLE_DESKTOP_GL ON)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000094 set(ENABLE_VULKAN ON)
Corentin Wallezd353ca02020-02-18 02:12:35 +000095 set(USE_X11 ON)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000096endif()
97
陈俊嘉b2bc57a2021-06-24 07:00:16 +000098# GLFW is not supported in UWP
99if((WIN32 AND NOT WINDOWS_STORE) OR UNIX)
100 set(DAWN_SUPPORTS_GLFW_FOR_WINDOWING ON)
101endif()
102
103# Current examples are depend on GLFW
104if (DAWN_SUPPORTS_GLFW_FOR_WINDOWING)
105 set(BUILD_EXAMPLE ON)
106endif()
107
Ben Clayton30eeac72021-09-24 10:38:18 +0000108option_if_not_defined(DAWN_ENABLE_D3D12 "Enable compilation of the D3D12 backend" ${ENABLE_D3D12})
109option_if_not_defined(DAWN_ENABLE_METAL "Enable compilation of the Metal backend" ${ENABLE_METAL})
110option_if_not_defined(DAWN_ENABLE_NULL "Enable compilation of the Null backend" ON)
111option_if_not_defined(DAWN_ENABLE_DESKTOP_GL "Enable compilation of the OpenGL backend" ${ENABLE_DESKTOP_GL})
112option_if_not_defined(DAWN_ENABLE_OPENGLES "Enable compilation of the OpenGL ES backend" ${ENABLE_OPENGLES})
113option_if_not_defined(DAWN_ENABLE_VULKAN "Enable compilation of the Vulkan backend" ${ENABLE_VULKAN})
114option_if_not_defined(DAWN_ALWAYS_ASSERT "Enable assertions on all build types" OFF)
115option_if_not_defined(DAWN_USE_X11 "Enable support for X11 surface" ${USE_X11})
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000116
Ben Clayton30eeac72021-09-24 10:38:18 +0000117option_if_not_defined(DAWN_BUILD_EXAMPLES "Enables building Dawn's exmaples" ${BUILD_EXAMPLE})
Ben Claytonaffb7a32021-09-28 11:14:42 +0000118option_if_not_defined(DAWN_BUILD_NODE_BINDINGS "Enables building Dawn's NodeJS bindings" OFF)
119
120option_if_not_defined(DAWN_ENABLE_PIC "Build with Position-Independent-Code enabled" OFF)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000121
Ben Clayton30eeac72021-09-24 10:38:18 +0000122set_if_not_defined(DAWN_THIRD_PARTY_DIR "${Dawn_SOURCE_DIR}/third_party" "Directory in which to find third-party dependencies.")
Corentin Walleze5570872020-10-20 14:26:10 +0000123
Brandon Jonesa04663c2021-09-23 20:36:03 +0000124# Recommended setting for compability with future abseil releases.
125set(ABSL_PROPAGATE_CXX_STD ON)
Brandon Jonesa04663c2021-09-23 20:36:03 +0000126
Ben Clayton30eeac72021-09-24 10:38:18 +0000127set_if_not_defined(DAWN_ABSEIL_DIR "${DAWN_THIRD_PARTY_DIR}/abseil-cpp" "Directory in which to find Abseil")
128set_if_not_defined(DAWN_GLFW_DIR "${DAWN_THIRD_PARTY_DIR}/glfw" "Directory in which to find GLFW")
Ben Clayton30eeac72021-09-24 10:38:18 +0000129set_if_not_defined(DAWN_JINJA2_DIR "${DAWN_THIRD_PARTY_DIR}/jinja2" "Directory in which to find Jinja2")
130set_if_not_defined(DAWN_SPIRV_CROSS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-cross/src" "Directory in which to find SPIRV-Cross")
131set_if_not_defined(DAWN_SPIRV_HEADERS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-headers/src" "Directory in which to find SPIRV-Headers")
132set_if_not_defined(DAWN_SPIRV_TOOLS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-tools/src" "Directory in which to find SPIRV-Tools")
133set_if_not_defined(DAWN_TINT_DIR "${DAWN_THIRD_PARTY_DIR}/tint" "Directory in which to find Tint")
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000134
Ben Claytonaffb7a32021-09-28 11:14:42 +0000135# Dependencies for DAWN_BUILD_NODE_BINDINGS
136set_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 +0000137set_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 +0000138set_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 +0000139set_if_not_defined(GO_EXECUTABLE "go" "Golang executable for running the IDL generator")
Ben Claytonaffb7a32021-09-28 11:14:42 +0000140
Sergey Karchevsky3a75b1c2021-06-30 15:06:43 +0000141# Much of the backend code is shared among desktop OpenGL and OpenGL ES
142if (${DAWN_ENABLE_DESKTOP_GL} OR ${DAWN_ENABLE_OPENGLES})
143 set(DAWN_ENABLE_OPENGL ON)
144endif()
145
Ben Claytonc19329c2021-09-23 19:24:43 +0000146# OpenGL backend requires SPIRV-Cross
147set(DAWN_REQUIRES_SPIRV_CROSS OFF)
148if (DAWN_ENABLE_OPENGL)
149 set(DAWN_REQUIRES_SPIRV_CROSS ON)
150endif()
151
Ben Claytonaffb7a32021-09-28 11:14:42 +0000152if(DAWN_ENABLE_PIC)
153 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
154endif()
155
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000156################################################################################
157# Dawn's public and internal "configs"
158################################################################################
159
160# The public config contains only the include paths for the Dawn headers.
161add_library(dawn_public_config INTERFACE)
162target_include_directories(dawn_public_config INTERFACE
163 "${DAWN_SRC_DIR}/include"
164 "${DAWN_BUILD_GEN_DIR}/src/include"
165)
166
167# The internal config conatins additional path but includes the dawn_public_config include paths
168add_library(dawn_internal_config INTERFACE)
169target_include_directories(dawn_internal_config INTERFACE
170 "${DAWN_SRC_DIR}"
171 "${DAWN_BUILD_GEN_DIR}/src"
172)
173target_link_libraries(dawn_internal_config INTERFACE dawn_public_config)
174
175# Compile definitions for the internal config
176if (DAWN_ALWAYS_ASSERT OR $<CONFIG:Debug>)
177 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_ASSERTS")
178endif()
179if (DAWN_ENABLE_D3D12)
180 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_D3D12")
181endif()
182if (DAWN_ENABLE_METAL)
183 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_METAL")
184endif()
185if (DAWN_ENABLE_NULL)
186 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_NULL")
187endif()
Sergey Karchevsky3a75b1c2021-06-30 15:06:43 +0000188if (DAWN_ENABLE_DESKTOP_GL)
189 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_DESKTOP_GL")
190endif()
191if (DAWN_ENABLE_OPENGLES)
192 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_OPENGLES")
193endif()
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000194if (DAWN_ENABLE_OPENGL)
195 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_OPENGL")
196endif()
197if (DAWN_ENABLE_VULKAN)
198 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_VULKAN")
199endif()
Corentin Wallezd353ca02020-02-18 02:12:35 +0000200if (DAWN_USE_X11)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000201 target_compile_definitions(dawn_internal_config INTERFACE "DAWN_USE_X11")
202endif()
Corentin Wallez33466972020-02-24 13:27:28 +0000203if (WIN32)
204 target_compile_definitions(dawn_internal_config INTERFACE "NOMINMAX" "WIN32_LEAN_AND_MEAN")
205endif()
206
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000207set(CMAKE_CXX_STANDARD "14")
208
209################################################################################
210# Run on all subdirectories
211################################################################################
212
213add_subdirectory(third_party)
214add_subdirectory(src/common)
215add_subdirectory(generator)
216add_subdirectory(src/dawn)
217add_subdirectory(src/dawn_platform)
218add_subdirectory(src/dawn_native)
219add_subdirectory(src/dawn_wire)
dan sinclairbb3d7982020-10-23 13:10:20 +0000220# TODO(dawn:269): Remove once the implementation-based swapchains are removed.
221add_subdirectory(src/utils)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000222
223if (DAWN_BUILD_EXAMPLES)
David Neto762b7c32020-10-27 19:38:27 +0000224 #TODO(dawn:269): Add this once implementation-based swapchains are removed.
225 #add_subdirectory(src/utils)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +0000226 add_subdirectory(examples)
227endif()
Ben Claytonaffb7a32021-09-28 11:14:42 +0000228
229if (DAWN_BUILD_NODE_BINDINGS)
230 set(NODE_BINDING_DEPS
231 ${NODE_ADDON_API_DIR}
232 ${NODE_API_HEADERS_DIR}
233 ${WEBGPU_IDL_PATH}
234 )
235 foreach(DEP ${NODE_BINDING_DEPS})
236 if (NOT EXISTS ${DEP})
237 message(FATAL_ERROR
238 "DAWN_BUILD_NODE_BINDINGS requires missing dependency '${DEP}'\n"
Ben Claytond611aeb2021-10-05 15:43:59 +0000239 "Please follow the 'Fetch dependencies' instructions at:\n"
240 "./src/dawn_node/README.md"
Ben Claytonaffb7a32021-09-28 11:14:42 +0000241 )
242 endif()
243 endforeach()
244 if (NOT CMAKE_POSITION_INDEPENDENT_CODE)
245 message(FATAL_ERROR "DAWN_BUILD_NODE_BINDINGS requires building with DAWN_ENABLE_PIC")
246 endif()
247
248 add_subdirectory(src/dawn_node)
249endif()