-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbuild
executable file
·241 lines (205 loc) · 7.14 KB
/
build
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env bash
export CDIR=`dirname "$0"`
. "$CARGO_HOME/env"
. "$CDIR/lib.sh"
# echo $PATH
# If the user sends a SIGINT, we want to exit the entire script
trap "exit" INT
# This is where docker expects our substrate/polkadot repo to be
cd /build
export VERSION=`cat /srtool/VERSION || echo 0.0.0`
export GEN="srtool v$VERSION"
export LATEST_VERSION=$(curl -L -s https://github.com/paritytech/srtool/raw/master/VERSION)
vercomp "$LATEST_VERSION" "$VERSION"
case $? in
0) op='=';;
1) op='>';;
2) op='<';;
esac
if [[ "$op" == ">" && ! "$*" == *"--json"* ]]; then
echo "_____________________________________________________________________________________"
echo ""
echo " You are using srtool v$VERSION. A newer version: v$LATEST_VERSION is now available."
echo " You should upgrade asap with the following command:"
echo " docker images | grep srtool | awk '{ print \$3 }' | xargs docker rmi --force"
echo "_____________________________________________________________________________________"
fi
# echo $VERSION $op $LATEST_VERSION
# if [[ "$op" == "=" ]]; then
# echo "You are using the latest version of srtool: v$VERSION"
# fi
# if [[ "$op" == "<" ]]; then
# echo "You are using srtool v$VERSION, it looks newer than the latest version :)"
# fi
if [[ ! "$*" =~ --json || "$*" =~ --app ]]; then
echo "🧰 Substrate Runtime Toolbox - $GEN 🧰"
echo " - by Chevdor -"
fi
if [ ! -z "$VERBOSE" ]; then
echo "Checking cache size. Give it a few seconds..."
echo -e "📦 Cache size:" `du -sh $HOME/.cargo`
fi
# The following is to prevent user's overrides to disturb srtool
rustup override set $RUSTC_VERSION
export RUSTCV=`rustc -V`
if [[ ! "$*" =~ --json || "$*" =~ --app ]]; then
echo -e "🏗 Building $PACKAGE as $PROFILE using $RUSTCV"
echo -e "⏳ That can take a little while, be patient... subsequent builds may be faster."
echo -e " Since you have to wait a little, you may want to learn more about Substrate runtimes:"
echo -e " https://docs.substrate.io/learn/architecture/"
fi
# Build the runtime
if [[ "$*" =~ --app ]]; then
COLOR=never
else
COLOR=always
fi
# As of https://github.com/paritytech/polkadot/pull/2522 we need to cd to the
# runtime directory itself
export RUNTIME_NAME="${PACKAGE/-runtime/}"
export GET_OPTS_CMD="/srtool/getBuildOpts.sh"
export GET_OPTS=`$GET_OPTS_CMD`
export BUILD_OPTS="${BUILD_OPTS:-${GET_OPTS}}"
export PROFILE="${PROFILE:-release}"
export RUNTIME_DIR="${RUNTIME_DIR:-runtime/$RUNTIME_NAME}"
# export RUSTC_BOOTSTRAP=1
# We check a few things before spending too much time building...
# srtool only makes sense when called from a Cargo folder so we check that
if [ ! -f "/build/$RUNTIME_DIR/Cargo.toml" ]; then
echo "!!! The RUNTIME_DIR '$RUNTIME_DIR' does not look like a Cargo project. Is it pointing to the folder of your runtime crate?"
exit 1
fi
CRATE_VERSION=$(get_runtime_package_version "$PACKAGE")
CRATE_NAME=`toml get "/build/$RUNTIME_DIR/Cargo.toml" package.name | jq -r`
if [[ $CRATE_NAME != "$PACKAGE" ]]; then
echo "!!! The PACKAGE '$PACKAGE' does not match the crate name at '$RUNTIME_DIR'."
if [ ! -z "$CRATE_NAME" ]; then
echo "Did you mean '$CRATE_NAME'?"
fi
exit 1
fi
if [[ "$*" == *"--json"* && ! "$*" =~ --app ]]; then
INT_OPTS="--quiet"
else
INT_OPTS=""
fi
if [[ "$*" == *"--json"* && ! "$*" =~ --app ]]; then
INT_OPTS="--quiet"
else
INT_OPTS=""
fi
pushd "$RUNTIME_DIR" > /dev/null || exit 1
# TODO: --out-dir /out would be handy but still unstable.
# See https://doc.rust-lang.org/cargo/commands/cargo-build.html#output-options
CMD="cargo build --locked ${INT_OPTS} ${BUILD_OPTS} --color=$COLOR --profile $PROFILE --target-dir target/srtool"
if [ ! -z "$VERBOSE" ]; then
echo "Command run:"
echo "$CMD"
time $CMD || exit 1
else
if [[ "$*" == *"--json"* && ! "$*" =~ --app ]]; then
$CMD > /dev/null 2>&1 || exit 1
else
$CMD 2>&1 || exit 1
fi
fi
PKG_FILENAME=${PACKAGE//-/_}
WASM=`find "target/srtool/$PROFILE/wbuild/$PACKAGE" -type f -name "${PKG_FILENAME}.compact.wasm"`
Z_WASM=`find "target/srtool/$PROFILE/wbuild/$PACKAGE" -type f -name "${PKG_FILENAME}.compact.compressed.wasm"`
popd > /dev/null || exit 1
if [[ ! "$*" =~ --json || "$*" =~ --app ]]; then
echo "✨ Your Substrate WASM Runtime is ready! ✨"
fi
export WASM=$RUNTIME_DIR/$WASM
cp -f "$WASM" /out/
if [ "$Z_WASM" ]; then
export Z_WASM=$RUNTIME_DIR/$Z_WASM
cp -f "$Z_WASM" /out/
fi
if [[ ! "$*" =~ --json || "$*" =~ --app ]]; then
echo "✨ WASM : $WASM"
echo "✨ Z_WASM: $Z_WASM"
fi
Z_WASM_JSON=$(subwasm -j info "$Z_WASM")
SZ=`du -sb "$Z_WASM" | awk '{print $1}'`
TMSP=`date --utc +%FT%TZ`
SHA256=0x`shasum -a 256 "$Z_WASM" | awk '{print $1}'`
PROP=`echo "$Z_WASM_JSON" | jq -r .proposal_hash`
AUTHORIZE_UPGRADE_PROP=`echo "$Z_WASM_JSON" | jq -r .parachain_authorize_upgrade_hash`
MULTIHASH=`echo "$Z_WASM_JSON" | jq -r .ipfs_hash`
# If we work on a snapshot, we will NOT get a .git folder to work on
if [ -d ".git" ]; then
SRC='git'
GIT_TAG=`git describe --tags --abbrev=0`
GIT_COMMIT_REF=`git rev-parse HEAD`
GIT_BRANCH=`git rev-parse --abbrev-ref HEAD`
else
SRC='zip'
fi
COMPACT_DETAILS=`/srtool/analyse.sh "$WASM" | tail -n 1`
if [ "$Z_WASM" ]; then
COMPRESSED_DETAILS=`/srtool/analyse.sh "$Z_WASM" | tail -n 1`
else
COMPRESSED_DETAILS={}
fi
INFO=$(/srtool/info -cM | tail -n 1)
CONTEXT=$(/srtool/getContext.sh | tail -n 1)
JSON=$( jq -n \
--arg gen "$GEN" \
--arg cargo_version "$CRATE_VERSION" \
--arg rustc "$RUSTCV" \
--arg pkg "$PACKAGE" \
--arg bytes "$SZ" \
--arg wasm "$WASM" \
--arg z_wasm "$Z_WASM" \
--arg prop "$PROP" \
--arg authorize_upgrade_prop "$AUTHORIZE_UPGRADE_PROP" \
--arg sha256 "$SHA256" \
--arg tmsp "$TMSP" \
--arg git_tag "$GIT_TAG" \
--arg git_commit_ref "$GIT_COMMIT_REF" \
--arg git_branch "$GIT_BRANCH" \
--arg src "$SRC" \
--arg ipfs "$MULTIHASH" \
--argjson info "$INFO" \
--argjson context "$CONTEXT" \
--argjson compact "$COMPACT_DETAILS" \
--argjson compressed "$COMPRESSED_DETAILS" \
'{
gen: $gen,
src: $src,
version: $cargo_version,
commit: $git_commit_ref,
tag: $git_tag,
branch: $git_branch,
rustc: $rustc,
pkg: $pkg,
tmsp: $tmsp,
size: $bytes,
prop: $prop,
authorize_upgrade_prop: $authorize_upgrade_prop,
ipfs: $ipfs,
sha256: $sha256,
wasm: $z_wasm,
info: $info,
context: $context,
runtimes: {
compact: $compact,
compressed: $compressed
}
}' )
if [[ "$*" =~ --json || "$*" =~ --app ]]; then
# if using --app, the json output fits on a single line
if [[ "$*" == *"--app"* ]]; then
echo "$JSON" | jq -cM
else
echo "$JSON" | jq
fi
if [[ "$*" == *"--save"* ]]; then
REPORT="./target/srtool/srtool-wasm-report-$TMSP.txt"
echo "$JSON" > $REPORT
echo "Report saved in $REPORT"
fi
else
echo $JSON | tera --template /srtool/templates/output.txt --stdin --env --fail-on-collision
fi