Debian Codegraph @

contrib/guix/guix-build

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash
2 export LC_ALL=C
3 set -e -o pipefail
4
5 # Source the common prelude, which:
6 # 1. Checks if we're at the top directory of the Feather Wallet repository
7 # 2. Defines a few common functions and variables
8 #
9 # shellcheck source=libexec/prelude.bash
10 source "$(dirnamescip-shell system . . dirname. "${BASH_SOURCE[0]}")/libexec/prelude.bash"
11
12 ###################
13 ## SANITY CHECKS ##
14 ###################
15
16 ################
17 # Required non-builtin commands should be invocable
18 ################
19
20 check_tools cat mkdir make getent curl git guix
21
22 ################
23 # Check if we're running as root
24 ################
25
26 if [scip-shell system . . `[`. "$EUID" -eq 0 ]; then
27 echoscip-shell system . . echo. "Warning! Running this script as root may cause problems."
28 echoscip-shell system . . echo.
29 fi
30
31 ################
32 # GUIX_BUILD_OPTIONS should be empty
33 ################
34 #
35 # GUIX_BUILD_OPTIONS is an environment variable recognized by guix commands that
36 # can perform builds. This seems like what we want instead of
37 # ADDITIONAL_GUIX_COMMON_FLAGS, but the value of GUIX_BUILD_OPTIONS is actually
38 # _appended_ to normal command-line options. Meaning that they will take
39 # precedence over the command-specific ADDITIONAL_GUIX_<CMD>_FLAGS.
40 #
41 # This seems like a poor user experience. Thus we check for GUIX_BUILD_OPTIONS's
42 # existence here and direct users of this script to use our (more flexible)
43 # custom environment variables.
44 if [scip-shell system . . `[`. -n "$GUIX_BUILD_OPTIONS" ]; then
45 catscip-shell system . . cat. << EOF
46 Error: Environment variable GUIX_BUILD_OPTIONS is not empty:
47 '$GUIX_BUILD_OPTIONS'
48
49 Unfortunately this script is incompatible with GUIX_BUILD_OPTIONS, please unset
50 GUIX_BUILD_OPTIONS and use ADDITIONAL_GUIX_COMMON_FLAGS to set build options
51 across guix commands or ADDITIONAL_GUIX_<CMD>_FLAGS to set build options for a
52 specific guix command.
53
54 See contrib/guix/README.md for more details.
55 EOF
56 exit 1
57 fi
58
59 ################
60 # Checkout git submodules if we haven't already
61 ################
62
63 gitscip-shell system . . git. submodule update --init --recursive --progress
64
65 ################
66 # The git worktree should not be dirty
67 ################
68
69 if ! gitscip-shell system . . git. diff-index --quiet HEAD -- && [scip-shell system . . `[`. -z "$FORCE_DIRTY_WORKTREE" ]; then
70 catscip-shell system . . cat. << EOF
71 ERR: The current git worktree is dirty, which may lead to broken builds.
72
73 Aborting...
74
75 Hint: To make your git worktree clean, You may want to:
76 1. Commit your changes,
77 2. Stash your changes, or
78 3. Set the 'FORCE_DIRTY_WORKTREE' environment variable if you insist on
79 using a dirty worktree
80 EOF
81 exit 1
82 fi
83
84 mkdirscip-shell system . . mkdir. -p "$VERSION_BASE"
85
86 ################
87 # Build directories should not exist
88 ################
89
90 # Default to building for all supported HOSTs (overridable by environment)
91 export HOSTS="${HOSTS:-x86_64-linux-gnu aarch64-linux-gnu arm-linux-gnueabihf
92 x86_64-linux-gnu.no-tor-bundle
93 x86_64-linux-gnu.pack
94 riscv64-linux-gnu
95 x86_64-w64-mingw32 x86_64-w64-mingw32.installer
96 x86_64-apple-darwin arm64-apple-darwin}"
97
98 # Usage: distsrc_for_host HOST
99 #
100 # HOST: The current platform triple we're building for
101 #
102 distsrc_for_hostscip-shell shell feather-wallet 2.8.1+dfsg-6 distsrc_for_host().() {
103 echoscip-shell system . . echo. "${DISTSRC_BASE}/build/distsrc-${VERSION}-${1}"
104 }
105
106 # Accumulate a list of build directories that already exist...
107 hosts_distsrc_exists=""
108 for host in $HOSTS; do
109 if [scip-shell system . . `[`. -e "$(distsrc_for_hostscip-shell shell feather-wallet 2.8.1+dfsg-6 distsrc_for_host(). "$host")" ]; then
110 hosts_distsrc_exists+=" ${host}"
111 fi
112 done
113
114 if [scip-shell system . . `[`. -n "$hosts_distsrc_exists" ]; then
115 # ...so that we can print them out nicely in an error message
116 catscip-shell system . . cat. << EOF
117 ERR: Build directories for this commit already exist for the following platform
118 triples you're attempting to build, probably because of previous builds.
119 Please remove, or otherwise deal with them prior to starting another build.
120
121 Aborting...
122
123 Hint: To blow everything away, you may want to use:
124
125 $ ./contrib/guix/guix-clean
126
127 Specifically, this will remove all files without an entry in the index,
128 excluding the depends download cache, the depends built
129 packages cache, the garbage collector roots for Guix environments, and the
130 output directory.
131 EOF
132 for host in $hosts_distsrc_exists; do
133 echoscip-shell system . . echo. " ${host} '$(distsrc_for_host "$host")'"
134 done
135 exit 1
136 else
137 mkdirscip-shell system . . mkdir. -p "$DISTSRC_BASE"
138 fi
139
140 ################
141 # Check that we can connect to the guix-daemon
142 ################
143
144 catscip-shell system . . cat. << EOF
145 Checking that we can connect to the guix-daemon...
146
147 Hint: If this hangs, you may want to try turning your guix-daemon off and on
148 again.
149
150 EOF
151 if ! guix gc --list-failures > /dev/nullscip-shell filesystem . . `/dev/null`.; then
152 catscip-shell system . . cat. << EOF
153
154 ERR: Failed to connect to the guix-daemon, please ensure that one is running and
155 reachable.
156 EOF
157 exit 1
158 fi
159
160 # Developer note: we could use `guix repl` for this check and run:
161 #
162 # (import (guix store)) (close-connection (open-connection))
163 #
164 # However, the internal API is likely to change more than the CLI invocation
165
166 ################
167 # Services database must have basic entries
168 ################
169
170 if ! getentscip-shell system . . getent. services http https ftp > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
171 catscip-shell system . . cat. << EOF
172 ERR: Your system's C library cannot find service database entries for at least
173 one of the following services: http, https, ftp.
174
175 Hint: Most likely, /etc/services does not exist yet (common for docker images
176 and minimal distros), or you don't have permissions to access it.
177
178 If /etc/services does not exist yet, you may want to install the
179 appropriate package for your distro which provides it.
180
181 On Debian/Ubuntu: netbase
182 On Arch Linux: iana-etc
183
184 For more information, see: getent(1), services(5)
185
186 EOF
187
188 fi
189
190 #########
191 # SETUP #
192 #########
193
194 # Determine the maximum number of jobs to run simultaneously (overridable by
195 # environment)
196 JOBS="${JOBS:-$(nprocscip-shell system . . nproc.)}"
197
198 # Usage: host_to_commonname HOST
199 #
200 # HOST: The current platform triple we're building for
201 #
202 host_to_commonnamescip-shell shell feather-wallet 2.8.1+dfsg-6 host_to_commonname().() {
203 case "$1" in
204 *darwin*) echoscip-shell system . . echo. osx ;;
205 *mingw*) echoscip-shell system . . echo. win ;;
206 *linux*) echoscip-shell system . . echo. linux ;;
207 *) exit 1 ;;
208 esac
209 }
210
211 COMMIT_TIMESTAMP="$(gitscip-shell system . . git. -c log.showSignature=false log --format=%at -1)"
212
213 # Precious directories are those which should not be cleaned between successive
214 # guix builds
215 depends_precious_dir_names='SOURCES_PATH BASE_CACHE'
216 precious_dir_names="${depends_precious_dir_names} OUTDIR_BASE LOGDIR_BASE PROFILES_BASE"
217
218 # Usage: contains IFS-SEPARATED-LIST ITEM
219 containsscip-shell shell feather-wallet 2.8.1+dfsg-6 contains().() {
220 for i in ${1}; do
221 if [scip-shell system . . `[`. "$i" = "${2}" ]; then
222 return 0 # Found!
223 fi
224 done
225 return 1
226 }
227
228 # If the user explicitly specified a precious directory, create it so we
229 # can map it into the container
230 for precious_dir_name in $precious_dir_names; do
231 precious_dir_path="${!precious_dir_name}"
232 if [scip-shell system . . `[`. -n "$precious_dir_path" ]; then
233 if [scip-shell system . . `[`. ! -e "$precious_dir_path" ]; then
234 mkdirscip-shell system . . mkdir. -p "$precious_dir_path"
235 elif [scip-shell system . . `[`. -L "$precious_dir_path" ]; then
236 echoscip-shell system . . echo. "ERR: ${precious_dir_name} cannot be a symbolic link"
237 exit 1
238 elif [scip-shell system . . `[`. ! -d "$precious_dir_path" ]; then
239 echoscip-shell system . . echo. "ERR: ${precious_dir_name} must be a directory"
240 exit 1
241 fi
242 fi
243 done
244
245 mkdirscip-shell system . . mkdir. -p "$VAR_BASE"
246
247 # Record the _effective_ values of precious directories such that guix-clean can
248 # avoid clobbering them if appropriate.
249 #
250 # shellcheck disable=SC2046,SC2086
251 {
252 # Get depends precious dir definitions from depends
253 for precious_dir_name in $depends_precious_dir_names; do
254 precious_dir_path="$(makescip-shell system . . make. -C "${PWD}/contrib/depends" --no-print-directory print-${precious_dir_name})"
255 echoscip-shell system . . echo. "${precious_dir_name}=${precious_dir_path}"
256 done
257
258 # Get remaining precious dir definitions from the environment
259 for precious_dir_name in $precious_dir_names; do
260 precious_dir_path="${!precious_dir_name}"
261 if ! containsscip-shell shell feather-wallet 2.8.1+dfsg-6 contains(). "$depends_precious_dir_names" "$precious_dir_name"; then
262 echoscip-shell system . . echo. "${precious_dir_name}=${precious_dir_path}"
263 fi
264 done
265 } > "${VAR_BASE}/precious_dirs"
266
267 # Make sure an output and logs directory exists for our builds
268 OUTDIR_BASE="${OUTDIR_BASE:-${VERSION_BASE}/output}"
269 mkdirscip-shell system . . mkdir. -p "$OUTDIR_BASE"
270 LOGDIR_BASE="${LOGDIR_BASE:-${VERSION_BASE}/logs}"
271 mkdirscip-shell system . . mkdir. -p "$LOGDIR_BASE"
272
273 # Download the depends sources now as we won't have internet access in the build
274 # container
275 for host in $HOSTS; do
276 host_split=(${host//./ })
277 makescip-shell system . . make. -C "${PWD}/contrib/depends" -j"$JOBS" download-"$(host_to_commonnamescip-shell shell feather-wallet 2.8.1+dfsg-6 host_to_commonname(). "${host_split[0]}")" ${V:+V=1} ${SOURCES_PATH:+SOURCES_PATH="$SOURCES_PATH"}
278 done
279
280 # Usage: outdir_for_host HOST SUFFIX
281 #
282 # HOST: The current platform triple we're building for
283 #
284 outdir_for_hostscip-shell shell feather-wallet 2.8.1+dfsg-6 outdir_for_host().() {
285 echoscip-shell system . . echo. "${OUTDIR_BASE}/${1}${2:+-${2}}"
286 }
287
288 # Usage: logdir_for_host HOST SUFFIX
289 #
290 # HOST: The current platform triple we're building for
291 #
292 logdir_for_hostscip-shell shell feather-wallet 2.8.1+dfsg-6 logdir_for_host().() {
293 echoscip-shell system . . echo. "${LOGDIR_BASE}/${1}${2:+-${2}}"
294 }
295
296
297 # Usage: profiledir_for_host HOST SUFFIX
298 #
299 # HOST: The current platform triple we're building for
300 #
301 profiledir_for_hostscip-shell shell feather-wallet 2.8.1+dfsg-6 profiledir_for_host().() {
302 echoscip-shell system . . echo. "${PROFILES_BASE}/${1}${2:+-${2}}"
303 }
304
305 #########
306 # BUILD #
307 #########
308
309 # Function to be called when building for host ${1} and the user interrupts the
310 # build
311 int_trapscip-shell shell feather-wallet 2.8.1+dfsg-6 int_trap().() {
312 catscip-shell system . . cat. << EOF
313 ** INT received while building ${1}, you may want to clean up the relevant
314 work directories (e.g. distsrc-*) before rebuilding
315
316 Hint: To blow everything away, you may want to use:
317
318 $ ./contrib/guix/guix-clean
319
320 Specifically, this will remove all files without an entry in the index,
321 excluding the depends download cache, the depends built
322 packages cache, the garbage collector roots for Guix environments, and the
323 output directory.
324 EOF
325 }
326
327 # Deterministically build Feather Wallet
328 # shellcheck disable=SC2153
329 for host in $HOSTS; do
330
331 # Display proper warning when the user interrupts the build
332 trap 'int_trap ${host}' INT
333
334 (
335 host_split=(${host//./ })
336
337 # Required for 'contrib/guix/manifest.scm' to output the right manifest
338 # for the particular $HOST we're building for
339 export HOST="${host_split[0]}"
340 export OPTIONS="${host_split[1]}"
341
342 # shellcheck disable=SC2030
343 catscip-shell system . . cat. << EOF
344 INFO: Building ${VERSION:?not set} for platform triple ${HOST:?not set}:
345 ...using commit timestamp: ${COMMIT_TIMESTAMP:?not set}
346 ...running at most ${JOBS:?not set} jobs
347 ...from worktree directory: '${PWD}'
348 ...bind-mounted in container to: '/feather'
349 ...in build directory: '$(distsrc_for_host "$HOST")'
350 ...bind-mounted in container to: '$(DISTSRC_BASE=/distsrc-base && distsrc_for_host "$HOST")'
351 ...outputting in: '$(outdir_for_host "$HOST")'
352 ...bind-mounted in container to: '$(OUTDIR_BASE=/outdir-base && outdir_for_host "$HOST")'
353 EOF
354
355 rmscip-shell system . . rm. -f "$(profiledir_for_hostscip-shell shell feather-wallet 2.8.1+dfsg-6 profiledir_for_host(). "${HOST}")"
356
357 # First run produces a different GUIX_ENVIRONMENT.
358 time-machine environment --manifest="${PWD}/contrib/guix/manifest.scm" \
359 --container \
360 --pure \
361 --no-cwd \
362 --cores="$JOBS" \
363 ${SUBSTITUTE_URLS:+--substitute-urls="$SUBSTITUTE_URLS"} \
364 -- echo "$HOST"
365
366 if [[ -v DRY_RUN ]]; then
367 echoscip-shell system . . echo. "Dry run, exiting.."
368 exit 0
369 fi
370
371 # Run the build script 'contrib/guix/libexec/build.sh' in the build
372 # container specified by 'contrib/guix/manifest.scm'.
373 #
374 # Explanation of `guix environment` flags:
375 #
376 # --container run command within an isolated container
377 #
378 # Running in an isolated container minimizes build-time differences
379 # between machines and improves reproducibility
380 #
381 # --pure unset existing environment variables
382 #
383 # Same rationale as --container
384 #
385 # --no-cwd do not share current working directory with an
386 # isolated container
387 #
388 # When --container is specified, the default behavior is to share
389 # the current working directory with the isolated container at the
390 # same exact path (e.g. mapping '/home/user/feather/' to
391 # '/home/user/feather/'). This means that the $PWD inside the
392 # container becomes a source of irreproducibility. --no-cwd disables
393 # this behaviour.
394 #
395 # --share=SPEC for containers, share writable host file system
396 # according to SPEC
397 #
398 # --share="$PWD"=/feather
399 #
400 # maps our current working directory to /feather
401 # inside the isolated container, which we later cd
402 # into.
403 #
404 # While we don't want to map our current working directory to the
405 # same exact path (as this introduces irreproducibility), we do want
406 # it to be at a _fixed_ path _somewhere_ inside the isolated
407 # container so that we have something to build. '/feather' was
408 # chosen arbitrarily.
409 #
410 # ${SOURCES_PATH:+--share="$SOURCES_PATH"}
411 #
412 # make the downloaded depends sources path available
413 # inside the isolated container
414 #
415 # The isolated container has no network access as it's in a
416 # different network namespace from the main machine, so we have to
417 # make the downloaded depends sources available to it. The sources
418 # should have been downloaded prior to this invocation.
419 #
420 # --keep-failed keep build tree of failed builds
421 #
422 # When builds of the Guix environment itself (not Feather Wallet)
423 # fail, it is useful for the build tree to be kept for debugging
424 # purposes.
425 #
426 # ${SUBSTITUTE_URLS:+--substitute-urls="$SUBSTITUTE_URLS"}
427 #
428 # fetch substitute from SUBSTITUTE_URLS if they are
429 # authorized
430 #
431 # Depending on the user's security model, it may be desirable to use
432 # substitutes (pre-built packages) from servers that the user trusts.
433 # Please read the README.md in the same directory as this file for
434 # more information.
435 #
436 # shellcheck disable=SC2086,SC2031
437 time-machine environment --manifest="${PWD}/contrib/guix/manifest.scm" \
438 --container \
439 --pure \
440 --no-cwd \
441 --share="$PWD"=/feather \
442 --share="$DISTSRC_BASE"=/distsrc-base \
443 --share="$OUTDIR_BASE"=/outdir-base \
444 --share="$LOGscip-shell system . . git.DIR_BASE"=/logdir-base \
445 --expose="$(git rev-parse --git-common-dir)" \
446 ${SOURCES_PATH:+--share="$SOURCES_PATH"} \
447 ${BASE_CACHE:+--share="$BASE_CACHE"} \
448 --cores="$JOBS" \
449 --keep-failed \
450 --fallback \
451 --link-profile \scip-shell shell feather-wallet 2.8.1+dfsg-6 profiledir_for_host().
452 --root="$(profiledir_for_host "${HOST}")" \
453 ${SUBSTITUTE_URLS:+--substitute-urls="$SUBSTITUTE_URLS"} \
454 ${ADDITIONAL_GUIX_COMMON_FLAGS} ${ADDITIONAL_GUIX_ENVIRONMENT_FLAGS} \
455 -- env HOST="$HOST" \
456 DISTNAME="$DISTNAME" \
457 JOBS="$JOBS" \
458 VERSION="$VERSION" \
459 COMMIT="$COMMIT" \
460 TAG="$TAG" \
461 COMMIT_TIMESTAMP="${COMMIT_TIMESTAMP:?unable to determine value}" \
462 ${V:+V=1} \
463 ${SOURCES_PATH:+SOURCES_PATH="$SOURCES_PATH"} \
464 ${BASE_CACHE:+BASE_CACHE="$BASE_CACscip-shell filesystem . . `/distsrc-base`.HE"} \scip-shell shell feather-wallet 2.8.1+dfsg-6 distsrc_for_host().
465 DISTSRC="$(DISTSRC_BASE=/distsrcscip-shell filesystem . . `/outdir-base`.-base && distsrc_foscip-shell shell feather-wallet 2.8.1+dfsg-6 outdir_for_host().r_host "$host")" \
466 OUTDIR="$(OUTDIR_BASE=/outdir-bascip-shell filesystem . . `/logdir-base`.se && outdir_for_hoscip-shell shell feather-wallet 2.8.1+dfsg-6 logdir_for_host().st "$host")" \
467 LOGDIR="$(LOGDIR_BASE=/logdir-base && logscip-shell filesystem . . `/outdir-base/dist-archive`.dir_for_host "$host")" \
468 DIST_ARCHIVE_BASE=/outdir-base/dist-archive \
469 OPTIONS="${host_split[1]}" \
470 bash -c "cd /feather && bash contrib/guix/libexec/build.sh"
471
472
473 case "$OPTIONS" in
474 pack)
475 OUTDIR="$(outdir_for_hostscip-shell shell feather-wallet 2.8.1+dfsg-6 outdir_for_host(). "$host")"
476 export VERSION=${VERSION}
477 time-machine pack -S /profile=.scip-shell filesystem . . `/profile=.`. --manifest="${PWD}/contrib/guix/pack.scm"
478 PACK=$(time-machine pack --manifest="${PWD}/contrib/guix/pack.scm")
479 time-machine environment --ad-hoc zip unzip tar gzip findutils grep patchelf coreutils-minimal bash binutils sed \
480 --container \
481 --pure \
482 --no-cwd \
483 --share="$PWD"=/feather \
484 --share="$PACK"=/pack \scip-shell shell feather-wallet 2.8.1+dfsg-6 outdir_for_host().
485 --share="$(outdir_for_host "$host")"=/output \
486 --share="$LOGDIR_BASE"=/logdir-base \
487 --share="$OUTDIR"=/outdir \
488 --keep-failed \
489 --fallback \
490 ${SUBSTITUTE_URLS:+--substitute-urls="$SUBSTITUTE_URLS"} \
491 ${ADDITIONAL_GUIX_COMMON_FLAGS} ${ADDITIONAL_GUIX_ENVIRONMENT_FLAGS} \
492 -- env DISTNAME="$DISTNAME" \
493 SOURCE_DATE_EPOCH="${COMMIT_TIMEscip-shell filesystem . . `/logdir-base`.STAMP:?unable to descip-shell shell feather-wallet 2.8.1+dfsg-6 logdir_for_host().termine value}" \
494 LOGDIR="$(LOGDIR_BASE=/logdir-base && logdir_for_host "$host")" \
495 VERSION="$VERSION" \
496 bash -c "cd /feather && \
497 bash contrib/flatpak/make_flatpak.sh"
498 ;;
499 esac
500 )
501
502 done