| @@ -1604,6 +1604,7 @@ dependencies = [ | |||||
| "cxx", | "cxx", | ||||
| "cxx-build", | "cxx-build", | ||||
| "dora-node-api", | "dora-node-api", | ||||
| "dora-ros2-bridge", | |||||
| "eyre", | "eyre", | ||||
| ] | ] | ||||
| @@ -14,11 +14,13 @@ crate-type = ["staticlib"] | |||||
| [features] | [features] | ||||
| default = ["tracing"] | default = ["tracing"] | ||||
| tracing = ["dora-node-api/tracing"] | tracing = ["dora-node-api/tracing"] | ||||
| ros2-bridge = [] | |||||
| [dependencies] | [dependencies] | ||||
| cxx = "1.0.73" | cxx = "1.0.73" | ||||
| dora-node-api = { workspace = true } | dora-node-api = { workspace = true } | ||||
| eyre = "0.6.8" | eyre = "0.6.8" | ||||
| dora-ros2-bridge = { workspace = true, features = ["cxx-bridge"] } | |||||
| [build-dependencies] | [build-dependencies] | ||||
| cxx-build = "1.0.73" | cxx-build = "1.0.73" | ||||
| @@ -1,4 +1,62 @@ | |||||
| use std::path::{Component, Path, PathBuf}; | |||||
| fn main() { | fn main() { | ||||
| let _build = cxx_build::bridge("src/lib.rs"); | let _build = cxx_build::bridge("src/lib.rs"); | ||||
| println!("cargo:rerun-if-changed=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 | |||||
| } | } | ||||
| @@ -2,6 +2,7 @@ | |||||
| name = "dora-ros2-bridge" | name = "dora-ros2-bridge" | ||||
| version = "0.1.0" | version = "0.1.0" | ||||
| edition = "2021" | edition = "2021" | ||||
| links = "dora-ros2-bridge" | |||||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||||
| @@ -9,9 +9,9 @@ fn main() { | |||||
| .format_tokens(generated) | .format_tokens(generated) | ||||
| .unwrap(); | .unwrap(); | ||||
| let out_dir = PathBuf::from(std::env::var("OUT_DIR").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(); | 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")] | #[cfg(feature = "cxx-bridge")] | ||||
| let _build = cxx_build::bridge(&target_file); | let _build = cxx_build::bridge(&target_file); | ||||
| @@ -2,7 +2,7 @@ pub use ros2_client; | |||||
| pub use rustdds; | pub use rustdds; | ||||
| pub mod messages { | pub mod messages { | ||||
| include!(env!("GENERATED_PATH")); | |||||
| include!(env!("MESSAGES_PATH")); | |||||
| } | } | ||||
| pub mod _core; | pub mod _core; | ||||