diff --git a/ros2/ament.bzl b/ros2/ament.bzl index 98b2a35b..c6403bdd 100644 --- a/ros2/ament.bzl +++ b/ros2/ament.bzl @@ -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, diff --git a/ros2/cc_defs.bzl b/ros2/cc_defs.bzl index d05f2a51..35d51c63 100644 --- a/ros2/cc_defs.bzl +++ b/ros2/cc_defs.bzl @@ -67,7 +67,9 @@ 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 @@ -75,12 +77,11 @@ def _ros2_cpp_exec(target, name, ros2_package_name, set_up_ament, idl_deps, **kw 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), @@ -119,6 +120,9 @@ 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. @@ -126,10 +130,7 @@ def ros2_cpp_test(name, ros2_package_name = None, set_up_ament = True, idl_deps 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 """ diff --git a/ros2/py_defs.bzl b/ros2/py_defs.bzl index 6b2c24f1..f6fc97c7 100644 --- a/ros2/py_defs.bzl +++ b/ros2/py_defs.bzl @@ -6,7 +6,9 @@ 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 @@ -14,8 +16,6 @@ def _ros2_py_exec(target, name, srcs, main, set_up_ament, **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, @@ -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), @@ -60,6 +61,9 @@ 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. @@ -67,10 +71,7 @@ def ros2_py_test(name, srcs, main, set_up_ament = True, **kwargs): 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) diff --git a/ros2/rust_defs.bzl b/ros2/rust_defs.bzl index 025f1c4a..31ad5d51 100644 --- a/ros2/rust_defs.bzl +++ b/ros2/rust_defs.bzl @@ -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): @@ -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 = {