Skip to content

Commit

Permalink
Move setup to launch wrapper, add rclpy test
Browse files Browse the repository at this point in the history
  • Loading branch information
lalten committed Apr 22, 2023
1 parent a004cc2 commit 54908cb
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 20 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
examples
bazel-rules_ros2
20 changes: 12 additions & 8 deletions ros2/cc_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ def ros2_c_binary(name, ros2_package_name = None, **kwargs):
"""
_ros2_cc_target(cc_binary, "c", name, ros2_package_name, **kwargs)

def _ros2_cpp_exec(target, name, ros2_package_name = None, set_up_ament = False, **kwargs):
if set_up_ament == False:
def _ros2_cpp_exec(target, name, ros2_package_name = None, set_up_ament = False, set_up_ros_home = False, **kwargs):
if not (set_up_ament or set_up_ros_home):
_ros2_cc_target(target, "cpp", name, ros2_package_name, **kwargs)
return

target_impl = name + "_impl"
tags = kwargs.pop("tags", [])
visibility = kwargs.pop("visibility", None)
size = kwargs.pop("size", None)
_ros2_cc_target(cc_binary, "cpp", target_impl, ros2_package_name, tags = ["manual"], **kwargs)

is_test = target == cc_test
Expand All @@ -82,7 +83,9 @@ def _ros2_cpp_exec(target, name, ros2_package_name = None, set_up_ament = False,
deps = [target_impl],
template = "@com_github_mvukov_rules_ros2//ros2:launch.sh.tpl",
substitutions = {
"{entry_point}": "$(rootpath {})".format(target_impl),
"{{set_up_ros_home}}": "set_up_ros_home" if set_up_ros_home else "",
"{{set_up_ament}}": "set_up_ament" if set_up_ament else "",
"{{entry_point}}": "$(rootpath {})".format(target_impl),
},
tags = ["manual"],
data = [target_impl],
Expand All @@ -92,6 +95,7 @@ def _ros2_cpp_exec(target, name, ros2_package_name = None, set_up_ament = False,
sh_target = native.sh_test if is_test else native.sh_binary
sh_target(
name = name,
size = size,
srcs = [launcher],
data = [target_impl],
tags = tags,
Expand All @@ -110,9 +114,11 @@ def ros2_cpp_binary(name, ros2_package_name = None, set_up_ament = False, **kwar
set_up_ament: If true, sets up ament file tree for the binary target.
**kwargs: https://bazel.build/reference/be/common-definitions#common-attributes-binaries
"""
if "set_up_ros_home" in kwargs:
fail("set_up_ros_home only makes sense for test targets.")
_ros2_cpp_exec(cc_binary, name, ros2_package_name, set_up_ament, **kwargs)

def ros2_cpp_test(name, ros2_package_name = None, set_up_ament = False, **kwargs):
def ros2_cpp_test(name, ros2_package_name = None, set_up_ament = False, set_up_ros_home = True, **kwargs):
""" Defines a ROS 2 C++ test.
Adds common ROS 2 C++ definitions on top of a cc_test.
Expand All @@ -122,9 +128,7 @@ def ros2_cpp_test(name, ros2_package_name = None, set_up_ament = False, **kwargs
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, sets up ament file tree for the test target.
set_up_ros_home: If true, sets up ROS_HOME for the test target.
**kwargs: https://bazel.build/reference/be/common-definitions#common-attributes-tests
"""
env = kwargs.pop("env", {})
env["ROS_HOME"] = env.get("ROS_HOME", "$TEST_UNDECLARED_OUTPUTS_DIR")
env["ROS_LOG_DIR"] = env.get("ROS_LOG_DIR", "$TEST_UNDECLARED_OUTPUTS_DIR")
_ros2_cpp_exec(cc_test, name, ros2_package_name, set_up_ament, env = env, **kwargs)
_ros2_cpp_exec(cc_test, name, ros2_package_name, set_up_ament, set_up_ros_home, **kwargs)
21 changes: 16 additions & 5 deletions ros2/launch.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@

set -o errexit -o nounset -o pipefail

ament_prefix_path="{{ament_prefix_path}}"
if [ -z "${ament_prefix_path}" ]; then
if [ -n "{{set_up_ros_home}}" ]; then
if [ -z "${ROS_HOME:-}" ] && [ -z "${ROS_LOG_DIR:-}" ]; then
ros_output_dir="${TEST_UNDECLARED_OUTPUTS_DIR:-${TEST_TMPDIR:-}}"
if [ -n "${ros_output_dir}" ]; then
export ROS_HOME="${ros_output_dir}"
export ROS_LOG_DIR="${ros_output_dir}"
fi
fi
fi

if [ -n "{{set_up_ament}}" ]; then
unset AMENT_PREFIX_PATH
{entry_point} "$@"
else
AMENT_PREFIX_PATH="${ament_prefix_path}" {entry_point} "$@"
if [ -n "{{ament_prefix_path}}" ]; then
export AMENT_PREFIX_PATH="{{ament_prefix_path}}"
fi
fi

