@@ -9,6 +9,18 @@ const WASM_SRC = resolve(__dirname, '../deps/llhttp')
9
9
const WASM_OUT = resolve ( __dirname , '../lib/llhttp' )
10
10
const DOCKERFILE = resolve ( __dirname , './Dockerfile' )
11
11
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
+
12
24
let platform = process . env . WASM_PLATFORM
13
25
if ( ! platform && process . argv [ 2 ] ) {
14
26
platform = execSync ( 'docker info -f "{{.OSType}}/{{.Architecture}}"' ) . toString ( ) . trim ( )
@@ -35,35 +47,25 @@ if (process.argv[2] === '--docker') {
35
47
process . exit ( 0 )
36
48
}
37
49
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 )
43
61
}
44
- writeFileSync ( join ( WASM_OUT , 'wasm_build_env.txt' ) , buildInfo )
45
62
46
63
// 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 } \
64
65
${ join ( WASM_SRC , 'src' ) } /*.c \
65
66
-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' } )
67
69
68
70
const base64Wasm = readFileSync ( join ( WASM_OUT , 'llhttp.wasm' ) ) . toString ( 'base64' )
69
71
writeFileSync (
@@ -72,27 +74,11 @@ writeFileSync(
72
74
)
73
75
74
76
// 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 } \
93
78
${ join ( WASM_SRC , 'src' ) } /*.c \
94
79
-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' } )
96
82
97
83
const base64WasmSimd = readFileSync ( join ( WASM_OUT , 'llhttp_simd.wasm' ) ) . toString ( 'base64' )
98
84
writeFileSync (
0 commit comments