diff --git a/crates/pile-value/build.rs b/crates/pile-value/build.rs index 06351f9..ecd79cd 100644 --- a/crates/pile-value/build.rs +++ b/crates/pile-value/build.rs @@ -1,7 +1,34 @@ 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"; +const PDFIUM_VERSION: &str = "chromium%2F7725"; + +fn pdfium_url(os: &str, arch: &str) -> String { + let platform = match (os, arch) { + ("linux", "x86_64") => "linux-x64", + ("linux", "aarch64") => "linux-arm64", + ("macos", "x86_64") => "mac-x64", + ("macos", "aarch64") => "mac-arm64", + _ => panic!("unsupported platform: {os}-{arch}"), + }; + format!( + "https://github.com/bblanchon/pdfium-binaries/releases/download/{PDFIUM_VERSION}/pdfium-{platform}.tgz" + ) +} + +fn lib_name(os: &str) -> &'static str { + match os { + "macos" => "libpdfium.dylib", + _ => "libpdfium.so", + } +} + +fn rpath_flag(os: &str) -> &'static str { + match os { + "macos" => "-Wl,-rpath,@loader_path", + _ => "-Wl,-rpath,$ORIGIN", + } +} #[expect(clippy::expect_used)] #[expect(clippy::unwrap_used)] @@ -13,6 +40,9 @@ fn main() { return; } + let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); + let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); // OUT_DIR is target//build/-/out @@ -30,15 +60,17 @@ fn main() { return; } - let lib_path = profile_dir.join("libpdfium.so"); + let lib_file = lib_name(&os); + let lib_path = profile_dir.join(lib_file); if !lib_path.exists() { + let url = pdfium_url(&os, &arch); let tgz_path = out_dir.join("pdfium.tgz"); - eprintln!("cargo:warning=Downloading PDFium from {PDFIUM_URL}"); + eprintln!("cargo:warning=Downloading PDFium from {url}"); let status = std::process::Command::new("curl") - .args(["-L", "--fail", "-o", tgz_path.to_str().unwrap(), PDFIUM_URL]) + .args(["-L", "--fail", "-o", tgz_path.to_str().unwrap(), &url]) .status() .expect("failed to run curl"); assert!(status.success(), "curl failed to download PDFium"); @@ -54,11 +86,11 @@ fn main() { .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"); + std::fs::copy(out_dir.join("lib").join(lib_file), &lib_path) + .expect("failed to copy pdfium library"); } println!("cargo:rustc-link-search=native={}", profile_dir.display()); println!("cargo:rustc-link-lib=dylib=pdfium"); - println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN"); + println!("cargo:rustc-link-arg={}", rpath_flag(&os)); } diff --git a/default.nix b/default.nix index 58e6270..b8d11fb 100644 --- a/default.nix +++ b/default.nix @@ -42,7 +42,10 @@ pileRustPlatform.buildRustPackage { postInstall = '' wrapProgram $out/bin/pile \ - --prefix LD_LIBRARY_PATH : ${pkgs.lib.makeLibraryPath [ pkgs.pdfium-binaries ]} + ${if pkgs.stdenv.isDarwin then + "--prefix DYLD_LIBRARY_PATH : ${pkgs.lib.makeLibraryPath [ pkgs.pdfium-binaries ]}" + else + "--prefix LD_LIBRARY_PATH : ${pkgs.lib.makeLibraryPath [ pkgs.pdfium-binaries ]}"} ''; meta = {