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

_realname=siesta
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.4.1
pkgrel=2
pkgdesc="A first-principles materials simulation code using DFT. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64')
url='https://siesta-project.org/siesta'
msys2_repository_url="https://gitlab.com/siesta-project/siesta"
msys2_references=(
  'aur: siesta'
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-s-dftd3"
         "${MINGW_PACKAGE_PREFIX}-libfdf"
         "${MINGW_PACKAGE_PREFIX}-libgridxc"
         "${MINGW_PACKAGE_PREFIX}-libpsml"
         #"${MINGW_PACKAGE_PREFIX}-libxc"
         "${MINGW_PACKAGE_PREFIX}-mctc-lib"
         "${MINGW_PACKAGE_PREFIX}-netcdf-fortran"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-scalapack"
         "${MINGW_PACKAGE_PREFIX}-xmlf90")
makedepends=("${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-msmpi"
             "${MINGW_PACKAGE_PREFIX}-python")
_w90_ver=3.1.0
source=("https://gitlab.com/siesta-project/siesta/-/releases/${pkgver}/downloads/${_realname}-${pkgver}.tar.gz"
        "https://github.com/wannier-developers/wannier90/archive/v${_w90_ver}/wannier90-${_w90_ver}.tar.gz"
        "001-fix-build-on-mingw.patch")
sha256sums=('531ed2fa021d6a733739b5c1ba7d8cf32fe5aee3df6774a8e748ba3d6faaebcc'
            '40651a9832eb93dec20a8360dd535262c261c34e13c41b6755fa6915c936b254'
            '6c5c587e1b66b94391e090688edc36daca9033360113148df4a5c43b8f619752')
noextract=("wannier90-${_w90_ver}.tar.gz")

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

  patch -Np1 -i "${srcdir}"/001-fix-build-on-mingw.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

  local _fflags=""
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _fflags+=" -fallow-invalid-boz"
  fi

  if [[ ${CARCH} == x86_64 ]]; then
    _fflags+=" -march=x86-64-v3"
    CFLAGS+=" -march=x86-64-v3"
  fi

  FFLAGS="${_fflags}" \
  WANNIER90_PACKAGE="${srcdir}"/wannier90-${_w90_ver}.tar.gz \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DSIESTA_SHARED_LIBS=ON \
      -DSIESTA_WITH_ELPA=OFF \
      -DSIESTA_WITH_ELSI=OFF \
      -DSIESTA_WITH_FLOOK=OFF \
      -DSIESTA_WITH_IPO=OFF \
      -DSIESTA_SET_RPATH=OFF \
      -DSIESTA_WITH_FFTW=ON \
      -DSIESTA_WITH_LIBXC=ON \
      -DSIESTA_WITH_MPI=ON \
      -DSIESTA_WITH_NETCDF=ON \
      -DSIESTA_WITH_OPENMP=ON \
      -DSIESTA_WITH_WANNIER90=ON \
      -DSIESTA_TESTS=OFF \
      -DBLA_VENDOR=OpenBLAS \
      -DSCALAPACK_LIBRARY=${MINGW_PREFIX}/lib/libscalapack.dll.a \
      -DFortran_FLAGS_RELEASE="-O3" \
      -DC_FLAGS_RELEASE="-O3" \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

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