# -*- 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           select 1.0
PortGroup           legacysupport 1.1

# dprintf
legacysupport.newest_darwin_requires_legacy 10

name                guile-3.0
categories          lang
maintainers         {@graywolf wolfsden.cz:~+macports} openmaintainer
license             LGPL-2.1+

# current version
version             3.0.10
revision            1
checksums           rmd160  c42990081a8fffaf63874d16f457d8e50db8bc36 \
                    sha256  2dbdbc97598b2faf31013564efb48e4fed44131d28e996c26abe8a5b23b56c2a \
                    size    9738824

# At the moment 3.0.10 is broken on 32-bit platforms.
if {${configure.build_arch} in [list i386 ppc]} {
    version         3.0.9
    revision        0
    checksums       rmd160  e5c6a757cc188f137edd2046a3d015152efad7fb \
                    sha256  18525079ad29a0d46d15c76581b5d91c8702301bfd821666d2e1d13726162811 \
                    size    9734735

    depends_build-append    port:gmake
    build.cmd               ${prefix}/bin/gmake
}

compiler.c_standard 2011

universal_variant   no
# Never cross compile.
supported_archs     ${build_arch}

description         GNU's Ubiquitous Intelligent Language for Extension (guile)
long_description    \
        Guile is an interpreter for the Scheme programming language, packaged \
        for use in a wide variety of environments. Guile implements Scheme as \
        described in the Revised^5 Report on the Algorithmic Language Scheme  \
        (usually known as R5RS), providing clean and general data and control \
        structures.                                                           \
                                                                              \
        Guile goes beyond the rather austere language presented in R5RS,      \
        extending it with a module system, full access to POSIX system calls, \
        networking support, multiple threads, dynamic linking, a foreign      \
        function call interface, powerful string processing, and many other   \
        features needed for programming in the real world.

distname            guile-${version}
homepage            https://www.gnu.org/software/guile/
master_sites        https://ftp.gnu.org/gnu/guile/

# Patches - BEGIN
# Fix MacOS specific bugs and make the test suite pass.
# Sent to upstream: https://www.mail-archive.com/bug-guile@gnu.org/msg11370.html
use_autoreconf      yes
# On legacy systems passing -f is needed:
# autoreconf: error: /opt/local/bin/autopoint failed with exit status: 1
# Since autoreconf is used anyway, we can add these flags unconditionally:
autoreconf.args     -fvi

patchfiles \
                    0001-tests-Check-TCP_NODELAY-for-non-zero-instead-of-1.patch    \
                    0002-tests-Skip-tests-of-abstract-Unix-sockets-on-Darwin.patch  \
                    0003-tests-Skip-hole-related-port-tests-on-Darwin.patch         \
                    0004-filesys.c-Fix-readlink-for-ports-on-Darwin.patch           \
                    0005-tests-Skip-mkdtemp-test-for-invalid-template-on-Darw.patch \
                    0006-tests-Fix-spawn-with-environment-on-MacOS.patch            \
                    0007-tests-Fix-spawn-if-file-not-found-with-Gnulib.patch        \
                    0008-Do-not-depend-on-tmpnam-in-posix.test.patch                \
                    0009-posix.c-Set-errno-when-pipe2-is-not-available-and-fl.patch
patch.pre_args      -p1
# Patches - END

# pkgconfig: guile-config is a wrapper around pkg-config
depends_lib-append \
                    path:bin/pkg-config:pkgconfig \
                    port:boehmgc                  \
                    port:gettext                  \
                    port:gmp                      \
                    port:libffi                   \
                    port:libiconv                 \
                    port:libtool                  \
                    port:libunistring             \
                    port:ncurses                  \
                    port:readline

depends_test-append path:libexec/coreutils/libstdbuf.so:coreutils
post-patch {
    # This test requires wc from coreutils.
    reinplace "s|\"wc\"|\"${prefix}/libexec/gnubin/wc\"|g" \
        ${worksrcpath}/test-suite/tests/posix.test

    # freadlink is detected on 12.0, but is missing in the header.  Sigh.
    reinplace "s|^#elif HAVE_FREADLINK\$|& \\&\\& __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 130000|" \
        ${worksrcpath}/libguile/filesys.c
}

