]> git.sesse.net Git - bcachefs-tools-debian/blob - bch_bindgen/build.rs
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[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("bcache_fs_open")
57         .allowlist_function("bcache_fs_close")
58         .allowlist_function("bio_.*")
59         .allowlist_function("derive_passphrase")
60         .allowlist_function("request_key")
61         .allowlist_function("add_key")
62         .allowlist_function("keyctl_search")
63         .allowlist_function("match_string")
64         .allowlist_function("printbuf.*")
65         .blocklist_type("rhash_lock_head")
66         .blocklist_type("srcu_struct")
67         .blocklist_type("bch_ioctl_data.*")
68         .allowlist_var("BCH_.*")
69         .allowlist_var("KEY_SPEC_.*")
70         .allowlist_var("Fix753_.*")
71         .allowlist_var("bch.*")
72         .allowlist_var("__bch2.*")
73         .allowlist_var("__BTREE_ITER.*")
74         .allowlist_var("BTREE_ITER.*")
75         .blocklist_item("bch2_bkey_ops")
76         .allowlist_type("bch_.*")
77         .allowlist_type("fsck_err_opts")
78         .rustified_enum("fsck_err_opts")
79         .allowlist_type("nonce")
80         .no_debug("bch_replicas_padded")
81         .newtype_enum("bch_kdf_types")
82         .rustified_enum("bch_key_types")
83         .opaque_type("gendisk")
84         .opaque_type("gc_stripe")
85         .opaque_type("open_bucket.*")
86         .opaque_type("replicas_delta_list")
87         .no_copy("btree_trans")
88         .no_copy("printbuf")
89         .no_partialeq("bkey")
90         .no_partialeq("bpos")
91         .generate_inline_functions(true)
92         .parse_callbacks(Box::new(Fix753 {}))
93         .generate()
94         .expect("BindGen Generation Failiure: [libbcachefs_wrapper]");
95
96     std::fs::write(
97         out_dir.join("bcachefs.rs"),
98         packed_and_align_fix(bindings.to_string()),
99     )
100     .expect("Writing to output file failed for: `bcachefs.rs`");
101
102     let keyutils = pkg_config::probe_library("libkeyutils").expect("Failed to find keyutils lib");
103     let bindings = bindgen::builder()
104         .header(
105             top_dir
106                 .join("src")
107                 .join("keyutils_wrapper.h")
108                 .display()
109                 .to_string(),
110         )
111         .clang_args(
112             keyutils
113                 .include_paths
114                 .iter()
115                 .map(|p| format!("-I{}", p.display())),
116         )
117         .generate()
118         .expect("BindGen Generation Failiure: [Keyutils]");
119     bindings
120         .write_to_file(out_dir.join("keyutils.rs"))
121         .expect("Writing to output file failed for: `keyutils.rs`");
122 }
123
124 // rustc has a limitation where it does not allow structs with a "packed" attribute to contain a
125 // member with an "align(N)" attribute. There are a few types in bcachefs with this problem. We can
126 // "fix" these types by stripping off "packed" from the outer type, or "align(N)" from the inner
127 // type. For all of the affected types, stripping "packed" from the outer type happens to preserve
128 // the same layout in Rust as in C.
129 //
130 // Some types are only affected on attributes on architectures where the natural alignment of u64
131 // is 4 instead of 8, for example i686 or ppc64: struct bch_csum and struct bch_sb_layout have
132 // "align(8)" added on such architecutres. These types are included by several "packed" types:
133 //   - bch_extent_crc128
134 //   - jset
135 //   - btree_node_entry
136 //   - bch_sb
137 //
138 // TODO: find a way to conditionally include arch-specific modifications when compiling for that
139 // target arch. Regular conditional compilation won't work here since build scripts are always
140 // compiled for the host arch, not the target arch, so that won't work when cross-compiling.
141 fn packed_and_align_fix(bindings: std::string::String) -> std::string::String {
142     bindings
143         .replace(
144             "#[repr(C, packed(8))]\npub struct btree_node {",
145             "#[repr(C, align(8))]\npub struct btree_node {",
146         )
147         .replace(
148             "#[repr(C, packed(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_crc128 {",
149             "#[repr(C, align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_crc128 {",
150         )
151         .replace(
152             "#[repr(C, packed(8))]\npub struct jset {",
153             "#[repr(C, align(8))]\npub struct jset {",
154         )
155         .replace(
156             "#[repr(C, packed(8))]\npub struct btree_node_entry {",
157             "#[repr(C, align(8))]\npub struct btree_node_entry {",
158         )
159         .replace(
160             "#[repr(C, packed(8))]\npub struct bch_sb {",
161             "#[repr(C, align(8))]\npub struct bch_sb {",
162         )
163 }