# Maintainer: Martin Diehl <aur@martin-diehl.net>
# Contributor: Alad Wenter <alad@archlinux.org>
# Contributor: Jingbei Li <i@jingbei.li>
# Contributor: Simon Pintarelli <simon.pintarelli@gmail.com>
# Contributor: Feng Wang <wanng.fenng@gmail.com>
# Contributor (msys): Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=trilinos
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=16.1.0
_pkgver=${pkgver//./-}
pkgrel=2
pkgdesc='Algorithms for the solution of large-scale scientific problems (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://trilinos.org"
msys2_repository_url="https://github.com/trilinos/Trilinos"
license=('spdx:BSD-3-Clause AND LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-fc"
             #"${MINGW_PACKAGE_PREFIX}-gtest"
             #"${MINGW_PACKAGE_PREFIX}-boost"
             #"${MINGW_PACKAGE_PREFIX}-matio"
             #$([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-msmpi")
             #"${MINGW_PACKAGE_PREFIX}-netcdf"
             "${MINGW_PACKAGE_PREFIX}-perl"
             "${MINGW_PACKAGE_PREFIX}-bc"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "unzip")
optdepends=("${MINGW_PACKAGE_PREFIX}-seacas")
provides=("${MINGW_PACKAGE_PREFIX}-kokkos")
source=("https://github.com/${_realname}/Trilinos/archive/${_realname}-release-${_pkgver}.tar.gz"
        "001-aarch64-clang.patch")
sha256sums=('e9651c88f581049457036cfc01b527a9d3903c257338eeeab942befd7452f23a'
            '4aa0ec733709ba9dc9f7425efb2fec5ae975c2be99601e06c5a82ef9933218ee')
noextract=(${_realname}-release-${_pkgver}.tar.gz)

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

prepare(){
  tar -xzf ${_realname}-release-${_pkgver}.tar.gz || true

  cd "Trilinos-${_realname}-release-${_pkgver}"

  apply_patch_with_msg \
    001-aarch64-clang.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

  if [[ ${MSYSTEM} != CLANG* ]]; then
    extra_config+=("-DCMAKE_Fortran_FLAGS='$FCFLAGS -fallow-argument-mismatch'")
  fi

  if [[ "${CARCH}" == "aarch64" ]]; then
    extra_config+=("-DKokkos_ARCH_ARMV81=ON")
  fi  

  #Static Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBLAS_LIBRARY_NAMES="openblas" \
      -DLAPACK_LIBRARY_NAMES="openblas" \
      -DTrilinos_ENABLE_Fortran=ON \
      -DTrilinos_ENABLE_ALL_OPTIONAL_PACKAGES=ON \
      -DTrilinos_ENABLE_ALL_PACKAGES=ON \
      -DTPL_ENABLE_DLlib=OFF \
      -DTrilinos_ENABLE_Xpetra=OFF \
      -DTrilinos_ENABLE_Epetra=ON \
      -DTrilinos_ENABLE_Intrepid2=OFF \
      -DTrilinos_ENABLE_SEACAS=OFF \
      -DTrilinos_ENABLE_Gtest=OFF \
      -DTPL_ENABLE_gtest=OFF \
      -DTrilinos_ENABLE_TESTS=OFF \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      ../Trilinos-${_realname}-release-${_pkgver}

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

  #Shared Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBLAS_LIBRARY_NAMES="openblas" \
      -DLAPACK_LIBRARY_NAMES="openblas" \
      -DTrilinos_ENABLE_Fortran=ON \
      -DTrilinos_ENABLE_ALL_OPTIONAL_PACKAGES=ON \
      -DTrilinos_ENABLE_ALL_PACKAGES=ON \
      -DTPL_ENABLE_DLlib=OFF \
      -DTrilinos_ENABLE_Xpetra=OFF \
      -DTrilinos_ENABLE_Epetra=ON \
      -DTrilinos_ENABLE_Intrepid2=OFF \
      -DTrilinos_ENABLE_SEACAS=OFF \
      -DTrilinos_ENABLE_Gtest=OFF \
      -DTPL_ENABLE_gtest=OFF \
      -DTrilinos_ENABLE_TESTS=OFF \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      ../Trilinos-${_realname}-release-${_pkgver}

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

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

  #Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}"/Trilinos-${_realname}-release-${_pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  # Remove conflict with seacas package
  #rm -r "${pkgdir}"${MINGW_PREFIX}/lib/external_packages/DLlib
}
