Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cmake_target_aliases set from CMakeDeps #17200

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion conan/tools/cmake/cmakedeps/cmakedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def set_property(self, dep, prop, value, build_context=False):
"""
Using this method you can overwrite the :ref:`property<CMakeDeps Properties>` values set by
the Conan recipes from the consumer. This can be done for `cmake_file_name`, `cmake_target_name`,
`cmake_find_mode`, `cmake_module_file_name` and `cmake_module_target_name` properties.
`cmake_find_mode`, `cmake_module_file_name`, `cmake_module_target_name`, `cmake_additional_variables_prefixes`,
`cmake_config_version_compat`, `system_package_version`, `cmake_set_interface_link_directories`,
`cmake_build_modules`, `nosoname`, and `cmake_target_aliases`.

:param dep: Name of the dependency to set the :ref:`property<CMakeDeps Properties>`. For
components use the syntax: ``dep_name::component_name``.
Expand Down
13 changes: 7 additions & 6 deletions conan/tools/cmake/cmakedeps/templates/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ def context(self):
target_pattern = "" if not self.generating_module else "module-"
target_pattern += "{}-Target-*.cmake".format(self.file_name)

cmake_target_aliases = self.conanfile.cpp_info.\
get_property("cmake_target_aliases", check_type=list) or dict()
cmake_target_aliases = self.cmakedeps.get_property("cmake_target_aliases",
self.conanfile,
check_type=list) or dict()

target = self.root_target_name
cmake_target_aliases = {alias: target for alias in cmake_target_aliases}

cmake_component_target_aliases = dict()
for comp_name in self.conanfile.cpp_info.components:
if comp_name is not None:
aliases = \
self.conanfile.cpp_info.components[comp_name].\
get_property("cmake_target_aliases", check_type=list) or dict()

aliases = self.cmakedeps.get_property("cmake_target_aliases",
self.conanfile,
comp_name=comp_name,
check_type=list) or dict()
target = self.get_component_alias(self.conanfile, comp_name)
cmake_component_target_aliases[comp_name] = {alias: target for alias in aliases}

Expand Down
39 changes: 39 additions & 0 deletions test/integration/toolchains/cmake/cmakedeps/test_cmakedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,3 +843,42 @@ def generate(self):
c.run("create dep")
c.run("create app", assert_error=True)
assert 'The expected type for foo is "list", but "int" was found' in c.out


def test_alias_cmakedeps_set_property():
tc = TestClient()
tc.save({"dep/conanfile.py": textwrap.dedent("""

from conan import ConanFile
class Dep(ConanFile):
name = "dep"
version = "1.0"
settings = "os", "compiler", "build_type", "arch"
def package_info(self):
self.cpp_info.components["mycomp"].set_property("cmake_target_name", "dep::mycomponent")
"""),
"conanfile.py": textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import CMakeDeps, CMake
class Pkg(ConanFile):
name = "pkg"
version = "1.0"
settings = "os", "compiler", "build_type", "arch"

requires = "dep/1.0"

def generate(self):
deps = CMakeDeps(self)
deps.set_property("dep", "cmake_target_aliases", ["alias", "dep::other_name"])
deps.set_property("dep::mycomp", "cmake_target_aliases", ["component_alias", "dep::my_aliased_component"])
deps.generate()
""")})
tc.run("create dep")
tc.run("install .")
targetsData = tc.load("depTargets.cmake")
assert "add_library(dep::dep" in targetsData
assert "add_library(alias" in targetsData
assert "add_library(dep::other_name" in targetsData

assert "add_library(component_alias" in targetsData
assert "add_library(dep::my_aliased_component" in targetsData