summaryrefslogtreecommitdiff
path: root/makechrootpkg.in
diff options
context:
space:
mode:
authorEli Schwartz <eschwartz@archlinux.org>2019-02-13 02:02:40 -0500
committerLevente Polyak <anthraxx@archlinux.org>2019-08-09 19:41:51 +0200
commitdf0d6b867b289ed98c4b9e7ced817dee155feb4f (patch)
treec2082ed57184ea3d5fb56a7201cc212c5606113a /makechrootpkg.in
parent8f5a02b23ddb2491c13553cb71276bdd088f63ac (diff)
downloaddevtools-df0d6b867b289ed98c4b9e7ced817dee155feb4f.tar.xz
Revert "makechrootpkg: Avoid having code floating around outside of a function."
This reverts commit 49088b0860276c664933c2b3e36a2fef714b7a07. The fundamental intention was flawed and broken, it caused annoying issues and regressions, and the self-avowed sole purpose of the change was so that a downstream project could *post-modify the script and source it as a library*. That is not okay. You don't wrap non-factorable code in a function called main() and call it a library. The only possible use for this is to treat makechrootpkg *internals* as a library, which is not supported. Downstream projects that wish to use the functionality of makechrootpkg should treat makepkg as a command with a public API in the form of command line options. That is kind of how commands of all kinds work, since forever. That is how all users of makechrootpkg *except for parabola* use it. Arguments that "it saves us the cost of fork+exec to bash" are simply invalid. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> Signed-off-by: Levente Polyak <anthraxx@archlinux.org>
Diffstat (limited to 'makechrootpkg.in')
-rw-r--r--makechrootpkg.in238
1 files changed, 117 insertions, 121 deletions
diff --git a/makechrootpkg.in b/makechrootpkg.in
index 4ea008b..6718d13 100644
--- a/makechrootpkg.in
+++ b/makechrootpkg.in
@@ -15,6 +15,27 @@ m4_include(lib/archroot.sh)
shopt -s nullglob
+default_makepkg_args=(--syncdeps --noconfirm --log --holdver --skipinteg)
+makepkg_args=("${default_makepkg_args[@]}")
+keepbuilddir=false
+update_first=false
+clean_first=false
+run_namcap=false
+temp_chroot=false
+chrootdir=
+passeddir=
+makepkg_user=
+declare -a install_pkgs
+declare -i ret=0
+
+bindmounts_ro=()
+bindmounts_rw=()
+
+copy=$USER
+[[ -n ${SUDO_USER:-} ]] && copy=$SUDO_USER
+[[ -z "$copy" || $copy = root ]] && copy=copy
+src_owner=${SUDO_USER:-$USER}
+
usage() {
echo "Usage: ${0##*/} [options] -r <chrootdir> [--] [makepkg args]"
echo ' Run this script in a PKGBUILD dir to build a package inside a'
@@ -293,140 +314,115 @@ move_products() {
}
# }}}
-main() {
- default_makepkg_args=(--syncdeps --noconfirm --log --holdver --skipinteg)
- makepkg_args=("${default_makepkg_args[@]}")
- keepbuilddir=false
- update_first=false
- clean_first=false
- run_namcap=false
- temp_chroot=false
- chrootdir=
- passeddir=
- makepkg_user=
- declare -a install_pkgs
- declare -i ret=0
-
- bindmounts_ro=()
- bindmounts_rw=()
-
- copy=$USER
- [[ -n ${SUDO_USER:-} ]] && copy=$SUDO_USER
- [[ -z "$copy" || $copy = root ]] && copy=copy
- src_owner=${SUDO_USER:-$USER}
-
- while getopts 'hcur:I:l:nTD:d:U:' arg; do
- case "$arg" in
- c) clean_first=true ;;
- D) bindmounts_ro+=("--bind-ro=$OPTARG") ;;
- d) bindmounts_rw+=("--bind=$OPTARG") ;;
- u) update_first=true ;;
- r) passeddir="$OPTARG" ;;
- I) install_pkgs+=("$OPTARG") ;;
- l) copy="$OPTARG" ;;
- n) run_namcap=true; makepkg_args+=(--install) ;;
- T) temp_chroot=true; copy+="-$$" ;;
- U) makepkg_user="$OPTARG" ;;
- h|*) usage ;;
- esac
- done
-
- [[ ! -f PKGBUILD && -z "${install_pkgs[*]}" ]] && die 'This must be run in a directory containing a PKGBUILD.'
- [[ -n $makepkg_user && -z $(id -u "$makepkg_user") ]] && die 'Invalid makepkg user.'
- makepkg_user=${makepkg_user:-${SUDO_USER:-$USER}}
-
- check_root SOURCE_DATE_EPOCH,GNUPGHOME,SRCDEST,SRCPKGDEST,PKGDEST,LOGDEST,MAKEFLAGS,PACKAGER
-
- # Canonicalize chrootdir, getting rid of trailing /
- chrootdir=$(readlink -e "$passeddir")
- [[ ! -d $chrootdir ]] && die "No chroot dir defined, or invalid path '%s'" "$passeddir"
- [[ ! -d $chrootdir/root ]] && die "Missing chroot dir root directory. Try using: mkarchroot %s/root base-devel" "$chrootdir"
-
- if [[ ${copy:0:1} = / ]]; then
- copydir=$copy
- else
- copydir="$chrootdir/$copy"
- fi
-
- # Pass all arguments after -- right to makepkg
- makepkg_args+=("${@:$OPTIND}")
-
- # See if -R or -e was passed to makepkg
- for arg in "${makepkg_args[@]}"; do
- case ${arg%%=*} in
- --repackage|--noextract) keepbuilddir=true; break ;;
- --repackage|--noextract) keepbuilddir=true; break ;;
- --*) ;;
- -*R*|-*e*) keepbuilddir=true; break ;;
- esac
- done
+while getopts 'hcur:I:l:nTD:d:U:' arg; do
+ case "$arg" in
+ c) clean_first=true ;;
+ D) bindmounts_ro+=("--bind-ro=$OPTARG") ;;
+ d) bindmounts_rw+=("--bind=$OPTARG") ;;
+ u) update_first=true ;;
+ r) passeddir="$OPTARG" ;;
+ I) install_pkgs+=("$OPTARG") ;;
+ l) copy="$OPTARG" ;;
+ n) run_namcap=true; makepkg_args+=(--install) ;;
+ T) temp_chroot=true; copy+="-$$" ;;
+ U) makepkg_user="$OPTARG" ;;
+ h|*) usage ;;
+ esac
+done
+
+[[ ! -f PKGBUILD && -z "${install_pkgs[*]}" ]] && die 'This must be run in a directory containing a PKGBUILD.'
+[[ -n $makepkg_user && -z $(id -u "$makepkg_user") ]] && die 'Invalid makepkg user.'
+makepkg_user=${makepkg_user:-${SUDO_USER:-$USER}}
+
+check_root SOURCE_DATE_EPOCH,GNUPGHOME,SRCDEST,SRCPKGDEST,PKGDEST,LOGDEST,MAKEFLAGS,PACKAGER
+
+# Canonicalize chrootdir, getting rid of trailing /
+chrootdir=$(readlink -e "$passeddir")
+[[ ! -d $chrootdir ]] && die "No chroot dir defined, or invalid path '%s'" "$passeddir"
+[[ ! -d $chrootdir/root ]] && die "Missing chroot dir root directory. Try using: mkarchroot %s/root base-devel" "$chrootdir"
+
+if [[ ${copy:0:1} = / ]]; then
+ copydir=$copy
+else
+ copydir="$chrootdir/$copy"
+fi
+
+# Pass all arguments after -- right to makepkg
+makepkg_args+=("${@:$OPTIND}")
+
+# See if -R or -e was passed to makepkg
+for arg in "${makepkg_args[@]}"; do
+ case ${arg%%=*} in
+ --repackage|--noextract) keepbuilddir=true; break ;;
+ --repackage|--noextract) keepbuilddir=true; break ;;
+ --*) ;;
+ -*R*|-*e*) keepbuilddir=true; break ;;
+ esac
+done
- if [[ -n $SUDO_USER ]]; then
- eval "USER_HOME=~$SUDO_USER"
- else
- USER_HOME=$HOME
- fi
+if [[ -n $SUDO_USER ]]; then
+ eval "USER_HOME=~$SUDO_USER"
+else
+ USER_HOME=$HOME
+fi
- umask 0022
+umask 0022
- load_vars "${XDG_CONFIG_HOME:-$USER_HOME/.config}/pacman/makepkg.conf" || load_vars "$USER_HOME/.makepkg.conf"
- load_vars /etc/makepkg.conf
+load_vars "${XDG_CONFIG_HOME:-$USER_HOME/.config}/pacman/makepkg.conf" || load_vars "$USER_HOME/.makepkg.conf"
+load_vars /etc/makepkg.conf
- # Use PKGBUILD directory if these don't exist
- [[ -d $PKGDEST ]] || PKGDEST=$PWD
- [[ -d $SRCDEST ]] || SRCDEST=$PWD
- [[ -d $SRCPKGDEST ]] || SRCPKGDEST=$PWD
- [[ -d $LOGDEST ]] || LOGDEST=$PWD
+# Use PKGBUILD directory if these don't exist
+[[ -d $PKGDEST ]] || PKGDEST=$PWD
+[[ -d $SRCDEST ]] || SRCDEST=$PWD
+[[ -d $SRCPKGDEST ]] || SRCPKGDEST=$PWD
+[[ -d $LOGDEST ]] || LOGDEST=$PWD
- # Lock the chroot we want to use. We'll keep this lock until we exit.
- lock 9 "$copydir.lock" "Locking chroot copy [%s]" "$copy"
+# Lock the chroot we want to use. We'll keep this lock until we exit.
+lock 9 "$copydir.lock" "Locking chroot copy [%s]" "$copy"
- if [[ ! -d $copydir ]] || $clean_first; then
- sync_chroot "$chrootdir" "$copydir" "$copy"
- fi
+if [[ ! -d $copydir ]] || $clean_first; then
+ sync_chroot "$chrootdir" "$copydir" "$copy"
+fi
- $update_first && arch-nspawn "$copydir" \
- "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \
- pacman -Syu --noconfirm
+$update_first && arch-nspawn "$copydir" \
+ "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \
+ pacman -Syu --noconfirm
- if [[ -n ${install_pkgs[*]:-} ]]; then
- install_packages "$copydir" "${install_pkgs[@]}"
- ret=$?
- # If there is no PKGBUILD we have done
- [[ -f PKGBUILD ]] || return $ret
- fi
+if [[ -n ${install_pkgs[*]:-} ]]; then
+ install_packages "$copydir" "${install_pkgs[@]}"
+ ret=$?
+ # If there is no PKGBUILD we have done
+ [[ -f PKGBUILD ]] || return $ret
+fi
- if [[ "$(id -u "$makepkg_user")" == 0 ]]; then
- error "Running makepkg as root is not allowed."
- exit 1
- fi
+if [[ "$(id -u "$makepkg_user")" == 0 ]]; then
+ error "Running makepkg as root is not allowed."
+ exit 1
+fi
- download_sources "$copydir" "$makepkg_user"
+download_sources "$copydir" "$makepkg_user"
- prepare_chroot "$copydir" "$USER_HOME" "$keepbuilddir" "$run_namcap"
+prepare_chroot "$copydir" "$USER_HOME" "$keepbuilddir" "$run_namcap"
- if arch-nspawn "$copydir" \
- --bind="$PWD:/startdir" \
- --bind="$SRCDEST:/srcdest" \
- "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \
- /chrootbuild "${makepkg_args[@]}"
- then
- move_products "$copydir" "$src_owner"
- else
- (( ret += 1 ))
- fi
+if arch-nspawn "$copydir" \
+ --bind="$PWD:/startdir" \
+ --bind="$SRCDEST:/srcdest" \
+ "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \
+ /chrootbuild "${makepkg_args[@]}"
+then
+ move_products "$copydir" "$src_owner"
+else
+ (( ret += 1 ))
+fi
- $temp_chroot && delete_chroot "$copydir" "$copy"
+$temp_chroot && delete_chroot "$copydir" "$copy"
- if (( ret != 0 )); then
- if $temp_chroot; then
- die "Build failed"
- else
- die "Build failed, check %s/build" "$copydir"
- fi
+if (( ret != 0 )); then
+ if $temp_chroot; then
+ die "Build failed"
else
- true
+ die "Build failed, check %s/build" "$copydir"
fi
-}
-
-main "$@"
+else
+ true
+fi