# Maintainer: Will Handley <wh260@cam.ac.uk> (aur.archlinux.org/account/wjhandley)

_versioned_llvm=
if [[ -n $_versioned_llvm ]]; then
  _clang=clang$_versioned_llvm
else
  _clang=clang
fi

pkgname=enzyme-julia
pkgver=0.0.290
pkgrel=1
pkgdesc="High-performance automatic differentiation of LLVM and MLIR."
arch=(aarch64 x86_64)
url="https://enzyme.mit.edu"
license=('Apache')
depends=(llvm-libs-julia-git)
makedepends=(cmake git $_clang llvm-julia-git)
source=("https://github.com/EnzymeAD/Enzyme/archive/v${pkgver}.tar.gz"
        0001-fix-gsl64-patching-script.patch)
sha256sums=('5eb0365b5e66309d5b604e538fdf8f4307cb93b3dd41d118c5b85e2997487b3f'
            '34644e0e64dae1b6ca5567373176fc9431b3ca7c4a87071a2e25e292215ab56b')

prepare() {
  cd Enzyme-$pkgver
  patch -Np1 --no-backup-if-mismatch < ../0001-fix-gsl64-patching-script.patch
}

build() {
  if [[ -n $_versioned_llvm ]]; then
    _llvm_dir=/usr/lib/llvm$_versioned_llvm/lib/cmake/llvm
    _clang_dir=/usr/lib/llvm$_versioned_llvm/lib/cmake/clang
  else
    _llvm_dir=/usr/lib/cmake/llvm
    _clang_dir=/usr/lib/cmake/clang
  fi
  cmake -B build -S "Enzyme-$pkgver/enzyme" \
        -DCMAKE_BUILD_TYPE='RelWithDebInfo' \
        -DLLVM_DIR=$_llvm_dir \
        -DClang_DIR=$_clang_dir \
        -DCMAKE_INSTALL_PREFIX='/usr' \
        -DENZYME_CLANG=On \
        -DENZYME_EXTERNAL_SHARED_LIB=On \
        -DLLVM_LINK_LLVM_DYLIB=On \
        -DBUILD_SHARED_LIBS=On \
        -Wno-dev
  cmake --build build
}

package() {
  DESTDIR="$pkgdir" cmake --install build
}
