#!/usr/bin/make -f
export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1

target=arm-none-eabi

package=binutils-arm-none-eabi
top_dir=$(shell pwd)
binutils_dir=/usr/src/binutils
stampdir=debian/stamp

source_version := $(shell dpkg-query -W -f="\$${Version}\n" binutils-source)
deb_version := $(source_version)+$(shell dpkg-parsechangelog | sed -ne "s/^Version: \(.*\)/\1/p")
deb_upstream_version := $(shell echo $(deb_version) | cut -d- -f1)

upstream_dir=binutils-$(deb_upstream_version)

untar_stamp=$(stampdir)/tar

buildflags=$(shell dpkg-buildflags --export=configure)
BUILT_USING := $(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W binutils-source)

configure_flags = \
	--target=$(target) \
	--prefix=/usr/lib \
	--bindir=/usr/bin \
	--libdir=/usr/lib/arm-none-eabi \
	--infodir=/usr/share/doc/$(package)/info \
	--htmldir=/usr/share/doc/$(package)/html \
	--pdfdir=/usr/share/doc/$(package)/pdf \
	--mandir=/usr/share/man \
	--disable-nls \
	--disable-werror \
	--enable-plugins \
	--enable-interwork \
	--with-system-zlib \
	"--with-pkgversion=$(deb_version)" \
	--enable-deterministic-archives \
	$(buildflags)

%:
	dh $@ -D$(upstream_dir) -Bbuild

$(untar_stamp):
	tar xf $(binutils_dir)/binutils-*.tar.*
	mkdir -p $(stampdir)
	cp /usr/share/doc/binutils-source/copyright debian/copyright
	touch $@

override_dh_auto_configure: $(untar_stamp)
	dh_auto_configure -D$(upstream_dir) -Bbuild -- $(configure_flags)

override_dh_gencontrol:
	dh_gencontrol -a -- -v$(deb_version) -Vlocal:Version=$(deb_upstream_version) -Vbinutils:Version=$(source_version) -VBuilt-Using="$(BUILT_USING)"

override_dh_auto_clean:
	rm -rf $(upstream_dir) build $(stampdir)

override_dh_installchangelogs:
	dh_installchangelogs $(upstream_dir)/ChangeLog

override_dh_auto_test:
