From 9dfb61b331b09552250cea7268fc632335816661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Stan=C4=9Bk?= Date: Thu, 2 Nov 2023 15:09:10 +0100 Subject: [PATCH] feat: allow customization of build environment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows for the WASM artifacts to be built elsewhere than only in the alpine-based node container. Signed-off-by: Jan Staněk --- build/wasm.js | 72 +++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 43 deletions(-) diff --git a/build/wasm.js b/build/wasm.js index fd90ac26..2b63f3c7 100644 --- a/build/wasm.js +++ b/build/wasm.js @@ -9,6 +9,18 @@ const WASM_SRC = resolve(__dirname, '../deps/llhttp') const WASM_OUT = resolve(__dirname, '../lib/llhttp') const DOCKERFILE = resolve(__dirname, './Dockerfile') +// These are defined by build environment +const WASM_CC = process.env.WASM_CC || 'clang' +let WASM_CFLAGS = process.env.WASM_CFLAGS || '--sysroot=/usr/share/wasi-sysroot -target wasm32-unknown-wasi' +let WASM_LDFLAGS = process.env.WASM_LDFLAGS || '' +const WASM_LDLIBS = process.env.WASM_LDLIBS || '' + +// These are relevant for undici and should not be overridden +WASM_CFLAGS += ' -Ofast -fno-exceptions -fvisibility=hidden -mexec-model=reactor' +WASM_LDFLAGS += ' -Wl,-error-limit=0 -Wl,-O3 -Wl,--lto-O3 -Wl,--strip-all' +WASM_LDFLAGS += ' -Wl,--allow-undefined -Wl,--export-dynamic -Wl,--export-table' +WASM_LDFLAGS += ' -Wl,--export=malloc -Wl,--export=free -Wl,--no-entry' + let platform = process.env.WASM_PLATFORM if (!platform && process.argv[2]) { platform = execSync('docker info -f "{{.OSType}}/{{.Architecture}}"').toString().trim() @@ -35,35 +47,25 @@ if (process.argv[2] === '--docker') { process.exit(0) } -// Gather information about the tools used for the build -const buildInfo = execSync('apk info -v').toString() -if (!buildInfo.includes('wasi-sdk')) { - console.log('Failed to generate build environment information') - process.exit(-1) +const hasApk = (function () { + try { execSync('command -v apk'); return true } catch (error) { return false } +})() +if (hasApk) { + // Gather information about the tools used for the build + const buildInfo = execSync('apk info -v').toString() + if (!buildInfo.includes('wasi-sdk')) { + console.log('Failed to generate build environment information') + process.exit(-1) + } + writeFileSync(join(WASM_OUT, 'wasm_build_env.txt'), buildInfo) } -writeFileSync(join(WASM_OUT, 'wasm_build_env.txt'), buildInfo) // Build wasm binary -execSync(`clang \ - --sysroot=/usr/share/wasi-sysroot \ - -target wasm32-unknown-wasi \ - -Ofast \ - -fno-exceptions \ - -fvisibility=hidden \ - -mexec-model=reactor \ - -Wl,-error-limit=0 \ - -Wl,-O3 \ - -Wl,--lto-O3 \ - -Wl,--strip-all \ - -Wl,--allow-undefined \ - -Wl,--export-dynamic \ - -Wl,--export-table \ - -Wl,--export=malloc \ - -Wl,--export=free \ - -Wl,--no-entry \ +execSync(`${WASM_CC} ${WASM_CFLAGS} ${WASM_LDFLAGS} \ ${join(WASM_SRC, 'src')}/*.c \ -I${join(WASM_SRC, 'include')} \ - -o ${join(WASM_OUT, 'llhttp.wasm')}`, { stdio: 'inherit' }) + -o ${join(WASM_OUT, 'llhttp.wasm')} \ + ${WASM_LDLIBS}`, { stdio: 'inherit' }) const base64Wasm = readFileSync(join(WASM_OUT, 'llhttp.wasm')).toString('base64') writeFileSync( @@ -72,27 +74,11 @@ writeFileSync( ) // Build wasm simd binary -execSync(`clang \ - --sysroot=/usr/share/wasi-sysroot \ - -target wasm32-unknown-wasi \ - -msimd128 \ - -Ofast \ - -fno-exceptions \ - -fvisibility=hidden \ - -mexec-model=reactor \ - -Wl,-error-limit=0 \ - -Wl,-O3 \ - -Wl,--lto-O3 \ - -Wl,--strip-all \ - -Wl,--allow-undefined \ - -Wl,--export-dynamic \ - -Wl,--export-table \ - -Wl,--export=malloc \ - -Wl,--export=free \ - -Wl,--no-entry \ +execSync(`${WASM_CC} ${WASM_CFLAGS} -msimd128 ${WASM_LDFLAGS} \ ${join(WASM_SRC, 'src')}/*.c \ -I${join(WASM_SRC, 'include')} \ - -o ${join(WASM_OUT, 'llhttp_simd.wasm')}`, { stdio: 'inherit' }) + -o ${join(WASM_OUT, 'llhttp_simd.wasm')} \ + ${WASM_LDLIBS}`, { stdio: 'inherit' }) const base64WasmSimd = readFileSync(join(WASM_OUT, 'llhttp_simd.wasm')).toString('base64') writeFileSync( -- 2.41.0