public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-dev] New eclass: scons.eclass
@ 2010-08-22 10:04 Michał Górny
  2010-08-22 10:26 ` Alex Alexander
                   ` (3 more replies)
  0 siblings, 4 replies; 34+ messages in thread
From: Michał Górny @ 2010-08-22 10:04 UTC (permalink / raw
  To: gentoo-dev


[-- Attachment #1.1: Type: text/plain, Size: 1961 bytes --]

Hello,

As per bug #333911, I'm working on a new eclass, providing some basic
functions common to most of the ebuilds using the SCons build system.

The basic reason for the new eclass is that a growing number of ebuilds
is calling SCons directly, and reimplementing the same ideas. This is
especially important with MAKEOPTS, as SCons implements only a subset
of GNU make options.

Currently, you can find a set of ebuilds which mangle the MAKEOPTS using
a copypaste sed expression (poor though), many other ebuilds which
don't mangle it at all (and thus risk passing unsupported options like
--load-average or --jobs without an argument), and even some which
ignore MAKEOPTS at all.

The eclass addresses the issue through providing scons-clean-makeopts()
function in a manner similar to flag-o-matic.eclass strip-flags()
function. This function, supposedly called within the package's
pkg_setup() phase function, makes sure MAKEOPTS contains a clean set of
options suitable for passing to scons.

Another issue the eclass addresses is passing USE-controlled options.
PMS provides use_with and use_enable calls, suitable for
autotools-based build systems; SCons uses make-like 'var=value' syntax
instead. This is where use-scons() comes in handy -- it outputs such a
variable definition, based on the state a particular USEflag.

The last thing the eclass does is simply adding the DEPEND for SCons. I
don't think that needs any explanation.

I'm attaching the eclass draft (the same which is attached to bug
#333911), and inlining a simple use example below:

#v+
inherit scons

# ...

pkg_setup() {
	scons-clean-makeopts
}

src_compile() {
	scons \
		$(scons-use unicode) \
		$(scons-use gnutls ssl gnutls openssl) \
		${MAKEOPTS} || die
	# expands into:
	# scons unicode={1|0} ssl={gnutls|openssl} -jN || die
}
#v-

-- 
Best regards,
Michał Górny

<http://mgorny.alt.pl>
<xmpp:mgorny@jabber.ru>

[-- Attachment #1.2: scons.eclass --]
[-- Type: application/octet-stream, Size: 4957 bytes --]

# Copyright 1999-2010 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

# @ECLASS: scons.eclass
# @MAINTAINER:
# gentoo@mgorny.alt.pl
#
# @CODE
# Michał Górny <gentoo@mgorny.alt.pl>
# @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: 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

# -- public functions --

# @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"
}

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 34+ messages in thread

end of thread, other threads:[~2010-10-11 16:11 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-22 10:04 [gentoo-dev] New eclass: scons.eclass Michał Górny
2010-08-22 10:26 ` Alex Alexander
2010-08-22 18:00   ` Michał Górny
2010-08-22 11:03 ` David Leverton
2010-08-22 18:02   ` Michał Górny
2010-08-22 18:39 ` [gentoo-dev] " Michał Górny
2010-08-22 20:44   ` Luca Barbato
2010-08-22 21:03   ` Maciej Mrozowski
2010-08-23 15:11     ` Michał Górny
2010-08-23 17:36       ` Maciej Mrozowski
2010-08-24  8:30         ` [gentoo-dev] " Michał Górny
2010-08-24 17:52           ` Maciej Mrozowski
2010-08-22 21:06   ` [gentoo-dev] " Mike Frysinger
2010-08-22 21:10     ` Tomáš Chvátal
2010-08-22 21:18       ` Mike Frysinger
2010-08-22 21:32         ` Tomáš Chvátal
2010-08-22 22:03           ` Mike Frysinger
     [not found]     ` <20100823174249.5eb1a865@pomiocik.lan>
2010-08-24 21:28       ` Mike Frysinger
2010-08-23 16:24   ` [gentoo-dev] " Michał Górny
2010-08-23 16:39     ` Mike Frysinger
2010-08-23 17:16       ` Michał Górny
2010-08-23 20:22         ` Mike Frysinger
2010-08-23 23:02           ` Michał Górny
2010-08-23 23:27             ` Mike Frysinger
2010-08-24  8:11               ` Michał Górny
2010-08-24 18:40   ` Michał Górny
2010-08-24 21:30     ` Mike Frysinger
2010-08-24 21:37       ` Michał Górny
2010-10-05 18:57 ` Michał Górny
2010-10-06 17:52   ` Donnie Berkholz
2010-10-06 18:26     ` Michał Górny
2010-10-08  3:08       ` Donnie Berkholz
2010-10-08  5:35         ` Michał Górny
2010-10-11 16:11           ` Donnie Berkholz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox