Skip to content

Commit

Permalink
feat: Allow to select WASM platform when using Docker.
Browse files Browse the repository at this point in the history
Fixes #208.
  • Loading branch information
ShogunPanda committed Jan 10, 2023
1 parent 85dd446 commit 0382ff2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
13 changes: 2 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
32 changes: 21 additions & 11 deletions bin/build_wasm.ts
Original file line number Diff line number Diff line change
@@ -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'));
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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 \
Expand All @@ -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')} \
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 0382ff2

Please sign in to comment.