Skip to content

Commit 9c309c4

Browse files
committed
- Streamline parts of the build script to get closer to docs.rs builds.
- Change the CI to use a WIP version of cargo-ament-build - Remove extra line from subscription.rs which caused test failures
1 parent 7cc282c commit 9c309c4

File tree

5 files changed

+40
-45
lines changed

5 files changed

+40
-45
lines changed

.github/workflows/rust-minimal.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ jobs:
6969

7070
# Colcon can not be run in a venv which is required in Ubuntu Noble
7171
# Removing the externally managed file
72-
- name: Install colcon-cargo and colcon-ros-cargo
72+
- name: Install colcon-cargo, colcon-ros-cargo, and cargo-ament-build
7373
run: |
7474
sudo rm -f /usr/lib/python3.12/EXTERNALLY-MANAGED
7575
sudo pip3 install git+https://github.com/colcon/colcon-cargo.git
7676
sudo pip3 install git+https://github.com/colcon/colcon-ros-cargo.git
77+
cargo install --git ssh://git@github.com/maspe36/cargo-ament-build.git --branch feature/skip_reexport_marker_file
7778
7879
- name: Check formatting of Rust packages
7980
run: |

.github/workflows/rust-stable.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ jobs:
6969

7070
# Colcon can not be run in a venv which is required in Ubuntu Noble
7171
# Removing the externally managed file
72-
- name: Install colcon-cargo and colcon-ros-cargo
72+
- name: Install colcon-cargo, colcon-ros-cargo, and cargo-ament-build
7373
run: |
7474
sudo rm -f /usr/lib/python3.12/EXTERNALLY-MANAGED
7575
sudo pip3 install git+https://github.com/colcon/colcon-cargo.git
7676
sudo pip3 install git+https://github.com/colcon/colcon-ros-cargo.git
77+
cargo install --git ssh://git@github.com/maspe36/cargo-ament-build.git --branch feature/skip_reexport_marker_file
7778
7879
- name: Check formatting of Rust packages
7980
run: |

.github/workflows/rust-win.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ jobs:
4040
# prerequisites and fixes for windows build ros2_rust:
4141
# * Libclang has to be added (from the ros2_rust instructions) and the dll has to be renamed
4242
# * colcon-ros-cargo and colcon-cargo have to be added as PyPI packages
43+
# * cargo-ament-build is manually installed from github for now to include the change to skip
44+
# installing the marker file if the package should be reexported
4345
run: |
4446
pixi add libclang --manifest-path C:\pixi_ws\pixi.toml
4547
$src = "C:\pixi_ws\.pixi\envs\default\Library\bin\libclang-13.dll"
@@ -48,6 +50,7 @@ jobs:
4850
pixi add --pypi "colcon-ros-cargo@git+https://github.com/colcon/colcon-ros-cargo.git" --manifest-path C:\pixi_ws\pixi.toml
4951
pixi add --pypi "colcon-cargo@git+https://github.com/colcon/colcon-cargo.git" --manifest-path C:\pixi_ws\pixi.toml
5052
pixi upgrade colcon-core --manifest-path C:\pixi_ws\pixi.toml
53+
cargo install --git ssh://git@github.com/maspe36/cargo-ament-build.git --branch feature/skip_reexport_marker_file
5154
5255
- name: Get prebuild ROS files and unzip
5356
run: |

rclrs/build.rs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@ use cargo_toml::Manifest;
66
const AMENT_PREFIX_PATH: &str = "AMENT_PREFIX_PATH";
77
const ROS_DISTRO: &str = "ROS_DISTRO";
88

