Skip to content

Commit e02e0ea

Browse files
authored
fix: let user pass compiler flag to specify the ROS distro (#514)
* fix: let user pass compiler flag to specify the ROS distro Signed-off-by: Esteve Fernandez <[email protected]> * fix: add compiler flag for picking ROS distro for release-plz Signed-off-by: Esteve Fernandez <[email protected]> * fix: escape compiler flags Signed-off-by: Esteve Fernandez <[email protected]> --------- Signed-off-by: Esteve Fernandez <[email protected]>
1 parent 95dc8e0 commit e02e0ea

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

.github/workflows/release-plz.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on:
55
branches:
66
- main
77

8+
env:
9+
RUSTFLAGS: "--cfg ros_distro=\"humble\""
10+
811
jobs:
912
release-plz-release:
1013
name: Release-plz release

rclrs/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ tempfile = "3.3.0"
4343
tokio = { version = "1", features = ["rt", "time", "macros"] }
4444

4545
[build-dependencies]
46-
# Needed for FFI
47-
bindgen = "0.70"
4846
# Needed for uploading documentation to docs.rs
4947
cfg-if = "1.0.0"
48+
rustflags = "0.1"
5049

5150
[features]
5251
default = []
@@ -58,3 +57,4 @@ use_ros_shim = ["rosidl_runtime_rs/use_ros_shim"]
5857

5958
[package.metadata.docs.rs]
6059
features = ["use_ros_shim"]
60+
rustc-args = ["--cfg", "ros_distro=\"humble\""]

rclrs/build.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
use std::{
2-
env,
3-
fs::read_dir,
4-
path::{Path, PathBuf},
5-
};
6-
1+
use std::{env, path::Path};
72
const AMENT_PREFIX_PATH: &str = "AMENT_PREFIX_PATH";
83
const ROS_DISTRO: &str = "ROS_DISTRO";
9-
const BINDGEN_WRAPPER: &str = "src/rcl_wrapper.h";
104

115
fn get_env_var_or_abort(env_var: &'static str) -> String {
126
if let Ok(value) = env::var(env_var) {
@@ -20,22 +14,37 @@ fn get_env_var_or_abort(env_var: &'static str) -> String {
2014
}
2115

2216
fn main() {
17+
println!(
18+
"cargo:rustc-check-cfg=cfg(ros_distro, values(\"{}\"))",
19+
vec!["humble", "jazzy", "kilted", "rolling"].join("\", \"")
20+
);
2321
let ros_distro = if let Ok(value) = env::var(ROS_DISTRO) {
2422
value
2523
} else {
26-
let error_msg =
27-
"ROS_DISTRO environment variable not set - please source ROS 2 installation first.";
24+
use rustflags;
2825
cfg_if::cfg_if! {
2926
if #[cfg(feature="use_ros_shim")] {
30-
println!("{}", error_msg);
31-
return;
27+
// // Look for --cfg ros_distro=<ros_distro>
28+
for flag in rustflags::from_env() {
29+
if matches!(flag, rustflags::Flag::Cfg { ref name, value : _ } if name == "ros_distro") {
30+
if let rustflags::Flag::Cfg {name:_, value: flag_value} = flag {
31+
println!("cargo:rustc-cfg=ros_distro=\"{}\"", flag_value.unwrap());
32+
return;
33+
} else {
34+
continue;
35+
}
36+
}
37+
}
38+
let error_msg =
39+
"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>\"";
40+
panic!("{}", error_msg);
3241
} else {
42+
let error_msg =
43+
"ROS_DISTRO environment variable not set - please source ROS 2 installation first.";
3344
panic!("{}", error_msg);
3445
}
3546
}
3647
};
37-
38-
println!("cargo:rustc-check-cfg=cfg(ros_distro, values(\"humble\", \"jazzy\", \"rolling\"))");
3948
println!("cargo:rustc-cfg=ros_distro=\"{ros_distro}\"");
4049

4150
let ament_prefix_paths = get_env_var_or_abort(AMENT_PREFIX_PATH);

rclrs/src/rcl_bindings.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,41 @@
1212
#![allow(missing_docs)]
1313

1414
cfg_if::cfg_if! {
15-
if #[cfg(feature="use_ros_shim")] {
15+
if #[cfg(ros_distro="humble")] {
1616
include!(
1717
concat!(
1818
env!("CARGO_MANIFEST_DIR"),
19-
"/src/rcl_bindings_generated_",
20-
"rolling", // rolling will always be a valid ROS distro
21-
".rs",
22-
)
23-
);
24-
25-
} else {
19+
"/src/rcl_bindings_generated_humble.rs",
20+
)
21+
);
22+
} else if #[cfg(ros_distro="jazzy")] {
2623
include!(
2724
concat!(
2825
env!("CARGO_MANIFEST_DIR"),
29-
"/src/rcl_bindings_generated_",
30-
env!("ROS_DISTRO"),
31-
".rs",
32-
)
33-
);
34-
35-
pub const RMW_GID_STORAGE_SIZE: usize = rmw_gid_storage_size_constant;
26+
"/src/rcl_bindings_generated_jazzy.rs",
27+
)
28+
);
29+
} else if #[cfg(ros_distro="kilted")] {
30+
include!(
31+
concat!(
32+
env!("CARGO_MANIFEST_DIR"),
33+
"/src/rcl_bindings_generated_kilted.rs",
34+
)
35+
);
36+
} else if #[cfg(ros_distro="rolling")] {
37+
include!(
38+
concat!(
39+
env!("CARGO_MANIFEST_DIR"),
40+
"/src/rcl_bindings_generated_rolling.rs",
41+
)
42+
);
43+
} else {
44+
panic!("Unsupported ROS distribution");
3645
}
3746
}
3847

48+
pub const RMW_GID_STORAGE_SIZE: usize = rmw_gid_storage_size_constant;
49+
3950
/// Wrapper around [`std::slice::from_raw_parts`] that accommodates the rcl
4051
/// convention of providing a null pointer to represent empty arrays. This
4152
/// violates the safety requirements of [`std::slice::from_raw_parts`].

0 commit comments

Comments
 (0)