Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[develop2] fixing transitive linux libraries cmake #13010

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions conan/tools/cmake/cmakedeps/templates/target_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ def template(self):
set_property(TARGET {{root_target_name}}
PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<$<CONFIG:{{configuration}}>:${{'{'}}{{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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused - I think this would only solve the problem in some cases, but not all.

If the INTERFACE_LINK_DIRECTORIES property eventually maps to cmake passing -L entries to the. linker, the linker documentation (https://linux.die.net/man/1/ld) for -L states that these only apply to libraries linked via -l. This is relevant since the linker will not search in -L directories for "indirect" transitive runtime dependencies that are also not expressed in the linker flags.

That's what -rpath or -rpath-link achieves that -L doesn't (-rpath works ok when not cross building, -rpath-link is needed when a sysroot is also passed).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command for creating app in my test is:

/usr/bin/cmake -E cmake_link_script CMakeFiles/app.dir/link.txt --verbose=1
/usr/bin/g++ -m64 -O3 -DNDEBUG -m64 CMakeFiles/app.dir/src/app.cpp.o -o app   -L"/tmp/tmpbiy1ou19conans/path with spaces/.conan2/p/31071b4017b74cd1/p/lib"  -L"/tmp/tmpbiy1ou19conans/path with spaces/.conan2/p/bbd4a1e27c1d25cc/p/lib" 

 -Wl,-rpath,"/tmp/tmpbiy1ou19conans/path with spaces/.conan2/p/31071b4017b74cd1/p/lib:/tmp/tmpbiy1ou19conans/path with spaces/.conan2/p/bbd4a1e27c1d25cc/p/lib" 

"/tmp/tmpbiy1ou19conans/path with spaces/.conan2/p/31071b4017b74cd1/p/lib/libchat.so"

I have isolated the -Wl, -rpath line, that contains 2 entries, for chat and hello.

$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_LIB_DIRS{{config_suffix}}}> APPEND)
set_property(TARGET {{root_target_name}}
PROPERTY INTERFACE_COMPILE_DEFINITIONS
$<$<CONFIG:{{configuration}}>:${{'{'}}{{pkg_name}}_COMPILE_DEFINITIONS{{config_suffix}}}> APPEND)
Expand Down Expand Up @@ -184,6 +188,8 @@ def template(self):
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'LINKER_FLAGS', config_suffix)}}> APPEND)
set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'INCLUDE_DIRS', config_suffix)}}> APPEND)
set_property(TARGET {{comp_target_name }} PROPERTY INTERFACE_LINK_DIRECTORIES
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'LIB_DIRS', config_suffix)}}> APPEND)
set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_COMPILE_DEFINITIONS
$<$<CONFIG:{{ configuration }}>:{{tvalue(pkg_name, comp_variable_name, 'COMPILE_DEFINITIONS', config_suffix)}}> APPEND)
set_property(TARGET {{ comp_target_name }} PROPERTY INTERFACE_COMPILE_OPTIONS
Expand Down
3 changes: 2 additions & 1 deletion conan/tools/cmake/cmakedeps/templates/target_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,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 = ""
Expand Down
84 changes: 84 additions & 0 deletions conans/test/functional/toolchains/cmake/test_shared_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down