comparison libpam-sys/libpam-sys-test/build.rs @ 121:397743cb70e2

Make libpam-sys-tests work on Illumos!
author Paul Fisher <paul@pfish.zone>
date Mon, 30 Jun 2025 03:43:48 -0400
parents 39760dfc9b3b
children 9e05e44050d0
comparison
equal deleted inserted replaced
120:0f913ec120ac 121:397743cb70e2
1 use bindgen::MacroTypeVariation; 1 use bindgen::MacroTypeVariation;
2 use libpam_sys_impls::cfg_pam_impl; 2 use libpam_sys::PamImpl;
3 use quote::{format_ident, ToTokens}; 3 use quote::{format_ident, ToTokens};
4 use std::path::PathBuf; 4 use std::path::PathBuf;
5 use std::{env, fs}; 5 use std::{env, fs};
6 use syn::{Item, ItemConst, Type, TypePath}; 6 use syn::{Item, ItemConst, Type, TypePath};
7 7
8 fn main() { 8 fn main() {
9 generate_const_test(); 9 let config = match PamImpl::CURRENT {
10 PamImpl::LinuxPam => TestConfig {
11 headers: vec![
12 "security/_pam_types.h".into(),
13 "security/pam_appl.h".into(),
14 "security/pam_ext.h".into(),
15 "security/pam_modules.h".into(),
16 ],
17 ignore_consts: vec![
18 "__LINUX_PAM__".into(),
19 "__LINUX_PAM_MINOR__".into(),
20 "PAM_AUTHTOK_RECOVER_ERR".into(),
21 ],
22 ..Default::default()
23 },
24 PamImpl::OpenPam => TestConfig {
25 headers: vec![
26 "security/pam_types.h".into(),
27 "security/openpam.h".into(),
28 "security/pam_appl.h".into(),
29 "security/pam_constants.h".into(),
30 ],
31 ignore_consts: vec![
32 "OPENPAM_VERSION".into(),
33 "OPENPAM_RELEASE".into(),
34 "PAM_SOEXT".into(),
35 ],
36 ..Default::default()
37 },
38 PamImpl::Sun => TestConfig {
39 headers: vec![
40 "security/pam_appl.h".into(),
41 "security/pam_modules.h".into(),
42 ],
43 block_headers: vec!["sys/types.h".into()],
44 ..Default::default()
45 },
46 PamImpl::XSso => Default::default(),
47 other => panic!("Unknown PAM implementation {other:?}"),
48 };
49 generate_const_test(&config);
10 } 50 }
11 51
12 #[cfg_pam_impl("LinuxPam")] 52 fn generate_const_test(config: &TestConfig) {
13 fn test_config() -> TestConfig { 53 let mut builder = bindgen::Builder::default()
14 TestConfig {
15 headers: vec![
16 "security/_pam_types.h".into(),
17 "security/pam_appl.h".into(),
18 "security/pam_ext.h".into(),
19 "security/pam_modules.h".into(),
20 ],
21 ignore_consts: vec![
22 "__LINUX_PAM__".into(),
23 "__LINUX_PAM_MINOR__".into(),
24 "PAM_AUTHTOK_RECOVER_ERR".into(),
25 ],
26 }
27 }
28
29 #[cfg_pam_impl("OpenPam")]
30 fn test_config() -> TestConfig {
31 TestConfig {
32 headers: vec![
33 "security/pam_types.h".into(),
34 "security/openpam.h".into(),
35 "security/pam_appl.h".into(),
36 "security/pam_constants.h".into(),
37 ],
38 ignore_consts: vec![
39 "OPENPAM_VERSION".into(),
40 "OPENPAM_RELEASE".into(),
41 "PAM_SOEXT".into(),
42 ],
43 }
44 }
45
46 #[cfg_pam_impl(not(any("LinuxPam", "OpenPam")))]
47 fn test_config() -> TestConfig {
48 panic!("This PAM implementation is not yet tested.")
49 }
50
51 fn generate_const_test() {
52 let config = test_config();
53 let builder = bindgen::Builder::default()
54 .header_contents("_.h", &config.header_contents()) 54 .header_contents("_.h", &config.header_contents())
55 .merge_extern_blocks(true) 55 .merge_extern_blocks(true)
56 .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) 56 .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
57 .blocklist_type(".*") 57 .blocklist_type(".*")
58 .blocklist_function(".*") 58 .blocklist_function(".*")
59 .allowlist_var(".*") 59 .allowlist_var(".*")
60 .default_macro_constant_type(MacroTypeVariation::Signed); 60 .default_macro_constant_type(MacroTypeVariation::Signed);
61 for hdr in config.block_headers.iter() {
62 builder = builder.blocklist_file(".*?/".to_owned() + hdr)
63 }
61 64
62 let generated = builder.generate().unwrap().to_string(); 65 let generated = builder.generate().unwrap().to_string();
63 let file = syn::parse_file(&generated).unwrap(); 66 let file = syn::parse_file(&generated).unwrap();
64 let mut tests = vec![]; 67 let mut tests = vec![];
65 tests.push("{".into()); 68 tests.push("{".into());
93 tests.join("\n"), 96 tests.join("\n"),
94 ) 97 )
95 .unwrap(); 98 .unwrap();
96 } 99 }
97 100
101 #[derive(Default)]
98 struct TestConfig { 102 struct TestConfig {
99 headers: Vec<String>, 103 headers: Vec<String>,
104 block_headers: Vec<String>,
100 ignore_consts: Vec<String>, 105 ignore_consts: Vec<String>,
101 } 106 }
102 107
103 impl TestConfig { 108 impl TestConfig {
104 fn header_contents(&self) -> String { 109 fn header_contents(&self) -> String {