[pacman-dev] [PATCH 6/6] makepkg: make built script sourceable

Dave Reisner dreisner at archlinux.org
Thu Aug 7 19:22:26 EDT 2014


Aside from the hack at the foot of the script, this commit introduces no
logical changes or new code. Any globally scoped declaration or code are
moved into a 'makepkg_main' function which is called when the script is
executed (not sourced).

Doing this allows other scripts to source makepkg for its juicy
functions, and also makes these functions more easily unit testable.

This commit explicitly does NOT attempt to define an API or any other
stability guarantees of any kind.
---
 scripts/makepkg.sh.in | 985 +++++++++++++++++++++++++-------------------------
 1 file changed, 496 insertions(+), 489 deletions(-)

diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
index 34ffb25..4b23a5b 100644
--- a/scripts/makepkg.sh.in
+++ b/scripts/makepkg.sh.in
@@ -30,72 +30,6 @@
 #   awk, bsdtar (libarchive), bzip2, coreutils, fakeroot, file, find (findutils),
 #   gettext, gpg, grep, gzip, openssl, sed, tput (ncurses), xz
 
-# gettext initialization
-export TEXTDOMAIN='pacman-scripts'
-export TEXTDOMAINDIR='@localedir@'
-
-# file -i does not work on Mac OSX unless legacy mode is set
-export COMMAND_MODE='legacy'
-# Ensure CDPATH doesn't screw with our cd calls
-unset CDPATH
-# Ensure GREP_OPTIONS doesn't screw with our grep calls
-unset GREP_OPTIONS
-
-declare -r makepkg_version='@PACKAGE_VERSION@'
-declare -r confdir='@sysconfdir@'
-declare -r BUILDSCRIPT='@BUILDSCRIPT@'
-declare -r startdir="$PWD"
-
-LIBRARY=${LIBRARY:-'@libmakepkgdir@'}
-
-packaging_options=('strip' 'docs' 'libtool' 'staticlibs' 'emptydirs' 'zipman'
-                   'purge' 'upx' 'debug')
-other_options=('ccache' 'distcc' 'buildflags' 'makeflags')
-splitpkg_overrides=('pkgdesc' 'arch' 'url' 'license' 'groups' 'depends'
-                    'optdepends' 'provides' 'conflicts' 'replaces' 'backup'
-                    'options' 'install' 'changelog')
-readonly -a packaging_options other_options splitpkg_overrides
-
-known_hash_algos=('md5' 'sha1' 'sha224' 'sha256' 'sha384' 'sha512')
-
-# Options
-ASDEPS=0
-BUILDFUNC=0
-CHECKFUNC=0
-CLEANBUILD=0
-CLEANUP=0
-DEP_BIN=0
-FORCE=0
-GENINTEG=0
-HOLDVER=0
-IGNOREARCH=0
-INFAKEROOT=0
-INSTALL=0
-LOGGING=0
-NEEDED=0
-NOARCHIVE=0
-NOBUILD=0
-NODEPS=0
-NOEXTRACT=0
-PKGFUNC=0
-PKGLIST=()
-PKGVERFUNC=0
-PREPAREFUNC=0
-REPKG=0
-RMDEPS=0
-SKIPCHECKSUMS=0
-SKIPPGPCHECK=0
-SIGNPKG=''
-SPLITPKG=0
-SOURCEONLY=0
-VERIFYSOURCE=0
-
-# Forces the pkgver of the current PKGBUILD. Used by the fakeroot call
-# when dealing with svn/cvs/etc PKGBUILDs.
-FORCE_VER=""
-
-PACMAN_OPTS=
-
 shopt -s extglob
 
 ### SUBROUTINES ###
@@ -2883,499 +2817,572 @@ This is free software; see the source for copying conditions.\n\
 There is NO WARRANTY, to the extent permitted by law.\n")"
 }
 
