use std::path::PathBuf;
fn main() {
println!("cargo:rerun-if-env-changed=PDFIUM_BUNDLE_LIB");
println!("cargo:rerun-if-env-changed=CARGO_FEATURE_BUNDLED");
if std::env::var("CARGO_FEATURE_BUNDLED").is_err() {
return;
}
let lib_src = match std::env::var("PDFIUM_BUNDLE_LIB") {
Ok(p) if !p.is_empty() => PathBuf::from(p),
_ => {
panic!(
"\n\
┌─────────────────────────────────────────────────────────┐\n\
│ pdfium-auto: `bundled` feature activated but │\n\
│ `PDFIUM_BUNDLE_LIB` is not set. │\n\
│ │\n\
│ Set it to the path of the platform pdfium shared lib: │\n\
│ │\n\
│ macOS : path/to/libpdfium.dylib │\n\
│ Linux : path/to/libpdfium.so │\n\
│ Windows: path\\to\\pdfium.dll │\n\
│ │\n\
│ Pre-built libraries are available from: │\n\
│ https://github.com/bblanchon/pdfium-binaries/releases │\n\
└─────────────────────────────────────────────────────────┘\n"
)
}
};
if !lib_src.exists() {
panic!(
"pdfium-auto: PDFIUM_BUNDLE_LIB points to a file that does not exist: {}",
lib_src.display()
);
}
let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR not set"));
let lib_dest = out_dir.join("bundled_pdfium_lib");
std::fs::copy(&lib_src, &lib_dest).unwrap_or_else(|e| {
panic!(
"pdfium-auto: failed to copy {} → {}: {}",
lib_src.display(),
lib_dest.display(),
e
)
});
let bundled_rs = out_dir.join("bundled.rs");
let code = r#"
/// The pdfium shared library embedded at compile time.
///
/// These bytes are written to the local cache directory on first use
/// (see [`super::bind_bundled`]).
pub static PDFIUM_BYTES: &[u8] = include_bytes!("bundled_pdfium_lib");
"#;
std::fs::write(&bundled_rs, code).unwrap_or_else(|e| {
panic!(
"pdfium-auto: failed to write {}: {}",
bundled_rs.display(),
e
)
});
println!("cargo:rerun-if-changed={}", lib_dest.display());
}