From 61e2b4f9446636cfb9abd8b038a0923bac3f2520 Mon Sep 17 00:00:00 2001 From: Esteve Fernandez Date: Tue, 7 Nov 2023 18:20:20 +0100 Subject: [PATCH] Allow rustdoc to run without a ROS distribution --- rclrs/Cargo.toml | 6 ++ rclrs/build.rs | 11 ++- rclrs/src/rcl_bindings.rs | 164 ++++++++++++++++++++++++++++++++++---- 3 files changed, 164 insertions(+), 17 deletions(-) diff --git a/rclrs/Cargo.toml b/rclrs/Cargo.toml index 01c6d082a..2022f0898 100644 --- a/rclrs/Cargo.toml +++ b/rclrs/Cargo.toml @@ -16,6 +16,7 @@ path = "src/lib.rs" [dependencies] # Needed for dynamically finding type support libraries ament_rs = { version = "0.2", optional = true } +cfg-if = "1.0.0" # Needed for clients futures = "0.3" # Needed for dynamic messages @@ -33,3 +34,8 @@ bindgen = "0.66.1" [features] dyn_msg = ["ament_rs", "libloading"] +generate_docs = [] + +[package.metadata.docs.rs] + +features = ["generate_docs"] diff --git a/rclrs/build.rs b/rclrs/build.rs index fe8cd8b9a..753345974 100644 --- a/rclrs/build.rs +++ b/rclrs/build.rs @@ -18,7 +18,16 @@ fn get_env_var_or_abort(env_var: &'static str) -> String { } fn main() { - let ros_distro = get_env_var_or_abort(ROS_DISTRO); + let ros_distro = if let Ok(value) = env::var(ROS_DISTRO) { + value + } else { + println!( + "{} environment variable not set - please source ROS 2 installation first.", + ROS_DISTRO + ); + return; + }; + println!("cargo:rustc-cfg=ros_distro=\"{ros_distro}\""); let mut builder = bindgen::Builder::default() diff --git a/rclrs/src/rcl_bindings.rs b/rclrs/src/rcl_bindings.rs index 245430269..aad2545c7 100644 --- a/rclrs/src/rcl_bindings.rs +++ b/rclrs/src/rcl_bindings.rs @@ -1,16 +1,148 @@ -#![allow(dead_code)] -#![allow(deref_nullptr)] -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(non_snake_case)] -// Added to avoid clippy complaining about u128 types not being FFI safe -// Caused by https://github.com/ros2/ros2/issues/1374 in iron and newer -// See https://github.com/ros2-rust/ros2_rust/issues/320 -#![allow(improper_ctypes)] -#![allow(improper_ctypes_definitions)] -#![allow(clippy::all)] -#![allow(missing_docs)] - -include!(concat!(env!("OUT_DIR"), "/rcl_bindings_generated.rs")); - -pub const RMW_GID_STORAGE_SIZE: usize = rmw_gid_storage_size_constant; +cfg_if::cfg_if! { + if #[cfg(feature="generate_docs")] { + #[repr(C)] + #[derive(Debug)] + pub struct rcl_wait_set_t; + #[repr(C)] + #[derive(Debug)] + pub struct rcl_clock_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_node_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_context_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_guard_condition_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_subscription_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rmw_request_id_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_service_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_client_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_publisher_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rmw_time_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_arguments_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_allocator_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_ret_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_clock_type_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_node_options_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rmw_names_and_types_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rmw_topic_endpoint_info_array_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcutils_string_array_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_names_and_types_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_topic_endpoint_info_array_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_node_params_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_variant_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rcl_params_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rmw_message_info_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rmw_qos_durability_policy_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rmw_qos_history_policy_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rmw_qos_liveliness_policy_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rmw_qos_profile_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rmw_qos_reliability_policy_t; + + #[repr(C)] + #[derive(Debug)] + pub struct rosidl_message_type_support_t; + + pub const RMW_GID_STORAGE_SIZE: usize = 16; + + extern "C" { + pub fn rcl_context_is_valid(context: *const rcl_context_t) -> bool; + } + } else { + #![allow(dead_code)] + #![allow(deref_nullptr)] + #![allow(non_upper_case_globals)] + #![allow(non_camel_case_types)] + #![allow(non_snake_case)] + // Added to avoid clippy complaining about u128 types not being FFI safe + // Caused by https://github.com/ros2/ros2/issues/1374 in iron and newer + // See https://github.com/ros2-rust/ros2_rust/issues/320 + #![allow(improper_ctypes)] + #![allow(improper_ctypes_definitions)] + #![allow(clippy::all)] + #![allow(missing_docs)] + include!(concat!(env!("OUT_DIR"), "/rcl_bindings_generated.rs")); + + pub const RMW_GID_STORAGE_SIZE: usize = rmw_gid_storage_size_constant; + } +}