# Maintainer: Felix Yan <felixonmars@archlinux.org>

_hkgname=hadrian
pkgname=haskell-hadrian
_ghcver=9.6.6
pkgver=0.1.0.0+$_ghcver
pkgrel=3
pkgdesc="GHC build system"
url="https://gitlab.haskell.org/ghc/ghc"
license=('BSD-3-Clause')
arch=('x86_64' 'aarch64')
depends=('ghc-libs' 'haskell-base16-bytestring' 'haskell-cryptohash-sha256' 'haskell-extra' 'haskell-quickcheck'
         'haskell-shake' 'haskell-unordered-containers')
makedepends=('ghc' 'uusi')
source=("https://downloads.haskell.org/~ghc/$_ghcver/ghc-${_ghcver}-src.tar.xz"
        ghc-enable-ghci-for-riscv64.patch::https://gitlab.haskell.org/ghc/ghc/-/commit/dd38aca95ac25adc9888083669b32ff551151259.patch)
sha256sums=('008f7a04d89ad10baae6486c96645d7d726aaac7e1476199f6dd86c6bd9977ad'
            '705c79d713e7344b5300c1b87eeb7faad31fa6bf65d5c164eb588606d58e803e')

prepare() {
  cd ghc-$_ghcver
  # apply patch to enable ghci for riscv64
  patch -p1 < "$srcdir"/ghc-enable-ghci-for-riscv64.patch
  uusi -u Cabal -u QuickCheck hadrian/hadrian.cabal
}

build() {
  cd ghc-$_ghcver/hadrian

  runhaskell Setup configure -O --enable-shared --enable-debug-info --enable-executable-dynamic --disable-library-vanilla \
    --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --datasubdir=haskell-hadrian --enable-tests \
    --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid \
    --ghc-option=-optl-Wl\,-z\,relro\,-z\,now \
    --ghc-option='-pie' \
    -fthreaded

  runhaskell Setup build $MAKEFLAGS
}

package() {
  cd ghc-$_ghcver/hadrian

  runhaskell Setup copy --destdir="$pkgdir"
  install -D -m644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
  rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
