diff --git a/mesonbuild/build.py b/mesonbuild/build.py index 72b42767d..12274a6b4 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -1053,7 +1053,7 @@ This will become a hard error in a future Meson release.''') [], dep.get_compile_args(), dep.get_link_args(), - [], [], [], []) + [], [], [], [], {}) self.external_deps.append(extpart) # Deps of deps. self.add_deps(dep.ext_deps) diff --git a/mesonbuild/dependencies/base.py b/mesonbuild/dependencies/base.py index e510934b0..189096190 100644 --- a/mesonbuild/dependencies/base.py +++ b/mesonbuild/dependencies/base.py @@ -223,7 +223,8 @@ class Dependency: self.ext_deps.append(dep_type(env, kwargs)) def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None, - configtool: T.Optional[str] = None, default_value: T.Optional[str] = None, + configtool: T.Optional[str] = None, internal: T.Optional[str] = None, + default_value: T.Optional[str] = None, pkgconfig_define: T.Optional[T.List[str]] = None) -> T.Union[str, T.List[str]]: if default_value is not None: return default_value @@ -235,7 +236,8 @@ class Dependency: return new_dep class InternalDependency(Dependency): - def __init__(self, version, incdirs, compile_args, link_args, libraries, whole_libraries, sources, ext_deps): + def __init__(self, version, incdirs, compile_args, link_args, libraries, + whole_libraries, sources, ext_deps, variables: T.Dict[str, T.Any]): super().__init__('internal', {}) self.version = version self.is_found = True @@ -246,6 +248,7 @@ class InternalDependency(Dependency): self.whole_libraries = whole_libraries self.sources = sources self.ext_deps = ext_deps + self.variables = variables def get_pkgconfig_variable(self, variable_name, kwargs): raise DependencyException('Method "get_pkgconfig_variable()" is ' @@ -270,7 +273,17 @@ class InternalDependency(Dependency): return InternalDependency( self.version, final_includes, final_compile_args, final_link_args, final_libraries, final_whole_libraries, - final_sources, final_deps) + final_sources, final_deps, self.variables) + + def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None, + configtool: T.Optional[str] = None, internal: T.Optional[str] = None, + default_value: T.Optional[str] = None, + pkgconfig_define: T.Optional[T.List[str]] = None) -> T.Union[str, T.List[str]]: + val = self.variables.get(internal, default_value) + if val is not None: + return val + raise DependencyException('Could not get an internal variable and no default provided for {!r}'.format(self)) + class HasNativeKwarg: def __init__(self, kwargs): @@ -554,7 +567,8 @@ class ConfigToolDependency(ExternalDependency): return self.type_name def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None, - configtool: T.Optional[str] = None, default_value: T.Optional[str] = None, + configtool: T.Optional[str] = None, internal: T.Optional[str] = None, + default_value: T.Optional[str] = None, pkgconfig_define: T.Optional[T.List[str]] = None) -> T.Union[str, T.List[str]]: if configtool: # In the not required case '' (empty string) will be returned if the @@ -1007,7 +1021,8 @@ class PkgConfigDependency(ExternalDependency): return self.type_name def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None, - configtool: T.Optional[str] = None, default_value: T.Optional[str] = None, + configtool: T.Optional[str] = None, internal: T.Optional[str] = None, + default_value: T.Optional[str] = None, pkgconfig_define: T.Optional[T.List[str]] = None) -> T.Union[str, T.List[str]]: if pkgconfig: kwargs = {} @@ -1549,7 +1564,8 @@ project(MesonTemp LANGUAGES {}) return '' def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None, - configtool: T.Optional[str] = None, default_value: T.Optional[str] = None, + configtool: T.Optional[str] = None, internal: T.Optional[str] = None, + default_value: T.Optional[str] = None, pkgconfig_define: T.Optional[T.List[str]] = None) -> T.Union[str, T.List[str]]: if cmake: try: diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 4dfb8b38c..bdbcc3869 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -483,7 +483,8 @@ class DependencyHolder(InterpreterObject, ObjectHolder): @FeatureNew('dep.get_variable', '0.51.0') @noPosargs - @permittedKwargs({'cmake', 'pkgconfig', 'configtool', 'default_value', 'pkgconfig_define'}) + @permittedKwargs({'cmake', 'pkgconfig', 'configtool', 'internal', 'default_value', 'pkgconfig_define'}) + @FeatureNewKwargs('dep.get_variable', '0.54.0', ['internal']) def variable_method(self, args, kwargs): return self.held_object.get_variable(**kwargs) @@ -2087,6 +2088,7 @@ permitted_kwargs = {'add_global_arguments': {'language', 'native'}, 'link_args', 'link_whole', 'version', + 'variables', }, 'executable': build.known_exe_kwargs, 'find_program': {'required', 'native', 'version', 'dirs'}, @@ -2385,6 +2387,7 @@ class Interpreter(InterpreterBase): return [mesonlib.File.from_source_file(self.environment.source_dir, self.subdir, fname) for fname in args] @FeatureNewKwargs('declare_dependency', '0.46.0', ['link_whole']) + @FeatureNewKwargs('declare_dependency', '0.54.0', ['variables']) @permittedKwargs(permitted_kwargs['declare_dependency']) @noPosargs def func_declare_dependency(self, node, args, kwargs): @@ -2399,6 +2402,12 @@ class Interpreter(InterpreterBase): deps = extract_as_list(kwargs, 'dependencies', unholder=True) compile_args = mesonlib.stringlistify(kwargs.get('compile_args', [])) link_args = mesonlib.stringlistify(kwargs.get('link_args', [])) + variables = kwargs.get('variables', {}) + if not isinstance(variables, dict): + raise InterpreterException('variables must be a dict.') + if not all(isinstance(v, str) for v in variables.values()): + # Because that is how they will come from pkg-config and cmake + raise InterpreterException('variables values be strings.') final_deps = [] for d in deps: try: @@ -2413,7 +2422,8 @@ class Interpreter(InterpreterBase): raise InterpreterException('''Entries in "link_with" may only be self-built targets, external dependencies (including libraries) must go to "dependencies".''') dep = dependencies.InternalDependency(version, incs, compile_args, - link_args, libs, libs_whole, sources, final_deps) + link_args, libs, libs_whole, sources, final_deps, + variables) return DependencyHolder(dep, self.subproject) @noKwargs diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py index 9bd7a996b..3d5d71813 100644 --- a/mesonbuild/modules/gnome.py +++ b/mesonbuild/modules/gnome.py @@ -1692,7 +1692,7 @@ G_END_DECLS''' # - add relevant directories to include dirs incs = [build.IncludeDirs(state.subdir, ['.'] + vapi_includes, False)] sources = [vapi_target] + vapi_depends - rv = InternalDependency(None, incs, [], [], link_with, [], sources, []) + rv = InternalDependency(None, incs, [], [], link_with, [], sources, [], {}) created_values.append(rv) return ModuleReturnValue(rv, created_values) diff --git a/test cases/common/218 dependency get_variable method/meson.build b/test cases/common/218 dependency get_variable method/meson.build index 72e65ee19..8c189bbe3 100644 --- a/test cases/common/218 dependency get_variable method/meson.build +++ b/test cases/common/218 dependency get_variable method/meson.build @@ -47,6 +47,15 @@ else 'cmake config-tool got default when we shouldn\'t have.') endif +idep = declare_dependency(variables : {'foo' : 'value'}) +assert(idep.get_variable(pkgconfig : 'foo', cmake : 'foo', configtool : 'foo', + internal : 'foo', default_value : default) == 'value', + 'internal got default when it shouldn\'t have.') +assert(idep.get_variable(pkgconfig : 'foo', cmake : 'foo', configtool : 'foo', + internal : 'bar', default_value : default) == default, + 'internal didn\'t default when it should have.') + idep = declare_dependency() -assert(idep.get_variable(pkgconfig : 'foo', cmake : 'foo', configtool : 'foo', default_value : default) == default, - 'Got something other than default from an internal dependency') +assert(idep.get_variable(pkgconfig : 'foo', cmake : 'foo', configtool : 'foo', + default_value : default) == default, + 'something went wrong with an InternalDependency with no variables.')