From 546058115425e1f2c4b8bcd2e119e71a79e13692 Mon Sep 17 00:00:00 2001 From: danimtb Date: Mon, 30 Nov 2020 17:55:42 +0100 Subject: [PATCH 01/15] [POC] Use build modules to generate non-namespaced targets --- .../client/generators/cmake_find_package.py | 4 ++ .../generators/cmake_find_package_common.py | 4 -- .../generators/cmake_find_package_test.py | 66 +++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/conans/client/generators/cmake_find_package.py b/conans/client/generators/cmake_find_package.py index 9480a4007bd..96ed67c23f1 100644 --- a/conans/client/generators/cmake_find_package.py +++ b/conans/client/generators/cmake_find_package.py @@ -46,6 +46,10 @@ class CMakeFindPackageGenerator(GeneratorComponentsMixin, Generator): {find_dependencies_block} endif() endif() + + foreach(_BUILD_MODULE_PATH ${{{name}_BUILD_MODULES_PATHS}}) + include(${{_BUILD_MODULE_PATH}}) + endforeach() """) find_components_tpl = Template(textwrap.dedent("""\ diff --git a/conans/client/generators/cmake_find_package_common.py b/conans/client/generators/cmake_find_package_common.py index f3a61ba13dc..ccfeb39618f 100644 --- a/conans/client/generators/cmake_find_package_common.py +++ b/conans/client/generators/cmake_find_package_common.py @@ -72,10 +72,6 @@ set(CMAKE_MODULE_PATH {deps.build_paths} ${{CMAKE_MODULE_PATH}}) set(CMAKE_PREFIX_PATH {deps.build_paths} ${{CMAKE_PREFIX_PATH}}) - -foreach(_BUILD_MODULE_PATH ${{{name}_BUILD_MODULES_PATHS{build_type_suffix}}}) - include(${{_BUILD_MODULE_PATH}}) -endforeach() """ diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py index ffe16ed8c05..757a98d2d6f 100644 --- a/conans/test/functional/generators/cmake_find_package_test.py +++ b/conans/test/functional/generators/cmake_find_package_test.py @@ -430,6 +430,72 @@ def build(self): client.run("create .") self.assertIn("Printing using a external module!", client.out) + def test_build_modules_alias_target(self): + client = TestClient() + client.run("new hello/1.0 -s") + print(os.listdir(client.current_folder)) + conanfile = textwrap.dedent(""" + import os + from conans import ConanFile, CMake + + class Conan(ConanFile): + name = "hello" + version = "1.0" + settings = "os", "arch", "compiler", "build_type" + exports_sources = ["target-alias.cmake", "src/*"] + generators = "cmake" + + def build(self): + cmake = CMake(self) + cmake.configure(source_folder="src") + cmake.build() + + def package(self): + self.copy("*.h", dst="include") + self.copy("*.a", dst="lib") + self.copy("*.lib", dst="lib") + self.copy("target-alias.cmake", dst="share/cmake") + + def package_info(self): + builddir = os.path.join("share", "cmake") + module = os.path.join(builddir, "target-alias.cmake") + self.cpp_info.build_modules.append(module) + self.cpp_info.builddirs = [builddir] + """) + target_alias = textwrap.dedent(""" + add_library(otherhello INTERFACE IMPORTED) + target_link_libraries(otherhello INTERFACE hello::hello) + """) + client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) + client.run("create .") + + consumer = textwrap.dedent(""" + from conans import ConanFile, CMake + + class Conan(ConanFile): + name = "consumer" + version = "1.0" + settings = "os", "compiler", "build_type", "arch" + exports_sources = ["CMakeLists.txt"] + generators = "cmake_find_package" + requires = "hello/1.0" + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + """) + cmakelists = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.0) + project(test) + find_package(hello) + get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) + message("otherhello link libraries: ${tmp}") + """) + client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) + client.run("create .") + self.assertIn("otherhello link libraries: hello::hello", client.out) + def test_cpp_info_name(self): client = TestClient() client.run("new hello/1.0 -s") From d08995586f1e16e716bfce72ffd9f2c38034f317 Mon Sep 17 00:00:00 2001 From: danimtb Date: Tue, 1 Dec 2020 12:27:43 +0100 Subject: [PATCH 02/15] move build modules in components cmake_find_package impl --- .../client/generators/cmake_find_package.py | 23 +++++++++++++++---- .../generators/cmake_find_package_test.py | 1 - 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/conans/client/generators/cmake_find_package.py b/conans/client/generators/cmake_find_package.py index 96ed67c23f1..35104c8e1fe 100644 --- a/conans/client/generators/cmake_find_package.py +++ b/conans/client/generators/cmake_find_package.py @@ -160,10 +160,6 @@ class CMakeFindPackageGenerator(GeneratorComponentsMixin, Generator): set(CMAKE_MODULE_PATH {{ comp.build_paths }} ${CMAKE_MODULE_PATH}) set(CMAKE_PREFIX_PATH {{ comp.build_paths }} ${CMAKE_PREFIX_PATH}) - foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_'+comp_name+'_BUILD_MODULES_PATHS}' }}) - include(${_BUILD_MODULE_PATH}) - endforeach() - {%- endfor %} @@ -203,6 +199,25 @@ class CMakeFindPackageGenerator(GeneratorComponentsMixin, Generator): endif() endif() + ########## BUILD MODULES #################################################################### + ############################################################################################# + + {%- for comp_name, comp in components %} + + ########## COMPONENT {{ comp_name }} BUILD MODULES ########################################## + + foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_'+comp_name+'_BUILD_MODULES_PATHS}' }}) + include(${_BUILD_MODULE_PATH}) + endforeach() + + {%- endfor %} + + ########## GLOBAL BUILD MODULES ############################################################# + + foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_BUILD_MODULES_PATHS}' }}) + include(${{_BUILD_MODULE_PATH}}) + endforeach() + """)) @property diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py index 757a98d2d6f..93a3430b3d4 100644 --- a/conans/test/functional/generators/cmake_find_package_test.py +++ b/conans/test/functional/generators/cmake_find_package_test.py @@ -433,7 +433,6 @@ def build(self): def test_build_modules_alias_target(self): client = TestClient() client.run("new hello/1.0 -s") - print(os.listdir(client.current_folder)) conanfile = textwrap.dedent(""" import os from conans import ConanFile, CMake From 5e49225c3257f7260dd5056a7cce3206574982fc Mon Sep 17 00:00:00 2001 From: danimtb Date: Tue, 1 Dec 2020 16:01:23 +0100 Subject: [PATCH 03/15] move all build modules after targets are created --- .../client/generators/cmake_find_package.py | 2 +- .../generators/cmake_find_package_multi.py | 32 +++++++ .../cmake_find_package_multi_test.py | 91 +++++++++++++++++++ .../generators/cmake_find_package_test.py | 43 +++++++-- .../test/functional/generators/cmake_test.py | 67 +++++++++++++- 5 files changed, 225 insertions(+), 10 deletions(-) diff --git a/conans/client/generators/cmake_find_package.py b/conans/client/generators/cmake_find_package.py index 410cfd41f8f..be13859e82a 100644 --- a/conans/client/generators/cmake_find_package.py +++ b/conans/client/generators/cmake_find_package.py @@ -215,7 +215,7 @@ class CMakeFindPackageGenerator(GeneratorComponentsMixin, Generator): ########## GLOBAL BUILD MODULES ############################################################# foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_BUILD_MODULES_PATHS}' }}) - include(${{_BUILD_MODULE_PATH}}) + include(${_BUILD_MODULE_PATH}) endforeach() """)) diff --git a/conans/client/generators/cmake_find_package_multi.py b/conans/client/generators/cmake_find_package_multi.py index b58a9870110..62c23ccf5ce 100644 --- a/conans/client/generators/cmake_find_package_multi.py +++ b/conans/client/generators/cmake_find_package_multi.py @@ -13,6 +13,7 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator): name = "cmake_find_package_multi" _configurations = ["Release", "RelWithDebInfo", "MinSizeRel", "Debug"] + _configurations_upper = ["RELEASE", "RELWITHDEBINFO", "MINSIZEREL", "DEBUG"] config_template = textwrap.dedent(""" {macros_and_functions} @@ -25,6 +26,7 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator): include(${{CMAKE_CURRENT_LIST_DIR}}/{filename}Targets.cmake) {target_props_block} + {build_modules_block} {find_dependencies_block} """) @@ -68,6 +70,15 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator): {%- endfor %}) """) + build_modules = Template(""" +# Build modules +{%- for config in configs %} +foreach(_BUILD_MODULE_PATH {{ '${'+name+'_BUILD_MODULES_PATHS_'+config+'}' }}) + include(${_BUILD_MODULE_PATH}) +endforeach() +{%- endfor %} + """) + # https://gitlab.kitware.com/cmake/cmake/blob/master/Modules/BasicConfigVersion-SameMajorVersion.cmake.in config_version_template = textwrap.dedent(""" set(PACKAGE_VERSION "{version}") @@ -255,6 +266,24 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator): $<$:{{ '${'+pkg_name+'_COMPONENTS_'+config.upper()+'}'}}> {%- endfor %}) endif() + + ########## BUILD MODULES #################################################################### + ############################################################################################# + + {%- for comp_name, comp in components %} + ########## COMPONENT {{ comp_name }} BUILD MODULES ########################################## + + foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_'+comp_name+'_BUILD_MODULES_PATHS}' }}) + include(${_BUILD_MODULE_PATH}) + endforeach() + + {%- endfor %} + + ########## GLOBAL BUILD MODULES ############################################################# + + foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_BUILD_MODULES_PATHS}' }}) + include(${{_BUILD_MODULE_PATH}}) + endforeach() """)) @property @@ -362,6 +391,8 @@ def _config(self, filename, name, version, public_deps_names): # Define the targets properties targets_props = self.target_properties.render(name=name, configs=self._configurations) + # Add build modules + build_modules_block = self.build_modules.render(name=name, configs=self._configurations_upper) # The find_dependencies_block find_dependencies_block = "" if public_deps_names: @@ -372,6 +403,7 @@ def _config(self, filename, name, version, public_deps_names): tmp = self.config_template.format(name=name, version=version, filename=filename, target_props_block=targets_props, + build_modules_block=build_modules_block, find_dependencies_block=find_dependencies_block, macros_and_functions=macros_and_functions) return tmp diff --git a/conans/test/functional/generators/cmake_find_package_multi_test.py b/conans/test/functional/generators/cmake_find_package_multi_test.py index f128dbcc83f..c38cbd20119 100644 --- a/conans/test/functional/generators/cmake_find_package_multi_test.py +++ b/conans/test/functional/generators/cmake_find_package_multi_test.py @@ -147,6 +147,97 @@ def build(self): client.run("create .") self.assertIn("Printing using a external module!", client.out) + @parameterized.expand([(False,), (True,)]) + def test_build_modules_alias_target(self, use_components): + print("USE COMPONENTS", use_components) + client = TestClient() + client.run("new hello/1.0 -s") + conanfile = textwrap.dedent(""" + import os + from conans import ConanFile, CMake + + class Conan(ConanFile): + name = "hello" + version = "1.0" + settings = "os", "arch", "compiler", "build_type" + exports_sources = ["target-alias.cmake", "src/*"] + generators = "cmake" + + def build(self): + cmake = CMake(self) + cmake.configure(source_folder="src") + cmake.build() + + def package(self): + self.copy("*.h", dst="include") + self.copy("*.a", dst="lib") + self.copy("*.lib", dst="lib") + self.copy("target-alias.cmake", dst="share/cmake") + + def package_info(self): + builddir = os.path.join("share", "cmake") + module = os.path.join(builddir, "target-alias.cmake") + %s + """) + if use_components: + info = textwrap.dedent("""\ + self.cpp_info.name = "namespace" + self.cpp_info.filenames["cmake_find_package_multi"] = "hello" + self.cpp_info.components["comp"].build_modules.append(module) + self.cpp_info.components["comp"].builddirs = [builddir] + """) + target_alias = textwrap.dedent(""" + if(NOT TARGET otherhello) + add_library(otherhello INTERFACE IMPORTED) + target_link_libraries(otherhello INTERFACE namespace::hello) + endif() + """) + else: + info = textwrap.dedent("""\ + self.cpp_info.build_modules.append(module) + self.cpp_info.builddirs = [builddir] + """) + target_alias = textwrap.dedent(""" + if(NOT TARGET otherhello) + add_library(otherhello INTERFACE IMPORTED) + target_link_libraries(otherhello INTERFACE hello::hello) + endif() + """) + conanfile = conanfile % textwrap.indent(info, " ") + print(conanfile) + client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) + client.run("create .") + + consumer = textwrap.dedent(""" + from conans import ConanFile, CMake + + class Conan(ConanFile): + name = "consumer" + version = "1.0" + settings = "os", "compiler", "build_type", "arch" + exports_sources = ["CMakeLists.txt"] + generators = "cmake_find_package_multi" + requires = "hello/1.0" + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + """) + cmakelists = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.0) + project(test) + find_package(hello) + get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) + message("otherhello link libraries: ${tmp}") + """) + client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) + client.run("create .") + if use_components: + self.assertIn("otherhello link libraries: namespace::hello", client.out) + else: + self.assertIn("otherhello link libraries: hello::hello", client.out) + def test_cmake_find_package_system_libs(self): conanfile = textwrap.dedent(""" from conans import ConanFile, tools diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py index 93a3430b3d4..3a1f0c4830a 100644 --- a/conans/test/functional/generators/cmake_find_package_test.py +++ b/conans/test/functional/generators/cmake_find_package_test.py @@ -6,6 +6,7 @@ import pytest import six from nose.plugins.attrib import attr +from parameterized import parameterized from conans.client.tools import replace_in_file from conans.model.ref import ConanFileReference, PackageReference @@ -430,7 +431,9 @@ def build(self): client.run("create .") self.assertIn("Printing using a external module!", client.out) - def test_build_modules_alias_target(self): + @parameterized.expand([(False,), (True,)]) + def test_build_modules_alias_target(self, use_components): + print("USE COMPONENTS", use_components) client = TestClient() client.run("new hello/1.0 -s") conanfile = textwrap.dedent(""" @@ -458,13 +461,34 @@ def package(self): def package_info(self): builddir = os.path.join("share", "cmake") module = os.path.join(builddir, "target-alias.cmake") - self.cpp_info.build_modules.append(module) - self.cpp_info.builddirs = [builddir] - """) - target_alias = textwrap.dedent(""" - add_library(otherhello INTERFACE IMPORTED) - target_link_libraries(otherhello INTERFACE hello::hello) + %s """) + if use_components: + info = textwrap.dedent("""\ + self.cpp_info.name = "namespace" + self.cpp_info.filenames["cmake_find_package"] = "hello" + self.cpp_info.components["comp"].build_modules.append(module) + self.cpp_info.components["comp"].builddirs = [builddir] + """) + target_alias = textwrap.dedent(""" + if(NOT TARGET otherhello) + add_library(otherhello INTERFACE IMPORTED) + target_link_libraries(otherhello INTERFACE namespace::hello) + endif() + """) + else: + info = textwrap.dedent("""\ + self.cpp_info.build_modules.append(module) + self.cpp_info.builddirs = [builddir] + """) + target_alias = textwrap.dedent(""" + if(NOT TARGET otherhello) + add_library(otherhello INTERFACE IMPORTED) + target_link_libraries(otherhello INTERFACE hello::hello) + endif() + """) + conanfile = conanfile % textwrap.indent(info, " ") + print(conanfile) client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) client.run("create .") @@ -493,7 +517,10 @@ def build(self): """) client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) client.run("create .") - self.assertIn("otherhello link libraries: hello::hello", client.out) + if use_components: + self.assertIn("otherhello link libraries: namespace::hello", client.out) + else: + self.assertIn("otherhello link libraries: hello::hello", client.out) def test_cpp_info_name(self): client = TestClient() diff --git a/conans/test/functional/generators/cmake_test.py b/conans/test/functional/generators/cmake_test.py index bde04dcd351..3f475a209f2 100644 --- a/conans/test/functional/generators/cmake_test.py +++ b/conans/test/functional/generators/cmake_test.py @@ -484,7 +484,6 @@ def build(self): message("comp compile options: ${tmp}") """) run_test("cmake_find_package", cmakelists) - print(client.out) # Test cmake_find_package generator without components run_test("cmake_find_package", cmakelists, with_components=False) @@ -507,3 +506,69 @@ def build(self): # Test cmake_find_package_multi generator without components run_test("cmake_find_package_multi", cmakelists, with_components=False) + + def test_build_modules_alias_target(self): + client = TestClient() + client.run("new hello/1.0 -s") + conanfile = textwrap.dedent(""" + import os + from conans import ConanFile, CMake + + class Conan(ConanFile): + name = "hello" + version = "1.0" + settings = "os", "arch", "compiler", "build_type" + exports_sources = ["target-alias.cmake", "src/*"] + generators = "cmake" + + def build(self): + cmake = CMake(self) + cmake.configure(source_folder="src") + cmake.build() + + def package(self): + self.copy("*.h", dst="include") + self.copy("*.a", dst="lib") + self.copy("*.lib", dst="lib") + self.copy("target-alias.cmake", dst="share/cmake") + + def package_info(self): + builddir = os.path.join("share", "cmake") + module = os.path.join(builddir, "target-alias.cmake") + self.cpp_info.build_modules.append(module) + self.cpp_info.builddirs = [builddir] + """) + target_alias = textwrap.dedent(""" + add_library(otherhello INTERFACE IMPORTED) + target_link_libraries(otherhello INTERFACE hello::hello) + """) + client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) + client.run("create .") + + consumer = textwrap.dedent(""" + from conans import ConanFile, CMake + + class Conan(ConanFile): + name = "consumer" + version = "1.0" + settings = "os", "compiler", "build_type", "arch" + exports_sources = ["CMakeLists.txt"] + generators = "cmake" + requires = "hello/1.0" + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + """) + cmakelists = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.0) + project(test) + include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) + conan_basic_setup() + get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) + message("otherhello link libraries: ${tmp}") + """) + client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) + client.run("create .") + self.assertIn("otherhello link libraries: hello::hello", client.out) From 03de1cfcd904d46efdb91a09c26c463669d11fbd Mon Sep 17 00:00:00 2001 From: danimtb Date: Tue, 1 Dec 2020 16:52:21 +0100 Subject: [PATCH 04/15] fixes --- .../generators/cmake_find_package_multi.py | 20 +++++++++++++++---- .../cmake_find_package_multi_test.py | 2 -- .../generators/cmake_find_package_test.py | 2 -- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/conans/client/generators/cmake_find_package_multi.py b/conans/client/generators/cmake_find_package_multi.py index 62c23ccf5ce..966f62777a6 100644 --- a/conans/client/generators/cmake_find_package_multi.py +++ b/conans/client/generators/cmake_find_package_multi.py @@ -232,7 +232,9 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator): {%- macro tvalue(pkg_name, comp_name, var, config) -%} {{'${'+pkg_name+'_'+comp_name+'_'+var+'_'+config.upper()+'}'}} {%- endmacro -%} + {%- for comp_name, comp in components %} + ########## COMPONENT {{ comp_name }} TARGET PROPERTIES ###################################### set_property(TARGET {{ pkg_name }}::{{ comp_name }} PROPERTY INTERFACE_LINK_LIBRARIES @@ -271,19 +273,28 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator): ############################################################################################# {%- for comp_name, comp in components %} + ########## COMPONENT {{ comp_name }} BUILD MODULES ########################################## - foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_'+comp_name+'_BUILD_MODULES_PATHS}' }}) + {%- for config in configs_upper %} + + foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_'+comp_name+'_BUILD_MODULES_PATHS_'+config+'}' }}) include(${_BUILD_MODULE_PATH}) endforeach() {%- endfor %} + {%- endfor %} + ########## GLOBAL BUILD MODULES ############################################################# - foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_BUILD_MODULES_PATHS}' }}) - include(${{_BUILD_MODULE_PATH}}) + {%- for config in configs_upper %} + + foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_BUILD_MODULES_PATHS_'+config+'}' }}) + include(${_BUILD_MODULE_PATH}) endforeach() + + {%- endfor %} """)) @property @@ -362,7 +373,8 @@ def content(self): components=components, pkg_public_deps=pkg_public_deps_filenames, conan_message=CMakeFindPackageCommonMacros.conan_message, - configs=self._configurations + configs=self._configurations, + configs_upper=self._configurations_upper ) ret[self._config_filename(pkg_filename)] = target_config return ret diff --git a/conans/test/functional/generators/cmake_find_package_multi_test.py b/conans/test/functional/generators/cmake_find_package_multi_test.py index c38cbd20119..8d3f736f68c 100644 --- a/conans/test/functional/generators/cmake_find_package_multi_test.py +++ b/conans/test/functional/generators/cmake_find_package_multi_test.py @@ -149,7 +149,6 @@ def build(self): @parameterized.expand([(False,), (True,)]) def test_build_modules_alias_target(self, use_components): - print("USE COMPONENTS", use_components) client = TestClient() client.run("new hello/1.0 -s") conanfile = textwrap.dedent(""" @@ -204,7 +203,6 @@ def package_info(self): endif() """) conanfile = conanfile % textwrap.indent(info, " ") - print(conanfile) client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) client.run("create .") diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py index 3a1f0c4830a..67f22f635ca 100644 --- a/conans/test/functional/generators/cmake_find_package_test.py +++ b/conans/test/functional/generators/cmake_find_package_test.py @@ -433,7 +433,6 @@ def build(self): @parameterized.expand([(False,), (True,)]) def test_build_modules_alias_target(self, use_components): - print("USE COMPONENTS", use_components) client = TestClient() client.run("new hello/1.0 -s") conanfile = textwrap.dedent(""" @@ -488,7 +487,6 @@ def package_info(self): endif() """) conanfile = conanfile % textwrap.indent(info, " ") - print(conanfile) client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) client.run("create .") From 30bfc71eed813630e777241c58d589f93cac99f4 Mon Sep 17 00:00:00 2001 From: danimtb Date: Wed, 2 Dec 2020 12:46:03 +0100 Subject: [PATCH 05/15] remove components build modules in cmake --- .../client/generators/cmake_find_package.py | 10 ------- .../generators/cmake_find_package_multi.py | 26 ++++--------------- 2 files changed, 5 insertions(+), 31 deletions(-) diff --git a/conans/client/generators/cmake_find_package.py b/conans/client/generators/cmake_find_package.py index be13859e82a..541a2d31681 100644 --- a/conans/client/generators/cmake_find_package.py +++ b/conans/client/generators/cmake_find_package.py @@ -202,16 +202,6 @@ class CMakeFindPackageGenerator(GeneratorComponentsMixin, Generator): ########## BUILD MODULES #################################################################### ############################################################################################# - {%- for comp_name, comp in components %} - - ########## COMPONENT {{ comp_name }} BUILD MODULES ########################################## - - foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_'+comp_name+'_BUILD_MODULES_PATHS}' }}) - include(${_BUILD_MODULE_PATH}) - endforeach() - - {%- endfor %} - ########## GLOBAL BUILD MODULES ############################################################# foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_BUILD_MODULES_PATHS}' }}) diff --git a/conans/client/generators/cmake_find_package_multi.py b/conans/client/generators/cmake_find_package_multi.py index 966f62777a6..5d9d93eab03 100644 --- a/conans/client/generators/cmake_find_package_multi.py +++ b/conans/client/generators/cmake_find_package_multi.py @@ -13,7 +13,6 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator): name = "cmake_find_package_multi" _configurations = ["Release", "RelWithDebInfo", "MinSizeRel", "Debug"] - _configurations_upper = ["RELEASE", "RELWITHDEBINFO", "MINSIZEREL", "DEBUG"] config_template = textwrap.dedent(""" {macros_and_functions} @@ -73,7 +72,7 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator): build_modules = Template(""" # Build modules {%- for config in configs %} -foreach(_BUILD_MODULE_PATH {{ '${'+name+'_BUILD_MODULES_PATHS_'+config+'}' }}) +foreach(_BUILD_MODULE_PATH {{ '${'+name+'_BUILD_MODULES_PATHS_'+config.upper()+'}' }}) include(${_BUILD_MODULE_PATH}) endforeach() {%- endfor %} @@ -272,25 +271,11 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator): ########## BUILD MODULES #################################################################### ############################################################################################# - {%- for comp_name, comp in components %} - - ########## COMPONENT {{ comp_name }} BUILD MODULES ########################################## - - {%- for config in configs_upper %} - - foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_'+comp_name+'_BUILD_MODULES_PATHS_'+config+'}' }}) - include(${_BUILD_MODULE_PATH}) - endforeach() - - {%- endfor %} - - {%- endfor %} - ########## GLOBAL BUILD MODULES ############################################################# - {%- for config in configs_upper %} + {%- for config in configs %} - foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_BUILD_MODULES_PATHS_'+config+'}' }}) + foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_BUILD_MODULES_PATHS_'+config.upper()+'}' }}) include(${_BUILD_MODULE_PATH}) endforeach() @@ -373,8 +358,7 @@ def content(self): components=components, pkg_public_deps=pkg_public_deps_filenames, conan_message=CMakeFindPackageCommonMacros.conan_message, - configs=self._configurations, - configs_upper=self._configurations_upper + configs=self._configurations ) ret[self._config_filename(pkg_filename)] = target_config return ret @@ -404,7 +388,7 @@ def _config(self, filename, name, version, public_deps_names): # Define the targets properties targets_props = self.target_properties.render(name=name, configs=self._configurations) # Add build modules - build_modules_block = self.build_modules.render(name=name, configs=self._configurations_upper) + build_modules_block = self.build_modules.render(name=name, configs=self._configurations) # The find_dependencies_block find_dependencies_block = "" if public_deps_names: From a2a733488c1c16ddc27ec61a526a9748b6e66e91 Mon Sep 17 00:00:00 2001 From: danimtb Date: Wed, 2 Dec 2020 13:23:22 +0100 Subject: [PATCH 06/15] fix target name --- conans/test/functional/generators/cmake_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conans/test/functional/generators/cmake_test.py b/conans/test/functional/generators/cmake_test.py index 3f475a209f2..4c8983a5b41 100644 --- a/conans/test/functional/generators/cmake_test.py +++ b/conans/test/functional/generators/cmake_test.py @@ -540,7 +540,7 @@ def package_info(self): """) target_alias = textwrap.dedent(""" add_library(otherhello INTERFACE IMPORTED) - target_link_libraries(otherhello INTERFACE hello::hello) + target_link_libraries(otherhello INTERFACE CONAN_PKG::hello) """) client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) client.run("create .") @@ -571,4 +571,4 @@ def build(self): """) client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) client.run("create .") - self.assertIn("otherhello link libraries: hello::hello", client.out) + self.assertIn("otherhello link libraries: CONAN_PKG::hello", client.out) From 1559525fd69afc0819c01d20d59dbc83a11ba9a2 Mon Sep 17 00:00:00 2001 From: danimtb Date: Wed, 2 Dec 2020 13:52:05 +0100 Subject: [PATCH 07/15] debugging print --- conans/test/functional/generators/cmake_test.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/conans/test/functional/generators/cmake_test.py b/conans/test/functional/generators/cmake_test.py index 4c8983a5b41..9c91a36da6b 100644 --- a/conans/test/functional/generators/cmake_test.py +++ b/conans/test/functional/generators/cmake_test.py @@ -546,7 +546,8 @@ def package_info(self): client.run("create .") consumer = textwrap.dedent(""" - from conans import ConanFile, CMake + import os + from conans import ConanFile, CMake, tools class Conan(ConanFile): name = "consumer" @@ -557,6 +558,7 @@ class Conan(ConanFile): requires = "hello/1.0" def build(self): + print(tools.load(os.path.join(self.install_folder, "conanbuildinfo.cmake"))) cmake = CMake(self) cmake.configure() cmake.build() @@ -571,4 +573,5 @@ def build(self): """) client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) client.run("create .") + print(client.out) self.assertIn("otherhello link libraries: CONAN_PKG::hello", client.out) From f822829b523d01f54effa58a33dad15e32d15a2c Mon Sep 17 00:00:00 2001 From: danimtb Date: Wed, 2 Dec 2020 14:28:23 +0100 Subject: [PATCH 08/15] print paths --- conans/test/functional/generators/cmake_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conans/test/functional/generators/cmake_test.py b/conans/test/functional/generators/cmake_test.py index 9c91a36da6b..081d629dc33 100644 --- a/conans/test/functional/generators/cmake_test.py +++ b/conans/test/functional/generators/cmake_test.py @@ -567,6 +567,7 @@ def build(self): cmake_minimum_required(VERSION 3.0) project(test) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) + message("CONAN_BUILD_MODULES_PATHS: ${CONAN_BUILD_MODULES_PATHS}") conan_basic_setup() get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) message("otherhello link libraries: ${tmp}") From 63e9c8556b83811ecd913e8223063d47c31b909e Mon Sep 17 00:00:00 2001 From: danimtb Date: Wed, 2 Dec 2020 16:10:01 +0100 Subject: [PATCH 09/15] use targets --- conans/test/functional/generators/cmake_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conans/test/functional/generators/cmake_test.py b/conans/test/functional/generators/cmake_test.py index 081d629dc33..d88d72247ec 100644 --- a/conans/test/functional/generators/cmake_test.py +++ b/conans/test/functional/generators/cmake_test.py @@ -568,7 +568,7 @@ def build(self): project(test) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) message("CONAN_BUILD_MODULES_PATHS: ${CONAN_BUILD_MODULES_PATHS}") - conan_basic_setup() + conan_basic_setup(TARGETS) get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) message("otherhello link libraries: ${tmp}") """) From 4bdbed26183194104877065d3269ddd2b20c4c8a Mon Sep 17 00:00:00 2001 From: danimtb Date: Wed, 2 Dec 2020 16:52:42 +0100 Subject: [PATCH 10/15] build app --- .../cmake_find_package_multi_test.py | 20 ++++++++++++---- .../generators/cmake_find_package_test.py | 20 ++++++++++++---- .../test/functional/generators/cmake_test.py | 24 ++++++++++++------- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/conans/test/functional/generators/cmake_find_package_multi_test.py b/conans/test/functional/generators/cmake_find_package_multi_test.py index 8d3f736f68c..4474f24cdfd 100644 --- a/conans/test/functional/generators/cmake_find_package_multi_test.py +++ b/conans/test/functional/generators/cmake_find_package_multi_test.py @@ -168,9 +168,9 @@ def build(self): cmake.build() def package(self): - self.copy("*.h", dst="include") - self.copy("*.a", dst="lib") - self.copy("*.lib", dst="lib") + self.copy("*.h", dst="include", src="src") + self.copy("*.lib", dst="lib", keep_path=False) + self.copy("*.a", dst="lib", keep_path=False) self.copy("target-alias.cmake", dst="share/cmake") def package_info(self): @@ -182,6 +182,7 @@ def package_info(self): info = textwrap.dedent("""\ self.cpp_info.name = "namespace" self.cpp_info.filenames["cmake_find_package_multi"] = "hello" + self.cpp_info.components["comp"].libs = ["hello"] self.cpp_info.components["comp"].build_modules.append(module) self.cpp_info.components["comp"].builddirs = [builddir] """) @@ -193,6 +194,7 @@ def package_info(self): """) else: info = textwrap.dedent("""\ + self.cpp_info.libs = ["hello"] self.cpp_info.build_modules.append(module) self.cpp_info.builddirs = [builddir] """) @@ -213,7 +215,7 @@ class Conan(ConanFile): name = "consumer" version = "1.0" settings = "os", "compiler", "build_type", "arch" - exports_sources = ["CMakeLists.txt"] + exports_sources = ["CMakeLists.txt", "main.cpp"] generators = "cmake_find_package_multi" requires = "hello/1.0" @@ -229,7 +231,15 @@ def build(self): get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) message("otherhello link libraries: ${tmp}") """) - client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) + main = textwrap.dedent(""" + #include "hello.h" + + int main() { + hello(); + return 0; + } + """) + client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists, "main.cpp": main}) client.run("create .") if use_components: self.assertIn("otherhello link libraries: namespace::hello", client.out) diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py index 67f22f635ca..60905705f59 100644 --- a/conans/test/functional/generators/cmake_find_package_test.py +++ b/conans/test/functional/generators/cmake_find_package_test.py @@ -452,9 +452,9 @@ def build(self): cmake.build() def package(self): - self.copy("*.h", dst="include") - self.copy("*.a", dst="lib") - self.copy("*.lib", dst="lib") + self.copy("*.h", dst="include", src="src") + self.copy("*.lib", dst="lib", keep_path=False) + self.copy("*.a", dst="lib", keep_path=False) self.copy("target-alias.cmake", dst="share/cmake") def package_info(self): @@ -466,6 +466,7 @@ def package_info(self): info = textwrap.dedent("""\ self.cpp_info.name = "namespace" self.cpp_info.filenames["cmake_find_package"] = "hello" + self.cpp_info.components["comp"].libs = ["hello"] self.cpp_info.components["comp"].build_modules.append(module) self.cpp_info.components["comp"].builddirs = [builddir] """) @@ -477,6 +478,7 @@ def package_info(self): """) else: info = textwrap.dedent("""\ + self.cpp_info.libs = ["hello"] self.cpp_info.build_modules.append(module) self.cpp_info.builddirs = [builddir] """) @@ -497,7 +499,7 @@ class Conan(ConanFile): name = "consumer" version = "1.0" settings = "os", "compiler", "build_type", "arch" - exports_sources = ["CMakeLists.txt"] + exports_sources = ["CMakeLists.txt", "main.cpp"] generators = "cmake_find_package" requires = "hello/1.0" @@ -513,7 +515,15 @@ def build(self): get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) message("otherhello link libraries: ${tmp}") """) - client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) + main = textwrap.dedent(""" + #include "hello.h" + + int main() { + hello(); + return 0; + } + """) + client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists, "main.cpp": main}) client.run("create .") if use_components: self.assertIn("otherhello link libraries: namespace::hello", client.out) diff --git a/conans/test/functional/generators/cmake_test.py b/conans/test/functional/generators/cmake_test.py index d88d72247ec..3d8b5c1c4f9 100644 --- a/conans/test/functional/generators/cmake_test.py +++ b/conans/test/functional/generators/cmake_test.py @@ -527,12 +527,13 @@ def build(self): cmake.build() def package(self): - self.copy("*.h", dst="include") - self.copy("*.a", dst="lib") - self.copy("*.lib", dst="lib") + self.copy("*.h", dst="include", src="src") + self.copy("*.lib", dst="lib", keep_path=False) + self.copy("*.a", dst="lib", keep_path=False) self.copy("target-alias.cmake", dst="share/cmake") def package_info(self): + self.cpp_info.libs = ["hello"] builddir = os.path.join("share", "cmake") module = os.path.join(builddir, "target-alias.cmake") self.cpp_info.build_modules.append(module) @@ -553,12 +554,11 @@ class Conan(ConanFile): name = "consumer" version = "1.0" settings = "os", "compiler", "build_type", "arch" - exports_sources = ["CMakeLists.txt"] + exports_sources = ["CMakeLists.txt", "main.cpp"] generators = "cmake" requires = "hello/1.0" def build(self): - print(tools.load(os.path.join(self.install_folder, "conanbuildinfo.cmake"))) cmake = CMake(self) cmake.configure() cmake.build() @@ -567,12 +567,20 @@ def build(self): cmake_minimum_required(VERSION 3.0) project(test) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) - message("CONAN_BUILD_MODULES_PATHS: ${CONAN_BUILD_MODULES_PATHS}") conan_basic_setup(TARGETS) get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) message("otherhello link libraries: ${tmp}") + add_executable(app main.cpp) + target_link_libraries(app otherhello) """) - client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) + main = textwrap.dedent(""" + #include "hello.h" + + int main() { + hello(); + return 0; + } + """) + client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists, "main.cpp": main}) client.run("create .") - print(client.out) self.assertIn("otherhello link libraries: CONAN_PKG::hello", client.out) From 071bcbaa5e05ec2106f62490e605bf359ef495a1 Mon Sep 17 00:00:00 2001 From: danimtb Date: Tue, 12 Jan 2021 16:22:10 +0100 Subject: [PATCH 11/15] fix parametrized --- .../generators/cmake_find_package_test.py | 203 +++++++++--------- 1 file changed, 104 insertions(+), 99 deletions(-) diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py index 3ab845151bb..0da5d02c6a4 100644 --- a/conans/test/functional/generators/cmake_find_package_test.py +++ b/conans/test/functional/generators/cmake_find_package_test.py @@ -13,6 +13,110 @@ from conans.test.utils.tools import TestClient, NO_SETTINGS_PACKAGE_ID +@pytest.mark.slow +@pytest.mark.tool_cmake +class TestCMakeFindPackageGenerator: + + @pytest.mark.parametrize("use_components", [False, True]) + def test_build_modules_alias_target(self, use_components): + client = TestClient() + client.run("new hello/1.0 -s") + conanfile = textwrap.dedent(""" + import os + from conans import ConanFile, CMake + + class Conan(ConanFile): + name = "hello" + version = "1.0" + settings = "os", "arch", "compiler", "build_type" + exports_sources = ["target-alias.cmake", "src/*"] + generators = "cmake" + + def build(self): + cmake = CMake(self) + cmake.configure(source_folder="src") + cmake.build() + + def package(self): + self.copy("*.h", dst="include", src="src") + self.copy("*.lib", dst="lib", keep_path=False) + self.copy("*.a", dst="lib", keep_path=False) + self.copy("target-alias.cmake", dst="share/cmake") + + def package_info(self): + builddir = os.path.join("share", "cmake") + module = os.path.join(builddir, "target-alias.cmake") + %s + """) + if use_components: + info = textwrap.dedent("""\ + self.cpp_info.name = "namespace" + self.cpp_info.filenames["cmake_find_package"] = "hello" + self.cpp_info.components["comp"].libs = ["hello"] + self.cpp_info.components["comp"].build_modules.append(module) + self.cpp_info.components["comp"].builddirs = [builddir] + """) + target_alias = textwrap.dedent(""" + if(NOT TARGET otherhello) + add_library(otherhello INTERFACE IMPORTED) + target_link_libraries(otherhello INTERFACE namespace::hello) + endif() + """) + else: + info = textwrap.dedent("""\ + self.cpp_info.libs = ["hello"] + self.cpp_info.build_modules.append(module) + self.cpp_info.builddirs = [builddir] + """) + target_alias = textwrap.dedent(""" + if(NOT TARGET otherhello) + add_library(otherhello INTERFACE IMPORTED) + target_link_libraries(otherhello INTERFACE hello::hello) + endif() + """) + conanfile = conanfile % textwrap.indent(info, " ") + client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) + client.run("create .") + + consumer = textwrap.dedent(""" + from conans import ConanFile, CMake + + class Conan(ConanFile): + name = "consumer" + version = "1.0" + settings = "os", "compiler", "build_type", "arch" + exports_sources = ["CMakeLists.txt", "main.cpp"] + generators = "cmake_find_package" + requires = "hello/1.0" + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + """) + cmakelists = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.0) + project(test) + find_package(hello) + get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) + message("otherhello link libraries: ${tmp}") + """) + main = textwrap.dedent(""" + #include "hello.h" + + int main() { + hello(); + return 0; + } + """) + client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists, "main.cpp": main}) + client.run("create .") + if use_components: + assert "otherhello link libraries: namespace::hello" in client.out + else: + assert "otherhello link libraries: hello::hello" in client.out + + @pytest.mark.slow @pytest.mark.tool_cmake class CMakeFindPathGeneratorTest(unittest.TestCase): @@ -428,105 +532,6 @@ def build(self): client.run("create .") self.assertIn("Printing using a external module!", client.out) - @parameterized.expand([(False,), (True,)]) - def test_build_modules_alias_target(self, use_components): - client = TestClient() - client.run("new hello/1.0 -s") - conanfile = textwrap.dedent(""" - import os - from conans import ConanFile, CMake - - class Conan(ConanFile): - name = "hello" - version = "1.0" - settings = "os", "arch", "compiler", "build_type" - exports_sources = ["target-alias.cmake", "src/*"] - generators = "cmake" - - def build(self): - cmake = CMake(self) - cmake.configure(source_folder="src") - cmake.build() - - def package(self): - self.copy("*.h", dst="include", src="src") - self.copy("*.lib", dst="lib", keep_path=False) - self.copy("*.a", dst="lib", keep_path=False) - self.copy("target-alias.cmake", dst="share/cmake") - - def package_info(self): - builddir = os.path.join("share", "cmake") - module = os.path.join(builddir, "target-alias.cmake") - %s - """) - if use_components: - info = textwrap.dedent("""\ - self.cpp_info.name = "namespace" - self.cpp_info.filenames["cmake_find_package"] = "hello" - self.cpp_info.components["comp"].libs = ["hello"] - self.cpp_info.components["comp"].build_modules.append(module) - self.cpp_info.components["comp"].builddirs = [builddir] - """) - target_alias = textwrap.dedent(""" - if(NOT TARGET otherhello) - add_library(otherhello INTERFACE IMPORTED) - target_link_libraries(otherhello INTERFACE namespace::hello) - endif() - """) - else: - info = textwrap.dedent("""\ - self.cpp_info.libs = ["hello"] - self.cpp_info.build_modules.append(module) - self.cpp_info.builddirs = [builddir] - """) - target_alias = textwrap.dedent(""" - if(NOT TARGET otherhello) - add_library(otherhello INTERFACE IMPORTED) - target_link_libraries(otherhello INTERFACE hello::hello) - endif() - """) - conanfile = conanfile % textwrap.indent(info, " ") - client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) - client.run("create .") - - consumer = textwrap.dedent(""" - from conans import ConanFile, CMake - - class Conan(ConanFile): - name = "consumer" - version = "1.0" - settings = "os", "compiler", "build_type", "arch" - exports_sources = ["CMakeLists.txt", "main.cpp"] - generators = "cmake_find_package" - requires = "hello/1.0" - - def build(self): - cmake = CMake(self) - cmake.configure() - cmake.build() - """) - cmakelists = textwrap.dedent(""" - cmake_minimum_required(VERSION 3.0) - project(test) - find_package(hello) - get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) - message("otherhello link libraries: ${tmp}") - """) - main = textwrap.dedent(""" - #include "hello.h" - - int main() { - hello(); - return 0; - } - """) - client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists, "main.cpp": main}) - client.run("create .") - if use_components: - self.assertIn("otherhello link libraries: namespace::hello", client.out) - else: - self.assertIn("otherhello link libraries: hello::hello", client.out) - def test_cpp_info_name(self): client = TestClient() client.run("new hello/1.0 -s") From 6db61f2a0fbbb8b5b4c2c0348871f389b0052331 Mon Sep 17 00:00:00 2001 From: danimtb Date: Tue, 12 Jan 2021 18:12:57 +0100 Subject: [PATCH 12/15] avoid building --- .../generators/cmake_find_package_test.py | 49 ++++--------------- 1 file changed, 10 insertions(+), 39 deletions(-) diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py index 0da5d02c6a4..ed2410582f2 100644 --- a/conans/test/functional/generators/cmake_find_package_test.py +++ b/conans/test/functional/generators/cmake_find_package_test.py @@ -20,7 +20,6 @@ class TestCMakeFindPackageGenerator: @pytest.mark.parametrize("use_components", [False, True]) def test_build_modules_alias_target(self, use_components): client = TestClient() - client.run("new hello/1.0 -s") conanfile = textwrap.dedent(""" import os from conans import ConanFile, CMake @@ -29,23 +28,14 @@ class Conan(ConanFile): name = "hello" version = "1.0" settings = "os", "arch", "compiler", "build_type" - exports_sources = ["target-alias.cmake", "src/*"] + exports_sources = ["target-alias.cmake"] generators = "cmake" - def build(self): - cmake = CMake(self) - cmake.configure(source_folder="src") - cmake.build() - def package(self): - self.copy("*.h", dst="include", src="src") - self.copy("*.lib", dst="lib", keep_path=False) - self.copy("*.a", dst="lib", keep_path=False) self.copy("target-alias.cmake", dst="share/cmake") def package_info(self): - builddir = os.path.join("share", "cmake") - module = os.path.join(builddir, "target-alias.cmake") + module = os.path.join("share", "cmake", "target-alias.cmake") %s """) if use_components: @@ -53,27 +43,17 @@ def package_info(self): self.cpp_info.name = "namespace" self.cpp_info.filenames["cmake_find_package"] = "hello" self.cpp_info.components["comp"].libs = ["hello"] - self.cpp_info.components["comp"].build_modules.append(module) - self.cpp_info.components["comp"].builddirs = [builddir] - """) - target_alias = textwrap.dedent(""" - if(NOT TARGET otherhello) - add_library(otherhello INTERFACE IMPORTED) - target_link_libraries(otherhello INTERFACE namespace::hello) - endif() + self.cpp_info.components["comp"].build_modules["cmake_find_package"].append(module) """) else: info = textwrap.dedent("""\ self.cpp_info.libs = ["hello"] - self.cpp_info.build_modules.append(module) - self.cpp_info.builddirs = [builddir] - """) - target_alias = textwrap.dedent(""" - if(NOT TARGET otherhello) - add_library(otherhello INTERFACE IMPORTED) - target_link_libraries(otherhello INTERFACE hello::hello) - endif() + self.cpp_info.build_modules["cmake_find_package"].append(module) """) + target_alias = textwrap.dedent(""" + add_library(otherhello INTERFACE IMPORTED) + target_link_libraries(otherhello INTERFACE {target_name}) + """).format(target_name="namespace::hello" if use_components else "hello::hello") conanfile = conanfile % textwrap.indent(info, " ") client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) client.run("create .") @@ -85,14 +65,13 @@ class Conan(ConanFile): name = "consumer" version = "1.0" settings = "os", "compiler", "build_type", "arch" - exports_sources = ["CMakeLists.txt", "main.cpp"] + exports_sources = ["CMakeLists.txt"] generators = "cmake_find_package" requires = "hello/1.0" def build(self): cmake = CMake(self) cmake.configure() - cmake.build() """) cmakelists = textwrap.dedent(""" cmake_minimum_required(VERSION 3.0) @@ -101,15 +80,7 @@ def build(self): get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) message("otherhello link libraries: ${tmp}") """) - main = textwrap.dedent(""" - #include "hello.h" - - int main() { - hello(); - return 0; - } - """) - client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists, "main.cpp": main}) + client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) client.run("create .") if use_components: assert "otherhello link libraries: namespace::hello" in client.out From 39a7823a8c418226efb7a6e6715d6f71d8452205 Mon Sep 17 00:00:00 2001 From: danimtb Date: Tue, 12 Jan 2021 18:37:11 +0100 Subject: [PATCH 13/15] tests without building --- .../cmake_find_package_multi_test.py | 174 ++++++++---------- .../generators/cmake_find_package_test.py | 1 - .../test/functional/generators/cmake_test.py | 44 +---- 3 files changed, 85 insertions(+), 134 deletions(-) diff --git a/conans/test/functional/generators/cmake_find_package_multi_test.py b/conans/test/functional/generators/cmake_find_package_multi_test.py index bdadc29e62b..5fcc16d15e7 100644 --- a/conans/test/functional/generators/cmake_find_package_multi_test.py +++ b/conans/test/functional/generators/cmake_find_package_multi_test.py @@ -12,6 +12,81 @@ from conans.util.files import load +@pytest.mark.tool_cmake +class TestCMakeFindPackageMultiGenerator: + + @pytest.mark.parametrize("use_components", [False, True]) + def test_build_modules_alias_target(self, use_components): + client = TestClient() + conanfile = textwrap.dedent(""" + import os + from conans import ConanFile, CMake + + class Conan(ConanFile): + name = "hello" + version = "1.0" + settings = "os", "arch", "compiler", "build_type" + exports_sources = ["target-alias.cmake"] + generators = "cmake" + + def package(self): + self.copy("target-alias.cmake", dst="share/cmake") + + def package_info(self): + module = os.path.join("share", "cmake", "target-alias.cmake") + %s + """) + if use_components: + info = textwrap.dedent("""\ + self.cpp_info.name = "namespace" + self.cpp_info.filenames["cmake_find_package_multi"] = "hello" + self.cpp_info.components["comp"].libs = ["hello"] + self.cpp_info.components["comp"].build_modules["cmake_find_package_multi"].append( + module) + """) + else: + info = textwrap.dedent("""\ + self.cpp_info.libs = ["hello"] + self.cpp_info.build_modules["cmake_find_package_multi"].append(module) + """) + target_alias = textwrap.dedent(""" + add_library(otherhello INTERFACE IMPORTED) + target_link_libraries(otherhello INTERFACE {target_name}) + """).format(target_name="namespace::hello" if use_components else "hello::hello") + conanfile = conanfile % textwrap.indent(info, " ") + client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) + client.run("create .") + + consumer = textwrap.dedent(""" + from conans import ConanFile, CMake + + class Conan(ConanFile): + name = "consumer" + version = "1.0" + settings = "os", "compiler", "build_type", "arch" + exports_sources = ["CMakeLists.txt"] + generators = "cmake_find_package_multi" + requires = "hello/1.0" + + def build(self): + cmake = CMake(self) + cmake.configure() + """) + cmakelists = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.0) + project(test) + find_package(hello) + get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) + message("otherhello link libraries: ${tmp}") + """) + client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) + client.run("create .") + if use_components: + assert "otherhello link libraries: namespace::hello" in client.out + else: + assert "otherhello link libraries: hello::hello" in client.out + + @pytest.mark.slow @pytest.mark.tool_cmake class CMakeFindPathMultiGeneratorTest(unittest.TestCase): @@ -139,105 +214,6 @@ def build(self): client.run("create .") self.assertIn("Printing using a external module!", client.out) - @parameterized.expand([(False,), (True,)]) - def test_build_modules_alias_target(self, use_components): - client = TestClient() - client.run("new hello/1.0 -s") - conanfile = textwrap.dedent(""" - import os - from conans import ConanFile, CMake - - class Conan(ConanFile): - name = "hello" - version = "1.0" - settings = "os", "arch", "compiler", "build_type" - exports_sources = ["target-alias.cmake", "src/*"] - generators = "cmake" - - def build(self): - cmake = CMake(self) - cmake.configure(source_folder="src") - cmake.build() - - def package(self): - self.copy("*.h", dst="include", src="src") - self.copy("*.lib", dst="lib", keep_path=False) - self.copy("*.a", dst="lib", keep_path=False) - self.copy("target-alias.cmake", dst="share/cmake") - - def package_info(self): - builddir = os.path.join("share", "cmake") - module = os.path.join(builddir, "target-alias.cmake") - %s - """) - if use_components: - info = textwrap.dedent("""\ - self.cpp_info.name = "namespace" - self.cpp_info.filenames["cmake_find_package_multi"] = "hello" - self.cpp_info.components["comp"].libs = ["hello"] - self.cpp_info.components["comp"].build_modules.append(module) - self.cpp_info.components["comp"].builddirs = [builddir] - """) - target_alias = textwrap.dedent(""" - if(NOT TARGET otherhello) - add_library(otherhello INTERFACE IMPORTED) - target_link_libraries(otherhello INTERFACE namespace::hello) - endif() - """) - else: - info = textwrap.dedent("""\ - self.cpp_info.libs = ["hello"] - self.cpp_info.build_modules.append(module) - self.cpp_info.builddirs = [builddir] - """) - target_alias = textwrap.dedent(""" - if(NOT TARGET otherhello) - add_library(otherhello INTERFACE IMPORTED) - target_link_libraries(otherhello INTERFACE hello::hello) - endif() - """) - conanfile = conanfile % textwrap.indent(info, " ") - client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) - client.run("create .") - - consumer = textwrap.dedent(""" - from conans import ConanFile, CMake - - class Conan(ConanFile): - name = "consumer" - version = "1.0" - settings = "os", "compiler", "build_type", "arch" - exports_sources = ["CMakeLists.txt", "main.cpp"] - generators = "cmake_find_package_multi" - requires = "hello/1.0" - - def build(self): - cmake = CMake(self) - cmake.configure() - cmake.build() - """) - cmakelists = textwrap.dedent(""" - cmake_minimum_required(VERSION 3.0) - project(test) - find_package(hello) - get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) - message("otherhello link libraries: ${tmp}") - """) - main = textwrap.dedent(""" - #include "hello.h" - - int main() { - hello(); - return 0; - } - """) - client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists, "main.cpp": main}) - client.run("create .") - if use_components: - self.assertIn("otherhello link libraries: namespace::hello", client.out) - else: - self.assertIn("otherhello link libraries: hello::hello", client.out) - def test_cmake_find_package_system_libs(self): conanfile = textwrap.dedent(""" from conans import ConanFile, tools diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py index ed2410582f2..48b07b695a1 100644 --- a/conans/test/functional/generators/cmake_find_package_test.py +++ b/conans/test/functional/generators/cmake_find_package_test.py @@ -13,7 +13,6 @@ from conans.test.utils.tools import TestClient, NO_SETTINGS_PACKAGE_ID -@pytest.mark.slow @pytest.mark.tool_cmake class TestCMakeFindPackageGenerator: diff --git a/conans/test/functional/generators/cmake_test.py b/conans/test/functional/generators/cmake_test.py index 717dc1c7ea5..f80042de434 100644 --- a/conans/test/functional/generators/cmake_test.py +++ b/conans/test/functional/generators/cmake_test.py @@ -505,9 +505,8 @@ def build(self): # Test cmake_find_package_multi generator without components run_test("cmake_find_package_multi", cmakelists, with_components=False) - def test_build_modules_alias_target(self): + def test_build_modules_alias_target(self, use_components=False): client = TestClient() - client.run("new hello/1.0 -s") conanfile = textwrap.dedent(""" import os from conans import ConanFile, CMake @@ -516,50 +515,37 @@ class Conan(ConanFile): name = "hello" version = "1.0" settings = "os", "arch", "compiler", "build_type" - exports_sources = ["target-alias.cmake", "src/*"] - generators = "cmake" - - def build(self): - cmake = CMake(self) - cmake.configure(source_folder="src") - cmake.build() + exports_sources = ["target-alias.cmake"] def package(self): - self.copy("*.h", dst="include", src="src") - self.copy("*.lib", dst="lib", keep_path=False) - self.copy("*.a", dst="lib", keep_path=False) self.copy("target-alias.cmake", dst="share/cmake") def package_info(self): + module = os.path.join("share", "cmake", "target-alias.cmake") self.cpp_info.libs = ["hello"] - builddir = os.path.join("share", "cmake") - module = os.path.join(builddir, "target-alias.cmake") - self.cpp_info.build_modules.append(module) - self.cpp_info.builddirs = [builddir] - """) + self.cpp_info.build_modules["cmake"].append(module) + """) target_alias = textwrap.dedent(""" add_library(otherhello INTERFACE IMPORTED) - target_link_libraries(otherhello INTERFACE CONAN_PKG::hello) + target_link_libraries(otherhello INTERFACE hello::hello) """) client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) client.run("create .") consumer = textwrap.dedent(""" - import os - from conans import ConanFile, CMake, tools + from conans import ConanFile, CMake class Conan(ConanFile): name = "consumer" version = "1.0" settings = "os", "compiler", "build_type", "arch" - exports_sources = ["CMakeLists.txt", "main.cpp"] + exports_sources = ["CMakeLists.txt"] generators = "cmake" requires = "hello/1.0" def build(self): cmake = CMake(self) cmake.configure() - cmake.build() """) cmakelists = textwrap.dedent(""" cmake_minimum_required(VERSION 3.0) @@ -568,17 +554,7 @@ def build(self): conan_basic_setup(TARGETS) get_target_property(tmp otherhello INTERFACE_LINK_LIBRARIES) message("otherhello link libraries: ${tmp}") - add_executable(app main.cpp) - target_link_libraries(app otherhello) """) - main = textwrap.dedent(""" - #include "hello.h" - - int main() { - hello(); - return 0; - } - """) - client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists, "main.cpp": main}) + client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) client.run("create .") - self.assertIn("otherhello link libraries: CONAN_PKG::hello", client.out) + assert "otherhello link libraries: hello::hello" in client.out From 8544eb3feab20617c0e5e9be17281a69aaa372e2 Mon Sep 17 00:00:00 2001 From: danimtb Date: Wed, 13 Jan 2021 15:42:42 +0100 Subject: [PATCH 14/15] review --- conans/client/generators/cmake_find_package.py | 7 +++++-- conans/client/generators/cmake_find_package_multi.py | 7 +++++-- .../functional/generators/cmake_find_package_multi_test.py | 6 +++--- .../test/functional/generators/cmake_find_package_test.py | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/conans/client/generators/cmake_find_package.py b/conans/client/generators/cmake_find_package.py index 9bcd22fdd89..d759759dc98 100644 --- a/conans/client/generators/cmake_find_package.py +++ b/conans/client/generators/cmake_find_package.py @@ -202,12 +202,15 @@ class CMakeFindPackageGenerator(GeneratorComponentsMixin, Generator): ########## BUILD MODULES #################################################################### ############################################################################################# - ########## GLOBAL BUILD MODULES ############################################################# + {%- for comp_name, comp in components %} + ########## COMPONENT {{ comp_name }} BUILD MODULES ########################################## - foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_BUILD_MODULES_PATHS}' }}) + foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_'+comp_name+'_BUILD_MODULES_PATHS}' }}) include(${_BUILD_MODULE_PATH}) endforeach() + {%- endfor %} + """)) @property diff --git a/conans/client/generators/cmake_find_package_multi.py b/conans/client/generators/cmake_find_package_multi.py index ddb564ad0e4..7ad8f3ec9fb 100644 --- a/conans/client/generators/cmake_find_package_multi.py +++ b/conans/client/generators/cmake_find_package_multi.py @@ -272,13 +272,16 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator): ########## BUILD MODULES #################################################################### ############################################################################################# - ########## GLOBAL BUILD MODULES ############################################################# + {%- for comp_name, comp in components %} + + ########## COMPONENT {{ comp_name }} BUILD MODULES ########################################## {%- for config in configs %} - foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_BUILD_MODULES_PATHS_'+config.upper()+'}' }}) + foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_'+comp_name+'_BUILD_MODULES_PATHS_'+config.upper()+'}' }}) include(${_BUILD_MODULE_PATH}) endforeach() + {%- endfor %} {%- endfor %} """)) diff --git a/conans/test/functional/generators/cmake_find_package_multi_test.py b/conans/test/functional/generators/cmake_find_package_multi_test.py index 5fcc16d15e7..797cbb82af4 100644 --- a/conans/test/functional/generators/cmake_find_package_multi_test.py +++ b/conans/test/functional/generators/cmake_find_package_multi_test.py @@ -40,8 +40,8 @@ def package_info(self): info = textwrap.dedent("""\ self.cpp_info.name = "namespace" self.cpp_info.filenames["cmake_find_package_multi"] = "hello" - self.cpp_info.components["comp"].libs = ["hello"] - self.cpp_info.components["comp"].build_modules["cmake_find_package_multi"].append( + self.cpp_info.components["hello"].libs = ["hello"] + self.cpp_info.components["hello"].build_modules["cmake_find_package_multi"].append( module) """) else: @@ -52,7 +52,7 @@ def package_info(self): target_alias = textwrap.dedent(""" add_library(otherhello INTERFACE IMPORTED) target_link_libraries(otherhello INTERFACE {target_name}) - """).format(target_name="namespace::hello" if use_components else "hello::hello") + """).format(target_name="namespace::comp" if use_components else "hello::hello") conanfile = conanfile % textwrap.indent(info, " ") client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) client.run("create .") diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py index 48b07b695a1..071f94b0bc2 100644 --- a/conans/test/functional/generators/cmake_find_package_test.py +++ b/conans/test/functional/generators/cmake_find_package_test.py @@ -52,7 +52,7 @@ def package_info(self): target_alias = textwrap.dedent(""" add_library(otherhello INTERFACE IMPORTED) target_link_libraries(otherhello INTERFACE {target_name}) - """).format(target_name="namespace::hello" if use_components else "hello::hello") + """).format(target_name="namespace::comp" if use_components else "hello::hello") conanfile = conanfile % textwrap.indent(info, " ") client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) client.run("create .") @@ -82,7 +82,7 @@ def build(self): client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) client.run("create .") if use_components: - assert "otherhello link libraries: namespace::hello" in client.out + assert "otherhello link libraries: namespace::comp" in client.out else: assert "otherhello link libraries: hello::hello" in client.out From 81f181aec3594b6ea30fbf10f617f42205ce9a60 Mon Sep 17 00:00:00 2001 From: danimtb Date: Wed, 13 Jan 2021 16:57:32 +0100 Subject: [PATCH 15/15] fix tests --- .../generators/cmake_find_package_multi_test.py | 9 ++++----- .../functional/generators/cmake_find_package_test.py | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/conans/test/functional/generators/cmake_find_package_multi_test.py b/conans/test/functional/generators/cmake_find_package_multi_test.py index 797cbb82af4..9278cc0e5ed 100644 --- a/conans/test/functional/generators/cmake_find_package_multi_test.py +++ b/conans/test/functional/generators/cmake_find_package_multi_test.py @@ -40,9 +40,8 @@ def package_info(self): info = textwrap.dedent("""\ self.cpp_info.name = "namespace" self.cpp_info.filenames["cmake_find_package_multi"] = "hello" - self.cpp_info.components["hello"].libs = ["hello"] - self.cpp_info.components["hello"].build_modules["cmake_find_package_multi"].append( - module) + self.cpp_info.components["comp"].libs = ["hello"] + self.cpp_info.components["comp"].build_modules["cmake_find_package_multi"].append(module) """) else: info = textwrap.dedent("""\ @@ -53,7 +52,7 @@ def package_info(self): add_library(otherhello INTERFACE IMPORTED) target_link_libraries(otherhello INTERFACE {target_name}) """).format(target_name="namespace::comp" if use_components else "hello::hello") - conanfile = conanfile % textwrap.indent(info, " ") + conanfile = conanfile % "\n".join([" %s" % line for line in info.splitlines()]) client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) client.run("create .") @@ -82,7 +81,7 @@ def build(self): client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) client.run("create .") if use_components: - assert "otherhello link libraries: namespace::hello" in client.out + assert "otherhello link libraries: namespace::comp" in client.out else: assert "otherhello link libraries: hello::hello" in client.out diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py index 071f94b0bc2..430ec4fe89b 100644 --- a/conans/test/functional/generators/cmake_find_package_test.py +++ b/conans/test/functional/generators/cmake_find_package_test.py @@ -53,7 +53,7 @@ def package_info(self): add_library(otherhello INTERFACE IMPORTED) target_link_libraries(otherhello INTERFACE {target_name}) """).format(target_name="namespace::comp" if use_components else "hello::hello") - conanfile = conanfile % textwrap.indent(info, " ") + conanfile = conanfile % "\n".join([" %s" % line for line in info.splitlines()]) client.save({"conanfile.py": conanfile, "target-alias.cmake": target_alias}) client.run("create .")