# 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.0.0
_pkgver=${pkgver//./-}
pkgrel=4
pkgdesc='Algorithms for the solution of large-scale scientific problems" (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
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}-openblas"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-matio"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-msmpi")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-gtest"
             "${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"
        "https://github.com/trilinos/Trilinos/commit/b7a7a71d9ab66970e082474775952348b5a0eea4.patch")
sha256sums=('46bfc40419ed2aa2db38c144fb8e61d4aa8170eaa654a88d833ba6b92903f309'
            '68eac70c267007fe50e23e9a988848a9801f11498296d574fb000d6ecf9f3d6d')
noextract=(${_realname}-release-${_pkgver}.tar.gz)

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

  patch -Np1 -i ../b7a7a71d9ab66970e082474775952348b5a0eea4.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

  #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 \
      ../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 \
      ../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
}
