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

Add cpp_info.name to cmake and pkg_config generators #5598

Merged
merged 8 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 2 additions & 1 deletion conans/client/generators/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def content(self):
sections = ["include(CMakeParseArguments)"]

# Per requirement variables
for dep_name, dep_cpp_info in self.deps_build_info.dependencies:
for _, dep_cpp_info in self.deps_build_info.dependencies:
dep_name = dep_cpp_info.name
deps = DepsCppCmake(dep_cpp_info)
dep_flags = cmake_dependency_vars(dep_name, deps=deps)
sections.append(dep_flags)
Expand Down
9 changes: 5 additions & 4 deletions conans/client/generators/cmake_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,15 @@ def generate_targets_section(dependencies):
' set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CONAN_CMD_CXX_FLAGS}")\n'
' set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CONAN_CMD_C_FLAGS}")\n'
' set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CONAN_CMD_SHARED_LINKER_FLAGS}")\n')

for dep_name, dep_info in dependencies:
use_deps = ["CONAN_PKG::%s" % d for d in dep_info.public_deps]
dependencies_dict = {name: dep_info for name, dep_info in dependencies}
for _, dep_info in dependencies:
dep_name = dep_info.name
use_deps = ["CONAN_PKG::%s" % dependencies_dict[d].name for d in dep_info.public_deps]
deps = "" if not use_deps else " ".join(use_deps)
section.append(_target_template.format(name="CONAN_PKG::%s" % dep_name, deps=deps,
uname=dep_name.upper(), pkg_name=dep_name))

all_targets = " ".join(["CONAN_PKG::%s" % name for name, _ in dependencies])
all_targets = " ".join(["CONAN_PKG::%s" % dep_info.name for _, dep_info in dependencies])
section.append(' set(CONAN_TARGETS %s)\n' % all_targets)
section.append('endmacro()\n')
return section
Expand Down
2 changes: 1 addition & 1 deletion conans/client/generators/cmake_find_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def filename(self):
def content(self):
ret = {}
for depname, cpp_info in self.deps_build_info.dependencies:
ret["Find%s.cmake" % depname] = self._find_for_dep(depname, cpp_info)
ret["Find%s.cmake" % cpp_info.name] = self._find_for_dep(cpp_info.name, cpp_info)
return ret

def _find_for_dep(self, name, cpp_info):
Expand Down
3 changes: 2 additions & 1 deletion conans/client/generators/cmake_find_package_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def content(self):
ret = {}
build_type = self.conanfile.settings.get_safe("build_type")
build_type_suffix = "_{}".format(build_type.upper()) if build_type else ""
for depname, cpp_info in self.deps_build_info.dependencies:
for _, cpp_info in self.deps_build_info.dependencies:
depname = cpp_info.name
deps = DepsCppCmake(cpp_info)
ret["{}Config.cmake".format(depname)] = self._find_for_dep(depname, cpp_info)

Expand Down
3 changes: 2 additions & 1 deletion conans/client/generators/cmake_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def _content_type(self, build_type):
sections = []

# Per requirement variables
for dep_name, dep_cpp_info in self.deps_build_info.dependencies:
for _, dep_cpp_info in self.deps_build_info.dependencies:
dep_name = dep_cpp_info.name
# Only the specific of the build_type
dep_cpp_info = extend(dep_cpp_info, build_type)
deps = DepsCppCmake(dep_cpp_info)
Expand Down
4 changes: 2 additions & 2 deletions conans/client/generators/cmake_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def content(self):
# The CONAN_XXX_ROOT variables are needed because the FindXXX.cmake or XXXConfig.cmake
# in a package could have been "patched" with the `cmake.patch_config_paths()`
# replacing absolute paths with CONAN_XXX_ROOT variables.
for dep_name, dep_cpp_info in self.deps_build_info.dependencies:
var_name = "CONAN_{}_ROOT".format(dep_name.upper())
for _, dep_cpp_info in self.deps_build_info.dependencies:
var_name = "CONAN_{}_ROOT".format(dep_cpp_info.name.upper())
lines.append('set({} {})'.format(var_name, DepsCppCmake(dep_cpp_info).rootpath))

# We want to prioritize the FindXXX.cmake files:
Expand Down
7 changes: 4 additions & 3 deletions conans/client/generators/pkg_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ def compiler(self):
@property
def content(self):
ret = {}
for depname, cpp_info in self.deps_build_info.dependencies:
ret["%s.pc" % depname] = self.single_pc_file_contents(depname, cpp_info)
for _, cpp_info in self.deps_build_info.dependencies:
ret["%s.pc" % cpp_info.name] = self.single_pc_file_contents(cpp_info)
return ret

def single_pc_file_contents(self, name, cpp_info):
def single_pc_file_contents(self, cpp_info):
name = cpp_info.name
prefix_path = cpp_info.rootpath.replace("\\", "/")
lines = ['prefix=%s' % prefix_path]

