annotate libpam-sys/libpam-sys-impls/build.rs @ 116:a12706e42c9d

Logging, macros, and building: - Changes logging API to accept the `Location` of the log statement. Fixes OpenPAM implementation. - Stops publicly exporting doc macros. - Uses dlopen to detect the PAM library rather than header jankery.
author Paul Fisher <paul@pfish.zone>
date Sun, 29 Jun 2025 18:27:51 -0400
parents 178310336596
children 39760dfc9b3b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
1 //! This absurd build script basically sets up everything for libpam-sys-impl.
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
2 //!
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
3 //! 1. It's the definition site for the [`PamImpl`] enum, which then gets
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
4 //! output to the `OUT_DIR/pam_impl_enum.rs` file for parsing/inclusion
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
5 //! into the `__pam_impl_enum__` macro.
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
6 //! 2. It detects the current PAM implementation and sets an env var for
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
7 //! the macros in `libpam-sys-impl`.
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
8
113
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 110
diff changeset
9 use proc_macro2::TokenStream;
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 110
diff changeset
10 use quote::quote;
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
11 use std::{env, fs};
116
a12706e42c9d Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
12 use std::ffi::c_void;
108
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13 use strum::EnumString;
116
a12706e42c9d Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
14 use dlopen::raw::Library;
108
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 fn main() {
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 let pam_impl = match option_env!("LIBPAMSYS_IMPL") {
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18 // The default option: Guess what PAM impl we're using based on OS.
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 None => {
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 if cfg!(target_os = "linux") {
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 PamImpl::LinuxPam
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22 } else if cfg!(any(
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 target_os = "macos",
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 target_os = "freebsd",
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25 target_os = "netbsd",
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26 target_os = "dragonfly",
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
27 target_os = "openbsd"
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
28 )) {
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
29 PamImpl::OpenPam
113
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 110
diff changeset
30 } else if cfg!(any(target_os = "illumos", target_os = "solaris",)) {
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
31 PamImpl::Sun
108
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
32 } else {
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
33 PamImpl::MinimalOpenPam
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34 }
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
35 }
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
36 Some("_detect") => {
116
a12706e42c9d Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
37 // Detect what library we're using based on the symbols.
a12706e42c9d Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
38 let lib = Library::open("libpam.so").unwrap();
a12706e42c9d Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
39 if symbol_exists(&lib, "pam_syslog") {
108
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
40 PamImpl::LinuxPam
116
a12706e42c9d Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
41 } else if symbol_exists(&lib, "_openpam_log") {
108
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
42 PamImpl::OpenPam
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
43 } else if header_exists("security/pam_appl.h") {
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
44 // We figure we're *probably* on a Sun derivative.
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
45 PamImpl::Sun
108
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 } else {
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
47 // If all else fails, assume the bare minimum.
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
48 PamImpl::MinimalOpenPam
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
49 }
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
50 }
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
51 Some(other) => match PamImpl::try_from(other) {
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
52 Ok(i) => i,
109
bb465393621f Minor cleanup and reorg.
Paul Fisher <paul@pfish.zone>
parents: 108
diff changeset
53 Err(_) => panic!("unknown PAM implementation {other:?}"),
bb465393621f Minor cleanup and reorg.
Paul Fisher <paul@pfish.zone>
parents: 108
diff changeset
54 },
108
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55 };
113
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 110
diff changeset
56 fs::write(
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 110
diff changeset
57 format!("{}/pam_impl_enum.rs", env::var("OUT_DIR").unwrap()),
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 110
diff changeset
58 PamImpl::enum_tokens().to_string(),
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 110
diff changeset
59 )
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 110
diff changeset
60 .unwrap();
108
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 println!("cargo:rustc-env=LIBPAMSYS_IMPL={pam_impl:?}");
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 }
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63
116
a12706e42c9d Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
64 fn symbol_exists(lib: &Library, symbol: &str) -> bool {
a12706e42c9d Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
65 unsafe { lib.symbol::<*mut c_void>(symbol) }.is_ok()
a12706e42c9d Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
66 }
a12706e42c9d Logging, macros, and building:
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
67
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
68 /// This defines a local enum with an `enum_tokens()` method that can spit out
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
69 /// its own contents.
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
70 macro_rules! self_aware_enum {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
71 (
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
72 $(#here[$here:meta])*
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
73 $(#[$attr:meta])*
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
74 $name:ident {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
75 $($tt:tt)*
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
76 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
77 ) => {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
78 $(#[$here])*
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
79 $(#[$attr])*
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
80 pub enum $name {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
81 $($tt)*
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
82 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
83
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
84 impl $name {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
85 fn enum_tokens() -> TokenStream {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
86 quote!(
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
87 $(#[$attr])*
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
88 pub enum $name {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
89 $($tt)*
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
90 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
91 )
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
92 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
93 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
94 }
108
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
95 }
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
96
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
97 self_aware_enum!(
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
98 #here[derive(EnumString, strum::Display)]
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
99 /// The PAM implementations supported by `libpam-sys`.
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
100 #[derive(Clone, Copy, Debug, PartialEq)]
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
101 PamImpl {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
102 /// [Linux-PAM] is provided by most Linux distributions.
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
103 ///
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
104 /// [Linux-PAM]: https://github.com/linux-pam/linux-pam
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
105 LinuxPam,
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
106 /// [OpenPAM] is used by most BSDs, including Mac OS X.
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
107 ///
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
108 /// [OpenPAM]: https://git.des.dev/OpenPAM/OpenPAM
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
109 OpenPam,
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
110 /// Illumos and Solaris use a derivative of [Sun's implementation][sun].
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
111 ///
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
112 /// [sun]: https://code.illumos.org/plugins/gitiles/illumos-gate/+/refs/heads/master/usr/src/lib/libpam
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
113 Sun,
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
114 /// Only the functionality in [the PAM spec], with OpenPAM/Sun consts.
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
115 ///
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
116 /// [the PAM spec]: https://pubs.opengroup.org/onlinepubs/8329799/toc.htm
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
117 MinimalOpenPam,
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
118 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
119 );
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents: 109
diff changeset
120
108
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
121 fn header_exists(header: &str) -> bool {
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
122 bindgen::Builder::default()
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
123 .blocklist_item(".*")
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
124 .header_contents("header.h", &format!("#include <{header}>"))
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
125 .generate()
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
126 .is_ok()
e97534be35e3 Make some proc macros for doing cfg-like stuff for PAM impls.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
127 }