Skip to content
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
19 changes: 17 additions & 2 deletions conan/tools/google/bazeldeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class _BazelDepBuildGenerator:
{% if lib_info['import_lib_path'] %}
interface_library = "{{ lib_info['import_lib_path'] }}",
{% endif %}
{% if lib_info["linkopts"] %}
linkopts = [
{% for linkopt in lib_info["linkopts"] %}
{{ linkopt }},
{% endfor %}
],
{% endif %}
)
{% endfor %}
{% endmacro %}
Expand Down Expand Up @@ -276,12 +283,20 @@ def package_info(self):
def _get_lib_info(self, cpp_info, deduced_cpp_info, component_name=None):

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

libs = cpp_info.libs
libs_info = []
Expand Down
46 changes: 46 additions & 0 deletions test/integration/toolchains/google/test_bazeldeps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import pathlib
import platform
import re
import textwrap

import pytest
Expand Down Expand Up @@ -1347,3 +1348,48 @@ def package_info(self):
)
""")
assert build_file_expected in build_content


def test_shared_libs_and_unix_includes_rpath():
"""
Testing the RPATH flag is added to the BUILD.bazel file if these conditions
are given: shared library and UNIX systems.

Issue: https://github.com/conan-io/conan/issues/19190
Issue: https://github.com/conan-io/conan/issues/19135
"""
client = TestClient()
csm = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import save
class Pkg(ConanFile):
name = "csm"
version = "1.0"
settings = "os"
options = {"shared": [True, False]}
default_options = {"shared": False}
def package(self):
if self.settings.os == "Windows":
save(self, os.path.join(self.package_folder, "bin", "csmapi.dll"), "")
save(self, os.path.join(self.package_folder, "lib", "csmapi.lib"), "")
else:
save(self, os.path.join(self.package_folder, "lib", "libcsmapi.so"), "")
def package_info(self):
self.cpp_info.libs = ["csmapi"]
""")
consumer = textwrap.dedent("""
[requires]
csm/1.0
[options]
*:shared=True
""")
client.save({"conanfile.txt": consumer, "csm/conanfile.py": csm})
client.run("export-pkg csm -o '*:shared=True' -s 'os=Windows'")
client.run("export-pkg csm -o '*:shared=True' -s 'os=Linux'")
client.run("install . -g BazelDeps -s 'os=Windows'")
build_content = load(None, os.path.join(client.current_folder, "csm", "BUILD.bazel"))
assert '"-Wl,-rpath,' not in build_content
client.run("install . -g BazelDeps -s 'os=Linux'")
build_content = load(None, os.path.join(client.current_folder, "csm", "BUILD.bazel"))
assert re.search('"-Wl,-rpath,.*/p/lib"', build_content.replace("\\", "/"))