# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer:
pkgname=go
pkgver=1.9.2
# This should be the latest commit on the corresponding release branch
_toolsver="5d2fd3ccab986d52112bf301d47a819783339d0e"
pkgrel=1
pkgdesc="Go programming language compiler"
url="http://www.golang.org/"
arch="all"
license="BSD"
depends="binutils gcc"
depends_dev=""
makedepends="bash go-bootstrap"
options="!strip"
provides="go-bootstrap=$pkgver-r$pkgrel"
install=""
subpackages="$pkgname-doc"
source="http://golang.org/dl/go${pkgver/_/}.src.tar.gz
	go-tools-${pkgver/_/}.tar.gz::https://github.com/golang/tools/archive/${_toolsver}.tar.gz
	default-buildmode-pie.patch
	set-external-linker.patch
	make-sure-R0-is-zero-before-main-on-ppc64le.patch
	"

# NOTE: building go for x86 with grsec kernel requires:
#           sysctl -w kernel.modify_ldt=1

if [ "$CBUILD" = "$CTARGET" ]; then
	makedepends="go-bootstrap $makedepends"
	provides="go-bootstrap=$pkgver-r$pkgrel"
	subpackages="$subpackages $pkgname-tools"
	_gotools="cover godoc stringer"
else
	pkgname="go-bootstrap"
	makedepends="go $makedepends"
	# Go expect host linker instead of the cross-compiler
	export CC_FOR_TARGET="$CC"
	export CC="${HOSTLD:-gcc}"
	export CXX="${HOSTLD:-g++}"
	export LD="${HOSTLD:-ld}"
fi

_tooldir="$srcdir"/tools-${_toolsver}

case "$CTARGET_ARCH" in
aarch64)export GOARCH="arm64" ;;
armhf)  export GOARCH="arm" GOARM=6 ;;
armv7)  export GOARCH="arm" GOARM=7 ;;
s390x)  export GOARCH="s390x" ;;
x86)    export GOARCH="386" GO386=387 ;;
x86_64) export GOARCH="amd64" ;;
ppc64le) export GOARCH="ppc64le" ;;
mips)   export GOARCH="mips" ;;
mips64) export GOARCH="mips64" ;;
mips64el) export GOARCH="mips64le" ;;
mipsel) export GOARCH="mipsle" ;;
*)      die "Unsupported arch" ;;
esac

