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

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)

base_targets := i686-w64-mingw32 x86_64-w64-mingw32
targets := $(strip $(foreach target,$(strip $(base_targets)), $(target) $(target)ucrt))

short_target = $(patsubst %-w64-mingw32,%,$(patsubst %-w64-mingw32ucrt,%-ucrt,$(subst _,-,$(strip $(1)))))
short_target_sed = sed -E 's/_/-/g;s/-w64-mingw32ucrt$$/-ucrt/;s/-w64-mingw32//'

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

PF := /usr
confflags = \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--host=$(DEB_HOST_GNU_TYPE) \
	--target=$(1) \
	--prefix=$(PF) \
	--bindir=$(PF)/bin \
	--libdir=$(PF)/$(1)/lib \
	--libexecdir=$(PF)/$(1)/lib \
	--disable-compressed-debug-sections \
	--disable-dependency-tracking \
	--disable-maintainer-mode \
	--disable-multilib \
	--disable-nls \
	--disable-silent-rules \
	--disable-werror \
	--disable-x86-used-note \
	--enable-deterministic-archives \
	--enable-jansson \
	--enable-lto \
	--enable-plugins \
	--enable-serial-configure \
	--enable-threads \
	--with-system-zlib

%:
	dh $@ --without autoreconf

debian/control: debian/rules $(wildcard debian/control.* debian/target.*)
	echo '# This file is generated using "debian/rules debian/control"' > $@
	echo '# DO NOT EDIT!' >> $@
	echo >> $@
	cat debian/control.source >> $@
	echo >> $@
	cat debian/control.base >> $@
	echo >> $@
	cat debian/control.meta >> $@
	for target in $(call short_target,$(targets)); do \
		echo >> $@; \
		sed -f debian/target.$${target}.sed debian/control.target \
		| sed "s/@@TARGET@@/$${target}/g" >> $@; \
	done
	echo >> $@
	cat debian/control.ucrt64-compat >> $@
	sed -E -i '/^(Pre-Depends|Depends|Recommends|Suggests|Breaks|Conflicts|Replaces|Provides): $$/d' $@
	sed -E -i 's/ +$$//g' $@

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)

	set -e; \
	env -C $(build_dir) \
	  mkdir -p $(targets); \
	for target in $(targets); do \
		env -C $(build_dir)/$${target} \
		  $(upstream_dir)/configure $(call confflags,$${target}); \
	done

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

# The test suite can't run in parallel (it fails because of libbfd.tmp)
# It also expects SOURCE_DATE_EPOCH to *not* be set
override_dh_auto_test-arch:
	set -e; \
	unset CFLAGS CPPFLAGS LDFLAGS SOURCE_DATE_EPOCH; \
	for target in $(targets); do \
		dh_auto_test --no-parallel -B$(build_dir)/$${target}; \
	done

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

# Drop unused files
	for target in $(filter x86_64-%,$(targets)); do \
		rm -f debian/tmp/usr/$${target}/lib/ldscripts/i386pe.* ; \
	done

# 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

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

execute_before_dh_install:
	for _in in debian/*.in; do \
		_out=$${_in%.in}; \
		case "$${_out}" in \
		*TARGET* ) \
			for target in $(call short_target,$(targets)); do \
				extra_in="$${_out}.$${target}.t"; \
				target_out=$$(printf '%s' "$${_out}" | sed "s/TARGET/$${target}/g"); \
				{ \
				  cat $${_in}; \
				  if [ -s "$${extra_in}" ]; then cat $${extra_in}; fi; \
				} \
				| sed -f debian/target.$${target}.sed \
				| sed "s/@@TARGET@@/$${target}/g" > $${target_out}; \
				chmod --reference=$${_in} $${target_out}; \
				touch -r $${_in} $${target_out}; \
			done; \
		: ;; \
		* ) \
			: not supported yet ; \
			cat < $${_in} > $${_out}; \
			chmod --reference=$${_in} $${_out}; \
			touch -r $${_in} $${_out}; \
		: ;; \
		esac; \
	done
	mkdir -p debian/binutils-mingw-w64-all/usr/share/mingw-w64
	echo binutils_targets := $(targets) > debian/binutils-mingw-w64-all/usr/share/mingw-w64/binutils.targets

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
	for p in $$(dh_listpackages -a); do \
		d=debian/$$p; \
		[ -d "$$d" ] || continue; \
		rdfind -outputname /dev/null -removeidentinode false -makesymlinks true $$d; \
		symlinks -r -s -c $$d; \
	done

override_dh_installchangelogs-arch:
	dh_installchangelogs -pbinutils-mingw-w64-base $(upstream_dir)/ChangeLog
	dh_installchangelogs -a --remaining-packages

# Share manpages across packages
execute_after_dh_compress-arch:
	dst_pkg='binutils-mingw-w64-base'; \
	dst_dir="debian/$${dst_pkg}/usr/share/man"; \
	mkdir -p $${dst_dir}; \
	for target in $(targets); do \
		target_stem=$$(echo "$${target}" | $(short_target_sed)); \
		src_pkg="binutils-mingw-w64-$${target_stem}"; \
		src_dir="debian/$${src_pkg}/usr/share/man"; \
		find $${src_dir}/ -type f | sort -uV \
		| while read -r src_file ; do \
			: share with binutils-common if applicable ; \
			dst_file=$$(echo "$${src_file}" | sed -E "s#^debian/$${src_pkg}##"); \
			dst_file=$$(echo "$${dst_file}" | sed -E "s#$${target}-##"); \
			link_path="$${dst_file##*/}"; \
			if [ -f "$${dst_file}" ]; then \
				rm -f "$${src_file}"; \
				ln -s "$${link_path}" "$${src_file}"; \
				continue; \
			fi; \
			: share with binutils-mingw-w64-base ; \
			dst_file=$$(echo "$${src_file}" | sed -E "s#^$${src_dir}#$${dst_dir}#"); \
			dst_file=$$(echo "$${dst_file}" | sed -E "s#$${target}#binutils-mingw-w64#"); \
			link_path="$${dst_file##*/}"; \
			[ -f "$${dst_file}" ] || install -D "$${src_file}" "$${dst_file}"; \
			rm -f "$${src_file}"; \
			ln -s "$${link_path}" "$${src_file}"; \
		done; \
	done

override_dh_installdocs-arch:
	dh_installdocs -pbinutils-mingw-w64-base
	dh_installdocs -a --remaining-packages --link-doc=binutils-mingw-w64-base

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