"{{entry_point}}" "$@"
17 changes: 12 additions & 5 deletions ros2/py_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ load("@com_github_mvukov_rules_ros2//ros2:ament.bzl", "sh_launcher")
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:
def _ros2_py_exec(target, name, srcs, main, set_up_ament = False, set_up_ros_home = False, **kwargs):
if not (set_up_ament or set_up_ros_home):
target(name = name, srcs = srcs, main = main, **kwargs)
return

target_impl = name + "_impl"
tags = kwargs.pop("tags", [])
visibility = kwargs.pop("visibility", None)
size = kwargs.pop("size", None)
target(name = target_impl, srcs = srcs, main = main, tags = ["manual"], **kwargs)

is_test = target == py_test
Expand All @@ -31,7 +32,9 @@ def _ros2_py_exec(target, name, srcs, main, set_up_ament, **kwargs):
deps = [target_impl],
template = "@com_github_mvukov_rules_ros2//ros2:launch.sh.tpl",
substitutions = {
"{entry_point}": "$(rootpath {})".format(target_impl_symlink),
"{{set_up_ros_home}}": "set_up_ros_home" if set_up_ros_home else "",
"{{set_up_ament}}": "set_up_ament" if set_up_ament else "",
"{{entry_point}}": "$(rootpath {})".format(target_impl_symlink),
},
tags = ["manual"],
data = [target_impl_symlink],
Expand All @@ -41,6 +44,7 @@ def _ros2_py_exec(target, name, srcs, main, set_up_ament, **kwargs):
sh_target = native.sh_test if is_test else native.sh_binary
sh_target(
name = name,
size = size,
srcs = [launcher],
data = [target_impl_symlink],
tags = tags,
Expand All @@ -57,16 +61,19 @@ def ros2_py_binary(name, srcs, main, set_up_ament = False, **kwargs):
set_up_ament: If true, sets up ament file tree for the binary target.
**kwargs: https://bazel.build/reference/be/common-definitions#common-attributes-binaries
"""
if "set_up_ros_home" in kwargs:
fail("set_up_ros_home only makes sense for test targets.")
_ros2_py_exec(py_binary, name, srcs, main, set_up_ament, **kwargs)

def ros2_py_test(name, srcs, main, set_up_ament = False, **kwargs):
def ros2_py_test(name, srcs, main, set_up_ament = False, set_up_ros_home = True, **kwargs):
""" Defines a ROS 2 Python test.
Args:
name: A unique target name.
srcs: List of source files.
main: Source file to use as entrypoint.
set_up_ament: If true, sets up ament file tree for the test target.
set_up_ros_home: If true, sets up ROS_HOME 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)
_ros2_py_exec(py_test, name, srcs, main, set_up_ament, set_up_ros_home, **kwargs)
2 changes: 1 addition & 1 deletion ros2/pytest_wrapper.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def finalize_coverage_session(coverage_session: coverage.Coverage) -> None:


def main() -> None:
test_outputs_dir = os.environ.get('TEST_UNDECLARED_OUTPUTS_DIR')
test_outputs_dir = os.environ.get('TEST_UNDECLARED_OUTPUTS_DIR') or os.environ.get('TEST_TMPDIR')
if test_outputs_dir:
os.environ['ROS_HOME'] = test_outputs_dir
os.environ['ROS_LOG_DIR'] = test_outputs_dir
Expand Down
2 changes: 1 addition & 1 deletion ros2/test.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sys.argv = sys.argv[:1] + [
LAUNCH_FILE,
] + sys.argv[1:]

test_outputs_dir = os.environ.get('TEST_UNDECLARED_OUTPUTS_DIR')
test_outputs_dir = os.environ.get('TEST_UNDECLARED_OUTPUTS_DIR') or os.environ.get('TEST_TMPDIR')
if test_outputs_dir:
os.environ['ROS_HOME'] = test_outputs_dir
os.environ['ROS_LOG_DIR'] = test_outputs_dir
Expand Down
9 changes: 9 additions & 0 deletions ros2/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

load("@com_github_mvukov_rules_ros2//ros2:cc_defs.bzl", "ros2_cpp_test")
load("@com_github_mvukov_rules_ros2//ros2:interfaces.bzl", "py_ros2_interface_library", "ros2_interface_library")
load("@com_github_mvukov_rules_ros2//ros2:py_defs.bzl", "ros2_py_test")
load("@rules_python//python:defs.bzl", "py_test")

py_test(
Expand Down Expand Up @@ -49,3 +50,11 @@ ros2_cpp_test(
srcs = ["test_rclcpp.cpp"],
deps = ["@ros2_rclcpp//:rclcpp"],
)

ros2_py_test(
name = "test_rclpy",
size = "small",
srcs = ["test_rclpy.py"],
main = "test_rclpy.py",
deps = ["@ros2_rclpy//:rclpy"],
)
3 changes: 3 additions & 0 deletions ros2/test/test_rclpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import rclpy

rclpy.init()

0 comments on commit 54908cb

Please sign in to comment.