9-
fn get_env_var_or_abort(env_var: &'static str) -> String {
10-
if let Ok(value) = env::var(env_var) {
11-
value
12-
} else {
13-
panic!(
14-
"{} environment variable not set - please source ROS 2 installation first.",
15-
env_var
16-
);
17-
}
9+
fn get_env_or_shim<F>(var_name: &str, shim_logic: F) -> String
10+
where
11+
F: FnOnce() -> Result<String, String>,
12+
{
13+
let res = env::var(var_name).or_else(|_| {
14+
if env::var("CARGO_FEATURE_USE_ROS_SHIM").is_ok() {
15+
shim_logic()
16+
} else {
17+
Err(format!(
18+
"{var_name} environment variable not set - please source ROS 2 installation first."
19+
))
20+
}
21+
}).expect(format!("Failed to get {var_name}").as_str());
22+
23+
// Make sure this script will rerun if the environment variable changes.
24+
// If we used `env!` or `option_env!`, we would not need this.
25+
println!("cargo:rerun-if-env-changed={var_name}");
26+
27+
res
1828
}
1929

2030
fn marked_reexport(cargo_toml: String) -> bool {
@@ -35,44 +45,26 @@ fn main() {
3545
"cargo:rustc-check-cfg=cfg(ros_distro, values(\"{}\"))",
3646
vec!["humble", "jazzy", "kilted", "rolling"].join("\", \"")
3747
);
38-
let ros_distro = if let Ok(value) = env::var(ROS_DISTRO) {
39-
value
40-
} else {
41-
cfg_if::cfg_if! {
42-
if #[cfg(feature="use_ros_shim")] {
43-
use rustflags;
44-
// // Look for --cfg ros_distro=<ros_distro>
45-
for flag in rustflags::from_env() {
46-
if matches!(flag, rustflags::Flag::Cfg { ref name, value : _ } if name == "ros_distro") {
47-
if let rustflags::Flag::Cfg {name:_, value: flag_value} = flag {
48-
println!("cargo:rustc-cfg=ros_distro=\"{}\"", flag_value.unwrap());
49-
return;
50-
} else {
51-
continue;
52-
}
53-
}
54-
}
55-
let error_msg =
56-
"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>\"";
57-
panic!("{}", error_msg);
58-
} else {
59-
let error_msg =
60-
"ROS_DISTRO environment variable not set - please source ROS 2 installation first.";
61-
panic!("{}", error_msg);
62-
}
63-
}
64-
};
48+
let ros_distro = get_env_or_shim(ROS_DISTRO, || {
49+
rustflags::from_env()
50+
.find_map(|flag| match flag {
51+
rustflags::Flag::Cfg { name, value } if &name == "ros_distro" => value,
52+
_ => None,
53+
})
54+
.ok_or_else(|| "When using use_ros_shim, you must pass --cfg ros_distro=\"...\" via RUSTFLAGS".to_string())
55+
});
56+
6557
println!("cargo:rustc-cfg=ros_distro=\"{ros_distro}\"");
6658

67-
let ament_prefix_paths = get_env_var_or_abort(AMENT_PREFIX_PATH);
59+
let ament_prefix_paths = get_env_or_shim(AMENT_PREFIX_PATH, || Ok(String::new()));
60+
6861
for ament_prefix_path in ament_prefix_paths.split(':').map(Path::new) {
6962
// Link the native libraries
7063
let library_path = ament_prefix_path.join("lib");
7164
println!("cargo:rustc-link-search=native={}", library_path.display());
7265
}
7366

7467
// Re-export any generated interface crates that we find
75-
let ament_prefix_paths = env!("AMENT_PREFIX_PATH", "AMENT_PREFIX_PATH environment variable not set - please source ROS 2 installation first.");
7668
let export_crate_tomls = ament_prefix_paths
7769
.split(':')
7870
.map(PathBuf::from)
@@ -123,7 +115,7 @@ fn main() {
123115
.filter(|entry| entry.path().is_file())
124116
// Ignore lib.rs and any rmw.rs. lib.rs is only used if the crate is consumed
125117
// independently, and rmw.rs files need their top level module
126-
// (i.e. msg, srv, action) to exist to be re-exported.
118+
// (i.e., msg, srv, action) to exist to be re-exported.
127119
.filter(|entry| {
128120
let name = entry.file_name();
129121
name != "lib.rs" && name != "rmw.rs"
@@ -154,7 +146,7 @@ fn main() {
154146
let dest_path = PathBuf::from(out_dir).join("interfaces.rs");
155147

156148
// TODO I would like to run rustfmt on this generated code, similar to how bindgen does it
157-
fs::write(dest_path, content.clone()).expect("Failed to write interfaces.rs");
149+
fs::write(&dest_path, content.clone()).expect("Failed to write interfaces.rs");
158150

159151
println!("cargo:rustc-link-lib=dylib=rcl");
160152
println!("cargo:rustc-link-lib=dylib=rcl_action");

rclrs/src/subscription.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,7 @@ mod tests {
533533
StreamExt,
534534
};
535535
use std::sync::atomic::{AtomicBool, Ordering};
536-
537-
let _ = crate::rcl_interfaces::msg::IntegerRange;
538-
536+
539537
let mut executor = Context::default().create_basic_executor();
540538
let node = executor
541539
.create_node(

0 commit comments

Comments
 (0)