dependencies: Add ability to set arbitrary variables on

declare_dependencies

This allows dependencies declared in subprojects to set variables, and
for those variables to be accessed via the get_variable method, just
like those from pkg-config and cmake. This makes it easier to use
projects from subprojects in a polymorphic manner, lowering the
distinction between a subproject and an external dependency every
further.
pull/6423/head
Dylan Baker 5 years ago
parent ff822990d1
commit cd895be99a
  1. 2
      mesonbuild/build.py
  2. 28
      mesonbuild/dependencies/base.py
  3. 14
      mesonbuild/interpreter.py
  4. 2
      mesonbuild/modules/gnome.py
  5. 13
      test cases/common/218 dependency get_variable method/meson.build

@ -1053,7 +1053,7 @@ This will become a hard error in a future Meson release.''')
[], [],
dep.get_compile_args(), dep.get_compile_args(),
dep.get_link_args(), dep.get_link_args(),
[], [], [], []) [], [], [], [], {})
self.external_deps.append(extpart) self.external_deps.append(extpart)
# Deps of deps. # Deps of deps.
self.add_deps(dep.ext_deps) self.add_deps(dep.ext_deps)

@ -223,7 +223,8 @@ class Dependency:
self.ext_deps.append(dep_type(env, kwargs)) self.ext_deps.append(dep_type(env, kwargs))
def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None, 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]]: pkgconfig_define: T.Optional[T.List[str]] = None) -> T.Union[str, T.List[str]]:
if default_value is not None: if default_value is not None:
return default_value return default_value
@ -235,7 +236,8 @@ class Dependency:
return new_dep return new_dep
class InternalDependency(Dependency): 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', {}) super().__init__('internal', {})
self.version = version self.version = version
self.is_found = True self.is_found = True
@ -246,6 +248,7 @@ class InternalDependency(Dependency):
self.whole_libraries = whole_libraries self.whole_libraries = whole_libraries
self.sources = sources self.sources = sources
self.ext_deps = ext_deps self.ext_deps = ext_deps
self.variables = variables
def get_pkgconfig_variable(self, variable_name, kwargs): def get_pkgconfig_variable(self, variable_name, kwargs):
raise DependencyException('Method "get_pkgconfig_variable()" is ' raise DependencyException('Method "get_pkgconfig_variable()" is '
@ -270,7 +273,17 @@ class InternalDependency(Dependency):
return InternalDependency( return InternalDependency(
self.version, final_includes, final_compile_args, self.version, final_includes, final_compile_args,
final_link_args, final_libraries, final_whole_libraries, 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: class HasNativeKwarg:
def __init__(self, kwargs): def __init__(self, kwargs):
@ -554,7 +567,8 @@ class ConfigToolDependency(ExternalDependency):
return self.type_name return self.type_name
def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None, 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]]: pkgconfig_define: T.Optional[T.List[str]] = None) -> T.Union[str, T.List[str]]:
if configtool: if configtool:
# In the not required case '' (empty string) will be returned if the # In the not required case '' (empty string) will be returned if the
@ -1007,7 +1021,8 @@ class PkgConfigDependency(ExternalDependency):
return self.type_name return self.type_name
def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None, 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]]: pkgconfig_define: T.Optional[T.List[str]] = None) -> T.Union[str, T.List[str]]:
if pkgconfig: if pkgconfig:
kwargs = {} kwargs = {}
@ -1549,7 +1564,8 @@ project(MesonTemp LANGUAGES {})
return '' return ''
def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None, 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]]: pkgconfig_define: T.Optional[T.List[str]] = None) -> T.Union[str, T.List[str]]:
if cmake: if cmake:
try: try:

@ -483,7 +483,8 @@ class DependencyHolder(InterpreterObject, ObjectHolder):
@FeatureNew('dep.get_variable', '0.51.0') @FeatureNew('dep.get_variable', '0.51.0')
@noPosargs @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): def variable_method(self, args, kwargs):
return self.held_object.get_variable(**kwargs) return self.held_object.get_variable(**kwargs)
@ -2087,6 +2088,7 @@ permitted_kwargs = {'add_global_arguments': {'language', 'native'},
'link_args', 'link_args',
'link_whole', 'link_whole',
'version', 'version',
'variables',
}, },
'executable': build.known_exe_kwargs, 'executable': build.known_exe_kwargs,
'find_program': {'required', 'native', 'version', 'dirs'}, '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] 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.46.0', ['link_whole'])
@FeatureNewKwargs('declare_dependency', '0.54.0', ['variables'])
@permittedKwargs(permitted_kwargs['declare_dependency']) @permittedKwargs(permitted_kwargs['declare_dependency'])
@noPosargs @noPosargs
def func_declare_dependency(self, node, args, kwargs): def func_declare_dependency(self, node, args, kwargs):
@ -2399,6 +2402,12 @@ class Interpreter(InterpreterBase):
deps = extract_as_list(kwargs, 'dependencies', unholder=True) deps = extract_as_list(kwargs, 'dependencies', unholder=True)
compile_args = mesonlib.stringlistify(kwargs.get('compile_args', [])) compile_args = mesonlib.stringlistify(kwargs.get('compile_args', []))
link_args = mesonlib.stringlistify(kwargs.get('link_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 = [] final_deps = []
for d in deps: for d in deps:
try: try:
@ -2413,7 +2422,8 @@ class Interpreter(InterpreterBase):
raise InterpreterException('''Entries in "link_with" may only be self-built targets, raise InterpreterException('''Entries in "link_with" may only be self-built targets,
external dependencies (including libraries) must go to "dependencies".''') external dependencies (including libraries) must go to "dependencies".''')
dep = dependencies.InternalDependency(version, incs, compile_args, 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) return DependencyHolder(dep, self.subproject)
@noKwargs @noKwargs

@ -1692,7 +1692,7 @@ G_END_DECLS'''
# - add relevant directories to include dirs # - add relevant directories to include dirs
incs = [build.IncludeDirs(state.subdir, ['.'] + vapi_includes, False)] incs = [build.IncludeDirs(state.subdir, ['.'] + vapi_includes, False)]
sources = [vapi_target] + vapi_depends sources = [vapi_target] + vapi_depends
rv = InternalDependency(None, incs, [], [], link_with, [], sources, []) rv = InternalDependency(None, incs, [], [], link_with, [], sources, [], {})
created_values.append(rv) created_values.append(rv)
return ModuleReturnValue(rv, created_values) return ModuleReturnValue(rv, created_values)

@ -47,6 +47,15 @@ else
'cmake config-tool got default when we shouldn\'t have.') 'cmake config-tool got default when we shouldn\'t have.')
endif 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() idep = declare_dependency()
assert(idep.get_variable(pkgconfig : 'foo', cmake : 'foo', configtool : 'foo', default_value : default) == default, assert(idep.get_variable(pkgconfig : 'foo', cmake : 'foo', configtool : 'foo',
'Got something other than default from an internal dependency') default_value : default) == default,
'something went wrong with an InternalDependency with no variables.')

Loading…
Cancel
Save