builddir="$srcdir"/go
build() {
	cd "$builddir/src"

	export GOOS="linux"
	export GOPATH="$srcdir"
	export GOROOT="$builddir"
	export GOBIN="$GOROOT"/bin
	export GOROOT_FINAL=/usr/lib/go
	local p; for p in /usr/lib/go-bootstrap /usr/lib/go-linux-$GOARCH-bootstrap /usr/lib/go; do
		if [ -d "$p" ]; then
			export GOROOT_BOOTSTRAP="$p"
			break
		fi
	done

	./make.bash

	# copied from bootstrap.bash to fixup cross-built bootstrap go
	if [ "$CBUILD" != "$CTARGET" ]; then
		local gohostos="$(../bin/go env GOHOSTOS)"
		local gohostarch="$(../bin/go env GOHOSTARCH)"
		mv ../bin/*_*/* ../bin
		rmdir ../bin/*_*
		rm -rf "../pkg/${gohostos}_${gohostarch}"* "../pkg/tool/${gohostos}_${gohostarch}"*
		rm -rf ../pkg/bootstrap ../pkg/obj
	fi

	# FIXME some tests fail:
	# PATH="$GOROOT/bin:$PATH" ./run.bash -no-rebuild

	mkdir -p "$GOPATH"/src/golang.org/x/tools
	cp -r "$_tooldir"/* "$GOPATH"/src/golang.org/x/tools

	for tool in $_gotools; do
		"$GOROOT"/bin/go install golang.org/x/tools/cmd/$tool
	done
}

package() {
	cd "$builddir"
	mkdir -p "$pkgdir"/usr/bin "$pkgdir"/usr/lib/go/bin "$pkgdir"/usr/share/doc/go

	for binary in go gofmt; do
		install -Dm755 bin/"$binary" "$pkgdir"/usr/lib/go/bin/"$binary"
		ln -s /usr/lib/go/bin/"$binary" "$pkgdir"/usr/bin/
	done

	cp -a pkg lib "$pkgdir"/usr/lib/go
	cp -r doc misc "$pkgdir"/usr/share/doc/go
	rm -rf "$pkgdir"/usr/lib/go/pkg/bootstrap
	rm -f  "$pkgdir"/usr/lib/go/pkg/tool/*/api

	# The source needs to be installed due to an upstream
	# bug (https://github.com/golang/go/issues/2775).
	# When this is resolved we can split out the source to a
	# go-src sub package.
	mkdir -p "$pkgdir"/usr/lib/go/
	cp -a "$builddir"/src "$pkgdir"/usr/lib/go

	# Remove tests from /usr/lib/go/src.
	# Those shouldn't be affacted by the upstream bug (see above).
	find "$pkgdir"/usr/lib/go/src \( -type f -a -name "*_test.go" \) \
		-exec rm -rf \{\} \+
	find "$pkgdir"/usr/lib/go/src \( -type d -a -name "testdata" \) \
		-exec rm -rf \{\} \+
	find "$pkgdir"/usr/lib/go/src -type f -a \( -name "*.bash" -o -name "*.rc" -o -name "*.bat" \) \
		-exec rm -rf \{\} \+
}

tools() {
	pkgdesc="Go programming language tools"
	depends="$pkgname"

	mkdir -p "$subpkgdir"/usr/bin "$subpkgdir"/usr/lib/go/bin \
		"$subpkgdir"/usr/lib/go/pkg/tool/linux_$GOARCH

	for tool in $_gotools; do
		if [ -f "$pkgdir"/usr/lib/go/pkg/tool/linux_$GOARCH/$tool ]; then
			mv "$pkgdir"/usr/lib/go/pkg/tool/linux_$GOARCH/$tool \
				"$subpkgdir"/usr/lib/go/pkg/tool/linux_$GOARCH/$tool
		fi
		if [ -f "$builddir/bin/$tool" ]; then
			install -Dm755 "$builddir/bin/$tool" "$subpkgdir"/usr/lib/go/bin/"$tool"
			ln -s /usr/lib/go/bin/"$tool" "$subpkgdir"/usr/bin/
		fi
	done
}

sha512sums="1034098575c317eeaf648629690a4dea0c479a69c3b80d9917f6b96c8781ce79c0f29859f667dc4e07d47a44972aa09bd0163a458f897cf45f9d09eb03e4abb5  go1.9.2.src.tar.gz
d679873082dbac6a47b7c43ac74c47cb19616fb053a4faa3e6ee78004ece86aa5c432fba3d24c030d47396089d7ec7c6357af5648bd767341056396066ff9a04  go-tools-1.9.2.tar.gz
ef9da66d76e4698314eaf2aa88cf40a8430a15f8f6cb9ad17ee9d72498ec049b60e63e1ff5acf13a916c5ea365f9d9a282b6f2b06e28de9e3484eb9a3d7dd98e  default-buildmode-pie.patch
6b36f3780ab10e5c4902473a8ab5c0417220ece584b537517e9e60979bdc5a548ed14dd2546605392c89ec5ea6691769d337d34e2e19b92eba5bbca1898f4ada  set-external-linker.patch
451ca02dea91d74d8e3216c7a48d963bbfc040b43d15868087d6339a1815c4996817c5ace265a20abcdb9d1da4e9ff58e373397d98df773b729876f4623b1cc8  make-sure-R0-is-zero-before-main-on-ppc64le.patch"
