diff --git a/conan/tools/cmake/cmakedeps/templates/macros.py b/conan/tools/cmake/cmakedeps/templates/macros.py index d3249bcea11..dbddc69d80a 100644 --- a/conan/tools/cmake/cmakedeps/templates/macros.py +++ b/conan/tools/cmake/cmakedeps/templates/macros.py @@ -67,13 +67,13 @@ def template(self): if(NOT TARGET ${_LIB_NAME}) add_library(${_LIB_NAME} UNKNOWN IMPORTED) endif() - set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION ${CONAN_FOUND_LIBRARY}) + set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION${config_suffix} ${CONAN_FOUND_LIBRARY}) else() if(NOT TARGET ${_LIB_NAME}) add_library(${_LIB_NAME} SHARED IMPORTED) endif() - set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION ${CONAN_SHARED_FOUND_LIBRARY}) - set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_IMPLIB ${CONAN_FOUND_LIBRARY}) + set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION${config_suffix} ${CONAN_SHARED_FOUND_LIBRARY}) + set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_IMPLIB${config_suffix} ${CONAN_FOUND_LIBRARY}) message(DEBUG "Found DLL and STATIC at ${CONAN_SHARED_FOUND_LIBRARY}, ${CONAN_FOUND_LIBRARY}") endif() unset(CONAN_SHARED_FOUND_LIBRARY CACHE) @@ -83,10 +83,8 @@ def template(self): add_library(${_LIB_NAME} ${library_type} IMPORTED) endif() message(DEBUG "Created target ${_LIB_NAME} ${library_type} IMPORTED") - set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION ${CONAN_FOUND_LIBRARY} IMPORTED_NO_SONAME ${no_soname_mode}) + set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION${config_suffix} ${CONAN_FOUND_LIBRARY} IMPORTED_NO_SONAME ${no_soname_mode}) endif() - # Link library file - 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/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py index d3001e94a3d..792dcfa6124 100644 --- a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py +++ b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py @@ -661,6 +661,37 @@ def package_info(self): assert "talk: Release!" in client.out assert "main: Debug!" in client.out +@pytest.mark.tool("cmake", "3.23") +@pytest.mark.skipif(platform.system() != "Windows", reason="Windows DLL specific") +def test_cmake_target_runtime_dlls(): + # https://github.com/conan-io/conan/issues/13504 + + client = TestClient() + client.run("new cmake_lib -d name=hello -d version=1.0") + client.run('create . -tf="" -s build_type=Release -o "hello/*":shared=True') + + client.run("new cmake_exe -d name=foo -d version=1.0 -d requires=hello/1.0 -f") + cmakelists = textwrap.dedent(""" + cmake_minimum_required(VERSION 3.15) + project(foo CXX) + find_package(hello CONFIG REQUIRED) + add_executable(foo src/foo.cpp src/main.cpp) + target_link_libraries(foo PRIVATE hello::hello) + # Make sure CMake copies DLLs from dependencies, next to the executable + # in this case it should copy hello.dll + add_custom_command(TARGET foo POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy $ $ + COMMAND_EXPAND_LISTS) + """) + client.save({"CMakeLists.txt": cmakelists}) + client.run('install . -s build_type=Release -o "hello/*":shared=True') + client.run_command("cmake -S . -B build/ -DCMAKE_TOOLCHAIN_FILE=build/generators/conan_toolchain.cmake") + client.run_command("cmake --build build --config Release") + client.run_command("build\\Release\\foo.exe") + + assert os.path.exists(os.path.join(client.current_folder, "build", "Release", "hello.dll")) + assert "hello/1.0: Hello World Release!" in client.out # if the DLL wasn't copied, the application would not run and show output + @pytest.mark.tool("cmake") def test_quiet():