-# PROGRAM START
-
-# ensure we have a sane umask set
-umask 0022
-
-# determine whether we have gettext; make it a no-op if we do not
-if ! type -p gettext >/dev/null; then
-	gettext() {
-		printf "%s\n" "$@"
-	}
-fi
+makepkg_main() {
+	# gettext initialization
+	export TEXTDOMAIN='pacman-scripts'
+	export TEXTDOMAINDIR='@localedir@'
+
+	# file -i does not work on Mac OSX unless legacy mode is set
+	export COMMAND_MODE='legacy'
+	# Ensure CDPATH doesn't screw with our cd calls
+	unset CDPATH
+	# Ensure GREP_OPTIONS doesn't screw with our grep calls
+	unset GREP_OPTIONS
+
+	declare -r makepkg_version='@PACKAGE_VERSION@'
+	declare -r confdir='@sysconfdir@'
+	declare -r BUILDSCRIPT='@BUILDSCRIPT@'
+	declare -r startdir="$PWD"
+
+	LIBRARY=${LIBRARY:-'@libmakepkgdir@'}
+
+	packaging_options=('strip' 'docs' 'libtool' 'staticlibs' 'emptydirs' 'zipman'
+										 'purge' 'upx' 'debug')
+	other_options=('ccache' 'distcc' 'buildflags' 'makeflags')
+	splitpkg_overrides=('pkgdesc' 'arch' 'url' 'license' 'groups' 'depends'
+											'optdepends' 'provides' 'conflicts' 'replaces' 'backup'
+											'options' 'install' 'changelog')
+	readonly -a packaging_options other_options splitpkg_overrides
+
+	known_hash_algos=('md5' 'sha1' 'sha224' 'sha256' 'sha384' 'sha512')
+
+	# Options
+	ASDEPS=0
+	BUILDFUNC=0
+	CHECKFUNC=0
+	CLEANBUILD=0
+	CLEANUP=0
+	DEP_BIN=0
+	FORCE=0
+	GENINTEG=0
+	HOLDVER=0
+	IGNOREARCH=0
+	INFAKEROOT=0
+	INSTALL=0
+	LOGGING=0
+	NEEDED=0
+	NOARCHIVE=0
+	NOBUILD=0
+	NODEPS=0
+	NOEXTRACT=0
+	PKGFUNC=0
+	PKGLIST=()
+	PKGVERFUNC=0
+	PREPAREFUNC=0
+	REPKG=0
+	RMDEPS=0
+	SKIPCHECKSUMS=0
+	SKIPPGPCHECK=0
+	SIGNPKG=''
+	SPLITPKG=0
+	SOURCEONLY=0
+	VERIFYSOURCE=0
+
+	# Forces the pkgver of the current PKGBUILD. Used by the fakeroot call
+	# when dealing with svn/cvs/etc PKGBUILDs.
+	FORCE_VER=""
+
+	PACMAN_OPTS=
+
+	# ensure we have a sane umask set
+	umask 0022
+
+	# determine whether we have gettext; make it a no-op if we do not
+	if ! type -p gettext >/dev/null; then
+		gettext() {
+			printf "%s\n" "$@"
+		}
+	fi
+
+	ARGLIST=("$@")
+
+	# Parse Command Line Options.
+	OPT_SHORT="AcCdefFghiLmop:rRsSV"
+	OPT_LONG=('allsource' 'check' 'clean' 'cleanbuild' 'config:' 'force' 'geninteg'
+						'help' 'holdver' 'ignorearch' 'install' 'key:' 'log' 'noarchive' 'nobuild'
+						'nocolor' 'nocheck' 'nodeps' 'noextract' 'noprepare' 'nosign' 'pkg:' 'repackage'
+						'rmdeps' 'sign' 'skipchecksums' 'skipinteg' 'skippgpcheck' 'source' 'syncdeps'
+						'verifysource' 'version')
+
+	# Pacman Options
+	OPT_LONG+=('asdeps' 'noconfirm' 'needed' 'noprogressbar')
+
+	if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
+		exit 1 # E_INVALID_OPTION;
+	fi
+	set -- "${OPTRET[@]}"
+	unset OPT_SHORT OPT_LONG OPTRET
+
+	while true; do
+		case "$1" in
+			# Pacman Options
+			--asdeps)         ASDEPS=1;;
+			--needed)         NEEDED=1;;
+			--noconfirm)      PACMAN_OPTS+=" --noconfirm" ;;
+			--noprogressbar)  PACMAN_OPTS+=" --noprogressbar" ;;
+
+			# Makepkg Options
+			--allsource)      SOURCEONLY=2 ;;
+			-A|--ignorearch)  IGNOREARCH=1 ;;
+			-c|--clean)       CLEANUP=1 ;;
+			-C|--cleanbuild)  CLEANBUILD=1 ;;
+			--check)          RUN_CHECK='y' ;;
+			--config)         shift; MAKEPKG_CONF=$1 ;;
+			-d|--nodeps)      NODEPS=1 ;;
+			-e|--noextract)   NOEXTRACT=1 ;;
+			-f|--force)       FORCE=1 ;;
+			-F)               INFAKEROOT=1 ;;
+			-g|--geninteg)    GENINTEG=1 ;;
+			--holdver)        HOLDVER=1 ;;
+			-i|--install)     INSTALL=1 ;;
+			--key)            shift; GPGKEY=$1 ;;
+			-L|--log)         LOGGING=1 ;;
+			-m|--nocolor)     USE_COLOR='n'; PACMAN_OPTS+=" --color never" ;;
+			--noarchive)      NOARCHIVE=1 ;;
+			--nocheck)        RUN_CHECK='n' ;;
+			--noprepare)      RUN_PREPARE='n' ;;
+			--nosign)         SIGNPKG='n' ;;
+			-o|--nobuild)     NOBUILD=1 ;;
+			-p)               shift; BUILDFILE=$1 ;;
+			--pkg)            shift; IFS=, read -ra p <<<"$1"; PKGLIST+=("${p[@]}"); unset p ;;
+			-r|--rmdeps)      RMDEPS=1 ;;
+			-R|--repackage)   REPKG=1 ;;
+			--sign)           SIGNPKG='y' ;;
+			--skipchecksums)  SKIPCHECKSUMS=1 ;;
+			--skipinteg)      SKIPCHECKSUMS=1; SKIPPGPCHECK=1 ;;
+			--skippgpcheck)   SKIPPGPCHECK=1;;
+			-s|--syncdeps)    DEP_BIN=1 ;;
+			-S|--source)      SOURCEONLY=1 ;;
+			--verifysource)   VERIFYSOURCE=1 ;;
+
+			-h|--help)        usage; exit 0 ;; # E_OK
+			-V|--version)     version; exit 0 ;; # E_OK
+
+			--)               OPT_IND=0; shift; break 2;;
+		esac
+		shift
+	done
 
-ARGLIST=("$@")
+	# attempt to consume any extra argv as environment variables. this supports
+	# overriding (e.g. CC=clang) as well as overriding (e.g. CFLAGS+=' -g').
+	extra_environment=()
+	while [[ $1 ]]; do
+		if [[ $1 = [_[:alpha:]]*([[:alnum:]_])?(+)=* ]]; then
+			extra_environment+=("$1")
+		fi
+		shift
+	done
 
