# Contributor: Oleg A. Khlybov <fougas@mail.ru>


# This package provides multiple library build flavors differentiated by
# the 3-character suffix XYZ as follows:
#
#  * X is the scalar type based on the Netlib's SDCZ notation:
#    - A for multiprecision or precision-neutral build
#    - S for single precision real
#    - D for double precision real
#    - C for single precision complex
#    - Z for double precision complex
#
#  * Y is the execution model:
#    - S for sequential code
#    - M for MPI parallel code
#    - T for multithreaded code, either bare threading or OpenMP, OpenACC etc.
#    - H for heterogeneous code with CUDA, OpenCL etc.
#
#  * Z is the build type:
#    - O for optimized build
#    - G for debugging build
#
# The suffix is used in the static and dynamic libraires as well as in PkgConfig .pc files.
# That is the ZMO suffix designates the optimized MPI parallel double precision complex library flavor.
# Consider the `pkg-config mumps-zmo --cflags` command to obtain the build-specific compilation flags.

_realname=mumps

pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.7.3
pkgrel=2
pkgdesc="Sparse direct SLAE solver (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://mumps-solver.org"
license=('spdx:CECILL-C')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-metis"
         "${MINGW_PACKAGE_PREFIX}-parmetis"
         "${MINGW_PACKAGE_PREFIX}-scotch"
         "${MINGW_PACKAGE_PREFIX}-scalapack")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-msmpi")
optdepends=("${MINGW_PACKAGE_PREFIX}-tcl: build & run test suite")
source=("https://mumps-solver.org/MUMPS_${pkgver}.tar.gz"
        "0001-makefile.inc.patch"
        "0002-skip-examples.patch"
        "0003-fix-setenv.patch"
        "mumps.tcl"
        "testme-0.tm"
        "buildme-0.tm"
        "xyz-0.tm")
sha256sums=('84a47f7c4231b9efdf4d4f631a2cae2bdd9adeaabc088261d15af040143ed112'
            'd4e3d06c316544304ac6fefd595f20fd5865e791c9377e031fbad49b6238406c'
            'bbefd4a5f841536132f32c1ffc684a58b778901cd777445ca817dbb34d983450'
            '11609d173fc0df7b1d7269c19d5d81b2fd05a3abc57eff8ba12de1597242236c'
            'aa8cc50e447eebb14492744c8be6bd8f731e0140c339a76cc31e54ea2f11813d'
            '8d8f5670ffdf34a10b0d027bff976131824e2e6b053f8a13d207ecddd4663d6d'
            'd0db2aa573c9aaf57b30cb3c2bd7c66e1677f3f468002f467da98b75943076b1'
            'd52709e3663d50ed793bfe991c571ec4ea5e6eaf72fcc920e1f8ba43d0ed7050')

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

prepare() {
  cd "${srcdir}/${_realname}_${pkgver}"
  apply_patch_with_msg \
    "0001-makefile.inc.patch" \
    "0002-skip-examples.patch" \
    "0003-fix-setenv.patch"
}

seq_pc="metis scotch openblas"
shm_pc=$seq_pc
mpi_pc="parmetis metis ptscotch scalapack openblas msmpi" # FIXME metis should come from parmetis

