mirror of
https://github.com/koverstreet/bcachefs-tools.git
synced 2025-02-09 00:00:04 +03:00
b2824ebac1
This patch addresses build issues with bch_bindgen on architectures where the natural alignment of u64 is 4 instead of 8, such as i686 and ppc64. The issue arises from rustc's refusing to compile structs with a "packed" attribute that have members with an explicit "align(N)" attribute. rust-bindgen generally does not place "align(N)" attributes on types when it is redundant with the type's natural alignment. There are a few types in bcachefs with "__aligned(8)" in C where the type's natural alignment is 8 except on architectures like those mentioned above where it is 4. rust-bindgen places the "align(8)" attribute on these types, for those architectures. The affected types include: - bch_csum - bch_sb_layout - bch_ioctl_data_progress bch_ioctl_data_progress, and all types that depend on it, are not used on the Rust side currently and bindings are only generated for them because they are covered by `.allowlist_type("bch_.*")` in bch_bindgen/build.rs. This patch resolves the build failure for this type by excluding it from bch_bindgen. If/when accessing this type in Rust is needed, a decision can be made then about how to represent its layout in Rust. bch_csum and bch_sb_layout, and types that depend on them, are currently used in Rust so they can't be excluded. This patch addresses these types by stripping off the "packed" attribute in Rust on types that embed these types, since that attribute happens to not be necessary for proper layout. Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com> Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
164 lines
6.1 KiB
Rust
164 lines
6.1 KiB
Rust
#[derive(Debug)]
|
|
pub struct Fix753 {}
|
|
impl bindgen::callbacks::ParseCallbacks for Fix753 {
|
|
fn item_name(&self, original_item_name: &str) -> Option<String> {
|
|
Some(original_item_name.trim_start_matches("Fix753_").to_owned())
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
use std::path::PathBuf;
|
|
|
|
println!("cargo:rerun-if-changed=src/libbcachefs_wrapper.h");
|
|
|
|
let out_dir: PathBuf = std::env::var_os("OUT_DIR")
|
|
.expect("ENV Var 'OUT_DIR' Expected")
|
|
.into();
|
|
let top_dir: PathBuf = std::env::var_os("CARGO_MANIFEST_DIR")
|
|
.expect("ENV Var 'CARGO_MANIFEST_DIR' Expected")
|
|
.into();
|
|
|
|
let urcu = pkg_config::probe_library("liburcu").expect("Failed to find urcu lib");
|
|
let bindings = bindgen::builder()
|
|
.formatter(bindgen::Formatter::Prettyplease)
|
|
.header(
|
|
top_dir
|
|
.join("src")
|
|
.join("libbcachefs_wrapper.h")
|
|
.display()
|
|
.to_string(),
|
|
)
|
|
.clang_args(
|
|
urcu
|
|
.include_paths
|
|
.iter()
|
|
.map(|p| format!("-I{}", p.display())),
|
|
)
|
|
.clang_arg("-I..")
|
|
.clang_arg("-I../c_src")
|
|
.clang_arg("-I../include")
|
|
.clang_arg("-DZSTD_STATIC_LINKING_ONLY")
|
|
.clang_arg("-DNO_BCACHEFS_FS")
|
|
.clang_arg("-D_GNU_SOURCE")
|
|
.clang_arg("-DRUST_BINDGEN")
|
|
.clang_arg("-fkeep-inline-functions")
|
|
.derive_debug(true)
|
|
.derive_default(true)
|
|
.layout_tests(true)
|
|
.default_enum_style(bindgen::EnumVariation::Rust {
|
|
non_exhaustive: true,
|
|
})
|
|
.allowlist_function("bcachefs_usage")
|
|
.allowlist_function("raid_init")
|
|
.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")
|
|
.allowlist_function("add_key")
|
|
.allowlist_function("keyctl_search")
|
|
.allowlist_function("match_string")
|
|
.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_.*")
|
|
.allowlist_var("bch.*")
|
|
.allowlist_var("__bch2.*")
|
|
.allowlist_var("__BTREE_ITER.*")
|
|
.allowlist_var("BTREE_ITER.*")
|
|
.blocklist_item("bch2_bkey_ops")
|
|
.allowlist_type("bch_.*")
|
|
.allowlist_type("fsck_err_opts")
|
|
.rustified_enum("fsck_err_opts")
|
|
.allowlist_type("nonce")
|
|
.no_debug("bch_replicas_padded")
|
|
.newtype_enum("bch_kdf_types")
|
|
.rustified_enum("bch_key_types")
|
|
.opaque_type("gendisk")
|
|
.opaque_type("gc_stripe")
|
|
.opaque_type("open_bucket.*")
|
|
.opaque_type("replicas_delta_list")
|
|
.no_copy("btree_trans")
|
|
.no_copy("printbuf")
|
|
.no_partialeq("bkey")
|
|
.no_partialeq("bpos")
|
|
.generate_inline_functions(true)
|
|
.parse_callbacks(Box::new(Fix753 {}))
|
|
.generate()
|
|
.expect("BindGen Generation Failiure: [libbcachefs_wrapper]");
|
|
|
|
std::fs::write(
|
|
out_dir.join("bcachefs.rs"),
|
|
packed_and_align_fix(bindings.to_string()),
|
|
)
|
|
.expect("Writing to output file failed for: `bcachefs.rs`");
|
|
|
|
let keyutils = pkg_config::probe_library("libkeyutils").expect("Failed to find keyutils lib");
|
|
let bindings = bindgen::builder()
|
|
.header(
|
|
top_dir
|
|
.join("src")
|
|
.join("keyutils_wrapper.h")
|
|
.display()
|
|
.to_string(),
|
|
)
|
|
.clang_args(
|
|
keyutils
|
|
.include_paths
|
|
.iter()
|
|
.map(|p| format!("-I{}", p.display())),
|
|
)
|
|
.generate()
|
|
.expect("BindGen Generation Failiure: [Keyutils]");
|
|
bindings
|
|
.write_to_file(out_dir.join("keyutils.rs"))
|
|
.expect("Writing to output file failed for: `keyutils.rs`");
|
|
}
|
|
|
|
// 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.
|
|
//
|
|
// 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
|
|
//
|
|
// 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))]\npub struct btree_node {",
|
|
"#[repr(C, align(8))]\npub struct btree_node {",
|
|
)
|
|
.replace(
|
|
"#[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))]\npub struct jset {",
|
|
"#[repr(C, align(8))]\npub struct jset {",
|
|
)
|
|
.replace(
|
|
"#[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 {",
|
|
)
|
|
}
|