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

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

# Go 1.18 does not build on macOS 10.12 and older
if {${os.platform} eq "darwin" && ${os.major} < 17} {
    version         1.17.13
    set unsupported_macos true
} else {
    version         1.18.5
    set unsupported_macos false
}

revision            0

# Subport for Go Unstable Version
subport ${name}-devel {
    version         1.19rc2
    revision        0
    epoch           1
}

homepage            https://golang.org

categories          lang
platforms           darwin freebsd linux
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)
    checksums       ${go_src_dist} \
                    rmd160  7db1537f2d69ed7a255aa6a706f8d0134797f064 \
                    sha256  c68d7019a6a0b9852ae0e96d7e7deb772492a23272fd6d13afe05b40c912e51b \
                    size    26593323 \
                    ${go_armbin_dist} \
                    rmd160  1c68a3d965d2e0fe0eb896eb84b31613b75c1b99 \
                    sha256  7a097fcd5ff9a9ea7d4585e5d54df51636fb78af9cd323e807d9b3c9db68c761 \
                    size    145055710 \
                    ${go_amdbin_dist} \
                    rmd160  23be6b1103aab45dd92d96fd753fb45246664402 \
                    sha256  e85f22cecec124ac8eff87be0ce8db0ed74b74aa4f3458681ea3044e9e10c411 \
                    size    151250426

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

    if {${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.18 does not build on macOS 10.12 and older, so Go ${version} has been installed.
        "
    } else {
        # 1.18
        checksums   ${go_src_dist} \
                    rmd160  6ff5c4b2ff2bb93bda934c3ad45d71393f4c03b0 \
                    sha256  9920d3306a1ac536cdd2c796d6cb3c54bc559c226fc3cc39c32f1e0bd7f50d2a \
                    size    22847094 \
                    ${go_armbin_dist} \
                    rmd160  329f30a3f65583b92f36e009bdd0c7d55a191403 \
                    sha256  923a377c6fc9a2c789f5db61c24b8f64133f7889056897449891f256af34065f \
                    size    138253414 \
                    ${go_amdbin_dist} \
                    rmd160  d7e2b703072163ed27d3c72fa8dd66b4d0737f7e \
                    sha256  828eeca8b5abea3e56921df8fa4b1101380a5ebcfee10acbc8ffe7ec0bf5876b \
                    size    143880926
    }

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

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

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.

    # 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.
    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} eq 10} {
    # The branch https://github.com/catap/go/tree/macos-10.6
    patchfiles-append   patch-macOS-10.6.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} {

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

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

    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} {
        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} \
        AUTHORS \
        CONTRIBUTING.md \
        CONTRIBUTORS \
        LICENSE \
        README.md \
        PATENTS \
        VERSION \
        ${docdir}

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

if {${os.platform} eq "darwin" && ${os.major} < 10} {
    known_fail yes
    pre-fetch {
        ui_error "${subport} @${version} requires Mac OS X 10.6 or greater."
        return -code error "incompatible Mac OS X version"
    }
}
