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

# IMPORTANT:
#
# When updating major versions, please ensure that you have committed and
# tested the candidate version using the `go-devel` port, to verify how it
# builds against current and older versions of macOS.

if {$subport eq $name} {
# Go 1.22 does not build on macOS 10.12 and older.
# 1.14.x is the last branch to support i386 on macOS.
# However past 1.11.x everything is broken.
# FIXME: consider implementing support for PowerPC.
if {${os.platform} eq "darwin" && ${os.major} < 17} {

    epoch 3

    if {${build_arch} eq "i386"} {
        version     1.11.13
        revision    0
        set unsupported_macos_386 true
        set unsupported_macos false
    } else {
        version     1.17.13
        revision    0
        set unsupported_macos true
        set unsupported_macos_386 false
    }
} else {

    # macOS 11's most recently supported Go major version is 1.24
    if {${os.platform} eq "darwin" && ${os.major} == 20} {
        version         1.24.6
        set unsupported_macos false
        set unsupported_macos_386 false
        revision        0
        epoch           4

    # macOS 12 and up - latest version
    } else {
        version         1.25.2
        set unsupported_macos false
        set unsupported_macos_386 false
        revision        0
        epoch           3
    }
}

platforms           {darwin >= 10} freebsd linux
}

# Subport for Go Unstable Version
subport ${name}-devel {

    # No go-devel for 10.15 and earlier
    platforms       {darwin >= 20} freebsd linux

    # macOS 11's most recently supported Go major version is 1.24
    if {${os.platform} eq "darwin" && ${os.major} == 20} {
        version         1.24.6
        set unsupported_macos false
        set unsupported_macos_386 false
        revision        0
        epoch           2

    # macOS 12 and up - latest version
    } else {
        version         1.25.2
        revision        0
        epoch           1
    }
}

if {${os.platform} eq "darwin" \
    && (${os.major} < 11 || ${configure.build_arch} eq "i386")} {
    set legacy_build    true
} else {
    set legacy_build    false
}

homepage            https://go.dev

categories          lang
license             BSD
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. Go is expressive, concise, clean, and efficient. Its \
    concurrency mechanisms make it easy to write programs that get the most \
    out of multicore and networked machines, while its novel type system \
    enables flexible and modular program construction. Go compiles quickly \
    to machine code yet has the convenience of garbage collection and the \
    power of run-time reflection. It's a fast, statically typed, compiled \
    language that feels like a dynamically typed, interpreted language. Go \
    is developed by Google Inc.

set go_src_dist     go${version}.src${extract.suffix}
set go_armbin_dist  go${version}.darwin-arm64${extract.suffix}
set go_amdbin_dist  go${version}.darwin-amd64${extract.suffix}

livecheck.type      regex
livecheck.url       ${homepage}/dl/

