# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=onnx
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=1.19.1
pkgrel=2
pkgdesc="Open standard for machine learning interoperability. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://www.onnx.ai'
msys2_repository_url="https://github.com/onnx/onnx"
msys2_references=(
  'purl: pkg:pypi/onnx'
)
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-protobuf"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-python-protobuf")
source=("https://github.com/onnx/onnx/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-build-on-mingw-w64.patch")
sha256sums=('ce9d2569a61d64e8a3d05b92194f60ffb7c868dbb754a71f5b4d992273a9413d'
            'af25c1193b9bdb6e9ae07640fad4ed42857c1b4961c1b4c2f8c05a6cb80a0870')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-on-mingw-w64.patch
}

build() {
  rm -rf "python-build-${MSYSTEM}"
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

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

  _cmake_args+=(
    "-Wno-dev"
    "-GNinja"
    "-DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}"
    "-DONNX_USE_PROTOBUF_SHARED_LIBS=ON"
  )

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _cmake_args+=("-DBUILD_SHARED_LIBS=OFF" )
  else
    _cmake_args+=("-DBUILD_SHARED_LIBS=ON")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ONNX_BUILD_TESTS=0 \
  CMAKE_ARGS="${_cmake_args[@]}" \
  python -m build --wheel --skip-dependency-check --no-isolation
}

package_onnx() {
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
             "${MINGW_PACKAGE_PREFIX}-protobuf")
  fi

  DESTDIR="${pkgdir}" cmake --install python-build-${MSYSTEM}/.setuptools-cmake-build

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

package_python-onnx() {
  depends=("${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-python-protobuf"
           "${MINGW_PACKAGE_PREFIX}-python-numpy"
           "${MINGW_PACKAGE_PREFIX}-python-ml_dtypes"
           "${MINGW_PACKAGE_PREFIX}-python-typing_extensions"
           "${MINGW_PACKAGE_PREFIX}-onnx"
           "${MINGW_PACKAGE_PREFIX}-cc-libs"
           "${MINGW_PACKAGE_PREFIX}-protobuf")
  options=(!strip)

  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

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

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