-# Parse Command Line Options.
-OPT_SHORT="AcCdefFghiLmop:rRsSV"
-OPT_LONG=('allsource' 'check' 'clean' 'cleanbuild' 'config:' 'force' 'geninteg'
-          'help' 'holdver' 'ignorearch' 'install' 'key:' 'log' 'noarchive' 'nobuild'
-          'nocolor' 'nocheck' 'nodeps' 'noextract' 'noprepare' 'nosign' 'pkg:' 'repackage'
-          'rmdeps' 'sign' 'skipchecksums' 'skipinteg' 'skippgpcheck' 'source' 'syncdeps'
-          'verifysource' 'version')
+	# setup signal traps
+	trap 'clean_up' 0
+	for signal in TERM HUP QUIT; do
+		trap "trap_exit $signal \"$(gettext "%s signal caught. Exiting...")\" \"$signal\"" "$signal"
+	done
+	trap 'trap_exit INT "$(gettext "Aborted by user! Exiting...")"' INT
+	trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' ERR
+
+	# preserve environment variables and canonicalize path
+	[[ -n ${PKGDEST} ]] && _PKGDEST=$(canonicalize_path ${PKGDEST})
+	[[ -n ${SRCDEST} ]] && _SRCDEST=$(canonicalize_path ${SRCDEST})
+	[[ -n ${SRCPKGDEST} ]] && _SRCPKGDEST=$(canonicalize_path ${SRCPKGDEST})
+	[[ -n ${LOGDEST} ]] && _LOGDEST=$(canonicalize_path ${LOGDEST})
+	[[ -n ${BUILDDIR} ]] && _BUILDDIR=$(canonicalize_path ${BUILDDIR})
+	[[ -n ${PKGEXT} ]] && _PKGEXT=${PKGEXT}
+	[[ -n ${SRCEXT} ]] && _SRCEXT=${SRCEXT}
+	[[ -n ${GPGKEY} ]] && _GPGKEY=${GPGKEY}
+	[[ -n ${PACKAGER} ]] && _PACKAGER=${PACKAGER}
+	[[ -n ${CARCH} ]] && _CARCH=${CARCH}
+
+	# default config is makepkg.conf
+	MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf}
+
+	# Source the config file; fail if it is not found
+	if [[ -r $MAKEPKG_CONF ]]; then
+		source_safe "$MAKEPKG_CONF"
+	else
+		error "$(gettext "%s not found.")" "$MAKEPKG_CONF"
+		plain "$(gettext "Aborting...")"
+		exit 1 # $E_CONFIG_ERROR
+	fi
 
