From ccdfc1ebc90cb22c33332167c8dec75fc088a7ad Mon Sep 17 00:00:00 2001 From: Martin Dahl Date: Fri, 2 Jun 2023 09:39:35 +0200 Subject: [PATCH] ROS_DISTRO as configuration option. Allows us to handle API differences between ros versions. --- r2r/build.rs | 1 + r2r/examples/parameters.rs | 2 ++ r2r/src/lib.rs | 13 +++++++++++++ r2r_actions/build.rs | 1 + r2r_common/src/lib.rs | 19 +++++++++++++++++++ r2r_msg_gen/build.rs | 1 + r2r_rcl/build.rs | 1 + 7 files changed, 38 insertions(+) diff --git a/r2r/build.rs b/r2r/build.rs index bec293f..491bc91 100644 --- a/r2r/build.rs +++ b/r2r/build.rs @@ -17,6 +17,7 @@ const GENERATED_FILES: &[&str] = &[ fn main() { r2r_common::print_cargo_watches(); + r2r_common::print_cargo_ros_distro(); let env_hash = r2r_common::get_env_hash(); let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); diff --git a/r2r/examples/parameters.rs b/r2r/examples/parameters.rs index 1fa5e20..c411cb4 100644 --- a/r2r/examples/parameters.rs +++ b/r2r/examples/parameters.rs @@ -10,6 +10,8 @@ use futures::task::LocalSpawnExt; // ros2 param get /demo/my_node key2 # (should return false) fn main() -> Result<(), Box> { + println!("Ros version: {}", r2r::ROS_DISTRO); + // set up executor let mut pool = LocalPool::new(); let spawner = pool.spawner(); diff --git a/r2r/src/lib.rs b/r2r/src/lib.rs index c5bf75f..0c63a2b 100644 --- a/r2r/src/lib.rs +++ b/r2r/src/lib.rs @@ -122,3 +122,16 @@ pub use nodes::{Node, Timer}; pub mod qos; pub use qos::QosProfile; + +/// The ros version currently built against. +#[cfg(r2r__ros__distro__foxy)] +pub const ROS_DISTRO: &str = "foxy"; +#[cfg(r2r__ros__distro__galactic)] +pub const ROS_DISTRO: &str = "galactic"; +#[cfg(r2r__ros__distro__humble)] +pub const ROS_DISTRO: &str = "humble"; +#[cfg(r2r__ros__distro__rolling)] +pub const ROS_DISTRO: &str = "rolling"; +#[cfg(not(any(r2r__ros__distro__foxy, r2r__ros__distro__galactic, + r2r__ros__distro__humble, r2r__ros__distro__rolling)))] +pub const ROS_DISTRO: &str = "unknown"; diff --git a/r2r_actions/build.rs b/r2r_actions/build.rs index d79a731..a347302 100644 --- a/r2r_actions/build.rs +++ b/r2r_actions/build.rs @@ -6,6 +6,7 @@ const BINDINGS_FILENAME: &str = "action_bindings.rs"; fn main() { r2r_common::print_cargo_watches(); + r2r_common::print_cargo_ros_distro(); run_bindgen(); run_dynlink(); diff --git a/r2r_common/src/lib.rs b/r2r_common/src/lib.rs index 127f64f..47f9824 100644 --- a/r2r_common/src/lib.rs +++ b/r2r_common/src/lib.rs @@ -6,12 +6,20 @@ use std::fs::{self, File}; use std::io::Read; use std::path::Path; +const SUPPORTED_ROS_DISTROS: &[&str] = &[ + "foxy", + "galactic", + "humble", + "rolling", +]; + const WATCHED_ENV_VARS: &[&str] = &[ "AMENT_PREFIX_PATH", "CMAKE_INCLUDE_DIRS", "CMAKE_LIBRARIES", "CMAKE_IDL_PACKAGES", "IDL_PACKAGE_FILTER", + "ROS_DISTRO", ]; pub fn get_env_hash() -> String { @@ -111,6 +119,17 @@ pub fn setup_bindgen_builder() -> bindgen::Builder { builder } +pub fn print_cargo_ros_distro() { + let ros_distro = env::var("ROS_DISTRO") + .unwrap_or_else(|_| panic!("ROS_DISTRO not set: Source your ROS!")); + + if SUPPORTED_ROS_DISTROS.iter().any(|d| d == &ros_distro) { + println!("cargo:rustc-cfg=r2r__ros__distro__{ros_distro}"); + } else { + panic!("ROS_DISTRO not supported: {ros_distro}"); + } +} + pub fn print_cargo_link_search() { if env::var("CMAKE_INCLUDE_DIRS").is_ok() { if let Ok(paths) = env::var("CMAKE_LIBRARIES") { diff --git a/r2r_msg_gen/build.rs b/r2r_msg_gen/build.rs index 993bc1b..66430ce 100644 --- a/r2r_msg_gen/build.rs +++ b/r2r_msg_gen/build.rs @@ -19,6 +19,7 @@ const GENERATED_FILES: &[&str] = &[ fn main() { r2r_common::print_cargo_watches(); + r2r_common::print_cargo_ros_distro(); let msg_list = r2r_common::get_wanted_messages(); run_bindgen(&msg_list); diff --git a/r2r_rcl/build.rs b/r2r_rcl/build.rs index b6ad7ab..e78cd03 100644 --- a/r2r_rcl/build.rs +++ b/r2r_rcl/build.rs @@ -6,6 +6,7 @@ use std::path::PathBuf; fn main() { r2r_common::print_cargo_watches(); + r2r_common::print_cargo_ros_distro(); run_bindgen(); run_dynlink(); }