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

target=msp430-elf

GCC_VERSION=16
GCC_PACKAGE=gcc-$(GCC_VERSION)
GCC_DIR := /usr/src/$(GCC_PACKAGE)
GCC_TARBALL := $(notdir $(wildcard $(GCC_DIR)/gcc-*.tar.*))

package=gcc-$(target)
top_dir=$(shell pwd)
stampdir=debian/stamp

include /usr/share/dpkg/pkg-info.mk
source_version := $(shell dpkg-query -W -f="\$${Version}\n" $(GCC_PACKAGE)-source)
deb_version := $(source_version)+$(DEB_VERSION)
deb_upstream_version := $(shell echo $(deb_version) | cut -d- -f1)
base_version := $(shell echo $(deb_version) | sed -e 's/\([1-9]\.[0-9]\).*-.*/\1/')
BUILT_USING := $(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W $(GCC_PACKAGE)-source)

unpack_stamp=$(stampdir)/unpack
tar_stamp=$(stampdir)/tar

build_dir=build

export DEB_BUILD_MAINT_OPTIONS=hardening=+all,-format
buildflags := $(shell dpkg-buildflags --export=configure)
buildflags += INHIBIT_LIBC_CFLAGS="-DUSE_TM_CLONE_REGISTRY=0"

configure_flags = \
	--enable-languages=c,c++,lto \
	--prefix=/usr \
	--program-prefix=$(target)- \
	--infodir=/usr/share/doc/$(package)/info \
	--mandir=/usr/share/man \
	--htmldir=/usr/share/doc/$(package)/html \
	--pdfdir=/usr/share/doc/$(package)/pdf \
	--libexecdir=/usr/libexec \
	--libdir=/usr/lib \
	--with-system-zlib \
	--enable-multilib \
	--disable-decimal-float \
	--disable-libffi \
	--disable-libgomp \
	--disable-libmudflap \
	--disable-libquadmath \
	--disable-libssp \
	--disable-libstdcxx \
	--disable-nls \
	--disable-shared \
	--disable-threads \
	--enable-target-optspace \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--host=$(DEB_HOST_GNU_TYPE) \
	--target=$(target) \
	--with-gcc-major-version-only \
	--with-gnu-as \
	--with-gnu-ld \
	--without-headers \
	--with-newlib \
	"--with-pkgversion=$(deb_version)" \
	--without-included-gettext \
	SED=/bin/sed \
	SHELL=/bin/sh \
	BASH=/bin/bash \
	CONFIG_SHELL=/bin/bash \
	$(buildflags)

%:
	dh $@ -Dsrc -B$(build_dir)

$(unpack_stamp): $(tar_stamp)
	set -ex; \
		GFDL_INVARIANT_FREE=yes \
		patchdir=$(top_dir)/patches \
		stampdir=$(top_dir)/$(stampdir) \
		make -f $(GCC_DIR)/debian/rules.patch series; \
		QUILT_PATCHES=$(top_dir)/patches QUILT_PATCH_OPTS='-E' \
		quilt --quiltrc /dev/null push -a; \
	touch src/gcc/distro-defaults.h;
	touch $@

$(tar_stamp):
	tar xf $(GCC_DIR)/$(GCC_TARBALL)
	ln -s gcc-$(GCC_VERSION)* src
	mkdir -p $(stampdir)
	cp /usr/share/doc/$(GCC_PACKAGE)-source/copyright debian/copyright-gcc
	cp -a $(GCC_DIR)/debian/patches .
	touch $@

override_dh_autoreconf: $(unpack_stamp)
	autoreconf2.69 -f -i src

override_dh_auto_configure: $(unpack_stamp)
	mkdir -p $(build_dir)
	DH_COMPAT=10 dh_auto_configure -Dsrc -B$(build_dir) -- $(configure_flags)

override_dh_auto_clean:
	rm -rf gcc-* patches src $(build_dir) debian/tmp debian/copyright-gcc $(stampdir)
	dh_autoreconf_clean

override_dh_auto_test:
	@echo "no testing, that's way too painful"
#	dh_auto_test -Dsrc -B$(build_dir) --max-parallel=1

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

override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp
	rm debian/tmp/usr/libexec/gcc/$(target)/$(GCC_VERSION)/liblto_plugin.la

override_dh_installman:
	rm -rf debian/tmp/usr/share/man

override_dh_installchangelogs:
	dh_installchangelogs src/ChangeLog

override_dh_makeshlibs: ;

override_dh_compress:
	dh_compress -Xexamples/

override_dh_strip:
	dh_strip -X.a
