Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch back workspace-optimizer to Rust stable #37

Merged
merged 6 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions optimize_workspace.py → build_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
37 changes: 30 additions & 7 deletions optimize_workspace.sh
Original file line number Diff line number Diff line change
@@ -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
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
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
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
do
echo -n "Optimizing $WASM..."
BASE=$(basename "$WASM")
wasm-opt -Os -o "$BASE" "$WASM"
chmod -x "$BASE"
echo "done."
done
mv ./*.wasm ../artifacts
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
)
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."
2 changes: 1 addition & 1 deletion rust-optimizer.Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 5 additions & 10 deletions workspace-optimizer.Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"]
Expand Down