Merge pull request #1016 from VorpalBlade/feature/3rd-party-completion
[aurutils.git] / lib / aur-build
bloba7fc5cbb0b536ec226dff7ecb94f2c6758a1e8d7
1 #!/bin/bash
2 # aur-build - build packages to a local repository
3 [[ -v AUR_DEBUG ]] && set -o xtrace
4 set -o errexit
5 shopt -s extglob
6 argv0=build
7 machine=$(uname -m)
8 startdir=$PWD
9 PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
11 # default options
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=()
18 # default arguments
19 gpg_args=(--detach-sign --no-armor --batch)
20 makechrootpkg_args=(-cu) # -c to clean working copy, -u to sync local repository builds
22 args_csv() {
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)
30 cat <<EOF >&2
31 Note:
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:
37 EOF
38 #shellcheck disable=SC2030
39 realpath -z -- "$@" | while read -rd ''; do
40 printf '%8s%s\n' ' ' "$REPLY"
41 done
44 diag_pacman_conf() {
45 cat <<EOF >&2
46 Error:
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:
52 EOF
53 printf '%8s%s\n' ' ' "$1"
56 # Allow to drop permissions for commands as needed (#907)
57 as_user() {
58 local USER HOME SHELL
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
63 { IFS= read -r USER
64 IFS= read -r HOME
65 IFS= read -r SHELL
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" -- "$@"
70 else
71 env -- "$@"
75 run_msg() {
76 printf >&2 'Running %s\n' "${*:$1}"
77 "${@:2}"
80 trap_exit() {
81 if [[ ! -v AUR_DEBUG ]]; then
82 rm -rf -- "$tmp"
84 # Only remove package directory if all files were moved (#593)
85 if ! rm -df -- "$var_tmp"; then
86 diag_moved_packages "$var_tmp"/*
88 else
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"
94 usage() {
95 plain >&2 'usage: %s [-acfNS] [-d repo] [--root path] [--margs makepkg_arg...]' "$argv0"
96 exit 1
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
106 ## option parsing
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
118 usage
120 set -- "${OPTRET[@]}"
122 unset build_user db_name db_path db_root makepkg_conf pacman_conf results_file queue
123 while true; do
124 case "$1" in
125 # build options
126 -a|--arg-file)
127 shift; queue=$1 ;;
128 -f|--force)
129 overwrite=1 ;;
130 -c|--chroot)
131 chroot=1 ;;
132 -d|--database|--repo)
133 shift; db_name=$1
134 repo_args+=(--repo "$1") ;;
135 --buildscript)
136 shift; makepkg_common_args+=(-p "$1")
137 pkglist_args+=(-p "$1") ;;
138 --dry-run)
139 dry_run=1 ;;
140 --nosync|--no-sync)
141 no_sync=1 ;;
142 --makepkg-conf)
143 shift; makepkg_conf=$1 ;;
144 --pacman-conf)
145 shift; pacman_conf=$1 ;;
146 --pkgver)
147 run_pkgver=1; makepkg_args+=(--noextract) ;;
148 --root)
149 shift; db_root=$1
150 repo_args+=(--root "$1") ;;
151 -S|--sign|--gpg-sign)
152 sign_pkg=1; repo_add_args+=(-s) ;;
153 # chroot options
154 -D|--directory)
155 shift; chroot_args+=(-D "$1") ;;
156 --bind)
157 shift; makechrootpkg_args+=(-D "$1") ;;
158 --bind-rw)
159 shift; makechrootpkg_args+=(-d"$1") ;;
160 -N|--namcap)
161 makechrootpkg_args+=(-n) ;;
162 --checkpkg)
163 makechrootpkg_args+=(-C) ;;
164 -T|--temp)
165 makechrootpkg_args+=(-T) ;;
166 -U|--user)
167 shift; build_user=$1
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) ;;
175 -r|--rmdeps)
176 makepkg_common_args+=(--rmdeps) ;;
177 -s|--syncdeps)
178 makepkg_common_args+=(--syncdeps) ;;
179 # makepkg options (build)
180 -C|--clean)
181 makepkg_args+=(--clean) ;;
182 -L|--log)
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[@]}") ;;
191 # repo-add options
192 -v|--verify)
193 repo_add_args+=(-v) ;;
194 -R|--remove)
195 repo_add_args+=(-R) ;;
196 --new)
197 repo_add_args+=(-n) ;;
198 --prevent-downgrade)
199 repo_add_args+=(-p) ;;
200 # other options
201 --results)
202 shift; results_file=$1 ;;
203 --results-append)
204 shift; results_file=$1; truncate=0 ;;
205 --dump-options)
206 printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
207 printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
208 exit ;;
209 --) shift; break ;;
210 esac
211 shift
212 done
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'
218 exit 1
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")
247 else
248 var_tmp_uid=$UID
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
256 trap 'exit' INT
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"
268 exit 2
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")
275 else
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
279 unset PKGEXT
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"
290 exit 2
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"
300 exit 2
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")
308 else
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[@]}")
313 wait "$!"
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"
320 exit 2
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"
325 exit 13
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
342 exec {fd}< "$queue"
343 else
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[@]}"
355 exit 1
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
365 cd "$startdir"
366 cd "$path"
368 # Allow running repo-add(8) on existing packages (#839)
369 create_package=1
370 pkglist=()
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
381 exists=()
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"
386 exists+=("$pkgpath")
387 else
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)
394 wait "$!"
396 if (( dry_run )); then
397 continue
400 if [[ ${exists[*]} ]]; then
401 warning '%s: skipping existing package (use -f to overwrite)' "$argv0"
402 create_package=0
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[@]}"
418 else
419 PKGDEST="$var_tmp" LOGDEST=${LOGDEST:-$PWD} \
420 run_msg 3 as_user makepkg "${makepkg_common_args[@]}" "${makepkg_args[@]}"
423 cd "$var_tmp"
424 pkglist=(!(*.sig)) # discard makepkg --sign from package list (#410)
425 else
426 cd "$var_tmp"
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).
432 siglist=()
434 for p in "${pkglist[@]}"; do
435 # Package basename (equals $p if create_package=1)
436 p_base=${p##*/}
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)
452 done
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
465 # Update database
466 cd "$db_root"
467 as_user LANG=C repo-add "${repo_add_args[@]}" "$db_path" "${pkglist[@]}"
469 if (( chroot )) || (( no_sync )); then
470 continue
471 else
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"
478 done
480 exec {fd}<&-
482 # vim: set et sw=4 sts=4 ft=sh: