diff --git a/r2r_actions/Cargo.toml b/r2r_actions/Cargo.toml index d7afd2a..d027d01 100644 --- a/r2r_actions/Cargo.toml +++ b/r2r_actions/Cargo.toml @@ -17,3 +17,10 @@ r2r_msg_gen = { path = "../r2r_msg_gen", version = "0.3.3" } [build-dependencies] bindgen = "0.63.0" r2r_common = { path = "../r2r_common", version = "0.3.2" } + +[features] +save-bindgen = ["r2r_rcl/save-bindgen", "r2r_msg_gen/save-bindgen"] +doc-only = ["r2r_rcl/doc-only", "r2r_msg_gen/doc-only"] + +[package.metadata.docs.rs] +features = ["doc-only"] diff --git a/r2r_actions/build.rs b/r2r_actions/build.rs index bdfd757..d79a731 100644 --- a/r2r_actions/build.rs +++ b/r2r_actions/build.rs @@ -1,16 +1,67 @@ -use std::env; -use std::path::PathBuf; +use std::fs::OpenOptions; +use std::path::{Path, PathBuf}; +use std::{env, fs}; + +const BINDINGS_FILENAME: &str = "action_bindings.rs"; fn main() { r2r_common::print_cargo_watches(); - r2r_common::print_cargo_link_search(); - let mut builder = r2r_common::setup_bindgen_builder(); - builder = builder.header("src/action_wrapper.h"); + run_bindgen(); + run_dynlink(); +} - println!("cargo:rustc-link-lib=dylib=rcl_action"); +fn run_bindgen() { + let env_hash = r2r_common::get_env_hash(); + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); + let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); + let bindgen_dir = out_dir.join(env_hash); + let save_dir = manifest_dir.join("bindings"); + let cache_file = bindgen_dir.join(BINDINGS_FILENAME); + let target_file = out_dir.join(BINDINGS_FILENAME); + let mark_file = bindgen_dir.join("done"); + let saved_file = save_dir.join(BINDINGS_FILENAME); - let bindings = builder + if cfg!(feature = "doc-only") { + // If "doc-only" feature is present, copy from $crate/bindings/* to OUT_DIR + eprintln!( + "Copy from '{}' to '{}'", + saved_file.display(), + target_file.display() + ); + fs::copy(&saved_file, &target_file).unwrap(); + } else { + // If bindgen was done before, use cached files. + if !mark_file.exists() { + eprintln!("Generate bindings file '{}'", cache_file.display()); + fs::create_dir_all(&bindgen_dir).unwrap(); + generate_bindings(&cache_file); + touch(&mark_file); + } else { + eprintln!("Used cached file '{}'", cache_file.display()); + } + + fs::copy(&cache_file, &target_file).unwrap(); + + #[cfg(feature = "save-bindgen")] + { + fs::create_dir_all(&save_dir).unwrap(); + fs::copy(&cache_file, &saved_file).unwrap(); + } + } +} + +fn run_dynlink() { + #[cfg(not(feature = "doc-only"))] + { + r2r_common::print_cargo_link_search(); + println!("cargo:rustc-link-lib=dylib=rcl_action"); + } +} + +fn generate_bindings(out_file: &Path) { + let bindings = r2r_common::setup_bindgen_builder() + .header("src/action_wrapper.h") .no_debug("_OSUnaligned.*") .derive_partialeq(true) .derive_copy(true) @@ -44,8 +95,15 @@ fn main() { .generate() .expect("Unable to generate bindings"); - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); bindings - .write_to_file(out_path.join("action_bindings.rs")) + .write_to_file(out_file) .expect("Couldn't write bindings!"); } + +fn touch(path: &Path) { + OpenOptions::new() + .create(true) + .write(true) + .open(path) + .unwrap_or_else(|_| panic!("Unable to create file '{}'", path.display())); +}