Mercurial > crates > nonstick
diff libpam-sys/libpam-sys-helpers/build.rs @ 148:4b3a5095f68c
Move libpam-sys helpers into their own library.
- Renames libpam-sys-helpers to libpam-sys-consts.
- Moves libpam-sys-helpers::helpers into libpam-sys-helpers,
which moves them completely out of libpam-sys's dependency chain.
- Moves the aliases from libpam-sys into libpam-sys::aliases.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Mon, 07 Jul 2025 12:11:43 -0400 |
parents | efbc235f01d3 |
children |
line wrap: on
line diff
--- a/libpam-sys/libpam-sys-helpers/build.rs Sun Jul 06 19:23:02 2025 -0400 +++ b/libpam-sys/libpam-sys-helpers/build.rs Mon Jul 07 12:11:43 2025 -0400 @@ -1,96 +1,6 @@ -#![allow(unexpected_cfgs)] - -use std::ffi::{c_void, CString}; -use std::ptr::NonNull; -use std::{env, fs}; - -include!("src/pam_impl.rs"); +use libpam_sys_consts::pam_impl; fn main() { - let pam_impl = match option_env!("LIBPAMSYS_IMPL") { - // The default option: Guess what PAM impl we're using based on OS. - None | Some("") => LibPam::detect(), - Some(other) => match PamImpl::try_from(other) { - Ok(i) => i, - Err(_) => { - panic!( - "unknown PAM implementation {other:?}. valid LIBPAMSYS_IMPLs are {:?}, or unset to detect", PamImpl::items() - ) - } - }, - }; - let impl_str = format!("{pam_impl:?}"); - println!("{}", generate_cfg(&impl_str)); - // We set this environment variable to substitute into docstrings. - println!("cargo:rustc-env=LIBPAMSYS_IMPL={impl_str}"); - fs::write( - format!("{}/pam_impl_const.rs", env::var("OUT_DIR").unwrap()), - generate_consts(&impl_str), - ) - .unwrap(); -} - -fn generate_consts(impl_str: &str) -> String { - format!( - "\ -impl PamImpl {{ -/// The implementation of libpam this was built for (`{impl_str}`). -pub const CURRENT: Self = Self::{impl_str}; -}} - -/// String name of [`PamImpl::CURRENT`], for substituting into docs. -#[macro_export] -macro_rules! pam_impl_name {{ () => ({impl_str:?}) }} - " - ) + println!("cargo:rustc-link-lib=pam"); + pam_impl::enable_pam_impl_cfg(); } - -struct LibPam(NonNull<c_void>); - -impl LibPam { - fn detect() -> PamImpl { - if let Some(lib) = Self::open() { - if lib.has("pam_syslog") { - return PamImpl::LinuxPam; - } else if lib.has("_openpam_log") { - return PamImpl::OpenPam; - } else if lib.has("__pam_get_authtok") { - return PamImpl::Sun; - } - } - if cfg!(target_os = "linux") { - PamImpl::LinuxPam - } else if cfg!(any( - target_os = "macos", - target_os = "freebsd", - target_os = "netbsd", - target_os = "dragonfly", - target_os = "openbsd", - )) { - PamImpl::OpenPam - } else if cfg!(any(target_os = "illumos", target_os = "solaris")) { - PamImpl::Sun - } else { - PamImpl::XSso - } - } - - fn open() -> Option<Self> { - NonNull::new(unsafe { libc::dlopen(b"libpam.so\0".as_ptr().cast(), libc::RTLD_LAZY) }) - .map(Self) - } - - fn has(&self, name: &str) -> bool { - let name = CString::new(name).unwrap(); - let symbol = unsafe { libc::dlsym(self.0.as_ptr(), name.as_ptr()) }; - !symbol.is_null() - } -} - -impl Drop for LibPam { - fn drop(&mut self) { - unsafe { - libc::dlclose(self.0.as_ptr()); - } - } -}