# -*- 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

name                fortran-stdlib
github.setup        fortran-lang stdlib 0.8.1 v
revision            1
epoch               1
categories-append   lang fortran devel science
conflicts           fortran-stdlib-devel
license             MIT
maintainers         {@barracuda156 macos-powerpc.org:barracuda}
description         Fortran Standard Library
long_description    {*}${description}
checksums           rmd160  3752fc7b9a3421698d1a39b35acfc8ae059f179f \
                    sha256  6d20b120a4b17fb23ee5353408f6826b521bd006cd42eb412b01984eb9c31ded \
                    size    2367641
github.tarball_from archive

subport fortran-stdlib-devel {
    github.setup    fortran-lang stdlib d57c77690288aa11690344b92e6edfc414b151b7
    version         2025.11.30
    revision        1
    conflicts       fortran-stdlib
    checksums       rmd160  942ae372ed01bb581adf7b6430f17890fad41835 \
                    sha256  60b117ef8d6c837961ae6b6763e48621838278537824d89e7e2ad6e413b712c5 \
                    size    2351620
    github.tarball_from archive

    # Drop on the next update.
    # https://github.com/fortran-lang/stdlib/issues/1063
    patchfiles-append \
                    patch-PR1064.diff
}

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    path:bin/git:git
        git.cmd                 ${prefix}/bin/git
    }
}

# Installation uses compiler-specific paths,
# so we should not use a generic gfortran.
# https://github.com/fortran-lang/stdlib/issues/846
compilers.setup     require_fortran -g95 -gfortran
compiler.blacklist-append \
                    {*gcc-[34].*} {macports-gcc-[58]} {clang < 500}
# Notice that this port requires ieee_arithmetic support in gfortran,
# which may not be available for some targets.
# https://github.com/fortran-lang/stdlib/issues/690
# Darwin PowerPC got ieee_arithmetic support in gcc15.
# https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=1cfe4a4d0d4447b364815d5e5c889deb2e533669
# PowerPC-specific version of gcc14 with backported patch is available at
# https://github.com/macos-powerpc/powerpc-ports/tree/main/lang/gcc14

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

platform darwin powerpc {
    # 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
}