depends_run-append  port:guile_select

# LTO breaks availability of debug information.  Ticket describing the issue (it
# is for cmake, but I expect the root cause to be the same):
#     https://gitlab.kitware.com/cmake/cmake/-/issues/25202 --disable-lto
configure.args      --program-suffix=-3.0                    \
                    --disable-deprecated                     \
                    --enable-guile-debug                     \
                    --disable-lto                            \
                    --infodir="${prefix}/share/info/${name}" \
                    --mandir="${prefix}/share/man"

platform darwin powerpc {
    # Do we need this for ppc64 as well?
    post-extract {
        # the prebuilt guile binaries are broken for Darwin PPC
        move ${worksrcpath}/prebuilt/32-bit-big-endian ${worksrcpath}/prebuilt/32-bit-big-endian-disabled
        ui_msg "*** This build can take some time, as guile has to do a full bootstrap on PPC."
    }
}

if {${configure.build_arch} eq "ppc"} {
    # We override patches here on purpose. Test suite will be dealt with once 3.0.10 is fixed.
    patchfiles              0009-posix.c-Set-errno-when-pipe2-is-not-available-and-fl.patch \
                            0010-filesys.patch \
                            0011-powerpc.patch

    configure.args-append   --disable-error-on-warning \
                            --disable-jit \
                            --without-64-calls \
                            --without-threads

} elseif {${configure.build_arch} eq "i386"} {
    # We override patches here on purpose. Test suite will be dealt with once 3.0.10 is fixed.
    patchfiles              0009-posix.c-Set-errno-when-pipe2-is-not-available-and-fl.patch \
                            0010-filesys.patch

    # Notice that i386 build is untested. It may or may not require
    # disabling JIT and threads.
    configure.args-append   --disable-error-on-warning \
                            --without-64-calls
}

# fixes: sed: -i may not be used with stdin
depends_build-append port:gsed
configure.env-append SED=${prefix}/bin/gsed
build.env-append     SED=${prefix}/bin/gsed

test.run        yes
test.target     check

select.group    guile
select.file     ${name}

post-destroot {
    move ${destroot}${prefix}/share/aclocal/guile.m4 \
        ${destroot}${prefix}/share/aclocal/${name}.m4

    # Ensure that in case there is 12th part the build fails.  When that
    # happens, update the guile_select and select entries in all guiles to
    # reflect that.
    if {[file exists ${destroot}${prefix}/share/info/${name}/guile.info-12 ]} {
        ui_error "Additional part of info page available."
        return -code error
    }
}

notes "
To make the Guile ${version} default one, run:

port select --set ${select.group} [file tail ${select.file}]
"

livecheck.type  regex
livecheck.url   "https://ftp.gnu.org/gnu/guile/"
livecheck.regex "guile-(3\\.0\\.\\d+(?:\\.\\d+)*)${extract.suffix}"

# First is check for `options needed to detect all undeclared functions'.
# Second is actual check for strchr, but it is correctly detected.
configure.checks.implicit_function_declaration.whitelist-append strchr
# No MIN in limits.h.
configure.checks.implicit_function_declaration.whitelist-append MIN
# Correctly detects that stdalign.h is required.
configure.checks.implicit_function_declaration.whitelist-append alignof
# These are actually missing in regex.h.
configure.checks.implicit_function_declaration.whitelist-append re_set_syntax
configure.checks.implicit_function_declaration.whitelist-append re_compile_pattern
configure.checks.implicit_function_declaration.whitelist-append re_search
# Not available on MacOS.
configure.checks.implicit_function_declaration.whitelist-append pthread_getattr_np

# Only occurence is when checking for static_assert and it fallbacks correctly.
# TODO: This fails in the CI, but works locally.  Not sure what is wrong.
#configure.checks.implicit_int no
