]> git.sesse.net Git - bcachefs-tools-debian/blob - bch_bindgen/build.rs
fix(bindgen): expand the Fix753 workaround for any type and document it
[bcachefs-tools-debian] / bch_bindgen / build.rs
1 #[derive(Debug)]
2 pub struct Fix753 {}
3 impl bindgen::callbacks::ParseCallbacks for Fix753 {
4     fn item_name(&self, original_item_name: &str) -> Option<String> {
5         Some(original_item_name.trim_start_matches("Fix753_").to_owned())
6     }
7 }
8
9 fn main() {
10     use std::path::PathBuf;
11
12     println!("cargo:rerun-if-changed=src/libbcachefs_wrapper.h");
13
14     let out_dir: PathBuf = std::env::var_os("OUT_DIR")
15         .expect("ENV Var 'OUT_DIR' Expected")
16         .into();
17     let top_dir: PathBuf = std::env::var_os("CARGO_MANIFEST_DIR")
18         .expect("ENV Var 'CARGO_MANIFEST_DIR' Expected")
19         .into();
20
21     let urcu = pkg_config::probe_library("liburcu").expect("Failed to find urcu lib");
22     let bindings = bindgen::builder()
23         .formatter(bindgen::Formatter::Prettyplease)
24         .header(
25             top_dir
26                 .join("src")
27                 .join("libbcachefs_wrapper.h")
28                 .display()
29                 .to_string(),
30         )
31         .clang_args(
32             urcu
33                 .include_paths
34                 .iter()
35                 .map(|p| format!("-I{}", p.display())),
36         )
37         .clang_arg("-I..")
38         .clang_arg("-I../c_src")
39         .clang_arg("-I../include")
40         .clang_arg("-DZSTD_STATIC_LINKING_ONLY")
41         .clang_arg("-DNO_BCACHEFS_FS")
42         .clang_arg("-D_GNU_SOURCE")
43         .clang_arg("-DRUST_BINDGEN")
44         .clang_arg("-fkeep-inline-functions")
45         .derive_debug(true)
46         .derive_default(true)
47         .layout_tests(true)
48         .default_enum_style(bindgen::EnumVariation::Rust {
49             non_exhaustive: true,
50         })
51         .allowlist_function("bcachefs_usage")
52         .allowlist_function("raid_init")
53         .allowlist_function("cmd_.*")
54         .allowlist_function(".*_cmds")
55         .allowlist_function(".*bch2_.*")
56         .allowlist_function("bio_.*")
57         .allowlist_function("derive_passphrase")
58         .allowlist_function("request_key")
59         .allowlist_function("add_key")
60         .allowlist_function("keyctl_search")
61         .allowlist_function("match_string")
62         .allowlist_function("printbuf.*")
63         .blocklist_type("rhash_lock_head")
64         .blocklist_type("srcu_struct")
65         .allowlist_var("BCH_.*")
66         .allowlist_var("KEY_SPEC_.*")
67         .allowlist_var("Fix753_.*")
68         .allowlist_var("bch.*")
69         .allowlist_var("__bch2.*")
70         .allowlist_var("__BTREE_ITER.*")
71         .allowlist_var("BTREE_ITER.*")
72         .blocklist_item("bch2_bkey_ops")
73         .allowlist_type("bch_.*")
74         .allowlist_type("fsck_err_opts")
75         .rustified_enum("fsck_err_opts")
76         .allowlist_type("nonce")
77         .no_debug("bch_replicas_padded")
78         .newtype_enum("bch_kdf_types")
79         .rustified_enum("bch_key_types")
80         .opaque_type("gendisk")
81         .opaque_type("gc_stripe")
82         .opaque_type("open_bucket.*")
83         .opaque_type("replicas_delta_list")
84         .no_copy("btree_trans")
85         .no_copy("printbuf")
86         .no_partialeq("bkey")
87         .no_partialeq("bpos")
88         .generate_inline_functions(true)
89         .parse_callbacks(Box::new(Fix753 {}))
90         .generate()
91         .expect("BindGen Generation Failiure: [libbcachefs_wrapper]");
92
93     std::fs::write(
94         out_dir.join("bcachefs.rs"),
95         packed_and_align_fix(bindings.to_string()),
96     )
97     .expect("Writing to output file failed for: `bcachefs.rs`");
98
99     let keyutils = pkg_config::probe_library("libkeyutils").expect("Failed to find keyutils lib");
100     let bindings = bindgen::builder()
101         .header(
102             top_dir
103                 .join("src")
104                 .join("keyutils_wrapper.h")
105                 .display()
106                 .to_string(),
107         )
108         .clang_args(
109             keyutils
110                 .include_paths
111                 .iter()
112                 .map(|p| format!("-I{}", p.display())),
113         )
114         .generate()
115         .expect("BindGen Generation Failiure: [Keyutils]");
116     bindings
117         .write_to_file(out_dir.join("keyutils.rs"))
118         .expect("Writing to output file failed for: `keyutils.rs`");
119 }
120
121 // rustc has a limitation where it does not allow structs to have both a "packed" and "align"
122 // attribute. This means that bindgen cannot copy all attributes from some C types, like struct
123 // bkey, that are both packed and aligned.
124 //
125 // bindgen tries to handle this situation smartly and for many types it will only apply a
126 // "packed(N)" attribute if that is good enough. However, there are a few types where bindgen
127 // does end up generating both a packed(N) and align(N) attribute. These types can't be compiled
128 // by rustc.
129 //
130 // To work around this, we can remove either the "packed" or "align" attribute. It happens that
131 // for all the types with this problem in bcachefs, removing the "packed" attribute and keeping
132 // the "align" attribute results in a type with the correct ABI.
133 //
134 // This function applies that transformation to the following bcachefs types that need it:
135 //   - bkey
136 //   - bch_extent_crc32
137 //   - bch_extent_ptr
138 //   - btree_node
139 fn packed_and_align_fix(bindings: std::string::String) -> std::string::String {
140     bindings
141         .replace(
142             "#[repr(C, packed(8))]\n#[repr(align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bkey {",
143             "#[repr(C, align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bkey {",
144         )
145         .replace(
146             "#[repr(C, packed(8))]\n#[repr(align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_crc32 {",
147             "#[repr(C, align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_crc32 {",
148         )
149         .replace(
150             "#[repr(C, packed(8))]\n#[repr(align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_ptr {",
151             "#[repr(C, align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_ptr {",
152         )
153         .replace(
154             "#[repr(C, packed(8))]\npub struct btree_node {",
155             "#[repr(C, align(8))]\npub struct btree_node {",
156         )
157 }