File PKGBUILD of Package paru

# Maintainer: Morgan <morganamilo@archlinux.org>
pkgname=paru
pkgver=2.1.0
pkgrel=5
pkgdesc='Feature packed AUR helper (God Mode Build with GCC 16)'
url='https://github.com/morganamilo/paru'
source=(
  "paru-2.1.0.tar.gz"
  "vendor.tar.gz"
)
backup=("etc/paru.conf")
arch=('i686' 'pentium4' 'x86_64' 'arm' 'armv7h' 'armv6h' 'aarch64')
license=('GPL-3.0-or-later')
makedepends=('cargo' 'git' 'gcc-git-god' 'llvm-god-mlgo')
depends=('git' 'pacman')
optdepends=('bat: colored pkgbuild printing' 'devtools: build in chroot and downloading pkgbuilds')
sha256sums=('SKIP'
            'SKIP')

_paru_srcdir() {
  find "$srcdir" -maxdepth 1 -type d -name "${pkgname}-*" -print -quit
}

pkgver() {
  local _ver _ts
  if [[ -f "${srcdir}/paru-2.1.0.obsinfo" ]]; then
    _ver=$(sed -n 's/^version: //p' "${srcdir}/paru-2.1.0.obsinfo" | head -n1)
  fi
  if [[ -z "$_ver" && -f "${srcdir}/._servicedata" ]]; then
    _ver=$(sed -n 's/.*<param name="version">\(.*\)<\/param>.*/\1/p' "${srcdir}/._servicedata" | head -n1)
  fi
  if [[ -z "$_ver" && -d "${srcdir}/paru-2.1.0" ]]; then
    _ts=$(find "${srcdir}/paru-2.1.0" -type f -printf '%T@\n' 2>/dev/null | sort -n | tail -n1 | cut -d. -f1)
    if [[ -n "$_ts" ]]; then
      _ver=$(date -u -d "@${_ts}" +%Y%m%d.%H%M%S 2>/dev/null)
    fi
  fi
  if [[ -z "$_ver" ]]; then
    _ver="${pkgver}"
  fi
  echo "${_ver//-/_}"
}


