use std::env;
use std::env::consts;
use std::fmt::format;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
extern crate bindgen;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap_or(".".to_string());
let arch = consts::ARCH.replace("x86_64", "x64").replace("aarch64", "arm64");
let os = consts::OS.replace("macos", "osx");
let triplet = env::var("VCPKG_DEFAULT_TRIPLET").unwrap_or_else(|_| format!("{}-{}", arch, os));
let vcpkg_output = Command::new("vcpkg")
.args([
"install",
"--triplet",
&triplet,
"--x-install-root",
format!("{}/vcpkg_installed", out_dir).as_str(),
])
.output()
.expect("Failed to run vcpkg.");
if !vcpkg_output.status.success() {
panic!("Failed to install vcpkg dependencies:\n{}", String::from_utf8_lossy(&vcpkg_output.stdout));
}
let mut vcpkg_dir = PathBuf::from(format!("vcpkg_installed/{}", triplet));
if !vcpkg_dir.exists() {
vcpkg_dir = PathBuf::from(format!("../vcpkg_installed/{}", triplet));
}
vcpkg_dir = fs::canonicalize(vcpkg_dir.clone()).expect(format!("{} not found", vcpkg_dir.display()).as_str());
println!("cargo:rustc-link-search=native={}/lib", vcpkg_dir.to_str().unwrap());
println!("cargo:rustc-link-lib=lexbor");
println!("cargo:rerun-if-changed=src/third_party/lexbor.h");
bindgen::Builder::default()
.header("src/third_party/lexbor.h")
.clang_arg(format!("-I{}/include", vcpkg_dir.to_str().unwrap()))
.allowlist_function("(lexbor|lxb)_.*")
.allowlist_type("(LEXBOR|lexbor|lxb)_.*")
.allowlist_var("(LEXBOR|LXB)_.*")
.default_enum_style(bindgen::EnumVariation::ModuleConsts)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Error generating Lexbor binding")
.write_to_file(PathBuf::from(out_dir).join("lexbor.rs"))
.unwrap();
}