#!/usr/bin/bash
# JOBoRun		: Jwm OpenBox Obarun RUNit
# Maintainer	: Joe Bo Run <joborun@disroot.org>
# PkgSource		: url="https://gittea.disroot.org/joborun-pkg/jobextra/$pkgname"
# Website		: https://pozol.eu
#-----------------------------------------| DESCRIPTION |---------------------------------------

pkgname=('llvm14' 'llvm14-libs')
pkgver=14.0.6
pkgrel=02
arch=('x86_64')
url="https://llvm.org/"
##  ex llvm pkg became llvm14 after the 15.0 upgrade
makedepends=('cmake' 'ninja' 'libffi' 'libedit' 'ncurses' 'libxml2'
		'python')
#	'python-setuptools' 'python-psutil' 'python-sphinx'
#	'python-recommonmark')
options=('staticlibs' '!lto') # Getting thousands of test failures with LTO
_source_base=https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver
source=($_source_base/llvm-$pkgver.src.tar.xz{,.sig}
        llvm-coroutines-ubsan.patch)
#        llvm-config.h)

prepare() {
  cd llvm-$pkgver.src
  mkdir build

  # https://github.com/llvm/llvm-project/issues/49689
  patch -Np2 -i ../llvm-coroutines-ubsan.patch

}

build() {
  cd llvm-$pkgver.src/build

  # Build only minimal debug info to reduce size
  CFLAGS+=' -g1'
  CXXFLAGS+=' -g1'

# try with build_tests off and sphinx errors off
  local cmake_args=(
    -G Ninja
    -DCMAKE_BUILD_TYPE=Release
    -DCMAKE_INSTALL_PREFIX=/usr/lib/llvm14
    -DCMAKE_SKIP_RPATH=ON
    -DLLVM_BINUTILS_INCDIR=/usr/include
    -DLLVM_BUILD_LLVM_DYLIB=ON
    -DLLVM_BUILD_TESTS=OFF
    -DLLVM_ENABLE_BINDINGS=OFF
    -DLLVM_ENABLE_FFI=ON
    -DLLVM_ENABLE_RTTI=ON
    -DLLVM_HOST_TRIPLE=$CHOST
    -DLLVM_INCLUDE_BENCHMARKS=OFF
    -DLLVM_INSTALL_UTILS=ON
    -DLLVM_LINK_LLVM_DYLIB=ON
    -DLLVM_USE_PERF=ON
    -DSPHINX_WARNINGS_AS_ERRORS=OFF
  )
  cmake .. "${cmake_args[@]}"
  ninja
}

##  almost at the end before last couple of checks it sticks and does not proceed - so skipped at this time
##  the above with 13.0.0  let's see how the 13.0.1 does with the altered ninja check
##  it did the same   Same story with 14.0.6 
#check() {
#  cd llvm-$pkgver.src/build
#  LD_LIBRARY_PATH=$PWD/lib ninja check
#}

package_llvm14() {
  pkgdesc="Compiler infrastructure (LLVM 14)"
  depends=('llvm14-libs' 'perl')

  cd llvm-$pkgver.src/build

  DESTDIR="$pkgdir" ninja install

  # The runtime libraries go into llvm14-libs
  mv -f "$pkgdir"/usr/lib/llvm14/lib/libLLVM-{14,$pkgver}.so "$srcdir/"
  mv -f "$pkgdir"/usr/lib/llvm14/lib/LLVMgold.so "$srcdir/"

  # Create versioned symlinks from /usr/bin/ to /usr/lib/llvm14/bin/
  install -d "$pkgdir/usr/bin"
  local _binary
  for _binary in "$pkgdir"/usr/lib/llvm14/bin/*; do
    local _basename=${_binary##*/}
    ln -s ../lib/llvm14/bin/$_basename "$pkgdir/usr/bin/$_basename-14"
  done

  install -Dm644 ../LICENSE.TXT "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

package_llvm14-libs() {
  pkgdesc="LLVM 14 runtime libraries"
  depends=('gcc-libs' 'zlib' 'libffi' 'libedit' 'ncurses' 'libxml2')

  install -d "$pkgdir/usr/lib/llvm14/lib"
  cp -P "$srcdir"/libLLVM-{14,$pkgver}.so "$pkgdir/usr/lib/"
  ln -s ../../libLLVM-14.so "$pkgdir/usr/lib/llvm14/lib/libLLVM-14.so"
  ln -s ../../libLLVM-14.so "$pkgdir/usr/lib/llvm14/lib/libLLVM-$pkgver.so"
  cp -P "$srcdir"/LLVMgold.so "$pkgdir/usr/lib/llvm14/lib/"

  install -Dm644 "$srcdir/llvm-$pkgver.src/LICENSE.TXT" \
    "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}


#---- license gpg-key sha256sums ----

license=('custom:Apache 2.0 with LLVM Exception')

validpgpkeys=(474E22316ABF4785A88C6E8EA2C794A986419D8A) # Tom Stellard <tstellar@redhat.com>

sha256sums=(050922ecaaca5781fdf6631ea92bc715183f202f9d2f15147226f023414f619a # llvm-14.0.6.src.tar.xz
	98fee8942d6f8c508760d76197018b3ceb0542bcf58d061c768a3e03ceeaa6e2 # llvm-14.0.6.src.tar.xz.sig
	ee9baf6df05474083857044d92f26f59d3ee709cdf82ba3bdb2792e6645f71d9) # llvm-coroutines-ubsan.patch
#	597dc5968c695bbdbb0eac9e8eb5117fcd2773bc91edf5ec103ecffffab8bc48) #  llvm-config.h


