Skip to content

Commit f5b350d

Browse files
committed
Switch back workspace-optimizer to Rust stable
Separate building from optimizing artifacts
1 parent b718d14 commit f5b350d

4 files changed

+28
-30
lines changed

optimize_workspace.py renamed to build_workspace.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,9 @@ def log(*args):
3030
contract_packages = [p for p in all_packages if p.startswith(PACKAGE_PREFIX)]
3131
log("Contracts to be built:", contract_packages)
3232

33-
artifacts_dir = os.path.realpath("artifacts")
34-
os.makedirs(artifacts_dir, exist_ok=True)
35-
3633
for contract in contract_packages:
3734
log("Building {} ...".format(contract))
38-
# make a tmp dir for the output (*.wasm and other) to not touch the host filesystem
39-
tmp_dir = "/tmp/" + contract
40-
os.makedirs(tmp_dir, exist_ok=True)
4135

42-
# Rust nightly and unstable-options is needed to use --out-dir
43-
cmd = [CARGO_PATH, "-Z=unstable-options", "build", "--release", "--target=wasm32-unknown-unknown", "--locked", "--out-dir={}".format(tmp_dir)]
36+
cmd = [CARGO_PATH, "build", "--release", "--target=wasm32-unknown-unknown", "--locked"]
4437
os.environ["RUSTFLAGS"] = "-C link-arg=-s"
4538
subprocess.check_call(cmd, cwd=contract)
46-
47-
for build_result in glob.glob("{}/*.wasm".format(tmp_dir)):
48-
log("Optimizing build {} ...".format(build_result))
49-
name = os.path.basename(build_result)
50-
cmd = ["wasm-opt", "-Os", "-o", "artifacts/{}".format(name), build_result]
51-
subprocess.check_call(cmd)

optimize_workspace.sh

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
#!/bin/bash
22
set -o errexit -o nounset -o pipefail
3-
command -v shellcheck > /dev/null && shellcheck "$0"
3+
command -v shellcheck >/dev/null && shellcheck "$0"
44

55
export PATH="$PATH:/root/.cargo/bin"
66

77
rustup toolchain list
88
cargo --version
99

10-
optimize_workspace.py
10+
# Build artifacts
11+
echo -n "Building artifacts in workspace..."
12+
/usr/local/bin/build_workspace.py
13+
echo "done."
1114

12-
# Postprocess artifacts
15+
echo -n "Optimizing artifacts in workspace..."
16+
# Start clean
17+
rm -rf ./artifacts
18+
mkdir artifacts
19+
# Optimize and postprocess artifacts
1320
(
1421
cd artifacts
15-
chmod -x ./*.wasm
16-
sha256sum -- *.wasm > checksums.txt
17-
)
1822

19-
echo "done"
23+
for WASM in ../target/wasm32-unknown-unknown/release/*/*.wasm
24+
do
25+
echo -n "Optimizing $WASM..."
26+
BASE=$(basename "$WASM")
27+
wasm-opt -Os -o "$BASE" "$WASM"
28+
chmod -x "$BASE"
29+
echo "done."
30+
done
31+
sha256sum -- *.wasm >checksums.txt
32+
)
33+
echo "done."

rust-optimizer.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Note: I tried slim and had issues compiling wasm-pack, even with --features vendored-openssl
22
FROM rust:1.51.0
33

4-
# setup rust with Wasm support
4+
# Setup Rust with Wasm support
55
RUN rustup target add wasm32-unknown-unknown
66

77
# Install sccache from https://crates.io/crates/sccache

workspace-optimizer.Dockerfile

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
# This version of Rust will not be used for compilation but just serves as a stable base image to get debian+rustup.
22
# See Rust nightly config below.
33
FROM rust:1.51.0
4-
RUN rustup toolchain remove 1.51.0
4+
5+
# Setup Rust with Wasm support
6+
RUN rustup target add wasm32-unknown-unknown
57

68
RUN apt update
79
RUN apt install python3 python3-toml -y
810

911
RUN python3 --version
1012

11-
# Install Rust nightly
12-
# Choose version from: https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu.html
13-
RUN rustup toolchain install nightly-2021-03-01 --allow-downgrade --profile minimal --target wasm32-unknown-unknown
14-
RUN rustup default nightly-2021-03-01
15-
RUN rustup toolchain list
1613
# Check cargo version
1714
RUN cargo --version
1815

@@ -31,9 +28,9 @@ RUN wasm-opt --version
3128
WORKDIR /code
3229

3330
# Add our scripts as entry point
34-
ADD optimize_workspace.py /usr/local/bin/
31+
ADD build_workspace.py /usr/local/bin/
3532
ADD optimize_workspace.sh /usr/local/bin/
36-
RUN chmod +x /usr/local/bin/optimize_workspace.py
33+
RUN chmod +x /usr/local/bin/build_workspace.py
3734
RUN chmod +x /usr/local/bin/optimize_workspace.sh
3835

3936
ENTRYPOINT ["optimize_workspace.sh"]

0 commit comments

Comments
 (0)