#!/usr/bin/make -f
# -*- makefile -*-
# Copyright © 2010-2016 Stephen Kitt <skitt@debian.org>
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.

# Disable package mangling in Launchpad; it currently fails to parse
# Built-Using, which results in build failures
export NO_PKG_MANGLE=1

top_dir := $(shell pwd)
binutils_dir := /usr/src/binutils
upstream_dir := $(top_dir)/upstream
build_dir := $(top_dir)/build
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)
gnu_upstream_version := $(shell echo $(deb_upstream_version) | cut -d. -f1-3)

targets := i686-w64-mingw32 x86_64-w64-mingw32

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@ --with autotools-dev

spelling = grep -rl "$(1)" $(upstream_dir) | xargs -r sed -i "s/$(1)/$(2)/g"

unpack: unpack-stamp
unpack-stamp:
	tar xf $(binutils_dir)/binutils-$(gnu_upstream_version).tar.*
	rm -rf $(upstream_dir)
	mv binutils-$(deb_upstream_version) $(upstream_dir)
	# Revert the ldscripts location patch so we avoid conflicting with binutils
	patch -d $(upstream_dir) -R -p1 < /usr/src/binutils/patches/001_ld_makefile_patch.patch
	# Apply our patches
	QUILT_SERIES=debian/patches/series QUILT_PATCHES=debian/patches quilt push -a
	# On big-endian architectures, disable the windres tests (#754829)
	if [ "$$(dpkg-architecture -qDEB_HOST_ARCH_ENDIAN)" = "big" ]; then \
		rm -f $(upstream_dir)/binutils/testsuite/binutils-all/windres/windres.exp; \
	fi
	# mpx-inval-2 fails when targeting x86_64
	sed -i 's/run_list_test "x86-64-mpx-inval-2" "-al"//g' $(upstream_dir)/gas/testsuite/gas/i386/i386.exp
	# gc and plugin tests are currently broken
	rm -f $(upstream_dir)/ld/testsuite/ld-gc/gc.exp
	rm -f $(upstream_dir)/ld/testsuite/ld-plugin/plugin.exp
	# Spelling fixes
	$(call spelling,allows to,allows one to)
	$(call spelling,auxilary,auxiliary)
	$(call spelling,maching,matching)
	$(call spelling,preceeded,preceded)
	$(call spelling,refered,referred)
	$(call spelling,sectinos,sections)
	$(call spelling,supressed,suppressed)
	$(call spelling,targetted,targeted)
	touch $@

override_dh_auto_clean:
	rm -rf .pc *-stamp $(build_dir) $(upstream_dir) $(patsubst %,debian/%-dllwrap.1,$(targets))
	dh_auto_clean

override_dh_auto_configure: unpack-stamp
	set -e; \
	for target in $(targets); do \
		dh_auto_configure -B$(build_dir)/$$target -D$(upstream_dir) -- \
				  --prefix=/usr --disable-multilib --enable-lto --enable-plugins --enable-deterministic-archives --with-system-zlib --target=$$target --disable-werror; \
	done

override_dh_auto_build-arch:
	set -e; \
	for target in $(targets); do \
		dh_auto_build --parallel -B$(build_dir)/$$target; \
	done

override_dh_auto_test-arch:
	set -e; \
	unset CFLAGS CPPFLAGS LDFLAGS; \
	env > $(build_dir)/env; \
	for target in $(targets); do \
		dh_auto_test -B$(build_dir)/$$target; \
	done

override_dh_auto_install-arch:
	for target in $(targets); do \
		dh_auto_install -B$(build_dir)/$$target; \
	done

	# Drop files which will conflict with other packages
	rm -rf debian/tmp/usr/lib/*.a
	rm -rf debian/tmp/usr/share/info
	rm -rf debian/tmp/usr/share/locale

	# Copy the dllwrap manpages
	for target in $(targets); do \
		cp debian/dllwrap.1 debian/$$target-dllwrap.1; \
	done

override_dh_install-arch:
	dh_install -a

# Symlink duplicates — see https://wiki.debian.org/dedup.debian.net
# We remove hardlinks too, otherwise one of the ld hardlinks is left alone and
# isn't symlinked
	for install in debian/*.install; do \
		rdfind -outputname /dev/null -removeidentinode false -makesymlinks true $${install%%.*}; \
		symlinks -r -s -c $${install%%.*}; \
	done

override_dh_installchangelogs:
	dh_installchangelogs $(upstream_dir)/ChangeLog

override_dh_gencontrol:
	dh_gencontrol -- -v$(deb_version) -Vlocal:Version=$(deb_upstream_version) -Vbinutils:Version=$(source_version)
