Skip to content

Commit

Permalink
Feature/examples repo (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: Finn <[email protected]>
  • Loading branch information
mvukov and finn-ball committed Aug 21, 2024
1 parent 9b00aef commit 43c7889
Show file tree
Hide file tree
Showing 37 changed files with 327 additions and 170 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ build --incompatible_default_to_explicit_init_py
# This is a mandatory flag.
build --cxxopt=-std=c++17

# This is a mandatory flag for user repos.
build --@rules_python//python/config_settings:python_version=3.10

# Ensure that you don't accidentally make non-hermetic actions/tests
# which depend on remote services. Tag an individual target with
# tags=["requires-network"] to opt-out of the enforcement.
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ jobs:
image: mvukov/bazel-builder:commit-49178bdf9c151b1fdb1fb0bac580da5d53a990fd
options: --user ${{ needs.configure.outputs.uid_gid }}

# Run bazel test with gcc and clang in each workspace
strategy:
matrix:
folder:
- "."
- "examples"

steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v4
Expand All @@ -74,12 +81,13 @@ jobs:
path: |
~/.cache/bazel
~/.cache/bazel-repo
key: v1-bazel-repo-cache-${{ hashFiles('.bazelversion', 'MODULE.bazel', 'repositories/**', 'requirements_lock.txt') }}
key: v4-bazel-repo-cache-${{ hashFiles('.bazelversion', 'examples/MODULE.bazel', 'repositories/**', 'requirements_lock.txt', 'MODULE.bazel') }}
restore-keys: v4-bazel-repo-cache
- name: bazel test //...
env:
# Bazelisk will download bazel to here, ensure it is cached between runs.
XDG_CACHE_HOME: /github/home/.cache/bazel-repo
BUILDBUDDY_ORG_API_KEY: ${{ secrets.BUILDBUDDY_ORG_API_KEY }}
USER: ${{ needs.configure.outputs.user_name }}
working-directory: ${{ matrix.folder }}
run: ${GITHUB_WORKSPACE}/.github/workflows/test.sh
30 changes: 0 additions & 30 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,3 @@ exports_files([
"requirements.txt",
"requirements_lock.txt",
])

alias(
name = "roscore",
actual = "//third_party/ros/roslaunch:roscore",
)

alias(
name = "rosgraph",
actual = "@ros_comm//:rosgraph",
)

alias(
name = "rosparam",
actual = "@ros_comm//:rosparam",
)

alias(
name = "rosbag_record",
actual = "@ros_comm//:rosbag_record",
)

alias(
name = "rosbag_play",
actual = "@ros_comm//:rosbag_play",
)

alias(
name = "rosservice",
actual = "//third_party/ros/rosservice",
)
10 changes: 3 additions & 7 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
module(
name = "rules_ros",
repo_name = "com_github_mvukov_rules_ros",
)
module(name = "rules_ros")

bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "bzip2", version = "1.0.8.bcr.1")
Expand All @@ -13,8 +10,7 @@ bazel_dep(name = "lz4", version = "1.9.4")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_foreign_cc", version = "0.11.1")

bazel_dep(name = "rules_python", version = "0.34.0", dev_dependency = True)
bazel_dep(name = "rules_python", version = "0.34.0")

non_module_ros_repositories = use_extension("//ros:extensions.bzl", "non_module_dependencies")
use_repo(
Expand Down Expand Up @@ -64,6 +60,6 @@ pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "rules_ros_pip_deps",
python_version = "3.10",
requirements_lock = "@com_github_mvukov_rules_ros//:requirements_lock.txt",
requirements_lock = "@rules_ros//:requirements_lock.txt",
)
use_repo(pip, "rules_ros_pip_deps")
53 changes: 1 addition & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,7 @@ So far a subset of ros-base packages can be built, including support for
- actions, and
- dynamic reconfiguration.

Here is an example.

Let's begin with starting `roscore`:

```sh
bazel run //:roscore
```

In a separate terminal, let's start a (C++) talker node:

```sh
bazel run //examples/chatter:talker
```

This single command will compile and run the talker node.

In a yet another terminal we can start a listener node:

