Skip to content

Commit

Permalink
chore: update noir_wasm build process to match acvm_js
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jul 27, 2023
1 parent ad118eb commit ad6286b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 25 deletions.
21 changes: 17 additions & 4 deletions crates/wasm/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,8 +30,6 @@ require_command wasm-opt

export pname=$(toml2json < Cargo.toml | jq -r .package.name)

./preBuild.sh
cargo build --lib --release --package noir_wasm --target wasm32-unknown-unknown
./postBuild.sh
./installPhase.sh
run_or_fail ./buildPhaseCargoCommand.sh
run_or_fail ./installPhase.sh

38 changes: 38 additions & 0 deletions crates/wasm/buildPhaseCargoCommand.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/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
}

# Clear out the existing build artifacts as these aren't automatically removed by wasm-pack.
if [ -d ./pkg/ ]; then
rm -rf ./pkg/
fi

# TODO: Handle the wasm target being built in release mode
WASM_BINARY=../../target/wasm32-unknown-unknown/release/${pname}.wasm

echo `pwd`
NODE_DIR=./pkg/nodejs/
BROWSER_DIR=./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
5 changes: 3 additions & 2 deletions crates/wasm/installPhase.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env bash

mkdir -p $out
cp ./crates/wasm/README.md $out/
cp ./crates/wasm/package.json $out/

cp ./README.md ./pkg/
cp ./package.json ./pkg/
cp -r ./pkg/* $out/

echo "## Tracking" >> $out/README.md
Expand Down
11 changes: 0 additions & 11 deletions crates/wasm/postBuild.sh

This file was deleted.

6 changes: 0 additions & 6 deletions crates/wasm/preBuild.sh

This file was deleted.

4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@
toml2json
];

postBuild = ''
bash crates/wasm/postBuild.sh
buildPhaseCargoCommand = ''
bash crates/wasm/buildPhaseCargoCommand.sh
'';

installPhase = ''
Expand Down

0 comments on commit ad6286b

Please sign in to comment.