#!/usr/bin/make -f

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
export DEBIAN_REVISION ?= $(shell dpkg-parsechangelog --show-field Version | sed -e 's,.*+dfsg,+dfsg,')
export SOURCE_DATE_EPOCH = $(shell date -d "$$(dpkg-parsechangelog --count 1 --show-field Date)" +%s)

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
export CROSS_COMPILE ?= $(DEB_HOST_GNU_TYPE)-
cross_build_tools ?= y
endif

# support parallel build using DEB_BUILD_OPTIONS=parallel=N
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  DEB_UBOOT_FLAGS += -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# the upstream build passes LDFLAGS directly to ld instead of calling gcc for
# linking; so instead of passing -Wl,foo in LDFLAGS as in automake builds, one
# should set LDFLAGS to foo directly
comma := ,
LDFLAGS := $(patsubst -Wl$(comma)%,%,$(LDFLAGS))

%:
	dh $@ --parallel

override_dh_auto_build: TOOLSDIR := debian/build/tools
override_dh_auto_build:
	set -e; sed -n 's/^$(DEB_HOST_ARCH)[[:space:]]\+//p' debian/targets \
	    | while read subarch platform targets; do \
	        builddir=debian/build/$$platform; \
		targets="$$targets uboot.elf" ;\
	        mkdir -p $$builddir; \
		$(MAKE) O=$$builddir $${platform}_defconfig; \
		$(MAKE) $(DEB_UBOOT_FLAGS) O=$$builddir all; \
		install -m 644 $$builddir/u-boot $$builddir/uboot.elf; \
	        $(CROSS_COMPILE)strip --remove-section=.comment \
	            $$builddir/uboot.elf; \
		for target in $$targets; do \
			chmod -x $$builddir/$$target; \
			echo $$builddir/$$target /usr/lib/u-boot/$$platform/ \
				>> debian/build/targets.$$subarch; \
			echo $$platform >> debian/build/platforms.$$subarch; \
		done ; \
	        case $${subarch} in \
	        sunxi) builddir=$${builddir}_FEL ;\
			targets="u-boot.bin spl/u-boot-spl.bin" ;\
		        mkdir -p $$builddir ;\
		        $(MAKE) O=$$builddir $${platform}_defconfig; \
			sed -i -e 's/^# \(CONFIG_SPL_FEL\) is not set/\1=y/g' $$builddir/.config ;\
			platform="$${platform}_FEL" ;\
			$(MAKE) $(DEB_UBOOT_FLAGS) O=$$builddir olddefconfig ; \
			$(MAKE) $(DEB_UBOOT_FLAGS) O=$$builddir $$targets ; \
			for target in $$targets ; do \
				chmod -x $$builddir/$$target; \
				echo $$builddir/$$target /usr/lib/u-boot/$$platform/ \
					>> debian/build/targets.$$subarch; \
			done ;; \
		rockchip) \
			mkimage=$$builddir/tools/mkimage; \
			test -x /usr/bin/mkimage && mkimage=/usr/bin/mkimage; \
			$$mkimage -T rksd -n rk3288 \
				-d $$builddir/spl/u-boot-spl-dtb.bin $$builddir/u-boot-spl-dtb.rksd; \
			echo $$builddir/u-boot-spl-dtb.rksd /usr/lib/u-boot/$$platform/ \
				>> debian/build/targets.$$subarch; \
			;; \
	        esac; \
	done

	# Avoid requirement for .config
	mkdir -p $(TOOLSDIR)/include/config
	touch $(TOOLSDIR)/include/config/auto.conf
	# Load dummy config
	$(MAKE) O=$(TOOLSDIR) CROSS_COMPILE=$(CROSS_COMPILE) sandbox_defconfig
	# board-independent tools
	$(MAKE) O=$(TOOLSDIR) $(DEB_UBOOT_FLAGS) \
		CROSS_COMPILE=$(CROSS_COMPILE) \
		CROSS_BUILD_TOOLS=$(cross_build_tools) \
		NO_SDL=1 \
	    tools-only
	$(MAKE) O=$(TOOLSDIR) $(DEB_UBOOT_FLAGS) \
		CROSS_COMPILE=$(CROSS_COMPILE) \
		NO_SDL=1 \
	    env
	$(CROSS_COMPILE)strip --remove-section=.comment $(TOOLSDIR)/tools/env/fw_printenv
	$(CROSS_COMPILE)strip --remove-section=.comment $(TOOLSDIR)/tools/mkimage
	$(CROSS_COMPILE)strip --remove-section=.comment $(TOOLSDIR)/tools/mkenvimage
	$(CROSS_COMPILE)strip --remove-section=.comment $(TOOLSDIR)/tools/kwboot
	$(CROSS_COMPILE)strip --remove-section=.comment $(TOOLSDIR)/tools/mksunxiboot
	$(CROSS_COMPILE)strip --remove-section=.comment $(TOOLSDIR)/tools/dumpimage

override_dh_auto_test:
	# skip tests.

override_dh_clean:
	rm -rf debian/build/
	dh_clean

override_dh_gencontrol:
	debian/bin/update-substvars
	dh_gencontrol

get-orig-source:
	# Use uscan from the devscripts package to download new
	# tarball, remove files incompatible with the DFSG (see
	# Files-Excluded in debian/copyright), and repack with xz
	# compression.
	uscan --download --repack --compression=xz
