Skip to content

Commit

Permalink
fix CMakeDeps.set_property() (#14813)
Browse files Browse the repository at this point in the history
* fix CMakeDeps.set_property()

* more tests
  • Loading branch information
memsharded authored Sep 26, 2023
1 parent 5c91983 commit fdba041
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
2 changes: 1 addition & 1 deletion conan/tools/cmake/cmakedeps/templates/config_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def filename(self):

@property
def context(self):
policy = self.conanfile.cpp_info.get_property("cmake_config_version_compat")
policy = self.cmakedeps.get_property("cmake_config_version_compat", self.conanfile)
if policy is None:
policy = "SameMajorVersion"
if policy not in ("AnyNewerVersion", "SameMajorVersion", "SameMinorVersion", "ExactVersion"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def context(self):
for components_target_name in components_targets_names]

is_win = self.conanfile.settings.get_safe("os") == "Windows"
auto_link = self.conanfile.cpp_info.get_property("cmake_set_interface_link_directories")
auto_link = self.cmakedeps.get_property("cmake_set_interface_link_directories",
self.conanfile)
return {"pkg_name": self.pkg_name,
"root_target_name": self.root_target_name,
"config_suffix": self.config_suffix,
Expand Down
13 changes: 8 additions & 5 deletions conan/tools/cmake/cmakedeps/templates/target_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ def _get_global_cpp_cmake(self):
global_cppinfo = self.conanfile.cpp_info.aggregated_components()
pfolder_var_name = "{}_PACKAGE_FOLDER{}".format(self.pkg_name, self.config_suffix)
return _TargetDataContext(global_cppinfo, pfolder_var_name, self._root_folder,
self.require, self.cmake_package_type, self.is_host_windows)
self.require, self.cmake_package_type, self.is_host_windows,
self.conanfile, self.cmakedeps)

@property
def _root_folder(self):
Expand All @@ -199,7 +200,8 @@ def _get_required_components_cpp(self):
for comp_name, comp in sorted_comps.items():
deps_cpp_cmake = _TargetDataContext(comp, pfolder_var_name, self._root_folder,
self.require, self.cmake_package_type,
self.is_host_windows)
self.is_host_windows, self.conanfile, self.cmakedeps,
comp_name)

public_comp_deps = []
for required_pkg, required_comp in comp.parsed_requires():
Expand Down Expand Up @@ -260,7 +262,7 @@ def _get_dependencies_find_modes(self):
class _TargetDataContext(object):

def __init__(self, cpp_info, pfolder_var_name, package_folder, require, library_type,
is_host_windows):
is_host_windows, conanfile, cmakedeps, comp_name=None):

def join_paths(paths):
"""
Expand Down Expand Up @@ -346,7 +348,8 @@ def join_defines(values, prefix=""):
if require and not require.run:
self.bin_paths = ""

build_modules = cpp_info.get_property("cmake_build_modules") or []
build_modules = cmakedeps.get_property("cmake_build_modules", conanfile) or []
self.build_modules_paths = join_paths(build_modules)
# SONAME flag only makes sense for SHARED libraries
self.no_soname = str((cpp_info.get_property("nosoname") if self.library_type == "SHARED" else False) or False).upper()
nosoname = cmakedeps.get_property("nosoname", conanfile, comp_name)
self.no_soname = str((nosoname if self.library_type == "SHARED" else False) or False).upper()
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,68 @@ def package_info(self):
c.run("create .")
c.run("install --requires=dep/0.1 -g CMakeDeps", assert_error=True)
assert "Unknown cmake_config_version_compat=Unknown in dep/0.1" in c.out

def test_cmake_version_config_compatibility_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/0.1"
def generate(self):
deps = CMakeDeps(self)
deps.set_property("dep", "cmake_config_version_compat", "AnyNewerVersion")
deps.generate()
""")

c.save({"dep/conanfile.py": GenConanfile("dep", "0.1"),
"app/conanfile.py": app})
c.run("create dep")
c.run("install app")
dep = c.load("app/dep-config-version.cmake")
expected = textwrap.dedent("""\
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()""")
assert expected in dep


def test_cmakedeps_set_property_overrides():
c = TestClient()
app = textwrap.dedent("""\
import os
from conan import ConanFile
from conan.tools.cmake import CMakeDeps
class Pkg(ConanFile):
settings = "build_type"
requires = "dep/0.1", "other/0.1"
def generate(self):
deps = CMakeDeps(self)
# Need the absolute path inside package
dep = self.dependencies["dep"].package_folder
deps.set_property("dep", "cmake_build_modules", [os.path.join(dep, "my_module1")])
deps.set_property("dep", "nosoname", True)
deps.set_property("other::mycomp1", "nosoname", True)
deps.generate()
""")

pkg_info = {"components": {"mycomp1": {"libs": ["mylib"]}}}
c.save({"dep/conanfile.py": GenConanfile("dep", "0.1").with_package_type("shared-library"),
"other/conanfile.py": GenConanfile("other", "0.1").with_package_type("shared-library")
.with_package_info(pkg_info, {}),
"app/conanfile.py": app})
c.run("create dep")
c.run("create other")
c.run("install app")
dep = c.load("app/dep-release-data.cmake")
assert 'set(dep_BUILD_MODULES_PATHS_RELEASE "${dep_PACKAGE_FOLDER_RELEASE}/my_module1")' in dep
assert 'set(dep_NO_SONAME_MODE_RELEASE TRUE)' in dep
other = c.load("app/other-release-data.cmake")
assert 'set(other_other_mycomp1_NO_SONAME_MODE_RELEASE TRUE)' in other

0 comments on commit fdba041

Please sign in to comment.