-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathbuild
More file actions
executable file
·338 lines (284 loc) · 11.7 KB
/
build
File metadata and controls
executable file
·338 lines (284 loc) · 11.7 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env bash
set -euo pipefail
# Make sure we always run from the root
SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPTS_DIR/.."
#########
# USAGE #
#########
function title() {
echo "Builds Internet Identity and Archive Canisters"
}
function usage() {
cat << EOF
Usage:
$0 [--only-dependencies] [--internet-identity] [--archive] [--frontend]
Options:
--only-dependencies only build rust dependencies (no js build, no wasm optimization)
--internet-identity build the internet_identity canister (alongside other specifically mentioned canisters), defaults to --internet-identity
--archive build the archive canister (alongside other specifically mentioned canisters), defaults to --internet-identity
--frontend build the frontend (alongside other specifically mentioned canisters), defaults to --internet-identity
EOF
}
function help() {
cat << EOF
Builds the Internet Identity and the Archive canister.
NOTE: This requires a working rust toolchain as well as ic-wasm.
EOF
}
ONLY_DEPS=
CANISTERS=()
while [[ $# -gt 0 ]]
do
case "$1" in
-h|--help)
title
usage
help
exit 0
;;
--only-dependencies)
ONLY_DEPS=1
shift
;;
--internet-identity)
CANISTERS+=("internet_identity")
shift
;;
--archive)
CANISTERS+=("archive")
shift
;;
--frontend)
CANISTERS+=("frontend")
shift
;;
*)
echo "ERROR: unknown argument $1"
usage
echo
echo "Use 'build --help' for more information"
exit 1
;;
esac
done
export II_VERSION=${II_VERSION:-$(./scripts/version)}
echo "The following version will be used: '$II_VERSION'"
# build II by default
if [ ${#CANISTERS[@]} -eq 0 ]; then
CANISTERS=("internet_identity")
fi
# Checking for dependencies
if [[ ! "$(command -v ic-wasm)" || "$(ic-wasm --version)" != "ic-wasm 0.8.5" ]]
then
echo "could not find ic-wasm 0.8.5"
echo "ic-wasm version 0.8.5 is needed, please run the following command:"
echo " cargo install ic-wasm --version 0.8.5"
exit 1
fi
# Check for exact node version
if [[ "$(node --version)" != "v$(cat .node-version)" ]]
then
echo; echo
echo "!!!WARNING!!!: could not find node with exact expected version: v$(cat .node-version)"
# On CI we abort
if [ -n "${CI:-}" ]
then
exit 1
fi
# If the node version doesn't match 100% the build might still succeed though is less likely
# to be reproducible. For developer convenience we still try to go through.
echo "This might cause build or reproducibility issues."
echo; echo
fi
frontend_init_arg() {
local init_arg
init_arg=$(cat <<'EOF'
(record { fetch_root_key = opt true; backend_canister_id = principal "rdmx6-jaaaa-aaaaa-aaadq-cai"; related_origins = opt vec { "http://uqzsh-gqaaa-aaaaq-qaada-cai.localhost:4943" }; backend_origin = "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943"; dev_csp = opt true})
EOF
)
printf '%s\n' "$init_arg"
}
# Builds a single canister
# build_canister CANISTER EXTRA_BUILD_ARGS...
# CANISTER: possible values: [internet_identity, archive]
# EXTRA_BUILD_ARGS: extra arguments supplied to cargo when building the canister
function build_canister() {
local canister=$1
shift
local extra_build_args=("$@")
echo "Building $canister"
echo
SRC_DIR="$PWD/src/$canister"
TARGET="wasm32-unknown-unknown"
# standardize source references
CARGO_HOME="${CARGO_HOME:-"$HOME/.cargo"}"
RUSTFLAGS="--remap-path-prefix $CARGO_HOME=/cargo -C link-args=-zstack-size=3000000"
cargo_build_args=(
--manifest-path "$SRC_DIR/Cargo.toml"
--target "$TARGET"
--release
-j1
)
# XXX: for bash > 4.4, empty arrays are considered unset, so do some substitution
cargo_build_args+=(${extra_build_args[@]+"${extra_build_args[@]}"})
echo Running cargo build "${cargo_build_args[@]}"
echo RUSTFLAGS: "$RUSTFLAGS"
RUSTFLAGS="$RUSTFLAGS" cargo build "${cargo_build_args[@]}"
if [ "$ONLY_DEPS" != "1" ]
then
CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$SRC_DIR/../../target/}"
ic-wasm \
"$CARGO_TARGET_DIR/$TARGET/release/$canister.wasm" \
-o "./$canister.wasm" \
shrink
ic-wasm "$canister.wasm" -o "$canister.wasm" metadata candid:service -f "$SRC_DIR/$canister.did" -v public
# indicate support for certificate version 1 and 2 in the canister metadata
ic-wasm "$canister.wasm" -o "$canister.wasm" metadata supported_certificate_versions -d "1,2" -v public
if [ "$canister" == "internet_identity" ]
then
# indicate the II canister init argument type
ic-wasm "$canister.wasm" -o "$canister.wasm" metadata candid:args -d "(opt InternetIdentityInit)" -v public
# Write metadata for dfx.
# The metadata includes a link to the release, which only exists if this is a release build.
# In case of a release build, the version looks like this: <commit>,<release>,<dirty>
# We include the URL of a checksum file so that dfx clients can check the hash
# of the build (for caching & integrity checking)
IFS=,
read -r -a version_parts <<< "$II_VERSION"
release="${version_parts[1]}"
if [ -n "$release" ]
then
asset_name="internet_identity_backend.wasm.gz"
wasm_url="https://github.com/dfinity/internet-identity/releases/download/$release/$asset_name"
wasm_hash_url="https://github.com/dfinity/internet-identity/releases/download/$release/$asset_name.sha256"
init_guide="Use '(null)' for sensible defaults. See the candid interface for more details."
metadata_json=$(echo '{}' | jq -cMr \
--arg wasm_url "$wasm_url" \
--arg wasm_hash_url "$wasm_hash_url" \
--arg init_arg "(null)" \
--arg init_guide "$init_guide" \
'. | .pullable = { wasm_url: $wasm_url, wasm_hash_url: $wasm_hash_url, dependencies: [], init_arg: "(null)", init_guide: $init_guide} ')
ic-wasm "$canister.wasm" -o "$canister.wasm" metadata dfx -d "$metadata_json" -v public
fi
fi
if [ "$canister" == "internet_identity_frontend" ]
then
# indicate the frontend canister init argument type
ic-wasm "$canister.wasm" -o "$canister.wasm" metadata candid:args -d "(InternetIdentityFrontendInit)" -v public
# Write pullable metadata for dfx.
IFS=, read -r -a version_parts <<< "$II_VERSION"
release="${version_parts[1]}"
if [ -n "$release" ]
then
asset_name="internet_identity_frontend.wasm.gz"
wasm_url="https://github.com/dfinity/internet-identity/releases/download/$release/$asset_name"
wasm_hash_url="https://github.com/dfinity/internet-identity/releases/download/$release/$asset_name.sha256"
fe_init_arg=$(frontend_init_arg)
init_guide="Provide backend_canister_id and backend_origin matching your II backend deployment. Set related_origins to contain the frontend canister's local origin, e.g., \"http://uqzsh-gqaaa-aaaaq-qaada-cai.localhost:4943\". Set dev_csp = opt true for local development over HTTP."
metadata_json=$(echo '{}' | jq -cMr \
--arg wasm_url "$wasm_url" \
--arg wasm_hash_url "$wasm_hash_url" \
--arg init_arg "$fe_init_arg" \
--arg init_guide "$init_guide" \
'. | .pullable = { wasm_url: $wasm_url, wasm_hash_url: $wasm_hash_url, dependencies: ["rdmx6-jaaaa-aaaaa-aaadq-cai"], init_arg: $init_arg, init_guide: $init_guide} ')
ic-wasm "$canister.wasm" -o "$canister.wasm" metadata dfx -d "$metadata_json" -v public
fi
fi
gzip --best --no-name --force "$canister.wasm"
fi
}
function build_internet_identity() {
if [ "$ONLY_DEPS" != "1" ]
then
DIST_DIR="$SCRIPTS_DIR/../dist"
# Compile frontend assets to dist
echo Compiling internet_identity frontend assets
npm run build
# Add preload links for all css files in every html file
CSS_LINKS=""
TMP_HTML_FILE=$(mktemp)
while IFS= read -r css_file; do
css_file_relative="/${css_file#$DIST_DIR/}"
CSS_LINKS+="<link rel=\"preload\" href=\"$css_file_relative\" as=\"style\">"
done < <(find "$DIST_DIR" -type f -name "*.css" | sort)
find "$DIST_DIR" -type f -name "*.html" | while read -r html_file; do
sed "s#</head>#${CSS_LINKS}</head>#" "$html_file" > "$TMP_HTML_FILE" && mv "$TMP_HTML_FILE" "$html_file"
done
# II canister only supports one content type per resource.
# That is why we only keep either compressed or uncompressed.
find "$DIST_DIR" -type f -name "*.gz" | while read -r gz_file; do
# Remove the .gz suffix to get the base filename
base_file="${gz_file%.gz}"
# Generate the .br filename
br_file="${base_file}.br"
# Delete the .br file if it exists
if [[ -f "$br_file" ]]; then
rm -f "$br_file"
fi
# Keep files not ending in .js or .woff2 as uncompressed files
if [[ ! "$base_file" =~ \.(js|woff2)$ ]]; then
rm -f "$gz_file"
continue
fi
# Delete the base file if it exists
if [[ -f "$base_file" ]]; then
rm -f "$base_file"
fi
done
fi
build_canister "internet_identity"
}
function build_internet_identity_frontend() {
if [ "$ONLY_DEPS" != "1" ]
then
DIST_DIR="$SCRIPTS_DIR/../dist"
# Compile frontend assets to dist
echo Compiling internet_identity frontend assets
npm run build
# Add preload links for all css files in every html file
CSS_LINKS=""
TMP_HTML_FILE=$(mktemp)
while IFS= read -r css_file; do
css_file_relative="/${css_file#$DIST_DIR/}"
CSS_LINKS+="<link rel=\"preload\" href=\"$css_file_relative\" as=\"style\">"
done < <(find "$DIST_DIR" -type f -name "*.css" | sort)
find "$DIST_DIR" -type f -name "*.html" | while read -r html_file; do
sed "s#</head>#${CSS_LINKS}</head>#" "$html_file" > "$TMP_HTML_FILE" && mv "$TMP_HTML_FILE" "$html_file"
done
# II canister only supports one content type per resource.
# That is why we only keep either compressed or uncompressed.
find "$DIST_DIR" -type f -name "*.gz" | while read -r gz_file; do
# Remove the .gz suffix to get the base filename
base_file="${gz_file%.gz}"
# Generate the .br filename
br_file="${base_file}.br"
# Delete the .br file if it exists
if [[ -f "$br_file" ]]; then
rm -f "$br_file"
fi
# Keep files not ending in .js or .woff2 as uncompressed files
if [[ ! "$base_file" =~ \.(js|woff2)$ ]]; then
rm -f "$gz_file"
continue
fi
# Delete the base file if it exists
if [[ -f "$base_file" ]]; then
rm -f "$base_file"
fi
done
fi
build_canister "internet_identity_frontend"
}
for canister in "${CANISTERS[@]}"
do
if [ "$canister" == "internet_identity" ]
then
build_internet_identity
elif [ "$canister" == "frontend" ]
then
build_internet_identity_frontend
else
build_canister "$canister"
fi
done