Skip to content
Open
7 changes: 7 additions & 0 deletions bazel_ros2_rules/lib/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ py_test(
srcs = ["test/plugin_xml_parsing_test.py"],
deps = [":ros2bzl_py"],
)

py_test(
name = "configure_package_rmw_implementation_override_test",
srcs = ["test/configure_package_rmw_implementation_override_test.py"],
data = ["templates/package_rmw_implementation_override.bazel.tpl"],
deps = [":ros2bzl_py"],
)
40 changes: 34 additions & 6 deletions bazel_ros2_rules/lib/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,39 @@ generate_file = rule(
)
"""Writes a string to a file at build time."""

def incorporate_rmw_implementation(kwargs, env_changes, rmw_implementation):
target = REPOSITORY_ROOT + ":%s_cc" % rmw_implementation
kwargs["data"] = kwargs.get("data", []) + [target]
def incorporate_rmw_implementation(
kwargs,
env_changes,
rmw_implementation,
rmw_implementation_explicit = False):
"""
Args:
rmw_implementation_explicit: if True, links the backend in at build
time in place of rmw_implementation's dispatch library, instead
of the default runtime dlopen() via RMW_IMPLEMENTATION. Only
works for backends shipping their own librmw_<name>.so; see
configure_package_rmw_implementation_override.
"""
env_changes = dict(env_changes)
env_changes.update({
"RMW_IMPLEMENTATION": ["replace", rmw_implementation],
})
if rmw_implementation_explicit:
# Also depend on the backend's own _cc library, for its transitive deps.
backend = REPOSITORY_ROOT + ":%s_cc" % rmw_implementation
override = REPOSITORY_ROOT + ":%s_as_rmw_implementation" % rmw_implementation
kwargs["data"] = kwargs.get("data", []) + [backend, override]

# ${LOAD_PATH} entries must be directories, not the .so itself,
# rooted like every other RUNTIME_ENVIRONMENT entry (see distro.bzl).
canonical_repo_name = REPOSITORY_ROOT[2:-2]
override_runfiles_dir = "{}/{}".format(
canonical_repo_name, rmw_implementation)
env_changes["${LOAD_PATH}"] = (
["path-prepend", override_runfiles_dir] +
env_changes["${LOAD_PATH}"][1:]
)
else:
target = REPOSITORY_ROOT + ":%s_cc" % rmw_implementation
kwargs["data"] = kwargs.get("data", []) + [target]
env_changes.update({
"RMW_IMPLEMENTATION": ["replace", rmw_implementation],
})
return kwargs, env_changes
1 change: 1 addition & 0 deletions bazel_ros2_rules/lib/private/repos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ COMMON_FILES_MANIFEST = [
"templates/package_meta_py_library.bazel.tpl",
"templates/package_py_library_with_cc_libs.bazel.tpl",
"templates/package_py_library.bazel.tpl",
"templates/package_rmw_implementation_override.bazel.tpl",
"templates/package_share_filegroup.bazel.tpl",
"templates/prologue.bazel",
]
Expand Down
29 changes: 29 additions & 0 deletions bazel_ros2_rules/lib/private/ros2bzl/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,35 @@ def configure_package_cc_library(
)


def configure_package_rmw_implementation_override(name, properties, sandbox):
"""
Emits a genrule copying librmw_<name>.so to librmw_implementation.so, so
it can be linked in directly instead of dlopen()'d. Returns
(target_name, None, None) if this package has no librmw_<name>.so.
"""
target_name = "%s_as_rmw_implementation" % name
libraries = [sandbox(library) for library in properties.link_libraries]
primary_library_basename = "lib%s.so" % name
primary_libraries = [
library for library in libraries
if library.split("/")[-1] == primary_library_basename
]
if not primary_libraries:
return (target_name, None, None)

template_path = "templates/package_rmw_implementation_override.bazel.tpl"
config = {
"name": target_name,
"srcs": [primary_libraries[0]],
"outs": ["%s/librmw_implementation.so" % name],
}
return (
target_name,
load_resource(template_path),
to_starlark_string_dict(config),
)