prepare() {
  local _src=$(_paru_srcdir)
  if [[ -z "$_src" || ! -d "$_src" ]]; then
    echo "Missing source directory for ${pkgname}"
    return 1
  fi
  cd "$_src"
  rm -rf vendor .cargo
  if [ -d "$srcdir/vendor" ]; then
    mv "$srcdir/vendor" "$_src/vendor"
  fi
  mkdir -p .cargo
  cat > .cargo/config.toml <<'EOF'
[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"
EOF
}

build () {
  local _src=$(_paru_srcdir)
  if [[ -z "$_src" || ! -d "$_src" ]]; then
    echo "Missing source directory for ${pkgname}"
    return 1
  fi
  cd "$_src"

  # --- GOD MODE INJECTION ---
  msg2 "Injecting God Mode Compiler Stack..."
  if [ ! -x /opt/gcc-git-god/bin/gcc ]; then
    echo "gcc-git-god missing in /opt/gcc-git-god"
    return 1
  fi
  export CC="/opt/gcc-git-god/bin/gcc"
  export CXX="/opt/gcc-git-god/bin/g++"
  export AR="/opt/gcc-git-god/bin/gcc-ar"
  export NM="/opt/gcc-git-god/bin/gcc-nm"
  export RANLIB="/opt/gcc-git-god/bin/gcc-ranlib"

  # Optimize C/C++ parts (libalpm bindings etc)
  # Keep profiling binaries executable on generic OBS builders.
  export CFLAGS="-O3 -march=x86-64 -mtune=generic -flto=auto -fgraphite-identity -floop-nest-optimize -pipe"
  export CXXFLAGS="$CFLAGS"
  export LDFLAGS="-L/opt/gcc-git-god/lib64 -Wl,-rpath,/opt/gcc-git-god/lib64 -flto=auto -Wl,--emit-relocs"

  # Optimize Rust parts and ensure it finds the God Libs
  # BOLT requires --emit-relocs to perform function reordering safely
  export RUSTFLAGS="-C target-cpu=x86-64 -C opt-level=3 -C linker=/opt/gcc-git-god/bin/g++ -C link-arg=-Wl,-rpath,/opt/gcc-git-god/lib64 -C link-arg=-L/opt/gcc-git-god/lib64 -C link-arg=-Wl,--emit-relocs -C link-arg=-lstdc++ -C link-arg=-lm -C link-arg=-pthread"
  # Avoid SIGILL on build hosts without alderlake by downgrading proc-macro/build-script flags
  cat > "$srcdir/rustc-wrapper" <<'EOF'
#!/bin/bash
args=("$@")
real_rustc="rustc"
if [[ "${args[0]}" == "rustc" || "${args[0]}" == */rustc ]]; then
  real_rustc="${args[0]}"
  args=("${args[@]:1}")
fi
strip_cpu=0
  for ((i=0; i<${#args[@]}; i++)); do
    if [[ "${args[i]}" == "--crate-type" && "${args[i+1]}" == *proc-macro* ]]; then
      strip_cpu=1
      break
    fi
    if [[ "${args[i]}" == --crate-type=proc-macro* ]]; then
      strip_cpu=1
      break
    fi
    if [[ "${args[i]}" == "--crate-name" && "${args[i+1]}" == build_script_* ]]; then
      strip_cpu=1
      break
    fi
    if [[ "${args[i]}" == "--extern" && "${args[i+1]}" == proc_macro* ]]; then
      strip_cpu=1
      break
    fi
    if [[ "${args[i]}" == --extern=proc_macro* ]]; then
      strip_cpu=1
      break
    fi
  done
if [[ $strip_cpu -eq 1 ]]; then
  new=()
  skip_next=0
  for ((i=0; i<${#args[@]}; i++)); do
    if [[ $skip_next -eq 1 ]]; then
      skip_next=0
      continue
    fi
    if [[ "${args[i]}" == "-C" && "${args[i+1]}" == target-cpu=* ]]; then
      skip_next=1
      continue
    fi
    if [[ "${args[i]}" == -Ctarget-cpu=* ]]; then
      continue
    fi
    new+=("${args[i]}")
  done
  exec "$real_rustc" "${new[@]}"
else
  exec "$real_rustc" "${args[@]}"
fi
EOF
  chmod +x "$srcdir/rustc-wrapper"
  export RUSTC_WRAPPER="$srcdir/rustc-wrapper"
  export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER="/opt/gcc-git-god/bin/g++"
  export CARGO_PROFILE_RELEASE_LTO=true
  export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1
  export CARGO_NET_OFFLINE=true
  # --- END INJECTION ---

  # Force enable git feature since we are using pacman-git
  _features="git,"

  if [[ $CARCH != x86_64 ]]; then
    export CARGO_PROFILE_RELEASE_LTO=off
  fi

  cargo build --features "${_features:-}" --release --target-dir target
  ./scripts/mkmo locale/
}

package() {
  local _src=$(_paru_srcdir)
  if [[ -z "$_src" || ! -d "$_src" ]]; then
    echo "Missing source directory for ${pkgname}"
    return 1
  fi
  cd "$_src"

  # --- BOLT CONFIGURATION ---
  # Resolve BOLT tools from llvm-god-mlgo or system llvm fallback.
  BOLT_BIN="$(command -v llvm-bolt || true)"
  MERGE_FDATA_BIN="$(command -v merge-fdata || true)"
  for d in /opt/llvm-god/bin /opt/llvm-god /usr/lib/llvm*/bin /usr/lib/llvm*; do
    if [ -z "$BOLT_BIN" ] && [ -x "$d/llvm-bolt" ]; then
      BOLT_BIN="$d/llvm-bolt"
    fi
    if [ -z "$MERGE_FDATA_BIN" ] && [ -x "$d/merge-fdata" ]; then
      MERGE_FDATA_BIN="$d/merge-fdata"
    fi
  done
  # --- END BOLT CONFIGURATION ---

  install -Dm755 target/release/paru "${pkgdir}/usr/bin/paru"
  install -Dm644 paru.conf "${pkgdir}/etc/paru.conf"

  # --- START BOLT OPTIMIZATION ---
  if [[ "${GOD_ENABLE_BOLT:-0}" != "1" ]]; then
    msg2 "GOD_ENABLE_BOLT is not enabled; skipping BOLT optimization."
  elif [ ! -x "$BOLT_BIN" ] || [ ! -x "$MERGE_FDATA_BIN" ]; then
    msg2 "BOLT tools missing (llvm-bolt/merge-fdata); skipping BOLT optimization."
  elif ! "$BOLT_BIN" --help >/dev/null 2>&1 || ! "$MERGE_FDATA_BIN" --help >/dev/null 2>&1; then
    msg2 "BOLT tools are present but not runnable on this builder; skipping BOLT optimization."
  else
    msg2 "Initiating BOLT Post-Link Optimization on paru..."
    
    # Create a temporary directory for profiling
    local paru_bin="$PWD/target/release/paru"
    mkdir -p bolt_profile_run
    cd bolt_profile_run
    
    # 1. Profile the binary
    # We use 'paru -Q' as a representative workload for startup and database query speed.
    # We must explicitly point to the binary we just built (target/release/paru) or the one in pkgdir.
    # Let's use the one in target/release to generate the profile.
    msg2 "Sampling paru performance (Profiling)..."
    
    # Ensure it finds the God Mode libraries during profiling
    export LD_LIBRARY_PATH="/opt/gcc-git-god/lib64:$LD_LIBRARY_PATH"

    bolt_rt=""
    for d in /opt/llvm-god/lib /opt/llvm-god/lib64 /usr/lib /usr/lib/llvm*/lib /usr/lib/llvm*/lib64; do
      if [[ -f "$d/libbolt_rt_instr.a" ]]; then
        bolt_rt="$d/libbolt_rt_instr.a"
        break
      fi
    done
    if [[ -z "$bolt_rt" ]]; then
      msg2 "BOLT runtime not found (libbolt_rt_instr.a); skipping BOLT optimization."
      cd ..
    else
      "$BOLT_BIN" "$paru_bin" -o ../target/release/paru.inst \
        -instrument \
        -instrumentation-file="$PWD/paru.fdata" \
        -instrumentation-file-append-pid
      ../target/release/paru.inst -Q > /dev/null 2>&1 || true
      
      cd ..

      if ls bolt_profile_run/paru.fdata.* &>/dev/null; then
        "$MERGE_FDATA_BIN" bolt_profile_run/paru.fdata.* > bolt_profile_run/paru.fdata
      fi
      if [ ! -s bolt_profile_run/paru.fdata ]; then
        msg2 "No BOLT profile data collected; keeping non-BOLT paru binary."
      else
        msg2 "Applying BOLT to paru..."
        "$BOLT_BIN" ./target/release/paru -o ./paru.bolt -data bolt_profile_run/paru.fdata \
          -reorder-blocks=ext-tsp \
          -reorder-functions=cdsort \
          -split-functions \
          -split-all-cold \
          -split-eh \
          -dyno-stats
        
        # 4. Overwrite the installed binary with the God Mode version
        install -Dm755 ./paru.bolt "${pkgdir}/usr/bin/paru"
        msg2 "BOLT optimization complete. The God Mode Paru is ready."
      fi
    fi
  fi
  # --- END BOLT OPTIMIZATION ---

  install -Dm644 man/paru.8 "$pkgdir/usr/share/man/man8/paru.8"
  install -Dm644 man/paru.conf.5 "$pkgdir/usr/share/man/man5/paru.conf.5"

  install -Dm644 completions/bash "${pkgdir}/usr/share/bash-completion/completions/paru.bash"
  install -Dm644 completions/fish "${pkgdir}/usr/share/fish/vendor_completions.d/paru.fish"
  install -Dm644 completions/zsh "${pkgdir}/usr/share/zsh/site-functions/_paru"

  install -d "$pkgdir/usr/share/"
  cp -r locale "$pkgdir/usr/share/"
}
openSUSE Build Service is sponsored by