Skip to content

Commit

Permalink
feat: allow build:wasm to auto detect platform (nodejs#2102)
Browse files Browse the repository at this point in the history
  • Loading branch information
jazelly authored and metcoder95 committed Jul 21, 2023
1 parent 3bf1b4c commit 5739bbd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
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 [email protected] 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

0 comments on commit 5739bbd

Please sign in to comment.