#![allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
fn main() {
#[cfg(feature = "flatbuffers-build")]
compile::run();
}
#[cfg(feature = "flatbuffers-build")]
mod compile {
use std::{env, ffi::OsStr, path::Path, process::Command};
const FLATC: &str = "FLATC_EXE";
const SCHEMAS: &str = "schemas";
pub(super) fn run() {
let flatc = env::var_os(FLATC).unwrap_or_else(|| {
panic!(
"The environmant variable \"{}\" must be set and point to the \"flatc\" executable.",
FLATC
)
});
let out_dir = env::var_os("OUT_DIR").expect("This should be set by Cargo");
let input_files = ["transforms_v001.fbs", "spherical_v001.fbs"];
println!("cargo::rerun-if-env-changed={}", FLATC);
println!("cargo::rerun-if-changed=build.rs");
for file in input_files {
println!("cargo::rerun-if-changed={}/{}", SCHEMAS, file);
}
let qualified_files = input_files.map(|file| format!("{}/{}", SCHEMAS, file));
compile(&flatc, &out_dir, &qualified_files);
let output_dirs = ["transforms", "spherical"];
let generated_folder = "./src/flatbuffers";
if std::fs::remove_dir_all(generated_folder).is_err() {
eprintln!("directory did not exist - ignoring");
}
std::fs::create_dir(generated_folder).unwrap();
for dir in output_dirs {
std::fs::create_dir(Path::new(generated_folder).join(dir)).unwrap();
for src in std::fs::read_dir(Path::new(&out_dir).join(dir)).unwrap() {
let src = src.unwrap();
println!("processing {:?}", src);
let file_name = src.file_name();
let dst = Path::new(generated_folder).join(dir).join(file_name);
println!("copying to {:?}", dst);
std::fs::copy(src.path(), dst).unwrap();
}
}
}
fn compile(flatc: &OsStr, out_dir: &OsStr, files: &[String]) {
let mut cmd = Command::new(flatc);
cmd.args(["--rust", "--rust-module-root-file", "-I", SCHEMAS, "-o"])
.arg(out_dir)
.args(files);
eprintln!("compilation command = {:?}", cmd);
let output = cmd.output().expect("schema compilation failed");
eprintln!(
"compilation stdout\n{}",
std::str::from_utf8(&output.stdout).unwrap()
);
eprintln!(
"compilation stderr\n{}",
std::str::from_utf8(&output.stderr).unwrap()
);
}
}