Skip to content

fix: let user pass compiler flag to specify the ROS distro #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/release-plz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
branches:
- main

env:
RUSTFLAGS: "--cfg ros_distro=\"humble\""

jobs:
release-plz-release:
name: Release-plz release
Expand Down
4 changes: 2 additions & 2 deletions rclrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ tempfile = "3.3.0"
tokio = { version = "1", features = ["rt", "time", "macros"] }

[build-dependencies]
# Needed for FFI
bindgen = "0.70"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason bindgen was removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not used anymore, the bindings are generated via the generate_binding.py script, which invokes the bindgen binary.

# Needed for uploading documentation to docs.rs
cfg-if = "1.0.0"
rustflags = "0.1"

[features]
default = []
Expand All @@ -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\""]
35 changes: 22 additions & 13 deletions rclrs/build.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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=<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=\"<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);
Expand Down
41 changes: 26 additions & 15 deletions rclrs/src/rcl_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down
Loading