if {$subport eq "${name}-devel"} {

    # Go (DEVEL / UNSTABLE) - macOS 11
    if {${os.platform} eq "darwin" && ${os.major} == 20} {
        checksums   ${go_src_dist} \
                    rmd160  7015227465326c4feb4f86371cd209b159a2b76b \
                    sha256  e1cb5582aab588668bc04c07de18688070f6b8c9b2aaf361f821e19bd47cfdbd \
                    size    30794139 \
                    ${go_armbin_dist} \
                    rmd160  7dd60961b2e2e851364743836e970be80a839a17 \
                    sha256  4e29202c49573b953be7cc3500e1f8d9e66ddd12faa8cf0939a4951411e09a2a \
                    size    76293244 \
                    ${go_amdbin_dist} \
                    rmd160  79910d8795a22d3dd02bf6ffee4413c840325b38 \
                    sha256  4a8d7a32052f223e71faab424a69430455b27b3fff5f4e651f9d97c3e51a8746 \
                    size    79994594

    # Go (DEVEL / UNSTABLE) - LATEST
     } else {
         checksums  ${go_src_dist} \
                    rmd160  52ba08c0016fb27d698fec089add60d3782fe375 \
                    sha256  3711140cfb87fce8f7a13f7cd860df041e6c12f7610f40cac6ec6fa2b65e96e4 \
                    size    31978632 \
                    ${go_armbin_dist} \
                    rmd160  f2ff9e9af1938056fa1013bc7e6a40314a138dfb \
                    sha256  d1ade1b480e51b6269b6e65856c602aed047e1f0d32fffef7eebbd7faa8d7687 \
                    size    58024236 \
                    ${go_amdbin_dist} \
                    rmd160  a8ad2c6b8401b72e1b7e2ff2779003d37c206b03 \
                    sha256  95493abb01da81638ab5083ff3f97e8f923cb42a64c2e16728e3cf5b0cd3fc5a \
                    size    60488889
     }

    livecheck.regex {go([0-9.A-z]+)\.src\.tar\.gz}
} else {
    # Go (RELEASE)

    if {${unsupported_macos_386}} {
        # 1.11
        checksums   ${go_src_dist} \
                    rmd160  19d71fb4c196bd5bb03cab40cc99b35f312aaefc \
                    sha256  5032095fd3f641cafcce164f551e5ae873785ce7b07ca7c143aecd18f7ba4076 \
                    size    21114296

        notes-append "
            Please note: Go 1.22 does not build on macOS 10.6 and older, so Go ${version} has been installed.
        "
    } elseif {${unsupported_macos}} {
        # 1.17
        checksums   ${go_src_dist} \
                    rmd160  6d8a13da5112ee67bb886eca0fec77ffaab27a5f \
                    sha256  a1a48b23afb206f95e7bbaa9b898d965f90826f6f1d1fc0c1d784ada0cd300fd \
                    size    22206518 \
                    ${go_armbin_dist} \
                    rmd160  1e2b1b394f912d92c3afde177f657733692ff593 \
                    sha256  e4ccc9c082d91eaa0b866078b591fc97d24b91495f12deb3dd2d8eda4e55a6ea \
                    size    130528194 \
                    ${go_amdbin_dist} \
                    rmd160  5ff00c1089ca3143c2f8e489f82c659f1066a17a \
                    sha256  c101beaa232e0f448fab692dc036cd6b4677091ff89c4889cc8754b1b29c6608 \
                    size    137060546

        notes-append "
            Please note: Go 1.22 does not build on macOS 10.12 and older, so Go ${version} has been installed.
        "
    } else {

        # GO RELEASE - macOS 11 - Go 1.24
        if {${os.platform} eq "darwin" && ${os.major} == 20} {
            checksums   ${go_src_dist} \
                        rmd160  7015227465326c4feb4f86371cd209b159a2b76b \
                        sha256  e1cb5582aab588668bc04c07de18688070f6b8c9b2aaf361f821e19bd47cfdbd \
                        size    30794139 \
                        ${go_armbin_dist} \
                        rmd160  7dd60961b2e2e851364743836e970be80a839a17 \
                        sha256  4e29202c49573b953be7cc3500e1f8d9e66ddd12faa8cf0939a4951411e09a2a \
                        size    76293244 \
                        ${go_amdbin_dist} \
                        rmd160  79910d8795a22d3dd02bf6ffee4413c840325b38 \
                        sha256  4a8d7a32052f223e71faab424a69430455b27b3fff5f4e651f9d97c3e51a8746 \
                        size    79994594

        # GO RELEASE - macOS 12+ - Latest version
        } else {

            checksums   ${go_src_dist} \
                        rmd160  52ba08c0016fb27d698fec089add60d3782fe375 \
                        sha256  3711140cfb87fce8f7a13f7cd860df041e6c12f7610f40cac6ec6fa2b65e96e4 \
                        size    31978632 \
                        ${go_armbin_dist} \
                        rmd160  f2ff9e9af1938056fa1013bc7e6a40314a138dfb \
                        sha256  d1ade1b480e51b6269b6e65856c602aed047e1f0d32fffef7eebbd7faa8d7687 \
                        size    58024236 \
                        ${go_amdbin_dist} \
                        rmd160  a8ad2c6b8401b72e1b7e2ff2779003d37c206b03 \
                        sha256  95493abb01da81638ab5083ff3f97e8f923cb42a64c2e16728e3cf5b0cd3fc5a \
                        size    60488889
        }
    }

    livecheck.regex {go([0-9.]+)\.src\.tar\.gz}
}

master_sites        https://storage.googleapis.com/golang/
distfiles           ${go_src_dist}
worksrcdir          ${name}

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

extract.only        ${go_src_dist}

set GOROOT          ${worksrcpath}
set GOROOT_FINAL    ${prefix}/lib/${subport}

