#!/usr/bin/make -f

# 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))

# remove -Bsymbolic-functions from LDFLAGS because is messes up ld and some
# initializations end up being zero
LDFLAGS := $(patsubst -Bsymbolic-functions,,$(LDFLAGS))

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

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

deb_source := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
deb_version := $(shell dpkg-parsechangelog | sed -n 's/^Version: //p')
upstream_version := $(shell echo '$(deb_version)' | sed 's/.*://; s/-[^-]*$$//')

# u-boot has build errors with 4.6, 4.5 and 4.7 work fine
export CC=
ifeq ($(strip $(CC)),)
ifneq ($(strip $(shell $(CROSS_COMPILE)gcc-4.7.1 -dumpversion 2>/dev/null || true)),)
export CC=$(CROSS_COMPILE)gcc-4.7.1
endif
endif
ifeq ($(strip $(CC)),)
ifneq ($(strip $(shell $(CROSS_COMPILE)gcc-4.7 -dumpversion 2>/dev/null || true)),)
export CC=$(CROSS_COMPILE)gcc-4.7
endif
endif
ifeq ($(strip $(CC)),)
ifneq ($(strip $(shell $(CROSS_COMPILE)gcc-4.5 -dumpversion 2>/dev/null || true)),)
export CC=$(CROSS_COMPILE)gcc-4.5
endif
endif

# --remote doesn't work with git.linaro.org so this needs to be
# run from a local checkout
get-orig-source:
	rm -rf $(deb_source)-$(upstream_version)
	rm -f $(deb_source)-$(upstream_version).orig.tar.gz
	git clone --depth 1 git://git.linaro.org/boot/u-boot-linaro-stable.git $(deb_source)-$(upstream_version)
	cd $(deb_source)-$(upstream_version) && git archive \
		--format=tar \
		--prefix=$(deb_source)-$(upstream_version)/ \
		$(upstream_version) \
		| gzip >../$(deb_source)_$(upstream_version).orig.tar.gz
	rm -rf $(deb_source)-$(upstream_version)

%:
	dh --with quilt $@

override_dh_auto_clean:
	# upstream only cleans these files if passing the right O=, but it
	# shouldn't create these the source tree in the first place
	# XXX this hardcodes "arm"
	rm -f arch/arm/include/asm/arch arch/arm/include/asm/proc
	rm -rf debian/build

override_dh_auto_build:
	echo building using $(CC)
	while read plat deb buildtarget ignoreinstalltargets; do \
		expr $$plat : "^#" > /dev/null && continue; \
		mkdir -p debian/build/$$plat; \
		$(MAKE) O=$$PWD/debian/build/$$plat $${plat}_config; \
		test "$$buildtarget" = "-" && $(MAKE) CC="$(CC)" O=$$PWD/debian/build/$$plat; \
		test "$$buildtarget" != "-" && $(MAKE) CC="$(CC)" O=$$PWD/debian/build/$$plat \
			$$PWD/debian/build/$$plat/$$buildtarget; \
		true; \
	done <debian/platforms-debs-targets

override_dh_auto_install:
	while read plat deb ignorebuildtarget installtargets; do \
		expr $$plat : "^#" > /dev/null && continue; \
		for target in $$installtargets; do \
			mkdir -p debian/u-boot-linaro-$$deb/usr/lib/u-boot/$$plat/; \
			test -e debian/build/$$plat/$$target && \
				install	-m644 \
					-t debian/u-boot-linaro-$$deb/usr/lib/u-boot/$$plat/ \
					debian/build/$$plat/$$target; \
		done; \
		true; \
	done <debian/platforms-debs-targets
	cp -a debian/u-boot-linaro-omap4-panda-splusb/usr debian/u-boot-linaro-omap4-panda

