# Maintainer: Daniel Bermond <dbermond@archlinux.org>
# Maintainer: Bruno Pagani <archange@archlinux.org>

_srcname=SPIRV-LLVM-Translator
pkgname=${_srcname,,}
pkgver=17.0.0.r6+g70883514
pkgrel=1
pkgdesc="Tool and a library for bi-directional translation between SPIR-V and LLVM IR"
arch=(x86_64 aarch64)
url="https://github.com/KhronosGroup/SPIRV-LLVM-Translator"
license=(custom)
depends=(llvm-libs spirv-tools)
makedepends=(git cmake llvm spirv-headers)
checkdepends=(python python-setuptools clang)
# Current point in the used LLVM branch
_commit=7088351420a8f0c3e5f9be678fd1d16ae826e37f
source=(git+${url}.git#commit=$_commit)
sha256sums=('SKIP')

pkgver() {
  cd ${_srcname}
  git describe --tags | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./;s/-/+/'
}

prepare() {
  cd ${_srcname}

  _bad_tests=(
      test/DebugInfo/Generic/2009-11-10-CurrentFn.ll
      test/DebugInfo/Generic/2010-03-12-llc-crash.ll
      test/DebugInfo/Generic/2010-03-24-MemberFn.ll
      test/DebugInfo/Generic/2010-10-01-crash.ll
      test/DebugInfo/Generic/PR20038.ll
      test/DebugInfo/Generic/constant-pointers.ll
      test/DebugInfo/Generic/dead-argument-order.ll
      test/DebugInfo/Generic/debug-info-eis-option.ll
      test/DebugInfo/Generic/dwarf-public-names.ll
      test/DebugInfo/Generic/enum.ll
      test/DebugInfo/Generic/func-using-decl.ll
      test/DebugInfo/Generic/global.ll
      test/DebugInfo/Generic/imported-name-inlined.ll
      test/DebugInfo/Generic/inline-scopes.ll
      test/DebugInfo/Generic/inlined-arguments.ll
      test/DebugInfo/Generic/inlined-vars.ll
      test/DebugInfo/Generic/linear-dbg-value.ll
      test/DebugInfo/Generic/linkage-name-abstract.ll
      test/DebugInfo/Generic/member-order.ll
      test/DebugInfo/Generic/missing-abstract-variable.ll
      test/DebugInfo/Generic/multiline.ll
      test/DebugInfo/Generic/namespace_function_definition.ll
      test/DebugInfo/Generic/namespace_inline_function_definition.ll
      test/DebugInfo/Generic/noscopes.ll
      test/DebugInfo/Generic/ptrsize.ll
      test/DebugInfo/Generic/restrict.ll
      test/DebugInfo/Generic/two-cus-from-same-file.ll
      test/DebugInfo/Generic/varargs.ll
      test/DebugInfo/Generic/version.ll
      test/DebugInfo/LocalAddressSpace.ll
      test/DebugInfo/UnknownBaseType.ll
      test/DebugInfo/expr-opcode.ll
  )

  # You can't reasonably expect X86 specific tests to pass with
  # a target triple that's different.
  find test/DebugInfo/X86 -name \*.ll | xargs sed -ie 's/%triple/x86_64-unknown-linux-gnu/'

  # The spir_func and spir_kernel calling conventions don't have support on
  # anything except x86_64, so there's no point expecting the tests to pass.
  for test in "${_bad_tests[@]}"; do
    sed -ie '1i; XFAIL: !target=x86{{.*}}' "${test}"
  done
}

build() {
  cmake -B build -S ${_srcname} \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
    -DCMAKE_SKIP_RPATH=ON \
    -DLLVM_INCLUDE_TESTS=ON \
    -DLLVM_EXTERNAL_LIT=/usr/bin/lit \
    -DLLVM_EXTERNAL_SPIRV_HEADERS_SOURCE_DIR=/usr/include/spirv/ \
    -Wno-dev
  make -C build
}

check() {
  LD_LIBRARY_PATH="${srcdir}/build/lib/SPIRV" make -C build test
}

package() {
  make -C build DESTDIR="${pkgdir}" install
  install -Dm755 build/tools/llvm-spirv/llvm-spirv -t "${pkgdir}"/usr/bin
  install -Dm644 ${_srcname}/LICENSE.TXT -t "${pkgdir}"/usr/share/licenses/${pkgname}/
}
