aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2021-02-22 17:19:48 +0100
committerPaolo Bonzini <pbonzini@redhat.com>2021-05-31 16:01:57 +0200
commit08a8043f1916cb2a337b93b6b6d600df9af6ecb7 (patch)
tree755610159fe1fb03430842492db81cbf6dbd38c2
parent6aef800ba83a4dd469c354900b6e537777d3080c (diff)
downloadmeson-08a8043f1916cb2a337b93b6b6d600df9af6ecb7.zip
meson-08a8043f1916cb2a337b93b6b6d600df9af6ecb7.tar.gz
meson-08a8043f1916cb2a337b93b6b6d600df9af6ecb7.tar.bz2
interpreter: add feature.allowed()
This method simplifies the conversion of Feature objects to booleans. Often, one has to use the "not" operator in order to treat "auto" and "enabled" the same way. "allowed()" also works well in conjunction with the require method that is introduced in the next patch. For example, if get_option('foo').require(host_machine.system() == 'windows').allowed() then src += ['foo.c'] config.set10('HAVE_FOO', 1) endif can be used instead of if host_machine.system() != 'windows' if get_option('foo').enabled() error('...') endif endif if not get_option('foo').disabled() then src += ['foo.c'] config.set10('HAVE_FOO', 1) endif Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r--docs/markdown/Reference-manual.md1
-rw-r--r--mesonbuild/interpreter/interpreterobjects.py6
-rw-r--r--test cases/common/192 feature option/meson.build4
3 files changed, 11 insertions, 0 deletions
diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md
index 103f4d8..f6b3fc2 100644
--- a/docs/markdown/Reference-manual.md
+++ b/docs/markdown/Reference-manual.md
@@ -2818,6 +2818,7 @@ The following methods are defined for all [`feature` options](Build-options.md#f
- `enabled()`: returns whether the feature was set to `'enabled'`
- `disabled()`: returns whether the feature was set to `'disabled'`
- `auto()`: returns whether the feature was set to `'auto'`
+- `allowed()` *(since 0.59.0)*: returns whether the feature was set to `'enabled'` or `'auto'`
### `generator` object
diff --git a/mesonbuild/interpreter/interpreterobjects.py b/mesonbuild/interpreter/interpreterobjects.py
index c70b8b5..5a37b48 100644
--- a/mesonbuild/interpreter/interpreterobjects.py
+++ b/mesonbuild/interpreter/interpreterobjects.py
@@ -72,6 +72,7 @@ class FeatureOptionHolder(InterpreterObject, ObjectHolder[coredata.UserFeatureOp
self.name = name
self.methods.update({'enabled': self.enabled_method,
'disabled': self.disabled_method,
+ 'allowed': self.allowed_method,
'auto': self.auto_method,
})
@@ -87,6 +88,11 @@ class FeatureOptionHolder(InterpreterObject, ObjectHolder[coredata.UserFeatureOp
@noPosargs
@permittedKwargs({})
+ def allowed_method(self, args, kwargs):
+ return not self.held_object.is_disabled()
+
+ @noPosargs
+ @permittedKwargs({})
def auto_method(self, args, kwargs):
return self.held_object.is_auto()
diff --git a/test cases/common/192 feature option/meson.build b/test cases/common/192 feature option/meson.build
index ef3fa22..030ccbc 100644
--- a/test cases/common/192 feature option/meson.build
+++ b/test cases/common/192 feature option/meson.build
@@ -8,18 +8,22 @@ disabled_opt = get_option('disabled')
assert(not feature_opts.enabled(), 'Should be auto option')
assert(not feature_opts.disabled(), 'Should be auto option')
assert(feature_opts.auto(), 'Should be auto option')
+assert(feature_opts.allowed(), 'Should be auto option')
assert(required_opt.enabled(), 'Should be enabled option')
assert(not required_opt.disabled(), 'Should be enabled option')
assert(not required_opt.auto(), 'Should be enabled option')
+assert(required_opt.allowed(), 'Should be enabled option')
assert(not optional_opt.enabled(), 'Should be auto option')
assert(not optional_opt.disabled(), 'Should be auto option')
assert(optional_opt.auto(), 'Should be auto option')
+assert(optional_opt.allowed(), 'Should be auto option')
assert(not disabled_opt.enabled(), 'Should be disabled option')
assert(disabled_opt.disabled(), 'Should be disabled option')
assert(not disabled_opt.auto(), 'Should be disabled option')
+assert(not disabled_opt.allowed(), 'Should be disabled option')
dep = dependency('threads', required : required_opt)
assert(dep.found(), 'Should find required "threads" dep')