supported_archs     arm64 i386 x86_64

switch ${configure.build_arch} {
    arm64 {
        set GOARCH arm64
    }
    i386 {
        set GOARCH 386
    }
    x86_64 {
        set GOARCH amd64
    }
    default {
        set GOARCH {}
    }
}

use_configure       no

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}

# Set build.jobs to -1 to disable MacPorts from adding the -j flag for
# parallel builds, since the build.cmd contains "make".
build.jobs          -1

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 {[vercmp ${version} < 1.19] && !${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
    }

    # Fix building with the -x flag ("show all commands as they are executed"),
    # useful for debugging of the bootstrapping process.
    # Only needed for debugging the build/bootstrapping process.
    # FIXME: report this as a bug and have it or a better version of it upstreamed.
    #patchfiles-append   patch-go-internal-buildid-and-gc-fix-debug.diff

    # Show all commands while building packages and only build one package at a time in
    # the last bootstrapping step.
    # Only needed for debugging the build/bootstrapping process.
    #patchfiles-append   patch-build-show-commands.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}"

    # Might be useful while debugging issues.
    build.args-append   "-v=3"

    notes-append [subst {
                    go 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} {
    if {${configure.build_arch} eq "x86_64"} {
        # The branch https://github.com/catap/go/tree/macos-10.6
        patchfiles-append   patch-macOS-10.6.diff
    } elseif {${configure.build_arch} eq "i386"} {
        patchfiles-append   patch-1.11.13-for-10.6-i386.diff
    }
}

use_parallel_build  no

if {${configure.build_arch} eq "arm64"} {

    # Use a temporary installation of the binary ARM64 Go distribution to
    # build Go for ARM64
    set go_bin_path         ${workpath}/${subport}_prebuilt

    build.env-append        GOROOT_BOOTSTRAP=${go_bin_path}/go

    distfiles-append        ${go_armbin_dist}

    post-extract {
        xinstall -d ${go_bin_path}
        system -W ${go_bin_path} \
            "${extract.cmd} ${extract.pre_args} ${distpath}/${go_armbin_dist} ${extract.post_args}"
    }

} elseif {${configure.build_arch} eq "x86_64" && (${os.major} >= 21 || !${legacy_build})} {

    # Use a temporary installation of the binary AMD64 Go distribution to
    # build Go for AMD64 on macOS 12 since go-1.4 fails to build
    set go_bin_path         ${workpath}/${subport}_prebuilt

    build.env-append        GOROOT_BOOTSTRAP=${go_bin_path}/go

    distfiles-append        ${go_amdbin_dist}

    post-extract {
        xinstall -d ${go_bin_path}
        system -W ${go_bin_path} \
            "${extract.cmd} ${extract.pre_args} ${distpath}/${go_amdbin_dist} ${extract.post_args}"
    }

} else {

    # https://trac.macports.org/ticket/69160
    build.env-append        GOROOT_BOOTSTRAP=${prefix}/lib/go-1.4 \
                            GOHOSTARCH=${GOARCH}

    depends_build-append    port:go-1.4
}

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

if {$subport eq "${name}-devel"} {
    set bin_suffix "-devel"

    notes "
        go-devel binaries are installed with a ${bin_suffix} suffix:
        - go${bin_suffix}
        - gofmt${bin_suffix}
    "
} else {
    set bin_suffix ""
}

destroot {

    delete ${worksrcpath}/src/cmd/vendor/github.com/google/pprof/internal/binutils/testdata/malformed_macho

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

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

    foreach f {api bin lib misc pkg src test VERSION} {
        copy ${worksrcpath}/${f} ${grfdir}
    }

    foreach f {go gofmt} {
        system -W ${destroot}${prefix}/bin/ "ln -s ../lib/${subport}/bin/$f ./${f}${bin_suffix}"
    }

    xinstall -m 0644 -W ${worksrcpath} \
        CONTRIBUTING.md \
        LICENSE \
        PATENTS \
        VERSION \
        ${docdir}

    if {!${legacy_build}} {
        xinstall -m 0644 -W ${worksrcpath} SECURITY.md ${docdir}
    }

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

    if { [ file exists ${worksrcpath}/go.env ] } {
        copy ${worksrcpath}/go.env ${grfdir}/
    }
}
