Skip to content

Commit

Permalink
Merge pull request #8512 from bonzini/feature-methods
Browse files Browse the repository at this point in the history
Utility methods for feature objects
  • Loading branch information
jpakkane authored Jun 8, 2021
2 parents 98efec5 + 6497e52 commit a4a61b6
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 5 deletions.
25 changes: 25 additions & 0 deletions docs/markdown/Reference-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -2818,6 +2818,31 @@ 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'`
- `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
is `'auto'` or `'disabled'` and the value is false.

`require` is useful to restrict the applicability of `'auto'` features,
for example based on other features or on properties of the host machine:

```
if get_option('directx').require(host_machine.system() == 'windows',
error_message: 'DirectX only available on Windows').allowed() then
src += ['directx.c']
config.set10('HAVE_DIRECTX', 1)
endif
```

### `generator` object

Expand Down
50 changes: 46 additions & 4 deletions mesonbuild/interpreter/interpreterobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,71 @@ class FeatureOptionHolder(InterpreterObject, ObjectHolder[coredata.UserFeatureOp
def __init__(self, env: 'Environment', name: str, option: coredata.UserFeatureOption):
InterpreterObject.__init__(self)
ObjectHolder.__init__(self, option)
if option.is_auto():
if option and option.is_auto():
# TODO: we need to case here because options is not a TypedDict
self.held_object = T.cast(coredata.UserFeatureOption, env.coredata.options[OptionKey('auto_features')])
self.name = name
self.methods.update({'enabled': self.enabled_method,
'disabled': self.disabled_method,
'allowed': self.allowed_method,
'auto': self.auto_method,
'require': self.require_method,
'disable_auto_if': self.disable_auto_if_method,
})

@property
def value(self):
return 'disabled' if not self.held_object else self.held_object.value

def as_disabled(self):
return FeatureOptionHolder(None, self.name, None)

@noPosargs
@permittedKwargs({})
def enabled_method(self, args, kwargs):
return self.held_object.is_enabled()
return self.value == 'enabled'

@noPosargs
@permittedKwargs({})
def disabled_method(self, args, kwargs):
return self.held_object.is_disabled()
return self.value == 'disabled'

@noPosargs
@permittedKwargs({})
def allowed_method(self, args, kwargs):
return not self.value == 'disabled'

@noPosargs
@permittedKwargs({})
def auto_method(self, args, kwargs):
return self.held_object.is_auto()
return self.value == 'auto'

@permittedKwargs({'error_message'})
def require_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.')
error_message = kwargs.pop('error_message', '')
if error_message and not isinstance(error_message, str):
raise InterpreterException("Error message must be a string.")
if args[0]:
return self

if self.value == 'enabled':
prefix = 'Feature {} cannot be enabled'.format(self.name)
prefix = prefix + ': ' if error_message else ''
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):

Expand Down
2 changes: 1 addition & 1 deletion run_unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6719,7 +6719,7 @@ def test_pkgconfig_gen(self):
self.assertEqual(libhello_nolib.get_compile_args(), [])
self.assertEqual(libhello_nolib.get_pkgconfig_variable('foo', {}), 'bar')
self.assertEqual(libhello_nolib.get_pkgconfig_variable('prefix', {}), self.prefix)
self.assertEqual(libhello_nolib.get_pkgconfig_variable('escaped_var', {}), 'hello\ world')
self.assertEqual(libhello_nolib.get_pkgconfig_variable('escaped_var', {}), r'hello\ world')
self.assertEqual(libhello_nolib.get_pkgconfig_variable('unescaped_var', {}), 'hello world')

cc = env.detect_c_compiler(MachineChoice.HOST)
Expand Down
15 changes: 15 additions & 0 deletions test cases/common/192 feature option/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,33 @@ 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(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')
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')
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')
Expand Down
2 changes: 2 additions & 0 deletions test cases/failing/111 feature require/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
project('no fallback', 'c')
foo = get_option('reqfeature').require(false, error_message: 'frobnicator not available')
2 changes: 2 additions & 0 deletions test cases/failing/111 feature require/meson_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
option('reqfeature', type : 'feature', value : 'enabled', description : 'A required feature')

8 changes: 8 additions & 0 deletions test cases/failing/111 feature require/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"stdout": [
{
"match": "re",
"line": ".*/meson\\.build:2:0: ERROR: Feature reqfeature cannot be enabled: frobnicator not available"
}
]
}

0 comments on commit a4a61b6

Please sign in to comment.