# Maintainer: Gene Harvey <gharveymn@gmail.com>

_realname=sundials
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.7.0
pkgrel=3
pkgdesc="SUite of Nonlinear and DIfferential/ALgebraic equation Solvers (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://computing.llnl.gov/projects/sundials"
msys2_repository_url="https://github.com/LLNL/sundials"
msys2_references=(
  'archlinux: sundials'
)
license=('spdx:BSD-3-Clause')
replaces=("${MINGW_PACKAGE_PREFIX}-cvode")
provides=("${MINGW_PACKAGE_PREFIX}-cvode")
conflicts=("${MINGW_PACKAGE_PREFIX}-cvode")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         $([[ ${CARCH} != x86_64 ]] || echo \
           "${MINGW_PACKAGE_PREFIX}-msmpi" \
           "${MINGW_PACKAGE_PREFIX}-petsc" \
           "${MINGW_PACKAGE_PREFIX}-superlu_dist"))
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: for running examples")
source=(https://github.com/LLNL/sundials/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz
        '0001-hash-size.patch'
        '0002-sundials-missing-export.patch'
        '0003-sundials-use-default-installdirs.patch'
        '0004-suitesparse-import-target.patch'
        '0008-sundials-petsc-pkg-config-module.patch')
sha256sums=('5f113a1564a9d2d98ff95249f4871a4c815a05dbb9b8866a82b13ab158c37adb'
            '9dd5c708bad0a592a7012c1bdfd91904867cd9efe89412d82706764b21c4537c'
            '7f119fbcc8a630a4e3443e3bba252dafb4b1567ced1bf1389052253529e97ddc'
            '310b9beb86426fd2f817391baf72c0f9aefe9dc31e737daed5cc0280b7693311'
            '753b127af0aa1fbbf03ab67104846f968bd476659cb151d65931b5e5beae3f0d'
            'd4a56ef02d87d8d17970733fe8391c12fea9465f093681a1091a24b2ad0a787a')

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

prepare() {
  cd "${_realname}-${pkgver}"

  apply_patch_with_msg \
    0001-hash-size.patch \
    0002-sundials-missing-export.patch \
    0003-sundials-use-default-installdirs.patch \
    0004-suitesparse-import-target.patch \
    0008-sundials-petsc-pkg-config-module.patch
}

