# Maintainer: Philip Top <top1@llnl.gov>

_realname=helics
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.6.1
pkgrel=2
pkgdesc="Hierarchical Engine for Large-scale Infrastructure Co-Simulation (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/GMLC-TDC/HELICS"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-zeromq"
         "${MINGW_PACKAGE_PREFIX}-fmt")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost")
options=('!strip')
source=("https://github.com/GMLC-TDC/HELICS/releases/download/v${pkgver}/HELICS-v${pkgver}-source.tar.gz"
        "001-aarch64-processor-name.patch")
sha256sums=('d607c1b47dd5ae32f3076c4aa4aa584d37b6056a9bd049234494698ed95cd70f'
            '5853683dc368453155bf690fb9082b98e88da5d854de8f2da8f32830996f0013')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"

  apply_patch_with_msg \
    001-aarch64-processor-name.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 \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_CXX_STANDARD=20 \
    -DBUILD_SHARED_LIBS=ON \
    -DHELICS_ENABLE_IPC_CORE=OFF \
    -DHELICS_DISABLE_GIT_OPERATIONS=ON \
    -DHELICS_DISABLE_VCPKG=ON \
    -DHELICS_DISABLE_ASIO=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo "OFF" || echo "ON") \
    -DHELICS_USE_EXTERNAL_FMT=ON \
    ${extra_config} \
    ..

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

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