Expand Down
12 changes: 12 additions & 0 deletions conans/test/unittests/client/generators/cmake_paths_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def cmake_vars_unit_test(self):
conanfile.initialize(settings, EnvValues())
tmp = temp_folder()
cpp_info = CppInfo(tmp)
cpp_info.name = "MyLib"
custom_dir = os.path.join(tmp, "custom_build_dir")
os.mkdir(custom_dir)
cpp_info.builddirs.append(os.path.join(tmp, "custom_build_dir"))
Expand All @@ -56,3 +57,14 @@ def cmake_vars_unit_test(self):
'${CMAKE_CURRENT_LIST_DIR})' % (path, custom_dir), cmake_lines[1])
self.assertEqual('set(CMAKE_PREFIX_PATH "%s/" "%s" ${CMAKE_PREFIX_PATH} '
'${CMAKE_CURRENT_LIST_DIR})' % (path, custom_dir), cmake_lines[2])

def cpp_info_name_test(self):
settings = _MockSettings("Release")
conanfile = ConanFile(TestBufferConanOutput(), None)
conanfile.initialize(settings, EnvValues())
tmp = temp_folder()
cpp_info = CppInfo(tmp)
cpp_info.name = "PkgCMakeName"
conanfile.deps_cpp_info.update(cpp_info, "pkg_reference_name")
generator = CMakePathsGenerator(conanfile)
self.assertIn('set(CONAN_PKGCMAKENAME_ROOT', generator.content)
58 changes: 58 additions & 0 deletions conans/test/unittests/client/generators/cmake_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from conans.client.build.cmake_flags import CMakeDefinitionsBuilder
from conans.client.conf import default_settings_yml
from conans.client.generators import CMakeFindPackageGenerator
from conans.client.generators.cmake import CMakeGenerator
from conans.client.generators.cmake_multi import CMakeMultiGenerator
from conans.errors import ConanException
Expand Down Expand Up @@ -51,10 +52,12 @@ def variables_setup_test(self):
conanfile.initialize(Settings({}), EnvValues())
ref = ConanFileReference.loads("MyPkg/0.1@lasote/stables")
cpp_info = CppInfo("dummy_root_folder1")
cpp_info.name = ref.name
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if you don't assign this?

Copy link
Member Author

Choose a reason for hiding this comment

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

When cpp_info.name is used inside the generator, the None type makes it crash

AttributeError: 'NoneType' object has no attribute 'upper'

cpp_info.defines = ["MYDEFINE1"]
conanfile.deps_cpp_info.update(cpp_info, ref.name)
ref = ConanFileReference.loads("MyPkg2/0.1@lasote/stables")
cpp_info = CppInfo("dummy_root_folder2")
cpp_info.name = ref.name
cpp_info.defines = ["MYDEFINE2"]
conanfile.deps_cpp_info.update(cpp_info, ref.name)
conanfile.deps_user_info["LIB1"].myvar = "myvalue"
Expand All @@ -81,6 +84,7 @@ def paths_cmake_multi_user_vars_test(self):
save(os.path.join(tmp_folder, "lib", "mylib.lib"), "")
save(os.path.join(tmp_folder, "include", "myheader.h"), "")
cpp_info = CppInfo(tmp_folder)
cpp_info.name = ref.name
cpp_info.release.libs = ["hello"]
cpp_info.debug.libs = ["hello_D"]
conanfile.deps_cpp_info.update(cpp_info, ref.name)
Expand All @@ -100,6 +104,7 @@ def paths_cmake_test(self):
save(os.path.join(tmp_folder, "lib", "mylib.lib"), "")
save(os.path.join(tmp_folder, "include", "myheader.h"), "")
cpp_info = CppInfo(tmp_folder)
cpp_info.name = ref.name
cpp_info.release.libs = ["hello"]
cpp_info.debug.libs = ["hello_D"]
conanfile.deps_cpp_info.update(cpp_info, ref.name)
Expand Down Expand Up @@ -143,11 +148,13 @@ def multi_flag_test(self):
conanfile.initialize(Settings({}), EnvValues())
ref = ConanFileReference.loads("MyPkg/0.1@lasote/stables")
cpp_info = CppInfo("dummy_root_folder1")
cpp_info.name = ref.name
cpp_info.includedirs.append("other_include_dir")
cpp_info.cxxflags = ["-DGTEST_USE_OWN_TR1_TUPLE=1", "-DGTEST_LINKED_AS_SHARED_LIBRARY=1"]
conanfile.deps_cpp_info.update(cpp_info, ref.name)
ref = ConanFileReference.loads("MyPkg2/0.1@lasote/stables")
cpp_info = CppInfo("dummy_root_folder2")
cpp_info.name = ref.name
cpp_info.cflags = ["-DSOMEFLAG=1"]
conanfile.deps_cpp_info.update(cpp_info, ref.name)
generator = CMakeGenerator(conanfile)
Expand All @@ -165,6 +172,7 @@ def escaped_flags_test(self):
conanfile.initialize(Settings({}), EnvValues())
ref = ConanFileReference.loads("MyPkg/0.1@lasote/stables")
cpp_info = CppInfo("dummy_root_folder1")
cpp_info.name = ref.name
cpp_info.includedirs.append("other_include_dir")
cpp_info.cxxflags = ["-load", r"C:\foo\bar.dll"]
cpp_info.cflags = ["-load", r"C:\foo\bar2.dll"]
Expand Down Expand Up @@ -307,3 +315,53 @@ def cmake_find_package_multi_definitions_test(self):
definitions = definitions_builder.get_definitions()
self.assertEqual(install_folder, definitions["CMAKE_PREFIX_PATH"])
self.assertEqual(install_folder, definitions["CMAKE_MODULE_PATH"])

