2023-04-27 21:02:00 +03:00
|
|
|
#[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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:27:51 +03:00
|
|
|
fn main() {
|
2023-01-16 11:22:49 +03:00
|
|
|
use std::path::PathBuf;
|
2021-10-18 20:27:51 +03:00
|
|
|
|
2024-01-16 09:41:00 +03:00
|
|
|
println!("cargo:rerun-if-changed=src/libbcachefs_wrapper.h");
|
|
|
|
|
2023-01-16 11:22:49 +03:00
|
|
|
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();
|
2021-10-18 20:27:51 +03:00
|
|
|
|
2024-01-21 14:04:31 +03:00
|
|
|
let urcu = pkg_config::probe_library("liburcu").expect("Failed to find urcu lib");
|
2023-01-16 11:22:49 +03:00
|
|
|
let bindings = bindgen::builder()
|
use upstream bindgen; fix packed and aligned types
bcachefs-tools has been using a patched bindgen to work around a
limitation of rustc that prevents compiling structs with
both #[repr(packed(N)] and #[repr(align(N)] attributes. The patch:
e8168ceda507 "codegen: Don't generate conflicting packed() and align()
representation hints." discards the "align" attribute in cases where
bindgen produces a type with both.
This may be correct for some types, but it turns out that for each
bcachefs type with this problem, keeping the "align" attribute and
discarding the "packed" attribute generates a type with the same ABI as
the original C type.
This can be tested automatically by running:
$ cargo test --manifest-path bch_bindgen/Cargo.toml
in the bcachefs-tools tree.
There has been pressure recently to start using upstream bindgen; both
externally, from distribution maintainers who want to build
bcachefs-tools with standard dependencies, and internally, in order to
enable using Rust for bcachefs in-kernel.
This patch updates bcachefs-tools to use upstream bindgen. It works
around the rustc limitation with a post-processing step in the bindgen
build that adjusts the attributes to include "#[repr(C, align(N))]" and
exclude #[repr(packed(N)] only for the 4 types that need it. It also
updates bch_bindgen to format the code with prettyplease so that this
will work even in environments with rustfmt installed.
Some types that had been manually implemented in
bch_bindgen/src/bcachefs.rs are now automatically generated by bindgen,
so that they will be covered by the ABI compatibility testing mentioned
above.
I intentionally targeted the post-processing to the exact 4 types with
the issue currently, so that any changes to bcachefs that result in this
issue appearing for a new type will require manual intervention. I
figured any such changes should require careful consideration.
Ideally, bindgen can be updated to handle situations where "align(N)"
is needed and "packed(N)" can be safely discarded. If a patch for this
is accepted into bindgen, the post-processing hack can be removed.
I update the minimum Rust version to 1.70 as this is needed to build
recent versions of some dependencies.
Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2024-01-23 17:36:02 +03:00
|
|
|
.formatter(bindgen::Formatter::Prettyplease)
|
2023-01-16 11:22:49 +03:00
|
|
|
.header(
|
|
|
|
top_dir
|
|
|
|
.join("src")
|
|
|
|
.join("libbcachefs_wrapper.h")
|
|
|
|
.display()
|
|
|
|
.to_string(),
|
|
|
|
)
|
2024-01-21 14:04:31 +03:00
|
|
|
.clang_args(
|
2024-05-27 03:38:08 +03:00
|
|
|
urcu.include_paths
|
2024-01-21 14:04:31 +03:00
|
|
|
.iter()
|
|
|
|
.map(|p| format!("-I{}", p.display())),
|
|
|
|
)
|
2024-01-17 01:00:02 +03:00
|
|
|
.clang_arg("-I..")
|
|
|
|
.clang_arg("-I../c_src")
|
|
|
|
.clang_arg("-I../include")
|
2023-01-16 11:22:49 +03:00
|
|
|
.clang_arg("-DZSTD_STATIC_LINKING_ONLY")
|
|
|
|
.clang_arg("-DNO_BCACHEFS_FS")
|
|
|
|
.clang_arg("-D_GNU_SOURCE")
|
2023-12-22 03:49:55 +03:00
|
|
|
.clang_arg("-DRUST_BINDGEN")
|
2023-02-27 05:38:12 +03:00
|
|
|
.clang_arg("-fkeep-inline-functions")
|
2023-01-16 11:22:49 +03:00
|
|
|
.derive_debug(true)
|
|
|
|
.derive_default(true)
|
|
|
|
.layout_tests(true)
|
|
|
|
.default_enum_style(bindgen::EnumVariation::Rust {
|
|
|
|
non_exhaustive: true,
|
|
|
|
})
|
2024-01-16 09:41:00 +03:00
|
|
|
.allowlist_function("bcachefs_usage")
|
|
|
|
.allowlist_function("raid_init")
|
|
|
|
.allowlist_function("cmd_.*")
|
|
|
|
.allowlist_function(".*_cmds")
|
2023-01-16 11:22:49 +03:00
|
|
|
.allowlist_function(".*bch2_.*")
|
2024-01-27 05:19:28 +03:00
|
|
|
.allowlist_function("bcache_fs_open")
|
|
|
|
.allowlist_function("bcache_fs_close")
|
2023-01-16 11:22:49 +03:00
|
|
|
.allowlist_function("bio_.*")
|
|
|
|
.allowlist_function("derive_passphrase")
|
|
|
|
.allowlist_function("request_key")
|
|
|
|
.allowlist_function("add_key")
|
|
|
|
.allowlist_function("keyctl_search")
|
2023-02-27 05:38:12 +03:00
|
|
|
.allowlist_function("match_string")
|
|
|
|
.allowlist_function("printbuf.*")
|
2023-01-16 11:22:49 +03:00
|
|
|
.blocklist_type("rhash_lock_head")
|
|
|
|
.blocklist_type("srcu_struct")
|
|
|
|
.allowlist_var("BCH_.*")
|
|
|
|
.allowlist_var("KEY_SPEC_.*")
|
2024-01-27 04:34:50 +03:00
|
|
|
.allowlist_var("Fix753_.*")
|
2023-02-27 05:38:12 +03:00
|
|
|
.allowlist_var("bch.*")
|
2023-10-20 05:52:02 +03:00
|
|
|
.allowlist_var("__bch2.*")
|
2023-02-28 06:15:10 +03:00
|
|
|
.allowlist_var("__BTREE_ITER.*")
|
|
|
|
.allowlist_var("BTREE_ITER.*")
|
2023-02-27 05:38:12 +03:00
|
|
|
.blocklist_item("bch2_bkey_ops")
|
2023-02-28 10:11:05 +03:00
|
|
|
.allowlist_type("bch_.*")
|
2023-02-28 14:15:48 +03:00
|
|
|
.allowlist_type("fsck_err_opts")
|
|
|
|
.rustified_enum("fsck_err_opts")
|
2023-01-16 11:22:49 +03:00
|
|
|
.allowlist_type("nonce")
|
2023-02-28 10:11:05 +03:00
|
|
|
.no_debug("bch_replicas_padded")
|
2023-01-16 11:22:49 +03:00
|
|
|
.newtype_enum("bch_kdf_types")
|
2023-02-28 10:11:05 +03:00
|
|
|
.rustified_enum("bch_key_types")
|
2023-01-16 11:22:49 +03:00
|
|
|
.opaque_type("gendisk")
|
|
|
|
.opaque_type("gc_stripe")
|
|
|
|
.opaque_type("open_bucket.*")
|
2023-02-27 05:38:12 +03:00
|
|
|
.opaque_type("replicas_delta_list")
|
|
|
|
.no_copy("btree_trans")
|
|
|
|
.no_copy("printbuf")
|
|
|
|
.no_partialeq("bkey")
|
|
|
|
.no_partialeq("bpos")
|
|
|
|
.generate_inline_functions(true)
|
2023-04-27 21:02:00 +03:00
|
|
|
.parse_callbacks(Box::new(Fix753 {}))
|
2023-01-16 11:22:49 +03:00
|
|
|
.generate()
|
|
|
|
.expect("BindGen Generation Failiure: [libbcachefs_wrapper]");
|
use upstream bindgen; fix packed and aligned types
bcachefs-tools has been using a patched bindgen to work around a
limitation of rustc that prevents compiling structs with
both #[repr(packed(N)] and #[repr(align(N)] attributes. The patch:
e8168ceda507 "codegen: Don't generate conflicting packed() and align()
representation hints." discards the "align" attribute in cases where
bindgen produces a type with both.
This may be correct for some types, but it turns out that for each
bcachefs type with this problem, keeping the "align" attribute and
discarding the "packed" attribute generates a type with the same ABI as
the original C type.
This can be tested automatically by running:
$ cargo test --manifest-path bch_bindgen/Cargo.toml
in the bcachefs-tools tree.
There has been pressure recently to start using upstream bindgen; both
externally, from distribution maintainers who want to build
bcachefs-tools with standard dependencies, and internally, in order to
enable using Rust for bcachefs in-kernel.
This patch updates bcachefs-tools to use upstream bindgen. It works
around the rustc limitation with a post-processing step in the bindgen
build that adjusts the attributes to include "#[repr(C, align(N))]" and
exclude #[repr(packed(N)] only for the 4 types that need it. It also
updates bch_bindgen to format the code with prettyplease so that this
will work even in environments with rustfmt installed.
Some types that had been manually implemented in
bch_bindgen/src/bcachefs.rs are now automatically generated by bindgen,
so that they will be covered by the ABI compatibility testing mentioned
above.
I intentionally targeted the post-processing to the exact 4 types with
the issue currently, so that any changes to bcachefs that result in this
issue appearing for a new type will require manual intervention. I
figured any such changes should require careful consideration.
Ideally, bindgen can be updated to handle situations where "align(N)"
is needed and "packed(N)" can be safely discarded. If a patch for this
is accepted into bindgen, the post-processing hack can be removed.
I update the minimum Rust version to 1.70 as this is needed to build
recent versions of some dependencies.
Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2024-01-23 17:36:02 +03:00
|
|
|
|
|
|
|
std::fs::write(
|
|
|
|
out_dir.join("bcachefs.rs"),
|
|
|
|
packed_and_align_fix(bindings.to_string()),
|
|
|
|
)
|
|
|
|
.expect("Writing to output file failed for: `bcachefs.rs`");
|
2021-10-18 20:27:51 +03:00
|
|
|
|
2023-01-16 11:22:49 +03:00
|
|
|
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`");
|
2021-10-18 20:27:51 +03:00
|
|
|
}
|
use upstream bindgen; fix packed and aligned types
bcachefs-tools has been using a patched bindgen to work around a
limitation of rustc that prevents compiling structs with
both #[repr(packed(N)] and #[repr(align(N)] attributes. The patch:
e8168ceda507 "codegen: Don't generate conflicting packed() and align()
representation hints." discards the "align" attribute in cases where
bindgen produces a type with both.
This may be correct for some types, but it turns out that for each
bcachefs type with this problem, keeping the "align" attribute and
discarding the "packed" attribute generates a type with the same ABI as
the original C type.
This can be tested automatically by running:
$ cargo test --manifest-path bch_bindgen/Cargo.toml
in the bcachefs-tools tree.
There has been pressure recently to start using upstream bindgen; both
externally, from distribution maintainers who want to build
bcachefs-tools with standard dependencies, and internally, in order to
enable using Rust for bcachefs in-kernel.
This patch updates bcachefs-tools to use upstream bindgen. It works
around the rustc limitation with a post-processing step in the bindgen
build that adjusts the attributes to include "#[repr(C, align(N))]" and
exclude #[repr(packed(N)] only for the 4 types that need it. It also
updates bch_bindgen to format the code with prettyplease so that this
will work even in environments with rustfmt installed.
Some types that had been manually implemented in
bch_bindgen/src/bcachefs.rs are now automatically generated by bindgen,
so that they will be covered by the ABI compatibility testing mentioned
above.
I intentionally targeted the post-processing to the exact 4 types with
the issue currently, so that any changes to bcachefs that result in this
issue appearing for a new type will require manual intervention. I
figured any such changes should require careful consideration.
Ideally, bindgen can be updated to handle situations where "align(N)"
is needed and "packed(N)" can be safely discarded. If a patch for this
is accepted into bindgen, the post-processing hack can be removed.
I update the minimum Rust version to 1.70 as this is needed to build
recent versions of some dependencies.
Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2024-01-23 17:36:02 +03:00
|
|
|
|
2024-02-04 23:49:23 +03:00
|
|
|
// rustc has a limitation where it does not allow structs with a "packed" attribute to contain a
|
2024-02-17 21:20:25 +03:00
|
|
|
// 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.
|
use upstream bindgen; fix packed and aligned types
bcachefs-tools has been using a patched bindgen to work around a
limitation of rustc that prevents compiling structs with
both #[repr(packed(N)] and #[repr(align(N)] attributes. The patch:
e8168ceda507 "codegen: Don't generate conflicting packed() and align()
representation hints." discards the "align" attribute in cases where
bindgen produces a type with both.
This may be correct for some types, but it turns out that for each
bcachefs type with this problem, keeping the "align" attribute and
discarding the "packed" attribute generates a type with the same ABI as
the original C type.
This can be tested automatically by running:
$ cargo test --manifest-path bch_bindgen/Cargo.toml
in the bcachefs-tools tree.
There has been pressure recently to start using upstream bindgen; both
externally, from distribution maintainers who want to build
bcachefs-tools with standard dependencies, and internally, in order to
enable using Rust for bcachefs in-kernel.
This patch updates bcachefs-tools to use upstream bindgen. It works
around the rustc limitation with a post-processing step in the bindgen
build that adjusts the attributes to include "#[repr(C, align(N))]" and
exclude #[repr(packed(N)] only for the 4 types that need it. It also
updates bch_bindgen to format the code with prettyplease so that this
will work even in environments with rustfmt installed.
Some types that had been manually implemented in
bch_bindgen/src/bcachefs.rs are now automatically generated by bindgen,
so that they will be covered by the ABI compatibility testing mentioned
above.
I intentionally targeted the post-processing to the exact 4 types with
the issue currently, so that any changes to bcachefs that result in this
issue appearing for a new type will require manual intervention. I
figured any such changes should require careful consideration.
Ideally, bindgen can be updated to handle situations where "align(N)"
is needed and "packed(N)" can be safely discarded. If a patch for this
is accepted into bindgen, the post-processing hack can be removed.
I update the minimum Rust version to 1.70 as this is needed to build
recent versions of some dependencies.
Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2024-01-23 17:36:02 +03:00
|
|
|
//
|
2024-02-17 21:20:25 +03:00
|
|
|
// 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.
|
use upstream bindgen; fix packed and aligned types
bcachefs-tools has been using a patched bindgen to work around a
limitation of rustc that prevents compiling structs with
both #[repr(packed(N)] and #[repr(align(N)] attributes. The patch:
e8168ceda507 "codegen: Don't generate conflicting packed() and align()
representation hints." discards the "align" attribute in cases where
bindgen produces a type with both.
This may be correct for some types, but it turns out that for each
bcachefs type with this problem, keeping the "align" attribute and
discarding the "packed" attribute generates a type with the same ABI as
the original C type.
This can be tested automatically by running:
$ cargo test --manifest-path bch_bindgen/Cargo.toml
in the bcachefs-tools tree.
There has been pressure recently to start using upstream bindgen; both
externally, from distribution maintainers who want to build
bcachefs-tools with standard dependencies, and internally, in order to
enable using Rust for bcachefs in-kernel.
This patch updates bcachefs-tools to use upstream bindgen. It works
around the rustc limitation with a post-processing step in the bindgen
build that adjusts the attributes to include "#[repr(C, align(N))]" and
exclude #[repr(packed(N)] only for the 4 types that need it. It also
updates bch_bindgen to format the code with prettyplease so that this
will work even in environments with rustfmt installed.
Some types that had been manually implemented in
bch_bindgen/src/bcachefs.rs are now automatically generated by bindgen,
so that they will be covered by the ABI compatibility testing mentioned
above.
I intentionally targeted the post-processing to the exact 4 types with
the issue currently, so that any changes to bcachefs that result in this
issue appearing for a new type will require manual intervention. I
figured any such changes should require careful consideration.
Ideally, bindgen can be updated to handle situations where "align(N)"
is needed and "packed(N)" can be safely discarded. If a patch for this
is accepted into bindgen, the post-processing hack can be removed.
I update the minimum Rust version to 1.70 as this is needed to build
recent versions of some dependencies.
Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2024-01-23 17:36:02 +03:00
|
|
|
fn packed_and_align_fix(bindings: std::string::String) -> std::string::String {
|
2024-02-17 21:20:25 +03:00
|
|
|
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 {",
|
|
|
|
)
|
use upstream bindgen; fix packed and aligned types
bcachefs-tools has been using a patched bindgen to work around a
limitation of rustc that prevents compiling structs with
both #[repr(packed(N)] and #[repr(align(N)] attributes. The patch:
e8168ceda507 "codegen: Don't generate conflicting packed() and align()
representation hints." discards the "align" attribute in cases where
bindgen produces a type with both.
This may be correct for some types, but it turns out that for each
bcachefs type with this problem, keeping the "align" attribute and
discarding the "packed" attribute generates a type with the same ABI as
the original C type.
This can be tested automatically by running:
$ cargo test --manifest-path bch_bindgen/Cargo.toml
in the bcachefs-tools tree.
There has been pressure recently to start using upstream bindgen; both
externally, from distribution maintainers who want to build
bcachefs-tools with standard dependencies, and internally, in order to
enable using Rust for bcachefs in-kernel.
This patch updates bcachefs-tools to use upstream bindgen. It works
around the rustc limitation with a post-processing step in the bindgen
build that adjusts the attributes to include "#[repr(C, align(N))]" and
exclude #[repr(packed(N)] only for the 4 types that need it. It also
updates bch_bindgen to format the code with prettyplease so that this
will work even in environments with rustfmt installed.
Some types that had been manually implemented in
bch_bindgen/src/bcachefs.rs are now automatically generated by bindgen,
so that they will be covered by the ABI compatibility testing mentioned
above.
I intentionally targeted the post-processing to the exact 4 types with
the issue currently, so that any changes to bcachefs that result in this
issue appearing for a new type will require manual intervention. I
figured any such changes should require careful consideration.
Ideally, bindgen can be updated to handle situations where "align(N)"
is needed and "packed(N)" can be safely discarded. If a patch for this
is accepted into bindgen, the post-processing hack can be removed.
I update the minimum Rust version to 1.70 as this is needed to build
recent versions of some dependencies.
Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2024-01-23 17:36:02 +03:00
|
|
|
}
|