interpreter: add a feature.enable_auto_if

It's always been strange to me we don't have an opposite method of the
`disable_auto_if` method, but I've been pressed to find a case where we
_need_ one, because `disable_auto_if` can't be logically contorted to
work. I finally found the case where they're not equivalent: when you
don't want to convert to a boolean:

```meson
f = get_option('feat').disable_auto_if(not foo)
g = get_option('feat').enable_auto_if(foo)

dep1 = dependency('foo', required : f)
dep2 = dependency('foo', required : g)
```
pull/10930/head
Dylan Baker 2 years ago committed by Eli Schwartz
parent a3098fbfa3
commit 3589815eb9
  1. 4
      docs/markdown/snippets/feature_enable_auto_if.md
  2. 17
      docs/yaml/objects/feature.yaml
  3. 13
      mesonbuild/interpreter/interpreterobjects.py
  4. 6
      test cases/common/192 feature option/meson.build

@ -0,0 +1,4 @@
## Feature objects now have an enable_auto_if method
This performs the opposite task of the disable_auto_if method, enabling the
feature if the condition is true.

@ -38,6 +38,23 @@ methods:
type: bool
description: See the table above
- name: enable_auto_if
since: 1.1.0
returns: feature
description: |
Returns the feature, with `'auto'` converted to `'enabled'` if value is true.
| Feature / Condition | `value = true` | `value = false` |
| ------------------- | -------------- | --------------- |
| Enabled | Enabled | Enabled |
| Disabled | Disabled | Disabled |
| Auto | Enabled | Auto |
posargs:
value:
type: bool
description: See the table above
- name: require
returns: feature
since: 0.59.0

@ -41,7 +41,6 @@ if T.TYPE_CHECKING:
separator: str
def extract_required_kwarg(kwargs: 'kwargs.ExtractRequired',
subproject: 'SubProject',
feature_check: T.Optional[FeatureCheckBase] = None,
@ -97,6 +96,7 @@ class FeatureOptionHolder(ObjectHolder[coredata.UserFeatureOption]):
'auto': self.auto_method,
'require': self.require_method,
'disable_auto_if': self.disable_auto_if_method,
'enable_auto_if': self.enable_auto_if_method,
})
@property
@ -108,6 +108,11 @@ class FeatureOptionHolder(ObjectHolder[coredata.UserFeatureOption]):
disabled.value = 'disabled'
return disabled
def as_enabled(self) -> coredata.UserFeatureOption:
enabled = copy.deepcopy(self.held_object)
enabled.value = 'enabled'
return enabled
@noPosargs
@noKwargs
def enabled_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> bool:
@ -152,6 +157,12 @@ class FeatureOptionHolder(ObjectHolder[coredata.UserFeatureOption]):
def disable_auto_if_method(self, args: T.Tuple[bool], kwargs: TYPE_kwargs) -> coredata.UserFeatureOption:
return copy.deepcopy(self.held_object) if self.value != 'auto' or not args[0] else self.as_disabled()
@FeatureNew('feature_option.enable_auto_if()', '1.1.0')
@noKwargs
@typed_pos_args('feature_option.enable_auto_if', bool)
def enable_auto_if_method(self, args: T.Tuple[bool], kwargs: TYPE_kwargs) -> coredata.UserFeatureOption:
return self.as_enabled() if self.value == 'auto' and args[0] else copy.deepcopy(self.held_object)
class RunProcess(MesonInterpreterObject):

@ -17,6 +17,8 @@ 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(required_opt.enable_auto_if(true).enabled(), 'Should be enabled option')
assert(required_opt.enable_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')
@ -26,6 +28,8 @@ 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(optional_opt.enable_auto_if(true).enabled(), 'Should be disabled auto option')
assert(optional_opt.enable_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')
@ -35,6 +39,8 @@ 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')
assert(disabled_opt.enable_auto_if(true).disabled(), 'Should be disabled option')
assert(disabled_opt.enable_auto_if(false).disabled(), 'Should be disabled option')
dep = dependency('threads', required : required_opt)
assert(dep.found(), 'Should find required "threads" dep')

Loading…
Cancel
Save