• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 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
20# case any of the generator files changes. We should also remove the CACHE "" FORCE stuff to
21# 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"`
23
24project(
25    Dawn
26    DESCRIPTION "Dawn, a WebGPU implementation"
27    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")
43
44set(DAWN_DUMMY_FILE "${DAWN_SRC_DIR}/Dummy.cpp")
45
46################################################################################
47# Configuration options
48################################################################################
49
50# 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
74# Default values for the backend-enabling options
75set(ENABLE_D3D12 OFF)
76set(ENABLE_METAL OFF)
77set(ENABLE_OPENGLES OFF)
78set(ENABLE_DESKTOP_GL OFF)
79set(ENABLE_VULKAN OFF)
80set(USE_X11 OFF)
81set(BUILD_EXAMPLE OFF)
82if (WIN32)
83    set(ENABLE_D3D12 ON)
84    if (NOT WINDOWS_STORE)
85        # Enable Vulkan in win32 compilation only
86        # since UWP only supports d3d
87        set(ENABLE_VULKAN ON)
88    endif()
89elseif(APPLE)
90    set(ENABLE_METAL ON)
91elseif(UNIX)
92    set(ENABLE_OPENGLES ON)
93    set(ENABLE_DESKTOP_GL ON)
94    set(ENABLE_VULKAN ON)
95    set(USE_X11 ON)
96endif()
97
98# 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
108option_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})
116
117option_if_not_defined(DAWN_BUILD_EXAMPLES "Enables building Dawn's exmaples" ${BUILD_EXAMPLE})
118option_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)
121
122set_if_not_defined(DAWN_THIRD_PARTY_DIR "${Dawn_SOURCE_DIR}/third_party" "Directory in which to find third-party dependencies.")
123
124# Recommended setting for compability with future abseil releases.
125set(ABSL_PROPAGATE_CXX_STD ON)
126
127set_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")
129set_if_not_defined(DAWN_GLM_DIR "${DAWN_THIRD_PARTY_DIR}/glm" "Directory in which to find GLM")
130set_if_not_defined(DAWN_JINJA2_DIR "${DAWN_THIRD_PARTY_DIR}/jinja2" "Directory in which to find Jinja2")
131set_if_not_defined(DAWN_SPIRV_CROSS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-cross/src" "Directory in which to find SPIRV-Cross")
132set_if_not_defined(DAWN_SPIRV_HEADERS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-headers/src" "Directory in which to find SPIRV-Headers")
133set_if_not_defined(DAWN_SPIRV_TOOLS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-tools/src" "Directory in which to find SPIRV-Tools")
134set_if_not_defined(DAWN_TINT_DIR "${DAWN_THIRD_PARTY_DIR}/tint" "Directory in which to find Tint")
135
136# Dependencies for DAWN_BUILD_NODE_BINDINGS
137set_if_not_defined(NODE_ADDON_API_DIR "${DAWN_THIRD_PARTY_DIR}/node-addon-api" "Directory in which to find node-addon-api")
138set_if_not_defined(NODE_API_HEADERS_DIR "${DAWN_THIRD_PARTY_DIR}/node-api-headers" "Directory in which to find node-api-headers")
139set_if_not_defined(WEBGPU_IDL_PATH "${DAWN_THIRD_PARTY_DIR}/gpuweb/webgpu.idl" "Path to the webgpu.idl definition file")
140set_if_not_defined(GO_EXECUTABLE "go" "Golang executable for running the IDL generator")
141
142# Much of the backend code is shared among desktop OpenGL and OpenGL ES
143if (${DAWN_ENABLE_DESKTOP_GL} OR ${DAWN_ENABLE_OPENGLES})
144    set(DAWN_ENABLE_OPENGL ON)
145endif()
146
147# OpenGL backend requires SPIRV-Cross
148set(DAWN_REQUIRES_SPIRV_CROSS OFF)
149if (DAWN_ENABLE_OPENGL)
150    set(DAWN_REQUIRES_SPIRV_CROSS ON)
151endif()
152
153if(DAWN_ENABLE_PIC)
154    set(CMAKE_POSITION_INDEPENDENT_CODE ON)
155endif()
156
157################################################################################
158# Dawn's public and internal "configs"
159################################################################################
160
161# The public config contains only the include paths for the Dawn headers.
162add_library(dawn_public_config INTERFACE)
163target_include_directories(dawn_public_config INTERFACE
164    "${DAWN_SRC_DIR}/include"
165    "${DAWN_BUILD_GEN_DIR}/src/include"
166)
167
168# The internal config conatins additional path but includes the dawn_public_config include paths
169add_library(dawn_internal_config INTERFACE)
170target_include_directories(dawn_internal_config INTERFACE
171    "${DAWN_SRC_DIR}"
172    "${DAWN_BUILD_GEN_DIR}/src"
173)
174target_link_libraries(dawn_internal_config INTERFACE dawn_public_config)
175
176# Compile definitions for the internal config
177if (DAWN_ALWAYS_ASSERT OR $<CONFIG:Debug>)
178    target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_ASSERTS")
179endif()
180if (DAWN_ENABLE_D3D12)
181    target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_D3D12")
182endif()
183if (DAWN_ENABLE_METAL)
184    target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_METAL")
185endif()
186if (DAWN_ENABLE_NULL)
187    target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_NULL")
188endif()
189if (DAWN_ENABLE_DESKTOP_GL)
190    target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_DESKTOP_GL")
191endif()
192if (DAWN_ENABLE_OPENGLES)
193    target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_OPENGLES")
194endif()
195if (DAWN_ENABLE_OPENGL)
196    target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_OPENGL")
197endif()
198if (DAWN_ENABLE_VULKAN)
199    target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_VULKAN")
200endif()
201if (DAWN_USE_X11)
202    target_compile_definitions(dawn_internal_config INTERFACE "DAWN_USE_X11")
203endif()
204if (WIN32)
205    target_compile_definitions(dawn_internal_config INTERFACE "NOMINMAX" "WIN32_LEAN_AND_MEAN")
206endif()
207
208set(CMAKE_CXX_STANDARD "14")
209
210################################################################################
211# Run on all subdirectories
212################################################################################
213
214add_subdirectory(third_party)
215add_subdirectory(src/common)
216add_subdirectory(generator)
217add_subdirectory(src/dawn)
218add_subdirectory(src/dawn_platform)
219add_subdirectory(src/dawn_native)
220add_subdirectory(src/dawn_wire)
221# TODO(dawn:269): Remove once the implementation-based swapchains are removed.
222add_subdirectory(src/utils)
223
224if (DAWN_BUILD_EXAMPLES)
225    #TODO(dawn:269): Add this once implementation-based swapchains are removed.
226    #add_subdirectory(src/utils)
227    add_subdirectory(examples)
228endif()
229
230if (DAWN_BUILD_NODE_BINDINGS)
231    set(NODE_BINDING_DEPS
232        ${NODE_ADDON_API_DIR}
233        ${NODE_API_HEADERS_DIR}
234        ${WEBGPU_IDL_PATH}
235    )
236    foreach(DEP ${NODE_BINDING_DEPS})
237        if (NOT EXISTS ${DEP})
238            message(FATAL_ERROR
239                "DAWN_BUILD_NODE_BINDINGS requires missing dependency '${DEP}'\n"
240                "Please follow the 'Fetch dependencies' instructions at:\n"
241                "./src/dawn_node/README.md"
242            )
243        endif()
244    endforeach()
245    if (NOT CMAKE_POSITION_INDEPENDENT_CODE)
246        message(FATAL_ERROR "DAWN_BUILD_NODE_BINDINGS requires building with DAWN_ENABLE_PIC")
247    endif()
248
249    add_subdirectory(src/dawn_node)
250endif()
251