#!/usr/bin/make -f
# This file is in the public domain.
# You may freely use, modify, distribute, and relicense it.

export GOROOT := $(CURDIR)
export GOROOT_FINAL := /usr/lib/go
export GOHOSTOS := $(shell ./debian/helpers/goenv.sh echo '$$GOHOSTOS')
export GOHOSTARCH := $(shell ./debian/helpers/goenv.sh echo '$$GOHOSTARCH')

%:
	+dh --parallel $(opt_no_act) $@

# We loop over supported operating systems and architectures multiple times,
# so these two variables contain a centralized version of that code.
# The variables $$os and $$arch can be used inside such a loop.
FOR_GO_ARCH := set -ex; \
	for os in $$(echo linux freebsd windows darwin netbsd); do \
		archs="amd64 386"; \
		[ "$$os" = "linux" ] || [ "$$os" = "freebsd" ] && archs="amd64 arm 386"; \
		for arch in $$(echo $$archs); do
FOR_GO_ARCH_END := done; done

override_dh_auto_clean:
	# golang-go-$os-$arch.install files are auto-generated in override_dh_install
	rm -f debian/golang-go-*-*.install
	# remove autogenerated files
	rm -f \
		src/cmd/cgo/zdefaultcc.go \
		src/cmd/gc/opnames.h \
		src/cmd/go/zdefaultcc.go \
		src/liblink/anames*.c \
		src/runtime/zaexperiment.h \
		src/runtime/zasm_*_*.h \
		src/runtime/zgoarch_*.go \
		src/runtime/zgoos_*.go \
		src/runtime/zruntime_defs_*_*.go \
		src/runtime/zsys_*_*.s \
		src/runtime/zversion.go
	# remove built objects
	rm -rf bin pkg

override_dh_auto_test-arch:
	set -ex; \
		cd src; \
		export PATH="$(GOROOT)/bin:$$PATH"; \
		eval "$$(go tool dist env)"; \
		bash run.bash --no-rebuild;
	# remove test artifacts
	rm -f \
		test/bench/shootout/6.out \
		test/bench/shootout/*.6

override_dh_compress-indep:
	dh_compress -Xusr/share/doc/golang-doc/html -Xusr/share/doc/golang-doc/godoc

# Generates debian/control from debian/control.base and debian/control.cross.
# In the latter, @OS@ and @ARCH@ are replaced with every supported combination
# of operating system and architecture (e.g. linux_amd64, linux_386, …).
gencontrol:
	echo "# DO NOT EDIT THIS FILE. EDIT debian/control.* instead!" > debian/control.tmp
	cat debian/control.base >> debian/control.tmp
	${FOR_GO_ARCH} \
		BUILDARCHS=all; \
		[ "$$os" = "linux" ] && BUILDARCHS="amd64 armel armhf i386"; \
		sed -e "s,@OS@,$$os,g; s,@ARCH@,$$arch,g; s,@BUILDARCHS@,$$BUILDARCHS,g" debian/control.cross >> debian/control.tmp; \
	${FOR_GO_ARCH_END}
	mv debian/control.tmp debian/control

install-overrides:
	# go install: -race is only supported on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64
	${FOR_GO_ARCH} \
		echo "pkg/$${os}_$${arch} /usr/lib/go/pkg/" > debian/golang-go-$$os-$$arch.install; \
		[ "$$arch" = '$(GOHOSTARCH)' -a "$$arch" = 'amd64' -a "$$os" = '$(GOHOSTOS)' -a "$$os" = 'linux' ] \
			&& echo "pkg/$${os}_$${arch}_race /usr/lib/go/pkg/" >> debian/golang-go-$$os-$$arch.install; \
		echo "golang-go-$$os-$$arch: arch-independent-package-contains-binary-or-object" > debian/golang-go-$$os-$$arch.lintian-overrides; \
	${FOR_GO_ARCH_END}

override_dh_install-indep:
	./debian/rules install-overrides
	dh_install --fail-missing

override_dh_install-arch:
	./debian/rules install-overrides
	# Install the native runtime/cgo.a to the golang-go package.
	# runtime/cgo is only built for the native architecture, but the
	# golang-go-OS-ARCH packages are built on one particular buildd, which
	# might either be i386 or amd64.
	for file in pkg/*/runtime/cgo.a; do \
		mkdir -p debian/golang-go/usr/lib/go/$$(dirname $$file); \
		mv $$file debian/golang-go/usr/lib/go/$$file; \
	done
	dh_install --fail-missing
	# Remove .syso files of the race detector; it is not used yet and the files are arch-dependent.
	find debian/golang-src/usr/share/go/src/runtime/race -type f -name '*.syso' -delete
	# Remove Plan9 rc(1) scripts
	find debian/golang-src/usr/share/go/src -type f -name '*.rc' -delete
	# Remove empty /usr/share/go/src from golang-go, it is provided by golang-src
	find debian/golang-go/usr/share/go/src -type d -delete
	# Subsequently, /usr/share/go is empty, too, so remove
	find debian/golang-go/usr/share/go -type d -delete
	# For some reason, Go cross-compiles a handful of tools: {yacc,fix,api,vet,cgo}.
	# Delete those, as they cannot be ran anyway.
	# This is tracked upstream at https://code.google.com/p/go/issues/detail?id=5667
	(cd debian/golang-go/usr/lib/go/pkg/tool && find . -depth -path "./$(GOHOSTOS)_$(GOHOSTARCH)*" -prune -o -delete)
	# Touch built and installed files and directories to have same timestamp
	touch debian/golang-go/usr/lib/go/pkg
	find debian/golang-go/usr/lib/go/pkg -exec touch -r $(CURDIR)/debian/golang-go/usr/lib/go/pkg {} \;

override_dh_strip:
	# strip disabled as golang upstream doesn't support it and it makes go
	# crash. See http://bugs.debian.org/717172

override_dh_prep:
	dh_prep
	echo 'go:Hostarch=$(GOHOSTARCH)' >> debian/golang-go.substvars

override_dh_shlibdeps:
	dh_shlibdeps -Xtestdata -Xtest

override_dh_auto_build:
	[ -f VERSION ] || echo "debian snapshot +$$(dpkg-parsechangelog -SVersion)" > VERSION
	# Build native tools first, then bootstrap all other GOOS/GOARCH combinations.
	cd src \
		&& $(CURDIR)/debian/helpers/goenv.sh \
			bash ./make.bash --no-banner
	# For the race detector to work (go test -race), we need to install the
	# std library with the race detector enabled. This will result in
	# having an additional “architecture” directory, e.g. linux_amd64_race.
	#
	# The race detector currently only works on linux/amd64. The check within
	# the following loop works precisely the same way src/race.bash does.
	#
	# go install: -race is only supported on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64
	${FOR_GO_ARCH} \
		cd src \
			&& $(CURDIR)/debian/helpers/goenv.sh \
				GOARCH=$$arch GOOS=$$os \
				bash ./make.bash --no-clean --no-banner; \
		if [ "$$arch" = '$(GOHOSTARCH)' -a "$$arch" = 'amd64' -a "$$os" = "$(GOHOSTOS)" -a "$$os" = 'linux' ]; then \
			$(CURDIR)/debian/helpers/goenv.sh \
				GOARCH=$$arch GOOS=$$os \
				$(GOROOT)/bin/go install -race std; \
		fi; \
		cd ..; \
	${FOR_GO_ARCH_END}

opt_no_act :=
ifneq (,$(findstring n,$(MAKEFLAGS)))
	opt_no_act := --no-act
endif
