Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 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 | |
| 15 | cmake_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 Wallez | 42450c6 | 2020-04-10 17:04:31 +0000 | [diff] [blame] | 20 | # case any of the generator files changes. We should also remove the CACHE "" FORCE stuff to |
dan sinclair | 1877c27 | 2020-10-20 19:46:21 +0000 | [diff] [blame] | 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"` |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 23 | |
| 24 | project( |
| 25 | Dawn |
| 26 | DESCRIPTION "Dawn, a WebGPU implementation" |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 27 | LANGUAGES C CXX |
| 28 | ) |
| 29 | |
| 30 | set_property(GLOBAL PROPERTY USE_FOLDERS ON) |
| 31 | |
| 32 | if(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) |
| 36 | endif() |
| 37 | |
| 38 | set(DAWN_BUILD_GEN_DIR "${Dawn_BINARY_DIR}/gen") |
| 39 | set(DAWN_GENERATOR_DIR "${Dawn_SOURCE_DIR}/generator") |
| 40 | set(DAWN_SRC_DIR "${Dawn_SOURCE_DIR}/src") |
| 41 | set(DAWN_INCLUDE_DIR "${DAWN_SRC_DIR}/include") |
| 42 | set(DAWN_TEMPLATE_DIR "${DAWN_GENERATOR_DIR}/templates") |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 43 | |
| 44 | set(DAWN_DUMMY_FILE "${DAWN_SRC_DIR}/Dummy.cpp") |
| 45 | |
| 46 | ################################################################################ |
| 47 | # Configuration options |
| 48 | ################################################################################ |
| 49 | |
Ben Clayton | 30eeac7 | 2021-09-24 10:38:18 +0000 | [diff] [blame] | 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. |
| 56 | function (option_if_not_defined name description default) |
| 57 | if(NOT DEFINED ${name}) |
| 58 | option(${name} ${description} ${default}) |
| 59 | endif() |
| 60 | endfunction() |
| 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. |
| 68 | function (set_if_not_defined name value description) |
| 69 | if(NOT DEFINED ${name}) |
| 70 | set(${name} ${value} CACHE STRING ${description}) |
| 71 | endif() |
| 72 | endfunction() |
| 73 | |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 74 | # Default values for the backend-enabling options |
| 75 | set(ENABLE_D3D12 OFF) |
| 76 | set(ENABLE_METAL OFF) |
Sergey Karchevsky | 3a75b1c | 2021-06-30 15:06:43 +0000 | [diff] [blame] | 77 | set(ENABLE_OPENGLES OFF) |
| 78 | set(ENABLE_DESKTOP_GL OFF) |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 79 | set(ENABLE_VULKAN OFF) |
Corentin Wallez | d353ca0 | 2020-02-18 02:12:35 +0000 | [diff] [blame] | 80 | set(USE_X11 OFF) |
陈俊嘉 | b2bc57a | 2021-06-24 07:00:16 +0000 | [diff] [blame] | 81 | set(BUILD_EXAMPLE OFF) |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 82 | if (WIN32) |
| 83 | set(ENABLE_D3D12 ON) |
陈俊嘉 | 610de1d | 2021-06-28 08:19:28 +0000 | [diff] [blame] | 84 | if (NOT WINDOWS_STORE) |
陈俊嘉 | b2bc57a | 2021-06-24 07:00:16 +0000 | [diff] [blame] | 85 | # Enable Vulkan in win32 compilation only |
| 86 | # since UWP only supports d3d |
| 87 | set(ENABLE_VULKAN ON) |
| 88 | endif() |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 89 | elseif(APPLE) |
| 90 | set(ENABLE_METAL ON) |
| 91 | elseif(UNIX) |
Sergey Karchevsky | 3a75b1c | 2021-06-30 15:06:43 +0000 | [diff] [blame] | 92 | set(ENABLE_OPENGLES ON) |
| 93 | set(ENABLE_DESKTOP_GL ON) |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 94 | set(ENABLE_VULKAN ON) |
Corentin Wallez | d353ca0 | 2020-02-18 02:12:35 +0000 | [diff] [blame] | 95 | set(USE_X11 ON) |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 96 | endif() |
| 97 | |
陈俊嘉 | b2bc57a | 2021-06-24 07:00:16 +0000 | [diff] [blame] | 98 | # GLFW is not supported in UWP |
| 99 | if((WIN32 AND NOT WINDOWS_STORE) OR UNIX) |
| 100 | set(DAWN_SUPPORTS_GLFW_FOR_WINDOWING ON) |
| 101 | endif() |
| 102 | |
| 103 | # Current examples are depend on GLFW |
| 104 | if (DAWN_SUPPORTS_GLFW_FOR_WINDOWING) |
| 105 | set(BUILD_EXAMPLE ON) |
| 106 | endif() |
| 107 | |
Ben Clayton | 30eeac7 | 2021-09-24 10:38:18 +0000 | [diff] [blame] | 108 | option_if_not_defined(DAWN_ENABLE_D3D12 "Enable compilation of the D3D12 backend" ${ENABLE_D3D12}) |
| 109 | option_if_not_defined(DAWN_ENABLE_METAL "Enable compilation of the Metal backend" ${ENABLE_METAL}) |
| 110 | option_if_not_defined(DAWN_ENABLE_NULL "Enable compilation of the Null backend" ON) |
| 111 | option_if_not_defined(DAWN_ENABLE_DESKTOP_GL "Enable compilation of the OpenGL backend" ${ENABLE_DESKTOP_GL}) |
| 112 | option_if_not_defined(DAWN_ENABLE_OPENGLES "Enable compilation of the OpenGL ES backend" ${ENABLE_OPENGLES}) |
| 113 | option_if_not_defined(DAWN_ENABLE_VULKAN "Enable compilation of the Vulkan backend" ${ENABLE_VULKAN}) |
| 114 | option_if_not_defined(DAWN_ALWAYS_ASSERT "Enable assertions on all build types" OFF) |
| 115 | option_if_not_defined(DAWN_USE_X11 "Enable support for X11 surface" ${USE_X11}) |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 116 | |
Ben Clayton | 30eeac7 | 2021-09-24 10:38:18 +0000 | [diff] [blame] | 117 | option_if_not_defined(DAWN_BUILD_EXAMPLES "Enables building Dawn's exmaples" ${BUILD_EXAMPLE}) |
Ben Clayton | affb7a3 | 2021-09-28 11:14:42 +0000 | [diff] [blame] | 118 | option_if_not_defined(DAWN_BUILD_NODE_BINDINGS "Enables building Dawn's NodeJS bindings" OFF) |
| 119 | |
| 120 | option_if_not_defined(DAWN_ENABLE_PIC "Build with Position-Independent-Code enabled" OFF) |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 121 | |
Ben Clayton | 30eeac7 | 2021-09-24 10:38:18 +0000 | [diff] [blame] | 122 | set_if_not_defined(DAWN_THIRD_PARTY_DIR "${Dawn_SOURCE_DIR}/third_party" "Directory in which to find third-party dependencies.") |
Corentin Wallez | e557087 | 2020-10-20 14:26:10 +0000 | [diff] [blame] | 123 | |
Brandon Jones | a04663c | 2021-09-23 20:36:03 +0000 | [diff] [blame] | 124 | # Recommended setting for compability with future abseil releases. |
| 125 | set(ABSL_PROPAGATE_CXX_STD ON) |
Brandon Jones | a04663c | 2021-09-23 20:36:03 +0000 | [diff] [blame] | 126 | |
Ben Clayton | 30eeac7 | 2021-09-24 10:38:18 +0000 | [diff] [blame] | 127 | set_if_not_defined(DAWN_ABSEIL_DIR "${DAWN_THIRD_PARTY_DIR}/abseil-cpp" "Directory in which to find Abseil") |
| 128 | set_if_not_defined(DAWN_GLFW_DIR "${DAWN_THIRD_PARTY_DIR}/glfw" "Directory in which to find GLFW") |
Ben Clayton | 30eeac7 | 2021-09-24 10:38:18 +0000 | [diff] [blame] | 129 | set_if_not_defined(DAWN_JINJA2_DIR "${DAWN_THIRD_PARTY_DIR}/jinja2" "Directory in which to find Jinja2") |
| 130 | set_if_not_defined(DAWN_SPIRV_CROSS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-cross/src" "Directory in which to find SPIRV-Cross") |
| 131 | set_if_not_defined(DAWN_SPIRV_HEADERS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-headers/src" "Directory in which to find SPIRV-Headers") |
| 132 | set_if_not_defined(DAWN_SPIRV_TOOLS_DIR "${DAWN_THIRD_PARTY_DIR}/vulkan-deps/spirv-tools/src" "Directory in which to find SPIRV-Tools") |
| 133 | set_if_not_defined(DAWN_TINT_DIR "${DAWN_THIRD_PARTY_DIR}/tint" "Directory in which to find Tint") |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 134 | |
Ben Clayton | affb7a3 | 2021-09-28 11:14:42 +0000 | [diff] [blame] | 135 | # Dependencies for DAWN_BUILD_NODE_BINDINGS |
| 136 | set_if_not_defined(NODE_ADDON_API_DIR "${DAWN_THIRD_PARTY_DIR}/node-addon-api" "Directory in which to find node-addon-api") |
Ben Clayton | 72e3ba6 | 2021-09-30 18:04:01 +0000 | [diff] [blame] | 137 | set_if_not_defined(NODE_API_HEADERS_DIR "${DAWN_THIRD_PARTY_DIR}/node-api-headers" "Directory in which to find node-api-headers") |
Ben Clayton | affb7a3 | 2021-09-28 11:14:42 +0000 | [diff] [blame] | 138 | set_if_not_defined(WEBGPU_IDL_PATH "${DAWN_THIRD_PARTY_DIR}/gpuweb/webgpu.idl" "Path to the webgpu.idl definition file") |
Austin Eng | 4948c81 | 2021-10-15 14:28:32 +0000 | [diff] [blame] | 139 | set_if_not_defined(GO_EXECUTABLE "go" "Golang executable for running the IDL generator") |
Ben Clayton | affb7a3 | 2021-09-28 11:14:42 +0000 | [diff] [blame] | 140 | |
Sergey Karchevsky | 3a75b1c | 2021-06-30 15:06:43 +0000 | [diff] [blame] | 141 | # Much of the backend code is shared among desktop OpenGL and OpenGL ES |
| 142 | if (${DAWN_ENABLE_DESKTOP_GL} OR ${DAWN_ENABLE_OPENGLES}) |
| 143 | set(DAWN_ENABLE_OPENGL ON) |
| 144 | endif() |
| 145 | |
Ben Clayton | c19329c | 2021-09-23 19:24:43 +0000 | [diff] [blame] | 146 | # OpenGL backend requires SPIRV-Cross |
| 147 | set(DAWN_REQUIRES_SPIRV_CROSS OFF) |
| 148 | if (DAWN_ENABLE_OPENGL) |
| 149 | set(DAWN_REQUIRES_SPIRV_CROSS ON) |
| 150 | endif() |
| 151 | |
Ben Clayton | affb7a3 | 2021-09-28 11:14:42 +0000 | [diff] [blame] | 152 | if(DAWN_ENABLE_PIC) |
| 153 | set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 154 | endif() |
| 155 | |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 156 | ################################################################################ |
| 157 | # Dawn's public and internal "configs" |
| 158 | ################################################################################ |
| 159 | |
| 160 | # The public config contains only the include paths for the Dawn headers. |
| 161 | add_library(dawn_public_config INTERFACE) |
| 162 | target_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 |
| 168 | add_library(dawn_internal_config INTERFACE) |
| 169 | target_include_directories(dawn_internal_config INTERFACE |
| 170 | "${DAWN_SRC_DIR}" |
| 171 | "${DAWN_BUILD_GEN_DIR}/src" |
| 172 | ) |
| 173 | target_link_libraries(dawn_internal_config INTERFACE dawn_public_config) |
| 174 | |
| 175 | # Compile definitions for the internal config |
| 176 | if (DAWN_ALWAYS_ASSERT OR $<CONFIG:Debug>) |
| 177 | target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_ASSERTS") |
| 178 | endif() |
| 179 | if (DAWN_ENABLE_D3D12) |
| 180 | target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_D3D12") |
| 181 | endif() |
| 182 | if (DAWN_ENABLE_METAL) |
| 183 | target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_METAL") |
| 184 | endif() |
| 185 | if (DAWN_ENABLE_NULL) |
| 186 | target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_NULL") |
| 187 | endif() |
Sergey Karchevsky | 3a75b1c | 2021-06-30 15:06:43 +0000 | [diff] [blame] | 188 | if (DAWN_ENABLE_DESKTOP_GL) |
| 189 | target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_DESKTOP_GL") |
| 190 | endif() |
| 191 | if (DAWN_ENABLE_OPENGLES) |
| 192 | target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_OPENGLES") |
| 193 | endif() |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 194 | if (DAWN_ENABLE_OPENGL) |
| 195 | target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_OPENGL") |
| 196 | endif() |
| 197 | if (DAWN_ENABLE_VULKAN) |
| 198 | target_compile_definitions(dawn_internal_config INTERFACE "DAWN_ENABLE_BACKEND_VULKAN") |
| 199 | endif() |
Corentin Wallez | d353ca0 | 2020-02-18 02:12:35 +0000 | [diff] [blame] | 200 | if (DAWN_USE_X11) |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 201 | target_compile_definitions(dawn_internal_config INTERFACE "DAWN_USE_X11") |
| 202 | endif() |
Corentin Wallez | 3346697 | 2020-02-24 13:27:28 +0000 | [diff] [blame] | 203 | if (WIN32) |
| 204 | target_compile_definitions(dawn_internal_config INTERFACE "NOMINMAX" "WIN32_LEAN_AND_MEAN") |
| 205 | endif() |
| 206 | |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 207 | set(CMAKE_CXX_STANDARD "14") |
| 208 | |
| 209 | ################################################################################ |
| 210 | # Run on all subdirectories |
| 211 | ################################################################################ |
| 212 | |
| 213 | add_subdirectory(third_party) |
| 214 | add_subdirectory(src/common) |
| 215 | add_subdirectory(generator) |
| 216 | add_subdirectory(src/dawn) |
| 217 | add_subdirectory(src/dawn_platform) |
| 218 | add_subdirectory(src/dawn_native) |
| 219 | add_subdirectory(src/dawn_wire) |
dan sinclair | bb3d798 | 2020-10-23 13:10:20 +0000 | [diff] [blame] | 220 | # TODO(dawn:269): Remove once the implementation-based swapchains are removed. |
| 221 | add_subdirectory(src/utils) |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 222 | |
| 223 | if (DAWN_BUILD_EXAMPLES) |
David Neto | 762b7c3 | 2020-10-27 19:38:27 +0000 | [diff] [blame] | 224 | #TODO(dawn:269): Add this once implementation-based swapchains are removed. |
| 225 | #add_subdirectory(src/utils) |
Corentin Wallez | 7fe6efb | 2020-02-05 17:16:05 +0000 | [diff] [blame] | 226 | add_subdirectory(examples) |
| 227 | endif() |
Ben Clayton | affb7a3 | 2021-09-28 11:14:42 +0000 | [diff] [blame] | 228 | |
| 229 | if (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 Clayton | d611aeb | 2021-10-05 15:43:59 +0000 | [diff] [blame] | 239 | "Please follow the 'Fetch dependencies' instructions at:\n" |
| 240 | "./src/dawn_node/README.md" |
Ben Clayton | affb7a3 | 2021-09-28 11:14:42 +0000 | [diff] [blame] | 241 | ) |
| 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) |
| 249 | endif() |