build() {
  cd "${srcdir}/${_realname}_${pkgver}"
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    export FC=gfortran
    export FFLAGS="-fallow-argument-mismatch -fallow-invalid-boz"
  else
    export FC=flang
  fi
  for mod in SEQ SHM PAR; do
    cp Makefile.inc.${mod} Makefile.inc
    make -j1 clean s d c z
    if [[ $mod == SEQ ]]; then
      cp libseq/libmpiseq_seq.a lib/libmumps_mpi_seq.a
      cp PORD/lib/libpord_seq.a lib/libmumps_pord.a
    fi
  done
  cd lib
  rm -rf libmumps-*.a
  strip -S *.a # Thin archives require the constituent archives to be stripped beforehand
  for x in c z s d; do
    ar crsT libmumps-${x}so.a lib${x}mumps_seq.a libmumps_common_seq.a libmumps_pord.a libmumps_mpi_seq.a
    ar crsT libmumps-${x}to.a lib${x}mumps_shm.a libmumps_common_shm.a libmumps_pord.a libmumps_mpi_seq.a
    ar crsT libmumps-${x}mo.a lib${x}mumps_mpi.a libmumps_common_mpi.a libmumps_pord.a
    # TODO export only the required driver routine to reduce the exported symbol table size
    ${FC} -shared -Wl,--enable-auto-import -Wl,--export-all-symbols -o libmumps-${x}so.dll -Wl,--out-implib,libmumps-${x}so.dll.a -Wl,--whole-archive libmumps-${x}so.a -Wl,--no-whole-archive -lesmumps $(pkg-config ${seq_pc} --libs)
    ${FC} -shared -fopenmp -Wl,--enable-auto-import -Wl,--export-all-symbols -o libmumps-${x}to.dll -Wl,--out-implib,libmumps-${x}to.dll.a -Wl,--whole-archive libmumps-${x}to.a -Wl,--no-whole-archive -lesmumps $(pkg-config ${shm_pc} --libs)
    mpifort -shared -fopenmp -Wl,--enable-auto-import -Wl,--export-all-symbols -o libmumps-${x}mo.dll -Wl,--out-implib,libmumps-${x}mo.dll.a -Wl,--whole-archive libmumps-${x}mo.a -Wl,--no-whole-archive -lptesmumps $(pkg-config ${mpi_pc} --libs)
  done
}

package() {
  cd "$srcdir/${_realname}_${pkgver}"
  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib,include/mumps/mpi_seq} ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig ${pkgdir}${MINGW_PREFIX}/share/test/mumps
  (
    cd libseq
    install -m644 mpi*.h "${pkgdir}${MINGW_PREFIX}/include/mumps/mpi_seq"
  )
  (
    cd lib
    rm -f libpord*.a
    install -m644 *.a "${pkgdir}${MINGW_PREFIX}/lib"
    install -m644 *.dll "${pkgdir}${MINGW_PREFIX}/bin"
  )
  (
    cd include
    install -m644 *.h "${pkgdir}${MINGW_PREFIX}/include"
  )
  (
    cp ${srcdir}/{*.tcl,*.tm} ${pkgdir}${MINGW_PREFIX}/share/test/mumps
    cd "$srcdir/${_realname}_${pkgver}/examples"
    cp c_example.c ?simpletest.F input_simpletest_* ${pkgdir}${MINGW_PREFIX}/share/test/mumps
  )
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _fc_libs="-lgfortran -lquadmath"
  fi
  for x in c z s d; do
    case $x in
      c) scalar='single precision complex';;
      z) scalar='double precision complex';;
      s) scalar='single precision';;
      d) scalar='double precision';;
    esac
    echo "
      prefix=${MINGW_PREFIX}
      libdir=\${prefix}/lib
      includedir=\${prefix}/include
      Name: ${_realname}
      URL: ${url}
      Version: ${pkgver}
      Description: Sequential $scalar MUMPS build
      Requires.private: ${seq_pc}
      Cflags: -I\${includedir} -I\${includedir}/mumps/mpi_seq
      Libs.private: -lmumps-${x}so -lesmumps ${_fc_libs}
      Libs: -lmumps-${x}so
    " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}-${x}so.pc"
    echo "
      prefix=${MINGW_PREFIX}
      libdir=\${prefix}/lib
      includedir=\${prefix}/include
      Name: ${_realname}
      URL: ${url}
      Version: ${pkgver}
      Description: OpenMP $scalar MUMPS build
      Requires.private: ${shm_pc}
      Cflags: -I\${includedir} -I\${includedir}/mumps/mpi_seq
      Libs.private: -fopenmp -lmumps-${x}to -lesmumps ${_fc_libs}
      Libs: -lmumps-${x}to
    " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}-${x}to.pc"
    echo "
      prefix=${MINGW_PREFIX}
      libdir=\${prefix}/lib
      includedir=\${prefix}/include
      Name: ${_realname}
      URL: ${url}
      Version: ${pkgver}
      Description: MPI+OpenMP $scalar MUMPS build
      Cflags: -I\${includedir}
      Requires.private: ${mpi_pc}
      Libs.private: -fopenmp -lmumps-${x}mo -lptesmumps ${_fc_libs}
      Libs: -lmumps-${x}mo
    " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}-${x}mo.pc"
  done
}
