summaryrefslogtreecommitdiff
path: root/src/devtools/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/devtools/lib')
-rw-r--r--src/devtools/lib/common.sh186
-rw-r--r--src/devtools/lib/makechrootpkg.sh.patch254
2 files changed, 254 insertions, 186 deletions
diff --git a/src/devtools/lib/common.sh b/src/devtools/lib/common.sh
deleted file mode 100644
index d6fbe7c..0000000
--- a/src/devtools/lib/common.sh
+++ /dev/null
@@ -1,186 +0,0 @@
-# Avoid any encoding problems
-export LANG=C
-
-# check if messages are to be printed using color
-unset ALL_OFF BOLD BLUE GREEN RED YELLOW
-if [[ -t 2 ]]; then
- # prefer terminal safe colored and bold text when tput is supported
- if tput setaf 0 &>/dev/null; then
- ALL_OFF="$(tput sgr0)"
- BOLD="$(tput bold)"
- BLUE="${BOLD}$(tput setaf 4)"
- GREEN="${BOLD}$(tput setaf 2)"
- RED="${BOLD}$(tput setaf 1)"
- YELLOW="${BOLD}$(tput setaf 3)"
- else
- ALL_OFF="\e[1;0m"
- BOLD="\e[1;1m"
- BLUE="${BOLD}\e[1;34m"
- GREEN="${BOLD}\e[1;32m"
- RED="${BOLD}\e[1;31m"
- YELLOW="${BOLD}\e[1;33m"
- fi
-fi
-readonly ALL_OFF BOLD BLUE GREEN RED YELLOW
-
-plain() {
- local mesg=$1; shift
- printf "${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-msg() {
- local mesg=$1; shift
- printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-msg2() {
- local mesg=$1; shift
- printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-warning() {
- local mesg=$1; shift
- printf "${YELLOW}==> WARNING:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-error() {
- local mesg=$1; shift
- printf "${RED}==> ERROR:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
-}
-
-stat_busy() {
- local mesg=$1; shift
- printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}...${ALL_OFF}" >&2
-}
-
-stat_done() {
- printf "${BOLD}done${ALL_OFF}\n" >&2
-}
-
-_setup_workdir=false
-setup_workdir() {
- [[ -z $WORKDIR ]] && WORKDIR=$(mktemp -d --tmpdir "${0##*/}.XXXXXXXXXX")
- _setup_workdir=true
- trap 'trap_abort' INT QUIT TERM HUP
- trap 'trap_exit' EXIT
-}
-
-cleanup() {
- if [[ -n $WORKDIR ]] && $_setup_workdir; then
- rm -rf "$WORKDIR"
- fi
- [[ -n $1 ]] && exit $1
-}
-
-abort() {
- msg 'Aborting...'
- cleanup 0
-}
-
-trap_abort() {
- trap - EXIT INT QUIT TERM HUP
- abort
-}
-
-trap_exit() {
- trap - EXIT INT QUIT TERM HUP
- cleanup
-}
-
-die() {
- error "$*"
- cleanup 1
-}
-
-##
-# usage : in_array( $needle, $haystack )
-# return : 0 - found
-# 1 - not found
-##
-in_array() {
- local needle=$1; shift
- local item
- for item in "$@"; do
- [[ $item = $needle ]] && return 0 # Found
- done
- return 1 # Not Found
-}
-
-##
-# usage : lock_open_write( $fd, $path, $wait_message )
-##
-lock_open_write() {
- local fd=$1
- local path=$2
- local msg=$3
-
- # Only reopen the FD if it wasn't handed to us
- if [[ $(readlink -f /dev/fd/$fd) != "${path}.lock" ]]; then
- mkdir -p "${path%/*}"
- eval "exec $fd>${path}.lock"
- fi
-
- if ! flock -n $fd; then
- stat_busy "$msg"
- flock $fd
- stat_done
- fi
-}
-
-##
-# usage : lock_open_read( $fd, $path, $wait_message )
-##
-lock_open_read() {
- local fd=$1
- local path=$2
- local msg=$3
-
- # Only reopen the FD if it wasn't handed to us
- if [[ $(readlink -f /dev/fd/$fd) != "${path}.lock" ]]; then
- mkdir -p "${path%/*}"
- eval "exec $fd>${path}.lock"
- fi
-
- if ! flock -sn $fd; then
- stat_busy "$msg"
- flock -s $fd
- stat_done
- fi
-}
-
-
-##
-# usage : lock_close( $fd )
-##
-lock_close() {
- local fd=$1
- eval "exec $fd>&-"
-}
-
-##
-# usage : get_full_version( [$pkgname] )
-# return : full version spec, including epoch (if necessary), pkgver, pkgrel
-##
-get_full_version() {
- # set defaults if they weren't specified in buildfile
- pkgbase=${pkgbase:-${pkgname[0]}}
- epoch=${epoch:-0}
- if [[ -z $1 ]]; then
- if [[ $epoch ]] && (( ! $epoch )); then
- echo $pkgver-$pkgrel
- else
- echo $epoch:$pkgver-$pkgrel
- fi
- else
- for i in pkgver pkgrel epoch; do
- local indirect="${i}_override"
- eval $(declare -f package_$1 | sed -n "s/\(^[[:space:]]*$i=\)/${i}_override=/p")
- [[ -z ${!indirect} ]] && eval ${indirect}=\"${!i}\"
- done
- if (( ! $epoch_override )); then
- echo $pkgver_override-$pkgrel_override
- else
- echo $epoch_override:$pkgver_override-$pkgrel_override
- fi
- fi
-}
diff --git a/src/devtools/lib/makechrootpkg.sh.patch b/src/devtools/lib/makechrootpkg.sh.patch
new file mode 100644
index 0000000..618d7e9
--- /dev/null
+++ b/src/devtools/lib/makechrootpkg.sh.patch
@@ -0,0 +1,254 @@
+--- makechrootpkg.sh.in
++++ makechrootpkg.sh.ugly
+@@ -12,12 +12,7 @@
+
+ shopt -s nullglob
+
+-# So that usage conflicts between upstream and -par mkarchroot don't get hidden
+-# silently in a merge.
+-archroot() {
+- mkarchroot "$@"
+-}
+-
++init_variables() {
+ makepkg_args='-s --noconfirm -L'
+ repack=false
+ update_first=false
+@@ -28,13 +23,14 @@
+ temp_chroot=false
+ chrootdir=
+ passeddir=
+-declare -a install_pkgs
+-declare -i ret=0
++declare -ag install_pkgs
++declare -ig ret=0
+
+ 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]"
+@@ -69,6 +65,7 @@
+ exit 1
+ }
+
++parse_options_init() {
+ while getopts 'hcudr:I:l:nT' arg; do
+ case "$arg" in
+ h) usage ;;
+@@ -129,8 +126,20 @@
+ # Note this is the same FD number as in mkarchroot
+ lock_open_write 9 "$copydir" \
+ "Waiting for existing lock on chroot copy to be released: [$copy]"
++}
++
++# Usage: chroot_sync $CHROOTDIR/$CHROOT [$CHROOTCOPY|$copydir]
++chroot_sync() {
++ local chrootdir=$1
++ local copy=$2
++ local copydir=''
++ if [[ ${copy:0:1} = / ]]; then
++ copydir=$copy
++ else
++ copydir="$chrootdir/$copy"
++ fi
++ local chroottype=$(stat -f -c %T "$chrootdir")
+
+-if [[ ! -d $copydir ]] || $clean_first; then
+ # Get a read lock on the root chroot to make
+ # sure we don't clone a half-updated chroot
+ lock_open_read 8 "$chrootdir/root" \
+@@ -152,9 +161,16 @@
+
+ # Drop the read lock again
+ lock_close 8
+-fi
++}
++
++# Usage: chroot_install_pkgs $copydir $pkgs...
++chroot_install_pkgs() {
++ local copydir=$1
++ shift
++ declare -i ret=0
++ declare -a install_pkgs=("$@")
++ local pkgname
+
+-if [[ -n "${install_pkgs[*]}" ]]; then
+ for install_pkg in "${install_pkgs[@]}"; do
+ pkgname="${install_pkg##*/}"
+ cp "$install_pkg" "$copydir/$pkgname"
+@@ -165,10 +181,10 @@
+ rm "$copydir/$pkgname"
+ done
+
+- # If there is no PKGBUILD we have done
+- [[ -f PKGBUILD ]] || exit $ret
+-fi
++ return $ret
++}
+
++func1() {
+ $update_first && archroot -u "$copydir"
+
+ mkdir -p "$copydir/build"
+@@ -230,7 +246,14 @@
+ # Set target CARCH as it might be used within the PKGBUILD to select correct sources
+ eval $(grep '^CARCH=' "$copydir/etc/makepkg.conf")
+ export CARCH
++}
+
++# Usage: chroot_copy_in $copydir
++# Environment:
++# - In the dirctory of a PKGBUILD
++# - $SRCDEST is set
++chroot_copy_in() {
++local copydir=$1
+ # Copy PKGBUILD and sources
+ cp PKGBUILD "$copydir/build/"
+ (
+@@ -256,13 +279,23 @@
+ )
+
+ chown -R nobody "$copydir"/{build,pkgdest,srcdest}
++}
+
++# Usage: chroot_let_nobody_use_pacman $copydir
++chroot_let_nobody_use_pacman() {
++local copydir=$1
+ cat > "$copydir/etc/sudoers.d/nobody-pacman" <<EOF
+ Defaults env_keep += "HOME"
+ nobody ALL = NOPASSWD: /usr/bin/pacman
+ EOF
+ chmod 440 "$copydir/etc/sudoers.d/nobody-pacman"
++}
+
++# Usage: chroot_build $copydir $makepkg_args $run_namcap
++chroot_build() {
++local copydir=$1
++local makepkg_args=$2
++local run_namcap=$3
+ # This is a little gross, but this way the script is recreated every time in the
+ # working copy
+ cat >"$copydir/chrootbuild" <<EOF
+@@ -284,37 +317,62 @@
+ exit 0
+ EOF
+ chmod +x "$copydir/chrootbuild"
++archroot -r "$copydir" "/chrootbuild"
++}
+
+-if archroot -r "$copydir" "/chrootbuild"; then
+- for pkgfile in "$copydir"/pkgdest/*.pkg.tar.?z; do
+- if $add_to_db; then
++# Usage: chroot_add_to_local_repo $copydir
++chroot_add_to_local_repo() {
++ local copydir=$1
++ for pkgfile in "$copydir"/pkgdest/*.pkg.tar*; do
+ mkdir -p "$copydir/repo"
+ pushd "$copydir/repo" >/dev/null
+ cp "$pkgfile" .
+ repo-add repo.db.tar.gz "${pkgfile##*/}"
+ popd >/dev/null
+- fi
++ done
++}
+
++# Usage: chroot_copy_out_pkgs $copydir $user
++# Environment:
++# - $PKGDEST is set
++chroot_copy_out_pkgs() {
++ local copydir=$1
++ local src_owner=$2
++ for pkgfile in "$copydir"/pkgdest/*.pkg.tar*; do
+ chown "$src_owner" "$pkgfile"
+ mv "$pkgfile" "$PKGDEST"
++ if [[ $PKGDEST != . ]]; then
++ ln -s "$PKGDEST/${pkgfile##*/}" .
++ fi
+ done
++}
+
+- for l in "$copydir"/build/*-{build,check,namcap,package,package_*}.log; do
++# Usage: chroot_copy_out_pkgs $copydir $user
++chroot_copy_out_logs() {
++ local copydir=$1
++ local src_owner=$2
++ for l in "$copydir"/build/*.log; do
+ chown "$src_owner" "$l"
+ [[ -f $l ]] && mv "$l" .
+ done
+-else
+- # Just in case. We returned 1, make sure we fail
+- ret=1
+-fi
++}
+
++# Usage: chroot_copy_out_srcs $copydir $user
++# Environment:
++# - $SRCDEST is set
++chroot_copy_out_srcs() {
++local copydir=$1
++local src_owner=$2
+ for f in "$copydir"/srcdest/*; do
+ chown "$src_owner" "$f"
+ mv "$f" "$SRCDEST"
+ done
++}
+
+-if $temp_chroot; then
+- stat_busy "Removing temporary directoy [$copy]"
++# Usage: chroot_delete $copydir
++chroot_delete() {
++ local chroottype=$(stat -f -c %T "$copydir")
++ stat_busy "Removing chroot copy [$copy]"
+ if [[ "$chroottype" == btrfs ]]; then
+ btrfs subvolume delete "$copydir" >/dev/null ||
+ die "Unable to delete subvolume $copydir"
+@@ -326,8 +384,39 @@
+ # remove lock file
+ rm --force "$copydir.lock"
+ stat_done
+-elif (( ret != 0 )); then
+- die "Build failed, check $copydir/build"
+-else
+- true
+-fi
++}
++
++main() {
++ init_variables
++ parse_options_init "$@"
++ if [[ ! -d $copydir ]] || $clean_first; then
++ chroot_sync "$chrootdir" "$copy"
++ fi
++ if [[ -n "${install_pkgs[*]}" ]]; then
++ chroot_install_pkgs "$copydir" "${install_pkgs[@]}"
++ ret=$?
++ # If there is no PKGBUILD we have done
++ [[ -f PKGBUILD ]] || exit $ret
++ fi
++ func1
++ chroot_copy_in "$copydir"
++ chroot_let_nobody_use_pacman "$copydir"
++ chroot_build "$copydir" "$makepkg_args" "$run_namcap"
++ func2
++ if chroot_build; then
++ chroot_add_to_local_repo "$copydir"
++ chroot_copy_out_pkgs "$copydir" "$src_owner"
++ chroot_copy_out_logs "$copydir" "$src_owner"
++ else
++ # Just in case. We returned 1, make sure we fail
++ ret=1
++ fi
++ chroot_copy_out_srcs "$copydir" "$src_owner"
++ if $temp_chroot; then
++ chroot_delete "$copydir"
++ elif (( ret != 0 )); then
++ die "Build failed, check $copydir/build"
++ else
++ true
++ fi
++}