def cpp_info_name_cmake_vars_test(self):
"""
Test cpp_info.names values are applied instead of the reference name
"""
conanfile = ConanFile(TestBufferConanOutput(), None)
conanfile.initialize(Settings({}), EnvValues())
ref = ConanFileReference.loads("my_pkg/0.1@lasote/stables")
cpp_info = CppInfo("dummy_root_folder1")
cpp_info.name = "MyPkG"
conanfile.deps_cpp_info.update(cpp_info, ref.name)
ref = ConanFileReference.loads("my_pkg2/0.1@lasote/stables")
cpp_info = CppInfo("dummy_root_folder2")
cpp_info.name = "MyPkG2"
conanfile.deps_cpp_info.update(cpp_info, ref.name)
generator = CMakeGenerator(conanfile)
content = generator.content
self.assertIn("set(CONAN_DEPENDENCIES my_pkg my_pkg2)", content)
content = content.replace("set(CONAN_DEPENDENCIES my_pkg my_pkg2)", "")
self.assertNotIn("my_pkg", content)
self.assertNotIn("MY_PKG", content)
self.assertIn('add_library(CONAN_PKG::MyPkG INTERFACE IMPORTED)', content)
self.assertIn('add_library(CONAN_PKG::MyPkG2 INTERFACE IMPORTED)', content)
self.assertNotIn('CONAN_PKG::my_pkg', content)
self.assertNotIn('CONAN_PKG::my_pkg2', content)

def cpp_info_name_cmake_find_package_test(self):
"""
Test cpp_info.names values are applied instead of the reference name
"""
conanfile = ConanFile(TestBufferConanOutput(), None)
conanfile.initialize(Settings({}), EnvValues())
ref = ConanFileReference.loads("my_pkg/0.1@lasote/stables")
cpp_info = CppInfo("dummy_root_folder1")
cpp_info.name = "MyPkG"
conanfile.deps_cpp_info.update(cpp_info, ref.name)
ref = ConanFileReference.loads("my_pkg2/0.1@lasote/stables")
cpp_info = CppInfo("dummy_root_folder2")
cpp_info.name = "MyPkG2"
conanfile.deps_cpp_info.update(cpp_info, ref.name)
generator = CMakeFindPackageGenerator(conanfile)
content = generator.content
self.assertIn("FindMyPkG.cmake", content.keys())
self.assertIn("FindMyPkG2.cmake", content.keys())
self.assertNotIn("my_pkg", content["FindMyPkG.cmake"])
self.assertNotIn("MY_PKG", content["FindMyPkG.cmake"])
self.assertNotIn("my_pkg", content["FindMyPkG2.cmake"])
self.assertNotIn("MY_PKG", content["FindMyPkG2.cmake"])
self.assertIn("add_library(MyPkG::MyPkG INTERFACE IMPORTED)", content["FindMyPkG.cmake"])
self.assertIn("add_library(MyPkG2::MyPkG2 INTERFACE IMPORTED)", content["FindMyPkG2.cmake"])
2 changes: 2 additions & 0 deletions conans/test/unittests/client/generators/pkg_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def variables_setup_test(self):
conanfile.initialize(Settings({}), EnvValues())
ref = ConanFileReference.loads("MyPkg/0.1@lasote/stables")
cpp_info = CppInfo("dummy_root_folder1")
cpp_info.name = ref.name
danimtb marked this conversation as resolved.
Show resolved Hide resolved
cpp_info.defines = ["MYDEFINE1"]
cpp_info.cflags.append("-Flag1=23")
cpp_info.version = "1.3"
Expand All @@ -24,6 +25,7 @@ def variables_setup_test(self):
conanfile.deps_cpp_info.update(cpp_info, ref.name)
ref = ConanFileReference.loads("MyPkg2/0.1@lasote/stables")
cpp_info = CppInfo("dummy_root_folder2")
cpp_info.name = ref.name
cpp_info.defines = ["MYDEFINE2"]
cpp_info.version = "2.3"
cpp_info.exelinkflags = ["-exelinkflag"]
Expand Down