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

Use correct attribute type instead of package_type #17943

Open
wants to merge 6 commits into
base: develop2
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions conan/tools/cmake/cmakedeps2/target_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def _requires(self, info, components):
required_comp)
dep_target = dep_target or f"{pkg_name}::{required_comp}"
link = not (pkg_type is PackageType.SHARED and
dep_comp.package_type is PackageType.SHARED)
dep_comp.type is PackageType.SHARED)
result[dep_target] = link
else: # Different package
try:
Expand All @@ -81,13 +81,16 @@ def _requires(self, info, components):
assert required_pkg == required_comp
comp = None
default_target = f"{dep.ref.name}::{dep.ref.name}" # replace_requires
link = pkg_type is not PackageType.SHARED
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the test that I added, this movement is not necessary.
If this is necessary for a different use case, it would be great to have yet another test that covers this scenario.

Copy link
Author

@NokiDev NokiDev Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right this is triggered by another use case, see new test added. That triggers the first condition dep_comp == None, and then crash when evaluating the link.

For that case evaluating the link only on the package_type seems the simpler way to fix this one

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, now with the test and the explanation is more clear, many thanks!
I have just fixed an indent error in the test, and CI is running.

else:
comp = required_comp
default_target = f"{required_pkg}::{required_comp}"
link = not (pkg_type is PackageType.SHARED and
dep_comp.type is PackageType.SHARED)

dep_target = self._cmakedeps.get_property("cmake_target_name", dep, comp)
dep_target = dep_target or default_target
link = not (pkg_type is PackageType.SHARED and
dep_comp.package_type is PackageType.SHARED)

result[dep_target] = link
return result

Expand Down
85 changes: 85 additions & 0 deletions test/integration/toolchains/cmake/cmakedeps2/test_cmakedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,88 @@ def package_info(self):
c.run("create . --name=pkg --version=0.1 -c tools.cmake.cmakedeps:new=will_break_next")
assert "CMakeConfigDeps: cmake_set_interface_link_directories deprecated and invalid. " \
"The package 'package_info()' must correctly define the (CPS) information" in c.out

def test_consuming_cpp_info_with_components_dependency_from_same_package():
c = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
def package_info(self):
self.cpp_info.components["lib"].type = 'shared-library'
self.cpp_info.components["lib_extended"].type = 'shared-library'
self.cpp_info.components["lib_extended"].requires = ['lib']
""")
c.save({"conanfile.py": conanfile,
"test_package/conanfile.py": GenConanfile().with_settings("build_type")
.with_test("pass")
.with_generator("CMakeDeps")})
c.run("create . --name=pkg --version=0.1 -c tools.cmake.cmakedeps:new=will_break_next")
# it doesn't break
assert "find_package(pkg)" in c.out


def test_consuming_cpp_info_with_components_dependency_from_other_package():
c = TestClient()
dep = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
name = "dep"
version = "0.1"
def package_info(self):
self.cpp_info.components["lib"].type = 'shared-library'
""")
conanfile = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
requires = "dep/0.1"
def package_info(self):
self.cpp_info.components["lib"].type = 'shared-library'
self.cpp_info.components["lib"].requires = ['dep::lib']
""")
c.save({"dep/conanfile.py": dep,
"pkg/conanfile.py": conanfile,
"pkg/test_package/conanfile.py": GenConanfile().with_settings("build_type")
.with_test("pass")
.with_generator("CMakeDeps")})
c.run("create dep")
c.run("create pkg --name=pkg --version=0.1 -c tools.cmake.cmakedeps:new=will_break_next")
# it doesn't break
assert "find_package(pkg)" in c.out


def test_consuming_cpp_info_transitively_by_requiring_root_component_in_another_component_from_another_package():
c = TestClient()
dependent_conanfile = textwrap.dedent("""
from conan import ConanFile
class Dependent(ConanFile):
settings = "os", "compiler", "arch", "build_type"
name = 'dependent'
""")

conanfile = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
settings = "os", "compiler", "arch", "build_type"
def requirements(self):
self.requires('dependent/0.1')
def package_info(self):
self.cpp_info.type = 'shared-library'
self.cpp_info.requires = ['dependent::dependent']
""")
test_package = textwrap.dedent("""
from conan import ConanFile
class TestPkg(ConanFile):
settings = "os", "compiler", "arch", "build_type"
generators = "VirtualRunEnv", "CMakeDeps"

def requirements(self):
self.requires(self.tested_reference_str)

def test(self):
pass
""")
c.save({"dependent/conanfile.py": dependent_conanfile,
"main/conanfile.py": conanfile,
"main/test_package/conanfile.py":test_package})
c.run("create ./dependent/ --name=dependent --version=0.1 -c tools.cmake.cmakedeps:new=will_break_next")
c.run("create ./main/ --name=pkg --version=0.1 -c tools.cmake.cmakedeps:new=will_break_next")