diff options
author | Xavier Claessens <xavier.claessens@collabora.com> | 2020-10-15 09:56:08 -0400 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2020-10-16 18:09:56 +0300 |
commit | bcf369ea3c6ac9d48759a9a11304a853dfdab5ff (patch) | |
tree | 3e5080da666144231c1c36ceaeb62fb3727df07a /mesonbuild | |
parent | 2e80c521295f45105229e5c7bffa3ebfd60b3445 (diff) | |
download | meson-bcf369ea3c6ac9d48759a9a11304a853dfdab5ff.zip meson-bcf369ea3c6ac9d48759a9a11304a853dfdab5ff.tar.gz meson-bcf369ea3c6ac9d48759a9a11304a853dfdab5ff.tar.bz2 |
Fix consistency in variables kwarg
Share common code to extract the `variables` kwarg in
declare_dependency() and pkg.generate().
Diffstat (limited to 'mesonbuild')
-rw-r--r-- | mesonbuild/interpreter.py | 33 | ||||
-rw-r--r-- | mesonbuild/modules/pkgconfig.py | 26 |
2 files changed, 34 insertions, 25 deletions
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index ad6f04e..cd201d0 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -2680,6 +2680,32 @@ class Interpreter(InterpreterBase): def func_files(self, node, args, kwargs): return [mesonlib.File.from_source_file(self.environment.source_dir, self.subdir, fname) for fname in args] + # Used by declare_dependency() and pkgconfig.generate() + def extract_variables(self, kwargs, argname='variables', list_new=False, dict_new=False): + variables = kwargs.get(argname, {}) + if isinstance(variables, dict): + if dict_new and variables: + FeatureNew.single_use('variables as dictionary', '0.56.0', self.subproject) + else: + varlist = mesonlib.stringlistify(variables) + if list_new: + FeatureNew.single_use('variables as list of strings', '0.56.0', self.subproject) + variables = {} + for v in varlist: + try: + (key, value) = v.split('=', 1) + except ValueError: + raise InterpreterException('Variable {!r} must have a value separated by equals sign.'.format(v)) + variables[key.strip()] = value.strip() + for k, v in variables.items(): + if not k or not v: + raise InterpreterException('Empty variable name or value') + if any(c.isspace() for c in k): + raise InterpreterException('Invalid whitespace in variable name "{}"'.format(k)) + if not isinstance(v, str): + raise InterpreterException('variables values must be strings.') + return variables + @FeatureNewKwargs('declare_dependency', '0.46.0', ['link_whole']) @FeatureNewKwargs('declare_dependency', '0.54.0', ['variables']) @permittedKwargs(permitted_kwargs['declare_dependency']) @@ -2696,12 +2722,7 @@ class Interpreter(InterpreterBase): deps = unholder(extract_as_list(kwargs, 'dependencies')) 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.') + variables = self.extract_variables(kwargs, list_new=True) final_deps = [] for d in deps: try: diff --git a/mesonbuild/modules/pkgconfig.py b/mesonbuild/modules/pkgconfig.py index 912c9ff..38c96a9 100644 --- a/mesonbuild/modules/pkgconfig.py +++ b/mesonbuild/modules/pkgconfig.py @@ -513,31 +513,17 @@ class PkgConfigModule(ExtensionModule): deps.remove_dups() - def parse_variable_list(stringlist): + def parse_variable_list(vardict): reserved = ['prefix', 'libdir', 'includedir'] variables = [] - for var in stringlist: - # foo=bar=baz is ('foo', 'bar=baz') - l = var.split('=', 1) - if len(l) < 2: - raise mesonlib.MesonException('Invalid variable "{}". Variables must be in \'name=value\' format'.format(var)) - - name, value = l[0].strip(), l[1].strip() - if not name or not value: - raise mesonlib.MesonException('Invalid variable "{}". Variables must be in \'name=value\' format'.format(var)) - - # Variable names must not contain whitespaces - if any(c.isspace() for c in name): - raise mesonlib.MesonException('Invalid whitespace in assignment "{}"'.format(var)) - + for name, value in vardict.items(): if name in reserved: raise mesonlib.MesonException('Variable "{}" is reserved'.format(name)) - variables.append((name, value)) - return variables - variables = parse_variable_list(mesonlib.stringlistify(kwargs.get('variables', []))) + variables = self.interpreter.extract_variables(kwargs, dict_new=True) + variables = parse_variable_list(variables) pcfile = filebase + '.pc' pkgroot = kwargs.get('install_dir', default_install_dir) @@ -552,7 +538,9 @@ class PkgConfigModule(ExtensionModule): version, pcfile, conflicts, variables, False, dataonly) res = build.Data(mesonlib.File(True, state.environment.get_scratch_dir(), pcfile), pkgroot) - variables = parse_variable_list(mesonlib.stringlistify(kwargs.get('uninstalled_variables', []))) + variables = self.interpreter.extract_variables(kwargs, argname='uninstalled_variables', dict_new=True) + variables = parse_variable_list(variables) + pcfile = filebase + '-uninstalled.pc' self.generate_pkgconfig_file(state, deps, subdirs, name, description, url, version, pcfile, conflicts, variables, |