# -*- 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           cmake 1.1
PortGroup           compilers 1.0
PortGroup           github 1.0
PortGroup           muniversal 1.1
PortGroup           xcode_workaround 1.0

name                OpenBLAS
categories          math science
license             BSD
maintainers         {nicos @NicosPavlov} {michaelld @michaelld} {@catap korins.ky:kirill} openmaintainer
description         OpenBLAS is an optimized BLAS library based on GotoBLAS2
long_description    {*}${description}

# Block compilers that do not support thread-local storage.
compiler.cxx_standard 2011
compiler.c_standard   2011
compiler.thread_local_storage yes

if {${os.major} < 9} {
    # For older versions, we force native variant as there is no PPCG3 target in OpenBLAS
    default_variants-append +native
}

subport OpenBLAS-devel {}
if {[string first "-devel" $subport] > 0} {

    github.setup    OpenMathLib OpenBLAS b1ae777afb071f3a80e6646ceaa587c4d2e10d23
    github.tarball_from archive
    version         20240209-[string range ${github.version} 0 7]
    checksums       rmd160  8666fb64ff9128a4674772e243e02c22b1a1b799 \
                    sha256  8f45efabd83bd1bcefd721a0a72330172f7f6ee0d725099713c7a2a88c805e24 \
                    size    24580697
    revision        0

    conflicts       OpenBLAS

    patchfiles      patch-OpenBLAS-i386-Apple.diff

    github.livecheck.branch develop

    # Not using precompiled binaries in -devel support
    default_variants-append +native

} else {

    github.setup    OpenMathLib OpenBLAS 0.3.25 v
    github.tarball_from releases
    checksums       rmd160  eea6fe2c33762df4c47c7241808dabbdb085eed3 \
                    sha256  4c25cb30c4bb23eddca05d7d0a85997b8db6144f5464ba7f8c09ce91e2f35543 \
                    size    24073168
    revision        6

    conflicts       OpenBLAS-devel

    patchfiles      patch-libnoarch.release.diff \
                    patch-xcode15-fix.diff \
                    patch-openblas.pc-fixed-version-and-url.diff \
                    patch-enable-overstep-of-too-long-args-without-DYNAMIC_ARCH.diff \
                    patch-ar-options-old-macos.diff \
                    patch-OpenBLAS-old-macos.diff \
                    patch-OpenBLAS-i386-Apple.diff \
                    patch-system_check.cmake-fix-arch-detection-for-Darwin-Pow.diff \
                    patch-cpuid_power.c-add-CPU_SUBTYPE_POWERPC_7400-case.diff \
                    patch-KERNEL.PPC970-KERNEL.PPCG4-unbreak-CMake-parsing.diff \
                    patch-cc.cmake-add-optflags-for-G5-and-G4-kernels.diff \
                    patch-cc.cmake-use-force_cpusubtype_ALL-for-Darwin-PPC.diff \
                    patch-PPC970-drop-mcpu-970-which-seems-to-produce-faulty-c.diff

    if {![variant_isset native]} {
        notes "
        This version is built based on a base architecture for convenience,
        which may not be optimized for your system. To build a version
        customized for your machine, use the +native variant"
    }
}

compilers.choose    fc

compilers.setup     default_fortran

variant lapack description "Add Lapack/CLapack support to the library" { }
default_variants-append +lapack

variant native description "Force compilation on machine to get fully optimized library" {
    # Prevent precompiled binaries to let compilation optimise the library for the user processor
    archive_sites
}

# Workaround for test failure :-
# > ./sblat2 < ./sblat2.dat
# Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
xcode_workaround.fixed_xcode_version 11.2
xcode_workaround.type append_to_compiler_name

if {${muniversal.build_arch} in [list arm64 ppc64 x86_64]} {
    configure.args-append \
                    -DBINARY64=ON
}

configure.cflags    -O3
configure.args-append \
                    -DCOMMON_PROF=-pg

if {![variant_isset lapack]} {
    configure.args-append \
                    -DBUILD_WITHOUT_LAPACK=ON
}

if {![variant_isset native]} {
    # Do not limit threads to the number on buildbots
    configure.args-append \
                    -DNUM_THREADS=56

    switch ${muniversal.build_arch} {
        arm64 {
            configure.args-append \
                    -DTARGET=ARMV8
        }
        x86_64 {
            if {${os.major} >= 18} {
                configure.args-append \
                    -DTARGET=NEHALEM
            } else {
                configure.args-append \
                    -DTARGET=CORE2
            }
        }
        i386 {
            configure.args-append \
                    -DTARGET=YONAH
        }
        ppc {
            configure.args-append \
                    -DTARGET=PPCG4
        }
        ppc64 {
            configure.args-append \
                    -DTARGET=PPC970
        }
        default {

        }
    }
}

if {${muniversal.build_arch} ni [list i386 x86_64]} {
    # not on x86 of some sort; just disable AVX
    configure.args-append \
                    -DNO_AVX=ON \
                    -DNO_AVX2=ON \
                    -DNO_AVX512=ON
} else {
    if {![avx_compiler_isset]} {
            configure.args-append \
                    -DNO_AVX=ON
    }
    # Disable AVX2 on OSX10.6 and older only.
    # On newer platforms AVX2 is now supported via
    # https://github.com/macports/macports-ports/pull/3486
    if { ${os.major} <= 10 } {
        configure.args-append \
                    -DNO_AVX2=ON
    }
    # Deactivating AVX512 instructions which do not build
    # at this point. Keeping them in -devel subport. See
    # https://trac.macports.org/ticket/57912
    if {[string first "-devel" $subport] < 1} {
        configure.args-append \
                    -DNO_AVX512=ON
    }
}

configure.args-append       -DCMAKE_AR=${prefix}/bin/ar \
                            -DCMAKE_NM=${prefix}/bin/nm \
                            -DCMAKE_OBJDUMP=${prefix}/bin/objdump \
                            -DCMAKE_RANLIB=${prefix}/bin/ranlib \
                            -DCMAKE_STRIP=${prefix}/bin/strip \
                            -DCMAKE_LINKER=${prefix}/bin/ld

depends_build-append        path:libexec/coreutils/libstdbuf.so:coreutils \
                            port:cctools \
                            port:ld64
depends_skip_archcheck      coreutils \
                            cctools \
                            ld64

muniversal.combine          ${prefix}/lib/pkgconfig/openblas.pc

# Build a shared library, not static!
configure.args-append       -DBUILD_SHARED_LIBS=ON

# compilers PG adds its own rpath to make GC happy, and cmake adds rpath which
# leads to two rpath and Sonoma linker failed on duplicated rpath. To avoid that
# mess let me remove cmake's rpath.
configure.pre_args-delete   -DCMAKE_INSTALL_RPATH=${prefix}/lib

configure.pre_args-replace  -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
                            -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF

cmake.install_rpath
compilers.add_gcc_rpath_support no

post-destroot {
    # For compatibility, put header files in ${prefix}/include
    foreach h [glob -tails -directory "${destroot}${prefix}/include/openblas" *.h] {
        if {${h} eq "cblas.h"} {
            ln -s openblas/${h} ${destroot}${prefix}/include/cblas_openblas.h
        } else {
            ln -s openblas/${h} ${destroot}${prefix}/include/${h}
        }
    }

    if {${muniversal.build_arch} ne ${configure.build_arch}} {
        reinplace "s|^openblas_config=|#openblas_config=|" ${destroot}${prefix}/lib/pkgconfig/openblas.pc
    }
}
