pcbjam/scripts/common/apply-finalize.sh
Istvan Matejcsok 380206dcfc fix(build): host postprocess silently no-ops when emsdk tools are stubbed
build-kicad-target.sh swaps emsdk's wasm-opt/wasm-emscripten-finalize for
no-op stubs (real binaries preserved as *.real) so emcc skips in-link
asyncify — intended for the CONTAINER emsdk, but a host-mode run left the
host tools/emsdk stubbed (since Jun 9). The stub fakes --version and exits 0,
so every local build's host-side finalize/asyncify/-O2 "succeeded" while
doing nothing: output wasm shipped non-asyncified and aborts at boot with
"asyncify_stop_unwind is not a function" (and stayed 122M vs the correct
187M). get-wasm-opt.sh and apply-finalize.sh now prefer the *.real binary
whenever it exists, making the resolution immune to a stubbed emsdk.

Found while validating the CI e2e fix: pcbnew.wasm built this morning could
not boot in any browser. Local artifacts built since Jun 9 may need their
postprocess re-run (apply-finalize.sh + apply-asyncify.sh).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 13:25:02 +02:00

47 lines
1.4 KiB
Shell
Executable file

#!/bin/bash
# Apply wasm-emscripten-finalize transformation on host
# Uses the same Binaryen v121 as wasm-opt to ensure version consistency
#
# Usage: ./scripts/common/apply-finalize.sh <input.wasm> <output.wasm>
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
# Get wasm-opt path (this downloads Binaryen if needed)
WASM_OPT=$("${SCRIPT_DIR}/get-wasm-opt.sh")
BINARYEN_BIN=$(dirname "${WASM_OPT}")
FINALIZE="${BINARYEN_BIN}/wasm-emscripten-finalize"
# Same stub hazard as wasm-opt (see get-wasm-opt.sh): a host-mode kicad build
# leaves the emsdk finalize stubbed with the real binary at .real — prefer it,
# the stub exits 0 having done nothing.
if [ -x "${FINALIZE}.real" ]; then
FINALIZE="${FINALIZE}.real"
fi
INPUT_WASM="${1:-output/pcbnew.wasm}"
OUTPUT_WASM="${2:-${INPUT_WASM}}"
if [ ! -f "${INPUT_WASM}" ]; then
echo "ERROR: Input file not found: ${INPUT_WASM}"
exit 1
fi
echo "Applying wasm-emscripten-finalize..."
echo " Input: ${INPUT_WASM}"
echo " Output: ${OUTPUT_WASM}"
echo " Tool: ${FINALIZE}"
# Run finalize with the same flags Emscripten would use
# NOTE: --dwarf removed because debug info is in separate .debug.wasm file
"${FINALIZE}" \
-g \
--bigint \
--no-legalize-javascript-ffi \
--detect-features \
"${INPUT_WASM}" \
-o "${OUTPUT_WASM}"
echo "Finalize complete: ${OUTPUT_WASM}"
ls -lh "${OUTPUT_WASM}"