]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - 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
index 70f03076afbd5444d2e3f57dce02ecb319b0d3d0..0329f1aed039a0ba2f20b7c962f312697e815bd4 100644 (file)
@@ -53,6 +53,8 @@ fn main() {
         .allowlist_function("cmd_.*")
         .allowlist_function(".*_cmds")
         .allowlist_function(".*bch2_.*")
+        .allowlist_function("bcache_fs_open")
+        .allowlist_function("bcache_fs_close")
         .allowlist_function("bio_.*")
         .allowlist_function("derive_passphrase")
         .allowlist_function("request_key")
@@ -62,6 +64,7 @@ fn main() {
         .allowlist_function("printbuf.*")
         .blocklist_type("rhash_lock_head")
         .blocklist_type("srcu_struct")
+        .blocklist_type("bch_ioctl_data.*")
         .allowlist_var("BCH_.*")
         .allowlist_var("KEY_SPEC_.*")
         .allowlist_var("Fix753_.*")
@@ -118,40 +121,43 @@ fn main() {
         .expect("Writing to output file failed for: `keyutils.rs`");
 }
 
-// rustc has a limitation where it does not allow structs to have both a "packed" and "align"
-// attribute. This means that bindgen cannot copy all attributes from some C types, like struct
-// bkey, that are both packed and aligned.
+// rustc has a limitation where it does not allow structs with a "packed" attribute to contain a
+// member with an "align(N)" attribute. There are a few types in bcachefs with this problem. We can
+// "fix" these types by stripping off "packed" from the outer type, or "align(N)" from the inner
+// type. For all of the affected types, stripping "packed" from the outer type happens to preserve
+// the same layout in Rust as in C.
 //
-// bindgen tries to handle this situation smartly and for many types it will only apply a
-// "packed(N)" attribute if that is good enough. However, there are a few types where bindgen
-// does end up generating both a packed(N) and align(N) attribute. These types can't be compiled
-// by rustc.
+// Some types are only affected on attributes on architectures where the natural alignment of u64
+// is 4 instead of 8, for example i686 or ppc64: struct bch_csum and struct bch_sb_layout have
+// "align(8)" added on such architecutres. These types are included by several "packed" types:
+//   - bch_extent_crc128
+//   - jset
+//   - btree_node_entry
+//   - bch_sb
 //
-// To work around this, we can remove either the "packed" or "align" attribute. It happens that
-// for all the types with this problem in bcachefs, removing the "packed" attribute and keeping
-// the "align" attribute results in a type with the correct ABI.
-//
-// This function applies that transformation to the following bcachefs types that need it:
-//   - bkey
-//   - bch_extent_crc32
-//   - bch_extent_ptr
-//   - btree_node
+// TODO: find a way to conditionally include arch-specific modifications when compiling for that
+// target arch. Regular conditional compilation won't work here since build scripts are always
+// compiled for the host arch, not the target arch, so that won't work when cross-compiling.
 fn packed_and_align_fix(bindings: std::string::String) -> std::string::String {
     bindings
         .replace(
-            "#[repr(C, packed(8))]\n#[repr(align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bkey {",
-            "#[repr(C, align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bkey {",
+            "#[repr(C, packed(8))]\npub struct btree_node {",
+            "#[repr(C, align(8))]\npub struct btree_node {",
         )
         .replace(
-            "#[repr(C, packed(8))]\n#[repr(align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_crc32 {",
-            "#[repr(C, align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_crc32 {",
+            "#[repr(C, packed(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_crc128 {",
+            "#[repr(C, align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_crc128 {",
         )
         .replace(
-            "#[repr(C, packed(8))]\n#[repr(align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_ptr {",
-            "#[repr(C, align(8))]\n#[derive(Debug, Default, Copy, Clone)]\npub struct bch_extent_ptr {",
+            "#[repr(C, packed(8))]\npub struct jset {",
+            "#[repr(C, align(8))]\npub struct jset {",
         )
         .replace(
-            "#[repr(C, packed(8))]\npub struct btree_node {",
-            "#[repr(C, align(8))]\npub struct btree_node {",
+            "#[repr(C, packed(8))]\npub struct btree_node_entry {",
+            "#[repr(C, align(8))]\npub struct btree_node_entry {",
+        )
+        .replace(
+            "#[repr(C, packed(8))]\npub struct bch_sb {",
+            "#[repr(C, align(8))]\npub struct bch_sb {",
         )
 }