2 # aur-build - build packages to a local repository
3 [[ -v AUR_DEBUG
]] && set -o xtrace
9 PS4
='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
12 chroot
=0 no_sync
=0 overwrite
=0 sign_pkg
=0 run_pkgver
=0 dry_run
=0 truncate
=1
14 # default arguments (empty)
15 chroot_args
=() pacconf_args
=() repo_args
=() repo_add_args
=() pkglist_args
=()
16 makepkg_args
=() makechrootpkg_makepkg_args
=() makepkg_common_args
=()
19 gpg_args
=(--detach-sign --no-armor --batch)
20 makechrootpkg_args
=(-cu) # -c to clean working copy, -u to sync local repository builds
23 # shellcheck disable=SC2155
24 local str
=$
(printf '%s,' "$@")
25 printf '%s' "${str%,}"
28 diag_moved_packages
() {
29 # Print diagnostic on non-moved packages (#794)
32 aur-build encountered an error before moving packages to the local repository.
33 This may happen when signing built packages with gpg (aur build --sign),
34 or with certain makepkg errors.
36 The following files were preserved:
38 #shellcheck disable=SC2030
39 realpath
-z -- "$@" |
while read -rd ''; do
40 printf '%8s%s\n' ' ' "$REPLY"
47 aur-build could not find a pacman.conf(5) file for container usage. Before
48 using --chroot, make sure this file is created and valid. See OPTIONS in
49 aur-build(1) for configuration details.
51 The following file path was checked:
53 printf '%8s%s\n' ' ' "$1"
56 # Allow to drop permissions for commands as needed (#907)
60 if [[ $UID == 0 ]] && [[ -v build_user
]]; then
61 # runuser --pty messes up the terminal with AUR_DEBUG set, use setpriv(1)
62 # and replicate the runuser(1) behavior for setting the environment
66 } < <(getent passwd
"$build_user" |
awk -F: '{printf("%s\n%s\n%s\n", $1, $6, $7); }')
68 setpriv
--reuid "$build_user" --regid "$build_user" --init-groups \
69 env USER
="$USER" HOME
="$HOME" LOGNAME
="$USER" SHELL
="$SHELL" -- "$@"
76 printf >&2 'Running %s\n' "${*:$1}"
81 if [[ ! -v AUR_DEBUG
]]; then
84 # Only remove package directory if all files were moved (#593)
85 if ! rm -df -- "$var_tmp"; then
86 diag_moved_packages
"$var_tmp"/*
89 printf >&2 'AUR_DEBUG: %s: temporary files at %s\n' "$argv0" "$tmp"
90 printf >&2 'AUR_DEBUG: %s: temporary files at %s\n' "$argv0" "$var_tmp"
95 plain
>&2 'usage: %s [-acfNS] [-d repo] [--root path] [--margs makepkg_arg...]' "$argv0"
99 source /usr
/share
/makepkg
/util
/message.sh
100 source /usr
/share
/makepkg
/util
/parseopts.sh
102 if [[ ! -v NO_COLOR
]] && [[ ! -v AUR_DEBUG
]]; then
103 [[ -t 2 ]] && colorize
107 opt_short
='a:d:D:U:AcCfnrsvLNRST'
108 opt_long
=('arg-file:' 'chroot' 'database:' 'force' 'root:' 'sign' 'gpg-sign'
109 'verify' 'directory:' 'no-sync' 'pacman-conf:' 'remove' 'pkgver'
110 'rmdeps' 'no-confirm' 'no-check' 'ignore-arch' 'log' 'new'
111 'makepkg-conf:' 'bind:' 'bind-rw:' 'prevent-downgrade' 'temp'
112 'syncdeps' 'clean' 'namcap' 'checkpkg' 'user:' 'makepkg-args:'
113 'margs:' 'buildscript:' 'dry-run')
114 opt_hidden
=('dump-options' 'ignorearch' 'noconfirm' 'nocheck' 'nosync' 'repo:'
115 'results:' 'results-append:')
117 if ! parseopts
"$opt_short" "${opt_long[@]}" "${opt_hidden[@]}" -- "$@"; then
120 set -- "${OPTRET[@]}"
122 unset build_user db_name db_path db_root makepkg_conf pacman_conf results_file queue
132 -d|
--database|
--repo)
134 repo_args
+=(--repo "$1") ;;
136 shift; makepkg_common_args
+=(-p "$1")
137 pkglist_args
+=(-p "$1") ;;
143 shift; makepkg_conf
=$1 ;;
145 shift; pacman_conf
=$1 ;;
147 run_pkgver
=1; makepkg_args
+=(--noextract) ;;
150 repo_args
+=(--root "$1") ;;
151 -S|
--sign|
--gpg-sign)
152 sign_pkg
=1; repo_add_args
+=(-s) ;;
155 shift; chroot_args
+=(-D "$1") ;;
157 shift; makechrootpkg_args
+=(-D "$1") ;;
159 shift; makechrootpkg_args
+=(-d"$1") ;;
161 makechrootpkg_args
+=(-n) ;;
163 makechrootpkg_args
+=(-C) ;;
165 makechrootpkg_args
+=(-T) ;;
168 makechrootpkg_args
+=(-U "$1") ;;
169 # makepkg options (common)
170 -A|
--ignorearch|
--ignore-arch)
171 makepkg_common_args
+=(--ignorearch)
172 makechrootpkg_makepkg_args
+=(--ignorearch) ;;
173 -n|
--noconfirm|
--no-confirm)
174 makepkg_common_args
+=(--noconfirm) ;;
176 makepkg_common_args
+=(--rmdeps) ;;
178 makepkg_common_args
+=(--syncdeps) ;;
179 # makepkg options (build)
181 makepkg_args
+=(--clean) ;;
183 makepkg_args
+=(--log) ;;
184 --nocheck|
--no-check)
185 makepkg_args
+=(--nocheck)
186 makechrootpkg_makepkg_args
+=(--nocheck) ;;
187 --makepkg-args|
--margs)
188 shift; IFS
=, read -a arg
-r <<< "$1"
189 makepkg_args
+=("${arg[@]}")
190 makechrootpkg_makepkg_args
+=("${arg[@]}") ;;
193 repo_add_args
+=(-v) ;;
195 repo_add_args
+=(-R) ;;
197 repo_add_args
+=(-n) ;;
199 repo_add_args
+=(-p) ;;
202 shift; results_file
=$1 ;;
204 shift; results_file
=$1; truncate
=0 ;;
206 printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
207 printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
214 # mollyguard for makepkg
215 if [[ $UID == 0 ]] && ! { [[ -v build_user ]] && [[ -v AUR_ASROOT ]]; }; then
216 warning 'aur-%s is not meant to be run as root.' "$argv0"
217 warning 'To proceed anyway, set the %s variable and specify --user <username>.' 'AUR_ASROOT'
221 # Assign environment variables
222 : "${db_ext=$AUR_DBEXT}" "${db_root=$AUR_DBROOT}" "${db_repo=$AUR_REPO}"
224 # Custom makepkg command
225 if [[ $MAKEPKG ]]; then
226 # shellcheck disable=SC2086
227 makepkg() { command -- $MAKEPKG "$@
"; }
230 # Custom elevation command
231 if [[ $UID == 0 ]]; then
232 sudo() { command -- "$@
"; }
234 elif [[ $AUR_PACMAN_AUTH ]]; then
235 # shellcheck disable=SC2086
236 sudo() { command -- $AUR_PACMAN_AUTH "$@
"; }
239 # shellcheck disable=SC2174
240 mkdir -pm 0700 "${TMPDIR:-/tmp}/aurutils-
$UID"
241 tmp=$(mktemp -d --tmpdir "aurutils-
$UID/$argv0.XXXXXXXX
")
243 # Only $var_tmp should be writeable by the build user (PKGDEST, signatures)
244 # If UID > 0 and build_user is unset, this is equivalent to $tmp above
245 if [[ -v build_user ]]; then
246 var_tmp_uid=$(id -u "$build_user")
251 # shellcheck disable=SC2174
252 as_user mkdir -pm 0700 "${TMPDIR:-/var/tmp}/aurutils-
$var_tmp_uid"
253 var_tmp=$(as_user mktemp -d --tmpdir="${TMPDIR:-/var/tmp/}" "aurutils-
$var_tmp_uid/$argv0.XXXXXXXX
")
255 trap 'trap_exit' EXIT
258 if (( chroot )); then
259 # Change the default /usr/share/devtools/pacman-extra.conf in aur-chroot to
260 # /etc/aurutils/pacman-<repo>.conf or /etc/aurutils/pacman-<uname>.conf in
261 # aur-build, and pass it on to aur-chroot (#824, #846)
262 pacman_conf=${pacman_conf-/etc/aurutils/pacman-${db_name:-$machine}.conf}
263 chroot_args+=(--pacman-conf "$pacman_conf")
265 # Early check for availability of pacman.conf (#783)
266 if [[ ! -f $pacman_conf ]]; then
267 diag_pacman_conf "$pacman_conf"
271 # The default path is /usr/share/devtools/makepkg-<uname.conf>, which is
272 # copied to <container path>/etc/makepkg.conf by arch-nspawn.
273 if [[ -v makepkg_conf ]]; then
274 chroot_args+=(--makepkg-conf "$makepkg_conf")
276 # When makechrootpkg calls makepkg inside the container, it uses the above
277 # makepkg.conf for most variables including PKGEXT. (makepkg --packagelist)
278 makepkg_conf=$(aur chroot --path "${chroot_args[@]}")/etc/makepkg.conf
283 # Propagate makepkg and pacman configuration to other tools. This needs to be
284 # done BEFORE retrieving the local repository name/root.
285 if [[ -v pacman_conf ]]; then
286 pacconf_args+=(--config "$pacman_conf")
288 if [[ ! -f $pacman_conf ]]; then
289 error '%s: %s: not a regular file' "$argv0" "$pacman_conf"
294 if [[ -v makepkg_conf ]]; then
295 makepkg_common_args+=(--config "$makepkg_conf")
296 pkglist_args+=(--config "$makepkg_conf")
298 if [[ -v makepkg_conf ]] && [[ ! -f $makepkg_conf ]]; then
299 error '%s: %s: not a regular file' "$argv0" "$makepkg_conf"
304 # Automatically choose the local repository based on the pacman configuration.
305 if [[ $db_name ]] && [[ $db_root ]]; then
306 db_path=$db_root/$db_name.${db_ext:-db}
307 db_path=$(realpath -- "$db_path")
309 { IFS=: read -r _ db_name
310 IFS=: read -r _ db_root
311 IFS=: read -r _ db_path # canonicalized
312 } < <(as_user aur repo --status "${repo_args[@]}" "${pacconf_args[@]}")
315 db_root=$(realpath -- "$db_root")
317 # Resolve symbolic link to database.
318 if ! [[ -f $db_path ]]; then
319 error '%s: %s: not a regular file' "$argv0" "$db_path"
322 # Check if build user can write to database
323 elif ! as_user test -w "$db_path"; then
324 error '%s: %s: permission denied' "$argv0" "$db_path"
328 # Write successfully built packages to file (#437, #980)
329 if [[ -v results_file ]]; then
330 results_file=$(realpath -- "$results_file")
331 (( truncate )) && true | as_user tee "$results_file"
334 if (( chroot )); then
335 # Update pacman and makepkg configuration for the chroot build
336 # queue. A full system upgrade is run on the /root container to
337 # avoid lenghty upgrades for makechrootpkg -u.
338 run_msg 2 aur chroot --create --update "${chroot_args[@]}"
341 if [[ -v queue ]]; then
344 exec {fd}< <(printf '\n')
347 # Early consistency check for signed database
348 if (( ! sign_pkg )); then
349 db_sigs=("$db_root/$db_name".sig "$db_root/$db_name".files.sig)
351 if [[ -f ${db_sigs[0]} ]]; then
352 error '%s: database signature found, but signing is disabled' "$argv0"
354 printf '%q\n' >&2 "${db_sigs[@]}"
358 elif [[ -v GPGKEY ]]; then
359 as_user gpg --list-keys "$GPGKEY"
360 gpg_args+=(-u "$GPGKEY")
363 while IFS= read -ru "$fd" path; do
364 # Use two cd calls to handle absolute paths in --arg-file
368 # Allow running repo-add(8) on existing packages (#839)
372 # Run pkgver before --packagelist (#500)
373 if (( run_pkgver )); then
374 as_user makepkg -od "${makepkg_common_args[@]}" >&2
377 # Check if the package is already built, but unlike makepkg, do not exit
378 # with an error when so. A warning avoids a queue of builds aborting because
379 # one member already exists.
380 if (( ! overwrite )) || (( dry_run )); then
383 while IFS=':' read -r pkgbase pkgpath; do
384 if [[ -f $pkgpath ]]; then
385 (( dry_run )) && printf '%s:%s:%s\n' exist "$pkgbase" "file://$pkgpath"
388 (( dry_run )) && printf '%s:%s:%s\n' build "$pkgbase" "file://$pkgpath"
390 # pkgbase may differ from pkgname; prefix to package path with --full
391 done < <(as_user PKGDEST="$db_root" aur build--pkglist --full "${pkglist_args[@]}")
393 # Preserve the exit status from aur-build--pkglist (#671)
396 if (( dry_run )); then
400 if [[ ${exists[*]} ]]; then
401 warning '%s: skipping existing package (use -f to overwrite)' "$argv0"
404 printf '%q\n' >&2 "${exists[@]}"
405 pkglist=("${exists[@]}")
409 if (( create_package )); then
410 if (( chroot )); then
411 if (( ${#makechrootpkg_args[@]} )); then
412 chroot_args+=(--cargs "$
(args_csv
"${makechrootpkg_args[@]}")")
414 if (( ${#makechrootpkg_makepkg_args[@]} )); then
415 chroot_args+=(--margs "$
(args_csv
"${makechrootpkg_makepkg_args[@]}")")
417 PKGDEST="$var_tmp" run_msg 2 aur chroot --build "${chroot_args[@]}"
419 PKGDEST="$var_tmp" LOGDEST=${LOGDEST:-$PWD} \
420 run_msg 3 as_user makepkg "${makepkg_common_args[@]}" "${makepkg_args[@]}"
424 pkglist=(!(*.sig)) # discard makepkg --sign from package list (#410)
427 # pkglist has paths to $db_root/<pkg>
430 # Sign any packages without signatures, even if the packages are existing.
431 # This is done in the temporary directory (write-access for build user).
434 for p in "${pkglist[@]}"; do
435 # Package basename (equals $p if create_package=1)
438 # Signature from makepkg --sign
439 if [[ -f $p_base.sig ]]; then
440 siglist+=("$p_base".sig)
442 # Skipped package build with signature
443 elif [[ -f $db_root/$p_base.sig ]] && [[ ! -f $p_base ]]; then
444 printf >&2 '%s: existing signature file %q\n' "$argv0" "$db_root/$p_base.sig
"
446 # No candidate signature, generate one
447 elif (( sign_pkg )); then
448 as_user gpg "${gpg_args[@]}" --output "$p_base".sig "$p"
449 printf >&2 '%s: created signature file %q\n' "$argv0" "$p_base".sig
450 siglist+=("$p_base".sig)
454 if (( ${#siglist[@]} )); then
455 mv -f "${siglist[@]}" "$db_root"
457 if (( create_package )); then
458 mv -f "${pkglist[@]}" "$db_root"
460 if [[ -v results_file ]]; then
461 printf "build
:file://$db_root/%s
\n" "${pkglist[@]}" | as_user tee -a "$results_file" >/dev/null
467 as_user LANG=C repo-add "${repo_add_args[@]}" "$db_path" "${pkglist[@]}"
469 if (( chroot )) || (( no_sync )); then
472 # Helper which can be used for e.g. sudoers rules (#1012)
473 # Like `makepkg --syncdeps`, this affects the host and so uses the host
474 # pacman configuration. --pacman-conf (which may also point to
475 # a world-writeable file) is not applied.
476 sudo aur build--sync "$db_name"
482 # vim: set et sw=4 sts=4 ft=sh: