diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2022-12-21 11:23:34 -0800 |
---|---|---|
committer | Eli Schwartz <eschwartz93@gmail.com> | 2023-02-15 22:58:50 -0500 |
commit | b2473b61cc2c61218f5be4057af0e42b63d00048 (patch) | |
tree | bf0cb6b3f776f057bcc81d9b067baa4e47ef69d5 /mesonbuild/interpreter/interpreterobjects.py | |
parent | 3589815eb9dd8402deabedcd4492f33e04870c56 (diff) | |
download | meson-b2473b61cc2c61218f5be4057af0e42b63d00048.zip meson-b2473b61cc2c61218f5be4057af0e42b63d00048.tar.gz meson-b2473b61cc2c61218f5be4057af0e42b63d00048.tar.bz2 |
interpreter: add FeatureOption.enable_if and .disable_if
This adds two new methods, that are conceptually related in the same way
that `enable_auto_if` and `disable_auto_if` are. They are different
however, in that they will always replace an `auto` value with an
`enabled` or `disabled` value, or error if the feature is in the
opposite state (calling `feature(disabled).enable_if(true)`, for
example). This matters when the feature will be passed to
dependency(required : …)`, which has different behavior when passed an
enabled feature than an auto one.
The `disable_if` method will be controversial, I'm sure, since it
can be expressed via `feature.require()` (`feature.require(not
condition) == feature.disable_if(condition)`). I have two defences of
this:
1) `feature.require` is difficult to reason about, I would expect
require to be equivalent to `feature.enable_if(condition)`, not to
`feature.disable_if(not condition)`.
2) mixing `enable_if` and `disable_if` in the same call chain is much
clearer than mixing `require` and `enable_if`:
```meson
get_option('feat') \
.enable_if(foo) \
.disable_if(bar) \
.enable_if(opt)
```
vs
```meson
get_option('feat') \
.enable_if(foo) \
.require(not bar) \
.enable_if(opt)
```
In the first chain it's immediately obvious what is happening, in the
second, not so much, especially if you're not familiar with what
`require` means.
Diffstat (limited to 'mesonbuild/interpreter/interpreterobjects.py')
-rw-r--r-- | mesonbuild/interpreter/interpreterobjects.py | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/mesonbuild/interpreter/interpreterobjects.py b/mesonbuild/interpreter/interpreterobjects.py index a750da9..fa91714 100644 --- a/mesonbuild/interpreter/interpreterobjects.py +++ b/mesonbuild/interpreter/interpreterobjects.py @@ -41,6 +41,9 @@ if T.TYPE_CHECKING: separator: str +_ERROR_MSG_KW: KwargInfo[T.Optional[str]] = KwargInfo('error_message', (str, NoneType)) + + def extract_required_kwarg(kwargs: 'kwargs.ExtractRequired', subproject: 'SubProject', feature_check: T.Optional[FeatureCheckBase] = None, @@ -97,6 +100,8 @@ class FeatureOptionHolder(ObjectHolder[coredata.UserFeatureOption]): 'require': self.require_method, 'disable_auto_if': self.disable_auto_if_method, 'enable_auto_if': self.enable_auto_if_method, + 'disable_if': self.disable_if_method, + 'enable_if': self.enable_if_method, }) @property @@ -134,22 +139,51 @@ class FeatureOptionHolder(ObjectHolder[coredata.UserFeatureOption]): def auto_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> bool: return self.value == 'auto' + def _disable_if(self, condition: bool, message: T.Optional[str]) -> coredata.UserFeatureOption: + if not condition: + return copy.deepcopy(self.held_object) + + if self.value == 'enabled': + err_msg = f'Feature {self.held_object.name} cannot be enabled' + if message: + err_msg += f': {message}' + raise InterpreterException(err_msg) + return self.as_disabled() + @FeatureNew('feature_option.require()', '0.59.0') @typed_pos_args('feature_option.require', bool) @typed_kwargs( 'feature_option.require', - KwargInfo('error_message', (str, NoneType)) + _ERROR_MSG_KW, ) def require_method(self, args: T.Tuple[bool], kwargs: 'kwargs.FeatureOptionRequire') -> coredata.UserFeatureOption: - if args[0]: + return self._disable_if(not args[0], kwargs['error_message']) + + @FeatureNew('feature_option.disable_if()', '1.1.0') + @typed_pos_args('feature_option.disable_if', bool) + @typed_kwargs( + 'feature_option.disable_if', + _ERROR_MSG_KW, + ) + def disable_if_method(self, args: T.Tuple[bool], kwargs: 'kwargs.FeatureOptionRequire') -> coredata.UserFeatureOption: + return self._disable_if(args[0], kwargs['error_message']) + + @FeatureNew('feature_option.enable_if()', '1.1.0') + @typed_pos_args('feature_option.enable_if', bool) + @typed_kwargs( + 'feature_option.enable_if', + _ERROR_MSG_KW, + ) + def enable_if_method(self, args: T.Tuple[bool], kwargs: 'kwargs.FeatureOptionRequire') -> coredata.UserFeatureOption: + if not args[0]: return copy.deepcopy(self.held_object) - if self.value == 'enabled': - err_msg = f'Feature {self.held_object.name} cannot be enabled' + if self.value == 'disabled': + err_msg = f'Feature {self.held_object.name} cannot be disabled' if kwargs['error_message']: err_msg += f': {kwargs["error_message"]}' raise InterpreterException(err_msg) - return self.as_disabled() + return self.as_enabled() @FeatureNew('feature_option.disable_auto_if()', '0.59.0') @noKwargs |