# Maintainer: Peter Budai <peterbudai at hotmail.com>

_realname=opencl-icd
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=47.c7fda8b
pkgrel=1
pkgdesc="OpenCL ICD Loader (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://github.com/KhronosGroup/OpenCL-ICD-Loader'
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-opencl-headers")
makedepends=(git
            "${MINGW_PACKAGE_PREFIX}-gcc"
            "${MINGW_PACKAGE_PREFIX}-cmake"
            "${MINGW_PACKAGE_PREFIX}-tools")
source=("${_realname}"::"git+https://github.com/KhronosGroup/OpenCL-ICD-Loader.git#branch=master"
        01-mingw-build.patch)
sha256sums=('SKIP'
            'a06d3b1ee9849c464a6d66bb3ee06e18fb0c6b23a2e46b89acff7303436423ac')

pkgver() {
  cd "${srcdir}"/${_realname}
  printf "%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/${_realname}
  patch -p1 -i "${srcdir}"/01-mingw-build.patch
}

build() {
  cd "${srcdir}"/${_realname}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DOPENCL_ICD_LOADER_HEADERS_DIR=${MINGW_PREFIX}/include \
      -DBUILD_TESTING=OFF \
      ../${_realname}

  make
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make DESTDIR=${pkgdir} install
  
  if [ "${CARCH}" = "i686" ]; then
    rm -f ${pkgdir}${MINGW_PREFIX}/lib/libOpenCL.dll.a
    ${MINGW_PREFIX}/bin/gendef ${pkgdir}${MINGW_PREFIX}/bin/libOpenCL.dll
    ${MINGW_PREFIX}/bin/dlltool -k -d libOpenCL.def -l ${pkgdir}${MINGW_PREFIX}/lib/libOpenCL.dll.a
  fi

  install -Dm644 ${srcdir}/${_realname}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
