Files
pezkuwi-wasm/scripts/build-wasm.sh
T
Satoshi Qazi Muhammed 8f3c7354c7 Keep build machine paths out of the wasm
Panic messages carry source paths, so the 7.5.20 wasm names the home
directory it was built in 54 times. Remap the sysroot, the cargo home
and the checkout to /rustc, /cargo and /build.
2026-10-12 06:17:29 +04:00

66 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copyright 2019-2025 @pezkuwi/wasm authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.
set -e
source "$(dirname "$0")/rust-version.sh"
WSM=$PKG_NAME/build-wasm/wasm_bg.wasm
OPT=$PKG_NAME/build-wasm/wasm_opt.wasm
ASM=$PKG_NAME-asmjs/build/cjs/data.js
DENO_DIR=$PKG_NAME-asmjs/build-deno/deno
DENO_ASM=$DENO_DIR/data.js
# build new via wasm-pack
echo "*** Building Rust sources"
cd $PKG_NAME
# Panic messages carry source paths. Without this the published wasm names the
# home directory of whoever built it; with it the bytes no longer depend on
# where the toolchain, the registry or the checkout live.
export RUSTFLAGS="${RUSTFLAGS:-} --remap-path-prefix=$(rustc --print sysroot)=/rustc --remap-path-prefix=${CARGO_HOME:-$HOME/.cargo}=/cargo --remap-path-prefix=$PWD=/build"
if [ "$RUST_VER" == "stable" ]; then
RUSTC_BOOTSTRAP=1 cargo build --target wasm32-unknown-unknown --release -Z build-std=std,panic_abort
else
rustup run $RUST_VER xargo build --target wasm32-unknown-unknown --release
fi
cd ..
echo "*** Converting to WASM"
# Use system wasm-bindgen (installed via cargo)
wasm-bindgen $PKG_NAME/target/wasm32-unknown-unknown/release/wasm.wasm --out-dir $PKG_NAME/build-wasm --target web
# optimise using system wasm-opt
echo "*** Optimising WASM output"
wasm-opt $WSM -Oz -o $OPT
# convert wasm to base64 structure
echo "*** Packing WASM into baseX"
node ../scripts/pack-wasm-base.mjs
# build asmjs version from the input (optimised) WASM
echo "*** Building asm.js version"
# Use system wasm2js
wasm2js -Oz --output $ASM $OPT
# copy the deno version
mkdir -p $DENO_DIR
cp $ASM $DENO_ASM
# cleanup the generated asm, converting to cjs
sed -i -e '/^import /d' $ASM
sed -i -e '1,/var retasmFunc = /!d' $ASM
sed -i -e 's/var retasmFunc = .*/exports.asmJsInit = (wbg) => asmFunc({ wbg });/g' $ASM
# same as the cjs version, this time for deno
sed -i -e '/^import /d' $DENO_ASM
sed -i -e '1,/var retasmFunc = /!d' $DENO_ASM
sed -i -e 's/var retasmFunc = .*/export const asmJsInit = (wbg) => asmFunc({ wbg });/g' $DENO_ASM
# cleanups
rm -rf $PKG_NAME-asmjs/build/cjs/*-e
rm -rf $PKG_NAME-asmjs/build-deno/deno/*-e
rm -rf $PKG_NAME-wasm/build/cjs/*-e
rm -rf $PKG_NAME-wasm/build-deno/deno/*-e