annotate libpam-sys/build.rs @ 134:6c1e1bdb4164

Use standard #[cfg] directives rather than custom proc macros. Instead of having to do a bunch of custom parsing and other logic that tools often choke on, this change introduces an easy way to depend upon custom #[cfg]s provided by the libpam-sys crate.
author Paul Fisher <paul@pfish.zone>
date Thu, 03 Jul 2025 11:03:36 -0400
parents a632a8874131
children efbc235f01d3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
134
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
1 #![allow(unexpected_cfgs)]
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 108
diff changeset
2
134
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
3 use std::env;
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
4 use std::ffi::{c_void, CString};
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
5 use std::fs;
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
6 use std::ptr::NonNull;
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
7
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
8 include!("src/pam_impl.rs");
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 108
diff changeset
9
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10 fn main() {
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 println!("cargo:rustc-link-lib=pam");
134
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
12
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
13 let pam_impl = match option_env!("LIBPAMSYS_IMPL") {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
14 // The default option: Guess what PAM impl we're using based on OS.
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
15 None | Some("") => detect(),
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
16 Some(other) => match PamImpl::try_from(other) {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
17 Ok(i) => i,
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
18 Err(_) => {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
19 panic!(
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
20 "unknown PAM implementation {other:?}. valid LIBPAMSYS_IMPLs are {:?}, or unset to detect", PamImpl::items()
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
21 )
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
22 }
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
23 },
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
24 };
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
25 println!("{}", checkcfg());
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
26 let impl_str = format!("{pam_impl:?}");
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
27 println!("{}", implcfg(&impl_str));
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
28 println!("cargo:rustc-env=LIBPAMSYS_IMPL={impl_str}");
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
29 fs::write(
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
30 format!("{}/pam_impl_const.rs", env::var("OUT_DIR").unwrap()),
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
31 format!(
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
32 "\
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
33 impl PamImpl {{\
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
34 /// The implementation of libpam this was built for (`{impl_str}`).
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
35 pub const CURRENT: Self = Self::{impl_str};\
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
36 /// String version for internal consumption.
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
37 const CURRENT_STR: &'static str = {impl_str:?};\
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
38 }}
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
39 "
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
40 ),
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
41 )
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
42 .unwrap();
106
49d9e2b5c189 An irresponsible mix of implementing libpam-sys and other stuff.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43 }
134
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
44
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
45 fn detect() -> PamImpl {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
46 if let Some(lib) = LibPam::open() {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
47 if lib.has("pam_syslog") {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
48 return PamImpl::LinuxPam;
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
49 } else if lib.has("_openpam_log") {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
50 return PamImpl::OpenPam;
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
51 } else if lib.has("__pam_get_authtok") {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
52 return PamImpl::Sun;
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
53 }
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
54 }
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
55 if cfg!(target_os = "linux") {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
56 PamImpl::LinuxPam
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
57 } else if cfg!(any(
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
58 target_os = "macos",
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
59 target_os = "freebsd",
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
60 target_os = "netbsd",
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
61 target_os = "dragonfly",
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
62 target_os = "openbsd",
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
63 )) {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
64 PamImpl::OpenPam
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
65 } else if cfg!(any(target_os = "illumos", target_os = "solaris")) {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
66 PamImpl::Sun
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
67 } else {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
68 PamImpl::XSso
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
69 }
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
70 }
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
71
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
72 struct LibPam(NonNull<c_void>);
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
73
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
74 impl LibPam {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
75 fn open() -> Option<Self> {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
76 NonNull::new(unsafe { libc::dlopen(b"libpam.so\0".as_ptr().cast(), libc::RTLD_LAZY) })
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
77 .map(Self)
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
78 }
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
79
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
80 fn has(&self, name: &str) -> bool {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
81 let name = CString::new(name).unwrap();
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
82 let symbol = unsafe { libc::dlsym(self.0.as_ptr(), name.as_ptr()) };
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
83 !symbol.is_null()
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
84 }
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
85 }
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
86
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
87 impl Drop for LibPam {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
88 fn drop(&mut self) {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
89 unsafe {
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
90 libc::dlclose(self.0.as_ptr());
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
91 }
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
92 }
6c1e1bdb4164 Use standard #[cfg] directives rather than custom proc macros.
Paul Fisher <paul@pfish.zone>
parents: 131
diff changeset
93 }