diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md index a70c94ba7..d81be4756 100644 --- a/docs/markdown/Reference-manual.md +++ b/docs/markdown/Reference-manual.md @@ -2678,7 +2678,7 @@ an external dependency with the following methods: - includes: any include_directories - sources: any compiled or static sources the dependency has - - `get_variable(cmake : str, pkgconfig : str, configtool : str, + - `get_variable(varname, cmake : str, pkgconfig : str, configtool : str, internal: str, default_value : str, pkgconfig_define : [str, str])` *(since 0.51.0)*: a generic variable getter method, which replaces the get_*type*_variable methods. This allows one to get the variable @@ -2686,8 +2686,12 @@ an external dependency with the following methods: was found. If default_value is set and the value cannot be gotten from the object then default_value is returned, if it is not set then an error is raised. - *(since 0.54.0)* added `internal` keyword. + *(since 0.58.0)* added `varname` as first positional argument. It is used as + default value for `cmake`, `pkgconfig`, `configtool` and `internal` keyword + arguments. It is useful in the common case where `pkgconfig` and `internal` + use the same variable name, in which case it's easier to write `dep.get_variable('foo')` + instead of `dep.get_variable(pkgconfig: 'foo', internal: 'foo')`. ### `disabler` object diff --git a/docs/markdown/snippets/dep_get_variable.md b/docs/markdown/snippets/dep_get_variable.md new file mode 100644 index 000000000..54373d963 --- /dev/null +++ b/docs/markdown/snippets/dep_get_variable.md @@ -0,0 +1,8 @@ +## `dep.get_variable(varname)` + +`dep.get_variable()` now has `varname` as first positional argument. +It is used as default value for `cmake`, `pkgconfig`, `configtool` and `internal` +keyword arguments. It is useful in the common case where `pkgconfig` and `internal` +use the same variable name, in which case it's easier to write `dep.get_variable('foo')` +instead of `dep.get_variable(pkgconfig: 'foo', internal: 'foo')`. + diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 782397158..ac0d786c6 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -512,10 +512,15 @@ class DependencyHolder(InterpreterObject, ObjectHolder[Dependency]): return DependencyHolder(pdep, self.subproject) @FeatureNew('dep.get_variable', '0.51.0') - @noPosargs + @typed_pos_args('dep.get_variable', optargs=[str]) @permittedKwargs({'cmake', 'pkgconfig', 'configtool', 'internal', 'default_value', 'pkgconfig_define'}) @FeatureNewKwargs('dep.get_variable', '0.54.0', ['internal']) - def variable_method(self, args, kwargs): + def variable_method(self, args: T.Tuple[T.Optional[str]], kwargs: T.Dict[str, T.Any]) -> str: + default_varname = args[0] + if default_varname is not None: + FeatureNew('0.58.0', 'Positional argument to dep.get_variable()').use(self.subproject) + for k in ['cmake', 'pkgconfig', 'configtool', 'internal']: + kwargs.setdefault(k, default_varname) return self.held_object.get_variable(**kwargs) @FeatureNew('dep.include_type', '0.52.0') diff --git a/test cases/common/212 dependency get_variable method/meson.build b/test cases/common/212 dependency get_variable method/meson.build index 9f60836e2..384b3f3d2 100644 --- a/test cases/common/212 dependency get_variable method/meson.build +++ b/test cases/common/212 dependency get_variable method/meson.build @@ -62,3 +62,5 @@ assert(idep.get_variable(pkgconfig : 'foo', cmake : 'foo', configtool : 'foo', idep = declare_dependency(variables : ['foo=value']) assert(idep.get_variable(internal: 'foo') == 'value') +assert(idep.get_variable('foo') == 'value') +assert(idep.get_variable('invalid', internal: 'foo') == 'value')