diff --git a/Dockerfile b/Dockerfile index 62ff3372..64dffa7a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,8 @@ -FROM node:14.16.0-buster - +FROM node:16-alpine ARG UID=1000 ARG GID=1000 -ARG WASI_SDK_VERSION_MAJOR=12 -ARG WASI_SDK_VERSION_MINOR=0 - -ENV WASI_ROOT=/home/node/wasi-sdk-12.0 - -RUN wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VERSION_MAJOR}/wasi-sdk-${WASI_SDK_VERSION_MAJOR}.${WASI_SDK_VERSION_MINOR}-linux.tar.gz -P /tmp - -RUN tar xvf /tmp/wasi-sdk-${WASI_SDK_VERSION_MAJOR}.${WASI_SDK_VERSION_MINOR}-linux.tar.gz --directory /home/node -RUN mkdir /home/node/llhttp +RUN apk add -U clang lld wasi-sdk && mkdir /home/node/llhttp WORKDIR /home/node/llhttp diff --git a/bin/build_wasm.ts b/bin/build_wasm.ts index f80fef77..77c26edc 100644 --- a/bin/build_wasm.ts +++ b/bin/build_wasm.ts @@ -1,13 +1,24 @@ import { execSync } from 'child_process'; -import { copyFileSync, mkdirSync, writeFileSync } from 'fs'; -import { stringify } from 'javascript-stringify'; +import { copyFileSync, mkdirSync } from 'fs'; import { join, resolve } from 'path'; -import { constants } from '..'; -const { WASI_ROOT } = process.env; +let platform = process.env.WASM_PLATFORM const WASM_OUT = resolve(__dirname, '../build/wasm'); const WASM_SRC = resolve(__dirname, '../'); +if (!platform) { + platform = execSync('docker info -f "{{.OSType}}/{{.Architecture}}"').toString().trim(); +} + +if (process.argv[2] === '--prebuild') { + const cmd = `docker build --platform=${platform.toString().trim()} -t llhttp_wasm_builder .`; + + console.log(`> ${cmd}\n\n`); + execSync(cmd, { stdio: 'inherit' }); + + process.exit(0); +} + if (process.argv[2] === '--setup') { try { mkdirSync(join(WASM_SRC, 'build')); @@ -21,7 +32,7 @@ if (process.argv[2] === '--setup') { } if (process.argv[2] === '--docker') { - let cmd = 'docker run --rm -it'; + let cmd = `docker run --rm -it --platform=${platform.toString().trim()}`; // Try to avoid root permission problems on compiled assets // when running on linux. // It will work flawessly if uid === gid === 1000 @@ -30,14 +41,12 @@ if (process.argv[2] === '--docker') { cmd += ` --user ${process.getuid()}:${process.getegid()}`; } cmd += ` --mount type=bind,source=${WASM_SRC}/build,target=/home/node/llhttp/build llhttp_wasm_builder npm run wasm`; + + console.log(`> ${cmd}\n\n`); execSync(cmd, { cwd: WASM_SRC, stdio: 'inherit' }); process.exit(0); } -if (!WASI_ROOT) { - throw new Error('Please setup the WASI_ROOT env variable.'); -} - try { mkdirSync(WASM_OUT); } catch (error) { @@ -50,8 +59,8 @@ try { execSync('npm run build', { cwd: WASM_SRC, stdio: 'inherit' }); // Build wasm binary -execSync(`${WASI_ROOT}/bin/clang \ - --sysroot=${WASI_ROOT}/share/wasi-sysroot \ +execSync(`clang \ + --sysroot=/usr/share/wasi-sysroot \ -target wasm32-unknown-wasi \ -Ofast \ -fno-exceptions \ @@ -66,6 +75,7 @@ execSync(`${WASI_ROOT}/bin/clang \ -Wl,--export-table \ -Wl,--export=malloc \ -Wl,--export=free \ + -Wl,--no-entry \ ${join(WASM_SRC, 'build', 'c')}/*.c \ ${join(WASM_SRC, 'src', 'native')}/*.c \ -I${join(WASM_SRC, 'build')} \ diff --git a/package.json b/package.json index 5273144e..85704f48 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "bench": "ts-node bench/", "build": "ts-node bin/generate.ts", "build-ts": "tsc", - "prebuild-wasm": "docker build -t llhttp_wasm_builder . && npm run wasm -- --setup", + "prebuild-wasm": "npm run wasm -- --prebuild && npm run wasm -- --setup", "build-wasm": "npm run wasm -- --docker", "wasm": "ts-node bin/build_wasm.ts", "clean": "rm -rf lib && rm -rf test/tmp",