#![allow(dead_code)]
use std::path::{Path, PathBuf};
pub const DECODER: &str = "draco_decoder";
pub const ENCODER: &str = "draco_encoder";
pub const BUILD_HINT: &str = "\
C++ Draco tools are required for this comparison. Point DRACO_CPP_BUILD_DIR at a
build of them, or build them next to this repository:
cmake -S . -B build && cmake --build build --config Release \
--target draco_decoder draco_encoder";
pub fn tool_file_name(tool: &str) -> String {
if cfg!(windows) {
format!("{tool}.exe")
} else {
tool.to_string()
}
}
fn tool_env_var(tool: &str) -> &'static str {
match tool {
DECODER => "DRACO_CPP_DECODER",
ENCODER => "DRACO_CPP_ENCODER",
_ => "",
}
}
fn tool_in_build_dir(build_dir: &Path, tool: &str) -> Option<PathBuf> {
let file_name = tool_file_name(tool);
let roots = [build_dir.to_path_buf(), build_dir.join("src").join("draco")];
for root in roots {
let direct = root.join(&file_name);
if direct.exists() {
return Some(direct);
}
for config in ["Release", "Debug"] {
let configured = root.join(config).join(&file_name);
if configured.exists() {
return Some(configured);
}
}
}
None
}
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("crates directory")
.parent()
.expect("repo root")
.to_path_buf()
}
pub fn find_cpp_tool(tool: &str) -> Option<PathBuf> {
let env_var = tool_env_var(tool);
if !env_var.is_empty() {
if let Ok(path) = std::env::var(env_var) {
let path = PathBuf::from(path);
assert!(
path.exists(),
"{env_var} points at a missing {tool}: {}\n{BUILD_HINT}",
path.display()
);
return Some(path);
}
}
if let Ok(build_dir) = std::env::var("DRACO_CPP_BUILD_DIR") {
if let Some(path) = tool_in_build_dir(Path::new(&build_dir), tool) {
return Some(path);
}
}
let root = repo_root();
["build-original", "build"]
.into_iter()
.find_map(|dir| tool_in_build_dir(&root.join(dir), tool))
}
pub fn cpp_tools_required() -> bool {
std::env::var_os("DRACO_REQUIRE_CPP_TOOLS").is_some()
}
pub fn require_cpp_tool(tool: &str) -> PathBuf {
find_cpp_tool(tool).unwrap_or_else(|| panic!("Could not find {tool}.\n{BUILD_HINT}"))
}
pub fn optional_cpp_tool(tool: &str) -> Option<PathBuf> {
if let Some(path) = find_cpp_tool(tool) {
return Some(path);
}
assert!(
!cpp_tools_required(),
"DRACO_REQUIRE_CPP_TOOLS is set and {tool} was not found.\n{BUILD_HINT}"
);
eprintln!("Skipping: {tool} not found. {BUILD_HINT}");
None
}
pub fn optional_cpp_codec() -> Option<(PathBuf, PathBuf)> {
let encoder = optional_cpp_tool(ENCODER)?;
let decoder = optional_cpp_tool(DECODER)?;
Some((encoder, decoder))
}