Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/tools/ports/emdawnwebgpu.py
6170 views
1
# Copyright 2025 The Emscripten Authors. All rights reserved.
2
# Emscripten is available under two separate licenses, the MIT license and the
3
# University of Illinois/NCSA Open Source License. Both these licenses can be
4
# found in the LICENSE file.
5
6
# https://dawn.googlesource.com/dawn/+/01940842b667a7812d0e4ca0ef4367fbec294241/src/emdawnwebgpu/pkg/README.md
7
r"""
8
This "remote port" instructs Emscripten (4.0.10+) how to automatically download
9
the actual port for Emdawnwebgpu. See README below for instructions.
10
11
# Emdawnwebgpu
12
13
Emdawnwebgpu is Dawn's implementation of webgpu.h for Emscripten (on top of the
14
WebGPU JS API). It is a fork of Emscripten's original `USE_WEBGPU` bindings,
15
and while it is maintained in Dawn, it works in any browser supporting WebGPU
16
(modulo individual feature support).
17
18
Emdawnwebgpu provides everything necessary to use `<webgpu/webgpu.h>` and the
19
Dawn-style `<webgpu/webgpu_cpp.h>` with Emscripten.
20
21
<!-- TODO(crbug.com/430616385): Link to a sample project. -->
22
23
If you find an issue in this release, please download the latest Emdawnwebgpu
24
release (see below) and latest Emscripten and verify the bug, then report it at
25
<https://crbug.com/new?component=1570785&noWizard=True>.
26
27
## API Stability
28
29
Core parts of `webgpu.h` (defined in
30
<https://github.com/webgpu-native/webgpu-headers>) are considered stable APIs
31
and should not change, except for bugfixes (though guarantees are not made).
32
Dawn/Emscripten-specific parts, and all of `webgpu_cpp.h`, are **NOT**
33
considered stable, and may change.
34
35
## How to use Emdawnwebgpu
36
37
Emdawnwebgpu is distributed in several ways. Choose the one that works for you.
38
39
In all cases, it is important to enable Closure to reduce code size in release
40
builds. Pass the following flag to `emcc` during linking:
41
42
--closure=1
43
44
### Targeting Web only
45
46
#### Easiest: "Remote" port built into Emscripten
47
48
Recent releases of Emscripten vendor a copy of a "remote" port which
49
automatically downloads a pinned version of Emdawnwebgpu and configures it.
50
51
Pass the following flag to `emcc` during both compilation and linking:
52
53
--use-port=emdawnwebgpu
54
55
#### Latest: "Remote" port from Dawn release
56
57
This is the same as the built-in port, but you can download a newer version if
58
you need recent bugfixes or features in Emdawnwebgpu that haven't been rolled
59
into Emscripten yet. **Requires Emscripten 4.0.10+.**
60
61
Download and extract the `emdawnwebgpu-*.remoteport.py` file from
62
<https://github.com/google/dawn/releases>.
63
64
Pass the following flag to `emcc` during both compilation and linking:
65
66
--use-port=path/to/emdawnwebgpu_remoteport_file.py
67
68
#### Latest, without automatic downloading: "Local" port from Dawn release
69
70
Use this method if your build system requires sources to be local (e.g. checked
71
into your repository) instead of automatically downloaded, or if you use
72
Emscripten before 4.0.10.
73
**Note that Emdawnwebgpu may not work with older Emscripten releases.**
74
75
Download and extract the `emdawnwebgpu_pkg-*.zip` package from
76
<https://github.com/google/dawn/releases>.
77
(Note the package is text-only and does not contain any binaries, but see below
78
if you need to build the package from the original source.)
79
80
Pass the following flag to `emcc` during both compilation and linking:
81
82
--use-port=path/to/emdawnwebgpu_pkg/emdawnwebgpu.port.py
83
84
If (and only if) using Emscripten before 4.0.7, pass this flag during linking:
85
86
--closure-args=--externs=path/to/emdawnwebgpu_pkg/webgpu/src/webgpu-externs.js
87
88
#### Without using a port file (**Unsupported!**)
89
90
It is possible to integrate the Emdawnwebgpu sources directly into your build
91
process, which may be necessary for certain build systems, but this is not
92
officially supported. Using a port file instead is strongly recommended.
93
94
If you do this, the port files or Dawn's GN or CMake files can serve as a
95
reference for the steps needed. Note that in all cases, the sources include both
96
C++ and JS code. While it is possible to precompile the C++ code to `.a`, the JS
97
code cannot be precompiled and must be provided at the final link step.
98
99
### Cross-targeting Web/Native
100
101
#### Using CMake
102
103
Use this method if your project uses CMake and targets both Emscripten and
104
native platforms.
105
106
<https://developer.chrome.com/docs/web-platform/webgpu/build-app>
107
108
#### Building the package locally to use with a non-CMake project
109
110
If your project already has Dawn source, or you otherwise want to cross-target
111
Web and native with your non-CMake project, you can use CMake to build
112
`emdawnwebgpu_pkg` locally (similar to how you would build binary libraries
113
to link with in native), then use the "Local" port instructions above.
114
115
<https://dawn.googlesource.com/dawn/+/refs/heads/main/src/emdawnwebgpu/README.md>
116
117
## Port options
118
119
Options can be set by appending `:key1=value:key2=value` to `--use-port`.
120
For information about port options, run:
121
122
emcc --use-port=emdawnwebgpu:help
123
emcc --use-port=path/to/emdawnwebgpu_remoteport_file.py:help
124
emcc --use-port=path/to/emdawnwebgpu_pkg/emdawnwebgpu.port.py:help
125
126
### C++ bindings
127
128
By default, C++ bindings are provided in the include path. Note that unlike
129
`webgpu.h`, these are not intended to be fully stable. If you don't want these
130
for any reason (you have custom bindings, you're using a pinned snapshot of
131
`webgpu_cpp.h`, etc.), you can set the option `cpp_bindings=false`:
132
133
--use-port=emdawnwebgpu:cpp_bindings=false
134
--use-port=path/to/emdawnwebgpu_remoteport_file.py:cpp_bindings=false
135
--use-port=path/to/emdawnwebgpu_pkg/emdawnwebgpu.port.py:cpp_bindings=false
136
137
## Embuilder
138
139
If your build process needs a separate step to build the port before linking,
140
use Emscripten's `embuilder`.
141
142
Under `embuilder`, some options cannot be set automatically, so they must be
143
set manually. For details, see `OPTIONS` in `emdawnwebgpu.port.py` (in the
144
package zip).
145
"""
146
147
import sys
148
149
if __name__ == '__main__':
150
print('Please see documentation inside this file for details on how to use this port.')
151
sys.exit(1)
152
153
_VERSION = 'v20251002.162335'
154
155
# Remote-specific port information
156
157
# - Where to download the port
158
EXTERNAL_PORT = f'https://github.com/google/dawn/releases/download/{_VERSION}/emdawnwebgpu_pkg-{_VERSION}.zip'
159
# - Hash to verify the download integrity
160
SHA512 = 'ed15672c2c495a77c764929e6979f4e155bf8b9c46dee5b0f234f3208a708bc2b846d89eef345b725d03454b56d549531f48fc84ff2afe7627d14115893b0fb0'
161
# - Path of the port inside the zip file
162
PORT_FILE = 'emdawnwebgpu_pkg/emdawnwebgpu.port.py'
163
164
# General port information
165
166
# - Visible in emcc --show-ports and emcc --use-port=emdawnwebgpu:help
167
LICENSE = "Some files: BSD 3-Clause License. Other files: Emscripten's license (available under both MIT License and University of Illinois/NCSA Open Source License)"
168
169
# - Visible in emcc --use-port=emdawnwebgpu:help
170
DESCRIPTION = "Emdawnwebgpu implements webgpu.h on WebGPU, replacing -sUSE_WEBGPU. **For info on usage and filing feedback, see link below.**"
171
URL = 'https://dawn.googlesource.com/dawn/+/01940842b667a7812d0e4ca0ef4367fbec294241/src/emdawnwebgpu/pkg/README.md'
172
173
174
# Emscripten <4.0.10 won't notice EXTERNAL_PORT and will try to use this.
175
def get(ports, settings, shared):
176
raise Exception('Remote ports require Emscripten 4.0.10+.')
177
178
179
# (Make this look like a port so that the error message above can be hit.)
180
def clear(ports, settings, shared):
181
pass
182
183