diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md index 103f4d85c..111a6f05c 100644 --- a/docs/markdown/Reference-manual.md +++ b/docs/markdown/Reference-manual.md @@ -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 diff --git a/mesonbuild/interpreter/interpreterobjects.py b/mesonbuild/interpreter/interpreterobjects.py index 61720c847..e87feb9fa 100644 --- a/mesonbuild/interpreter/interpreterobjects.py +++ b/mesonbuild/interpreter/interpreterobjects.py @@ -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): diff --git a/run_unittests.py b/run_unittests.py index 21bb30e4d..6f5ec58b1 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -6719,7 +6719,7 @@ class LinuxlikeTests(BasePlatformTests): 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) diff --git a/test cases/common/192 feature option/meson.build b/test cases/common/192 feature option/meson.build index ef3fa2285..b5e26fac3 100644 --- a/test cases/common/192 feature option/meson.build +++ b/test cases/common/192 feature option/meson.build @@ -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') diff --git a/test cases/failing/111 feature require/meson.build b/test cases/failing/111 feature require/meson.build new file mode 100644 index 000000000..d976ae8d6 --- /dev/null +++ b/test cases/failing/111 feature require/meson.build @@ -0,0 +1,2 @@ +project('no fallback', 'c') +foo = get_option('reqfeature').require(false, error_message: 'frobnicator not available') diff --git a/test cases/failing/111 feature require/meson_options.txt b/test cases/failing/111 feature require/meson_options.txt new file mode 100644 index 000000000..d6f2ce656 --- /dev/null +++ b/test cases/failing/111 feature require/meson_options.txt @@ -0,0 +1,2 @@ +option('reqfeature', type : 'feature', value : 'enabled', description : 'A required feature') + diff --git a/test cases/failing/111 feature require/test.json b/test cases/failing/111 feature require/test.json new file mode 100644 index 000000000..7c4640de0 --- /dev/null +++ b/test cases/failing/111 feature require/test.json @@ -0,0 +1,8 @@ +{ + "stdout": [ + { + "match": "re", + "line": ".*/meson\\.build:2:0: ERROR: Feature reqfeature cannot be enabled: frobnicator not available" + } + ] +}