diff --git a/Cargo.toml b/Cargo.toml index 48b6c88..20573e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,10 @@ categories = ["api-bindings", "encoding", "compression", repository = "https://github.com/DuckerMan/magnum-opus" documentation = "https://docs.rs/magnum-opus" +[features] +linux-pkg-config = ["dep:pkg-config"] + [build-dependencies] target_build_utils = "0.3" bindgen = "0.59" +pkg-config = { version = "0.3.27", optional = true } diff --git a/build.rs b/build.rs index 1925833..ac5fbf6 100644 --- a/build.rs +++ b/build.rs @@ -3,6 +3,17 @@ use std::{ path::{Path, PathBuf}, }; +#[cfg(all(target_os = "linux", feature = "linux-pkg-config"))] +fn link_pkg_config(name: &str) -> Vec { + let lib = pkg_config::probe_library(name) + .expect(format!( + "unable to find '{name}' development headers with pkg-config (feature linux-pkg-config is enabled). + try installing '{name}-dev' from your system package manager.").as_str()); + + lib.include_paths +} + +#[cfg(not(all(target_os = "linux", feature = "linux-pkg-config")))] fn link_vcpkg(mut path: PathBuf, name: &str) -> PathBuf { let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); let mut target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(); @@ -45,6 +56,7 @@ fn link_vcpkg(mut path: PathBuf, name: &str) -> PathBuf { include } +#[cfg(not(all(target_os = "linux", feature = "linux-pkg-config")))] fn link_homebrew_m1(name: &str) -> PathBuf { let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(); @@ -95,6 +107,12 @@ fn link_homebrew_m1(name: &str) -> PathBuf { include } +#[cfg(all(target_os = "linux", feature = "linux-pkg-config"))] +fn find_package(name: &str) -> Vec { + return link_pkg_config(name); +} + +#[cfg(not(all(target_os = "linux", feature = "linux-pkg-config")))] fn find_package(name: &str) -> Vec { if let Ok(vcpkg_root) = std::env::var("VCPKG_ROOT") { vec![link_vcpkg(vcpkg_root.into(), name)]