-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathFetchWgpuNativePrecompiled.cmake
More file actions
223 lines (180 loc) · 6.41 KB
/
Copy pathFetchWgpuNativePrecompiled.cmake
File metadata and controls
223 lines (180 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# This file is part of the "Learn WebGPU for C++" book.
# https://eliemichel.github.io/LearnWebGPU
#
# MIT License
# Copyright (c) 2022-2025 Elie Michel and the wgpu-native authors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
include(FetchContent)
# Not using emscripten, so we download binaries. There are many different
# combinations of OS, CPU architecture and compiler (the later is only
# relevant when using static linking), so here are a lot of boring "if".
detect_system_architecture()
# Check 'WEBGPU_LINK_TYPE' argument
set(USE_SHARED_LIB)
if (WEBGPU_LINK_TYPE STREQUAL "SHARED")
set(USE_SHARED_LIB TRUE)
elseif (WEBGPU_LINK_TYPE STREQUAL "STATIC")
set(USE_SHARED_LIB FALSE)
else()
message(FATAL_ERROR "Link type '${WEBGPU_LINK_TYPE}' is not valid. Possible values for WEBGPU_LINK_TYPE are SHARED and STATIC.")
endif()
# Build URL to fetch
set(URL_OS)
set(URL_COMPILER)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(URL_OS "windows")
if (MSVC)
set(URL_COMPILER "msvc")
else()
set(URL_COMPILER "gnu")
endif()
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(URL_OS "linux")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(URL_OS "macos")
else()
message(FATAL_ERROR "Platform system '${CMAKE_SYSTEM_NAME}' not supported by this release of WebGPU. You may consider building it yourself from its source (see https://github.com/gfx-rs/wgpu-native)")
endif()
set(URL_ARCH)
if (ARCH STREQUAL "x86_64")
set(URL_ARCH "x86_64")
elseif (ARCH STREQUAL "aarch64")
set(URL_ARCH "aarch64")
elseif (ARCH STREQUAL "i686" AND CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(URL_ARCH "i686")
else()
message(FATAL_ERROR "Platform architecture '${ARCH}' not supported by this release of WebGPU. You may consider building it yourself from its source (see https://github.com/gfx-rs/wgpu-native)")
endif()
# We only fetch release builds (NB: this may cause issue when using static
# linking, but not with dynamic)
set(URL_CONFIG release)
# Finally build the URL. The URL_NAME is also used as FetchContent
# identifier (BTW it must be lowercase).
if (URL_COMPILER)
set(URL_NAME "wgpu-${URL_OS}-${URL_ARCH}-${URL_COMPILER}-${URL_CONFIG}")
else()
set(URL_NAME "wgpu-${URL_OS}-${URL_ARCH}-${URL_CONFIG}")
endif()
set(URL "${WGPU_BINARY_MIRROR}/releases/download/${WGPU_VERSION}/${URL_NAME}.zip")
string(TOLOWER "${URL_NAME}" FC_NAME)
# Declare FetchContent, then make available
FetchContent_Declare(${FC_NAME}
URL ${URL}
)
# TODO: Display the "Fetching" message only when actually downloading
message(STATUS "Fetching WebGPU implementation from '${URL}'")
FetchContent_MakeAvailable(${FC_NAME})
set(ZIP_DIR "${${FC_NAME}_SOURCE_DIR}")
# A pre-compiled target (IMPORTED) that is a dynamically linked library
# (SHARED, meaning .dll, .so or .dylib) or statically linked (.a or .lib).
if (USE_SHARED_LIB)
add_library(webgpu SHARED IMPORTED GLOBAL)
else()
add_library(webgpu STATIC IMPORTED GLOBAL)
endif()
# This is used to advertise the flavor of WebGPU that this zip provides
target_compile_definitions(webgpu INTERFACE WEBGPU_BACKEND_WGPU)
# This add webgpu.hpp
target_include_directories(webgpu INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include")
# TODO: There should be a wgpu-native-config.cmake file provided together with wgpu-native
build_lib_filename(BINARY_FILENAME "wgpu_native" ${USE_SHARED_LIB})
set(WEBGPU_RUNTIME_LIB "${ZIP_DIR}/lib/${BINARY_FILENAME}")
set_target_properties(
webgpu
PROPERTIES
IMPORTED_LOCATION "${WEBGPU_RUNTIME_LIB}"
)
target_include_directories(webgpu INTERFACE
"${ZIP_DIR}/include"
"${ZIP_DIR}/include/webgpu" # see https://github.com/gfx-rs/wgpu-native/pull/424
)
if (USE_SHARED_LIB)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
if (MSVC)
set(STATIC_LIB_EXT "lib")
set(STATIC_LIB_PREFIX "")
else()
set(STATIC_LIB_EXT "a")
set(STATIC_LIB_PREFIX "lib")
endif()
set(WGPU_IMPLIB "${ZIP_DIR}/lib/${STATIC_LIB_PREFIX}${BINARY_FILENAME}.${STATIC_LIB_EXT}")
set_target_properties(
webgpu
PROPERTIES
IMPORTED_IMPLIB "${WGPU_IMPLIB}"
)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set_target_properties(
webgpu
PROPERTIES
IMPORTED_NO_SONAME TRUE
)
endif()
message(STATUS "Using WebGPU runtime from '${WEBGPU_RUNTIME_LIB}'")
set(WEBGPU_RUNTIME_LIB ${WEBGPU_RUNTIME_LIB} CACHE INTERNAL "Path to the WebGPU library binary")
# The application's binary must find the .dll/.so/.dylib at runtime,
# so we automatically copy it (it's called WEBGPU_RUNTIME_LIB in general)
# next to the binary.
# Also make sure that the binary's RPATH is set to find this shared library.
function(target_copy_webgpu_binaries Target)
add_custom_command(
TARGET ${Target} POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_if_different
${WEBGPU_RUNTIME_LIB}
$<TARGET_FILE_DIR:${Target}>
COMMENT
"Copying '${WEBGPU_RUNTIME_LIB}' to '$<TARGET_FILE_DIR:${Target}>'..."
)
endfunction()
else (USE_SHARED_LIB)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_link_libraries(
webgpu
INTERFACE
d3dcompiler.lib
Ws2_32.lib
Userenv.lib
ntdll.lib
Bcrypt.lib
Opengl32.lib
Propsys.lib
RuntimeObject.lib
)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(
webgpu
INTERFACE
dl
pthread
m
)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
target_link_libraries(
webgpu
INTERFACE
"-framework Metal"
"-framework QuartzCore"
"-framework MetalKit"
)
endif()
function(target_copy_webgpu_binaries Target)
endfunction()
endif (USE_SHARED_LIB)