From 0b282b7bb81538660b19d4438dbb19f5787a434a Mon Sep 17 00:00:00 2001 From: Luis Martinez Date: Tue, 22 Nov 2022 16:40:32 +0100 Subject: [PATCH] CMakeDeps managing CMAKE_MAP_IMPORTED_CONFIG_XXX (#12049) Co-authored-by: memsharded --- .../tools/cmake/cmakedeps/templates/macros.py | 2 +- .../templates/target_configuration.py | 4 +- .../cmake/cmakedeps/test_cmakedeps.py | 83 +++++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/conan/tools/cmake/cmakedeps/templates/macros.py b/conan/tools/cmake/cmakedeps/templates/macros.py index 17d96f88d2f..6ae09ea753f 100644 --- a/conan/tools/cmake/cmakedeps/templates/macros.py +++ b/conan/tools/cmake/cmakedeps/templates/macros.py @@ -59,7 +59,7 @@ def template(self): add_library(${_LIB_NAME} UNKNOWN IMPORTED) endif() # Link library file - set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION ${CONAN_FOUND_LIBRARY}) + set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION${config_suffix} ${CONAN_FOUND_LIBRARY}) list(APPEND _out_libraries_target ${_LIB_NAME}) message(VERBOSE "Conan: Found: ${CONAN_FOUND_LIBRARY}") else() diff --git a/conan/tools/cmake/cmakedeps/templates/target_configuration.py b/conan/tools/cmake/cmakedeps/templates/target_configuration.py index a56422356ff..7c29c05efe2 100644 --- a/conan/tools/cmake/cmakedeps/templates/target_configuration.py +++ b/conan/tools/cmake/cmakedeps/templates/target_configuration.py @@ -67,7 +67,7 @@ def template(self): ######## Create an interface target to contain all the dependencies (frameworks, system and conan deps) if(NOT TARGET {{ pkg_name+'_DEPS_TARGET'}}) - add_library({{ pkg_name+'_DEPS_TARGET'}} INTERFACE) + add_library({{ pkg_name+'_DEPS_TARGET'}} INTERFACE IMPORTED) endif() set_property(TARGET {{ pkg_name + '_DEPS_TARGET'}} @@ -146,7 +146,7 @@ def template(self): ######## Create an interface target to contain all the dependencies (frameworks, system and conan deps) if(NOT TARGET {{ pkg_name + '_' + comp_variable_name + '_DEPS_TARGET'}}) - add_library({{ pkg_name + '_' + comp_variable_name + '_DEPS_TARGET'}} INTERFACE) + add_library({{ pkg_name + '_' + comp_variable_name + '_DEPS_TARGET'}} INTERFACE IMPORTED) endif() set_property(TARGET {{ pkg_name + '_' + comp_variable_name + '_DEPS_TARGET'}} diff --git a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py index f44aea035ac..45b5d1d97e6 100644 --- a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py +++ b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py @@ -574,3 +574,86 @@ def test_error_missing_build_type(): run_app = r".\Release\app.exe" if platform.system() == "Windows" else "./Release/app" client.run_command(run_app) assert "Hello World Release!" in client.out + + +@pytest.mark.tool_cmake +def test_map_imported_config(): + # https://github.com/conan-io/conan/issues/12041 + + client = TestClient() + client.run("new hello/1.0 -m=cmake_lib") + client.run("create . -tf=None -s build_type=Release") + + # It is necessary a 2-level test to make the fixes evident + talk_cpp = gen_function_cpp(name="talk", includes=["hello"], calls=["hello"]) + talk_h = gen_function_h(name="talk") + conanfile = textwrap.dedent(""" + import os + from conan import ConanFile + from conan.tools.cmake import CMake + from conan.tools.files import copy + class HelloConan(ConanFile): + name = 'talk' + version = '1.0' + exports_sources = "*" + generators = "CMakeDeps", "CMakeToolchain" + requires = ("hello/1.0", ) + settings = "os", "compiler", "arch", "build_type" + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def package(self): + cmake = CMake(self) + cmake.install() + + def package_info(self): + self.cpp_info.libs.append("talk") + """) + + client.save({"conanfile.py": conanfile, + "CMakeLists.txt": gen_cmakelists(libname="talk", + libsources=["talk.cpp"], find_package=["hello"], + install=True, public_header="talk.h"), + "talk.cpp": talk_cpp, + "talk.h": talk_h}, clean_first=True) + client.run("create . -tf=None -s build_type=Release") + + conanfile = textwrap.dedent(""" + [requires] + talk/1.0 + [generators] + CMakeDeps + CMakeToolchain + """) + + cmakelists = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.15) + project(app) + set(CMAKE_MAP_IMPORTED_CONFIG_DEBUG Release) + find_package(talk REQUIRED) + add_executable(app main.cpp) + target_link_libraries(app talk::talk) + """) + + client.save({ + "conanfile.txt": conanfile, + "main.cpp": gen_function_cpp(name="main", includes=["talk"], calls=["talk"]), + "CMakeLists.txt": cmakelists + }, clean_first=True) + + client.run("install . -s build_type=Release") + if platform.system() != "Windows": + client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake " + "-DCMAKE_BUILD_TYPE=DEBUG") + client.run_command("cmake --build .") + client.run_command("./app") + else: + client.run_command("cmake . -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake") + client.run_command("cmake --build . --config Debug") + client.run_command("Debug\\app.exe") + assert "hello/1.0: Hello World Release!" in client.out + assert "talk: Release!" in client.out + assert "main: Debug!" in client.out