Skip to content

Commit ff0b746

Browse files
authored
Merge pull request #37 from CosmWasm/workspace-optmimizer-avoid-nightly
Switch back workspace-optimizer to Rust stable
2 parents ab5be9d + 6cf7c2a commit ff0b746

4 files changed

+37
-32
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: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
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+
# Delete already built artifacts
11+
rm -f target/wasm32-unknown-unknown/release/*/*.wasm
1112

12-
# Postprocess artifacts
13+
# Build artifacts
14+
echo -n "Building artifacts in workspace..."
15+
/usr/local/bin/build_workspace.py
16+
echo "done."
17+
18+
echo -n "Optimizing artifacts in workspace..."
19+
mkdir -p artifacts
20+
TMPARTIFACTS=$(mktemp -p "$(pwd)" -d artifacts.XXXXXX)
21+
# Optimize artifacts
22+
(
23+
cd "$TMPARTIFACTS"
24+
25+
for WASM in ../target/wasm32-unknown-unknown/release/*/*.wasm
26+
do
27+
echo -n "Optimizing $WASM..."
28+
BASE=$(basename "$WASM")
29+
wasm-opt -Os -o "$BASE" "$WASM"
30+
chmod -x "$BASE"
31+
echo "done."
32+
done
33+
mv ./*.wasm ../artifacts
34+
)
35+
rm -rf "$TMPARTIFACTS"
36+
echo "done."
37+
echo -n "Post-processing artifacts in workspace..."
1338
(
1439
cd artifacts
15-
chmod -x ./*.wasm
16-
sha256sum -- *.wasm > checksums.txt
40+
sha256sum -- *.wasm >checksums.txt
1741
)
18-
19-
echo "done"
42+
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
# Download sccache and verify checksum

workspace-optimizer.Dockerfile

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

66
RUN apt update
77
RUN apt install python3 python3-toml -y
88

99
RUN python3 --version
1010

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
1611
# Check cargo version
1712
RUN cargo --version
1813

@@ -32,9 +27,9 @@ RUN wasm-opt --version
3227
WORKDIR /code
3328

3429
# Add our scripts as entry point
35-
ADD optimize_workspace.py /usr/local/bin/
30+
ADD build_workspace.py /usr/local/bin/
3631
ADD optimize_workspace.sh /usr/local/bin/
37-
RUN chmod +x /usr/local/bin/optimize_workspace.py
32+
RUN chmod +x /usr/local/bin/build_workspace.py
3833
RUN chmod +x /usr/local/bin/optimize_workspace.sh
3934

4035
ENTRYPOINT ["optimize_workspace.sh"]

0 commit comments

Comments
 (0)