# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Filipe Laíns (FFY00) <lains@archlinux.org>
# Maintainer: Daniel M. Capella <polyzen@archlinux.org>

pkgbase=tree-sitter
pkgname=('tree-sitter' 'tree-sitter-cli')
pkgver=0.22.6
pkgrel=1
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
url=https://github.com/tree-sitter/tree-sitter
license=('MIT')
makedepends=('git' 'rust')
options=('!lto') # Needed for CLI build
source=("git+$url.git#commit=v$pkgver")
b2sums=('9d60024e03e7ca7c5335ede099cf14b3658e41ef2eb138d17b7c4ec5871d2e2f4bb348c7dfb7bcd82b770a268116d8b8f793de8d247798fddd21e58c3747e43f')
#validpgpkeys=('FCC13F47A6900D64239FF13BE67890ADC4227273') # Amaan Qureshi <amaanq12@gmail.com>

prepare() {
  cd $pkgbase/cli
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd $pkgbase
  make PREFIX=/usr LDFLAGS="$LDFLAGS -flto" CFLAGS="$CFLAGS -flto" CXXFLAGS="$CXXFLAGS -flto"

  cd cli
  cargo build --release --locked --offline
}

package_tree-sitter() {
  pkgdesc='Incremental parsing library'
  provides=('libtree-sitter.so')

  cd $pkgbase
  make DESTDIR="$pkgdir" PREFIX=/usr install
  install -Dm644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgbase
}

package_tree-sitter-cli() {
  pkgdesc='CLI tool for developing, testing, and using Tree-sitter parsers'
  depends=('gcc-libs')
  optdepends=('nodejs: for the generate subcommand')

  cd $pkgbase
  install -Dt "$pkgdir"/usr/bin target/release/$pkgbase
  install -Dm644 -t "$pkgdir"/usr/share/licenses/${pkgbase}-cli LICENSE
}