```sh
bazel run //examples/chatter:listener # C++ version or
bazel run //examples/chatter:py_listener # Python version
```

Rosbag recording & playing works as well:

```sh
bazel run //:rosbag_record -- /chatter -o /tmp/foo.bag # to record a bag or
bazel run //:rosbag_play -- /tmp/foo_<timestamp>.bag # to play a bag
```

`rostopic`, tied to this example (see `examples/chatter/BUILD.bazel` for more
info) can be used as

```sh
bazel run //examples/chatter:rostopic -- echo /chatter
```

Not too shabby.

Next, let's start a deployment with the talker and the listener nodes. You can
stop the nodes you started with the above commands. Now execute

```sh
bazel run //examples/chatter:chatter
```

This command will build the necessary nodes and launch them. This is similar
to executing good-ol' `roslaunch`, but, running the chatter `ros_launch` target
using Bazel ensures all necessary dependencies are (re-)built.

In `//examples/dishwasher` you can find another example that demonstrates
defining and usage of ROS actions (and actionlib).
Please take a look at the [examples](examples) folder to get started.

## Background and design decisions

Expand Down
1 change: 1 addition & 0 deletions examples/.bazelrc
31 changes: 31 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# The following aliases are defined for convenience.

alias(
name = "roscore",
actual = "@rules_ros//third_party/ros/roslaunch:roscore",
)

alias(
name = "rosgraph",
actual = "@ros_comm//:rosgraph",
)

alias(
name = "rosparam",
actual = "@ros_comm//:rosparam",
)

alias(
name = "rosbag_record",
actual = "@ros_comm//:rosbag_record",
)

alias(
name = "rosbag_play",
actual = "@ros_comm//:rosbag_play",
)

alias(
name = "rosservice",
actual = "@rules_ros//third_party/ros/rosservice",
)
57 changes: 57 additions & 0 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module(name = "rules_ros_examples")

bazel_dep(name = "rules_python", version = "0.34.0")

# This import is relevant for these examples and this (rules_ros) repository.
bazel_dep(name = "rules_ros")
local_path_override(
module_name = "rules_ros",
path = "..",
)

# In a normal workflow, you would typically import rules_ros into your
# (mono)repo as follows:
# bazel_dep(name = "rules_ros")
# archive_override(
# module_name = "rules_ros",
# integrity = "sha256-<integrity sum of the .tar.gz archive below>",
# strip_prefix = "rules_ros-<git commit SHA>",
# urls = "https://github.com/mvukov/rules_ros/archive/<git commit SHA>.tar.gz"
# )

non_module_ros_repositories = use_extension("@rules_ros//ros:extensions.bzl", "non_module_dependencies")
use_repo(
non_module_ros_repositories,
"console_bridge",
"orocos_kdl",
"ros_actionlib",
"ros_comm",
"ros_comm_msgs",
"ros_common_msgs",
"ros_dynamic_reconfigure",
"ros_gencpp",
"ros_genmsg",
"ros_genpy",
"ros_geometry2",
"ros_ros",
"ros_std_msgs",
"rosconsole",
"roscpp_core",
"tinyxml",
"urdfdom",
"urdfdom_headers",
)

bazel_dep(name = "rules_boost")
archive_override(
module_name = "rules_boost",
integrity = "sha256-I1iTdF3qckTmDRab+0yjA37Iya9AOGvEGJVoPtfpADM=",
strip_prefix = "rules_boost-42d8155d8f20a1aee8ee20b7903a495bdfb9befd",
urls = "https://github.com/nelhage/rules_boost/archive/42d8155d8f20a1aee8ee20b7903a495bdfb9befd.zip",
)

non_module_boost_repositories = use_extension("@rules_boost//:boost/repositories.bzl", "non_module_dependencies")
use_repo(
non_module_boost_repositories,
"boost",
)
19 changes: 19 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Examples for rules_ros

This folder provides some examples and guidelines on how you can use this repo
in your very own (mono)repo. It's important to note here that this folder is
a yet another Bazel module (it has a MODULE.bazel file). Please take a look at
the module file to get an idea how to set up yourself.

