Skip to content

Commit 4a289a6

Browse files
authored
Refactor build scripts (#177)
1 parent 8d77a40 commit 4a289a6

File tree

10 files changed

+107
-111
lines changed

10 files changed

+107
-111
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
!packages/webgpu/scripts/build
12
# Yarn
23
.package/.yarn/*
34
.yarn/*

packages/webgpu/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
!package/scripts/build
1+
!scripts/build
22
# OSX
33
#
44
.DS_Store

packages/webgpu/cpp/rnwgpu/api/GPU.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ class GPU : public m::HybridObject {
5858
std::shared_ptr<AsyncRunner> _async;
5959
};
6060

61-
} // namespace rnwgpu
61+
} // namespace rnwgpu

packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include "Convertors.h"
33
#include "RNWebGPUManager.h"
44

5+
#ifdef __APPLE__
6+
#include "dawn/native/MetalBackend.h"
7+
#endif
8+
59
namespace rnwgpu {
610

711
void GPUCanvasContext::configure(

packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.h

-16
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,6 @@
1717
#include "GPUTexture.h"
1818
#include "SurfaceRegistry.h"
1919

20-
#ifdef __APPLE__
21-
22-
namespace dawn {
23-
namespace native {
24-
namespace metal {
25-
26-
// See
27-
// https://source.chromium.org/chromium/chromium/src/+/main:third_party/dawn/include/dawn/native/MetalBackend.h;l=41
28-
void WaitForCommandsToBeScheduled(WGPUDevice device);
29-
30-
} // namespace metal
31-
} // namespace native
32-
} // namespace dawn
33-
34-
#endif
35-
3620
namespace rnwgpu {
3721

3822
namespace m = margelo;

packages/webgpu/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"tsc": "tsc --noEmit",
2727
"build": "bob build",
2828
"build-dawn": "ts-node scripts/build/dawn.ts",
29+
"copy-headers": "ts-node scripts/build/copy-headers.ts",
2930
"clean-dawn": "rimraf ./libs && rimraf ../../externals/dawn/out",
3031
"clang-format": "yarn clang-format-ios && yarn clang-format-android && yarn clang-format-common",
3132
"clang-format-ios": "find apple/ -iname \"*.h\" -o -iname \"*.mm\" -o -iname \"*.cpp\" | xargs clang-format -i",

packages/webgpu/scripts/build/copy-artifacts.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { $, checkBuildArtifacts } from "./util";
1+
import { checkBuildArtifacts } from "./dawn-configuration";
2+
import { $ } from "./util";
23

34
$("cp -R ../../artifacts/libs .");
45
$("cp -R ../../artifacts/cpp/webgpu cpp");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* eslint-disable max-len */
2+
import { $, checkFileExists, runAsync } from "./util";
3+
4+
export const libs = ["libwebgpu_dawn"] as const;
5+
6+
export const projectRoot = "packages/webgpu";
7+
8+
export const platforms = [
9+
"arm64",
10+
"x86_64",
11+
"x86",
12+
"armeabi-v7a",
13+
"arm64-v8a",
14+
"universal",
15+
] as const;
16+
17+
export type OS = "apple" | "android";
18+
export type Platform = (typeof platforms)[number];
19+
20+
export const copyHeaders = () => {
21+
console.log("📗 Copy headers");
22+
[
23+
`rm -rf ${projectRoot}/cpp/webgpu`,
24+
`rm -rf ${projectRoot}/cpp/dawn`,
25+
`cp -a externals/dawn/out/android_arm64-v8a/gen/include/webgpu ${projectRoot}/cpp`,
26+
`cp -a externals/dawn/out/android_arm64-v8a/gen/include/dawn ${projectRoot}/cpp`,
27+
`cp -a externals/dawn/include/webgpu/. ${projectRoot}/cpp/webgpu`,
28+
`cp -a externals/dawn/include/dawn/. ${projectRoot}/cpp/dawn`,
29+
`sed -i '' 's/#include "dawn\\/webgpu.h"/#include "webgpu\\/webgpu.h"/' ${projectRoot}/cpp/dawn/dawn_proc_table.h`,
30+
`cp ${projectRoot}/cpp/dawn/webgpu.h ${projectRoot}/cpp/webgpu/webgpu.h`,
31+
`cp ${projectRoot}/cpp/dawn/webgpu_cpp.h ${projectRoot}/cpp/webgpu/webgpu_cpp.h`,
32+
`rm -rf ${projectRoot}/cpp/dawn/webgpu.h`,
33+
`rm -rf ${projectRoot}/cpp/dawn/webgpu_cpp.h`,
34+
`rm -rf ${projectRoot}/cpp/dawn/wire`,
35+
`cp externals/dawn/src/dawn/dawn.json ${projectRoot}/libs`,
36+
].map((cmd) => $(cmd));
37+
};
38+
39+
const serializeCMakeArgs = (args: Record<string, string>) => {
40+
return Object.keys(args)
41+
.map((key) => `-D${key}=${args[key]}`)
42+
.join(" ");
43+
};
44+
45+
export const build = async (
46+
label: string,
47+
args: Record<string, string>,
48+
debugLabel: string,
49+
) => {
50+
console.log(`🔨 Building ${label}`);
51+
$(`mkdir -p externals/dawn/out/${label}`);
52+
process.chdir(`externals/dawn/out/${label}`);
53+
const cmd = `cmake ../.. -G Ninja ${serializeCMakeArgs(args)}`;
54+
await runAsync(cmd, debugLabel);
55+
await runAsync("ninja", debugLabel);
56+
process.chdir("../../../..");
57+
};
58+
59+
export const copyLib = (os: OS, platform: Platform, sdk?: string) => {
60+
const suffix = `${platform}${sdk ? `_${sdk}` : ""}`;
61+
const out = `${os}_${suffix}`;
62+
const dstPath = `${projectRoot}/libs/${os}/${suffix}/`;
63+
$(`mkdir -p ${dstPath}`);
64+
if (os === "android") {
65+
console.log("Strip debug symbols from libwebgpu_dawn.a...");
66+
$(
67+
`$ANDROID_NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/llvm-strip externals/dawn/out/${out}/src/dawn/native/libwebgpu_dawn.so`,
68+
);
69+
}
70+
[
71+
`externals/dawn/out/${out}/src/dawn/native/libwebgpu_dawn.${os === "apple" ? "a" : "so"}`,
72+
].forEach((lib) => {
73+
const libPath = lib;
74+
console.log(`Copying ${libPath} to ${dstPath}`);
75+
$(`cp ${libPath} ${dstPath}`);
76+
});
77+
};
78+
79+
export const checkBuildArtifacts = () => {
80+
console.log("Check build artifacts...");
81+
platforms
82+
.filter((arch) => arch !== "arm64" && arch !== "universal")
83+
.forEach((platform) => {
84+
libs.forEach((lib) => {
85+
checkFileExists(`libs/android/${platform}/${lib}.so`);
86+
});
87+
});
88+
libs.forEach((lib) => {
89+
checkFileExists(`libs/apple/${lib}.xcframework`);
90+
});
91+
checkFileExists("libs/dawn.json");
92+
};

packages/webgpu/scripts/build/dawn.ts

+5-20
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import { chdir } from "process";
66
import yargs from "yargs";
77
import { hideBin } from "yargs/helpers";
88

9-
import type { Platform } from "./util";
9+
import type { Platform } from "./dawn-configuration";
10+
import { $, mapKeys } from "./util";
1011
import {
11-
$,
1212
build,
1313
checkBuildArtifacts,
14+
copyHeaders,
1415
copyLib,
1516
libs,
16-
mapKeys,
1717
projectRoot,
18-
} from "./util";
18+
} from "./dawn-configuration";
1919

