# 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.6.2
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
pkgdesc="Sparse direct SLAE solver (mingw-w64)"
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"
         "${MINGW_PACKAGE_PREFIX}-msmpi")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
optdepends=("${MINGW_PACKAGE_PREFIX}-tcl: build & run test suite")
license=('spdx:CECILL-C')
url="https://mumps-solver.org"
source=("https://graal.ens-lyon.fr/MUMPS/MUMPS_${pkgver}.tar.gz"
        "0001-makefile.inc.patch"
        "0002-skip-examples.patch"
        "0003-fix-setenv.patch"
        "all.tcl"
        "tclbuildtest.tcl"
        "mumps.test")
sha256sums=('13a2c1aff2bd1aa92fe84b7b35d88f43434019963ca09ef7e8c90821a8f1d59a'
            'd4e3d06c316544304ac6fefd595f20fd5865e791c9377e031fbad49b6238406c'
            'bbefd4a5f841536132f32c1ffc684a58b778901cd777445ca817dbb34d983450'
            '11609d173fc0df7b1d7269c19d5d81b2fd05a3abc57eff8ba12de1597242236c'
            'ec5072630e1c0309fe383669e9187790cd135a393c67bc4bc35cf60b0ba396ff'
            '277a71049b2d4cde76ca21253b253ccd7ea9e8bcfb654868c05c6bfb137d2690'
            '58bc4cc4820cc91dc8cc67c2c95774b00c628fa56cc9c627894a5da08900b11b')

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}/{all.tcl,tclbuildtest.tcl,mumps.test} ${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
}
