File PKGBUILD of Package aur_published-bcachefs-tools-git

# Maintainer: Kyle De'Vir (QuartzDragon) <kyle.devir.mykolab.com>
# Contributor: neycrol <330578697@qq.com>

pkgname=bcachefs-tools-git
_pkgname=bcachefs-tools
pkgver=19700101000000.r1.g00000000
pkgrel=5
pkgdesc="Bcachefs userspace tools (Git version) with FUSE support enabled"
arch=('x86_64')
url="https://github.com/koverstreet/bcachefs-tools"
license=('GPL-2.0-only')
provides=("$_pkgname")
conflicts=("$_pkgname")

depends=(
    'glibc'
    'libaio'
    'util-linux-libs'
    'keyutils'
    'libsodium'
    'liburcu'
    'zlib'
    'zstd'
    'lz4'
    'systemd-libs'
    'fuse3'
)
makedepends=(
    'rust'
    'cargo'
    'clang'
    'llvm'
    'jq'
    'pkgconf'
)

source=(
  "bcachefs-tools.tar.gz"
  "git-head.log"
  "vendor.tar.gz"
)
sha256sums=('SKIP'
            'SKIP'
            'SKIP')

# Disable LTO to prevent issues with Rust's ThinLTO
options=('!lto' '!debug')

pkgver() {
    local _repo_dir _ts _hash

    _repo_dir=$(find "$srcdir" -maxdepth 1 -type d -name "${_pkgname}*" | head -n 1)
    [ -z "$_repo_dir" ] && _repo_dir="$srcdir/$_pkgname"

    if [ -s "$srcdir/git-head.log" ]; then
        read -r _ts _hash < <(awk 'NF { ts=$5; h=$2 } END { print ts, h }' "$srcdir/git-head.log")
    fi

    [ -z "$_ts" ] && _ts=$(stat -c %Y "$_repo_dir" 2>/dev/null || date +%s)
    [ -z "$_hash" ] && _hash="00000000"
    printf '%s.r1.g%s\n' "$(date -u -d "@$_ts" +%Y%m%d%H%M%S)" "${_hash:0:8}"
}



prepare() {
    cd "$_pkgname"
    rm -rf vendor .cargo
    if [ -d "$srcdir/vendor" ]; then
        mv "$srcdir/vendor" "$srcdir/$_pkgname/vendor"
    fi
    mkdir -p .cargo
    cat > .cargo/config.toml <<'EOF'
[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"
EOF
    if [[ ! -d vendor/fuser ]]; then
        echo "Vendored crates are incomplete (missing fuser). Refresh vendor.tar.gz for current source revision."
        return 1
    fi
    # Upstream Makefile scans all *.c recursively. Exclude vendored crate sources
    # so Cargo-owned test fixtures are not pulled into the C toolchain build.
    if ! grep -q "! -path './vendor/*'" Makefile; then
        sed -i "s|find \\. -type f ! -path '\\*/\\.\\*/\\*' -iname '\\*.c'|find . -type f ! -path '*/.*/*' ! -path './vendor/*' -iname '*.c'|" Makefile
    fi
    export RUSTUP_TOOLCHAIN=stable
}

build() {
    cd "$_pkgname"

    local _cargo_linker
    if command -v x86_64-linux-gnu-gcc >/dev/null 2>&1; then
        _cargo_linker=x86_64-linux-gnu-gcc
    elif command -v x86_64-pc-linux-gnu-gcc >/dev/null 2>&1; then
        _cargo_linker=x86_64-pc-linux-gnu-gcc
    else
        _cargo_linker=gcc
    fi

    export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER="$_cargo_linker"
    export CC_x86_64_unknown_linux_gnu="$_cargo_linker"

    # Use system CFLAGS/LDFLAGS for Rust
    # Also strip build paths to avoid packaging warnings and reproducible build issues
    export RUSTFLAGS="-C link-arg=$LDFLAGS -Clinker=$_cargo_linker --remap-path-prefix=$srcdir/=/"
    export CFLAGS="$CFLAGS -fdebug-prefix-map=$srcdir/=/"
    export CARGO_NET_OFFLINE=true

    make \
        BCACHEFS_FUSE=1 \
        PREFIX=/usr \
        ROOT_SBINDIR=/usr/bin \
        LIBEXECDIR=/usr/lib \
        INITRAMFS_DIR=/usr/lib/initcpio \
        EXTRA_CFLAGS="-include linux/types.h" \
        all
    
    # Generate shell completions using the built binary
    local _bin="./target/release/bcachefs"
    if [ -x "$_bin" ]; then
        msg2 "Generating shell completions..."
        "$_bin" completions bash > bcachefs.bash
        "$_bin" completions zsh  > _bcachefs
        "$_bin" completions fish > bcachefs.fish
    fi
}

package() {
    cd "$_pkgname"

    make \
        BCACHEFS_FUSE=1 \
        PREFIX=/usr \
        ROOT_SBINDIR=/usr/bin \
        LIBEXECDIR=/usr/lib \
        INITRAMFS_DIR=/usr/lib/initcpio \
        DESTDIR="$pkgdir" \
        install

    # REMOVE DKMS SOURCE INSTALLED BY MAKEFILE
    # This package provides userspace tools only. The kernel module should be 
    # managed by 'bcachefs-kernel-dkms-git' or official packages to avoid
    # version mismatch and stale drivers.
    rm -rf "$pkgdir/usr/src"

    # Install License
    install -Dm644 COPYING "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

    # Install Shell Completions
    if [ -f bcachefs.bash ]; then
        install -Dm644 bcachefs.bash "$pkgdir/usr/share/bash-completion/completions/bcachefs"
        install -Dm644 _bcachefs      "$pkgdir/usr/share/zsh/site-functions/_bcachefs"
        install -Dm644 bcachefs.fish  "$pkgdir/usr/share/fish/vendor_completions.d/bcachefs.fish"
    fi
}
openSUSE Build Service is sponsored by