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

PACKAGE = golang

libexecdir := /usr/lib/go
datadir := /usr/share/go
bindir := /bin

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
golang_archs = armel armhf amd64 i386

# 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 := 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

build-arch build-indep build clean install binary-arch binary-indep binary: debian/control
	+dh --parallel $(opt_no_act) $@

override_dh_auto_clean:
	rm -rf bin pkg
	# golang-go-$os-$arch.install files are auto-generated in override_dh_install
	rm -f debian/golang-go-*.install
	rm -f debian/*+
	rm -f debian/build.stamp
	rm -f test/pass.out test/run.out test/times.out
	rm -rf debian/home

override_dh_auto_build: debian/build.stamp

override_dh_auto_test:
ifneq (,$(filter $(DEB_HOST_ARCH),$(golang_archs)))
ifeq ($(RUN_TEST), true)
	cd $(CURDIR)/src && \
	$(CURDIR)/debian/test.bash $(GOROOT)/pkg/tool/$(GOHOSTOS)_$(GOHOSTARCH);
else
	:
endif
endif

override_dh_compress:
	dh_compress -Xusr/share/doc/$(PACKAGE)-doc/html -Xusr/share/doc/$(PACKAGE)-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="i386 amd64 armel armhf"; \
		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

override_dh_install:
ifneq (,$(filter $(DEB_HOST_ARCH),$(golang_archs)))
	-${FOR_GO_ARCH} \
		echo "pkg/$${os}_$${arch} /usr/lib/go/pkg/" > debian/golang-go-$$os-$$arch.install; \
		[ "$$arch" = "amd64" ] && [ "$$os" = "linux" ] && echo "pkg/linux_amd64_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}
	# 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.
	echo "listing of $$(pwd):"; ls -hlR .
	echo "listing of GOBIN: ($(GOBIN))"; ls -hlR $(GOBIN)
	for file in $$(cd pkg/ && ls */runtime/cgo.a); do \
		mkdir -p $(CURDIR)/debian/golang-go/usr/lib/go/pkg/$$(dirname $$file); \
		mv pkg/$$file $(CURDIR)/debian/golang-go/usr/lib/go/pkg/$$file; \
	done
	mv bin/go bin/golang-go
	mv bin/gofmt bin/golang-gofmt
	dh_install --fail-missing
	# Remove .syso files of the race detector; it is not used yet and the files are arch-dependent.
	find $(CURDIR)/debian/golang-src/usr/share/go/src/pkg/runtime/race -type f -name '*.syso' -delete
	# Remove Plan9 rc(1) scripts
	find $(CURDIR)/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 $(CURDIR)/debian/golang-go/usr/share/go/src -type d -delete
	# Subsequently, /usr/share/go is empty, too, so remove
	find $(CURDIR)/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 $(CURDIR)/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 $(CURDIR)/debian/golang-go/usr/lib/go/pkg
	find $(CURDIR)/debian/golang-go/usr/lib/go/pkg -exec touch -r $(CURDIR)/debian/golang-go/usr/lib/go/pkg {} \;
endif

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_builddeb:
	dh_builddeb -- -Zxz

override_dh_shlibdeps:
	dh_shlibdeps -Xtestdata -Xtest

debian/build.stamp:
ifneq (,$(filter $(DEB_HOST_ARCH),$(golang_archs)))
	rm -f debian/build.stamp
	mkdir -p $(GOBIN)
	# Build native tools first, then bootstrap all other GOOS/GOARCH combinations.
	cd src && 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.
	-${FOR_GO_ARCH} \
		export GOARCH=$$arch; \
		export GOOS=$$os; \
		cd src && bash ./make.bash --no-clean; \
		[ "$$arch" = "amd64" ] && [ "$$os" = "linux" ] && $(GOBIN)/go install -race std; \
		cd ..; \
	${FOR_GO_ARCH_END}
	>debian/build.stamp
else
	touch debian/build.stamp
endif

opt_no_act =
RUN_TEST := true

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
    $(warning no support for disabling optimization)
endif

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

ifneq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
    RUN_TEST := false
endif

GOROOT := $(CURDIR)
GOROOT_FINAL := $(libexecdir)
GOBIN := $(CURDIR)$(bindir)
GOARM :=

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH 2>/dev/null)
DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS 2>/dev/null)
DEB_HOST_ARCH_CPU := $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU 2>/dev/null)
DEB_BUILD_ARCH_OS := $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS 2>/dev/null)
DEB_BUILD_ARCH_CPU := $(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU 2>/dev/null)

ifeq ($(DEB_BUILD_ARCH_OS), kfreebsd)
    RUN_TEST := false
    GOHOSTOS := freebsd
else ifeq ($(DEB_BUILD_ARCH_OS), linux)
    GOHOSTOS := linux
else
    $(error unrecongized build host kernel $(DEB_BUILD_ARCH_OS)!)
endif

ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
    RUN_TEST := false
    GOOS := freebsd
else ifeq ($(DEB_HOST_ARCH_OS), linux)
    GOOS := linux
else
    $(error unrecognized kernel $(DEB_HOST_ARCH_OS)!)
endif

ifeq ($(DEB_BUILD_ARCH_CPU), i386)
    GOHOSTARCH := 386
else ifeq ($(DEB_BUILD_ARCH_CPU), amd64)
    GOHOSTARCH := amd64
else ifeq ($(DEB_BUILD_ARCH_CPU), arm)
    GOHOSTARCH := arm
else ifeq ($(DEB_BUILD_ARCH_CPU), armhf)
    GOHOSTARCH := arm
else
#    $(error unrecognized build host instruction set $(DEB_HOST_ARCH_CPU)!)
endif

ifeq ($(DEB_HOST_ARCH_CPU), i386)
    GOARCH := 386
    GOPREFIX := 8
else ifeq ($(DEB_HOST_ARCH_CPU), amd64)
    GOARCH := amd64
    GOPREFIX := 6
else ifeq ($(DEB_HOST_ARCH_CPU), arm)
    GOARCH := arm
    GOPREFIX := 5
ifeq ($(DEB_HOST_ARCH), armel)
    RUN_TEST := false
    GOARM := 5
else ifeq ($(DEB_HOST_ARCH), armhf)
    GOARM := 6
else
    $(error unrecognized arm architecture $(DEB_HOST_ARCH)!)
endif
else
#    $(error unrecognized instruction set $(DEB_HOST_ARCH_CPU)!)
endif

export GOROOT GOROOT_FINAL GOOS GOARCH GOARM GOBIN GOHOSTARCH GOHOSTOS

.PHONY: build clean install binary-arch binary-indep binary
