# -*- 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           compiler_blacklist_versions 1.0
PortGroup           compilers 1.0
PortGroup           github 1.0

name                fortran-stdlib
github.setup        fortran-lang stdlib 8c7461dee1f10ea7042c6819aa11d96d9ebbc519
version             2024.01.17
revision            0
categories-append   lang fortran devel
license             MIT
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
description         Fortran Standard Library
long_description    {*}${description}
checksums           rmd160  cb59ebc5b114a1ab050418b4f10e64acb451afea \
                    sha256  afce968ca33e1f017401d0a06e3714e9391c54809588ed215b5ff1e504b2744b \
                    size    395152
github.tarball_from archive

cmake.generator     Ninja

set py_ver          3.11
set py_ver_nodot    [string map {. {}} ${py_ver}]
depends_build-append \
                    port:py${py_ver_nodot}-fypp \
                    port:python${py_ver_nodot}

platform darwin {
    if {${os.major} < 13} {
        depends_build-append    port:git
        git.cmd                 ${prefix}/bin/git
    }
}

compilers.setup     require_fortran -g95
compiler.blacklist-append \
                    {*gcc-[34].*} {macports-gcc-[58]} {clang < 500}

configure.args-append \
                    -DPython_EXECUTABLE=${prefix}/bin/python${py_ver} \
                    -DFYPP=${prefix}/bin/fypp-${py_ver} \
                    -DCMAKE_BUILD_TYPE=NoConfig \
                    -DCMAKE_MAXIMUM_RANK:String=7 \
                    -DBUILD_SHARED_LIBS=ON \
                    -DBUILD_TESTING=OFF

# TODO: add gcc13 after it is tested and confirmed to build fine.
# The experimental port was added in https://github.com/macports/macports-ports/pull/20607
platform darwin powerpc {
    # https://github.com/fortran-lang/stdlib/issues/690
    # This will normally fail, since atm GCC does not support ieee_arithmetic on Darwin PPC;
    # however, we rather make it conditional on the module actually being present for a given compiler.
    # See discussion here: https://github.com/iains/darwin-toolchains-start-here/discussions/40

    # These have to be updated following GCC releases:
    if {[string match macports-gcc-10 ${configure.compiler}]} {
        set gcc_v_major 10
        set gcc_v_full  ${gcc_v_major}.4.0
    } elseif {[string match macports-gcc-11 ${configure.compiler}]} {
        set gcc_v_major 11
        set gcc_v_full  ${gcc_v_major}.4.0
    } elseif {[string match macports-gcc-12 ${configure.compiler}]} {
        set gcc_v_major 12
        set gcc_v_full  ${gcc_v_major}.3.0
    } elseif {[string match macports-gcc-13 ${configure.compiler}]} {
        set gcc_v_major 13
        set gcc_v_full  ${gcc_v_major}.2.0
    } elseif {[string match macports-gcc-devel ${configure.compiler}]} {
        set gcc_v_major -devel
        set gcc_v_full  14.0.0
    }
    
    if {${build_arch} eq "ppc"} {
        # Macports presently sets incorrect dir name on PowerPC, using build_arch instead of powerpc*; check both:
        if {![file exists "${prefix}/lib/gcc${gcc_v_major}/gcc/powerpc-apple-darwin${os.major}/${gcc_v_full}/finclude/ieee_arithmetic.mod"] \
            && ![file exists "${prefix}/lib/gcc${gcc_v_major}/gcc/ppc-apple-darwin${os.major}/${gcc_v_full}/finclude/ieee_arithmetic.mod]"]} {
            known_fail         yes

            pre-fetch {
                ui_error "Building ${name} requires ieee_arithmetic module in Gfortran, however it is absent in this GCC version.\
                    There is an experimental branch with ieee_arithmetic support for Darwin PowerPC here: https://github.com/barracuda156/gcc-git/tree/gfortran"
                return -code error "Unsupported compiler."
            }
        }
    }

    if {${build_arch} eq "ppc64"} {
        # Macports presently sets incorrect dir name on PowerPC, using build_arch instead of powerpc*; check both:
        if {![file exists "${prefix}/lib/gcc${gcc_v_major}/gcc/powerpc64-apple-darwin${os.major}/${gcc_v_full}/finclude/ieee_arithmetic.mod"] \
            && ![file exists "${prefix}/lib/gcc${gcc_v_major}/gcc/ppc64-apple-darwin${os.major}/${gcc_v_full}/finclude/ieee_arithmetic.mod]"]} {
            known_fail         yes

            pre-fetch {
                ui_error "Building ${name} requires ieee_arithmetic module in Gfortran, however it is absent in this GCC version.\
                    There is an experimental branch with ieee_arithmetic support for Darwin PowerPC here: https://github.com/barracuda156/gcc-git/tree/gfortran"
                return -code error "Unsupported compiler."
            }
        }
    }

    # See: https://github.com/fortran-lang/stdlib/issues/694
    configure.args-append \
                    -DWITH_XDP=OFF
}

variant tests description "Enable testing" {
    configure.pre_args-replace \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF
    configure.args-replace \
                    -DBUILD_TESTING=OFF \
                    -DBUILD_TESTING=ON
    test.run        yes
    test.cmd        ctest
}
