Skip to content

Commit 5b61b9a

Browse files
khardixcrysmags
authored andcommitted
feat: allow customization of build environment (nodejs#2403)
This allows for the WASM artifacts to be built elsewhere than only in the alpine-based node container.
1 parent ec1e958 commit 5b61b9a

File tree

1 file changed

+29
-43
lines changed

1 file changed

+29
-43
lines changed

build/wasm.js

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ const WASM_SRC = resolve(__dirname, '../deps/llhttp')
99
const WASM_OUT = resolve(__dirname, '../lib/llhttp')
1010
const DOCKERFILE = resolve(__dirname, './Dockerfile')
1111

12+
// These are defined by build environment
13+
const WASM_CC = process.env.WASM_CC || 'clang'
14+
let WASM_CFLAGS = process.env.WASM_CFLAGS || '--sysroot=/usr/share/wasi-sysroot -target wasm32-unknown-wasi'
15+
let WASM_LDFLAGS = process.env.WASM_LDFLAGS || ''
16+
const WASM_LDLIBS = process.env.WASM_LDLIBS || ''
17+
18+
// These are relevant for undici and should not be overridden
19+
WASM_CFLAGS += ' -Ofast -fno-exceptions -fvisibility=hidden -mexec-model=reactor'
20+
WASM_LDFLAGS += ' -Wl,-error-limit=0 -Wl,-O3 -Wl,--lto-O3 -Wl,--strip-all'
21+
WASM_LDFLAGS += ' -Wl,--allow-undefined -Wl,--export-dynamic -Wl,--export-table'
22+
WASM_LDFLAGS += ' -Wl,--export=malloc -Wl,--export=free -Wl,--no-entry'
23+
1224
let platform = process.env.WASM_PLATFORM
1325
if (!platform && process.argv[2]) {
1426
platform = execSync('docker info -f "{{.OSType}}/{{.Architecture}}"').toString().trim()
@@ -35,35 +47,25 @@ if (process.argv[2] === '--docker') {
3547
process.exit(0)
3648
}
3749

38-
// Gather information about the tools used for the build
39-
const buildInfo = execSync('apk info -v').toString()
40-
if (!buildInfo.includes('wasi-sdk')) {
41-
console.log('Failed to generate build environment information')
42-
process.exit(-1)
50+
const hasApk = (function () {
51+
try { execSync('command -v apk'); return true } catch (error) { return false }
52+
})()
53+
if (hasApk) {
54+
// Gather information about the tools used for the build
55+
const buildInfo = execSync('apk info -v').toString()
56+
if (!buildInfo.includes('wasi-sdk')) {
57+
console.log('Failed to generate build environment information')
58+
process.exit(-1)
59+
}
60+
writeFileSync(join(WASM_OUT, 'wasm_build_env.txt'), buildInfo)
4361
}
44-
writeFileSync(join(WASM_OUT, 'wasm_build_env.txt'), buildInfo)
4562

4663
// Build wasm binary
47-
execSync(`clang \
48-
--sysroot=/usr/share/wasi-sysroot \
49-
-target wasm32-unknown-wasi \
50-
-Ofast \
51-
-fno-exceptions \
52-
-fvisibility=hidden \
53-
-mexec-model=reactor \
54-
-Wl,-error-limit=0 \
55-
-Wl,-O3 \
56-
-Wl,--lto-O3 \
57-
-Wl,--strip-all \
58-
-Wl,--allow-undefined \
59-
-Wl,--export-dynamic \
60-
-Wl,--export-table \
61-
-Wl,--export=malloc \
62-
-Wl,--export=free \
63-
-Wl,--no-entry \
64+
execSync(`${WASM_CC} ${WASM_CFLAGS} ${WASM_LDFLAGS} \
6465
${join(WASM_SRC, 'src')}/*.c \
6566
-I${join(WASM_SRC, 'include')} \
66-
-o ${join(WASM_OUT, 'llhttp.wasm')}`, { stdio: 'inherit' })
67+
-o ${join(WASM_OUT, 'llhttp.wasm')} \
68+
${WASM_LDLIBS}`, { stdio: 'inherit' })
6769

6870
const base64Wasm = readFileSync(join(WASM_OUT, 'llhttp.wasm')).toString('base64')
6971
writeFileSync(
@@ -72,27 +74,11 @@ writeFileSync(
7274
)
7375

7476
// Build wasm simd binary
75-
execSync(`clang \
76-
--sysroot=/usr/share/wasi-sysroot \
77-
-target wasm32-unknown-wasi \
78-
-msimd128 \
79-
-Ofast \
80-
-fno-exceptions \
81-
-fvisibility=hidden \
82-
-mexec-model=reactor \
83-
-Wl,-error-limit=0 \
84-
-Wl,-O3 \
85-
-Wl,--lto-O3 \
86-
-Wl,--strip-all \
87-
-Wl,--allow-undefined \
88-
-Wl,--export-dynamic \
89-
-Wl,--export-table \
90-
-Wl,--export=malloc \
91-
-Wl,--export=free \
92-
-Wl,--no-entry \
77+
execSync(`${WASM_CC} ${WASM_CFLAGS} -msimd128 ${WASM_LDFLAGS} \
9378
${join(WASM_SRC, 'src')}/*.c \
9479
-I${join(WASM_SRC, 'include')} \
95-
-o ${join(WASM_OUT, 'llhttp_simd.wasm')}`, { stdio: 'inherit' })
80+
-o ${join(WASM_OUT, 'llhttp_simd.wasm')} \
81+
${WASM_LDLIBS}`, { stdio: 'inherit' })
9682

9783
const base64WasmSimd = readFileSync(join(WASM_OUT, 'llhttp_simd.wasm')).toString('base64')
9884
writeFileSync(

0 commit comments

Comments
 (0)