Skip to content

Commit 45d9af5

Browse files
committed
Introduce cfg attribute for ROS distro
1 parent d73394f commit 45d9af5

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed

rclrs/build.rs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,23 @@ use std::fs::read_dir;
33
use std::path::{Path, PathBuf};
44

55
const AMENT_PREFIX_PATH: &str = "AMENT_PREFIX_PATH";
6+
const ROS_DISTRO: &str = "ROS_DISTRO";
7+
8+
fn get_env_var_or_abort(env_var: &'static str) -> String {
9+
if let Ok(value) = env::var(env_var) {
10+
value
11+
} else {
12+
panic!(
13+
"{} environment variable not set - please source ROS 2 installation first.",
14+
env_var
15+
);
16+
}
17+
}
618

719
fn main() {
20+
let distro = get_env_var_or_abort(ROS_DISTRO);
21+
println!("cargo:rustc-cfg=distro=\"{distro}\"");
22+
823
let mut builder = bindgen::Builder::default()
924
.header("src/rcl_wrapper.h")
1025
.derive_copy(false)
@@ -39,47 +54,41 @@ fn main() {
3954
//
4055
// See REP 122 for more details: https://www.ros.org/reps/rep-0122.html#filesystem-layout
4156

42-
if let Ok(ament_prefix_paths) = env::var(AMENT_PREFIX_PATH) {
43-
for ament_prefix_path in ament_prefix_paths.split(':').map(Path::new) {
44-
// Locate the ament index
45-
let ament_index = ament_prefix_path.join("share/ament_index/resource_index/packages");
46-
if !ament_index.is_dir() {
47-
continue;
48-
}
57+
let ament_prefix_paths = get_env_var_or_abort(AMENT_PREFIX_PATH);
58+
for ament_prefix_path in ament_prefix_paths.split(':').map(Path::new) {
59+
// Locate the ament index
60+
let ament_index = ament_prefix_path.join("share/ament_index/resource_index/packages");
61+
if !ament_index.is_dir() {
62+
continue;
63+
}
4964

50-
// Old-style include directory
51-
let include_dir = ament_prefix_path.join("include");
65+
// Old-style include directory
66+
let include_dir = ament_prefix_path.join("include");
5267

53-
// Including the old-style packages
54-
builder = builder.clang_arg(format!("-isystem{}", include_dir.display()));
68+
// Including the old-style packages
69+
builder = builder.clang_arg(format!("-isystem{}", include_dir.display()));
5570

56-
// Search for and include new-style-converted package paths
57-
for dir_entry in read_dir(&ament_index).unwrap().filter_map(|p| p.ok()) {
58-
let package = dir_entry.file_name();
59-
let package_include_dir = include_dir.join(&package);
71+
// Search for and include new-style-converted package paths
72+
for dir_entry in read_dir(&ament_index).unwrap().filter_map(|p| p.ok()) {
73+
let package = dir_entry.file_name();
74+
let package_include_dir = include_dir.join(&package);
6075

61-
if package_include_dir.is_dir() {
62-
let new_style_include_dir = package_include_dir.join(&package);
76+
if package_include_dir.is_dir() {
77+
let new_style_include_dir = package_include_dir.join(&package);
6378

64-
// CycloneDDS is a special case - it needs to be included as if it were a new-style path, but
65-
// doesn't actually have a secondary folder within it called "CycloneDDS"
66-
// TODO(jhdcs): if this changes in future, remove this check
67-
if package == "CycloneDDS" || new_style_include_dir.is_dir() {
68-
builder =
69-
builder.clang_arg(format!("-isystem{}", package_include_dir.display()));
70-
}
79+
// CycloneDDS is a special case - it needs to be included as if it were a new-style path, but
80+
// doesn't actually have a secondary folder within it called "CycloneDDS"
81+
// TODO(jhdcs): if this changes in future, remove this check
82+
if package == "CycloneDDS" || new_style_include_dir.is_dir() {
83+
builder =
84+
builder.clang_arg(format!("-isystem{}", package_include_dir.display()));
7185
}
7286
}
73-
74-
// Link the native libraries
75-
let library_path = ament_prefix_path.join("lib");
76-
println!("cargo:rustc-link-search=native={}", library_path.display());
7787
}
78-
} else {
79-
panic!(
80-
"{} environment variable not set - please source ROS 2 installation first.",
81-
AMENT_PREFIX_PATH
82-
);
88+
89+
// Link the native libraries
90+
let library_path = ament_prefix_path.join("lib");
91+
println!("cargo:rustc-link-search=native={}", library_path.display());
8392
}
8493

8594
println!("cargo:rustc-link-lib=dylib=rcl");

rclrs/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,15 @@ pub fn spin_once(node: &Node, timeout: Option<Duration>) -> Result<(), RclReturn
5454
///
5555
/// This function additionally checks that the context is still valid.
5656
pub fn spin(node: &Node) -> Result<(), RclReturnCode> {
57+
// The context_is_valid functions exists only to abstract away ROS distro differences
58+
#[cfg(distro = "foxy")]
5759
// SAFETY: No preconditions for this function.
58-
// The mutability of the argument has changed between ROS 2 versions, hence the extra `as _`
59-
while unsafe { rcl_context_is_valid(&mut *node.context.lock() as _) } {
60+
let context_is_valid = || unsafe { rcl_context_is_valid(&mut *node.context.lock()) };
61+
#[cfg(not(distro = "foxy"))]
62+
// SAFETY: No preconditions for this function.
63+
let context_is_valid = || unsafe { rcl_context_is_valid(&*node.context.lock()) };
64+
65+
while context_is_valid() {
6066
if let Some(error) = spin_once(node, None).err() {
6167
match error {
6268
RclReturnCode::Timeout => continue,

0 commit comments

Comments
 (0)