Skip to content

Commit

Permalink
CMakeDeps managing CMAKE_MAP_IMPORTED_CONFIG_XXX (#12049)
Browse files Browse the repository at this point in the history

Co-authored-by: memsharded <james@conan.io>
  • Loading branch information
lasote and memsharded authored Nov 22, 2022
1 parent c021ce7 commit 0b282b7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion conan/tools/cmake/cmakedeps/templates/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions conan/tools/cmake/cmakedeps/templates/target_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}
Expand Down Expand Up @@ -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'}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0b282b7

Please sign in to comment.