# -*- 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 10.13 - 10.15, 11's most recently supported major version is 1.24
    if {${os.platform} eq "darwin" && ${os.major} <= 20} {
        version         1.24.8
        set unsupported_macos false
        set unsupported_macos_386 false
        revision        0
        epoch           4

    # macOS 12 and up - latest version
    } else {
        version         1.25.3
        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.12 and earlier
    platforms       {darwin >= 17} freebsd linux

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

    # macOS 12 and up - latest version
    } else {
        version         1.25.3
        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 10.13 - 10.15, 11
    if {${os.platform} eq "darwin" && ${os.major} <= 20} {
        checksums   ${go_src_dist} \
                    rmd160  f0b0a7fe7867dc9ea9c7f662332dcaf954728625 \
                    sha256  b1ff32c5c4a50ddfa1a1cb78b60dd5a362aeb2184bb78f008b425b62095755fb \
                    size    30797581 \
                    ${go_armbin_dist} \
                    rmd160  2d1d9eb6decfa110168acbda9ccf3962a704bf5d \
                    sha256  0db27ff8c3e35fd93ccf9d31dd88a0f9c6454e8d9b30c28bd88a70b930cc4240 \
                    size    76429684 \
                    ${go_amdbin_dist} \
                    rmd160  86fdbaddbb31cfc967f8cb60adb918689e94700a \
                    sha256  ecb3cecb1e0bcfb24e50039701f9505b09744cc4730a8b9fc512b0a3b47cf232 \
                    size    80113551

    # Go (DEVEL / UNSTABLE) - LATEST
     } else {
         checksums  ${go_src_dist} \
                    rmd160  ad9f507b2d021ffb75126a41ac8d71f143b67141 \
                    sha256  a81a4ba593d0015e10c51e267de3ff07c7ac914dfca037d9517d029517097795 \
                    size    31980799 \
                    ${go_armbin_dist} \
                    rmd160  467162ef7ab959ab5fe80fb3e66b73ed129b1051 \
                    sha256  7c083e3d2c00debfeb2f77d9a4c00a1aac97113b89b9ccc42a90487af3437382 \
                    size    58028114 \
                    ${go_amdbin_dist} \
                    rmd160  8b570deae47240653dd7130654ea7915c6653dc8 \
                    sha256  1641050b422b80dfd6299f8aa7eb8798d1cd23eac7e79f445728926e881b7bcd \
                    size    60491973
     }

    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 10.13 - 10.15, 11 - Go 1.24
        if {${os.platform} eq "darwin" && ${os.major} <= 20} {
            checksums   ${go_src_dist} \
                        rmd160  f0b0a7fe7867dc9ea9c7f662332dcaf954728625 \
                        sha256  b1ff32c5c4a50ddfa1a1cb78b60dd5a362aeb2184bb78f008b425b62095755fb \
                        size    30797581 \
                        ${go_armbin_dist} \
                        rmd160  2d1d9eb6decfa110168acbda9ccf3962a704bf5d \
                        sha256  0db27ff8c3e35fd93ccf9d31dd88a0f9c6454e8d9b30c28bd88a70b930cc4240 \
                        size    76429684 \
                        ${go_amdbin_dist} \
                        rmd160  86fdbaddbb31cfc967f8cb60adb918689e94700a \
                        sha256  ecb3cecb1e0bcfb24e50039701f9505b09744cc4730a8b9fc512b0a3b47cf232 \
                        size    80113551

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

            checksums   ${go_src_dist} \
                        rmd160  ad9f507b2d021ffb75126a41ac8d71f143b67141 \
                        sha256  a81a4ba593d0015e10c51e267de3ff07c7ac914dfca037d9517d029517097795 \
                        size    31980799 \
                        ${go_armbin_dist} \
                        rmd160  467162ef7ab959ab5fe80fb3e66b73ed129b1051 \
                        sha256  7c083e3d2c00debfeb2f77d9a4c00a1aac97113b89b9ccc42a90487af3437382 \
                        size    58028114 \
                        ${go_amdbin_dist} \
                        rmd160  8b570deae47240653dd7130654ea7915c6653dc8 \
                        sha256  1641050b422b80dfd6299f8aa7eb8798d1cd23eac7e79f445728926e881b7bcd \
                        size    60491973
        }
    }

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