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

preparing migration of names to CMakeDeps #8568

Merged
merged 3 commits into from Mar 4, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 36 additions & 6 deletions conan/tools/cmake/cmakedeps.py
Expand Up @@ -5,6 +5,7 @@

from conans.errors import ConanException
from conans.model.build_info import CppInfo, merge_dicts
from conans.util.conan_v2_mode import conan_v2_error
from conans.util.files import save

COMPONENT_SCOPE = "::"
Expand Down Expand Up @@ -560,12 +561,40 @@ def _check_component_in_requirements(require):
for pkg_require in cpp_info.requires:
_check_component_in_requirements(pkg_require)

def _get_name(self, cpp_info, pkg_name):
# FIXME: This is a workaround to be able to use existing recipes that declare
# FIXME: cpp_info.names["cmake_find_package_multi"] = "xxxxx"
name = cpp_info.names.get(self.name)
if name is not None:
return name
find_name = cpp_info.names.get("cmake_find_package_multi")
if find_name is not None:
# Not displaying a warning, too noisy as this is called many times
conan_v2_error("'{}' defines information for 'cmake_find_package_multi', "
"but not 'CMakeDeps'".format(pkg_name))
return find_name
return cpp_info._name

def _get_filename(self, cpp_info, pkg_name):
# FIXME: This is a workaround to be able to use existing recipes that declare
# FIXME: cpp_info.filenames["cmake_find_package_multi"] = "xxxxx"
name = cpp_info.filenames.get(self.name)
if name is not None:
return name
find_name = cpp_info.filenames.get("cmake_find_package_multi")
if find_name is not None:
# Not displaying a warning, too noisy as this is called many times
conan_v2_error("'{}' defines information for 'cmake_find_package_multi', "
"but not 'CMakeDeps'".format(pkg_name))
return find_name
return cpp_info._name

def _get_require_name(self, pkg_name, req):
pkg, cmp = req.split(COMPONENT_SCOPE) if COMPONENT_SCOPE in req else (pkg_name, req)
pkg_cpp_info = self._conanfile.deps_cpp_info[pkg]
pkg_name = pkg_cpp_info.get_name(self.name)
pkg_name = self._get_name(pkg_cpp_info, pkg_name)
if cmp in pkg_cpp_info.components:
cmp_name = pkg_cpp_info.components[cmp].get_name(self.name)
cmp_name = self._get_name(pkg_cpp_info.components[cmp], pkg_name)
else:
cmp_name = pkg_name
return pkg_name, cmp_name
Expand All @@ -575,7 +604,7 @@ def _get_components(self, pkg_name, cpp_info):
sorted_comps = cpp_info._get_sorted_components()

for comp_name, comp in sorted_comps.items():
comp_genname = cpp_info.components[comp_name].get_name(self.name)
comp_genname = self._get_name(cpp_info.components[comp_name], pkg_name)
comp_requires_gennames = []
for require in comp.requires:
comp_requires_gennames.append(self._get_require_name(pkg_name, require))
Expand Down Expand Up @@ -617,8 +646,8 @@ def content(self):

for pkg_name, cpp_info in self._conanfile.deps_cpp_info.dependencies:
self._validate_components(cpp_info)
pkg_filename = cpp_info.get_filename(self.name)
pkg_findname = cpp_info.get_name(self.name)
pkg_filename = self._get_filename(cpp_info, pkg_name)
pkg_findname = self._get_name(cpp_info, pkg_name)
pkg_version = cpp_info.version

public_deps = self.get_public_deps(cpp_info)
Expand All @@ -628,7 +657,8 @@ def content(self):
if name not in deps_names:
deps_names.append(name)
deps_names = ';'.join(deps_names)
pkg_public_deps_filenames = [self._conanfile.deps_cpp_info[it[0]].get_filename(self.name)
pkg_public_deps_filenames = [self._get_filename(self._conanfile.deps_cpp_info[it[0]],
pkg_name)
for it in public_deps]
config_version = self.config_version_template.format(version=pkg_version)
ret[self._config_version_filename(pkg_filename)] = config_version
Expand Down
57 changes: 57 additions & 0 deletions conans/test/unittests/tools/cmake/test_cmakedeps.py
@@ -0,0 +1,57 @@
import pytest
from mock import Mock

from conan.tools.cmake import CMakeDeps
from conans import ConanFile, Settings
from conans.client.tools import environment_append
from conans.errors import ConanException
from conans.model.build_info import CppInfo, DepCppInfo
from conans.model.env_info import EnvValues
from conans.util.conan_v2_mode import CONAN_V2_MODE_ENVVAR


def test_cpp_info_name_cmakedeps():
conanfile = ConanFile(Mock(), None)
conanfile.settings = "os", "compiler", "build_type", "arch"
conanfile.initialize(Settings({"os": ["Windows"],
"compiler": ["gcc"],
"build_type": ["Release"],
"arch": ["x86"]}), EnvValues())

cpp_info = CppInfo("mypkg", "dummy_root_folder1")
cpp_info.names["cmake_find_package_multi"] = "MySuperPkg1"
cpp_info.filenames["cmake_find_package_multi"] = "ComplexFileName1"
conanfile.deps_cpp_info.add("mypkg", cpp_info)

cmakedeps = CMakeDeps(conanfile)
files = cmakedeps.content
assert "TARGET MySuperPkg1::MySuperPkg1" in files["ComplexFileName1Config.cmake"]

with pytest.raises(ConanException,
match="'mypkg' defines information for 'cmake_find_package_multi'"):
with environment_append({CONAN_V2_MODE_ENVVAR: "1"}):
_ = cmakedeps.content


def test_cpp_info_name_cmakedeps_components():
conanfile = ConanFile(Mock(), None)
conanfile.settings = "os", "compiler", "build_type", "arch"
conanfile.initialize(Settings({"os": ["Windows"],
"compiler": ["gcc"],
"build_type": ["Release"],
"arch": ["x86"]}), EnvValues())

cpp_info = CppInfo("mypkg", "dummy_root_folder1")
cpp_info.names["cmake_find_package_multi"] = "GlobakPkgName1"
cpp_info.components["mycomp"].names["cmake_find_package_multi"] = "MySuperPkg1"
cpp_info.filenames["cmake_find_package_multi"] = "ComplexFileName1"
conanfile.deps_cpp_info.add("mypkg", DepCppInfo(cpp_info))

cmakedeps = CMakeDeps(conanfile)
files = cmakedeps.content
assert "TARGET GlobakPkgName1::MySuperPkg1" in files["ComplexFileName1Config.cmake"]

with pytest.raises(ConanException,
match="'mypkg' defines information for 'cmake_find_package_multi'"):
with environment_append({CONAN_V2_MODE_ENVVAR: "1"}):
_ = cmakedeps.content