Skip to content

Commit 5b80d23

Browse files
committed
initial commit
0 parents  commit 5b80d23

File tree

16 files changed

+1738
-0
lines changed

16 files changed

+1738
-0
lines changed

.github/workflows/build.yml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
name: Build cross platform
2+
3+
permissions:
4+
contents: write
5+
6+
# Controls when the action will run. Triggers the workflow on push
7+
on:
8+
push:
9+
pull_request:
10+
release:
11+
# tags:
12+
# - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
13+
14+
jobs:
15+
build:
16+
name: ${{ matrix.config.name }}
17+
runs-on: ${{ matrix.config.os }}
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
config:
22+
- {
23+
name: "Windows Latest MSVC",
24+
os: windows-latest,
25+
artifact: win32-x64,
26+
}
27+
- {
28+
name: "Ubuntu_Latest_GCC",
29+
os: ubuntu-latest,
30+
artifact: linux-x64,
31+
}
32+
- {
33+
name: "macOS Latest Clang",
34+
os: macos-latest,
35+
artifact: darwin-arm64,
36+
}
37+
38+
steps:
39+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
40+
- name: Checkout
41+
uses: actions/checkout@v4
42+
with:
43+
persist-credentials: false
44+
45+
- name: Use Node.js
46+
uses: actions/setup-node@v4
47+
with:
48+
node-version: '20.x'
49+
50+
- name: Print env
51+
run: |
52+
echo github.event.action: ${{ github.event.action }}
53+
echo github.event_name: ${{ github.event_name }}
54+
55+
- name: Install dependencies on windows
56+
if: startsWith(matrix.config.os, 'windows')
57+
run: |
58+
choco install node cmake
59+
node --version
60+
cmake --version
61+
"C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Auxiliary/Build/vcvars64.bat"
62+
63+
- name: Add msbuild to PATH
64+
if: startsWith(matrix.config.os, 'windows')
65+
uses: microsoft/setup-msbuild@v2
66+
67+
- name: Install dependencies on ubuntu
68+
if: startsWith(matrix.config.name, 'Ubuntu_Latest_GCC')
69+
run: |
70+
sudo apt-get update
71+
sudo apt-get install cmake
72+
cmake --version
73+
gcc --version
74+
75+
- name: Install dependencies on macos
76+
if: startsWith(matrix.config.os, 'macos')
77+
run: |
78+
brew install cmake
79+
cmake --version
80+
81+
- name: Build
82+
shell: bash
83+
run: |
84+
npm ci
85+
npm run build
86+
87+
- name: Upload Artifact ⬆️
88+
uses: actions/upload-artifact@v4
89+
with:
90+
path: ./dist/*.dawn.node
91+
name: ${{ matrix.config.artifact }}.dawn.node
92+
overwrite: true
93+
94+
- name: Release
95+
uses: softprops/action-gh-release@v2
96+
if: startsWith(github.ref, 'refs/tags/')
97+
with:
98+
files: dist/*.dawn.node
99+

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.pyc
2+
.DS_Store
3+
node_modules
4+
dist

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "dawn"]
2+
path = third_party/dawn
3+
url = https://dawn.googlesource.com/dawn.git
4+
[submodule "depot_tools"]
5+
path = third_party/depot_tools
6+
url = https://chromium.googlesource.com/chromium/tools/depot_tools.git

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# webgpu - dawn.node bundler.
2+
3+
This is a script to bundle/package dawn.node and hopefully publish it on npm
4+
5+
[Dawn](https://dawn.googlesource.com/dawn) is an implementation of [WebGPU](https://gpuweb.github.io/gpuweb/).
6+
It includes a node plugin and this repo builds that plugin.
7+
8+
# Updating
9+
10+
This updates to the latest dawn and depot_tools
11+
12+
```sh
13+
npm ci
14+
npm run update
15+
```
16+
17+
# Publishing
18+
19+
To publish
20+
21+
1. Bump the package version and tag.
22+
23+
The easiest way is `npm version patch`
24+
25+
2. Push the patch
26+
27+
```sh
28+
git push --tag origin main
29+
```
30+
31+
3. Wait for github actions to successfully build all the versions
32+
33+
4. Run `npm run publish`
34+
35+
This will download the files from the latest release to the `dist` folder
36+
and then publish them.
37+
38+
```sh
39+
npm run login <your-publisher-id>
40+
```
41+
42+
Then run `npm run publish`
43+
44+
# Building on all supported platforms
45+
46+
Push a new version. Check the github actions. You should see build artifacts
47+
added to the bottom of the latest action run.
48+
49+
# Building
50+
51+
This builds for the local OS (win64,macOS-intel,macOS-arm,linux)
52+
53+
```sh
54+
npm ci
55+
npm run build
56+
```
57+
58+
## Prerequisites
59+
60+
### Windows
61+
62+
Before running the build script above you must have
63+
Visual Studio C++ installed and have run the `vcvars64.bat` file.
64+
I've tested with Visual Studio Community Edition 2022
65+
66+
Further you must have [cmake installed](https://cmake.org/download/)
67+
and either in your path or at it's standard place of `C:\Program Files\CMake`
68+
69+
And you must have `node.js` installed, at least version 18.
70+
I recommend using [nvm-windows](https://github.com/coreybutler/nvm-windows) to install it
71+
as it makes it easy to switch version
72+
73+
### MacOS
74+
75+
Before running the build script above you must have
76+
XCode installed and its command line tools
77+
78+
Further you must have [cmake installed](https://cmake.org/download/)
79+
and either in your path or at it's standard place of `/Applications/CMake.app`
80+
81+
And you must have `node.js` installed, at least version 18.
82+
I recommend using [nvm](https://github.com/nvm-sh/nvm) to install it
83+
as it makes it easy to switch versions.
84+
85+
### Linux (Ubuntu)
86+
87+
Before running the build script above you need to install
88+
the following dependencies
89+
90+
```sh
91+
sudo apt-get install cmake
92+
```
93+
94+
And you must have `node.js` installed, at least version 18.
95+
I recommend using [nvm](https://github.com/nvm-sh/nvm) to install it
96+
as it makes it easy to switch versions.

build/build.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
4+
import {execute} from './execute.js';
5+
import {addElemIf, appendPathIfItExists, prependPathIfItExists} from './utils.js';
6+
7+
//const __dirname = dirname(fileURLToPath(import.meta.url));
8+
const cwd = process.cwd();
9+
const depotToolsPath = path.join(cwd, 'third_party', 'depot_tools');
10+
const dawnPath = `${cwd}/third_party/dawn`;
11+
const buildPath = `${dawnPath}/out/cmake-release`
12+
13+
const isMac = process.platform === 'darwin';
14+
const isWin = process.platform === 'win32';
15+
16+
prependPathIfItExists(depotToolsPath);
17+
appendPathIfItExists('/Applications/CMake.app/Contents/bin');
18+
appendPathIfItExists('C:\\Program Files\\CMake\\bin');
19+
20+
function fixupPackageJson(filename) {
21+
const pkg = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf8'}));
22+
const vsPkg = JSON.parse(fs.readFileSync(filename, {encoding: 'utf8'}));
23+
const newPkg = {
24+
...pkg,
25+
...vsPkg,
26+
type: "commonjs",
27+
scripts: {},
28+
version: pkg.version,
29+
};
30+
fs.writeFileSync(filename, JSON.stringify(newPkg, null, 2));
31+
}
32+
33+
async function buildDawnNode() {
34+
try {
35+
process.env.DEPOT_TOOLS_WIN_TOOLCHAIN = '0'
36+
process.chdir('third_party/dawn');
37+
fs.copyFileSync('scripts/standalone-with-node.gclient', '.gclient');
38+
await execute('gclient', ['metrics', '--opt-out']);
39+
await execute('gclient', ['sync']);
40+
fs.mkdirSync('out/cmake-release', {recursive: true});
41+
process.chdir('out/cmake-release');
42+
43+
await execute('cmake', [
44+
dawnPath,
45+
...addElemIf(!isWin, '-GNinja'),
46+
'-DDAWN_BUILD_NODE_BINDINGS=1',
47+
...addElemIf(isMac, '-DCMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'),
48+
]);
49+
if (isWin) {
50+
await execute('cmake', ['--build', '.', '-target', 'dawn.node'])
51+
} else {
52+
await execute('ninja', ['dawn.node']);
53+
}
54+
} finally {
55+
process.chdir(cwd);
56+
}
57+
}
58+
59+
async function packageExtension(target) {
60+
try {
61+
process.chdir(buildPath);
62+
await execute('npm', ['install']);
63+
await execute(`${cwd}/node_modules/.bin/vsce`, [
64+
'package',
65+
'--allow-star-activation',
66+
'--target', target,
67+
]);
68+
} finally {
69+
process.chdir(cwd);
70+
}
71+
}
72+
73+
async function copyResult(filepath, target) {
74+
const srcFilename = path.join(filepath, 'dawn.node');
75+
const dstFilename = path.join('dist', `${target}.dawn.node`);
76+
fs.mkdirSync(path.dirname(dstFilename), {recursive: true});
77+
fs.copyFileSync(srcFilename, dstFilename);
78+
return dstFilename;
79+
}
80+
81+
async function main() {
82+
try {
83+
const target = `${process.platform}-${process.arch}`;
84+
console.log('building for:', target);
85+
await execute('git', ['submodule', 'update', '--init']);
86+
await buildDawnNode();
87+
//fixupPackageJson(`${buildPath}/package.json`);
88+
//fs.copyFileSync('third_party/dawn/LICENSE', `${buildPath}/LICENSE`);
89+
//await packageExtension(target);
90+
const packageName = await copyResult(buildPath, target);
91+
console.log('created:', packageName);
92+
} catch (e) {
93+
console.error(e);
94+
console.error(e.stack);
95+
process.exit(1);
96+
}
97+
}
98+
99+
main();

build/execute.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {spawn} from 'child_process';
2+
3+
export function execute(cmd, args, options) {
4+
return new Promise((resolve, reject) => {
5+
const proc = spawn(cmd, args, {...options || {}, shell: true, stdio: 'inherit'});
6+
proc.on('close', function(code) {
7+
const result = {exitCode: code};
8+
if (parseInt(code) !== 0) {
9+
reject(result);
10+
} else {
11+
resolve(null, result);
12+
}
13+
});
14+
});
15+
}

build/github.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export async function getLatestRelease({owner, repo}) {
2+
const res = await fetch(`https://api.github.com/repos/${owner}/${repo}/releases/latest`, {
3+
headers: {
4+
'Accept': 'application/vnd.github+json',
5+
'X-GitHub-Api-Version': '2022-11-28',
6+
}
7+
});
8+
return await res.json();
9+
}

build/publish.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
4+
import {execute} from './execute.js';
5+
import * as github from './github.js';
6+
7+
const owner = 'greggman';
8+
const repo = 'node-webgpu';
9+
10+
function executeL(cmd, args) {
11+
console.log(cmd, args.join(' '));
12+
execute(cmd, args);
13+
}
14+
15+
async function downloadFile(name, url, filepath) {
16+
const res = await fetch(url);
17+
const data = await res.arrayBuffer();
18+
const filename = path.join(filepath, name);
19+
console.log('download:', filename);
20+
fs.mkdirSync(filepath, {recursive: true});
21+
fs.writeFileSync(filename, new Uint8Array(data));
22+
return filename;
23+
}
24+
25+
async function main() {
26+
const data = await github.getLatestRelease({
27+
owner,
28+
repo,
29+
});
30+
//const vsixFilenames = await Promise.all(
31+
// data.assets
32+
// .filter(({name}) => name?.endsWith('.vsix'))
33+
// .map(({name, browser_download_url}) => downloadFile(name, browser_download_url, 'dist'))
34+
//);
35+
//executeL('./node_modules/.bin/vsce', ['publish', '--packagePath', ...vsixFilenames]);
36+
}
37+
38+
main();

build/update.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
4+
import {execute} from './execute.js';
5+
6+
const cwd = process.cwd();
7+
8+
async function main() {
9+
try {
10+
await execute('git', ['submodule', 'update', '--init']);
11+
process.chdir('third_party/dawn');
12+
await execute('git', ['pull', 'origin', 'main']);
13+
process.chdir(cwd);
14+
process.chdir('third_party/depot_tools');
15+
await execute('git', ['pull', 'origin', 'main']);
16+
} catch (e) {
17+
console.error(e);
18+
console.error(e.stack);
19+
process.exit(1);
20+
}
21+
}
22+
23+
main();

0 commit comments

Comments
 (0)