From 9409a5d109e471ed794c7e4d2bcd62377c3a4968 Mon Sep 17 00:00:00 2001 From: SSE4 Date: Fri, 19 Feb 2021 16:41:48 +0700 Subject: [PATCH] - add tools.create_cmake_module_alias_targets Signed-off-by: SSE4 --- conans/client/tools/__init__.py | 2 + conans/client/tools/cmake.py | 11 ++++ .../cmake_find_package_multi_test.py | 61 +++++++++++++++++++ conans/tools.py | 1 + 4 files changed, 75 insertions(+) create mode 100644 conans/client/tools/cmake.py diff --git a/conans/client/tools/__init__.py b/conans/client/tools/__init__.py index f1572b1acaf..1cbbf589a41 100644 --- a/conans/client/tools/__init__.py +++ b/conans/client/tools/__init__.py @@ -3,6 +3,8 @@ # noinspection PyUnresolvedReferences from .apple import * # noinspection PyUnresolvedReferences +from .cmake import * +# noinspection PyUnresolvedReferences from .env import * # noinspection PyUnresolvedReferences from .files import * diff --git a/conans/client/tools/cmake.py b/conans/client/tools/cmake.py new file mode 100644 index 00000000000..c6179037fb6 --- /dev/null +++ b/conans/client/tools/cmake.py @@ -0,0 +1,11 @@ +from conans.client.tools.files import save + + +def create_cmake_module_alias_targets(_, module, targets): + content = "" + for alias, aliased in targets.items(): + content += "if(TARGET {aliased} AND NOT TARGET {alias})\n" \ + " add_library({alias} INTERFACE IMPORTED)\n" \ + " set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased})\n" \ + "endif()\n".format(alias=alias, aliased=aliased) + save(module, content) 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 bc5cd45fe12..2e5c904eff3 100644 --- a/conans/test/functional/generators/cmake_find_package_multi_test.py +++ b/conans/test/functional/generators/cmake_find_package_multi_test.py @@ -11,6 +11,67 @@ from conans.util.files import load +@pytest.mark.tool_cmake +class TestCreateModuleOfficialCMakeTargets: + def test_create_module_official_cmake_targets(self): + client = TestClient() + + conanfile = textwrap.dedent(""" + import os + from conans import ConanFile, CMake, tools + + class Conan(ConanFile): + name = "hello" + version = "1.0" + settings = "os", "arch", "compiler", "build_type" + + @property + def _module(self): + return os.path.join(self.package_folder, "lib", "cmake", + "conan-official-foo-targets.cmake") + + def package(self): + tools.create_cmake_module_alias_targets(self, self._module, + {"hello": "hello::hello"}) + + def package_info(self): + self.cpp_info.builddirs = [os.path.dirname(self._module)] + self.cpp_info.build_modules = [self._module] + """) + + client.save({"conanfile.py": conanfile}) + 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 REQUIRED) + get_target_property(link_libraries hello INTERFACE_LINK_LIBRARIES) + message("hello link libraries: ${link_libraries}") + """) + + client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists}) + client.run("create .") + + assert "hello link libraries: hello::hello" in client.out + + @pytest.mark.tool_cmake class TestCMakeFindPackageMultiGenerator: diff --git a/conans/tools.py b/conans/tools.py index 9b1a11806d1..d967665987a 100644 --- a/conans/tools.py +++ b/conans/tools.py @@ -23,6 +23,7 @@ from conans.client.tools.intel import * # pylint: disable=unused-import from conans.client.tools.apple import * from conans.client.tools.android import * +from conans.client.tools.cmake import * # Tools form conans.util from conans.util.env_reader import get_env from conans.util.files import _generic_algorithm_sum, load, md5, md5sum, mkdir, relative_dirs, \