# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           github 1.0
PortGroup           mpi 1.0

github.setup        AlphaSparse AlphaSparse 57eefee000734f40d9e972cbd110fbc4c51a71d3
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
version             2023.06.19
revision            1
categories          math
license             BSD
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
description         Sparse BLAS lib supporting multiple backends
long_description    AlphaSparse aims to build a common interface that provides BLAS \
                    for sparse computation for diverse multi-core and many-core processors, \
                    and expects to be extended on distributed and heterogeneous platforms. \
                    AlphaSparse is created using the basic C/C++ and can be deployed \
                    on both CPU and DCU (HIP-based many-core platform).
checksums           rmd160  ad56116e0c4e9672bfb82415540ea459c8a7777a \
                    sha256  d61b35bf14ab7e6edf12e54a8a5e1a158fa8b453409159462f21dfba3aa45c3e \
                    size    9093990

patch.pre_args-replace  -p0 -p1
patchfiles-append   0001-Support-PPC-in-Makefile.patch \
                    0002-Makefile-cleanup.patch \
                    0003-Fix-for-missing-COMPAR_FN_T.patch \
                    0004-Use-stdlib.h-instead-of-malloc.h.patch \
                    0005-Remove-some-unneeded-spaces.patch \
                    0006-Makefile-for-x86-test-fix-lib-name-to-libalpha_spbla.patch \
                    0007-src-Makefile-there-is-no-tuning-dir.patch \
                    0008-Makefile-fix-dylib-target-for-macOS.patch \
                    0009-Fixes-for-Macports.patch \
                    0010-Makefile.tail-split-lib-target-to-fix-ar-error.patch \
                    0011-Fix-some-includes.patch

post-extract {
    # These dirs are needed but not created automatically:
    xinstall -d ${worksrcpath}/asm
    xinstall -d ${worksrcpath}/bin
    xinstall -d ${worksrcpath}/lib
    xinstall -d ${worksrcpath}/obj
    # This is an ugly workaround for ar: Argument list too long error:
    xinstall -d ${worksrcpath}/src/plain/kernel_1
    xinstall -d ${worksrcpath}/src/plain/kernel_2
    xinstall -d ${worksrcpath}/src/plain/kernel_3
    xinstall -d ${worksrcpath}/src/plain/kernel_4
    set l1 ${worksrcpath}/src/plain/kernel/level1
    set l2 ${worksrcpath}/src/plain/kernel/level2
    set l3a ${worksrcpath}/src/plain/kernel/level3/add
    set l3b ${worksrcpath}/src/plain/kernel/level3/mm
    set l3c ${worksrcpath}/src/plain/kernel/level3/set
    set l3d ${worksrcpath}/src/plain/kernel/level3/spmm
    set l3e ${worksrcpath}/src/plain/kernel/level3/spmmd
    set l3f ${worksrcpath}/src/plain/kernel/level3/trsm
    set l3g ${worksrcpath}/src/plain/kernel/level3/update
    fs-traverse f [list ${l1} ${l2}] {
        if {[file isfile ${f}] && [file extension ${f}] == ".c"} {
            move ${f} ${worksrcpath}/src/plain/kernel_1/
        }
    }
    fs-traverse f ${l3a} {
        if {[file isfile ${f}] && [file extension ${f}] == ".c"} {
            move ${f} ${worksrcpath}/src/plain/kernel_2/
        }
    }
    fs-traverse f ${l3b} {
        if {[file isfile ${f}] && [file extension ${f}] == ".c"} {
            move ${f} ${worksrcpath}/src/plain/kernel_3/
        }
    }
    fs-traverse f [list ${l3c} ${l3d} ${l3e} ${l3f} ${l3g}] {
        if {[file isfile ${f}] && [file extension ${f}] == ".c"} {
            move ${f} ${worksrcpath}/src/plain/kernel_4/
        }
    }
    file copy ${worksrcpath}/src/plain/kernel/Makefile ${worksrcpath}/src/plain/kernel_1
    file copy ${worksrcpath}/src/plain/kernel/Makefile ${worksrcpath}/src/plain/kernel_2
    file copy ${worksrcpath}/src/plain/kernel/Makefile ${worksrcpath}/src/plain/kernel_3
    file copy ${worksrcpath}/src/plain/kernel/Makefile ${worksrcpath}/src/plain/kernel_4
    file delete -force ${worksrcpath}/src/plain/kernel
}

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/Makefile
    reinplace "s|@CC@|${configure.cc}|g" ${worksrcpath}/Makefile
    reinplace "s|kernel op op_|kernel_1 kernel_2 kernel_3 kernel_4 op op_|" ${worksrcpath}/src/plain/Makefile
}

compiler.c_standard     2011
compiler.cxx_standard   2011

# Xcode clang does not support OpenMP:
# error: unsupported option '-fopenmp': unsupported option '-fopenmp'
# mpich-clang16 does not build it.
# mpich-clang15 and mpich-gcc13 work fine.
compiler.blacklist-append \
                    {clang} {macports-clang-1[6-9]}

if {(${os.platform} eq "darwin" && ${os.arch} eq "powerpc") ||
    ${configure.cxx_stdlib} ne "libc++"} {
    mpi.setup       require require_fortran \
                    -gcc44 -gcc45 -gcc46 -gcc47 -gcc48 -gcc49 -gcc5 -gcc6 -gcc7 -gcc8 \
                    -clang -fortran
} else {
    mpi.setup       require require_fortran \
                    -gcc44 -gcc45 -gcc46 -gcc47 -gcc48 -gcc49 -gcc5 -gcc6 -gcc7 -gcc8
}

use_configure       no

# Other targets are broken, build static lib. If dylib target is fixed, add so below:
build.target        lib

build.args-append   OPENMP=1 \
                    PLAIN_ON=1 \
                    ASM_COMPILE=0 \
                    HIP_ON=0 \
                    HYGON_ON=0

if {${build_arch} eq "arm64"} {
    build.args-append \
                    ARM_ON=1
}

# Rosetta-specific fix:
platform darwin 10 powerpc {
    patchfiles-append \
                    0012-Hack-for-Rosetta.patch
}

if {[string match *clang* ${configure.compiler}]} {
    patchfiles-append \
                    0013-patch-clang.diff
}

destroot {
    copy ${worksrcpath}/include/alphasparse.h ${destroot}${prefix}/include
    copy ${worksrcpath}/include/alpha_spblas_dcu.h ${destroot}${prefix}/include
    copy ${worksrcpath}/include/alphasparse ${destroot}${prefix}/include
    copy ${worksrcpath}/lib/libalpha_spblas.a ${destroot}${prefix}/lib
#   copy ${worksrcpath}/lib/libalpha_spblas.dylib ${destroot}${prefix}/lib
}
