From a23cff064f23025396bbecf67fe22aa959ad35e5 Mon Sep 17 00:00:00 2001 From: Chuck Atkins Date: Sun, 7 Jan 2024 20:31:16 -0500 Subject: [PATCH] Use pkg-config to find the gssapi system library This uses pkg-config to locate the gssapi system library and the necessary include paths. This can be particularly useful when gssapi has interface dependencies (mit has comerr includes in it's public interface, for example) that are installed in different prefixes as is often the case with third party package managers. --- libgssapi-sys/Cargo.toml | 1 + libgssapi-sys/build.rs | 49 ++++++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/libgssapi-sys/Cargo.toml b/libgssapi-sys/Cargo.toml index a2a05c92..ef4faeb1 100644 --- a/libgssapi-sys/Cargo.toml +++ b/libgssapi-sys/Cargo.toml @@ -15,3 +15,4 @@ links = "gssapi_krb5" [build-dependencies] bindgen = "0.64" +pkg-config = "0.3" diff --git a/libgssapi-sys/build.rs b/libgssapi-sys/build.rs index 38acfd23..dc1e2f30 100644 --- a/libgssapi-sys/build.rs +++ b/libgssapi-sys/build.rs @@ -18,8 +18,24 @@ enum Gssapi { Apple, } +fn builder_from_pkgconfig(lib: pkg_config::Library) -> bindgen::Builder { + bindgen::Builder::default() + .clang_args(lib.include_paths.iter().map(|path| format!("-I{}", path.to_string_lossy()))) +} + +fn try_pkgconfig() -> Result<(Gssapi, bindgen::Builder), pkg_config::Error> { + match pkg_config::probe_library("mit-krb5-gssapi") { + Ok(lib) => Ok((Gssapi::Mit, builder_from_pkgconfig(lib))), + Err(_) => match pkg_config::probe_library("heimdal-gssapi") { + Ok(lib) => Ok((Gssapi::Heimdal, builder_from_pkgconfig(lib))), + Err(lib) => Err(lib), + } + } +} + fn which() -> Gssapi { if cfg!(target_os = "macos") { + println!("cargo:rustc-link-lib=framework=GSS"); return Gssapi::Apple; } else if cfg!(target_os = "windows") { panic!("use SSPI on windows") @@ -37,9 +53,11 @@ fn which() -> Gssapi { for path in krb5_path.into_iter().chain(ldpath.split(':')).chain(paths) { if !path.is_empty() { if search_pat(path, "libgssapi_krb5.so*") { + println!("cargo:rustc-link-lib=gssapi_krb5"); return Gssapi::Mit; } if search_pat(path, "libgssapi.so*") { + println!("cargo:rustc-link-lib=gssapi"); return Gssapi::Heimdal; } } @@ -51,21 +69,22 @@ fn which() -> Gssapi { } fn main() { - let imp = which(); - match imp { - Gssapi::Mit => println!("cargo:rustc-link-lib=gssapi_krb5"), - Gssapi::Heimdal => println!("cargo:rustc-link-lib=gssapi"), - Gssapi::Apple => println!("cargo:rustc-link-lib=framework=GSS"), - } - let builder = bindgen::Builder::default(); - let nix_cflags = env::var("NIX_CFLAGS_COMPILE"); - let builder = match imp { - Gssapi::Mit | Gssapi::Heimdal => match nix_cflags { - Err(_) => builder, - Ok(flags) => builder.clang_args(flags.split(" ")), - }, - Gssapi::Apple => - builder.clang_arg("-F/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks") + let (imp, builder) = match try_pkgconfig() { + Ok((imp, builder)) => (imp, builder), + Err(_) => { + let imp = which(); + let builder = bindgen::Builder::default(); + let nix_cflags = env::var("NIX_CFLAGS_COMPILE"); + let builder = match imp { + Gssapi::Mit | Gssapi::Heimdal => match nix_cflags { + Err(_) => builder, + Ok(flags) => builder.clang_args(flags.split(" ")), + }, + Gssapi::Apple => + builder.clang_arg("-F/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks") + }; + (imp, builder) + } }; let bindings = builder .allowlist_type("(OM_.+|gss_.+)")