diff --git a/apis/c++/node/build.rs b/apis/c++/node/build.rs index 44b01ed7b..0d1ec2319 100644 --- a/apis/c++/node/build.rs +++ b/apis/c++/node/build.rs @@ -9,12 +9,7 @@ fn main() { println!("cargo:rerun-if-changed=src/lib.rs"); // rename header files - let root = Path::new(env!("CARGO_MANIFEST_DIR")) - .ancestors() - .nth(3) - .unwrap(); - let src_dir = root - .join("target") + let src_dir = target_dir() .join("cxxbridge") .join("dora-node-api-cxx") .join("src"); @@ -38,6 +33,18 @@ fn main() { bridge_files.clear(); } +fn target_dir() -> PathBuf { + std::env::var("CARGO_TARGET_DIR") + .map(PathBuf::from) + .unwrap_or_else(|_| { + let root = Path::new(env!("CARGO_MANIFEST_DIR")) + .ancestors() + .nth(3) + .unwrap(); + root.join("target") + }) +} + #[cfg(feature = "ros2-bridge")] mod ros2 { use std::path::{Component, Path, PathBuf}; @@ -107,12 +114,7 @@ mod ros2 { .join("ros2_bindings.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") + let target_path = target_dir() .join("cxxbridge") .join("dora-node-api-cxx") .join("dora-ros2-bindings.h");