Skip to content

Commit

Permalink
Refactor lang defs (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvukov authored Jan 5, 2025
1 parent cfa0f1f commit f28fa0c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
20 changes: 11 additions & 9 deletions ros2/ament.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -384,16 +384,18 @@ sh_launcher_rule = rule(
toolchains = [SH_TOOLCHAIN],
)

def sh_launcher(name, deps, idl_deps = None, **kwargs):
ament_setup = name + "_ament_setup"
def sh_launcher(name, ament_setup_deps = None, idl_deps = None, **kwargs):
testonly = kwargs.get("testonly", False)
ros2_ament_setup(
name = ament_setup,
deps = deps,
idl_deps = idl_deps,
tags = ["manual"],
testonly = testonly,
)
ament_setup = None
if ament_setup_deps != None:
ament_setup = name + "_ament_setup"
ros2_ament_setup(
name = ament_setup,
deps = ament_setup_deps,
idl_deps = idl_deps,
tags = ["manual"],
testonly = testonly,
)
sh_launcher_rule(
name = name,
ament_setup = ament_setup,
Expand Down
17 changes: 9 additions & 8 deletions ros2/cc_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,21 @@ def ros2_c_binary(name, ros2_package_name = None, **kwargs):
def _ros2_cpp_exec(target, name, ros2_package_name, set_up_ament, idl_deps, **kwargs):
if idl_deps != None and len(idl_deps) > 0:
set_up_ament = True
if set_up_ament == False:
is_test = target == cc_test
set_up_launcher = is_test or set_up_ament
if set_up_launcher == False:
_ros2_cc_target(target, "cpp", name, ros2_package_name, **kwargs)
return

launcher_target_kwargs, binary_kwargs = split_kwargs(**kwargs)
target_impl = name + "_impl"
_ros2_cc_target(cc_binary, "cpp", target_impl, ros2_package_name, tags = ["manual"], **binary_kwargs)

is_test = target == cc_test

launcher = "{}_launch".format(name)
ament_setup_deps = [target_impl] if set_up_ament else None
sh_launcher(
launcher,
deps = [target_impl],
ament_setup_deps = ament_setup_deps,
template = "@com_github_mvukov_rules_ros2//ros2:launch.sh.tpl",
substitutions = {
"{entry_point}": "$(rootpath {})".format(target_impl),
Expand Down Expand Up @@ -119,17 +120,17 @@ def ros2_cpp_test(name, ros2_package_name = None, set_up_ament = True, idl_deps
Adds common ROS 2 C++ definitions on top of a cc_test.
Defaults ROS_HOME and ROS_LOG_DIR to $TEST_UNDECLARED_OUTPUTS_DIR (if set,
otherwise to $TEST_TMPDIR, see https://bazel.build/reference/test-encyclopedia#initial-conditions)
Please make sure that --sandbox_default_allow_network=false is set in .bazelrc.
This ensures proper network isolation.
Args:
name: A unique target name.
ros2_package_name: If given, defines a ROS package name for the target.
Otherwise, the `name` is used as the package name.
set_up_ament: If true, generate a launcher for the target which:
* Sets AMENT_PREFIX_PATH to point to a generated ament file tree
* Defaults ROS_HOME and ROS_LOG_DIR to $TEST_UNDECLARED_OUTPUTS_DIR (if set,
otherwise to $TEST_TMPDIR, see https://bazel.build/reference/test-encyclopedia#initial-conditions)
set_up_ament: If true, sets up ament file tree for the test target.
idl_deps: Additional IDL deps that are used as runtime plugins.
**kwargs: https://bazel.build/reference/be/common-definitions#common-attributes-tests
"""
Expand Down
17 changes: 9 additions & 8 deletions ros2/py_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ load("@com_github_mvukov_rules_ros2//third_party:symlink.bzl", "symlink")
load("@rules_python//python:defs.bzl", "py_binary", "py_test")

def _ros2_py_exec(target, name, srcs, main, set_up_ament, **kwargs):
if set_up_ament == False:
is_test = target == py_test
set_up_launcher = is_test or set_up_ament
if set_up_launcher == False:
target(name = name, srcs = srcs, main = main, **kwargs)
return

launcher_target_kwargs, binary_kwargs = split_kwargs(**kwargs)
target_impl = name + "_impl"
target(name = target_impl, srcs = srcs, main = main, tags = ["manual"], **binary_kwargs)

is_test = target == py_test

target_impl_symlink = target_impl + "_symlink"
symlink(
name = target_impl_symlink,
Expand All @@ -25,9 +25,10 @@ def _ros2_py_exec(target, name, srcs, main, set_up_ament, **kwargs):
)

launcher = "{}_launch".format(name)
ament_setup_deps = [target_impl] if set_up_ament else None
sh_launcher(
launcher,
deps = [target_impl],
ament_setup_deps = ament_setup_deps,
template = "@com_github_mvukov_rules_ros2//ros2:launch.sh.tpl",
substitutions = {
"{entry_point}": "$(rootpath {})".format(target_impl_symlink),
Expand Down Expand Up @@ -60,17 +61,17 @@ def ros2_py_binary(name, srcs, main, set_up_ament = False, **kwargs):
def ros2_py_test(name, srcs, main, set_up_ament = True, **kwargs):
""" Defines a ROS 2 Python test.
Defaults ROS_HOME and ROS_LOG_DIR to $TEST_UNDECLARED_OUTPUTS_DIR (if set,
otherwise to $TEST_TMPDIR, see https://bazel.build/reference/test-encyclopedia#initial-conditions)
Please make sure that --sandbox_default_allow_network=false is set in .bazelrc.
This ensures proper network isolation.
Args:
name: A unique target name.
srcs: List of source files.
main: Source file to use as entrypoint.
set_up_ament: If true, generate a launcher for the target which:
* Sets AMENT_PREFIX_PATH to point to a generated ament file tree
* Defaults ROS_HOME and ROS_LOG_DIR to $TEST_UNDECLARED_OUTPUTS_DIR (if set,
otherwise to $TEST_TMPDIR, see https://bazel.build/reference/test-encyclopedia#initial-conditions)
set_up_ament: If true, sets up ament file tree for the test target.
**kwargs: https://bazel.build/reference/be/common-definitions#common-attributes-tests
"""
_ros2_py_exec(py_test, name, srcs, main, set_up_ament, **kwargs)
8 changes: 2 additions & 6 deletions ros2/rust_defs.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
""" Defines commonly used Rust macros.
"""

load(
"@com_github_mvukov_rules_ros2//ros2:ament.bzl",
"sh_launcher_rule",
"split_kwargs",
)
load("@com_github_mvukov_rules_ros2//ros2:ament.bzl", "sh_launcher", "split_kwargs")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test")

def ros2_rust_test(name, **kwargs):
Expand All @@ -31,7 +27,7 @@ def ros2_rust_test(name, **kwargs):
)

launcher = "{}_launch".format(name)
sh_launcher_rule(
sh_launcher(
name = launcher,
template = "@com_github_mvukov_rules_ros2//ros2:launch.sh.tpl",
substitutions = {
Expand Down

0 comments on commit f28fa0c

Please sign in to comment.