|
| 1 | +use cargo_manifest::Manifest; |
| 2 | +use std::path::Path; |
| 3 | +use std::fs::read_dir; |
| 4 | + |
| 5 | +const AMENT_PREFIX_PATH: &str = "AMENT_PREFIX_PATH"; |
| 6 | +const ROS_DISTRO: &str = "ROS_DISTRO"; |
| 7 | +const BINDGEN_WRAPPER: &str = "rclrs/src/rcl_wrapper.h"; |
| 8 | + |
| 9 | +fn get_env_var_or_abort(env_var: &'static str) -> String { |
| 10 | + if let Ok(value) = std::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 | + } |
| 18 | +} |
| 19 | + |
| 20 | +fn main() { |
| 21 | + let ros_distro = get_env_var_or_abort(ROS_DISTRO); |
| 22 | + let ament_prefix_paths = get_env_var_or_abort(AMENT_PREFIX_PATH); |
| 23 | + |
| 24 | + let Ok(workspace) = Manifest::from_path("Cargo.toml") |
| 25 | + .map_err(|_| ()) |
| 26 | + .and_then(|manifest| manifest.workspace.ok_or(())) else |
| 27 | + { |
| 28 | + panic!(" > ERROR: Run the generate_bindings script from the root Cargo workspace of ros2_rust"); |
| 29 | + }; |
| 30 | + |
| 31 | + let has_rclrs = workspace.members.iter().find(|member | *member == "rclrs").is_some(); |
| 32 | + if !has_rclrs { |
| 33 | + panic!( |
| 34 | + " > ERROR: Run the generate_bindings script from the root Cargo workspace of ros2_rust. \ |
| 35 | + Could not find rclrs in this workspace. Members include {:?}", |
| 36 | + workspace.members, |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + let mut builder = bindgen::Builder::default() |
| 41 | + .header(BINDGEN_WRAPPER) |
| 42 | + .derive_copy(false) |
| 43 | + .allowlist_type("rcl_.*") |
| 44 | + .allowlist_type("rmw_.*") |
| 45 | + .allowlist_type("rcutils_.*") |
| 46 | + .allowlist_type("rosidl_.*") |
| 47 | + .allowlist_function("rcl_.*") |
| 48 | + .allowlist_function("rmw_.*") |
| 49 | + .allowlist_function("rcutils_.*") |
| 50 | + .allowlist_function("rosidl_.*") |
| 51 | + .allowlist_var("rcl_.*") |
| 52 | + .allowlist_var("rmw_.*") |
| 53 | + .allowlist_var("rcutils_.*") |
| 54 | + .allowlist_var("rosidl_.*") |
| 55 | + .layout_tests(false) |
| 56 | + .default_enum_style(bindgen::EnumVariation::Rust { |
| 57 | + non_exhaustive: false, |
| 58 | + }); |
| 59 | + |
| 60 | + for ament_prefix_path in ament_prefix_paths.split(':').map(Path::new) { |
| 61 | + // Locate the ament index |
| 62 | + let ament_index = ament_prefix_path.join("share/ament_index/resource_index/packages"); |
| 63 | + if !ament_index.is_dir() { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + // Old-style include directory |
| 68 | + let include_dir = ament_prefix_path.join("include"); |
| 69 | + |
| 70 | + // Including the old-style packages |
| 71 | + builder = builder.clang_arg(format!("-isystem{}", include_dir.display())); |
| 72 | + |
| 73 | + // Search for and include new-style-converted package paths |
| 74 | + for dir_entry in read_dir(&ament_index).unwrap().filter_map(|p| p.ok()) { |
| 75 | + let package = dir_entry.file_name(); |
| 76 | + let package_include_dir = include_dir.join(&package); |
| 77 | + |
| 78 | + if package_include_dir.is_dir() { |
| 79 | + let new_style_include_dir = package_include_dir.join(&package); |
| 80 | + |
| 81 | + // CycloneDDS is a special case - it needs to be included as if it were a new-style path, but |
| 82 | + // doesn't actually have a secondary folder within it called "CycloneDDS" |
| 83 | + // TODO(jhdcs): if this changes in future, remove this check |
| 84 | + if package == "CycloneDDS" || new_style_include_dir.is_dir() { |
| 85 | + builder = |
| 86 | + builder.clang_arg(format!("-isystem{}", package_include_dir.display())); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + let bindings = builder.generate().expect("Unable to generate bindings"); |
| 93 | + let bindings_path = format!("rclrs/src/rcl_bindings_generated_{ros_distro}.rs"); |
| 94 | + bindings.write_to_file(bindings_path).expect("Failed to generate bindings"); |
| 95 | +} |
0 commit comments