forked from dfinity/nns-dapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·127 lines (109 loc) · 3.87 KB
/
build.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
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
#!/usr/bin/env bash
#
# The frontend is built into public/. The dapp is bundled into a tarball,
# assets.tar.xz. This tarball is baked into the wasm binary output at build
# time by cargo, and finally the wasm binary is read by ic-cdk-optimizer and
# optimizer. This scripts outputs a single file, nns-dapp.wasm.
#
# frontend◄─────────── assets.tar.xz
# ▲
# │
# │
# cargo build
# ▲
# │ ic-cdk-optimizer
# │
# nns-dapp.wasm
set -euo pipefail
TOPLEVEL="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# If the WASM has been provided, there is no need to build it:
[[ "${NNS_DAPP_WASM:-}" == "" ]] || {
test -f "${NNS_DAPP_WASM}" || {
echo "File not found, or node is not a file: NNS_DAPP_WASM='${NNS_DAPP_WASM}'"
exit 1
} >&2
INSTALL_LOCATION="$(jq -r '.canisters["nns-dapp"].wasm' dfx.json)"
[[ "$(realpath "${NNS_DAPP_WASM}")" == "$(realpath "${INSTALL_LOCATION}")" ]] ||
cp "${NNS_DAPP_WASM}" "${INSTALL_LOCATION}"
echo "Skipping build as the WASM file has already been provided."
exit 0
}
# Need to know which deployment we are building for:
DFX_NETWORK="${DFX_NETWORK:-}"
export DFX_NETWORK
jq -e '.networks[env.DFX_NETWORK]' dfx.json || {
echo "Which deployment? Set DFX_NETWORK to one of:"
jq -er '.networks | keys | join(" ")' dfx.json
exit 1
} >&2
# Assemble the configuration
. config.sh
export HOST
export IDENTITY_SERVICE_URL
export OWN_CANISTER_ID
export OWN_CANISTER_URL
export FETCH_ROOT_KEY
export GOVERNANCE_CANISTER_ID
export GOVERNANCE_CANISTER_URL
export LEDGER_CANISTER_ID
export LEDGER_CANISTER_URL
export WASM_CANISTER_ID
set -x
###################
# frontend # (output: frontend/public/)
###################
(cd "$TOPLEVEL/frontend" && npm ci && npm run build)
#################
# assets.tar.xz #
#################
# we need GNU tar (see below) so we check early
if tar --help | grep GNU >/dev/null; then
echo "found GNU tar as tar"
tar="tar"
elif command -v gtar >/dev/null; then
echo "found GNU tar as gtar"
tar="gtar"
else
echo "did not find GNU tar, please install"
echo " brew install gnu-tar"
exit 1
fi
if ! command -v xz >/dev/null; then
echo "did not find xz, please install"
echo " brew install xz"
exit 1
fi
# We use a local directory, and we don't delete it after the build, so that
# assets can be inspected.
tarball_dir="$TOPLEVEL/web-assets"
rm -rf "$tarball_dir"
echo "using $tarball_dir for tarball directory"
cp -R "$TOPLEVEL/frontend/public/" "$tarball_dir/"
# Bundle into a tight tarball
# On macOS you need to install gtar + xz
# brew install gnu-tar
# brew install xz
cd "$tarball_dir"
"$tar" cJv --mtime='2021-05-07 17:00+00' --sort=name --exclude .last_build_id -f "$TOPLEVEL/assets.tar.xz" .
cd "$TOPLEVEL"
ls -sh "$TOPLEVEL/assets.tar.xz"
sha256sum "$TOPLEVEL/assets.tar.xz"
###############
# cargo build # (output: target/release/.../nns-dapp.wasm)
###############
echo Compiling rust package
cargo_args=(--target wasm32-unknown-unknown --release --package nns-dapp)
if [[ $DFX_NETWORK != "mainnet" ]]; then
cargo_args+=(--features mock_conversion_rate)
fi
(cd "$TOPLEVEL" && cargo build "${cargo_args[@]}")
####################
# ic-cdk-optimizer # (output: nns-dapp.wasm)
####################
echo Optimising wasm
cd "$TOPLEVEL"
ic-cdk-optimizer ./target/wasm32-unknown-unknown/release/nns-dapp.wasm -o ./nns-dapp.wasm
gzip -f -n nns-dapp.wasm
mv nns-dapp.wasm.gz nns-dapp.wasm
ls -sh ./nns-dapp.wasm
sha256sum ./nns-dapp.wasm