-# Pacman Options
-OPT_LONG+=('asdeps' 'noconfirm' 'needed' 'noprogressbar')
+	# Source user-specific makepkg.conf overrides, but only if no override config
+	# file was specified
+	XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman"
+	if [[ "$MAKEPKG_CONF" = "$confdir/makepkg.conf" ]]; then
+		if [[ -r "$XDG_PACMAN_DIR/makepkg.conf" ]]; then
+			source_safe "$XDG_PACMAN_DIR/makepkg.conf"
+		elif [[ -r "$HOME/.makepkg.conf" ]]; then
+			source_safe "$HOME/.makepkg.conf"
+		fi
+	fi
+
+	# set pacman command if not already defined
+	PACMAN=${PACMAN:-pacman}
+	# save full path to command as PATH may change when sourcing /etc/profile
+	PACMAN_PATH=$(type -P $PACMAN)
+
+	# check if messages are to be printed using color
+	unset ALL_OFF BOLD BLUE GREEN RED YELLOW
+	if [[ -t 2 && ! $USE_COLOR = "n" ]] && check_buildenv "color" "y"; 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[0m"
+			BOLD="\e[1m"
+			BLUE="${BOLD}\e[34m"
+			GREEN="${BOLD}\e[32m"
+			RED="${BOLD}\e[31m"
+			YELLOW="${BOLD}\e[33m"
+		fi
+	fi
+	readonly ALL_OFF BOLD BLUE GREEN RED YELLOW
+
+	# override settings with an environment variable for batch processing
+	BUILDDIR=${_BUILDDIR:-$BUILDDIR}
+	BUILDDIR=${BUILDDIR:-$startdir} #default to $startdir if undefined
+	if [[ ! -d $BUILDDIR ]]; then
+		if ! mkdir -p "$BUILDDIR"; then
+			error "$(gettext "You do not have write permission to create packages in %s.")" "$BUILDDIR"
+			plain "$(gettext "Aborting...")"
+			exit 1
+		fi
+		chmod a-s "$BUILDDIR"
+	fi
+	if [[ ! -w $BUILDDIR ]]; then
+		error "$(gettext "You do not have write permission to create packages in %s.")" "$BUILDDIR"
+		plain "$(gettext "Aborting...")"
+		exit 1
+	fi
 
-if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
-	exit 1 # E_INVALID_OPTION;
-fi
-set -- "${OPTRET[@]}"
-unset OPT_SHORT OPT_LONG OPTRET
-
-while true; do
-	case "$1" in
-		# Pacman Options
-		--asdeps)         ASDEPS=1;;
-		--needed)         NEEDED=1;;
-		--noconfirm)      PACMAN_OPTS+=" --noconfirm" ;;
-		--noprogressbar)  PACMAN_OPTS+=" --noprogressbar" ;;
-
-		# Makepkg Options
-		--allsource)      SOURCEONLY=2 ;;
-		-A|--ignorearch)  IGNOREARCH=1 ;;
-		-c|--clean)       CLEANUP=1 ;;
-		-C|--cleanbuild)  CLEANBUILD=1 ;;
-		--check)          RUN_CHECK='y' ;;
-		--config)         shift; MAKEPKG_CONF=$1 ;;
-		-d|--nodeps)      NODEPS=1 ;;
-		-e|--noextract)   NOEXTRACT=1 ;;
-		-f|--force)       FORCE=1 ;;
-		-F)               INFAKEROOT=1 ;;
-		-g|--geninteg)    GENINTEG=1 ;;
-		--holdver)        HOLDVER=1 ;;
-		-i|--install)     INSTALL=1 ;;
-		--key)            shift; GPGKEY=$1 ;;
-		-L|--log)         LOGGING=1 ;;
-		-m|--nocolor)     USE_COLOR='n'; PACMAN_OPTS+=" --color never" ;;
-		--noarchive)      NOARCHIVE=1 ;;
-		--nocheck)        RUN_CHECK='n' ;;
-		--noprepare)      RUN_PREPARE='n' ;;
-		--nosign)         SIGNPKG='n' ;;
-		-o|--nobuild)     NOBUILD=1 ;;
-		-p)               shift; BUILDFILE=$1 ;;
-		--pkg)            shift; IFS=, read -ra p <<<"$1"; PKGLIST+=("${p[@]}"); unset p ;;
-		-r|--rmdeps)      RMDEPS=1 ;;
-		-R|--repackage)   REPKG=1 ;;
-		--sign)           SIGNPKG='y' ;;
-		--skipchecksums)  SKIPCHECKSUMS=1 ;;
-		--skipinteg)      SKIPCHECKSUMS=1; SKIPPGPCHECK=1 ;;
-		--skippgpcheck)   SKIPPGPCHECK=1;;
-		-s|--syncdeps)    DEP_BIN=1 ;;
-		-S|--source)      SOURCEONLY=1 ;;
-		--verifysource)   VERIFYSOURCE=1 ;;
-
-		-h|--help)        usage; exit 0 ;; # E_OK
-		-V|--version)     version; exit 0 ;; # E_OK
-
-		--)               OPT_IND=0; shift; break 2;;
-	esac
-	shift
-done
-
-# attempt to consume any extra argv as environment variables. this supports
-# overriding (e.g. CC=clang) as well as overriding (e.g. CFLAGS+=' -g').
-extra_environment=()
-while [[ $1 ]]; do
-	if [[ $1 = [_[:alpha:]]*([[:alnum:]_])?(+)=* ]]; then
-		extra_environment+=("$1")
-	fi
-	shift
-done
-
-# setup signal traps
-trap 'clean_up' 0
-for signal in TERM HUP QUIT; do
-	trap "trap_exit $signal \"$(gettext "%s signal caught. Exiting...")\" \"$signal\"" "$signal"
-done
-trap 'trap_exit INT "$(gettext "Aborted by user! Exiting...")"' INT
-trap 'trap_exit USR1 "$(gettext "An unknown error has occurred. Exiting...")"' ERR
-
-# preserve environment variables and canonicalize path
-[[ -n ${PKGDEST} ]] && _PKGDEST=$(canonicalize_path ${PKGDEST})
-[[ -n ${SRCDEST} ]] && _SRCDEST=$(canonicalize_path ${SRCDEST})
-[[ -n ${SRCPKGDEST} ]] && _SRCPKGDEST=$(canonicalize_path ${SRCPKGDEST})
-[[ -n ${LOGDEST} ]] && _LOGDEST=$(canonicalize_path ${LOGDEST})
-[[ -n ${BUILDDIR} ]] && _BUILDDIR=$(canonicalize_path ${BUILDDIR})
-[[ -n ${PKGEXT} ]] && _PKGEXT=${PKGEXT}
-[[ -n ${SRCEXT} ]] && _SRCEXT=${SRCEXT}
-[[ -n ${GPGKEY} ]] && _GPGKEY=${GPGKEY}
-[[ -n ${PACKAGER} ]] && _PACKAGER=${PACKAGER}
-[[ -n ${CARCH} ]] && _CARCH=${CARCH}
-
-# default config is makepkg.conf
-MAKEPKG_CONF=${MAKEPKG_CONF:-$confdir/makepkg.conf}
-
-# Source the config file; fail if it is not found
-if [[ -r $MAKEPKG_CONF ]]; then
-	source_safe "$MAKEPKG_CONF"
-else
-	error "$(gettext "%s not found.")" "$MAKEPKG_CONF"
-	plain "$(gettext "Aborting...")"
-	exit 1 # $E_CONFIG_ERROR
-fi
+	# override settings from extra variables on commandline, if any
+	if (( ${#extra_environment[*]} )); then
+		export "${extra_environment[@]}"
+	fi
 
-# Source user-specific makepkg.conf overrides, but only if no override config
-# file was specified
-XDG_PACMAN_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pacman"
-if [[ "$MAKEPKG_CONF" = "$confdir/makepkg.conf" ]]; then
-	if [[ -r "$XDG_PACMAN_DIR/makepkg.conf" ]]; then
-		source_safe "$XDG_PACMAN_DIR/makepkg.conf"
-	elif [[ -r "$HOME/.makepkg.conf" ]]; then
-		source_safe "$HOME/.makepkg.conf"
+	PKGDEST=${_PKGDEST:-$PKGDEST}
+	PKGDEST=${PKGDEST:-$startdir} #default to $startdir if undefined
+	if (( ! (NOBUILD || GENINTEG) )) && [[ ! -w $PKGDEST ]]; then
+		error "$(gettext "You do not have write permission to store packages in %s.")" "$PKGDEST"
+		plain "$(gettext "Aborting...")"
+		exit 1
 	fi
-fi
 
-# set pacman command if not already defined
-PACMAN=${PACMAN:-pacman}
-# save full path to command as PATH may change when sourcing /etc/profile
-PACMAN_PATH=$(type -P $PACMAN)
-
-# check if messages are to be printed using color
-unset ALL_OFF BOLD BLUE GREEN RED YELLOW
-if [[ -t 2 && ! $USE_COLOR = "n" ]] && check_buildenv "color" "y"; 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[0m"
-		BOLD="\e[1m"
-		BLUE="${BOLD}\e[34m"
-		GREEN="${BOLD}\e[32m"
-		RED="${BOLD}\e[31m"
-		YELLOW="${BOLD}\e[33m"
+	SRCDEST=${_SRCDEST:-$SRCDEST}
+	SRCDEST=${SRCDEST:-$startdir} #default to $startdir if undefined
+	if [[ ! -w $SRCDEST ]] ; then
+		error "$(gettext "You do not have write permission to store downloads in %s.")" "$SRCDEST"
+		plain "$(gettext "Aborting...")"
+		exit 1
 	fi
-fi
-readonly ALL_OFF BOLD BLUE GREEN RED YELLOW
 
-# override settings with an environment variable for batch processing
-BUILDDIR=${_BUILDDIR:-$BUILDDIR}
-BUILDDIR=${BUILDDIR:-$startdir} #default to $startdir if undefined
-if [[ ! -d $BUILDDIR ]]; then
-	if ! mkdir -p "$BUILDDIR"; then
-		error "$(gettext "You do not have write permission to create packages in %s.")" "$BUILDDIR"
+	SRCPKGDEST=${_SRCPKGDEST:-$SRCPKGDEST}
+	SRCPKGDEST=${SRCPKGDEST:-$startdir} #default to $startdir if undefined
+	if (( SOURCEONLY )) && [[ ! -w $SRCPKGDEST ]]; then
+		error "$(gettext "You do not have write permission to store source tarballs in %s.")" "$SRCPKGDEST"
 		plain "$(gettext "Aborting...")"
 		exit 1
 	fi
-	chmod a-s "$BUILDDIR"
-fi
-if [[ ! -w $BUILDDIR ]]; then
-	error "$(gettext "You do not have write permission to create packages in %s.")" "$BUILDDIR"
-	plain "$(gettext "Aborting...")"
-	exit 1
-fi
 
-# override settings from extra variables on commandline, if any
-if (( ${#extra_environment[*]} )); then
-	export "${extra_environment[@]}"
-fi
+	LOGDEST=${_LOGDEST:-$LOGDEST}
+	LOGDEST=${LOGDEST:-$startdir} #default to $startdir if undefined
+	if (( LOGGING )) && [[ ! -w $LOGDEST ]]; then
+		error "$(gettext "You do not have write permission to store logs in %s.")" "$LOGDEST"
+		plain "$(gettext "Aborting...")"
+		exit 1
+	fi
 
-PKGDEST=${_PKGDEST:-$PKGDEST}
-PKGDEST=${PKGDEST:-$startdir} #default to $startdir if undefined
-if (( ! (NOBUILD || GENINTEG) )) && [[ ! -w $PKGDEST ]]; then
-	error "$(gettext "You do not have write permission to store packages in %s.")" "$PKGDEST"
-	plain "$(gettext "Aborting...")"
-	exit 1
-fi
+	PKGEXT=${_PKGEXT:-$PKGEXT}
+	SRCEXT=${_SRCEXT:-$SRCEXT}
+	GPGKEY=${_GPGKEY:-$GPGKEY}
+	PACKAGER=${_PACKAGER:-$PACKAGER}
+	CARCH=${_CARCH:-$CARCH}
 
-SRCDEST=${_SRCDEST:-$SRCDEST}
-SRCDEST=${SRCDEST:-$startdir} #default to $startdir if undefined
-if [[ ! -w $SRCDEST ]] ; then
-	error "$(gettext "You do not have write permission to store downloads in %s.")" "$SRCDEST"
-	plain "$(gettext "Aborting...")"
-	exit 1
-fi
+	if (( ! INFAKEROOT )); then
+		if (( EUID == 0 )); then
+			error "$(gettext "Running %s as root is not allowed as it can cause permanent,\n\
+	catastrophic damage to your system.")"
+			exit 1 # $E_USER_ABORT
+		fi
+	else
+		if [[ -z $FAKEROOTKEY ]]; then
+			error "$(gettext "Do not use the %s option. This option is only for use by %s.")" "'-F'" "makepkg"
+			exit 1 # TODO: error code
+		fi
+	fi
 
-SRCPKGDEST=${_SRCPKGDEST:-$SRCPKGDEST}
-SRCPKGDEST=${SRCPKGDEST:-$startdir} #default to $startdir if undefined
-if (( SOURCEONLY )) && [[ ! -w $SRCPKGDEST ]]; then
-	error "$(gettext "You do not have write permission to store source tarballs in %s.")" "$SRCPKGDEST"
-	plain "$(gettext "Aborting...")"
-	exit 1
-fi
+	unset pkgname pkgbase pkgver pkgrel epoch pkgdesc url license groups provides
+	unset md5sums replaces depends conflicts backup source install changelog build
+	unset makedepends optdepends options noextract validpgpkeys
 
-LOGDEST=${_LOGDEST:-$LOGDEST}
-LOGDEST=${LOGDEST:-$startdir} #default to $startdir if undefined
-if (( LOGGING )) && [[ ! -w $LOGDEST ]]; then
-	error "$(gettext "You do not have write permission to store logs in %s.")" "$LOGDEST"
-	plain "$(gettext "Aborting...")"
-	exit 1
-fi
+	BUILDFILE=${BUILDFILE:-$BUILDSCRIPT}
+	if [[ ! -f $BUILDFILE ]]; then
+		error "$(gettext "%s does not exist.")" "$BUILDFILE"
+		exit 1
+	else
+		if [[ $(<"$BUILDFILE") = *$'\r'* ]]; then
+			error "$(gettext "%s contains %s characters and cannot be sourced.")" "$BUILDFILE" "CRLF"
+			exit 1
+		fi
 
-PKGEXT=${_PKGEXT:-$PKGEXT}
-SRCEXT=${_SRCEXT:-$SRCEXT}
-GPGKEY=${_GPGKEY:-$GPGKEY}
-PACKAGER=${_PACKAGER:-$PACKAGER}
-CARCH=${_CARCH:-$CARCH}
+		if [[ ! $BUILDFILE -ef $PWD/${BUILDFILE##*/} ]]; then
+			error "$(gettext "%s must be in the current working directory.")" "$BUILDFILE"
+			exit 1
+		fi
 
-if (( ! INFAKEROOT )); then
-	if (( EUID == 0 )); then
-		error "$(gettext "Running %s as root is not allowed as it can cause permanent,\n\
-catastrophic damage to your system.")"
-		exit 1 # $E_USER_ABORT
-	fi
-else
-	if [[ -z $FAKEROOTKEY ]]; then
-		error "$(gettext "Do not use the %s option. This option is only for use by %s.")" "'-F'" "makepkg"
-		exit 1 # TODO: error code
+		if [[ ${BUILDFILE:0:1} != "/" ]]; then
+			BUILDFILE="$startdir/$BUILDFILE"
+		fi
+		source_buildfile "$BUILDFILE"
 	fi
-fi
 
-unset pkgname pkgbase pkgver pkgrel epoch pkgdesc url license groups provides
-unset md5sums replaces depends conflicts backup source install changelog build
-unset makedepends optdepends options noextract validpgpkeys
-
-BUILDFILE=${BUILDFILE:-$BUILDSCRIPT}
-if [[ ! -f $BUILDFILE ]]; then
-	error "$(gettext "%s does not exist.")" "$BUILDFILE"
-	exit 1
-else
-	if [[ $(<"$BUILDFILE") = *$'\r'* ]]; then
-		error "$(gettext "%s contains %s characters and cannot be sourced.")" "$BUILDFILE" "CRLF"
-		exit 1
-	fi
+	# set defaults if they weren't specified in buildfile
+	pkgbase=${pkgbase:-${pkgname[0]}}
+	epoch=${epoch:-0}
+	basever=$(get_full_version)
 
-	if [[ ! $BUILDFILE -ef $PWD/${BUILDFILE##*/} ]]; then
-		error "$(gettext "%s must be in the current working directory.")" "$BUILDFILE"
-		exit 1
-	fi
+	if [[ $BUILDDIR = "$startdir" ]]; then
+		srcdir="$BUILDDIR/src"
+		pkgdirbase="$BUILDDIR/pkg"
+	else
+		srcdir="$BUILDDIR/$pkgbase/src"
+		pkgdirbase="$BUILDDIR/$pkgbase/pkg"
 
-	if [[ ${BUILDFILE:0:1} != "/" ]]; then
-		BUILDFILE="$startdir/$BUILDFILE"
 	fi
-	source_buildfile "$BUILDFILE"
-fi
-
-# set defaults if they weren't specified in buildfile
-pkgbase=${pkgbase:-${pkgname[0]}}
-epoch=${epoch:-0}
-basever=$(get_full_version)
 
-if [[ $BUILDDIR = "$startdir" ]]; then
-	srcdir="$BUILDDIR/src"
-	pkgdirbase="$BUILDDIR/pkg"
-else
-	srcdir="$BUILDDIR/$pkgbase/src"
-	pkgdirbase="$BUILDDIR/$pkgbase/pkg"
+	# set pkgdir to something "sensible" for (not recommended) use during build()
+	pkgdir="$pkgdirbase/$pkgbase"
 
-fi
+	if (( GENINTEG )); then
+		mkdir -p "$srcdir"
+		chmod a-s "$srcdir"
+		cd_safe "$srcdir"
+		download_sources fast
+		generate_checksums
+		exit 0 # $E_OK
+	fi
 
-# set pkgdir to something "sensible" for (not recommended) use during build()
-pkgdir="$pkgdirbase/$pkgbase"
+	if have_function pkgver; then
+		PKGVERFUNC=1
+	fi
 
-if (( GENINTEG )); then
-	mkdir -p "$srcdir"
-	chmod a-s "$srcdir"
-	cd_safe "$srcdir"
-	download_sources fast
-	generate_checksums
-	exit 0 # $E_OK
-fi
+	# check the PKGBUILD for some basic requirements
+	check_sanity || exit 1
 
-if have_function pkgver; then
-	PKGVERFUNC=1
-fi
+	# check we have the software required to process the PKGBUILD
+	check_software || exit 1
 
-# check the PKGBUILD for some basic requirements
-check_sanity || exit 1
+	if (( ${#pkgname[@]} > 1 )); then
+		SPLITPKG=1
+	fi
 
-# check we have the software required to process the PKGBUILD
-check_software || exit 1
+	# test for available PKGBUILD functions
+	if have_function prepare; then
+		# "Hide" prepare() function if not going to be run
+		if [[ $RUN_PREPARE != "n" ]]; then
+			PREPAREFUNC=1
+		fi
+	fi
+	if have_function build; then
+		BUILDFUNC=1
+	fi
+	if have_function check; then
+		# "Hide" check() function if not going to be run
+		if [[ $RUN_CHECK = 'y' ]] || { ! check_buildenv "check" "n" && [[ $RUN_CHECK != "n" ]]; }; then
+			CHECKFUNC=1
+		fi
+	fi
+	if have_function package; then
+		PKGFUNC=1
+	elif [[ $SPLITPKG -eq 0 ]] && have_function package_${pkgname}; then
+		SPLITPKG=1
+	fi
 
-if (( ${#pkgname[@]} > 1 )); then
-	SPLITPKG=1
-fi
+	if [[ -n "${PKGLIST[@]}" ]]; then
+		unset pkgname
+		pkgname=("${PKGLIST[@]}")
+	fi
 
-# test for available PKGBUILD functions
-if have_function prepare; then
-	# "Hide" prepare() function if not going to be run
-	if [[ $RUN_PREPARE != "n" ]]; then
-		PREPAREFUNC=1
+	# check if gpg signature is to be created and if signing key is valid
+	if { [[ -z $SIGNPKG ]] && check_buildenv "sign" "y"; } || [[ $SIGNPKG == 'y' ]]; then
+		SIGNPKG='y'
+		if ! gpg --list-key ${GPGKEY} &>/dev/null; then
+			if [[ ! -z $GPGKEY ]]; then
+				error "$(gettext "The key %s does not exist in your keyring.")" "${GPGKEY}"
+			else
+				error "$(gettext "There is no key in your keyring.")"
+			fi
+			exit 1
+		fi
 	fi
-fi
-if have_function build; then
-	BUILDFUNC=1
-fi
-if have_function check; then
-	# "Hide" check() function if not going to be run
-	if [[ $RUN_CHECK = 'y' ]] || { ! check_buildenv "check" "n" && [[ $RUN_CHECK != "n" ]]; }; then
-		CHECKFUNC=1
+
+	if (( ! PKGVERFUNC )); then
+		check_build_status
 	fi
-fi
-if have_function package; then
-	PKGFUNC=1
-elif [[ $SPLITPKG -eq 0 ]] && have_function package_${pkgname}; then
-	SPLITPKG=1
-fi
 
-if [[ -n "${PKGLIST[@]}" ]]; then
-	unset pkgname
-	pkgname=("${PKGLIST[@]}")
-fi
+	# Run the bare minimum in fakeroot
+	if (( INFAKEROOT )); then
+		if (( SOURCEONLY )); then
+			create_srcpackage
+			msg "$(gettext "Leaving %s environment.")" "fakeroot"
+			exit 0 # $E_OK
+		fi
 
-# check if gpg signature is to be created and if signing key is valid
-if { [[ -z $SIGNPKG ]] && check_buildenv "sign" "y"; } || [[ $SIGNPKG == 'y' ]]; then
-	SIGNPKG='y'
-	if ! gpg --list-key ${GPGKEY} &>/dev/null; then
-		if [[ ! -z $GPGKEY ]]; then
-			error "$(gettext "The key %s does not exist in your keyring.")" "${GPGKEY}"
+		chmod 755 "$pkgdirbase"
+		if (( ! SPLITPKG )); then
+			pkgdir="$pkgdirbase/$pkgname"
+			mkdir "$pkgdir"
+			if (( PKGFUNC )); then
+				run_package
+			fi
+			tidy_install
+			create_package
+			create_debug_package
 		else
-			error "$(gettext "There is no key in your keyring.")"
+			run_split_packaging
 		fi
-		exit 1
+
+		msg "$(gettext "Leaving %s environment.")" "fakeroot"
+		exit 0 # $E_OK
 	fi
-fi
 
-if (( ! PKGVERFUNC )); then
-	check_build_status
-fi
+	msg "$(gettext "Making package: %s")" "$pkgbase $basever ($(date))"
 
-# Run the bare minimum in fakeroot
-if (( INFAKEROOT )); then
+	# if we are creating a source-only package, go no further
 	if (( SOURCEONLY )); then
-		create_srcpackage
-		msg "$(gettext "Leaving %s environment.")" "fakeroot"
-		exit 0 # $E_OK
+		if [[ -f $SRCPKGDEST/${pkgbase}-${basever}${SRCEXT} ]] \
+				&& (( ! FORCE )); then
+			error "$(gettext "A source package has already been built. (use %s to overwrite)")" "-f"
+			exit 1
+		fi
+
+		# Get back to our src directory so we can begin with sources.
+		mkdir -p "$srcdir"
+		chmod a-s "$srcdir"
+		cd_safe "$srcdir"
+		if (( SOURCEONLY == 2 )); then
+			download_sources
+		elif ( (( ! SKIPCHECKSUMS )) || \
+				( (( ! SKIPPGPCHECK )) && source_has_signatures ) ); then
+			download_sources fast
+		fi
+		check_source_integrity
+		cd_safe "$startdir"
+
+		enter_fakeroot
+
+		msg "$(gettext "Source package created: %s")" "$pkgbase ($(date))"
+		exit 0
 	fi
 
-	chmod 755 "$pkgdirbase"
-	if (( ! SPLITPKG )); then
-		pkgdir="$pkgdirbase/$pkgname"
-		mkdir "$pkgdir"
-		if (( PKGFUNC )); then
-			run_package
+	if (( NODEPS || (NOBUILD && !DEP_BIN ) )); then
+		# no warning message needed for nobuild
+		if (( NODEPS )); then
+			warning "$(gettext "Skipping dependency checks.")"
 		fi
-		tidy_install
-		create_package
-		create_debug_package
 	else
-		run_split_packaging
-	fi
+		if (( RMDEPS && ! INSTALL )); then
+			original_pkglist=($(run_pacman -Qq))    # required by remove_dep
+		fi
+		deperr=0
 
-	msg "$(gettext "Leaving %s environment.")" "fakeroot"
-	exit 0 # $E_OK
-fi
+		msg "$(gettext "Checking runtime dependencies...")"
+		resolve_deps ${depends[@]} || deperr=1
 
-msg "$(gettext "Making package: %s")" "$pkgbase $basever ($(date))"
+		if (( RMDEPS && INSTALL )); then
+			original_pkglist=($(run_pacman -Qq))    # required by remove_dep
+		fi
 
-# if we are creating a source-only package, go no further
-if (( SOURCEONLY )); then
-	if [[ -f $SRCPKGDEST/${pkgbase}-${basever}${SRCEXT} ]] \
-			&& (( ! FORCE )); then
-		error "$(gettext "A source package has already been built. (use %s to overwrite)")" "-f"
-		exit 1
+		msg "$(gettext "Checking buildtime dependencies...")"
+		if (( CHECKFUNC )); then
+			resolve_deps "${makedepends[@]}" "${checkdepends[@]}" || deperr=1
+		else
+			resolve_deps "${makedepends[@]}" || deperr=1
+		fi
+
+		if (( RMDEPS )); then
+			current_pkglist=($(run_pacman -Qq))    # required by remove_deps
+		fi
+
+		if (( deperr )); then
+			error "$(gettext "Could not resolve all dependencies.")"
+			exit 1
+		fi
 	fi
 
-	# Get back to our src directory so we can begin with sources.
+	# get back to our src directory so we can begin with sources
 	mkdir -p "$srcdir"
 	chmod a-s "$srcdir"
 	cd_safe "$srcdir"
-	if (( SOURCEONLY == 2 )); then
-		download_sources
-	elif ( (( ! SKIPCHECKSUMS )) || \
-			( (( ! SKIPPGPCHECK )) && source_has_signatures ) ); then
-		download_sources fast
-	fi
-	check_source_integrity
-	cd_safe "$startdir"
-
-	enter_fakeroot
-
-	msg "$(gettext "Source package created: %s")" "$pkgbase ($(date))"
-	exit 0
-fi
 
-if (( NODEPS || (NOBUILD && !DEP_BIN ) )); then
-	# no warning message needed for nobuild
-	if (( NODEPS )); then
-		warning "$(gettext "Skipping dependency checks.")"
-	fi
-else
-	if (( RMDEPS && ! INSTALL )); then
-		original_pkglist=($(run_pacman -Qq))    # required by remove_dep
-	fi
-	deperr=0
+	if (( NOEXTRACT && ! VERIFYSOURCE )); then
+		warning "$(gettext "Using existing %s tree")" "\$srcdir/"
+	elif (( !REPKG )); then
+		download_sources
+		check_source_integrity
+		(( VERIFYSOURCE )) && exit 0 # $E_OK
 
-	msg "$(gettext "Checking runtime dependencies...")"
-	resolve_deps ${depends[@]} || deperr=1
+		if (( CLEANBUILD )); then
+			msg "$(gettext "Removing existing %s directory...")" "\$srcdir/"
+			rm -rf "$srcdir"/*
+		fi
 
-	if (( RMDEPS && INSTALL )); then
-		original_pkglist=($(run_pacman -Qq))    # required by remove_dep
+		extract_sources
+		if (( PKGVERFUNC )); then
+			update_pkgver
+			basever=$(get_full_version)
+			check_build_status
+		fi
+		if (( PREPAREFUNC )); then
+			run_prepare
+		fi
 	fi
 
-	msg "$(gettext "Checking buildtime dependencies...")"
-	if (( CHECKFUNC )); then
-		resolve_deps "${makedepends[@]}" "${checkdepends[@]}" || deperr=1
+	if (( NOBUILD )); then
+		msg "$(gettext "Sources are ready.")"
+		exit 0 #E_OK
 	else
-		resolve_deps "${makedepends[@]}" || deperr=1
-	fi
+		# clean existing pkg directory
+		if [[ -d $pkgdirbase ]]; then
+			msg "$(gettext "Removing existing %s directory...")" "\$pkgdir/"
+			rm -rf "$pkgdirbase"
+		fi
+		mkdir -p "$pkgdirbase"
+		chmod a-srwx "$pkgdirbase"
+		cd_safe "$startdir"
 
-	if (( RMDEPS )); then
-		current_pkglist=($(run_pacman -Qq))    # required by remove_deps
-	fi
+		if (( ! REPKG )); then
+			(( BUILDFUNC )) && run_build
+			(( CHECKFUNC )) && run_check
+			cd_safe "$startdir"
+		fi
 
-	if (( deperr )); then
-		error "$(gettext "Could not resolve all dependencies.")"
-		exit 1
+		enter_fakeroot
 	fi
-fi
 
-# get back to our src directory so we can begin with sources
-mkdir -p "$srcdir"
-chmod a-s "$srcdir"
-cd_safe "$srcdir"
-
-if (( NOEXTRACT && ! VERIFYSOURCE )); then
-	warning "$(gettext "Using existing %s tree")" "\$srcdir/"
-elif (( !REPKG )); then
-	download_sources
-	check_source_integrity
-	(( VERIFYSOURCE )) && exit 0 # $E_OK
-
-	if (( CLEANBUILD )); then
-		msg "$(gettext "Removing existing %s directory...")" "\$srcdir/"
-		rm -rf "$srcdir"/*
+	# if inhibiting archive creation, go no further
+	if (( NOARCHIVE )); then
+		msg "$(gettext "Package folder is ready.")"
+		exit 0
 	fi
 
-	extract_sources
-	if (( PKGVERFUNC )); then
-		update_pkgver
-		basever=$(get_full_version)
-		check_build_status
-	fi
-	if (( PREPAREFUNC )); then
-		run_prepare
-	fi
-fi
+	msg "$(gettext "Finished making: %s")" "$pkgbase $basever ($(date))"
 
-if (( NOBUILD )); then
-	msg "$(gettext "Sources are ready.")"
-	exit 0 #E_OK
-else
-	# clean existing pkg directory
-	if [[ -d $pkgdirbase ]]; then
-		msg "$(gettext "Removing existing %s directory...")" "\$pkgdir/"
-		rm -rf "$pkgdirbase"
-	fi
-	mkdir -p "$pkgdirbase"
-	chmod a-srwx "$pkgdirbase"
-	cd_safe "$startdir"
-
-	if (( ! REPKG )); then
-		(( BUILDFUNC )) && run_build
-		(( CHECKFUNC )) && run_check
-		cd_safe "$startdir"
-	fi
+	install_package
 
-	enter_fakeroot
-fi
+	exit 0 #E_OK
+}
 
-# if inhibiting archive creation, go no further
-if (( NOARCHIVE )); then
-	msg "$(gettext "Package folder is ready.")"
-	exit 0
+# if this file was not sourced, then "return" will be an error here, and we'll
+# run makepkg_main. Otherwise, the return succeeds and we'll only have sourced
+# the functions out of this file.
+if ! return 0 2>/dev/null; then
+	makepkg_main "$@"
 fi
 
-msg "$(gettext "Finished making: %s")" "$pkgbase $basever ($(date))"
-
-install_package
-
-exit 0 #E_OK
-
 # vim: set noet:
-- 
2.0.4


More information about the pacman-dev mailing list