# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>

pkgbase=linux-ppc64
pkgver=6.11.9.arch1
pkgrel=1
pkgdesc='Linux (powerpc64le/ 4K page size)'
url='https://github.com/archlinux/linux'
arch=(any)
license=(GPL2)
makedepends=(
  bc
  cpio
  gettext
  libelf
  pahole
  perl
  python
  tar
  xz
)
# cross compiler
makedepends_powerpc=(powerpc64-unknown-linux-gnu-gcc)
makedepends_powerpc64le=(powerpc64-unknown-linux-gnu-gcc)
makedepends_riscv64=(powerpc64-unknown-linux-gnu-gcc)
makedepends_x86_64=(powerpc64-unknown-linux-gnu-gcc)

options=('!debug' '!strip')
_srcname=linux-${pkgver%.*}
_srctag=v${pkgver%.*}-${pkgver##*.}
source=(
  https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/${_srcname}.tar.{xz,sign}
  $url/releases/download/$_srctag/linux-$_srctag.patch.zst{,.sig}
  config  # the main kernel config file
  0002-hotfix-usb-audio.patch
  0003-ppc-radeon-qw-pm.patch
  0004-pcmcia-iomega-clik.patch
  0005-i915-diag.patch
  0006-i915-page-64k.patch
  0007-i915-ppc.patch
  0666-hotfix-windfarm_rm31.patch
  hotfix-aha152x-printk-cont.patch
  hotfix-appledisplay.patch
  xserve-frontpanel.patch
)
validpgpkeys=(                                                                                                                                                                               
  ABAF11C65A2970B130ABE3C479BE3E4300411886  # Linus Torvalds                                                                                                                                 
  647F28654894E3BD457199BE38DBBDC86092693E  # Greg Kroah-Hartman                                                                                                                             
  83BC8889351B5DEBBB68416EB8AC08600F108CDF  # Jan Alexander Steffens (heftig)                                                                                                                
) 
# https://www.kernel.org/pub/linux/kernel/v6.x/sha256sums.asc
sha256sums=('75658a7aa3bd9598c96ee1e5862c5e1d34fced75c28d825c727a1510a6f384b4'
            'SKIP'
            '8538475bdb3b4684082217a9282d8d149538b6b6e5a6b3b5efa12217ae1f69a6'
            'SKIP'
            '8fd7ef6e2ab31b61c7b980cd881ab923f4c7ed4fdf3d00d73819b44a735da13e'
            'd1f25e9415a749268ede5ce2aa2595faa08d5bf6e29cee63b27b9334d5efc3a2'
            '07dbbf3e8f743e8d2ad90891a8b567392271db139e339529df1ab4387e0e6400'
            'b8a5d1e9dbed702d23bda79af3ac6f3d61228e123f7d6e5717779f2e28c4b677'
            '060a30a4148aa88701003fd56bb389971db8527fa376e3436094cddcdec76c24'
            '5dc5eb5772dd79ea4c87a60c2bafbb0503f20a3e4ddbf2075c02ca69e159e21d'
            '7d44df97c0e565cfec60b77c0ad582e7f0da54726cfedc149c0b49e45f3a5d8b'
            '49186b0457740869214eafbda48d457d673106ec821a88cfb61d6129c4ac08ae'
            'cf7de242e4ccd61e2594d272f066aca1d5e3df17ccbca20e75ff14d8a414c2f4'
            'bf6c4bd24f55c17a1a22d96ff1c3acdf24fe48cf2835932a6885c7c97e15ffdf'
            'eb2e6a3e6676aa965015483d0c1e97dd469802abe507e1cd87ab858c91b54482')
b2sums=('97787b71bcf6a57872078e46917b3b6e339f25a0320dabe226fe5fe91e3eb829e3749b096eec4525fd1d6f25805f54f8a2d2b71a41bc94fd35c2b9c9140f25d7'
        'SKIP'
        'd90b8cadee652e79a57011e26585d57a1ddb1b8f0a3a58ff09383cc20d048a3545fdd7bc4569008fbb002acf1ffebd8261f540f848735550d81f538d7b2b030d'
        'SKIP'
        'ef14be5383aa4b33402b19340d0f4a7a6d2cfd58321b438c0a04c4fd782204eaaa199fc3f302f021c75e6d265b0935cde4c85204460d66c26e98db169f024a74'
        '91b488e69c8b34c7347d135cde425e681faa6a329951648172ab00561e498a74063e345b897ea45142d167a2c02a27fee9ffe585f6a7919040b37f43547c1fc8'
        '9677bbe86e5bb846c1fef6065f854e72b97a6e2b3b0e547192159fd56fac8b879999c6d52a835ebf7566be00c1a78644112564a4f76ec1c912181474479ad0a0'
        '7af37e02f6b8233a72c03be8586abeabc253dae70208e8169b208f49360c96c63edc5d68c1dbb51d6480d8b1bfdf8ba5f3303d1b18e9ab453c01ebebf3cf19a7'
        'bae7c8f1b50b9a43c99b91496446e9e0eff9442e40c7501aa5c1f24587dd39bea5bc0c5b48e6439118dcc779e36fbfaf75b2d92617257ade5e30109df21ae102'
        '6dc95da4e14b9c4dd8c62480560c061a5c1a29ce1af8d9ee1e73401f79339e1b6c4f8339568d57414a51341f382c1ee1a636bdaaf70bca72692d62c0a03670f8'
        '45ca140885508e53bc779fe650e84702928980b7b3cedd63d68558a20a9504e93cb2e78ca20e613917d17732b6e18c3fb37b348cfdf5e1a5325b827986268a1e'
        '99656109ed282ba45066d0cdc3515d11e1e660b7eadac7d8dccadb2429e929f77280d57cf800fa3e7326465e3060695325fecac72c21fcec4cd356fe2801b60c'
        '78adf3566e63f2cb56c6a3e988b7710db34ad7c923c93f9a391e7d3b41486487f17660900d0cdb531132ef444f94a1eba90f3ec02b76f6c3082ad9c73f5eefd9'
        'f4706d9eb3a1daf38cca5694553a3c8ade323bff0e9d67bffa3ed8512873aec03ed4ced76dd3830595d115340d0bd35576ef71efeda400f2ccb99612119f7202'
        '10af3c974b5cdd87d1f65c3c70e67cf43c6fd9d5f96eb2fb454b29a70b5024715b26c9c7e7542f544e7c7ebb3dce210ac0c87f745375325045e51fe2150e212a')

export KBUILD_BUILD_HOST=archpower
export KBUILD_BUILD_USER=$pkgbase
export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

prepare() {
  cd $_srcname

  echo "Setting version..."
  echo "-$pkgrel" > localversion.10-pkgrel
  echo "${pkgbase#linux}" > localversion.20-pkgname

  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    src="${src%.zst}"
    [[ $src = *.patch ]] || continue
    echo "Applying patch $src..."
    patch -Np1 < "../$src"
  done

  echo "Setting config..."
  cp ../config .config
  ARCH=powerpc CROSS_COMPILE=powerpc64-unknown-linux-gnu- make olddefconfig
  diff -u ../config .config || :

  ARCH=powerpc CROSS_COMPILE=powerpc64-unknown-linux-gnu- make -s kernelrelease > version
  echo "Prepared $pkgbase version $(<version)"
}

build() {
  cd $_srcname
  ARCH=powerpc CROSS_COMPILE=powerpc64-unknown-linux-gnu- make all
}

_package() {
  pkgdesc="The $pkgdesc kernel and modules"
  depends=(
    coreutils
    initramfs
    kmod
  )
  optdepends=(
    'wireless-regdb: to set the correct wireless channels of your country'
    'linux-firmware: firmware images needed for some devices'
  )
  provides=(
    KSMBD-MODULE
    VIRTUALBOX-GUEST-MODULES
    WIREGUARD-MODULE
  )
  replaces=(
    virtualbox-guest-modules-arch
    wireguard-arch
  )

  cd $_srcname
  local modulesdir="$pkgdir/usr/lib/modules/$(<version)"

  echo "Installing boot image..."
  # systemd expects to find the kernel here to allow hibernation
  # https://github.com/systemd/systemd/commit/edda44605f06a41fb86b7ab8128dcf99161d2344
  install -Dm644 "$(ARCH=powerpc CROSS_COMPILE=powerpc64-unknown-linux-gnu- make -s image_name)" "$modulesdir/vmlinuz"

  # Used by mkinitcpio to name the kernel
  echo "$pkgbase" | install -Dm644 /dev/stdin "$modulesdir/pkgbase"

  echo "Installing modules..."
  ARCH=powerpc CROSS_COMPILE=powerpc64-unknown-linux-gnu- \
  ZSTD_CLEVEL=19 make INSTALL_MOD_PATH="$pkgdir/usr" INSTALL_MOD_STRIP=1 \
    DEPMOD=/doesnt/exist modules_install  # Suppress depmod

  # remove build link
  rm "$modulesdir"/build
}

_package-headers() {
  pkgdesc="Headers and scripts for building modules for the $pkgdesc kernel"
  depends=(pahole)

  cd $_srcname
  local builddir="$pkgdir/usr/lib/modules/$(<version)/build"

  echo "Installing build files..."
  install -Dt "$builddir" -m644 .config Makefile Module.symvers System.map \
    localversion.* version vmlinux
  install -Dt "$builddir/kernel" -m644 kernel/Makefile
  install -Dt "$builddir/arch/powerpc" -m644 arch/powerpc/Makefile
  cp -t "$builddir" -a scripts

  echo "Installing headers..."
  cp -t "$builddir" -a include
  cp -t "$builddir/arch/powerpc" -a arch/powerpc/include
  install -Dt "$builddir/arch/powerpc/kernel" -m644 arch/powerpc/kernel/asm-offsets.s

  install -Dt "$builddir/drivers/md" -m644 drivers/md/*.h
  install -Dt "$builddir/net/mac80211" -m644 net/mac80211/*.h

  # https://bugs.archlinux.org/task/13146
  install -Dt "$builddir/drivers/media/i2c" -m644 drivers/media/i2c/msp3400-driver.h

  # https://bugs.archlinux.org/task/20402
  install -Dt "$builddir/drivers/media/usb/dvb-usb" -m644 drivers/media/usb/dvb-usb/*.h
  install -Dt "$builddir/drivers/media/dvb-frontends" -m644 drivers/media/dvb-frontends/*.h
  install -Dt "$builddir/drivers/media/tuners" -m644 drivers/media/tuners/*.h

  # https://bugs.archlinux.org/task/71392
  install -Dt "$builddir/drivers/iio/common/hid-sensors" -m644 drivers/iio/common/hid-sensors/*.h

  echo "Installing KConfig files..."
  find . -name 'Kconfig*' -exec install -Dm644 {} "$builddir/{}" \;

  echo "Removing unneeded architectures..."
  local arch
  for arch in "$builddir"/arch/*/; do
    [[ $arch = */powerpc/ ]] && continue
    echo "Removing $(basename "$arch")"
    rm -r "$arch"
  done

  echo "Removing documentation..."
  rm -r "$builddir/Documentation"

  echo "Removing broken symlinks..."
  find -L "$builddir" -type l -printf 'Removing %P\n' -delete

  echo "Removing loose objects..."
  find "$builddir" -type f -name '*.o' -printf 'Removing %P\n' -delete

  echo "Stripping build tools..."
  local file
  while read -rd '' file; do
    case "$(file -Sib "$file")" in
      application/x-sharedlib\;*)      # Libraries (.so)
        powerpc64-unknown-linux-gnu-strip -v $STRIP_SHARED "$file" ;;
      application/x-archive\;*)        # Libraries (.a)
        powerpc64-unknown-linux-gnu-strip -v $STRIP_STATIC "$file" ;;
      application/x-executable\;*)     # Binaries
        powerpc64-unknown-linux-gnu-strip -v $STRIP_BINARIES "$file" ;;
      application/x-pie-executable\;*) # Relocatable binaries
        powerpc64-unknown-linux-gnu-strip -v $STRIP_SHARED "$file" ;;
    esac
  done < <(find "$builddir" -type f -perm -u+x ! -name vmlinux -print0)

  echo "Stripping vmlinux..."
  powerpc64-unknown-linux-gnu-strip -v $STRIP_STATIC "$builddir/vmlinux"

  echo "Adding symlink..."
  mkdir -p "$pkgdir/usr/src"
  ln -sr "$builddir" "$pkgdir/usr/src/$pkgbase"
}

pkgname=(
  "$pkgbase"
  "$pkgbase-headers"
)
for _p in "${pkgname[@]}"; do
  eval "package_$_p() {
    $(declare -f "_package${_p#$pkgbase}")
    _package${_p#$pkgbase}
  }"
done

# vim:set ts=8 sts=2 sw=2 et:
