Skip to content

Commit

Permalink
add cmakedeps listening to component_version property (#14808)
Browse files Browse the repository at this point in the history
* 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 <james@conan.io>
  • Loading branch information
nicosmd and memsharded authored Oct 19, 2023
1 parent b82c154 commit 4af6e50
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 3 additions & 1 deletion conan/tools/cmake/cmakedeps/templates/config_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion conan/tools/gnu/pkgconfigdeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("""\
Expand Down

0 comments on commit 4af6e50

Please sign in to comment.