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

legacysupport.newest_darwin_requires_legacy 16

name                go-1.17
version             1.17.13
revision            0
categories          lang
license             BSD

maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

description         compiled, garbage-collected, concurrent programming \
                    language developed by Google Inc.

long_description    \
    The Go programming language is an open source project to make programmers \
    more productive. This port provides Go ${version}, the newest release that \
    can be built for versions of macOS older than 10.13 High Sierra. It is \
    installed alongside any other Go toolchain as go-1.17 and gofmt-1.17.

homepage            https://go.dev

# Upstream discontinued support for macOS older than 10.13 in Go 1.17, so the
# official binaries for this release will not run on the systems this port
# targets. MacPorts has nonetheless built 1.17.13 from source down to 10.6
# using legacysupport and the patches below, and this port carries that
# forward. Newer systems are deliberately excluded: from 10.13 onwards a newer
# Go is available, and a modern toolchain building this release is untested.
platforms           {darwin < 17}

# Go dropped darwin/386 in 1.15; the i386 systems that need an older release
# than this one continue to be served by the main `go` port.
supported_archs     x86_64

master_sites        ${homepage}/dl/
distfiles           go${version}.src${extract.suffix}
worksrcdir          go

checksums           rmd160  6d8a13da5112ee67bb886eca0fec77ffaab27a5f \
                    sha256  a1a48b23afb206f95e7bbaa9b898d965f90826f6f1d1fc0c1d784ada0cd300fd \
                    size    22206518

set GOROOT          ${worksrcpath}
set GOROOT_FINAL    ${prefix}/lib/${name}
set GOARCH          amd64

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    set legacy_build    true
} else {
    set legacy_build    false
}

use_configure       no
use_parallel_build  no

# build.cmd is a shell script that runs make itself, so stop the port system
# adding a -j flag.
build.jobs          -1
build.dir           ${worksrcpath}/src
build.cmd           ./make.bash
build.target
build.env           GOROOT=${GOROOT} \
                    GOARCH=${GOARCH} \
                    GOOS=darwin \
                    GOROOT_FINAL=${GOROOT_FINAL} \
                    CC=${configure.cc}

# Bootstrap from go-1.4, the last release written in C, rather than from the
# official prebuilt 1.17.13 toolchain. The prebuilt binaries for this release
# are built for 10.13 and will not run on any system this port supports, which
# is the same trap that broke the main `go` port on newer systems
# (https://trac.macports.org/ticket/73086).
depends_build       port:go-1.4

build.env-append    GOROOT_BOOTSTRAP=${prefix}/lib/go-1.4 \
                    GOHOSTARCH=${GOARCH}

if {${os.platform} eq "darwin" \
    && ${os.major} <= ${legacysupport.newest_darwin_requires_legacy}} {
    # The legacy support PG will not actually change anything in this port
    # directly, since go doesn't use the standard CFLAGS/CXXFLAGS. We need to
    # patch the build system and set up env variables manually.

    if {!${legacy_build}} {
        # Older compilers don't support the -Wno-nullability-completeness flag
        # and if that's the case, they won't need it anyway, so just patch it
        # out. Upstream no longer uses the flag as of 1.19beta1.
        # https://github.com/golang/go/commit/bf19163a545c3117ab3c309a691f32a42cf29efd
        patchfiles-append   patch-cgo-drop-no-nullability-completeness.diff
    }

    build.env-append    "GO_EXTLINK_ENABLED=1" \
                        "GO_LDFLAGS=\"-extldflags=${configure.ldflags}\"" \
                        "BOOT_GO_LDFLAGS=-extldflags=${configure.ldflags}" \
                        "CGO_LDFLAGS=-g -O2 ${configure.ldflags}"

    notes-append [subst {
                    go-1.17 had to be specially patched and built to work on\
                    your platform.

                    It likely won't work out of the box when building other\
                    projects, so make sure change your environment to use the\
                    following variables:
                      * GO_EXTLINK_ENABLED="1"
                    to always force go to use the external gcc or clang linker\
                    and
                      * GO_LDFLAGS="\\\"-extldflags=\${configure.ldflags}\\\""
                      * CGO_LDFLAGS="-g -O2 \${configure.ldflags}"
                    to force-link any binary against the legacy support\
                    library. Use exactly the quoting provided here, even if it\
                    may look odd, or compilation will fail.

                    Failure to do so will leave you unable to create binaries\
                    that use features not natively available on your system,\
                    either directly or through a go core dependency.
    }]
}

if {${os.platform} eq "darwin" && ${os.major} == 10} {
    # The branch https://github.com/catap/go/tree/macos-10.6
    patchfiles-append   patch-macOS-10.6.diff
}

post-build {
    system "find ${worksrcpath} -type d -name .hg* -print0 | xargs -0 rm -rf"

    if {[file exists ${worksrcpath}/pkg/bootstrap]} {
        delete ${worksrcpath}/pkg/bootstrap
    }
}

destroot {
    # A deliberately malformed Mach-O test fixture that upsets destroot.
    set malformed ${worksrcpath}/src/cmd/vendor/github.com/google/pprof/internal/binutils/testdata/malformed_macho
    if {[file exists ${malformed}]} {
        delete ${malformed}
    }

    set grfdir ${destroot}${GOROOT_FINAL}
    set docdir ${destroot}${prefix}/share/doc/${name}

    xinstall -d ${grfdir}
    xinstall -d ${docdir}

    foreach f {api bin lib misc pkg src test VERSION} {
        if {[file exists ${worksrcpath}/${f}]} {
            copy ${worksrcpath}/${f} ${grfdir}
        }
    }

    foreach f {go gofmt} {
        ln -s ../lib/${name}/bin/${f} ${destroot}${prefix}/bin/${f}-1.17
    }

    foreach f {CONTRIBUTING.md LICENSE PATENTS SECURITY.md VERSION} {
        if {[file exists ${worksrcpath}/${f}]} {
            xinstall -m 0644 -W ${worksrcpath} ${f} ${docdir}
        }
    }

    if {[file exists ${worksrcpath}/doc]} {
        copy {*}[glob -directory ${worksrcpath}/doc *] ${docdir}
    }
}

notes-append "
    This port installs Go ${version} as go-1.17 and gofmt-1.17, so it does not
    interfere with the main `go` port.
"

# Match only this branch, so this port is not reported as outdated against
# whatever the current Go release happens to be.
livecheck.type      regex
livecheck.url       ${homepage}/dl/
livecheck.regex     {go(1\.17\.[0-9.]+)\.src\.tar\.gz}
