From bc463323b41bf2cfa1855236e2579d0961025e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Stan=C4=9Bk?= Date: Wed, 14 Aug 2024 14:48:58 +0200 Subject: [PATCH] build: parametrize the location of wasm-opt (#3454) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The wasm-opt binary may be available in different place than the local directory (`./wasm-opt`) – for example, in /usr/bin/wasm-opt. Similarly to the other parametrized WASM build options, use WASM_OPT environment variable to identify the wanted binary, with fallback to the previous value. Even with the environment variable available, the `hasOptimizer` is kept to make the optimization optional. Signed-off-by: Jan Staněk --- CONTRIBUTING.md | 1 + build/wasm.js | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6b6c01d2264..68c9b977f1a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -159,6 +159,7 @@ an unbundled version instead of bundling one in `libnode.so`. To enable this, pass `EXTERNAL_PATH=/path/to/global/node_modules/undici` to `build/wasm.js`. Pass this path with `loader.js` appended to `--shared-builtin-undici/undici-path` in Node.js's `configure.py`. If building on a non-Alpine Linux distribution, you may need to also set the `WASM_CC`, `WASM_CFLAGS`, `WASM_LDFLAGS` and `WASM_LDLIBS` environment variables before running `build/wasm.js`. +Similarly, you can set the `WASM_OPT` environment variable to utilize your own `wasm-opt` optimizer. ### Benchmarks diff --git a/build/wasm.js b/build/wasm.js index 2f65c0e1d74..1880ce3dfe4 100644 --- a/build/wasm.js +++ b/build/wasm.js @@ -14,6 +14,7 @@ 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 || '' +const WASM_OPT = process.env.WASM_OPT || './wasm-opt' // For compatibility with Node.js' `configure --shared-builtin-undici/undici-path ...` const EXTERNAL_PATH = process.env.EXTERNAL_PATH @@ -77,7 +78,7 @@ const hasApk = (function () { try { execSync('command -v apk'); return true } catch (error) { return false } })() const hasOptimizer = (function () { - try { execSync('./wasm-opt --version'); return true } catch (error) { return false } + try { execSync(`${WASM_OPT} --version`); return true } catch (error) { return false } })() if (hasApk) { // Gather information about the tools used for the build @@ -97,7 +98,7 @@ ${join(WASM_SRC, 'src')}/*.c \ ${WASM_LDLIBS}`, { stdio: 'inherit' }) if (hasOptimizer) { - execSync(`./wasm-opt ${WASM_OPT_FLAGS} -o ${join(WASM_OUT, 'llhttp.wasm')} ${join(WASM_OUT, 'llhttp.wasm')}`, { stdio: 'inherit' }) + execSync(`${WASM_OPT} ${WASM_OPT_FLAGS} -o ${join(WASM_OUT, 'llhttp.wasm')} ${join(WASM_OUT, 'llhttp.wasm')}`, { stdio: 'inherit' }) } writeWasmChunk('llhttp.wasm', 'llhttp-wasm.js') @@ -109,7 +110,7 @@ ${join(WASM_SRC, 'src')}/*.c \ ${WASM_LDLIBS}`, { stdio: 'inherit' }) if (hasOptimizer) { - execSync(`./wasm-opt ${WASM_OPT_FLAGS} --enable-simd -o ${join(WASM_OUT, 'llhttp_simd.wasm')} ${join(WASM_OUT, 'llhttp_simd.wasm')}`, { stdio: 'inherit' }) + execSync(`${WASM_OPT} ${WASM_OPT_FLAGS} --enable-simd -o ${join(WASM_OUT, 'llhttp_simd.wasm')} ${join(WASM_OUT, 'llhttp_simd.wasm')}`, { stdio: 'inherit' }) } writeWasmChunk('llhttp_simd.wasm', 'llhttp_simd-wasm.js')