From ede669255b83483eef820af3cb38dbe962e8d206 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Sun, 16 May 2021 17:54:43 +0200 Subject: [PATCH] interpreter: add feature.disable_auto_if() Add a method to downgrade an option to disabled if it is not used. This is useful to avoid unnecessary search for dependencies; for example dep = dependency('dep', required: get_option('feature').disable_auto_if(not foo)) can be used instead of the more verbose and complex if get_option('feature').auto() and not foo then dep = dependency('', required: false) else dep = dependency('dep', required: get_option('feature')) endif or to avoid unnecessary dependency searches: dep1 = dependency('dep1', required: get_option('foo')) # dep2 is only used together with dep1 dep2 = dependency('dep2', required: get_option('foo').disable_auto_if(not dep1.found())) ``` Signed-off-by: Paolo Bonzini --- docs/markdown/Reference-manual.md | 9 +++++++++ mesonbuild/interpreter/interpreterobjects.py | 9 +++++++++ test cases/common/192 feature option/meson.build | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md index 95b70ed4b1bd..e2d1efdbc268 100644 --- a/docs/markdown/Reference-manual.md +++ b/docs/markdown/Reference-manual.md @@ -2819,6 +2819,15 @@ The following methods are defined for all [`feature` options](Build-options.md#f - `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'` +- `disable_auto_if(value)` *(since 0.59.0)*: returns the feature, with + `'auto'` converted to `'disabled'` if value is true. + + | Feature / Condition | True | False | + | ------------------- | ---- | ----- | + | Enabled | Enabled | Enabled | + | Disabled | Disabled | Disabled | + | Auto | Disabled | Auto | + - `require(value, error_message: '')` *(since 0.59.0)*: returns the object itself if the value is true; an error if the object is `'enabled'` and the value is false; a disabled feature if the object diff --git a/mesonbuild/interpreter/interpreterobjects.py b/mesonbuild/interpreter/interpreterobjects.py index 0bf188cc5996..37f486a84a04 100644 --- a/mesonbuild/interpreter/interpreterobjects.py +++ b/mesonbuild/interpreter/interpreterobjects.py @@ -75,6 +75,7 @@ def __init__(self, env: 'Environment', name: str, option: coredata.UserFeatureOp 'allowed': self.allowed_method, 'auto': self.auto_method, 'require': self.require_method, + 'disable_auto_if': self.disable_auto_if_method, }) @property @@ -122,6 +123,14 @@ def require_method(self, args, kwargs): raise InterpreterException(prefix + error_message) return self.as_disabled() + @permittedKwargs({}) + def disable_auto_if_method(self, args, kwargs): + if len(args) != 1: + raise InvalidArguments('Expected 1 argument, got %d.' % (len(args), )) + if not isinstance(args[0], bool): + raise InvalidArguments('boolean argument expected.') + return self if self.value != 'auto' or not args[0] else self.as_disabled() + class RunProcess(InterpreterObject): diff --git a/test cases/common/192 feature option/meson.build b/test cases/common/192 feature option/meson.build index 6d26debf8fd9..b5e26fac36df 100644 --- a/test cases/common/192 feature option/meson.build +++ b/test cases/common/192 feature option/meson.build @@ -15,6 +15,8 @@ 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(required_opt.require(true, error_message: 'xyz').enabled(), 'Should be enabled option') +assert(required_opt.disable_auto_if(true).enabled(), 'Should be enabled option') +assert(required_opt.disable_auto_if(false).enabled(), 'Should be enabled option') assert(not optional_opt.enabled(), 'Should be auto option') assert(not optional_opt.disabled(), 'Should be auto option') @@ -22,6 +24,8 @@ assert(optional_opt.auto(), 'Should be auto option') assert(optional_opt.allowed(), 'Should be auto option') assert(optional_opt.require(true).auto(), 'Should be auto option') assert(optional_opt.require(false, error_message: 'xyz').disabled(), 'Should be disabled auto option') +assert(optional_opt.disable_auto_if(true).disabled(), 'Should be disabled auto option') +assert(optional_opt.disable_auto_if(false).auto(), 'Should be auto option') assert(not disabled_opt.enabled(), 'Should be disabled option') assert(disabled_opt.disabled(), 'Should be disabled option') @@ -29,6 +33,8 @@ assert(not disabled_opt.auto(), 'Should be disabled option') assert(not disabled_opt.allowed(), 'Should be disabled option') assert(disabled_opt.require(true).disabled(), 'Should be disabled option') assert(disabled_opt.require(false, error_message: 'xyz').disabled(), 'Should be disabled option') +assert(disabled_opt.disable_auto_if(true).disabled(), 'Should be disabled option') +assert(disabled_opt.disable_auto_if(false).disabled(), 'Should be disabled option') dep = dependency('threads', required : required_opt) assert(dep.found(), 'Should find required "threads" dep')