2020
const { argv } = yargs(hideBin(process.argv))
2121
.option("exclude", {
@@ -164,22 +164,7 @@ const apple = {
164164
);
165165
});
166166

167-
console.log("Copy headers");
168-
$(`rm -rf ${projectRoot}/cpp/webgpu`);
169-
$(`rm -rf ${projectRoot}/cpp/dawn`);
170-
$(
171-
`cp -R externals/dawn/out/android_arm64-v8a/gen/include/webgpu ${projectRoot}/cpp`,
172-
);
173-
$(
174-
`cp externals/dawn/out/android_arm64-v8a/gen/include/dawn/webgpu.h ${projectRoot}/cpp/webgpu/webgpu.h`,
175-
);
176-
$(
177-
`cp externals/dawn/out/android_arm64-v8a/gen/include/dawn/webgpu_cpp.h ${projectRoot}/cpp/webgpu/webgpu_cpp.h`,
178-
);
179-
$(
180-
`cp externals/dawn/include/webgpu/webgpu_enum_class_bitmasks.h ${projectRoot}/cpp/webgpu/`,
181-
);
182-
$(`cp externals/dawn/src/dawn/dawn.json ${projectRoot}/libs`);
167+
copyHeaders();
183168
chdir(projectRoot);
184169
checkBuildArtifacts();
185170
})();

