From 6ad35b7bf06477ffacfbce4c8d6e1216be1de7e9 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Thu, 27 Jul 2023 13:39:31 +0000 Subject: [PATCH 1/2] chore: update `noir_wasm` build process to match `acvm_js` --- crates/wasm/build.sh | 36 ++++++++++++++++++++---- crates/wasm/buildPhaseCargoCommand.sh | 40 +++++++++++++++++++++++++++ crates/wasm/installPhase.sh | 11 ++++++-- crates/wasm/postBuild.sh | 11 -------- crates/wasm/preBuild.sh | 6 ---- flake.nix | 4 +-- 6 files changed, 81 insertions(+), 27 deletions(-) create mode 100755 crates/wasm/buildPhaseCargoCommand.sh delete mode 100755 crates/wasm/postBuild.sh delete mode 100644 crates/wasm/preBuild.sh diff --git a/crates/wasm/build.sh b/crates/wasm/build.sh index 900c1868c08..22814b74936 100755 --- a/crates/wasm/build.sh +++ b/crates/wasm/build.sh @@ -6,6 +6,21 @@ function require_command { exit 1 fi } +function check_installed { + if ! command -v "$1" >/dev/null 2>&1; then + echo "$1 is not installed. Please install it." >&2 + return 1 + fi + return 0 +} +function run_or_fail { + "$@" + local status=$? + if [ $status -ne 0 ]; then + echo "Command '$*' failed with exit code $status" >&2 + exit $status + fi +} require_command toml2json require_command jq @@ -13,10 +28,21 @@ require_command cargo require_command wasm-bindgen require_command wasm-opt -export pname=$(toml2json < Cargo.toml | jq -r .package.name) +self_path=$(dirname "$(readlink -f "$0")") +export pname=$(toml2json < ${self_path}/Cargo.toml | jq -r .package.name) +export CARGO_TARGET_DIR=$self_path/target + +rm -rf $self_path/outputs >/dev/null 2>&1 +rm -rf $self_path/result >/dev/null 2>&1 + +if [ -v out ]; then + echo "Will install package to $out (defined outside installPhase.sh script)" +else + out="$self_path/outputs/out" + echo "Will install package to $out" +fi -./preBuild.sh -cargo build --lib --release --package noir_wasm --target wasm32-unknown-unknown -./postBuild.sh -./installPhase.sh +run_or_fail ${self_path}/buildPhaseCargoCommand.sh +run_or_fail ${self_path}/installPhase.sh +ln -s $out $self_path/result diff --git a/crates/wasm/buildPhaseCargoCommand.sh b/crates/wasm/buildPhaseCargoCommand.sh new file mode 100755 index 00000000000..5fff9eff808 --- /dev/null +++ b/crates/wasm/buildPhaseCargoCommand.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +function run_or_fail { + "$@" + local status=$? + if [ $status -ne 0 ]; then + echo "Command '$*' failed with exit code $status" >&2 + exit $status + fi +} +function run_if_available { + if command -v "$1" >/dev/null 2>&1; then + "$@" + else + echo "$1 is not installed. Please install it to use this feature." >&2 + fi +} + +export self_path=$(dirname "$(readlink -f "$0")") + +# Clear out the existing build artifacts as these aren't automatically removed by wasm-pack. +if [ -d ${self_path}/pkg/ ]; then + rm -rf ${self_path}/pkg/ +fi + +# TODO: Handle the wasm target being built in release mode +TARGET=wasm32-unknown-unknown +WASM_BINARY=${self_path}/../../target/${TARGET}/release/${pname}.wasm + +NODE_DIR=${self_path}/pkg/nodejs/ +BROWSER_DIR=${self_path}/pkg/web/ +NODE_WASM=${NODE_DIR}/${pname}_bg.wasm +BROWSER_WASM=${BROWSER_DIR}/${pname}_bg.wasm + +# Build the new wasm package +run_or_fail cargo build --lib --release --package noir_wasm --target wasm32-unknown-unknown +run_or_fail wasm-bindgen $WASM_BINARY --out-dir $NODE_DIR --typescript --target nodejs +run_or_fail wasm-bindgen $WASM_BINARY --out-dir $BROWSER_DIR --typescript --target web +run_if_available wasm-opt $NODE_WASM -o $NODE_WASM -O +run_if_available wasm-opt $BROWSER_WASM -o $BROWSER_WASM -O diff --git a/crates/wasm/installPhase.sh b/crates/wasm/installPhase.sh index 4dd1473b941..f57875f4780 100755 --- a/crates/wasm/installPhase.sh +++ b/crates/wasm/installPhase.sh @@ -1,9 +1,14 @@ #!/usr/bin/env bash +export self_path=$(dirname "$(readlink -f "$0")") mkdir -p $out -cp ./crates/wasm/README.md $out/ -cp ./crates/wasm/package.json $out/ -cp -r ./pkg/* $out/ + +cp ${self_path}/README.md ${self_path}/pkg/ +cp ${self_path}/package.json ${self_path}/pkg/ +cp -r ${self_path}/pkg/* $out/ echo "## Tracking" >> $out/README.md echo "Built from [noir-lang/noir@$GIT_COMMIT](https://github.com/noir-lang/noir/tree/$GIT_COMMIT)" >> $out/README.md + +# Cleanup temporary pkg directory +rm -r $self_path/pkg diff --git a/crates/wasm/postBuild.sh b/crates/wasm/postBuild.sh deleted file mode 100755 index 1b01901adbe..00000000000 --- a/crates/wasm/postBuild.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -# TODO: Handle the wasm target being built in release mode -WASM_BINARY=./target/wasm32-unknown-unknown/release/${pname}.wasm -NODE_WASM=./pkg/nodejs/${pname}_bg.wasm -BROWSER_WASM=./pkg/web/${pname}_bg.wasm - -wasm-bindgen $WASM_BINARY --out-dir ./pkg/nodejs --typescript --target nodejs -wasm-bindgen $WASM_BINARY --out-dir ./pkg/web --typescript --target web -wasm-opt $NODE_WASM -o $NODE_WASM -O -wasm-opt $BROWSER_WASM -o $BROWSER_WASM -O diff --git a/crates/wasm/preBuild.sh b/crates/wasm/preBuild.sh deleted file mode 100644 index aa334e022c6..00000000000 --- a/crates/wasm/preBuild.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash - -# Clear out the existing build artifacts as these aren't automatically removed by wasm-pack. -if [ -d ./pkg/ ]; then - rm -rf ./pkg/ -fi \ No newline at end of file diff --git a/flake.nix b/flake.nix index a7bd0e39a7e..ea677501147 100644 --- a/flake.nix +++ b/flake.nix @@ -306,8 +306,8 @@ toml2json ]; - postBuild = '' - bash crates/wasm/postBuild.sh + buildPhaseCargoCommand = '' + bash crates/wasm/buildPhaseCargoCommand.sh ''; installPhase = '' From 72890a75b63b7fa2e2b947cb23f28baea203489e Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 10 Aug 2023 15:16:57 +0100 Subject: [PATCH 2/2] chore: add missing newline to `README.md` output --- crates/wasm/installPhase.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/wasm/installPhase.sh b/crates/wasm/installPhase.sh index f57875f4780..98578059be9 100755 --- a/crates/wasm/installPhase.sh +++ b/crates/wasm/installPhase.sh @@ -7,6 +7,7 @@ cp ${self_path}/README.md ${self_path}/pkg/ cp ${self_path}/package.json ${self_path}/pkg/ cp -r ${self_path}/pkg/* $out/ +echo "" >> $out/README.md echo "## Tracking" >> $out/README.md echo "Built from [noir-lang/noir@$GIT_COMMIT](https://github.com/noir-lang/noir/tree/$GIT_COMMIT)" >> $out/README.md