diff options
Diffstat (limited to 'libremakepkg')
-rwxr-xr-x | libremakepkg | 219 |
1 files changed, 156 insertions, 63 deletions
diff --git a/libremakepkg b/libremakepkg index 2226f81..13e7617 100755 --- a/libremakepkg +++ b/libremakepkg @@ -3,93 +3,186 @@ # Copyright 2011 Joshua Ismael Haase Hernández # ---------- GNU General Public License 3 ---------- - -# This file is part of Parabola. - -# Parabola is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# Parabola is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with Parabola. If not, see <http://www.gnu.org/licenses/>. + +# This file is part of Parabola. + +# Parabola is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# Parabola is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with Parabola. If not, see <http://www.gnu.org/licenses/>. source /etc/libretools.conf +source /etc/makepkg.conf -function usage { - echo "cd to a dir containing a PKGBUILD and run:" - echo "$0 [options] [makepkg args]" - echo "This script will build your package on a chroot." +function usage { # Display message and exit + + echo 'cd to a dir containing a PKGBUILD and run:' + echo '$0 [options] [makepkg args]' + echo 'This script will build your package on a chroot.' + echo + echo 'OPTIONS:' echo - echo "OPTIONS:" - echo - echo " -h : show this message." - echo " -c : cleans CHCOPY before building." - echo " -u : updates CHROOT before building." - echo " -n : use this dir instead of CHCOPY." - echo " -M \"makepkg long arg\" : passes long args to makepkg, use it as many times as needed." - echo " -I pkgname : install this package, use it as many times needed." + echo ' -h show this message.' + echo ' -c cleans the chroot before building.' + echo ' -u updates the chroot before building.' + echo ' -n use this dir instead of "${CHCOPY}".' + echo ' -M <--arg> passes long args to makepkg, use it as many times as needed.' echo + exit 1 +} + +function buildenv { # Mounts *DEST from makepkg.conf + + msg "Building env" + for mp in ${SRCDEST} ${PKGDEST} ${SRCPKGDEST}; do + msg2 "binding ${mp} to ${CHROOTDIR}/${CHCOPY}${mp}" + mkdir -p "${CHROOTDIR}/${CHCOPY}${mp}" + mount -o bind ${mp} "${CHROOTDIR}/${CHCOPY}${mp}" || exit 1 + done + +} + +function clean_chroot { # Clean packages with pacman + + plain "making list of packages in ${CHROOTDIR}/${CHROOTNAME}/root/" + cp "/etc/libretools.d/cleansystem" "${CHROOTDIR}/${CHROOTNAME}/root/cleansystem" + (cat <<EOF +#!/bin/bash +export LANG=C + +clean='false' + +while [ "\$clean" = 'false' ]; do + + pkgs=(\$(comm -23 <(pacman -Qq | sort) <(sort /root/cleansystem))) + + if [ \${#pkgs[@]} -gt 0 ]; then + pacman --noconfirm -Rcs \${pkgs[@]} + else + clean="true" + echo "clean" + fi +done +EOF + ) > "${CHROOTDIR}/${CHROOTNAME}/clean" + chmod +x "${CHROOTDIR}/${CHROOTNAME}/clean" + mkarchroot -r "/clean" "${CHROOTDIR}/${CHROOTNAME}" + +} + +function copy_log { # copy logs if they exist + + if [ "${USE_LOG}" == 'y' ]; then + find ${CHROOTDIR}/${CHROOTNAME}/build/ -name "*\.log" -exec cp {} ./ \; + fi + +} + +function trap_exit { # End inmediately but print a useful message + +# args are treated as part of the message + + for mp in ${SRCDEST} ${PKGDEST} ${SRCPKGDEST}; do + umount "${CHROOTDIR}/${CHCOPY}${mp}" + done + + copy_log + + error "$@" + + exit 1 } -_CLEAN="" +# Trap signals from makepkg +set -E +trap 'trap_exit "(libremakepkg): TERM signal caught. Exiting..."' TERM HUP QUIT +trap 'trap_exit "(libremakepkg): Aborted by user! Exiting..."' INT +trap 'trap_exit "(libremakepkg): An unknown error has occurred. Exiting..."' ERR + +CLEAN_FIRST="n" CLEAN_CACHE="" -update_first="n" -use_log='n' -chrootname=${CHCOPY} -_PKGINSTALL="" -_MAKEPKG_ARGS="" -#libremakepkg own args -libremakepkgargs='hcun:I:M:' -#now makepkg args -libremakepkgargs+='ACdefiLmop:rRs' +UPDATE_FIRST="n" +USE_LOG='n' +CHROOTNAME=${CHCOPY} +MAKEPKG_ARGS="" + +libremakepkgargs='hcuUn:I:M:' # libremakepkg own args +libremakepkgargs+='ACdefiLmop:rRs' # makepkg args while getopts ${libremakepkgargs} arg ; do case "${arg}" in - h) usage; exit 0 ;; - c) _CLEAN="-c" ;; - u) update_first="y" ;; - n) chrootname="$OPTARG"; echo $chrootname ;; - I) _PKGINSTALL+="-I $OPTARG " ;; - M) _MAKEPKG_ARGS+=" $OPTARG" ;; - L) _MAKEPKG_ARGS+=" -$arg $OPTARG" - use_log='y';; - *) _MAKEPKG_ARGS+=" -$arg $OPTARG" ;; + h) usage ;; + c) CLEAN_FIRST="y" ;; + u) UPDATE_FIRST="y" ;; + n) CHROOTNAME="$OPTARG" ;; + M) MAKEPKG_ARGS+=" $OPTARG" ;; + L) MAKEPKG_ARGS+=" -$arg $OPTARG" + USE_LOG='y';; + *) MAKEPKG_ARGS+=" -$arg $OPTARG" ;; esac done -if [ ! -w / ]; then +if [ ${UID} -ne 0 ]; then error "This script must be run as root" exit 1 fi +if [ ! -r PKGBUILD ]; then # Check if we are actually on a build directory. Do this early. + + error "This isn't a build directory"; usage + +fi + msg "Checking PKGBUILD for non-free issues" -pkgbuild-check-nonfree ||{ -# pkgbuild-check-nonfree uses 15 for nonfree and -# other errors if something failed. - if [[ $? -eq 15 ]]; then - error "PKGBUILD contains non-free issues" - exit 15 +if ! pkgbuild-check-nonfree; then + + if [[ $? -eq 15 ]]; then # other errors mean fail, not nonfree + error "PKGBUILD contains non-free issues" + exit 15 + else + true fi -} -if [ $update_first = y ]; then - msg "Updating the main chroot" -# -c option in mkarchroot indicates cache - mkarchroot -c ${CACHEDIR} -u "${CHROOTDIR}/${CHROOT}" fi +buildenv msg "Creating the package" -makechrootpkg $_CLEAN -r ${CHROOTDIR} -l "${chrootname}" $_PKGINSTALL -- $_MAKEPKG_ARGS -ev=$? # exit value +if [ -d "${CHROOTDIR}/${CHROOTNAME}" ]; then # use chroot + + if [ "${UPDATE_FIRST}" = 'y' ]; then + msg "Updating the chroot in use..." + mkarchroot -c ${CACHEDIR} -u "${CHROOTDIR}/${CHROOTNAME}" # -c option is for cache + fi + + if [ "${CLEAN_FIRST}" = 'y' ]; then + msg "Cleaning" + clean_chroot + fi + + makechrootpkg -r "${CHROOTDIR}" -l "${CHROOTNAME}" -- "${MAKEPKG_ARGS}" + ev=$? # exit value + +else # build new chroot before using + + if [ "${UPDATE_FIRST}" = 'y' ]; then # update CHROOT + msg "Updating the chroot in use..." + mkarchroot -c ${CACHEDIR} -u "${CHROOTDIR}/${CHROOT}" # -c option is for cache + fi + + makechrootpkg -c -r "${CHROOTDIR}" -l "${CHROOTNAME}" -- "${MAKEPKG_ARGS}" + ev=$? # exit value + +fi + +copy_log -[ "$use_log" == 'y' -a -e ${CHROOTDIR}/${chrootname}/build/*.log ] && { - cp ${CHROOTDIR}/${chrootname}/build/*.log ./ -} exit $ev |