# -*- 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 0.7.0 v
revision            0
epoch               1
categories-append   lang fortran devel science
conflicts           fortran-stdlib-devel
license             MIT
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
description         Fortran Standard Library
long_description    {*}${description}
checksums           rmd160  7b737a35727c4b9067694ac15ee7864e5d60a25e \
                    sha256  07615b1fd0d9c78f04ec5a26234d091cb7e359933ba2caee311dcd6f58d87af0 \
                    size    3669255
github.tarball_from archive

subport fortran-stdlib-devel {
    github.setup    fortran-lang stdlib 6d9d7fde67c1735f10427e4d358fcb5c84b6936b
    version         2024.07.01
    conflicts       fortran-stdlib
    checksums       rmd160  eafd4645407319e5b22efb3a055dcab1f96cb89b \
                    sha256  c6cf459328d4f8088dcecbbdcc67194d9d0f3addd227706ac293723b722ec251 \
                    size    3670492
    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
    }
}

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

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

# 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
# In particular, GCC does not support ieee_arithmetic on Darwin PPC yet.
# However, there is an experimental implementation, which will be submitted to GCC upstream.
# https://github.com/barracuda156/gcc-git/tree/gfortran
# See discussion here: https://github.com/iains/darwin-toolchains-start-here/discussions/40
# I have dropped checks for ieee_arithmetic module, since those require that directories
# match exactly; that is fragile and may unnecessarily prevent the port from building.
# The right fix is to support ieee_arithmetic in gfortran, not to carry hacks for its detection.

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
}
