diff --git a/optimize_workspace.py b/build_workspace.py similarity index 52% rename from optimize_workspace.py rename to build_workspace.py index 38a2347..a0250df 100644 --- a/optimize_workspace.py +++ b/build_workspace.py @@ -30,22 +30,9 @@ def log(*args): contract_packages = [p for p in all_packages if p.startswith(PACKAGE_PREFIX)] log("Contracts to be built:", contract_packages) -artifacts_dir = os.path.realpath("artifacts") -os.makedirs(artifacts_dir, exist_ok=True) - for contract in contract_packages: log("Building {} ...".format(contract)) - # make a tmp dir for the output (*.wasm and other) to not touch the host filesystem - tmp_dir = "/tmp/" + contract - os.makedirs(tmp_dir, exist_ok=True) - # Rust nightly and unstable-options is needed to use --out-dir - cmd = [CARGO_PATH, "-Z=unstable-options", "build", "--release", "--target=wasm32-unknown-unknown", "--locked", "--out-dir={}".format(tmp_dir)] + cmd = [CARGO_PATH, "build", "--release", "--target=wasm32-unknown-unknown", "--locked"] os.environ["RUSTFLAGS"] = "-C link-arg=-s" subprocess.check_call(cmd, cwd=contract) - - for build_result in glob.glob("{}/*.wasm".format(tmp_dir)): - log("Optimizing build {} ...".format(build_result)) - name = os.path.basename(build_result) - cmd = ["wasm-opt", "-Os", "-o", "artifacts/{}".format(name), build_result] - subprocess.check_call(cmd) diff --git a/optimize_workspace.sh b/optimize_workspace.sh index b69fe36..6ef7959 100644 --- a/optimize_workspace.sh +++ b/optimize_workspace.sh @@ -1,19 +1,42 @@ #!/bin/bash set -o errexit -o nounset -o pipefail -command -v shellcheck > /dev/null && shellcheck "$0" +command -v shellcheck >/dev/null && shellcheck "$0" export PATH="$PATH:/root/.cargo/bin" rustup toolchain list cargo --version -optimize_workspace.py +# Delete already built artifacts +rm -f target/wasm32-unknown-unknown/release/*/*.wasm -# Postprocess artifacts +# Build artifacts +echo -n "Building artifacts in workspace..." +/usr/local/bin/build_workspace.py +echo "done." + +echo -n "Optimizing artifacts in workspace..." +mkdir -p artifacts +TMPARTIFACTS=$(mktemp -p "$(pwd)" -d artifacts.XXXXXX) +# Optimize artifacts +( + cd "$TMPARTIFACTS" + + for WASM in ../target/wasm32-unknown-unknown/release/*/*.wasm + do + echo -n "Optimizing $WASM..." + BASE=$(basename "$WASM") + wasm-opt -Os -o "$BASE" "$WASM" + chmod -x "$BASE" + echo "done." + done + mv ./*.wasm ../artifacts +) +rm -rf "$TMPARTIFACTS" +echo "done." +echo -n "Post-processing artifacts in workspace..." ( cd artifacts - chmod -x ./*.wasm - sha256sum -- *.wasm > checksums.txt + sha256sum -- *.wasm >checksums.txt ) - -echo "done" +echo "done." diff --git a/rust-optimizer.Dockerfile b/rust-optimizer.Dockerfile index 9c73096..70bfcf2 100644 --- a/rust-optimizer.Dockerfile +++ b/rust-optimizer.Dockerfile @@ -1,7 +1,7 @@ # Note: I tried slim and had issues compiling wasm-pack, even with --features vendored-openssl FROM rust:1.51.0 -# setup rust with Wasm support +# Setup Rust with Wasm support RUN rustup target add wasm32-unknown-unknown # Download sccache and verify checksum diff --git a/workspace-optimizer.Dockerfile b/workspace-optimizer.Dockerfile index 4842ac5..2016f00 100644 --- a/workspace-optimizer.Dockerfile +++ b/workspace-optimizer.Dockerfile @@ -1,18 +1,13 @@ -# This version of Rust will not be used for compilation but just serves as a stable base image to get debian+rustup. -# See Rust nightly config below. FROM rust:1.51.0 -RUN rustup toolchain remove 1.51.0 + +# Setup Rust with Wasm support +RUN rustup target add wasm32-unknown-unknown RUN apt update RUN apt install python3 python3-toml -y RUN python3 --version -# Install Rust nightly -# Choose version from: https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu.html -RUN rustup toolchain install nightly-2021-03-01 --allow-downgrade --profile minimal --target wasm32-unknown-unknown -RUN rustup default nightly-2021-03-01 -RUN rustup toolchain list # Check cargo version RUN cargo --version @@ -32,9 +27,9 @@ RUN wasm-opt --version WORKDIR /code # Add our scripts as entry point -ADD optimize_workspace.py /usr/local/bin/ +ADD build_workspace.py /usr/local/bin/ ADD optimize_workspace.sh /usr/local/bin/ -RUN chmod +x /usr/local/bin/optimize_workspace.py +RUN chmod +x /usr/local/bin/build_workspace.py RUN chmod +x /usr/local/bin/optimize_workspace.sh ENTRYPOINT ["optimize_workspace.sh"]