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

_realname=opencl-icd
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2024.10.24
pkgrel=1
pkgdesc="OpenCL ICD Loader (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/KhronosGroup/OpenCL-ICD-Loader'
msys2_references=(
  'aur: mingw-w64-opencl-icd'
)
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-opencl-headers"
             "${MINGW_PACKAGE_PREFIX}-tools")
source=("https://github.com/KhronosGroup/OpenCL-ICD-Loader/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-do-not-use-mingw32-def-on-clang32.patch")
sha256sums=('95f2f0cda375b13d2760290df044ebea9c6ff954a7d7faa0867422442c9174dc'
            'eb947c742dd31f2d1cc18ec8eb9af686385cc8634a9c56a3c4fb36d530d8280f')

prepare() {
  cd "${srcdir}"/OpenCL-ICD-Loader-${pkgver}
  patch -p1 -i "${srcdir}"/001-do-not-use-mingw32-def-on-clang32.patch
}

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

  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'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DOPENCL_ICD_LOADER_DISABLE_OPENCLON12=ON \
      -DOPENCL_ICD_LOADER_HEADERS_DIR=${MINGW_PREFIX}/include \
      -DBUILD_TESTING=OFF \
      ../OpenCL-ICD-Loader-${pkgver}

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

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

  install -Dm644 "${srcdir}"/OpenCL-ICD-Loader-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
