Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow to select WASM platform when using Docker. #215

Merged
merged 2 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:18-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
39 changes: 27 additions & 12 deletions bin/build_wasm.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
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 && process.argv[2]) {
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 .`;

/* tslint:disable-next-line no-console */
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 +33,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 +42,13 @@ 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`;

/* tslint:disable-next-line no-console */
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 +61,9 @@ 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,10 +78,13 @@ 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')} \
-o ${join(WASM_OUT, 'llhttp.wasm')}`, { stdio: 'inherit' });
-o ${join(WASM_OUT, 'llhttp.wasm')}`,
{ stdio: 'inherit' },
);

// Copy constants for `.js` and `.ts` users.
copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.js'), join(WASM_OUT, 'constants.js'));
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