Skip to content

Commit

Permalink
- add tools.create_cmake_module_alias_targets
Browse files Browse the repository at this point in the history
Signed-off-by: SSE4 <[email protected]>
  • Loading branch information
SSE4 committed Feb 19, 2021
1 parent e04135f commit 9409a5d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions conans/client/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# noinspection PyUnresolvedReferences
from .apple import *
# noinspection PyUnresolvedReferences
from .cmake import *
# noinspection PyUnresolvedReferences
from .env import *
# noinspection PyUnresolvedReferences
from .files import *
Expand Down
11 changes: 11 additions & 0 deletions conans/client/tools/cmake.py
Original file line number Diff line number Diff line change
@@ -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)
61 changes: 61 additions & 0 deletions conans/test/functional/generators/cmake_find_package_multi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions conans/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down

0 comments on commit 9409a5d

Please sign in to comment.