From 8efd9400922f6f8eb876f9ef8d0042679d836c90 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Thu, 22 Mar 2018 16:40:58 -0400 Subject: [PATCH] pkgconfig generator: Add required version --- mesonbuild/modules/pkgconfig.py | 56 ++++++++++++++++--- run_unittests.py | 14 ++--- .../51 pkgconfig-gen/dependencies/meson.build | 2 +- 3 files changed, 56 insertions(+), 16 deletions(-) diff --git a/mesonbuild/modules/pkgconfig.py b/mesonbuild/modules/pkgconfig.py index 79a4423b5..c85624c27 100644 --- a/mesonbuild/modules/pkgconfig.py +++ b/mesonbuild/modules/pkgconfig.py @@ -31,6 +31,7 @@ class DependenciesHelper: self.priv_libs = [] self.priv_reqs = [] self.cflags = [] + self.version_reqs = {} def add_pub_libs(self, libs): libs, reqs, cflags = self._process_libs(libs, True) @@ -57,12 +58,17 @@ class DependenciesHelper: processed_reqs.append(obj.generated_pc) elif hasattr(obj, 'pcdep'): pcdeps = mesonlib.listify(obj.pcdep) - processed_reqs += [i.name for i in pcdeps] + for d in pcdeps: + processed_reqs += d.name + self.add_version_reqs(d.name, obj.version_reqs) elif isinstance(obj, dependencies.PkgConfigDependency): if obj.found(): processed_reqs.append(obj.name) + self.add_version_reqs(obj.name, obj.version_reqs) elif isinstance(obj, str): - processed_reqs.append(obj) + name, version_req = self.split_version_req(obj) + processed_reqs.append(name) + self.add_version_reqs(name, version_req) elif isinstance(obj, dependencies.Dependency) and not obj.found(): pass else: @@ -83,12 +89,15 @@ class DependenciesHelper: for obj in libs: if hasattr(obj, 'pcdep'): pcdeps = mesonlib.listify(obj.pcdep) - processed_reqs += [i.name for i in pcdeps] + for d in pcdeps: + processed_reqs += d.name + self.add_version_reqs(d.name, obj.version_reqs) elif hasattr(obj, 'generated_pc'): processed_reqs.append(obj.generated_pc) elif isinstance(obj, dependencies.PkgConfigDependency): if obj.found(): processed_reqs.append(obj.name) + self.add_version_reqs(obj.name, obj.version_reqs) elif isinstance(obj, dependencies.ThreadDependency): processed_libs += obj.get_compiler().thread_link_flags(obj.env) processed_cflags += obj.get_compiler().thread_flags(obj.env) @@ -123,6 +132,36 @@ class DependenciesHelper: return processed_libs, processed_reqs, processed_cflags + def add_version_reqs(self, name, version_reqs): + if version_reqs: + vreqs = self.version_reqs.get(name, []) + vreqs += mesonlib.stringlistify(version_reqs) + self.version_reqs[name] = vreqs + + def split_version_req(self, s): + for op in ['>=', '<=', '!=', '==', '=', '>', '<']: + pos = s.find(op) + if pos > 0: + return s[0:pos].strip(), s[pos:].strip() + return s, None + + def format_vreq(self, vreq): + # vreq are '>=1.0' and pkgconfig wants '>= 1.0' + for op in ['>=', '<=', '!=', '==', '=', '>', '<']: + if vreq.startswith(op): + return op + ' ' + vreq[len(op):] + return vreq + + def format_reqs(self, reqs): + result = [] + for name in reqs: + vreqs = self.version_reqs.get(name, None) + if vreqs: + result += [name + ' ' + self.format_vreq(vreq) for vreq in vreqs] + else: + result += [name] + return ', '.join(result) + def remove_dups(self): def _fn(xs): # Remove duplicates whilst preserving original order @@ -207,11 +246,12 @@ class PkgConfigModule(ExtensionModule): if len(url) > 0: ofile.write('URL: %s\n' % url) ofile.write('Version: %s\n' % version) - if len(deps.pub_reqs) > 0: - ofile.write('Requires: {}\n'.format(' '.join(deps.pub_reqs))) - if len(deps.priv_reqs) > 0: - ofile.write( - 'Requires.private: {}\n'.format(' '.join(deps.priv_reqs))) + reqs_str = deps.format_reqs(deps.pub_reqs) + if len(reqs_str) > 0: + ofile.write('Requires: {}\n'.format(reqs_str)) + reqs_str = deps.format_reqs(deps.priv_reqs) + if len(reqs_str) > 0: + ofile.write('Requires.private: {}\n'.format(reqs_str)) if len(conflicts) > 0: ofile.write('Conflicts: {}\n'.format(' '.join(conflicts))) diff --git a/run_unittests.py b/run_unittests.py index 94e39c857..0c84475b9 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -2266,11 +2266,11 @@ class LinuxlikeTests(BasePlatformTests): os.environ['PKG_CONFIG_LIBDIR'] = os.pathsep.join([privatedir1, privatedir2]) cmd = ['pkg-config', 'dependency-test'] - out = self._run(cmd + ['--print-requires']).strip().split() + out = self._run(cmd + ['--print-requires']).strip().split('\n') self.assertEqual(sorted(out), sorted(['libexposed'])) - out = self._run(cmd + ['--print-requires-private']).strip().split() - self.assertEqual(sorted(out), sorted(['libfoo'])) + out = self._run(cmd + ['--print-requires-private']).strip().split('\n') + self.assertEqual(sorted(out), sorted(['libfoo >= 1.0'])) out = self._run(cmd + ['--cflags-only-other']).strip().split() self.assertEqual(sorted(out), sorted(['-pthread', '-DCUSTOM'])) @@ -2286,12 +2286,12 @@ class LinuxlikeTests(BasePlatformTests): '-lfoo'])) cmd = ['pkg-config', 'requires-test'] - out = self._run(cmd + ['--print-requires']).strip().split() - self.assertEqual(sorted(out), sorted(['libexposed', 'libfoo', 'libhello'])) + out = self._run(cmd + ['--print-requires']).strip().split('\n') + self.assertEqual(sorted(out), sorted(['libexposed', 'libfoo >= 1.0', 'libhello'])) cmd = ['pkg-config', 'requires-private-test'] - out = self._run(cmd + ['--print-requires-private']).strip().split() - self.assertEqual(sorted(out), sorted(['libexposed', 'libfoo', 'libhello'])) + out = self._run(cmd + ['--print-requires-private']).strip().split('\n') + self.assertEqual(sorted(out), sorted(['libexposed', 'libfoo >= 1.0', 'libhello'])) def test_pkg_unfound(self): testdir = os.path.join(self.unit_test_dir, '22 unfound pkgconfig') diff --git a/test cases/common/51 pkgconfig-gen/dependencies/meson.build b/test cases/common/51 pkgconfig-gen/dependencies/meson.build index 2e0094355..822a7b772 100644 --- a/test cases/common/51 pkgconfig-gen/dependencies/meson.build +++ b/test cases/common/51 pkgconfig-gen/dependencies/meson.build @@ -14,7 +14,7 @@ pkgg.generate(libraries : exposed_lib, ) # Declare a few different Dependency objects -pc_dep = dependency('libfoo') +pc_dep = dependency('libfoo', version : '>=1.0') notfound_dep = dependency('notfound', required : false) threads_dep = dependency('threads') custom_dep = declare_dependency(link_args : ['-lcustom'], compile_args : ['-DCUSTOM'])