# Maintainer: Morten Linderud <foxboron@archlinux.org>
# Contributor: Daniel Martí <mvdan@mvdan.cc>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Alexander F. Rødseth <xyproto@archlinux.org>
# Contributor: Pierre Neidhardt <ambrevar@gmail.com>
# Contributor: Vesa Kaihlavirta <vegai@iki.fi>
# Contributor: Rémy Oudompheng <remy@archlinux.org>
# Contributor: Andres Perera <andres87p gmail>
# Contributor: Matthew Bauer <mjbauer95@gmail.com>
# Contributor: Christian Himpel <chressie@gmail.com>
# Contributor: Mike Rosset <mike.rosset@gmail.com>
# Contributor: Daniel YC Lin <dlin.tw@gmail.com>
# Contributor: John Luebs <jkluebs@gmail.com>

## ENABLE x86-64-v3 optimization
_optimize_v3=${_optimize_v3-}

_optimize_v4=${_optimize_v4-}

pkgname=go
epoch=2
pkgver=1.26.5
pkgrel=2
pkgdesc='Core compiler tools for the Go programming language'
arch=(x86_64)
url='https://golang.org/'
license=(BSD-3-Clause)
makedepends=(git go)
replaces=(go-pie)
provides=(go-pie)
options=(!strip staticlibs)
source=("https://go.dev/dl/go${pkgver}.src.tar.gz"{,.asc}
        fix-avx512ScanPackedReqsMet.patch)
validpgpkeys=('EB4C1BFD4F042F6DDDCCEC917721F63BD38B4796')
sha256sums=('495be4bc87176ac567392e5b4116abd98466d33d7b49d41e764ccc6976b2dc42'
            'SKIP'
            '37909d8154d245b22ea506851bccc71eeffe2ad2756eb6dfb53ab48b58de589b')

prepare() {
  cd $pkgname
  # It leaves some traces...
  rm -vf src/runtime/{os_plan9.go.orig,os_windows.go.orig,proc.go.orig,vgetrandom_linux.go.orig}

  # Fix AMD test failures on build.archlinux.org; see https://github.com/golang/go/issues/77674.
  patch -p1 -i ../fix-avx512ScanPackedReqsMet.patch
}

build() {
  ## Enable Optimization, depending on the optimize.
  ## Might we can simply use grep in the future for autodetection
  if [ -n "$_optimize_v3" ]; then
      export GOAMD64=v3
  elif [ -n "$_optimize_v4" ]; then
      export GOAMD64=v4
  else
      export GOAMD64=v1
  fi

  export GOARCH=amd64
  export GOROOT_FINAL=/usr/lib/go
  export GOROOT_BOOTSTRAP=/usr/lib/go

  # Disable dwarf5 until debugedit catches up
  export GOEXPERIMENT=nodwarf5

  cd "$pkgname/src"
  ./make.bash -v
}

check() {
  export GO_TEST_TIMEOUT_SCALE=3

  cd $pkgname/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
  ./run.bash --no-rebuild -v -v -v -k -run "!cmd/cgo/internal/testsanitizers"
}

package() {
  cd "$pkgname"

  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"

  # Reset GOAMD64 to v1 in the system go.env so that user builds default to the
  # portable x86-64 baseline and do not inherit the optimization level used to
  # compile this toolchain. Users or PKGBUILDs can still override via the
  # GOAMD64 env var or `go env -w GOAMD64=v3`. (GH#1114)
  if grep -q '^GOAMD64=' "$pkgdir/usr/lib/go/go.env"; then
    sed -i 's/^GOAMD64=.*/GOAMD64=v1/' "$pkgdir/usr/lib/go/go.env"
  else
    echo 'GOAMD64=v1' >> "$pkgdir/usr/lib/go/go.env"
  fi

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

# vim: ts=2 sw=2 et
