#!/usr/bin/make -f
# -*- makefile -*-
# Copyright © 2010-2020 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/>.

include /usr/share/dpkg/default.mk

squote := '
escsq = $(subst $(squote),'\$(squote)',$1)

top_dir := $(shell pwd)
binutils_dir := /usr/src/binutils
upstream_dir := $(top_dir)/upstream
build_dir := $(top_dir)/build

# $(source_version) may arrive in format "epoch:upstream_version-debian_revision"
source_version := $(shell dpkg-query -W -f='$${Version}' binutils-source)
source_version_noepoch := $(lastword $(subst :, ,$(source_version)))
source_version_upstream := $(firstword $(subst -, ,$(source_version_noepoch)))

deb_version := $(source_version_noepoch)+$(DEB_VERSION_UPSTREAM_REVISION)
deb_local_version := $(firstword $(subst -, ,$(source_version)))
gnu_upstream_version := $(shell echo $(source_version_upstream) | cut -d. -f1-3)

target := i386-pc-msdosdjgpp

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

PF := /usr
confflags = \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--host=$(DEB_HOST_GNU_TYPE) \
	--target=$(target) \
	--prefix=$(PF) \
	--bindir=$(PF)/bin \
	--libdir=$(PF)/$(target)/lib \
	--libexecdir=$(PF)/$(target)/libexec \
	--disable-compressed-debug-sections \
	--disable-dependency-tracking \
	--disable-maintainer-mode \
	--disable-silent-rules \
	--disable-werror \
	--enable-deterministic-archives \
	--enable-lto \
	--enable-plugins \
	--with-system-zlib

%:
	dh $@ --without autoreconf --with single-binary

spelling = @$(top_dir)/debian/scripts/spelling.sh $(upstream_dir) '$(call escsq,$(strip $(1)))' '$(call escsq,$(strip $(2)))'

.PHONY: unpack
unpack:
	rm -rf $(upstream_dir); mkdir -p $(upstream_dir); \
	ls -l $(binutils_dir)/binutils-$(gnu_upstream_version).tar.* ; \
	tar -C $(upstream_dir) --strip-components=1 -xf $(binutils_dir)/binutils-$(gnu_upstream_version).tar.*
# 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 codeview tests
	if [ '$(DEB_HOST_ARCH_ENDIAN)' = 'big' ]; then \
		rm -f $(upstream_dir)/gas/testsuite/gas/i386/codeview.exp; \
		rm -f $(upstream_dir)/ld/testsuite/ld-pe/pdb.exp; \
	fi
# Spelling fixes
	@echo 'Spelling fixes:'
	$(call spelling,allows to,allows one to)
	$(call spelling,ambigious,ambiguous)
	$(call spelling,complier,compiler)
	$(call spelling,explict,explicit)
	$(call spelling,extention,extension)
	$(call spelling,indentify,identify)
	$(call spelling,maching,matching)
	$(call spelling,preceeded,preceded)
	$(call spelling,refered,referred)
	$(call spelling,specifed,specified)
	$(call spelling,supressed,suppressed)
	$(call spelling,targetted,targeted)

override_dh_clean:
	dh_clean
	rm -rf .pc $(build_dir) $(upstream_dir) $(patsubst %,debian/%-dllwrap.1,$(targets))

# Unpack before autotools
override_dh_update_autotools_config: unpack
	dh_update_autotools_config

override_dh_auto_configure:
	rm -rf $(build_dir); mkdir -p $(build_dir)/$(target)
	cd $(build_dir)/$(target) && $(upstream_dir)/configure $(confflags)

override_dh_auto_build-arch:
	dh_auto_build -B$(build_dir)/$(target)

# The test suite expects SOURCE_DATE_EPOCH to *not* be set
override_dh_auto_test-arch:
	unset CFLAGS CPPFLAGS LDFLAGS SOURCE_DATE_EPOCH; \
	dh_auto_test -B$(build_dir)/$(target)

override_dh_auto_install-arch:
	dh_auto_install -B$(build_dir)/$(target)

# Drop files which will conflict with other packages
	rm -f debian/tmp/usr/lib/*.a

	rm -rf debian/tmp/usr/share/info
	rm -rf debian/tmp/usr/share/locale

# Drop files we don’t need
	rm -f debian/tmp/usr/share/man/man1/*-dlltool.1* \
	      debian/tmp/usr/share/man/man1/*-wind*.1*

execute_after_dh_install-arch:
# Before mass-processing symlinks, handle ld -> ld.bfd
# ld.bfd is the implementation, ld the variant selector so
# the link should be ld -> ld.bfd; rdfind produces the opposite
	for file in $$(find debian/binutils*/ -name \*ld.bfd); \
		do (cd $$(dirname $$file) && \
		ln -sf $$(basename $$file) $$(basename $$file .bfd)); \
	done
	for file in $$(find debian/binutils*/ -name \*ld.1); \
		do mv $$file $${file%.1}.bfd.1; \
	done
# 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
	rdfind -outputname /dev/null -removeidentinode false -makesymlinks true debian/binutils-djgpp
	symlinks -r -s -c debian/binutils-djgpp

override_dh_installchangelogs:
	dh_installchangelogs $(upstream_dir)/ChangeLog

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