From 1b29187343bb4d08f86a8d96e42e5aac1fc7ff46 Mon Sep 17 00:00:00 2001 From: Ole Untzelmann Date: Mon, 8 May 2023 12:32:33 +0200 Subject: [PATCH 1/4] CMakeDeps: Fix imported library config suffix Currently the imported libraries are set without config suffix first. At the end there is one additional set using the config suffix, which overwrites the already set libraries. Fixes #13504 --- conan/tools/cmake/cmakedeps/templates/macros.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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() From 841dd9783df2938e1cc418bc5ffa05dc6f9d6cb8 Mon Sep 17 00:00:00 2001 From: Luis Caro <3535649+jcar87@users.noreply.github.com> Date: Fri, 12 May 2023 11:16:14 +0100 Subject: [PATCH 2/4] cmakedeps: add test to ensure CMake TARGET_RUNTIME_DLLS works --- .../cmake/cmakedeps/test_cmakedeps.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py index d3001e94a3d..48b448780dc 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") +@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(): From 935b224a3bb218aa87b8b52e156d6477202733fe Mon Sep 17 00:00:00 2001 From: Luis Caro <3535649+jcar87@users.noreply.github.com> Date: Fri, 12 May 2023 12:27:58 +0100 Subject: [PATCH 3/4] test_cmake_target_runtime_dlls: requires cmake 3.21 --- .../functional/toolchains/cmake/cmakedeps/test_cmakedeps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py index 48b448780dc..162e74ba9a7 100644 --- a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py +++ b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py @@ -661,7 +661,7 @@ def package_info(self): assert "talk: Release!" in client.out assert "main: Debug!" in client.out -@pytest.mark.tool("cmake") +@pytest.mark.tool("cmake", "3.21") @pytest.mark.skipif(platform.system() != "Windows", reason="Windows DLL specific") def test_cmake_target_runtime_dlls(): # https://github.com/conan-io/conan/issues/13504 From 83300a7337ba7cbb710f6109eb2ebb2f382ce7aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Ram=C3=ADrez?= Date: Fri, 12 May 2023 13:31:31 +0200 Subject: [PATCH 4/4] Update conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py --- .../functional/toolchains/cmake/cmakedeps/test_cmakedeps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py index 162e74ba9a7..792dcfa6124 100644 --- a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py +++ b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py @@ -661,7 +661,7 @@ def package_info(self): assert "talk: Release!" in client.out assert "main: Debug!" in client.out -@pytest.mark.tool("cmake", "3.21") +@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