Besides setting up your module file, please make sure you copy the `.bazelrc`
file as well to your monorepo -- or at least adjust your own using the provided
one.

Before you build/run/test any of the targets in this folder, please make sure
your terminal is in this folder, or in a subfolder, and not in the root
of the repo.

You can start with a simple [chatter](chatter) example.

In `dishwasher` folder you can find another example that demonstrates
defining and usage of ROS actions (and actionlib).
31 changes: 24 additions & 7 deletions examples/chatter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
"""

load("@rules_python//python:defs.bzl", "py_binary")
load("//ros:cc_defs.bzl", "cc_ros_binary")
load("@rules_ros//ros:cc_defs.bzl", "cc_ros_binary")
load(
"//ros:interfaces.bzl",
"@rules_ros//ros:interfaces.bzl",
"cc_ros_interface_library",
"py_ros_interface_library",
"ros_interface_library",
)
load("//ros:launch.bzl", "ros_launch")
load("//ros:test.bzl", "ros_test")
load("//ros:topic.bzl", "ros_topic")
load("@rules_ros//ros:launch.bzl", "ros_launch")
load("@rules_ros//ros:test.bzl", "ros_test")
load("@rules_ros//ros:topic.bzl", "ros_topic")
load("@rules_ros//third_party:expand_template.bzl", "expand_template")

# Handling of ROS messages & services resembles to some extent Bazel's rules for
# handling protobuf messages (e.g. proto_library and cc_proto_library).
Expand Down Expand Up @@ -61,11 +62,27 @@ ros_test(
launch_file = "talker_tests.launch",
nodes = [
":talker",
"//third_party/ros/rostest:advertisetest",
"//third_party/ros/rostest:publishtest",
"@rules_ros//third_party/ros/rostest:advertisetest",
"@rules_ros//third_party/ros/rostest:publishtest",
],
)

expand_template(
name = "talker_tests_launch",
out = "talker_tests.launch",
data = [
":talker",
"@rules_ros//third_party/ros/rostest:advertisetest",
"@rules_ros//third_party/ros/rostest:publishtest",
],
substitutions = {
"{talker}": "$(rootpath :talker)",
"{advertisetest}": "$(rootpath @rules_ros//third_party/ros/rostest:advertisetest)",
"{publishtest}": "$(rootpath @rules_ros//third_party/ros/rostest:publishtest)",
},
template = "talker_tests.launch.tpl",
)

# Defines a C++ listener ROS node.
cc_ros_binary(
name = "listener",
Expand Down
48 changes: 48 additions & 0 deletions examples/chatter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# A chatter example

Let's begin with starting `roscore`:

```sh
bazel run //:roscore
```

In a separate terminal, let's start a (C++) talker node:

```sh
bazel run //chatter:talker
```

This single command will compile and run the talker node.

In a yet another terminal we can start a listener node:

```sh
bazel run //chatter:listener # C++ version or
bazel run //chatter:py_listener # Python version
```

Rosbag recording & playing works as well:

```sh
bazel run //:rosbag_record -- /chatter -o /tmp/foo.bag # to record a bag or
bazel run //:rosbag_play -- /tmp/foo_<timestamp>.bag # to play a bag
```

`rostopic`, tied to this example (see `BUILD.bazel` for more info) can be used as

```sh
bazel run //chatter:rostopic -- echo /chatter
```

Not too shabby.

Next, let's start a deployment with the talker and the listener nodes. You can
stop the nodes you started with the above commands. Now execute

```sh
bazel run //chatter:chatter
```

This command will build the necessary nodes and launch them. This is similar
to executing good-ol' `roslaunch`, but, running the chatter `ros_launch` target
using Bazel ensures all necessary dependencies are (re-)built.
4 changes: 2 additions & 2 deletions examples/chatter/chatter.launch
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<launch>
<node type="examples/chatter/talker" name="talker" />
<node type="examples/chatter/listener" name="listener" output="screen" />
<node type="chatter/talker" name="talker" />
<node type="chatter/listener" name="listener" output="screen" />
</launch>
Loading

0 comments on commit 43c7889

Please sign in to comment.