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 build:wasm to auto detect platform #2102

Merged
merged 2 commits into from
Apr 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
FROM node:16.13.0-buster
FROM node:18-alpine

ARG UID=1000
ARG GID=1000
ARG WASI_SDK_VERSION_MAJOR=14
ARG WASI_SDK_VERSION_MINOR=0

ENV WASI_ROOT=/home/node/wasi-sdk-${WASI_SDK_VERSION_MAJOR}.${WASI_SDK_VERSION_MINOR}

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 apk add -U clang lld wasi-sdk
RUN mkdir /home/node/undici

WORKDIR /home/node/undici
Expand Down
34 changes: 24 additions & 10 deletions build/wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,41 @@
const { execSync } = require('child_process')
const { writeFileSync, readFileSync } = require('fs')
const { join, resolve } = require('path')
const { WASI_ROOT } = process.env

const ROOT = resolve(__dirname, '../')
const WASM_SRC = resolve(__dirname, '../deps/llhttp')
const WASM_OUT = resolve(__dirname, '../lib/llhttp')
const DOCKERFILE = resolve(__dirname, './Dockerfile')

let platform = process.env.WASM_PLATFORM
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 -f ${DOCKERFILE} ${ROOT}`

console.log(`> ${cmd}\n\n`)
execSync(cmd, { stdio: 'inherit' })

process.exit(0)
}

if (process.argv[2] === '--docker') {
let cmd = 'docker run --rm -it'
let cmd = `docker run --rm -it --platform=${platform.toString().trim()}`
if (process.platform === 'linux') {
cmd += ` --user ${process.getuid()}:${process.getegid()}`
}

cmd += ` --mount type=bind,source=${ROOT}/lib/llhttp,target=/home/node/undici/lib/llhttp llhttp_wasm_builder node build/wasm.js`
console.log(`> ${cmd}\n\n`)
execSync(cmd, { stdio: 'inherit' })
process.exit(0)
}

if (!WASI_ROOT) {
throw new Error('Please setup the WASI_ROOT env variable.')
}

// 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 @@ -40,6 +52,7 @@ execSync(`${WASI_ROOT}/bin/clang \
-Wl,--export-table \
-Wl,--export=malloc \
-Wl,--export=free \
-Wl,--no-entry \
${join(WASM_SRC, 'src')}/*.c \
-I${join(WASM_SRC, 'include')} \
-o ${join(WASM_OUT, 'llhttp.wasm')}`, { stdio: 'inherit' })
Expand All @@ -51,8 +64,8 @@ writeFileSync(
)

// Build wasm simd binary
execSync(`${WASI_ROOT}/bin/clang \
--sysroot=${WASI_ROOT}/share/wasi-sysroot \
execSync(`clang \
--sysroot=/usr/share/wasi-sysroot \
-target wasm32-unknown-wasi \
-msimd128 \
-Ofast \
Expand All @@ -68,6 +81,7 @@ execSync(`${WASI_ROOT}/bin/clang \
-Wl,--export-table \
-Wl,--export=malloc \
-Wl,--export=free \
-Wl,--no-entry \
${join(WASM_SRC, 'src')}/*.c \
-I${join(WASM_SRC, 'include')} \
-o ${join(WASM_OUT, 'llhttp_simd.wasm')}`, { stdio: 'inherit' })
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
],
"scripts": {
"build:node": "npx esbuild@0.14.38 index-fetch.js --bundle --platform=node --outfile=undici-fetch.js",
"prebuild:wasm": "docker build -t llhttp_wasm_builder -f build/Dockerfile .",
"prebuild:wasm": "node build/wasm.js --prebuild",
"build:wasm": "node build/wasm.js --docker",
"lint": "standard | snazzy",
"lint:fix": "standard --fix | snazzy",
Expand Down