|
| 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 | +}; |
0 commit comments