pkgname=go
pkgver=1.25.5
pkgrel=1
pkgdesc='Core compiler tools for the Go programming language'
arch=(x86_64)
homepage='https://go.dev/'
options=(!strip)
sources=("go${pkgver}.src.tar.gz")
urls=("https://go.dev/dl/${sources[0]}")
md5sums=("e7441aa32bcc87d3c4aba36665d9d6fd")

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  rm -vf src/runtime/{os_plan9.go.orig,os_windows.go.orig,proc.go.orig,vgetrandom_linux.go.orig}
}

src_build() {
  export GOARCH=amd64
  export GOAMD64=v3 # make sure we're building for the right x86-64 version
  export GOROOT_FINAL=/usr/lib/go
  export GOROOT_BOOTSTRAP=/usr/lib/go-bootstrap

  # Disable dwarf5 until debugedit catches up
  export GOEXPERIMENT=nodwarf5

  pushd "${srcdir}"/src
  ./make.bash -v
  popd
}

src_check() {
  export GO_TEST_TIMEOUT_SCALE=3
  chown -R leaf .
  pushd "${srcdir}"/src
  # TODO: Disable LSAN tests as it's crashing and we don't want to wait for upstream.
  #       See: https://github.com/golang/go/issues/74476
  chown -R leaf {$HOME,$TMPDIR}
  su leaf -c "PATH=$PATH HOME=$HOME TMPDIR=$TMPDIR GO_TEST_TIMEOUT_SCALE=${GO_TEST_TIMEOUT_SCALE} ./run.bash --no-rebuild -v -v -v -k -run '!cmd/cgo/internal/testsanitizers'"
  popd
}

src_install() {
  install -d "$pkgdir/usr/bin" "$pkgdir/usr/lib/go" "$pkgdir/usr/share/doc/go" \
    "$pkgdir/usr/lib/go/pkg/linux_amd64_"{dynlink,race}
  cp -a bin pkg src lib misc api test "$pkgdir/usr/lib/go"
  # We can't strip all binaries and libraries,
  # as that also strips some testdata directories and breaks the tests.
  # Just strip the packaged binaries as a compromise.
  strip $STRIP_BINARIES "$pkgdir/usr/lib/go"{/bin/*,/pkg/tool/*/*}

  cp -r doc/* "$pkgdir/usr/share/doc/go"

  ln -sf /usr/lib/go/bin/go "$pkgdir/usr/bin/go"
  ln -sf /usr/lib/go/bin/gofmt "$pkgdir/usr/bin/gofmt"
  ln -sf /usr/share/doc/go "$pkgdir/usr/lib/go/doc"

  install -Dm644 VERSION "$pkgdir/usr/lib/go/VERSION"

  rm -rf "$pkgdir/usr/lib/go/pkg/bootstrap"

  # TODO: Figure out if really needed
  rm -rf "$pkgdir"/usr/lib/go/pkg/obj/go-build

  # https://github.com/golang/go/issues/57179
  install -Dm644 go.env "$pkgdir/usr/lib/go/go.env"

  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

# vim:ft=sh syn=sh et sw=2:
