ⓇThe vehicle OS company.

Bazel and ROS 2 –Building Large Scale Safety Applications

Kilian Funk, Karl Wallner

October 19-21, 2022
at ROSCon ‘22© 2022 Apex.AI, Inc.

Large scale safety applications. How hard can it be?
Function and Performance in a distributedsystem

Well deﬁned Process
Compliance to State of the Art (e.g. V-Model,ISO 26262)● Safety Case (HARA, FMEA, FuSaCo, TeSaCo)● Veriﬁcation and Validation● Standardized code creation (four eyes, etc.)● CI/CD for a large team of developers●Eﬃcient communication for large data
● Realtime requirements●Fail-Safe or Fail-Operational Behavior
Error-detection● Redundancy● Special Hardware (e.g. lock step)● Architectural measures●●Traceability●Reproducibility© 2022 Apex.AI, Inc.

Traceability & reproducibility
Validate

Vehicle deﬁnition
and requirements
Analysis

Requirements analysis
Design systemarchitectureN

Mechanics

arearerdw
Ha

are
Cross

E/E
Deployable
Softwareftw

NTIO
CAIFI

ftw

So

arerdwHa

Component level
physicalVerifyThe safety case relies on our ability toprove transparently:

EGEC

E/E
Subsystem level
logicalRATIOSP

Mechanics

Application inthe ﬁeld

System test
and integration

SoSystem level
functions

Validate

INT

Vehicle level
requirements

Vehicle test
and integration● why our application behaves like itdoes● what exactly we have developed anddeployed into the ﬁeld

Source code

domainimplementation

Arrows show dependencies that shall be traceable.
• Along the V for artifact creation• Right to left for veriﬁcation and validation3© 2022 Apex.AI, Inc.

Traceability & reproducibility
Validate

Vehicle deﬁnition
and requirements
Analysis

Requirements analysis
Design systemarchitectureNMechanics

domainare

arerdw
HaareCross

E/E
Deployable
Softwareftw

NTIOCAIFIftw

Soarerdw
Ha

Component level
physicalVerify

The safety case relies on our ability toprove transparently:

EGEC

E/E
Subsystem level
logicalRA
TIO

SP

Mechanics

Application inthe ﬁeld

System test
and integration

SoSystem level
functions

Validate

INT

Vehicle level
requirements

Vehicle test
and integration● why our application behaves like itdoes● what exactly we have developed anddeployed into the ﬁeldSource code

Build System

implementation

Arrows show dependencies that shall be traceable.
• Along the V for artifact creation• Right to left for veriﬁcation and validation4© 2022 Apex.AI, Inc.

Comparing Colcon/CMake vs. Bazel
Colcon/CMakeBazel

Imperative macro language, diﬀerent (redundant)
deﬁnitions

Declarative, abstract deﬁnition of build

Various diﬀerent build tools(CMakeLists.txt, setuptools, Make)

Full programming language (starlark) for
extension/customization

C/C++ as (main) target language

Multi language support

Caching not safe, due to potentially missing
dependencies

Integrated (reliable, artifact based) local and remotecaching

Building in host environment(Almost) Hermetic build in sandboxes

Compile time and runtime dependency discovery

Explicit (full) dependency tree

Disclaimer: Biased towards use case of large scale safety applications!

5© 2022 Apex.AI, Inc.

Things to consider when migrating to Bazel
Colcon/CMakeBazel

Artifacts are tightly organized in packages

Strongly artifacts oriented; packages are mainly to
improve clarity for user

Package names must be unique

Workspace names must be unique.
Package and target names are absolute paths withina workspace# BUILD.bazel

package.xml

cc_binary(name = “simple_publisher”,…
)

CMakeLists.txt

msg_library(name = “my_msgs”,…
)
ros_pkg(name = “my_cool_pkg”,executables_lib = [“simple_publisher”],)

6© 2022 Apex.AI, Inc.

Package deployment
Goal: Provide a means to “install/setup” bazel built ROS 2 packages (C++, Python for now) onto a target for
usage with ros2cli
Non-Goal: Provide a pre-built ROS 2 package that can be dependent on for building other ROS 2 packages«cc_binary»:some_executable$ # Execute directly in your bazel workspace:$ bazel run :some_executable7© 2022 Apex.AI, Inc.

