add helpers for working with options

Add the helpers vopt_if, vopt_with and vopt_enable that simplify common
option-based operations.

Instead of a bunch of

if [ "$build_option_foo" ]; then
	configure_args+=" --with-foo"
	makedepends+=" foo-devel"
else
	configure_args+=" --without-foo"
fi

one can use

configure_args="... $(vopt_with foo)
makedepends="... $(vopt_if foo foo-devel)"

instead.

We're adding these functions to common/xbps-src/shutils/common.sh but
that might not be the ideal place. I would've preferred
common/helpers/options.sh, but helpers are only available in the
individual phases, not when the template itself gets parsed.
This commit is contained in:
Dominik Honnef 2014-08-30 00:29:33 +02:00
parent a81950142e
commit cdd2ce0da4
2 changed files with 43 additions and 7 deletions

View File

@ -514,7 +514,26 @@ should be added to `common/options.description` instead.
After defining those required variables, you can check for the After defining those required variables, you can check for the
`build_option_<option>` variable to know if it has been set and adapt the source `build_option_<option>` variable to know if it has been set and adapt the source
package accordingly. package accordingly. Additionally, the following functions are available:
- *vopt_if()* `vopt_if <option> <if_true> [<if_false>]`
Outputs `if_true` if `option` is set, or `if_false` if it isn't set.
- *vopt_with()* `vopt_with <option> [<flag>]`
Outputs `--with-<flag>` if the option is set, or `--without-<flag>`
otherwise. If `flag` isn't set, it defaults to `option`.
Examples:
- `vopt_with dbus`
- `vopt_with xml xml2`
- *vopt_enable()* `vopt_enable <option> [<flag>]`
Same as `vopt_with`, but uses `--enable-<flag>` and
`--disable-<flag>` respectively.
The following example shows how to change a source package that uses GNU The following example shows how to change a source package that uses GNU
configure to enable a new build option to support PNG images: configure to enable a new build option to support PNG images:
@ -525,6 +544,8 @@ pkgname=foo
version=1.0 version=1.0
revision=1 revision=1
build_style=gnu-configure build_style=gnu-configure
configure_args="... $(vopt_with png)"
makedepends="... $(vopt_if png libpng-devel)"
... ...
# Package build options # Package build options
@ -535,12 +556,6 @@ desc_option_png="Enable support for PNG images"
# #
# build_options_default="png" # build_options_default="png"
if [ "$build_option_png" ]; then
configure_args+=" --with-png"
makedepends+=" libpng-devel"
else
configure_args+=" --without-png"
fi
... ...
``` ```

View File

@ -566,3 +566,24 @@ remove_cross_pkg() {
msg_error "failed to remove cross-${XBPS_CROSS_TRIPLET} (error $rval)\n" msg_error "failed to remove cross-${XBPS_CROSS_TRIPLET} (error $rval)\n"
fi fi
} }
vopt_if() {
local opt="$1" t="$2" f="$3"
name="build_option_$opt"
if [ ${!name} ]; then
echo -n "$t"
else
echo -n "$f"
fi
}
vopt_with() {
local opt="$1" flag="${2:-$1}"
vopt_if "$1" "--with-${flag}" "--without-${flag}"
}
vopt_enable() {
local opt="$1" flag="${2:-$1}"
vopt_if "$1" "--enable-${flag}" "--disable-${flag}"
}