Here goes the second revision. A short changelog:

83678d1 Export a default pkg_setup() and src_compile().
7f9b565 Introduce escons() function (similar to emake).
19b7e14 Use underscores instead of dashes in function names.

Attaching both the new rev and a diff against the first one.

-- 
Best regards,
Michał Górny

<http://mgorny.alt.pl>
<xmpp:[email protected]>
# Copyright 1999-2010 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

# @ECLASS: scons.eclass
# @MAINTAINER:
# [email protected]
#
# @CODE
# Michał Górny <[email protected]>
# @CODE
# @BLURB: helper functions to deal with SCons buildsystem
# @DESCRIPTION:
# This eclass provides a set of function to help developers sanely call
# dev-util/scons and pass parameters to it.

# -- public variables --

# @ECLASS-VARIABLE: SCONS_MIN_VERSION
# @DESCRIPTION:
# The minimal version of SCons required for the build to work.
: ${SCONS_MIN_VERSION}

# @ECLASS-VARIABLE: EXTRA_ESCONS
# @DESCRIPTION:
# The additional parameters to pass to SCons whenever escons() is used.
: ${EXTRA_ESCONS:=}

# @ECLASS-VARIABLE: SCONS_USE_TRUE
# @DESCRIPTION:
# The default value for truth in scons-use() (1 by default).
: ${SCONS_USE_TRUE:=1}

# @ECLASS-VARIABLE: SCONS_USE_FALSE
# @DESCRIPTION:
# The default value for false in scons-use() (0 by default).
: ${SCONS_USE_FALSE:=0}

# -- ebuild variables setup --

if [[ -n ${SCONS_MIN_VERSION} ]]; then
	DEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
else
	DEPEND="dev-util/scons"
fi

# -- exported phase functions --

EXPORT_FUNCTIONS pkg_setup src_compile

# @FUNCTION: scons_pkg_setup
# @DESCRIPTION:
# The exported pkg_setup() implementation. Calls scons_clean_makeopts()
# to get a sane MAKEOPTS.
scons_pkg_setup() {
	debug-print-function ${FUNCNAME} "$...@}"

	scons_clean_makeopts
}

# @FUNCTION: scons_src_compile
# @DESCRIPTION:
# The exported src_compile() implementation. Simply calls escons().
scons_src_compile() {
	debug-print-function ${FUNCNAME} "$...@}"

	escons || die 'escons failed.'
}

# -- public functions --

# @FUNCTION: escons
# @USAGE: [scons-arg] ...
# @DESCRIPTION:
# Call scons, passing the supplied arguments, ${MAKEOPTS} and
# ${EXTRA_ESCONS}. Similar to emake.
escons() {
	debug-print-function ${FUNCNAME} "$...@}"
	debug-print scons ${MAKEOPTS} ${EXTRA_ESCONS} "$...@}"

	scons ${MAKEOPTS} ${EXTRA_ESCONS} "$...@}"
}

# @FUNCTION: scons_clean_makeopts
# @DESCRIPTION:
# Strip MAKEOPTS of options not supported by SCons and make sure --jobs
# gets an argument.
scons_clean_makeopts() {
	local new_makeopts

	debug-print-function ${FUNCNAME} "$...@}"

	set -- ${MAKEOPTS}
	while [[ ${#} -gt 0 ]]; do
		case ${1} in
			# clean, simple to check -- we like that
			--jobs=*|--keep-going)
				new_makeopts=${new_makeopts+${new_makeopts} }${1}
				;;
			# need to take a look at the next arg and guess
			--jobs)
				if [[ ${#} -gt 1 && ${2} =~ [0-9]+ ]]; then
					new_makeopts="${new_makeopts+${new_makeopts} }${1} ${2}"
					shift
				else
					# no value means no limit, let's pass a random int
					new_makeopts=${new_makeopts+${new_makeopts} }${1}=255
				fi
				;;
			# strip other long options
			--*)
				;;
			# short option hell
			-*)
				local str new_optstr
				new_optstr=
				str=${1#-}

				while [[ -n ${str} ]]; do
					case ${str} in
						k*)
							new_optstr=${new_optstr}k
							;;
						# -j needs to come last
						j)
							if [[ ${#} -gt 1 && ${2} =~ [0-9]+ ]]; then
								new_optstr="${new_optstr}j ${2}"
								shift
							else
								new_optstr="${new_optstr}j 255"
							fi
							;;
						# otherwise, everything after -j is treated as an arg
						j*)
							new_optstr=${new_optstr}${str}
							break
							;;
					esac
					str=${str#?}
				done

				if [[ -n ${new_optstr} ]]; then
					new_makeopts=${new_makeopts+${new_makeopts} }-${new_optstr}
				fi
				;;
		esac
		shift
	done

	MAKEOPTS=${new_makeopts}
	export MAKEOPTS
}

# @FUNCTION: scons_use
# @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
# @DESCRIPTION:
# Output an SCons parameter with value depending on the USE flag state.
# If the USE flag is set, output <var-name>=<var-opt-true>; otherwise
# <var-name>=<var-opt-false>.
#
# If <var-name> is not set, <use-flag> will be used instead.
# If <var-opt-true> or <var-opt-false> is unset, SCONS_USE_TRUE
# or SCONS_USE_FALSE will be used instead.
scons_use() {
	local flag=${1}
	local varname=${2:-${flag}}
	local vartrue=${3:-${SCONS_USE_TRUE}}
	local varfalse=${4:-${SCONS_USE_FALSE}}

	debug-print-function ${FUNCNAME} "$...@}"

	if [[ ${#} -eq 0 ]]; then
		eerror "Usage: scons-use <use-flag> [var-name] [var-opt-true] [var-opt-false]"
		die 'scons-use(): not enough arguments'
	fi

	if use "${flag}"; then
		echo "${varname}=${vartrue}"
	else
		echo "${varname}=${varfalse}"
	fi
}

# -- self-tests --

_scons-clean-makeopts-perform-test() {
	MAKEOPTS=${1}
	scons-clean-makeopts
	if [[ ${MAKEOPTS} != ${2-${1}} ]]; then
		cat >&2 <<_EOF_
Self-test failed:
	Input string: ${1}
	Output string: ${MAKEOPTS}
	Expected: ${2-${1}}
_EOF_
	fi
}

# Perform a self-test on scons-clean-makeopts.
_scons-clean-makeopts-tests() {
	# jobcount expected for non-specified state
	local jc=255

	# sane MAKEOPTS
	_scons-clean-makeopts-perform-test '--jobs=14 -k'
	_scons-clean-makeopts-perform-test '--jobs=14 -k'
	_scons-clean-makeopts-perform-test '--jobs 15 -k'
	_scons-clean-makeopts-perform-test '--jobs=16 --keep-going'
	_scons-clean-makeopts-perform-test '-j17 --keep-going'
	_scons-clean-makeopts-perform-test '-j 18 --keep-going'

	# needing cleaning
	_scons-clean-makeopts-perform-test '--jobs -k' "--jobs=${jc} -k"
	_scons-clean-makeopts-perform-test '--jobs --keep-going' "--jobs=${jc} --keep-going"
	_scons-clean-makeopts-perform-test '-kj' "-kj ${jc}"

	# broken by definition (but passed as it breaks make as well)
	_scons-clean-makeopts-perform-test '-jk'
	_scons-clean-makeopts-perform-test '--jobs=randum'
	_scons-clean-makeopts-perform-test '-kjrandum'

	# needing stripping
	_scons-clean-makeopts-perform-test '--load-average=25 -kj16' '-kj16'
	_scons-clean-makeopts-perform-test '--load-average 25 -k -j17' '-k -j17'
	_scons-clean-makeopts-perform-test '-j2 HOME=/tmp' '-j2'
	_scons-clean-makeopts-perform-test '--jobs funnystuff -k' "--jobs=${jc} -k"
}
diff --git a/scons.eclass b/scons.eclass
index a6a0daa..8321ee0 100644
--- a/scons.eclass
+++ b/scons.eclass
@@ -21,6 +21,11 @@
 # The minimal version of SCons required for the build to work.
 : ${SCONS_MIN_VERSION}
 
+# @ECLASS-VARIABLE: EXTRA_ESCONS
+# @DESCRIPTION:
+# The additional parameters to pass to SCons whenever escons() is used.
+: ${EXTRA_ESCONS:=}
+
 # @ECLASS-VARIABLE: SCONS_USE_TRUE
 # @DESCRIPTION:
 # The default value for truth in scons-use() (1 by default).
@@ -39,13 +44,48 @@ else
 	DEPEND="dev-util/scons"
 fi
 
+# -- exported phase functions --
+
+EXPORT_FUNCTIONS pkg_setup src_compile
+
+# @FUNCTION: scons_pkg_setup
+# @DESCRIPTION:
+# The exported pkg_setup() implementation. Calls scons_clean_makeopts()
+# to get a sane MAKEOPTS.
+scons_pkg_setup() {
+	debug-print-function ${FUNCNAME} "$...@}"
+
+	scons_clean_makeopts
+}
+
+# @FUNCTION: scons_src_compile
+# @DESCRIPTION:
+# The exported src_compile() implementation. Simply calls escons().
+scons_src_compile() {
+	debug-print-function ${FUNCNAME} "$...@}"
+
+	escons || die 'escons failed.'
+}
+
 # -- public functions --
 
-# @FUNCTION: scons-clean-makeopts
+# @FUNCTION: escons
+# @USAGE: [scons-arg] ...
+# @DESCRIPTION:
+# Call scons, passing the supplied arguments, ${MAKEOPTS} and
+# ${EXTRA_ESCONS}. Similar to emake.
+escons() {
+	debug-print-function ${FUNCNAME} "$...@}"
+	debug-print scons ${MAKEOPTS} ${EXTRA_ESCONS} "$...@}"
+
+	scons ${MAKEOPTS} ${EXTRA_ESCONS} "$...@}"
+}
+
+# @FUNCTION: scons_clean_makeopts
 # @DESCRIPTION:
 # Strip MAKEOPTS of options not supported by SCons and make sure --jobs
 # gets an argument.
-scons-clean-makeopts() {
+scons_clean_makeopts() {
 	local new_makeopts
 
 	debug-print-function ${FUNCNAME} "$...@}"
@@ -111,7 +151,7 @@ scons-clean-makeopts() {
 	export MAKEOPTS
 }
 
-# @FUNCTION: scons-use
+# @FUNCTION: scons_use
 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
 # @DESCRIPTION:
 # Output an SCons parameter with value depending on the USE flag state.
@@ -121,7 +161,7 @@ scons-clean-makeopts() {
 # If <var-name> is not set, <use-flag> will be used instead.
 # If <var-opt-true> or <var-opt-false> is unset, SCONS_USE_TRUE
 # or SCONS_USE_FALSE will be used instead.
-scons-use() {
+scons_use() {
 	local flag=${1}
 	local varname=${2:-${flag}}
 	local vartrue=${3:-${SCONS_USE_TRUE}}

Attachment: signature.asc
Description: PGP signature

Reply via email to