From 4af6e50391dc44d530f5a11ead0a149c49cfd28a Mon Sep 17 00:00:00 2001 From: Nico Schmidt <73886020+nicosmd@users.noreply.github.com> Date: Thu, 19 Oct 2023 16:24:37 +0200 Subject: [PATCH] add cmakedeps listening to component_version property (#14808) * add cmakedeps listening to component_version property * use cmakedeps interface for properties and add integration test * change it to system_package_version * add PkgConfigDeps too --------- Co-authored-by: memsharded --- .../cmakedeps/templates/config_version.py | 4 +- conan/tools/gnu/pkgconfigdeps.py | 4 +- .../cmake/cmakedeps/test_cmakedeps.py | 45 +++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/conan/tools/cmake/cmakedeps/templates/config_version.py b/conan/tools/cmake/cmakedeps/templates/config_version.py index 261605660df..8fdaa234c57 100644 --- a/conan/tools/cmake/cmakedeps/templates/config_version.py +++ b/conan/tools/cmake/cmakedeps/templates/config_version.py @@ -25,7 +25,9 @@ def context(self): policy = "SameMajorVersion" if policy not in ("AnyNewerVersion", "SameMajorVersion", "SameMinorVersion", "ExactVersion"): raise ConanException(f"Unknown cmake_config_version_compat={policy} in {self.conanfile}") - return {"version": self.conanfile.ref.version, + version = self.cmakedeps.get_property("system_package_version", self.conanfile) + version = version or self.conanfile.ref.version + return {"version": version, "policy": policy} @property diff --git a/conan/tools/gnu/pkgconfigdeps.py b/conan/tools/gnu/pkgconfigdeps.py index 1aa86e9d472..bf0c4bebdc6 100644 --- a/conan/tools/gnu/pkgconfigdeps.py +++ b/conan/tools/gnu/pkgconfigdeps.py @@ -181,7 +181,9 @@ def _get_context(self, info): } if info.cpp_info is not None: context.update({ - "version": info.cpp_info.get_property("component_version") or self._dep.ref.version, + "version": (info.cpp_info.get_property("component_version") or + info.cpp_info.get_property("system_package_version") or + self._dep.ref.version), "cflags": self._get_cflags([d for d in pc_variables if d.startswith("includedir")], info.cpp_info), "libflags": self._get_lib_flags([d for d in pc_variables if d.startswith("libdir")], diff --git a/conans/test/integration/toolchains/cmake/cmakedeps/test_cmakedeps.py b/conans/test/integration/toolchains/cmake/cmakedeps/test_cmakedeps.py index 2732a48da9c..f316b89795d 100644 --- a/conans/test/integration/toolchains/cmake/cmakedeps/test_cmakedeps.py +++ b/conans/test/integration/toolchains/cmake/cmakedeps/test_cmakedeps.py @@ -630,6 +630,51 @@ def generate(self): assert expected in dep +class TestSystemPackageVersion: + def test_component_version(self): + c = TestClient() + dep = textwrap.dedent("""\ + from conan import ConanFile + class Pkg(ConanFile): + name = "dep" + version = "system" + def package_info(self): + self.cpp_info.set_property("system_package_version", "1.0") + self.cpp_info.components["mycomp"].set_property("component_version", "2.3") + """) + + c.save({"conanfile.py": dep}) + c.run("create .") + c.run("install --requires=dep/system -g CMakeDeps -g PkgConfigDeps") + dep = c.load("dep-config-version.cmake") + assert 'set(PACKAGE_VERSION "1.0")' in dep + dep = c.load("dep.pc") + assert 'Version: 1.0' in dep + dep = c.load("dep-mycomp.pc") + assert 'Version: 2.3' in dep + + def test_component_version_consumer(self): + c = TestClient() + app = textwrap.dedent("""\ + from conan import ConanFile + from conan.tools.cmake import CMakeDeps + class Pkg(ConanFile): + settings = "build_type" + requires = "dep/system" + def generate(self): + deps = CMakeDeps(self) + deps.set_property("dep", "system_package_version", "1.0") + deps.generate() + """) + + c.save({"dep/conanfile.py": GenConanfile("dep", "system"), + "app/conanfile.py": app}) + c.run("create dep") + c.run("install app") + dep = c.load("app/dep-config-version.cmake") + assert 'set(PACKAGE_VERSION "1.0")' in dep + + def test_cmakedeps_set_property_overrides(): c = TestClient() app = textwrap.dedent("""\