From b5cbea1a209c188467562accbbec530eda2dbb33 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 5 Jan 2024 17:23:02 +0100 Subject: [PATCH] Include ros2-bridge header files in `dora-node-api-cxx` --- Cargo.lock | 1 + apis/c++/node/Cargo.toml | 2 + apis/c++/node/build.rs | 58 +++++++++++++++++++++ libraries/extensions/ros2-bridge/Cargo.toml | 1 + libraries/extensions/ros2-bridge/build.rs | 4 +- libraries/extensions/ros2-bridge/src/lib.rs | 2 +- 6 files changed, 65 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3dada54f..20942d39 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1604,6 +1604,7 @@ dependencies = [ "cxx", "cxx-build", "dora-node-api", + "dora-ros2-bridge", "eyre", ] diff --git a/apis/c++/node/Cargo.toml b/apis/c++/node/Cargo.toml index 77e58c25..347f07e4 100644 --- a/apis/c++/node/Cargo.toml +++ b/apis/c++/node/Cargo.toml @@ -14,11 +14,13 @@ crate-type = ["staticlib"] [features] default = ["tracing"] tracing = ["dora-node-api/tracing"] +ros2-bridge = [] [dependencies] cxx = "1.0.73" dora-node-api = { workspace = true } eyre = "0.6.8" +dora-ros2-bridge = { workspace = true, features = ["cxx-bridge"] } [build-dependencies] cxx-build = "1.0.73" diff --git a/apis/c++/node/build.rs b/apis/c++/node/build.rs index dba00707..f51d2af0 100644 --- a/apis/c++/node/build.rs +++ b/apis/c++/node/build.rs @@ -1,4 +1,62 @@ +use std::path::{Component, Path, PathBuf}; + fn main() { let _build = cxx_build::bridge("src/lib.rs"); println!("cargo:rerun-if-changed=src/lib.rs"); + + if cfg!(feature = "ros2-bridge") { + generate_ros2_message_header(); + } +} + +fn generate_ros2_message_header() { + let prefix = std::env::var("DEP_DORA_ROS2_BRIDGE_CXXBRIDGE_PREFIX").unwrap(); + let include_dir = PathBuf::from(std::env::var("DEP_DORA_ROS2_BRIDGE_CXXBRIDGE_DIR0").unwrap()); + let _crate_dir = std::env::var("DEP_DORA_ROS2_BRIDGE_CXXBRIDGE_DIR1").unwrap(); + + let header_path = include_dir + .join(&prefix) + .join(local_relative_path(&include_dir)) + .ancestors() + .nth(2) + .unwrap() + .join("messages.rs.h"); + let code_path = include_dir + .parent() + .unwrap() + .join("sources") + .join(&prefix) + .join(local_relative_path(&include_dir)) + .ancestors() + .nth(2) + .unwrap() + .join("messages.rs.cc"); + + // copy message files to target directory + let root = Path::new(env!("CARGO_MANIFEST_DIR")) + .ancestors() + .nth(3) + .unwrap(); + let target_path = root + .join("target") + .join("cxxbridge") + .join("dora-node-api-cxx") + .join("src") + .join("messages.rs.h"); + + std::fs::copy(header_path, &target_path).unwrap(); + std::fs::copy(code_path, target_path.with_file_name("messages.rs.cc")).unwrap(); +} + +// copy from cxx-build source +fn local_relative_path(path: &Path) -> PathBuf { + let mut rel_path = PathBuf::new(); + for component in path.components() { + match component { + Component::Prefix(_) | Component::RootDir | Component::CurDir => {} + Component::ParentDir => drop(rel_path.pop()), // noop if empty + Component::Normal(name) => rel_path.push(name), + } + } + rel_path } diff --git a/libraries/extensions/ros2-bridge/Cargo.toml b/libraries/extensions/ros2-bridge/Cargo.toml index 6b3b916b..2da5a5d3 100644 --- a/libraries/extensions/ros2-bridge/Cargo.toml +++ b/libraries/extensions/ros2-bridge/Cargo.toml @@ -2,6 +2,7 @@ name = "dora-ros2-bridge" version = "0.1.0" edition = "2021" +links = "dora-ros2-bridge" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/libraries/extensions/ros2-bridge/build.rs b/libraries/extensions/ros2-bridge/build.rs index a8ec4c98..44a94895 100644 --- a/libraries/extensions/ros2-bridge/build.rs +++ b/libraries/extensions/ros2-bridge/build.rs @@ -9,9 +9,9 @@ fn main() { .format_tokens(generated) .unwrap(); let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap()); - let target_file = out_dir.join("generated.rs"); + let target_file = out_dir.join("messages.rs"); std::fs::write(&target_file, generated_string).unwrap(); - println!("cargo:rustc-env=GENERATED_PATH={}", target_file.display()); + println!("cargo:rustc-env=MESSAGES_PATH={}", target_file.display()); #[cfg(feature = "cxx-bridge")] let _build = cxx_build::bridge(&target_file); diff --git a/libraries/extensions/ros2-bridge/src/lib.rs b/libraries/extensions/ros2-bridge/src/lib.rs index 7d29c7b1..85fcf116 100644 --- a/libraries/extensions/ros2-bridge/src/lib.rs +++ b/libraries/extensions/ros2-bridge/src/lib.rs @@ -2,7 +2,7 @@ pub use ros2_client; pub use rustdds; pub mod messages { - include!(env!("GENERATED_PATH")); + include!(env!("MESSAGES_PATH")); } pub mod _core;