use std::env; use std::path::PathBuf; const PDFIUM_URL: &str = "https://github.com/bblanchon/pdfium-binaries/releases/download/chromium%2F7725/pdfium-linux-x64.tgz"; #[expect(clippy::expect_used)] #[expect(clippy::unwrap_used)] #[expect(clippy::print_stderr)] fn main() { println!("cargo:rerun-if-changed=build.rs"); if env::var("CARGO_FEATURE_PDFIUM").is_err() { return; } let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); // OUT_DIR is target//build/-/out // Go up 3 levels to reach target// let profile_dir = out_dir .ancestors() .nth(3) .expect("unexpected OUT_DIR structure") .to_path_buf(); let lib_path = profile_dir.join("libpdfium.so"); if !lib_path.exists() { let tgz_path = out_dir.join("pdfium.tgz"); eprintln!("cargo:warning=Downloading PDFium from {PDFIUM_URL}"); let status = std::process::Command::new("curl") .args(["-L", "--fail", "-o", tgz_path.to_str().unwrap(), PDFIUM_URL]) .status() .expect("failed to run curl"); assert!(status.success(), "curl failed to download PDFium"); let status = std::process::Command::new("tar") .args([ "-xzf", tgz_path.to_str().unwrap(), "-C", out_dir.to_str().unwrap(), ]) .status() .expect("failed to run tar"); assert!(status.success(), "tar failed to extract PDFium"); std::fs::copy(out_dir.join("lib").join("libpdfium.so"), &lib_path) .expect("failed to copy libpdfium.so"); } println!("cargo:rustc-link-search=native={}", profile_dir.display()); println!("cargo:rustc-link-lib=dylib=pdfium"); println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN"); }