packages/webgpu/scripts/build/util.ts

-72
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
1-
/* eslint-disable max-len */
21
import { spawn, execSync } from "child_process";
32
import { existsSync } from "fs";
43
import { exit } from "process";
54

6-
export const libs = ["libwebgpu_dawn"] as const;
7-
8-
export const projectRoot = "packages/webgpu";
9-
10-
export const platforms = [
11-
"arm64",
12-
"x86_64",
13-
"x86",
14-
"armeabi-v7a",
15-
"arm64-v8a",
16-
"universal",
17-
] as const;
18-
19-
export type OS = "apple" | "android";
20-
export type Platform = (typeof platforms)[number];
21-
225
export const runAsync = (command: string, label: string): Promise<void> => {
236
return new Promise((resolve, reject) => {
247
const [cmd, ...args] = command.split(" ");
@@ -72,58 +55,3 @@ export const $ = (command: string) => {
7255
exit(1);
7356
}
7457
};
75-
76-
const serializeCMakeArgs = (args: Record<string, string>) => {
77-
return Object.keys(args)
78-
.map((key) => `-D${key}=${args[key]}`)
79-
.join(" ");
80-
};
81-
82-
export const build = async (
83-
label: string,
84-
args: Record<string, string>,
85-
debugLabel: string,
86-
) => {
87-
console.log(`🔨 Building ${label}`);
88-
$(`mkdir -p externals/dawn/out/${label}`);
89-
process.chdir(`externals/dawn/out/${label}`);
90-
const cmd = `cmake ../.. -G Ninja ${serializeCMakeArgs(args)}`;
91-
await runAsync(cmd, debugLabel);
92-
await runAsync("ninja", debugLabel);
93-
process.chdir("../../../..");
94-
};
95-
96-
export const copyLib = (os: OS, platform: Platform, sdk?: string) => {
97-
const suffix = `${platform}${sdk ? `_${sdk}` : ""}`;
98-
const out = `${os}_${suffix}`;
99-
const dstPath = `${projectRoot}/libs/${os}/${suffix}/`;
100-
$(`mkdir -p ${dstPath}`);
101-
if (os === "android") {
102-
console.log("Strip debug symbols from libwebgpu_dawn.a...");
103-
$(
104-
`$ANDROID_NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/llvm-strip externals/dawn/out/${out}/src/dawn/native/libwebgpu_dawn.so`,
105-
);
106-
}
107-
[
108-
`externals/dawn/out/${out}/src/dawn/native/libwebgpu_dawn.${os === "apple" ? "a" : "so"}`,
109-
].forEach((lib) => {
110-
const libPath = lib;
111-
console.log(`Copying ${libPath} to ${dstPath}`);
112-
$(`cp ${libPath} ${dstPath}`);
113-
});
114-
};
115-
116-
export const checkBuildArtifacts = () => {
117-
console.log("Check build artifacts...");
118-
platforms
119-
.filter((arch) => arch !== "arm64" && arch !== "universal")
120-
.forEach((platform) => {
121-
libs.forEach((lib) => {
122-
checkFileExists(`libs/android/${platform}/${lib}.so`);
123-
});
124-
});
125-
libs.forEach((lib) => {
126-
checkFileExists(`libs/apple/${lib}.xcframework`);
127-
});
128-
checkFileExists("libs/dawn.json");
129-
};

0 commit comments

Comments
 (0)