From ee475f0b2d18c62e8150da466f2e534e7c443a1b Mon Sep 17 00:00:00 2001 From: James Date: Tue, 31 Jan 2023 12:19:53 +0100 Subject: [PATCH] [develop2] fixing transitive linux libraries cmake (#13010) * fixing transitive linux libraries cmake * adding test and support for components --- .../templates/target_configuration.py | 6 ++ .../cmake/cmakedeps/templates/target_data.py | 3 +- .../toolchains/cmake/test_shared_cmake.py | 84 +++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/conan/tools/cmake/cmakedeps/templates/target_configuration.py b/conan/tools/cmake/cmakedeps/templates/target_configuration.py index 752a7b6b304..2363147b794 100644 --- a/conan/tools/cmake/cmakedeps/templates/target_configuration.py +++ b/conan/tools/cmake/cmakedeps/templates/target_configuration.py @@ -108,6 +108,10 @@ def template(self): set_property(TARGET {{root_target_name}} PROPERTY INTERFACE_INCLUDE_DIRECTORIES $<$:${{'{'}}{{pkg_name}}_INCLUDE_DIRS{{config_suffix}}}> APPEND) + # Necessary to find LINK shared libraries in Linux + set_property(TARGET {{root_target_name}} + PROPERTY INTERFACE_LINK_DIRECTORIES + $<$:${{'{'}}{{pkg_name}}_LIB_DIRS{{config_suffix}}}> APPEND) set_property(TARGET {{root_target_name}} PROPERTY INTERFACE_COMPILE_DEFINITIONS $<$:${{'{'}}{{pkg_name}}_COMPILE_DEFINITIONS{{config_suffix}}}> APPEND) @@ -184,6 +188,8 @@ def template(self): $<$:{{tvalue(pkg_name, comp_variable_name, 'LINKER_FLAGS', config_suffix)}}> APPEND) set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_INCLUDE_DIRECTORIES $<$:{{tvalue(pkg_name, comp_variable_name, 'INCLUDE_DIRS', config_suffix)}}> APPEND) + set_property(TARGET {{comp_target_name }} PROPERTY INTERFACE_LINK_DIRECTORIES + $<$:{{tvalue(pkg_name, comp_variable_name, 'LIB_DIRS', config_suffix)}}> APPEND) set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_COMPILE_DEFINITIONS $<$:{{tvalue(pkg_name, comp_variable_name, 'COMPILE_DEFINITIONS', config_suffix)}}> APPEND) set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_COMPILE_OPTIONS diff --git a/conan/tools/cmake/cmakedeps/templates/target_data.py b/conan/tools/cmake/cmakedeps/templates/target_data.py index ffbce46b897..e682235c115 100644 --- a/conan/tools/cmake/cmakedeps/templates/target_data.py +++ b/conan/tools/cmake/cmakedeps/templates/target_data.py @@ -319,7 +319,8 @@ def join_defines(values, prefix=""): if require and not require.headers: self.include_paths = "" if require and not require.libs: - self.lib_paths = "" + # self.lib_paths = "" IMPORTANT! LINKERS IN LINUX FOR SHARED MIGHT NEED IT EVEN IF + # NOT REALLY LINKING LIB self.libs = "" self.system_libs = "" self.frameworks = "" diff --git a/conans/test/functional/toolchains/cmake/test_shared_cmake.py b/conans/test/functional/toolchains/cmake/test_shared_cmake.py index 58d36b49dee..0293100e687 100644 --- a/conans/test/functional/toolchains/cmake/test_shared_cmake.py +++ b/conans/test/functional/toolchains/cmake/test_shared_cmake.py @@ -34,6 +34,90 @@ def test_shared_cmake_toolchain(): assert "chat: Release!" in client.out assert "hello: Release!" in client.out + # https://github.com/conan-io/conan/issues/13000 + # This failed, because of rpath link in Linux + client = TestClient(servers=client.servers, inputs=["admin", "password"]) + client.save(pkg_cmake_app("app", "0.1", requires=["chat/0.1"]), clean_first=True) + client.run("create . -o chat/*:shared=True -o hello/*:shared=True") + client.run("upload * -c -r default") + client.run("remove * -c") + + client = TestClient(servers=client.servers) + client.run("install --requires=app/0.1@ -o chat*:shared=True -o hello/*:shared=True -g VirtualRunEnv") + # This only finds "app" executable because the "app/0.1" is declaring package_type="application" + # otherwise, run=None and nothing can tell us if the conanrunenv should have the PATH. + command = environment_wrap_command("conanrun", client.current_folder, "app") + + client.run_command(command) + assert "main: Release!" in client.out + assert "chat: Release!" in client.out + assert "hello: Release!" in client.out + + +@pytest.mark.tool("cmake") +def test_shared_cmake_toolchain_components(): + """ the same as above, but with components. + """ + client = TestClient(default_server_user=True) + + client.save(pkg_cmake("hello", "0.1")) + conanfile = client.load("conanfile.py") + conanfile2 = conanfile.replace('self.cpp_info.libs = ["hello"]', + 'self.cpp_info.components["hi"].libs = ["hello"]') + assert conanfile != conanfile2 + client.save({"conanfile.py": conanfile2}) + client.run("create . -o hello/*:shared=True") + # Chat + client.save(pkg_cmake("chat", "0.1", requires=["hello/0.1"]), clean_first=True) + conanfile = client.load("conanfile.py") + conanfile2 = conanfile.replace('self.cpp_info.libs = ["chat"]', + 'self.cpp_info.components["talk"].libs = ["chat"]\n' + ' self.cpp_info.components["talk"].requires=["hello::hi"]') + assert conanfile != conanfile2 + client.save({"conanfile.py": conanfile2}) + client.run("create . -o chat/*:shared=True -o hello/*:shared=True") + + # App + client.save(pkg_cmake_app("app", "0.1", requires=["chat/0.1"]), clean_first=True) + cmakelist = client.load("CMakeLists.txt") + cmakelist2 = cmakelist.replace('target_link_libraries(app chat::chat )', + 'target_link_libraries(app chat::talk )') + assert cmakelist != cmakelist2 + client.save({"CMakeLists.txt": cmakelist2}) + client.run("create . -o chat/*:shared=True -o hello/*:shared=True") + client.run("upload * -c -r default") + client.run("remove * -c") + + client = TestClient(servers=client.servers) + client.run("install --requires=app/0.1@ -o chat*:shared=True -o hello/*:shared=True -g VirtualRunEnv") + # This only finds "app" executable because the "app/0.1" is declaring package_type="application" + # otherwise, run=None and nothing can tell us if the conanrunenv should have the PATH. + command = environment_wrap_command("conanrun", client.current_folder, "app") + + client.run_command(command) + assert "main: Release!" in client.out + assert "chat: Release!" in client.out + assert "hello: Release!" in client.out + + # https://github.com/conan-io/conan/issues/13000 + # This failed, because of rpath link in Linux + client = TestClient(servers=client.servers, inputs=["admin", "password"]) + client.save(pkg_cmake_app("app", "0.1", requires=["chat/0.1"]), clean_first=True) + client.run("create . -o chat/*:shared=True -o hello/*:shared=True") + client.run("upload * -c -r default") + client.run("remove * -c") + + client = TestClient(servers=client.servers) + client.run("install --requires=app/0.1@ -o chat*:shared=True -o hello/*:shared=True -g VirtualRunEnv") + # This only finds "app" executable because the "app/0.1" is declaring package_type="application" + # otherwise, run=None and nothing can tell us if the conanrunenv should have the PATH. + command = environment_wrap_command("conanrun", client.current_folder, "app") + + client.run_command(command) + assert "main: Release!" in client.out + assert "chat: Release!" in client.out + assert "hello: Release!" in client.out + @pytest.mark.tool("cmake") def test_shared_cmake_toolchain_test_package():