_petsc_arch=dmo

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

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    export FFLAGS="-Qunused-arguments"
  fi

  if [[ ${CARCH} != x86_64 ]]; then
    # There are currently no msmpi libraries for Windows on ARM.
    # See: https://github.com/microsoft/Microsoft-MPI/issues/66
    # Dont' build the features that require msmpi.
    _extra_config+=("-DENABLE_MPI=OFF"
                    "-DENABLE_PETSC=OFF"
                    "-DENABLE_SUPERLUDIST=OFF")
  else
    _extra_config+=("-DBUILD_NVECTOR_MPIMANYVECTOR=ON"
                    "-DBUILD_NVECTOR_MPIPLUSX=ON"
                    "-DBUILD_NVECTOR_PARALLEL=ON"
                    "-DENABLE_MPI=ON"
                    "-DENABLE_PETSC=ON"
                    "-DENABLE_SUPERLUDIST=ON"
                    "-DPETSC_PKG_CONFIG_MODULE=petsc-${_petsc_arch}"
                    "-DSUNDIALS_LOGGING_ENABLE_MPI=OFF"
                    "-DSUPERLUDIST_INCLUDE_DIRS=${MINGW_PREFIX}/include/superlu_dist"
                    "-DSUPERLUDIST_CONFIG_PATH=${MINGW_PREFIX}/include/superlu_dist/superlu_dist_config.h"
                    "-DSUPERLUDIST_INDEX_SIZE=32"
                    "-DSUPERLUDIST_OpenMP=ON"
                    "-DMPI_C_COMPILER=${MINGW_PREFIX}/bin/mpicc.exe"
                    "-DMPI_CXX_COMPILER=${MINGW_PREFIX}/bin/mpicxx.exe")
  fi

  I_MPI_ROOT="" \
  MSYS2_ARG_CONV_EXCL="CMAKE_INSTALL_PREFIX=;EXAMPLES_INSTALL_PATH=" \
  ${MINGW_PREFIX}/bin/cmake \
    -D BUILD_ARKODE=ON \
    -D BUILD_CVODE=ON \
    -D BUILD_CVODES=ON \
    -D BUILD_FORTRAN_MODULE_INTERFACE=OFF \
    -D BUILD_IDA=ON \
    -D BUILD_IDAS=ON \
    -D BUILD_KINSOL=ON \
    -D BUILD_NVECTOR_MANYVECTOR=ON \
    -D BUILD_NVECTOR_OPENMP=ON \
    -D BUILD_NVECTOR_PTHREADS=ON \
    -D BUILD_SHARED_LIBS=ON \
    -D BUILD_STATIC_LIBS=ON \
    -D BUILD_SUNLINSOL_KLU=ON \
    -D BUILD_SUNLINSOL_LAPACKBAND=ON \
    -D BUILD_SUNLINSOL_LAPACKDENSE=ON \
    -D BUILD_TESTING=OFF \
    "${_extra_config[@]}" \
    -D CMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -D CMAKE_RUNTIME_OUTPUT_DIRECTORY="${srcdir}/build-${MSYSTEM}"/build-output/bin \
    -D CMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -D CMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -D ENABLE_CUDA=OFF \
    -D ENABLE_HIP=OFF \
    -D ENABLE_HYPRE=OFF \
    -D ENABLE_KLU=ON \
    -D ENABLE_LAPACK=ON \
    -D ENABLE_MAGMA=OFF \
    -D ENABLE_OPENMP=ON \
    -D ENABLE_OPENMP_DEVICE=OFF \
    -D ENABLE_PTHREAD=ON \
    -D ENABLE_RAJA=OFF \
    -D ENABLE_SUPERLUMT=OFF \
    -D ENABLE_SYCL=OFF \
    -D ENABLE_TRILINOS=OFF \
    -D ENABLE_XBRAID=OFF \
    -D EXAMPLES_ENABLE_C=ON \
    -D EXAMPLES_ENABLE_CXX=ON \
    -D EXAMPLES_ENABLE_F2003=OFF \
    -D EXAMPLES_INSTALL=ON \
    -D EXAMPLES_INSTALL_PATH=${MINGW_PREFIX}/share/sundials/examples \
    -D KLU_LIBRARY=${MINGW_PREFIX}/lib/libklu.dll.a \
    -D LAPACK_LIBRARIES=${MINGW_PREFIX}/lib/libopenblas.dll.a \
    -D SUITESPARSECONFIG_LIBRARY=${MINGW_PREFIX}/lib/libsuitesparseconfig.dll.a \
    -D SUNDIALS_BUILD_WITH_PROFILING=OFF \
    -D SUNDIALS_BUILD_WITH_MONITORING=ON \
    -D SUNDIALS_INDEX_SIZE=32 \
    -D SUNDIALS_PRECISION=DOUBLE \
    -DSUNDIALS_F77_FUNC_CASE=LOWER \
    -DSUNDIALS_F77_FUNC_UNDERSCORES=ONE \
    -B build-${MSYSTEM} \
    -S ${_realname}-${pkgver}

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

check() {
  # Skip tests requiring MPI.
  ${MINGW_PREFIX}/bin/cmake -Bbuild-${MSYSTEM} -S${_realname}-${pkgver} -DBUILD_TESTING=ON
  ${MINGW_PREFIX}/bin/cmake --build build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/ctest \
    --exclude-regex "mpi|parallel|petsc|superludist|slunrloc" \
    --test-dir build-${MSYSTEM}
}

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

  # Install the LICENSE and NOTICE files to the system licenses directory.
  install -D --mode=644  \
    --target-directory="${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname} \
    "${srcdir}/${_realname}-${pkgver}"/{LICENSE,NOTICE}

  ## Patch various files to be relocatable.

  # Patch the CMake export modules.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/sundials/SUNDIALSTargets*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
  done

  # Patch the CMake config module.
  sed -e "s|${PREFIX_WIN}|\$\{PACKAGE_PREFIX_DIR\}|g" \
      -i "${pkgdir}${MINGW_PREFIX}"/lib/cmake/sundials/SUNDIALSConfig.cmake

  sed -e "s|${MINGW_PREFIX}|\$\{PACKAGE_PREFIX_DIR\}|g" \
      -i "${pkgdir}${MINGW_PREFIX}"/lib/cmake/sundials/SUNDIALSConfig.cmake

  # Patch the CMakeLists.txt files in the examples.
  shopt -s globstar
  for _f in "${pkgdir}${MINGW_PREFIX}"/share/sundials/examples/**/CMakeLists.txt; do
    _fdir=$(dirname "${_f}")
    _relative_root=$(realpath --relative-to="${_fdir}" "${pkgdir}${MINGW_PREFIX}")
    sed -e "s|${PREFIX_WIN}|\$\{CMAKE_CURRENT_LIST_DIR\}/${_relative_root}|g"   -i "${_f}"
    sed -e "s|${MINGW_PREFIX}|\$\{CMAKE_CURRENT_LIST_DIR\}/${_relative_root}|g" -i "${_f}"
  done
}
