forked from CosmWasm/optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize.sh
executable file
·61 lines (49 loc) · 2.04 KB
/
optimize.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/ash
# shellcheck shell=dash
# See https://www.shellcheck.net/wiki/SC2187
set -o errexit -o nounset -o pipefail
command -v shellcheck >/dev/null && shellcheck "$0"
export PATH=$PATH:/root/.cargo/bin
# Suffix for non-Intel built artifacts
MACHINE=$(uname -m)
SUFFIX=${MACHINE#x86_64}
SUFFIX=${SUFFIX:+-$SUFFIX}
echo "Info: RUSTC_WRAPPER=$RUSTC_WRAPPER"
echo "Info: sccache stats before build"
sccache -s
mkdir -p artifacts
rm -f artifacts/checksums_intermediate.txt
# There are two cases here
# 1. All contracts (or one) are included in the root workspace (eg. `cosmwasm-template`, `cosmwasm-examples`, `cosmwasm-plus`)
# In this case, we pass no argument, just mount the proper directory.
# 2. Contracts are excluded from the root workspace, but import relative paths from other packages (only `cosmwasm`).
# In this case, we mount root workspace and pass in a path `docker run <repo> ./contracts/hackatom`
# This parameter allows us to mount a folder into docker container's "/code"
# and build "/code/contracts/mycontract".
# Note: if CONTRACTDIR is "." (default in Docker), this ends up as a noop
for CONTRACTDIR in "$@"; do
echo "Building contract in $(realpath "$CONTRACTDIR") ..."
(
cd "$CONTRACTDIR"
# Linker flag "-s" for stripping (https://github.com/rust-lang/cargo/issues/3483#issuecomment-431209957)
# Note that shortcuts from .cargo/config are not available in source code packages from crates.io
RUSTFLAGS='-C link-arg=-s' cargo build --release --lib --target wasm32-unknown-unknown --locked
)
# wasm-optimize on all results
for WASM in "$CONTRACTDIR"/target/wasm32-unknown-unknown/release/*.wasm; do
NAME=$(basename "$WASM" .wasm)${SUFFIX}.wasm
echo "Creating intermediate hash for $NAME ..."
sha256sum -- "$WASM" | tee -a artifacts/checksums_intermediate.txt
echo "Optimizing $NAME ..."
wasm-opt -Os "$WASM" -o "artifacts/$NAME"
done
done
# create hash
echo "Creating hashes ..."
(
cd artifacts
sha256sum -- *.wasm | tee checksums.txt
)
echo "Info: sccache stats after build"
sccache -s
echo "done"