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())
10 use std::path::PathBuf;
12 println!("cargo:rerun-if-changed=src/libbcachefs_wrapper.h");
14 let out_dir: PathBuf = std::env::var_os("OUT_DIR")
15 .expect("ENV Var 'OUT_DIR' Expected")
17 let top_dir: PathBuf = std::env::var_os("CARGO_MANIFEST_DIR")
18 .expect("ENV Var 'CARGO_MANIFEST_DIR' Expected")
21 let urcu = pkg_config::probe_library("liburcu").expect("Failed to find urcu lib");
22 let bindings = bindgen::builder()
23 .formatter(bindgen::Formatter::Prettyplease)
27 .join("libbcachefs_wrapper.h")
35 .map(|p| format!("-I{}", p.display())),
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")
48 .default_enum_style(bindgen::EnumVariation::Rust {
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 .allowlist_var("BCH_.*")
68 .allowlist_var("KEY_SPEC_.*")
69 .allowlist_var("Fix753_.*")
70 .allowlist_var("bch.*")
71 .allowlist_var("__bch2.*")
72 .allowlist_var("__BTREE_ITER.*")
73 .allowlist_var("BTREE_ITER.*")
74 .blocklist_item("bch2_bkey_ops")
75 .allowlist_type("bch_.*")
76 .allowlist_type("fsck_err_opts")
77 .rustified_enum("fsck_err_opts")
78 .allowlist_type("nonce")
79 .no_debug("bch_replicas_padded")
80 .newtype_enum("bch_kdf_types")
81 .rustified_enum("bch_key_types")
82 .opaque_type("gendisk")
83 .opaque_type("gc_stripe")
84 .opaque_type("open_bucket.*")
85 .opaque_type("replicas_delta_list")
86 .no_copy("btree_trans")
90 .generate_inline_functions(true)
91 .parse_callbacks(Box::new(Fix753 {}))
93 .expect("BindGen Generation Failiure: [libbcachefs_wrapper]");
96 out_dir.join("bcachefs.rs"),
97 packed_and_align_fix(bindings.to_string()),
99 .expect("Writing to output file failed for: `bcachefs.rs`");
101 let keyutils = pkg_config::probe_library("libkeyutils").expect("Failed to find keyutils lib");
102 let bindings = bindgen::builder()
106 .join("keyutils_wrapper.h")
114 .map(|p| format!("-I{}", p.display())),
117 .expect("BindGen Generation Failiure: [Keyutils]");
119 .write_to_file(out_dir.join("keyutils.rs"))
120 .expect("Writing to output file failed for: `keyutils.rs`");
123 // rustc has a limitation where it does not allow structs to have both a "packed" and "align"
124 // attribute. This means that bindgen cannot copy all attributes from some C types, like struct
125 // bkey, that are both packed and aligned.
127 // bindgen tries to handle this situation smartly and for many types it will only apply a
128 // "packed(N)" attribute if that is good enough. However, there are a few types where bindgen
129 // does end up generating both a packed(N) and align(N) attribute. These types can't be compiled
132 // To work around this, we can remove either the "packed" or "align" attribute. It happens that
133 // for all the types with this problem in bcachefs, removing the "packed" attribute and keeping
134 // the "align" attribute results in a type with the correct ABI.
136 // This function applies that transformation to the following bcachefs types that need it:
138 // - bch_extent_crc32
141 fn packed_and_align_fix(bindings: std::string::String) -> std::string::String {
144 "#[repr(C, packed(8))]\n#[repr(align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bkey {",
145 "#[repr(C, align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bkey {",
148 "#[repr(C, packed(8))]\n#[repr(align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_crc32 {",
149 "#[repr(C, align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_crc32 {",
152 "#[repr(C, packed(8))]\n#[repr(align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_ptr {",
153 "#[repr(C, align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_ptr {",
156 "#[repr(C, packed(8))]\npub struct btree_node {",
157 "#[repr(C, align(8))]\npub struct btree_node {",