From b36ab6897a90a34909008faf25e4e1b678d4c08b Mon Sep 17 00:00:00 2001 From: Esteve Fernandez Date: Tue, 19 Aug 2025 12:31:46 +0200 Subject: [PATCH 1/3] fix: let user pass compiler flag to specify the ROS distro Signed-off-by: Esteve Fernandez --- rclrs/Cargo.toml | 4 ++-- rclrs/build.rs | 35 ++++++++++++++++++++------------- rclrs/src/rcl_bindings.rs | 41 +++++++++++++++++++++++++-------------- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/rclrs/Cargo.toml b/rclrs/Cargo.toml index ee4d948c..914df8dd 100644 --- a/rclrs/Cargo.toml +++ b/rclrs/Cargo.toml @@ -43,10 +43,9 @@ tempfile = "3.3.0" tokio = { version = "1", features = ["rt", "time", "macros"] } [build-dependencies] -# Needed for FFI -bindgen = "0.70" # Needed for uploading documentation to docs.rs cfg-if = "1.0.0" +rustflags = "0.1" [features] default = [] @@ -58,3 +57,4 @@ use_ros_shim = ["rosidl_runtime_rs/use_ros_shim"] [package.metadata.docs.rs] features = ["use_ros_shim"] +rustc-args = ["--cfg", "ros_distro=\"humble\""] diff --git a/rclrs/build.rs b/rclrs/build.rs index 0df0688d..4710d362 100644 --- a/rclrs/build.rs +++ b/rclrs/build.rs @@ -1,12 +1,6 @@ -use std::{ - env, - fs::read_dir, - path::{Path, PathBuf}, -}; - +use std::{env, path::Path}; const AMENT_PREFIX_PATH: &str = "AMENT_PREFIX_PATH"; const ROS_DISTRO: &str = "ROS_DISTRO"; -const BINDGEN_WRAPPER: &str = "src/rcl_wrapper.h"; fn get_env_var_or_abort(env_var: &'static str) -> String { if let Ok(value) = env::var(env_var) { @@ -20,22 +14,37 @@ fn get_env_var_or_abort(env_var: &'static str) -> String { } fn main() { + println!( + "cargo:rustc-check-cfg=cfg(ros_distro, values(\"{}\"))", + vec!["humble", "jazzy", "kilted", "rolling"].join("\", \"") + ); let ros_distro = if let Ok(value) = env::var(ROS_DISTRO) { value } else { - let error_msg = - "ROS_DISTRO environment variable not set - please source ROS 2 installation first."; + use rustflags; cfg_if::cfg_if! { if #[cfg(feature="use_ros_shim")] { - println!("{}", error_msg); - return; + // // Look for --cfg ros_distro= + for flag in rustflags::from_env() { + if matches!(flag, rustflags::Flag::Cfg { ref name, value : _ } if name == "ros_distro") { + if let rustflags::Flag::Cfg {name:_, value: flag_value} = flag { + println!("cargo:rustc-cfg=ros_distro=\"{}\"", flag_value.unwrap()); + return; + } else { + continue; + } + } + } + let error_msg = + "When using the use_ros_shim feature, you must pass the ROS distribution you are targeting as a compiler flag with --cfg ros_distro=\"\""; + panic!("{}", error_msg); } else { + let error_msg = + "ROS_DISTRO environment variable not set - please source ROS 2 installation first."; panic!("{}", error_msg); } } }; - - println!("cargo:rustc-check-cfg=cfg(ros_distro, values(\"humble\", \"jazzy\", \"rolling\"))"); println!("cargo:rustc-cfg=ros_distro=\"{ros_distro}\""); let ament_prefix_paths = get_env_var_or_abort(AMENT_PREFIX_PATH); diff --git a/rclrs/src/rcl_bindings.rs b/rclrs/src/rcl_bindings.rs index fb10d4ab..3f62b1a4 100644 --- a/rclrs/src/rcl_bindings.rs +++ b/rclrs/src/rcl_bindings.rs @@ -12,30 +12,41 @@ #![allow(missing_docs)] cfg_if::cfg_if! { - if #[cfg(feature="use_ros_shim")] { + if #[cfg(ros_distro="humble")] { include!( concat!( env!("CARGO_MANIFEST_DIR"), - "/src/rcl_bindings_generated_", - "rolling", // rolling will always be a valid ROS distro - ".rs", - ) - ); - - } else { + "/src/rcl_bindings_generated_humble.rs", + ) + ); + } else if #[cfg(ros_distro="jazzy")] { include!( concat!( env!("CARGO_MANIFEST_DIR"), - "/src/rcl_bindings_generated_", - env!("ROS_DISTRO"), - ".rs", - ) - ); - - pub const RMW_GID_STORAGE_SIZE: usize = rmw_gid_storage_size_constant; + "/src/rcl_bindings_generated_jazzy.rs", + ) + ); + } else if #[cfg(ros_distro="kilted")] { + include!( + concat!( + env!("CARGO_MANIFEST_DIR"), + "/src/rcl_bindings_generated_kilted.rs", + ) + ); + } else if #[cfg(ros_distro="rolling")] { + include!( + concat!( + env!("CARGO_MANIFEST_DIR"), + "/src/rcl_bindings_generated_rolling.rs", + ) + ); + } else { + panic!("Unsupported ROS distribution"); } } +pub const RMW_GID_STORAGE_SIZE: usize = rmw_gid_storage_size_constant; + /// Wrapper around [`std::slice::from_raw_parts`] that accommodates the rcl /// convention of providing a null pointer to represent empty arrays. This /// violates the safety requirements of [`std::slice::from_raw_parts`]. From 17d794c49d3b219475a3fd4180c67575860eae52 Mon Sep 17 00:00:00 2001 From: Esteve Fernandez Date: Tue, 19 Aug 2025 17:52:08 +0200 Subject: [PATCH 2/3] fix: add compiler flag for picking ROS distro for release-plz Signed-off-by: Esteve Fernandez --- .github/workflows/release-plz.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/release-plz.yml b/.github/workflows/release-plz.yml index 271ed228..c15a71ab 100644 --- a/.github/workflows/release-plz.yml +++ b/.github/workflows/release-plz.yml @@ -5,6 +5,9 @@ on: branches: - main +env: + RUSTFLAGS: --cfg ros_distro="humble" + jobs: release-plz-release: name: Release-plz release From a1e7810c394c1387f7cd651bccd0dae6e4b0105e Mon Sep 17 00:00:00 2001 From: Esteve Fernandez Date: Tue, 19 Aug 2025 18:35:13 +0200 Subject: [PATCH 3/3] fix: escape compiler flags Signed-off-by: Esteve Fernandez --- .github/workflows/release-plz.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release-plz.yml b/.github/workflows/release-plz.yml index c15a71ab..c95f1437 100644 --- a/.github/workflows/release-plz.yml +++ b/.github/workflows/release-plz.yml @@ -6,7 +6,7 @@ on: - main env: - RUSTFLAGS: --cfg ros_distro="humble" + RUSTFLAGS: "--cfg ros_distro=\"humble\"" jobs: release-plz-release: