Skip to content

Commit c87b048

Browse files
authored
Merge pull request #28 from CosmWasm/shared-compilation-cache
Shared compilation cache
2 parents 6ebc9f8 + 16bbaca commit c87b048

File tree

5 files changed

+40
-21
lines changed

5 files changed

+40
-21
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 0.10.7
4+
5+
- Add shared build cache (sccache) to rust-optimizer
6+
37
## 0.10.6
48

59
- Add support for building multiple non-workspace contracts at once (#25)

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
DOCKER_NAME_RUST_OPTIMIZER := "cosmwasm/rust-optimizer"
44
DOCKER_NAME_WORKSPACE_OPTIMIZER := "cosmwasm/workspace-optimizer"
5-
DOCKER_TAG := 0.10.6
5+
DOCKER_TAG := 0.10.7
66

77
build-rust-optimizer:
88
docker build -t $(DOCKER_NAME_RUST_OPTIMIZER):$(DOCKER_TAG) --file rust-optimizer.Dockerfile .

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ you to produce a smaller build that works with the cosmwasm integration tests
2424
docker run --rm -v "$(pwd)":/code \
2525
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
2626
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
27-
cosmwasm/rust-optimizer:0.10.6
27+
cosmwasm/rust-optimizer:0.10.7
2828
```
2929

3030
Demo this with `cosmwasm-examples` (going into eg. `erc20` subdir before running),
@@ -57,7 +57,7 @@ To compile all contracts in the workspace deterministically, you can run:
5757
docker run --rm -v "$(pwd)":/code \
5858
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
5959
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
60-
cosmwasm/workspace-optimizer:0.10.6
60+
cosmwasm/workspace-optimizer:0.10.7
6161
```
6262

6363
The downside is that to verify one contract in the workspace, you need to compile them
@@ -83,7 +83,7 @@ case, we can use the optimize.sh command:
8383
docker run --rm -v "$(pwd)":/code \
8484
--mount type=volume,source="devcontract_cache_burner",target=/code/contracts/burner/target \
8585
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
86-
cosmwasm/rust-optimizer:0.10.6 ./contracts/burner
86+
cosmwasm/rust-optimizer:0.10.7 ./contracts/burner
8787
```
8888

8989
## Development

optimize.sh

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ command -v shellcheck > /dev/null && shellcheck "$0"
44

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

7+
echo "Info: RUSTC_WRAPPER=$RUSTC_WRAPPER"
8+
9+
echo "Info: sccache stats before build"
10+
sccache -s
11+
712
mkdir -p artifacts
813
contractdirs="$@"
914

@@ -16,22 +21,22 @@ contractdirs="$@"
1621
# This parameter allows us to mount a folder into docker container's "/code"
1722
# and build "/code/contracts/mycontract".
1823
# Note: if contractdir is "." (default in Docker), this ends up as a noop
19-
for contractdir in $contractdirs
20-
do
21-
echo "Building contract in $(realpath -m "$contractdir")"
22-
(
23-
cd "$contractdir"
24-
25-
# Linker flag "-s" for stripping (https://github.com/rust-lang/cargo/issues/3483#issuecomment-431209957)
26-
# Note that shortcuts from .cargo/config are not available in source code packages from crates.io
27-
RUSTFLAGS='-C link-arg=-s' cargo build --release --target wasm32-unknown-unknown --locked
28-
)
29-
30-
# wasm-optimize on all results
31-
for wasm in "$contractdir"/target/wasm32-unknown-unknown/release/*.wasm; do
32-
name=$(basename "$wasm")
33-
echo "Optimizing $name"
34-
wasm-opt -Os "$wasm" -o "artifacts/$name"
24+
for contractdir in $contractdirs; do
25+
echo "Building contract in $(realpath -m "$contractdir")"
26+
(
27+
cd "$contractdir"
28+
29+
# Linker flag "-s" for stripping (https://github.com/rust-lang/cargo/issues/3483#issuecomment-431209957)
30+
# Note that shortcuts from .cargo/config are not available in source code packages from crates.io
31+
RUSTFLAGS='-C link-arg=-s' cargo build --release --target wasm32-unknown-unknown --locked
32+
)
33+
34+
# wasm-optimize on all results
35+
for wasm in "$contractdir"/target/wasm32-unknown-unknown/release/*.wasm; do
36+
name=$(basename "$wasm")
37+
echo "Optimizing $name"
38+
wasm-opt -Os "$wasm" -o "artifacts/$name"
39+
done
3540
done
3641

3742
# create hash
@@ -40,5 +45,7 @@ done
4045
sha256sum -- *.wasm > checksums.txt
4146
)
4247

48+
echo "Info: sccache stats after build"
49+
sccache -s
50+
4351
echo "done"
44-
done

rust-optimizer.Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ FROM rust:1.47.0
44
# setup rust with Wasm support
55
RUN rustup target add wasm32-unknown-unknown
66

7+
# Install sccache from https://crates.io/crates/sccache
8+
# This is slow due to full release compilation from source.
9+
# Use full version string here to ensure Docker caching works well.
10+
RUN cargo install --version 0.2.13 sccache
11+
712
# Download binaryen and verify checksum
813
ADD https://github.com/WebAssembly/binaryen/releases/download/version_96/binaryen-version_96-x86_64-linux.tar.gz /tmp/binaryen.tar.gz
914
RUN sha256sum /tmp/binaryen.tar.gz | grep 9f8397a12931df577b244a27c293d7c976bc7e980a12457839f46f8202935aac
@@ -15,6 +20,9 @@ RUN mv binaryen-version_*/wasm-opt /usr/local/bin
1520
# Check wasm-opt version
1621
RUN wasm-opt --version
1722

23+
# Use sccache. Users can override this variable to disable caching.
24+
ENV RUSTC_WRAPPER=sccache
25+
1826
# Assume we mount the source code in /code
1927
WORKDIR /code
2028

0 commit comments

Comments
 (0)