# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=libclc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_pkgver=21.1.8
pkgver=${_pkgver/-/}
pkgrel=1
pkgdesc='Library requirements of the OpenCL C programming language (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://libclc.llvm.org/'
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-spirv-llvm-translator")
_url=https://github.com/llvm/llvm-project/releases/download/llvmorg-${_pkgver}
_pkgfn=${_realname}-${_pkgver}.src
source=($_url/$_pkgfn.tar.xz{,.sig}
        0001-cmake-copy-instead-symlink.patch
        0002-fix-pkgconfig-file.patch)
sha256sums=('6c2677362a53531c35edf482bdc9171ea0471ca0a1e9138ac9b5a1782925616f'
            'SKIP'
            'b974a117ec1a658ec529f271960ff10cedc3eb0636f3f0d6773a89934a7d1e90'
            '3b1b825d9b2dae860f1ed0e832f58da8fa469a118544cca0d6ef4751dacfdde3')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard
              '71046D1E9C6656BDD61171873E83BABF4A4F9E85'  # Cullen Rhodes
              'FFB3368980F3E6BB5737145A316C56D064CACBA5'  # Douglas Yung
              'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}.src"
  patch -p1 -i "${srcdir}/0001-cmake-copy-instead-symlink.patch"
  patch -p1 -i "${srcdir}/0002-fix-pkgconfig-file.patch"
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      "../${_realname}-${_pkgver}.src"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  install -Dm644 "${srcdir}"/${_realname}-${_pkgver}.src/LICENSE.TXT \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.TXT
}
