Skip to content

Commit cc6dae1

Browse files
[BazelDeps] Adding RPATH to linkopts (#19193)
* Adding rpath when shared and UNIX * wip * Added test * win * Update conan/tools/google/bazeldeps.py
1 parent fee7bbb commit cc6dae1

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

conan/tools/google/bazeldeps.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ class _BazelDepBuildGenerator:
5353
{% if lib_info['import_lib_path'] %}
5454
interface_library = "{{ lib_info['import_lib_path'] }}",
5555
{% endif %}
56+
{% if lib_info["linkopts"] %}
57+
linkopts = [
58+
{% for linkopt in lib_info["linkopts"] %}
59+
{{ linkopt }},
60+
{% endfor %}
61+
],
62+
{% endif %}
5663
)
5764
{% endfor %}
5865
{% endmacro %}
@@ -276,12 +283,20 @@ def package_info(self):
276283
def _get_lib_info(self, cpp_info, deduced_cpp_info, component_name=None):
277284

278285
def _lib_info(lib_name, virtual_cpp_info):
279-
return {
286+
info = {
280287
"name": lib_name,
281288
"is_shared": virtual_cpp_info.type == PackageType.SHARED,
282289
"lib_path": _relativize_path(virtual_cpp_info.location, self._package_folder),
283-
"import_lib_path": _relativize_path(virtual_cpp_info.link_location, self._package_folder)
290+
"import_lib_path": _relativize_path(virtual_cpp_info.link_location, self._package_folder),
291+
"linkopts": []
284292
}
293+
if info['is_shared'] and info["lib_path"] and not info["lib_path"].endswith(".dll"):
294+
# Issue: https://github.com/conan-io/conan/issues/19190
295+
# Issue: https://github.com/conan-io/conan/issues/19135
296+
# (UNIX) Adding the rpath flag as any application could link through the library
297+
# which points out a symlink, but that name does not appear in the library location
298+
info["linkopts"] = [f'"-Wl,-rpath,{libdir}"' for libdir in cpp_info.libdirs]
299+
return info
285300

286301
libs = cpp_info.libs
287302
libs_info = []

test/integration/toolchains/google/test_bazeldeps.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import pathlib
33
import platform
4+
import re
45
import textwrap
56

67
import pytest
@@ -1347,3 +1348,48 @@ def package_info(self):
13471348
)
13481349
""")
13491350
assert build_file_expected in build_content
1351+
1352+
1353+
def test_shared_libs_and_unix_includes_rpath():
1354+
"""
1355+
Testing the RPATH flag is added to the BUILD.bazel file if these conditions
1356+
are given: shared library and UNIX systems.
1357+
1358+
Issue: https://github.com/conan-io/conan/issues/19190
1359+
Issue: https://github.com/conan-io/conan/issues/19135
1360+
"""
1361+
client = TestClient()
1362+
csm = textwrap.dedent("""
1363+
import os
1364+
from conan import ConanFile
1365+
from conan.tools.files import save
1366+
class Pkg(ConanFile):
1367+
name = "csm"
1368+
version = "1.0"
1369+
settings = "os"
1370+
options = {"shared": [True, False]}
1371+
default_options = {"shared": False}
1372+
def package(self):
1373+
if self.settings.os == "Windows":
1374+
save(self, os.path.join(self.package_folder, "bin", "csmapi.dll"), "")
1375+
save(self, os.path.join(self.package_folder, "lib", "csmapi.lib"), "")
1376+
else:
1377+
save(self, os.path.join(self.package_folder, "lib", "libcsmapi.so"), "")
1378+
def package_info(self):
1379+
self.cpp_info.libs = ["csmapi"]
1380+
""")
1381+
consumer = textwrap.dedent("""
1382+
[requires]
1383+
csm/1.0
1384+
[options]
1385+
*:shared=True
1386+
""")
1387+
client.save({"conanfile.txt": consumer, "csm/conanfile.py": csm})
1388+
client.run("export-pkg csm -o '*:shared=True' -s 'os=Windows'")
1389+
client.run("export-pkg csm -o '*:shared=True' -s 'os=Linux'")
1390+
client.run("install . -g BazelDeps -s 'os=Windows'")
1391+
build_content = load(None, os.path.join(client.current_folder, "csm", "BUILD.bazel"))
1392+
assert '"-Wl,-rpath,' not in build_content
1393+
client.run("install . -g BazelDeps -s 'os=Linux'")
1394+
build_content = load(None, os.path.join(client.current_folder, "csm", "BUILD.bazel"))
1395+
assert re.search('"-Wl,-rpath,.*/p/lib"', build_content.replace("\\", "/"))

0 commit comments

Comments
 (0)