blob: 8771e417df70a4a5a63fc1298155cb4e31d99dae [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#
Austin Engcc2516a2023-10-17 20:57:54 +00003# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are met:
Corentin Wallez7fe6efb2020-02-05 17:16:05 +00005#
Austin Engcc2516a2023-10-17 20:57:54 +00006# 1. Redistributions of source code must retain the above copyright notice, this
7# list of conditions and the following disclaimer.
Corentin Wallez7fe6efb2020-02-05 17:16:05 +00008#
Austin Engcc2516a2023-10-17 20:57:54 +00009# 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.
12#
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.
16#
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.
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000027
Aleksi Sapon9a106742021-09-28 16:48:01 +000028# Don't build testing in third_party dependencies
29set(BUILD_TESTING OFF)
30
Elie Michel9ae8ed22023-05-12 20:19:41 +000031# fetch_dawn_dependencies.py is an alternative to using depot_tools
32# It is particularly interesting when building dawn as a subdirectory in
33# a parent project that does not want to use depot_tools.
34if (${DAWN_FETCH_DEPENDENCIES})
Elie Michel9ae8ed22023-05-12 20:19:41 +000035 set(EXTRA_FETCH_ARGS)
36 if (NOT TARGET gmock AND ${TINT_BUILD_TESTS})
37 list(APPEND EXTRA_FETCH_ARGS --use-test-deps)
38 endif()
39
40 message(STATUS "Running fetch_dawn_dependencies:")
41 execute_process(
42 COMMAND
Corentin Wallez299ad592024-03-28 20:21:11 +000043 ${Python3_EXECUTABLE}
Elie Michel9ae8ed22023-05-12 20:19:41 +000044 "${PROJECT_SOURCE_DIR}/tools/fetch_dawn_dependencies.py"
45 --directory ${PROJECT_SOURCE_DIR}
46 ${EXTRA_FETCH_ARGS}
47 )
48endif ()
49
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000050if (NOT TARGET SPIRV-Headers)
Corentin Wallez42450c62020-04-10 17:04:31 +000051 set(SPIRV_HEADERS_SKIP_EXAMPLES ON CACHE BOOL "" FORCE)
52 set(SPIRV_HEADERS_SKIP_INSTALL ON CACHE BOOL "" FORCE)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000053
54 message(STATUS "Dawn: using SPIRV-Headers at ${DAWN_SPIRV_HEADERS_DIR}")
Aleksi Sapon9a106742021-09-28 16:48:01 +000055 add_subdirectory(${DAWN_SPIRV_HEADERS_DIR} "${CMAKE_CURRENT_BINARY_DIR}/spirv-headers")
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000056endif()
57
58if (NOT TARGET SPIRV-Tools)
Corentin Wallez42450c62020-04-10 17:04:31 +000059 set(SPIRV_SKIP_TESTS ON CACHE BOOL "" FORCE)
60 set(SPIRV_SKIP_EXECUTABLES ON CACHE BOOL "" FORCE)
61 set(SKIP_SPIRV_TOOLS_INSTALL ON CACHE BOOL "" FORCE)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000062
Ryan Harrisone87ac762022-04-06 15:37:27 -040063 if(${TINT_BUILD_SPV_READER} OR ${TINT_BUILD_SPV_WRITER})
Corentin Wallezf9f1c4b2022-05-16 09:11:21 +000064 set(SPIRV_SKIP_TESTS ON CACHE BOOL "Controls whether SPIR-V tests are run" FORCE)
65 set(SPIRV_WERROR OFF CACHE BOOL OFF FORCE)
66 if (${TINT_BUILD_SPIRV_TOOLS_FUZZER})
67 set(SPIRV_BUILD_FUZZER ON CACHE BOOL "Controls whether spirv-fuzz is built" FORCE)
68 endif()
Ryan Harrisone87ac762022-04-06 15:37:27 -040069 endif()
70
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000071 message(STATUS "Dawn: using SPIRV-Tools at ${DAWN_SPIRV_TOOLS_DIR}")
Ryan Harrisone87ac762022-04-06 15:37:27 -040072 add_subdirectory(${DAWN_SPIRV_TOOLS_DIR} "${CMAKE_CURRENT_BINARY_DIR}/spirv-tools" EXCLUDE_FROM_ALL)
Corentin Wallez7fe6efb2020-02-05 17:16:05 +000073endif()
74
Antonio Maiorano47591452023-10-19 16:09:04 +000075if(NOT TARGET glslang AND (${TINT_BUILD_GLSL_WRITER} OR ${TINT_BUILD_GLSL_VALIDATOR}) AND ${TINT_BUILD_CMD_TOOLS})
Corentin Wallezf9f1c4b2022-05-16 09:11:21 +000076 set(SKIP_GLSLANG_INSTALL ON CACHE BOOL "" FORCE)
Corentin Wallez2f18c9a2024-04-02 08:51:45 +000077 set(ENABLE_OPT OFF CACHE BOOL "" FORCE)
Corentin Wallezf9f1c4b2022-05-16 09:11:21 +000078 add_subdirectory("${DAWN_THIRD_PARTY_DIR}/vulkan-deps/glslang/src" "${CMAKE_CURRENT_BINARY_DIR}/glslang" EXCLUDE_FROM_ALL)
79endif()
80
Erin Melucci38b58e42023-06-14 16:45:33 +000081if (NOT TARGET glfw AND DAWN_USE_GLFW)
Ben Clayton0ecd67a2021-09-23 20:13:53 +000082 set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
83 set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
84 set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
Corentin Wallez978bc122023-05-15 13:01:16 +000085 set(GLFW_BUILD_X11 ${DAWN_USE_X11} CACHE BOOL "" FORCE)
86 set(GLFW_BUILD_WAYLAND ${DAWN_USE_WAYLAND} CACHE BOOL "" FORCE)
Ben Clayton0ecd67a2021-09-23 20:13:53 +000087
88 message(STATUS "Dawn: using GLFW at ${DAWN_GLFW_DIR}")
Aleksi Sapon9a106742021-09-28 16:48:01 +000089 add_subdirectory(${DAWN_GLFW_DIR} "${CMAKE_CURRENT_BINARY_DIR}/glfw")
Ben Clayton0ecd67a2021-09-23 20:13:53 +000090endif()
91
Brandon Jonesa04663c2021-09-23 20:36:03 +000092if (NOT TARGET libabsl)
93 message(STATUS "Dawn: using Abseil at ${DAWN_ABSEIL_DIR}")
dan sinclaircf2456b2023-01-07 23:09:14 +000094 if (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") OR
95 ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang"))
96 add_compile_options(
97 -Wno-array-parameter
98 -Wno-deprecated-builtins
99 -Wno-unknown-warning-option
100 )
101 endif()
102
Aleksi Sapon9a106742021-09-28 16:48:01 +0000103 add_subdirectory(${DAWN_ABSEIL_DIR} "${CMAKE_CURRENT_BINARY_DIR}/abseil")
Brandon Jonesa04663c2021-09-23 20:36:03 +0000104endif()
105
Loko Kungc96ef432022-04-02 03:02:40 +0000106if (NOT TARGET Vulkan-Headers)
107 message(STATUS "Dawn: using Vulkan-Headers at ${DAWN_VULKAN_HEADERS_DIR}")
108 add_subdirectory(${DAWN_VULKAN_HEADERS_DIR} "${CMAKE_CURRENT_BINARY_DIR}/vulkan-headers")
109endif()
110
Corentin Wallez215b5372020-02-15 00:39:13 +0000111# Header-only library for khrplatform.h
112add_library(dawn_khronos_platform INTERFACE)
Antonio Maiorano83bdc7f2023-08-21 15:29:35 +0000113target_sources(dawn_khronos_platform INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/khronos/EGL-Registry/api/KHR/khrplatform.h")
114target_include_directories(dawn_khronos_platform INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/khronos/EGL-Registry/api")
Ryan Harrisone87ac762022-04-06 15:37:27 -0400115
116# Header-only library for Vulkan headers
117add_library(dawn_vulkan_headers INTERFACE)
118target_sources(dawn_vulkan_headers INTERFACE
119 "${CMAKE_CURRENT_SOURCE_DIR}/khronos/vulkan/vk_icd.h"
120 "${CMAKE_CURRENT_SOURCE_DIR}/khronos/vulkan/vk_layer.h"
121 "${CMAKE_CURRENT_SOURCE_DIR}/khronos/vulkan/vk_platform.h"
122 "${CMAKE_CURRENT_SOURCE_DIR}/khronos/vulkan/vk_sdk_platform.h"
123 "${CMAKE_CURRENT_SOURCE_DIR}/khronos/vulkan/vulkan.h"
124 "${CMAKE_CURRENT_SOURCE_DIR}/khronos/vulkan/vulkan_core.h"
125)
126target_include_directories(dawn_vulkan_headers INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/khronos")
127
Corentin Wallezf9f1c4b2022-05-16 09:11:21 +0000128if (NOT TARGET vk_swiftshader AND ${DAWN_ENABLE_SWIFTSHADER})
Corentin Wallezbe352ea2022-04-11 16:48:43 +0000129 set(SWIFTSHADER_BUILD_TESTS OFF CACHE BOOL "" FORCE)
130 set(SWIFTSHADER_BUILD_BENCHMARKS OFF CACHE BOOL "" FORCE)
131
132 message(STATUS "Dawn: using Swiftshader at ${DAWN_SWIFTSHADER_DIR}")
133 add_subdirectory(${DAWN_SWIFTSHADER_DIR} "${CMAKE_CURRENT_BINARY_DIR}/swiftshader")
134endif()
135
Austin Eng6a7bba52023-04-17 18:11:51 +0000136if (${TINT_BUILD_BENCHMARKS} OR ${DAWN_BUILD_BENCHMARKS})
Corentin Wallezf9f1c4b2022-05-16 09:11:21 +0000137 set(BENCHMARK_ENABLE_TESTING FALSE CACHE BOOL FALSE FORCE)
Austin Eng6a7bba52023-04-17 18:11:51 +0000138 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/google_benchmark/src EXCLUDE_FROM_ALL)
Ben Claytonbe2362b2022-01-18 18:58:16 +0000139endif()
140
Corentin Wallezf9f1c4b2022-05-16 09:11:21 +0000141if (NOT TARGET gmock AND ${TINT_BUILD_TESTS})
142 set(gtest_force_shared_crt ON CACHE BOOL "Controls whether a shared run-time library should be used even when Google Test is built as static library" FORCE)
143 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/googletest EXCLUDE_FROM_ALL)
David Neto5b46d712020-06-26 22:29:27 +0000144endif()
Antonio Maioranoc69356e2023-11-23 18:04:51 +0000145
146function(AddSubdirectoryDXC)
147 # We use a CMake function so that all these (non-cache) variables are scoped
148 # only to this function.
149 set(HLSL_OPTIONAL_PROJS_IN_DEFAULT OFF)
150 set(HLSL_ENABLE_ANALYZE OFF)
151 set(HLSL_OFFICIAL_BUILD OFF)
152 set(HLSL_ENABLE_FIXED_VER OFF)
153 set(HLSL_BUILD_DXILCONV OFF)
154 set(HLSL_INCLUDE_TESTS OFF)
155
156 set(ENABLE_SPIRV_CODEGEN OFF)
157 set(SPIRV_BUILD_TESTS OFF)
158
159 set(LLVM_BUILD_RUNTIME ON)
160 set(LLVM_BUILD_EXAMPLES OFF)
161 set(LLVM_BUILD_TESTS OFF)
162 set(LLVM_INCLUDE_TESTS OFF)
163 set(LLVM_INCLUDE_DOCS OFF)
164 set(LLVM_INCLUDE_EXAMPLES OFF)
165 set(LLVM_OPTIMIZED_TABLEGEN OFF)
166 set(LLVM_APPEND_VC_REV OFF)
167 # Enable exception handling (requires RTTI)
168 set(LLVM_ENABLE_RTTI ON)
169 set(LLVM_ENABLE_EH ON)
170 set(CLANG_CL OFF)
171
Antonio Maiorano39357642024-03-27 18:23:32 +0000172 if (DAWN_ENABLE_ASAN AND DAWN_ENABLE_UBSAN)
173 set(LLVM_USE_SANITIZER "Address;Undefined")
174 elseif(DAWN_ENABLE_ASAN)
175 set(LLVM_USE_SANITIZER "Address")
176 elseif(DAWN_ENABLE_UBSAN)
177 set(LLVM_USE_SANITIZER "Undefined")
178 endif()
179
Antonio Maioranoc69356e2023-11-23 18:04:51 +0000180 # Cache variables -- these are *not* scoped to this function
181 set(LLVM_TARGETS_TO_BUILD "None" CACHE STRING "" FORCE)
182 set(LLVM_DEFAULT_TARGET_TRIPLE "dxil-ms-dx" CACHE STRING "" FORCE)
183 set(CLANG_ENABLE_STATIC_ANALYZER OFF CACHE BOOL "" FORCE)
184 set(CLANG_ENABLE_ARCMT OFF CACHE BOOL "" FORCE)
185 set(CLANG_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
186 set(CLANG_INCLUDE_TESTS OFF CACHE BOOL "" FORCE)
187
Antonio Maiorano76d82192024-01-25 17:45:32 +0000188 if (NOT WIN32)
189 set(DIRECTX_HEADER_INCLUDE_DIR "${DAWN_THIRD_PARTY_DIR}/dxheaders/include")
190 endif()
Antonio Maioranoc69356e2023-11-23 18:04:51 +0000191
192 # Disable HCT.cmake looking for and using clang-format. This is used to compare generated files
193 # against the copy that is committed to the repo, but fails because the DXC .clangformat file is
194 # not visible from our build dir. We don't need this validation, so just disable it.
195 set(CLANG_FORMAT_EXE "" CACHE STRING "" FORCE)
196
Antonio Maioranob4d05de2024-04-16 19:36:58 +0000197 if (CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT DAWN_DXC_DISABLE_ASSERTS_DEBUG)
198 set(LLVM_ENABLE_ASSERTIONS ON CACHE BOOL "" FORCE)
199 else()
200 set(LLVM_ENABLE_ASSERTIONS OFF CACHE BOOL "" FORCE)
201 endif()
Antonio Maioranoc69356e2023-11-23 18:04:51 +0000202
203 # Override RPATH so that it points to current dir (exe path). This allows both executable and
204 # shared library to be in the same location, which we set below. Note that DXC places places
205 # executables in a bin directory, and shared libraries in a lib directory.
206 if (APPLE)
207 set(CMAKE_INSTALL_NAME_DIR "@rpath")
208 set(CMAKE_INSTALL_RPATH "@executable_path")
209 else()
210 set(CMAKE_INSTALL_RPATH "\$ORIGIN")
211 endif()
212
213 message(STATUS "\nAdding DXC to build:\n")
214 add_subdirectory(dxc
215 # Disable all targets by default, and enable only the dxc and dxcompiler targets (below)
216 EXCLUDE_FROM_ALL
217 )
218 set_target_properties(dxc PROPERTIES EXCLUDE_FROM_ALL FALSE)
219 set_target_properties(dxcompiler PROPERTIES EXCLUDE_FROM_ALL FALSE)
220
221 # Override output dir for both targets so that they end up in the same place
222 # as the rest of our binaries. Otherwise, DXC wants its outputs in bin and lib dirs.
223 get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
224 if (isMultiConfig)
225 set_target_properties(dxc dxcompiler PROPERTIES
226 "RUNTIME_OUTPUT_DIRECTORY_DEBUG" "${CMAKE_BINARY_DIR}/$<CONFIG>"
227 "RUNTIME_OUTPUT_DIRECTORY_RELEASE" "${CMAKE_BINARY_DIR}/$<CONFIG>"
228 "LIBRARY_OUTPUT_DIRECTORY_DEBUG" "${CMAKE_BINARY_DIR}/$<CONFIG>"
229 "LIBRARY_OUTPUT_DIRECTORY_RELEASE" "${CMAKE_BINARY_DIR}/$<CONFIG>"
230 )
231 else()
232 set_target_properties(dxc dxcompiler PROPERTIES
233 "RUNTIME_OUTPUT_DIRECTORY" "${CMAKE_BINARY_DIR}"
234 "LIBRARY_OUTPUT_DIRECTORY" "${CMAKE_BINARY_DIR}"
235 )
236 endif()
237
238 # Create a target that copies dxil.dll from the platform SDK
Antonio Maioranoc69356e2023-11-23 18:04:51 +0000239 if (WIN32)
Antonio Maiorano08c4d0a2024-03-19 15:09:08 +0000240 message(STATUS "Finding Windows SDK Directory")
241
242 message(STATUS "Display environment variables:")
243 execute_process(COMMAND ${CMAKE_COMMAND} -E environment COMMAND_ECHO STDOUT)
244
Antonio Maiorano01c4a652024-03-13 19:38:13 +0000245 if(DEFINED ENV{WINDOWSSDKDIR})
246 # If WINDOWSSDKDIR env var is defined, use its value. This is defined, for example, when
247 # using a Visual Studio command prompt.
248 set(WIN10_SDK_PATH "$ENV{WINDOWSSDKDIR}")
Antonio Maiorano08c4d0a2024-03-19 15:09:08 +0000249 message(STATUS "Found WINDOWSSDKDIR environment variable: ${WIN10_SDK_PATH}")
Antonio Maiorano01c4a652024-03-13 19:38:13 +0000250 else()
251 # There's no easy way to get the Windows SDK path in CMake; however, conveniently, DXC
252 # contains a FindD3D12.cmake file that returns WIN10_SDK_PATH and WIN10_SDK_VERSION,
253 # so let's use that.
254 # TODO(crbug.com/tint/2106): Get the Win10 SDK path and version ourselves until
255 # dxc/cmake/modules/FindD3D12.cmake supports non-VS generators.
256 get_filename_component(WIN10_SDK_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots;KitsRoot10]" ABSOLUTE CACHE)
Antonio Maiorano08c4d0a2024-03-19 15:09:08 +0000257 message(STATUS "WINDOWSSDKDIR environment variable is not defined, retrieving from registry: ${WIN10_SDK_PATH}")
Antonio Maiorano01c4a652024-03-13 19:38:13 +0000258 endif()
259
Antonio Maiorano08c4d0a2024-03-19 15:09:08 +0000260 message(STATUS "Finding Windows SDK version")
Antonio Maioranoc69356e2023-11-23 18:04:51 +0000261 if (CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
262 set (WIN10_SDK_VERSION ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION})
Antonio Maiorano08c4d0a2024-03-19 15:09:08 +0000263 message(STATUS "Found Windows SDK version from CMake variable 'CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION': ${WIN10_SDK_VERSION}")
Antonio Maioranoc69356e2023-11-23 18:04:51 +0000264 else()
265 # CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION may not be defined if, for example,
266 # the Ninja generator is used instead of Visual Studio. Attempt to retrieve the
267 # most recent SDK version from the list of paths under "${WIN10_SDK_PATH}/Include/".
268 file(GLOB sdk_dirs RELATIVE "${WIN10_SDK_PATH}/Include/" "${WIN10_SDK_PATH}/Include/10.*")
269 if (sdk_dirs)
Antonio Maiorano08c4d0a2024-03-19 15:09:08 +0000270 list(POP_BACK sdk_dirs WIN10_SDK_VERSION)
Antonio Maioranoc69356e2023-11-23 18:04:51 +0000271 endif()
272 unset(sdk_dirs)
Antonio Maiorano08c4d0a2024-03-19 15:09:08 +0000273 message(STATUS "Windows SDK version not found from CMake variable 'CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION', attempted to find from SDK path: ${WIN10_SDK_VERSION}")
Antonio Maioranoc69356e2023-11-23 18:04:51 +0000274 endif()
275
276 set(DXIL_DLL_PATH "${WIN10_SDK_PATH}/bin/${WIN10_SDK_VERSION}/x64/dxil.dll")
277 add_custom_target(copy_dxil_dll)
278 add_custom_command(
279 TARGET copy_dxil_dll
280 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DXIL_DLL_PATH} $<TARGET_FILE_DIR:dxcompiler>
281 COMMENT "Copying ${DXIL_DLL_PATH} to $<TARGET_FILE_DIR:dxcompiler>")
282 # Make dxc target depend on copy_dxil_dll
283 add_dependencies(dxc copy_dxil_dll)
284 endif()
285endfunction()
286
287if (DAWN_USE_BUILT_DXC)
288 AddSubdirectoryDXC()
289endif()
Ben Claytonf9a5b712024-03-13 17:37:33 +0000290
291if (TINT_BUILD_TINTD)
292 set(LANGSVR_JSON_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp")
293 add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp" EXCLUDE_FROM_ALL)
294 add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/langsvr" EXCLUDE_FROM_ALL)
295endif()