Debian Codegraph @

third_party/python/aiohttp/vendor/llhttp/bin/build_wasm.ts

raw
1 import { execSync } from 'child_process';
2 import { copyFileSync, mkdirSync } from 'fs';
3 import { join, resolve } from 'path';
4
5 let platform = process.env.WASM_PLATFORM ?? '';
6 const WASM_OUT = resolve(__dirname, '../build/wasm');
7 const WASM_SRC = resolve(__dirname, '../');
8
9 if (!platform && process.argv[2]) {
10 platform = execSync('docker info -f "{{.OSType}}/{{.Architecture}}"').toString().trim();
11 }
12
13 if (process.argv[2] === '--prebuild') {
14 const cmd = `docker build --platform=${platform.toString().trim()} -t llhttp_wasm_builder .`;
15
16 // eslint-disable-next-line no-console
17 console.log(`> ${cmd}\n\n`);
18 execSync(cmd, { stdio: 'inherit' });
19
20 process.exit(0);
21 }
22
23 if (process.argv[2] === '--setup') {
24 try {
25 mkdirSync(join(WASM_SRC, 'build'));
26 process.exit(0);
27 } catch (error: unknown) {
28 if (isErrorWithCode(error) && error.code !== 'EEXIST') {
29 throw error;
30 }
31 process.exit(0);
32 }
33 }
34
35 if (process.argv[2] === '--docker') {
36 let cmd = `docker run --rm -it --platform=${platform.toString().trim()}`;
37 // Try to avoid root permission problems on compiled assets
38 // when running on linux.
39 // It will work flawessly if uid === gid === 1000
40 // there will be some warnings otherwise.
41 if (process.platform === 'linux') {
42 cmd += ` --user ${process.getuid!()}:${process.getegid!()}`;
43 }
44 cmd += ` --mount type=bind,source=${WASM_SRC}/build,target=/home/node/llhttp/build llhttp_wasm_builder npm run wasm`;
45
46 // eslint-disable-next-line no-console
47 console.log(`> ${cmd}\n\n`);
48 execSync(cmd, { cwd: WASM_SRC, stdio: 'inherit' });
49 process.exit(0);
50 }
51
52 try {
53 mkdirSync(WASM_OUT);
54 } catch (error: unknown) {
55 if (isErrorWithCode(error) && error.code !== 'EEXIST') {
56 throw error;
57 }
58 }
59
60 // Build ts
61 execSync('npm run build', { cwd: WASM_SRC, stdio: 'inherit' });
62
63 // Build wasm binary
64 execSync(
65 `clang \
66 --sysroot=/usr/share/wasi-sysroot \
67 -target wasm32-unknown-wasi \
68 -Ofast \
69 -fno-exceptions \
70 -fvisibility=hidden \
71 -mexec-model=reactor \
72 -Wl,-error-limit=0 \
73 -Wl,-O3 \
74 -Wl,--lto-O3 \
75 -Wl,--strip-all \
76 -Wl,--allow-undefined \
77 -Wl,--export-dynamic \
78 -Wl,--export-table \
79 -Wl,--export=malloc \
80 -Wl,--export=free \
81 -Wl,--no-entry \
82 ${join(WASM_SRC, 'build', 'c')}/*.c \
83 ${join(WASM_SRC, 'src', 'native')}/*.c \
84 -I${join(WASM_SRC, 'build')} \
85 -o ${join(WASM_OUT, 'llhttp.wasm')}`,
86 { stdio: 'inherit' },
87 );
88
89 // Copy constants for `.js` and `.ts` users.
90 copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.js'), join(WASM_OUT, 'constants.js'));
91 copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.js.map'), join(WASM_OUT, 'constants.js.map'));
92 copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.d.ts'), join(WASM_OUT, 'constants.d.ts'));
93 copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.js'), join(WASM_OUT, 'utils.js'));
94 copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.js.map'), join(WASM_OUT, 'utils.js.map'));
95 copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.d.ts'), join(WASM_OUT, 'utils.d.ts'));
96
97 function isErrorWithCode(error: unknown): error is Error & { code: string } {
98 return typeof error === 'object' && error !== null && 'code' in error;
99 }