bcachefs-tools/build.nix
Thomas Bertschinger 7717a439cf 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 13:08:12 -05:00

57 lines
1.3 KiB
Nix

{ lib, stdenv, pkg-config, attr, libuuid, libsodium, keyutils, liburcu, zlib
, libaio, udev, zstd, lz4, nix-gitignore, rustPlatform, rustc, cargo, fuse3
, fuseSupport ? false, }:
let
src = nix-gitignore.gitignoreSource [ ] ./.;
commit = lib.strings.substring 0 7 (builtins.readFile ./.bcachefs_revision);
version = "git-${commit}";
in stdenv.mkDerivation {
inherit src version;
pname = "bcachefs-tools";
nativeBuildInputs = [
pkg-config
cargo
rustc
rustPlatform.cargoSetupHook
rustPlatform.bindgenHook
];
buildInputs = [
libaio
keyutils # libkeyutils
lz4 # liblz4
libsodium
liburcu
libuuid
zstd # libzstd
zlib # zlib1g
attr
udev
] ++ lib.optional fuseSupport fuse3;
${if fuseSupport then "BCACHEFS_FUSE" else null} = "1";
cargoRoot = ".";
# when git-based crates are updated, run:
# nix run github:Mic92/nix-update -- --version=skip --flake default
# to update the hashes
cargoDeps = rustPlatform.importCargoLock {
lockFile = "${src}/Cargo.lock";
};
makeFlags = [ "DESTDIR=${placeholder "out"}" "PREFIX=" "VERSION=${commit}" ];
dontStrip = true;
checkPhase = "./target/release/bcachefs version";
doCheck = true;
meta = {
mainProgram = "bcachefs";
license = lib.licenses.gpl2Only;
};
}