Package deployment
Goal: Provide a means to “install/setup” bazel built ROS 2 packages (C++, Python for now) onto a target for
usage with ros2cli
Non-Goal: Provide a pre-built ROS 2 package that can be dependent on for building other ROS 2 packages«cc_binary»«ros_pkg»«ros_pkg_archive»:some_executable→CcInfo:my_cool_pkg:my_cool_pkg_archive→RosPkgInfo«py_wheel»:a_python_pkg→PyInfo«ros_pkg»:some_other_ros_pkg$ # Execute directly in your bazel workspace:$ bazel run :some_executable$ # Execute installed version:$ bazel build :install_archive$ bazel run :install_archive.install -- $INSTALL_PATH$ source $INSTALL_PATH/setup.bash$ ros2 run my_cool_pkg some_executable→RosPkgInfo
8© 2022 Apex.AI, Inc.

Message generation
Goal: Provide an extensible multi-language concept for message code generation● msg_library rule only provides
information about the input● no output artifact is generated«msg_library»*.idl/*.msg:some_custom_msgs→MsgInfo«msg_library»*.idl/*.msg
xxx.BUILD
xxx.BUILD
xxx.BUILD

9:std_msgs→MsgInfo© 2022 Apex.AI, Inc.

Message generation
Goal: Provide an extensible multi-language concept for message code generation● msg_library rule only provides
information about the input● no output artifact is generated● cc_msg_library aspects are instantiated by the user of amessage on demand● they generate the “linkable” library for the required language«cc_msg_library»«msg_library»*.idl/*.msg

cc_A(:some_msgs)→CcInfo:some_custom_msgs→MsgInfo«cc_msg_library»«msg_library»*.idl/*.msg
xxx.BUILD
xxx.BUILD
xxx.BUILD

10

cc_A(:std_msgs)→CcInfo:std_msgs→MsgInfo© 2022 Apex.AI, Inc.

Message generation
Goal: Provide an extensible multi-language concept for message code generation● msg_library rule only provides
information about the input● no output artifact is generated● cc_msg_library and py_msg_library aspects areinstantiated by the user of a message on demand● they generate the “linkable” library for the required language«cc_msg_library»«msg_library»*.idl/*.msg

cc_A(:some_msgs)→CcInfo:some_custom_msgs→MsgInfo«py_msg_library»*.idl
py_A(:some_msgs)→PyInfo«cc_msg_library»«msg_library»*.idl/*.msg
xxx.BUILD
xxx.BUILD
xxx.BUILD

11

cc_A(:std_msgs)«py_msg_library»→CcInfo:std_msgs→MsgInfopy_A(:std_msgs)→PyInfo© 2022 Apex.AI, Inc.

Repository setup
Goal: Keep as much of the federated repo concept as possible• Bazel build conﬁguration can be added on top of existing ROS 2• A pinning mechanism is introduced to ensure reproducibility«Bazel workspace»«Github repository»«Bazel workspace»@ros2_config@ros2.rclcpp
setup.bzl
BUILD.bazel

ros2/ros2.
.
.

ros2_conﬁg()
copy ofload«Bazel workspace»«Bazel workspace»«Github repository»@ros2….

@ros2
BUILD.bazelros2.repos

ros2_.lock

ApexAI/rules_ros
xxx.BUILD
xxx.BUILD
xxx.BUILD
xxx.BUILD
xxx.BUILD

12© 2022 Apex.AI, Inc.

Setup a workspace to use the ROS2-Bazel fork
● Install bazel/bazelisk on your host system● Add the WORKSPACE ﬁle to an empty folder● Add (.bazelrc, .bazelingore, .bazelversion) asneeded● Create your own package within your workspace# WORKSPACEworkspace(name = “my_cool_workspace)
http_archive(name = “ros2”,url =“https://github.com/ApexAI/rules_ros/…/rules_ros-x.x.tgz”
sha = “xxxxxx”,)
load("@ros2//bazel/rules_repo:defs.bzl", "configure_ros2")
configure_ros2(distro = "humble")load("@ros2_config//:setup1.bzl", "setup1")
setup1()[...]load("@ros2_config//:setup4.bzl", "setup4")
setup4()13© 2022 Apex.AI, Inc.

Summary● Bazel is an alternative to the native ROS 2 build system Colcon/CMake● Traceability from deployed software back to source code is achieved by a complete dependency tree
● Reproducibility can be achieved through a hermetic build● We have shown how ROS 2 can be set up with bazel including core concepts like message generation andpackage deployment

We are in the process of open sourcing the contents of this talk:

http://github.com/ApexAI/rules_ros
We are looking forward to your feedback on github.

14© 2022 Apex.AI, Inc.