Revert "makechrootpkg: Avoid having code floating around outside of a function."
This reverts commit 49088b0860
.
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>
This commit is contained in:
parent
8f5a02b23d
commit
df0d6b867b
238
makechrootpkg.in
238
makechrootpkg.in
|
@ -15,6 +15,27 @@ m4_include(lib/archroot.sh)
|
||||||
|
|
||||||
shopt -s nullglob
|
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() {
|
usage() {
|
||||||
echo "Usage: ${0##*/} [options] -r <chrootdir> [--] [makepkg args]"
|
echo "Usage: ${0##*/} [options] -r <chrootdir> [--] [makepkg args]"
|
||||||
echo ' Run this script in a PKGBUILD dir to build a package inside a'
|
echo ' Run this script in a PKGBUILD dir to build a package inside a'
|
||||||
|
@ -293,140 +314,115 @@ move_products() {
|
||||||
}
|
}
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
main() {
|
while getopts 'hcur:I:l:nTD:d:U:' arg; do
|
||||||
default_makepkg_args=(--syncdeps --noconfirm --log --holdver --skipinteg)
|
case "$arg" in
|
||||||
makepkg_args=("${default_makepkg_args[@]}")
|
c) clean_first=true ;;
|
||||||
keepbuilddir=false
|
D) bindmounts_ro+=("--bind-ro=$OPTARG") ;;
|
||||||
update_first=false
|
d) bindmounts_rw+=("--bind=$OPTARG") ;;
|
||||||
clean_first=false
|
u) update_first=true ;;
|
||||||
run_namcap=false
|
r) passeddir="$OPTARG" ;;
|
||||||
temp_chroot=false
|
I) install_pkgs+=("$OPTARG") ;;
|
||||||
chrootdir=
|
l) copy="$OPTARG" ;;
|
||||||
passeddir=
|
n) run_namcap=true; makepkg_args+=(--install) ;;
|
||||||
makepkg_user=
|
T) temp_chroot=true; copy+="-$$" ;;
|
||||||
declare -a install_pkgs
|
U) makepkg_user="$OPTARG" ;;
|
||||||
declare -i ret=0
|
h|*) usage ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
bindmounts_ro=()
|
[[ ! -f PKGBUILD && -z "${install_pkgs[*]}" ]] && die 'This must be run in a directory containing a PKGBUILD.'
|
||||||
bindmounts_rw=()
|
[[ -n $makepkg_user && -z $(id -u "$makepkg_user") ]] && die 'Invalid makepkg user.'
|
||||||
|
makepkg_user=${makepkg_user:-${SUDO_USER:-$USER}}
|
||||||
|
|
||||||
copy=$USER
|
check_root SOURCE_DATE_EPOCH,GNUPGHOME,SRCDEST,SRCPKGDEST,PKGDEST,LOGDEST,MAKEFLAGS,PACKAGER
|
||||||
[[ -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
|
# Canonicalize chrootdir, getting rid of trailing /
|
||||||
case "$arg" in
|
chrootdir=$(readlink -e "$passeddir")
|
||||||
c) clean_first=true ;;
|
[[ ! -d $chrootdir ]] && die "No chroot dir defined, or invalid path '%s'" "$passeddir"
|
||||||
D) bindmounts_ro+=("--bind-ro=$OPTARG") ;;
|
[[ ! -d $chrootdir/root ]] && die "Missing chroot dir root directory. Try using: mkarchroot %s/root base-devel" "$chrootdir"
|
||||||
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.'
|
if [[ ${copy:0:1} = / ]]; then
|
||||||
[[ -n $makepkg_user && -z $(id -u "$makepkg_user") ]] && die 'Invalid makepkg user.'
|
copydir=$copy
|
||||||
makepkg_user=${makepkg_user:-${SUDO_USER:-$USER}}
|
else
|
||||||
|
copydir="$chrootdir/$copy"
|
||||||
|
fi
|
||||||
|
|
||||||
check_root SOURCE_DATE_EPOCH,GNUPGHOME,SRCDEST,SRCPKGDEST,PKGDEST,LOGDEST,MAKEFLAGS,PACKAGER
|
# Pass all arguments after -- right to makepkg
|
||||||
|
makepkg_args+=("${@:$OPTIND}")
|
||||||
|
|
||||||
# Canonicalize chrootdir, getting rid of trailing /
|
# See if -R or -e was passed to makepkg
|
||||||
chrootdir=$(readlink -e "$passeddir")
|
for arg in "${makepkg_args[@]}"; do
|
||||||
[[ ! -d $chrootdir ]] && die "No chroot dir defined, or invalid path '%s'" "$passeddir"
|
case ${arg%%=*} in
|
||||||
[[ ! -d $chrootdir/root ]] && die "Missing chroot dir root directory. Try using: mkarchroot %s/root base-devel" "$chrootdir"
|
--repackage|--noextract) keepbuilddir=true; break ;;
|
||||||
|
--repackage|--noextract) keepbuilddir=true; break ;;
|
||||||
|
--*) ;;
|
||||||
|
-*R*|-*e*) keepbuilddir=true; break ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
if [[ ${copy:0:1} = / ]]; then
|
if [[ -n $SUDO_USER ]]; then
|
||||||
copydir=$copy
|
eval "USER_HOME=~$SUDO_USER"
|
||||||
else
|
else
|
||||||
copydir="$chrootdir/$copy"
|
USER_HOME=$HOME
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Pass all arguments after -- right to makepkg
|
umask 0022
|
||||||
makepkg_args+=("${@:$OPTIND}")
|
|
||||||
|
|
||||||
# See if -R or -e was passed to makepkg
|
load_vars "${XDG_CONFIG_HOME:-$USER_HOME/.config}/pacman/makepkg.conf" || load_vars "$USER_HOME/.makepkg.conf"
|
||||||
for arg in "${makepkg_args[@]}"; do
|
load_vars /etc/makepkg.conf
|
||||||
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
|
# Use PKGBUILD directory if these don't exist
|
||||||
eval "USER_HOME=~$SUDO_USER"
|
[[ -d $PKGDEST ]] || PKGDEST=$PWD
|
||||||
else
|
[[ -d $SRCDEST ]] || SRCDEST=$PWD
|
||||||
USER_HOME=$HOME
|
[[ -d $SRCPKGDEST ]] || SRCPKGDEST=$PWD
|
||||||
fi
|
[[ -d $LOGDEST ]] || LOGDEST=$PWD
|
||||||
|
|
||||||
umask 0022
|
# Lock the chroot we want to use. We'll keep this lock until we exit.
|
||||||
|
lock 9 "$copydir.lock" "Locking chroot copy [%s]" "$copy"
|
||||||
|
|
||||||
load_vars "${XDG_CONFIG_HOME:-$USER_HOME/.config}/pacman/makepkg.conf" || load_vars "$USER_HOME/.makepkg.conf"
|
if [[ ! -d $copydir ]] || $clean_first; then
|
||||||
load_vars /etc/makepkg.conf
|
sync_chroot "$chrootdir" "$copydir" "$copy"
|
||||||
|
fi
|
||||||
|
|
||||||
# Use PKGBUILD directory if these don't exist
|
$update_first && arch-nspawn "$copydir" \
|
||||||
[[ -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"
|
|
||||||
|
|
||||||
if [[ ! -d $copydir ]] || $clean_first; then
|
|
||||||
sync_chroot "$chrootdir" "$copydir" "$copy"
|
|
||||||
fi
|
|
||||||
|
|
||||||
$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 [[ "$(id -u "$makepkg_user")" == 0 ]]; then
|
|
||||||
error "Running makepkg as root is not allowed."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
download_sources "$copydir" "$makepkg_user"
|
|
||||||
|
|
||||||
prepare_chroot "$copydir" "$USER_HOME" "$keepbuilddir" "$run_namcap"
|
|
||||||
|
|
||||||
if arch-nspawn "$copydir" \
|
|
||||||
--bind="$PWD:/startdir" \
|
|
||||||
--bind="$SRCDEST:/srcdest" \
|
|
||||||
"${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \
|
"${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \
|
||||||
/chrootbuild "${makepkg_args[@]}"
|
pacman -Syu --noconfirm
|
||||||
then
|
|
||||||
move_products "$copydir" "$src_owner"
|
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
|
||||||
|
|
||||||
|
download_sources "$copydir" "$makepkg_user"
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
$temp_chroot && delete_chroot "$copydir" "$copy"
|
||||||
|
|
||||||
|
if (( ret != 0 )); then
|
||||||
|
if $temp_chroot; then
|
||||||
|
die "Build failed"
|
||||||
else
|
else
|
||||||
(( ret += 1 ))
|
die "Build failed, check %s/build" "$copydir"
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
$temp_chroot && delete_chroot "$copydir" "$copy"
|
true
|
||||||
|
fi
|
||||||
if (( ret != 0 )); then
|
|
||||||
if $temp_chroot; then
|
|
||||||
die "Build failed"
|
|
||||||
else
|
|
||||||
die "Build failed, check %s/build" "$copydir"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
true
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
main "$@"
|
|
||||||
|
|
Loading…
Reference in New Issue