def configure_package_meta_py_library(name, metadata, dependencies):
deps = []
for dependency_name, dependency_metadata in dependencies.items():
Expand Down
5 changes: 5 additions & 0 deletions bazel_ros2_rules/lib/private/ros_cc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ load(
def ros_cc_binary(
name,
rmw_implementation = None,
rmw_implementation_explicit = False,
cc_binary_rule = native.cc_binary,
cc_library_rule = native.cc_library,
shim = "reexec",
Expand All @@ -37,6 +38,9 @@ def ros_cc_binary(
Args:
name: C/C++ binary target name
rmw_implementation: optional RMW implementation to run against
rmw_implementation_explicit: if True, links the backend in at build
time instead of via runtime dlopen(). See
incorporate_rmw_implementation().
cc_binary_rule: optional cc_binary() rule override
cc_library_rule: optional cc_library() rule override
shim: optional tactic to use for shimming ("reexec" or "ldwrap")
Expand Down Expand Up @@ -72,6 +76,7 @@ def ros_cc_binary(
kwargs,
env_changes,
rmw_implementation = rmw_implementation,
rmw_implementation_explicit = rmw_implementation_explicit,
)

# Declare an unshimmed, manual binary. For the "reexec" case, this will be
Expand Down
8 changes: 8 additions & 0 deletions bazel_ros2_rules/lib/private/scripts/generate_build_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
configure_package_interfaces_filegroup,
configure_package_meta_py_library,
configure_package_py_library,
configure_package_rmw_implementation_override,
configure_package_share_filegroup,
configure_prologue,
)
Expand Down Expand Up @@ -145,6 +146,13 @@ def write_build_file(fd, repo_name, distro, sandbox, cache):

fd.write(interpolate(template, config) + "\n")

if name in rmw_implementation_packages:
_, template, config = configure_package_rmw_implementation_override(
name, properties, sandbox
)
if template is not None:
fd.write(interpolate(template, config) + "\n")

if "rosidl_interface_packages" in metadata.get("groups", []):
# Alias C++ library as C library for interface packages
# as their headers and artifacts cannot be discriminated.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
genrule(
name = @name@,
srcs = @srcs@,
outs = @outs@,
cmd = "cp $< $@",
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import unittest

from ros2bzl.scraping.properties import CcProperties
from ros2bzl.templates import configure_package_rmw_implementation_override
from ros2bzl.utilities import interpolate


def identity_sandbox(library):
return library


class ConfigurePackageRmwImplementationOverrideTest(unittest.TestCase):
def test_package_with_matching_library(self):
properties = CcProperties(
link_libraries=[
"/opt/ros/jazzy/lib/librmw_cyclonedds_cpp.so",
"/opt/ros/jazzy/lib/libddsc.so.0",
]
)
target_name, template, config = (
configure_package_rmw_implementation_override(
"rmw_cyclonedds_cpp", properties, identity_sandbox
)
)
self.assertEqual(target_name, "rmw_cyclonedds_cpp_as_rmw_implementation")
self.assertIsNotNone(template)
content = interpolate(template, config)
self.assertIn(
'name = "rmw_cyclonedds_cpp_as_rmw_implementation"', content
)
self.assertIn(
'srcs = ["/opt/ros/jazzy/lib/librmw_cyclonedds_cpp.so"]', content
)
self.assertIn(
'outs = ["rmw_cyclonedds_cpp/librmw_implementation.so"]', content
)

def test_package_without_matching_library(self):
properties = CcProperties(
link_libraries=["/opt/ros/jazzy/lib/libsome_other_thing.so"]
)
target_name, template, config = (
configure_package_rmw_implementation_override(
"rmw_cyclonedds_cpp", properties, identity_sandbox
)
)
self.assertEqual(target_name, "rmw_cyclonedds_cpp_as_rmw_implementation")
self.assertIsNone(template)
self.assertIsNone(config)


if __name__ == "__main__":
unittest.main()