diff --git a/rclrs/Cargo.toml b/rclrs/Cargo.toml index ee4d948cd..542122af4 100644 --- a/rclrs/Cargo.toml +++ b/rclrs/Cargo.toml @@ -43,18 +43,13 @@ 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 = [] dyn_msg = ["ament_rs", "libloading"] serde = ["dep:serde", "dep:serde-big-array", "rosidl_runtime_rs/serde"] -# This feature is solely for the purpose of being able to generate documetation without a ROS installation -# The only intended usage of this feature is for docs.rs builders to work, and is not intended to be used by end users -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 0df0688db..acd34d354 100644 --- a/rclrs/build.rs +++ b/rclrs/build.rs @@ -1,12 +1,11 @@ use std::{ env, - fs::read_dir, - path::{Path, PathBuf}, + path::Path, }; +use rustflags::Flag; 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) { @@ -23,16 +22,20 @@ fn main() { 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."; - cfg_if::cfg_if! { - if #[cfg(feature="use_ros_shim")] { - println!("{}", error_msg); - return; - } else { - panic!("{}", error_msg); + // Look for --cfg ros_distro= + for flag in rustflags::from_env() { + if matches!(flag, Flag::Cfg { ref name, value : _ } if name == "ros_distro") { + if let Flag::Cfg {name:_, value: flag_value} = flag { + println!("cargo:rustc-cfg=ros_distro={}", flag_value.unwrap()); + return; + } else { + continue; + } } } + 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\"))"); diff --git a/rclrs/src/rcl_bindings.rs b/rclrs/src/rcl_bindings.rs index fb10d4ab1..93225cf0d 100644 --- a/rclrs/src/rcl_bindings.rs +++ b/rclrs/src/rcl_bindings.rs @@ -12,28 +12,26 @@ #![allow(missing_docs)] cfg_if::cfg_if! { - if #[cfg(feature="use_ros_shim")] { - include!( - concat!( - env!("CARGO_MANIFEST_DIR"), - "/src/rcl_bindings_generated_", - "rolling", // rolling will always be a valid ROS distro - ".rs", - ) - ); - + let ros_distro = if #[cfg(ros_distro="humble")] { + "humble" + } else if #[cfg(ros_distro="jazzy")] { + "jazzy" + } else if #[cfg(ros_distro="kilted")] { + "kilted" + } else if #[cfg(ros_distro="rolling")] { + "rolling" } else { - include!( - concat!( - env!("CARGO_MANIFEST_DIR"), - "/src/rcl_bindings_generated_", - env!("ROS_DISTRO"), - ".rs", + panic!("Unsupported ROS distribution"); + } + include!( + concat!( + env!("CARGO_MANIFEST_DIR"), + "/src/rcl_bindings_generated_", + ros_distro, + ".rs", ) ); - - pub const RMW_GID_STORAGE_SIZE: usize = rmw_gid_storage_size_constant; - } + pub const RMW_GID_STORAGE_SIZE: usize = rmw_gid_storage_size_constant; } /// Wrapper around [`std::slice::from_raw_parts`] that accommodates the rcl