#!/usr/bin/make -f
# debian/rules file - for binutils (2.20)
# Based on sample debian/rules file - for GNU Hello (1.3).
# Copyright 1994,1995 by Ian Jackson.
# Copyright 1998-2007 James Troup.
# Portions Copyright 2008-2015 Canonical Ltd.
# Portions Copyright 2008-2015 Matthias Klose.
# I hereby give you perpetual unlimited permission to copy,
# modify and relicense this file, provided that you do not remove
# my name from the file itself.  (I assert my moral right of
# paternity under the Copyright, Designs and Patents Act 1988.)
# This file may have to be extensively modified

###############################################################################

p_bin = binutils
p_dev = $(p_bin)-dev
p_mul = $(p_bin)-multiarch
p_mdev = $(p_mul)-dev
p_doc = $(p_bin)-doc
p_hppa64 = $(p_bin)-hppa64-linux-gnu
p_src = $(p_bin)-source

distribution := $(shell lsb_release -is)
distrelease  := $(shell lsb_release -cs)
ifeq ($(distrelease),n/a)
  distrelease     := sid
endif

# BACKPORT is used for cross builds for a -source package not conflicting
# with the source package of the native package.
ifeq ($(BACKPORT),true)
  p_src = $(p_bin)-$(VERSION)-source
else
  p_src = $(p_bin)-source
endif

pwd   := $(shell pwd)
d     = debian/tmp
d_bin = $(d)
d_dev = debian/$(p_dev)
d_mul = debian/$(p_mul)
d_mdev = debian/$(p_mdev)
d_doc = debian/$(p_doc)
d_hppa64 = debian/$(p_hppa64)
d_src = debian/$(p_src)

install_dir    = install -d -m 755
install_file   = install -m 644
install_script = install -m 755
install_binary = install -m 755 -s

vafilt = $(subst $(2)=,,$(filter $(2)=%,$(1)))
DPKG_VARS := $(shell dpkg-architecture)
DEB_BUILD_GNU_TYPE	?= $(call vafilt,$(DPKG_VARS),DEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH		?= $(call vafilt,$(DPKG_VARS),DEB_HOST_ARCH)
DEB_HOST_GNU_CPU	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_CPU)
DEB_HOST_GNU_TYPE	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_TYPE)
DEB_HOST_MULTIARCH	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_MULTIARCH)

ifeq (,$(filter $(distrelease),lenny etch squeeze wheezy jessie))
  ifeq ($(DEB_HOST_GNU_TYPE),i586-linux-gnu)
    DEB_BUILD_GNU_TYPE	= i686-linux-gnu
    DEB_HOST_GNU_CPU	= i686
    DEB_HOST_GNU_TYPE	= i686-linux-gnu
    TRIPLET_SYMLINK	= i586-linux-gnu
  else ifeq ($(DEB_HOST_GNU_TYPE),i586-kfreebsd-gnu)
    DEB_BUILD_GNU_TYPE	= i686-kfreebsd-gnu
    DEB_HOST_GNU_CPU	= i686
    DEB_HOST_GNU_TYPE	= i686-kfreebsd-gnu
    TRIPLET_SYMLINK	= i586-kfreebsd-gnu
  else ifeq ($(DEB_HOST_GNU_TYPE),i586-gnu)
    DEB_BUILD_GNU_TYPE	= i686-gnu
    DEB_HOST_GNU_CPU	= i686
    DEB_HOST_GNU_TYPE	= i686-gnu
    TRIPLET_SYMLINK	= i586-gnu
  endif
endif

src_name := $(shell dpkg-parsechangelog -S Source)
ifeq ($(src_name),binutils)
  with_native	= yes
  with_cross	= yes
  same_source	= yes
  CROSS_ARCHS	?= s390x ppc64el powerpc arm64 armhf armel mips mipsel
  CROSS_ARCHS	+= alpha hppa m68k mips64 mips64el powerpcspe ppc64 sh4 sparc64
  #CROSS_ARCHS	+= kfreebsd-amd64 kfreebsd-i386 hurd-i386
else
  # binutils-ports ...
  with_cross	= yes
  CROSS_ARCHS	?= m68k sh4 mips64 mips64el
endif

ifneq (,$(filter $(DEB_HOST_ARCH),amd64 i386 x32 hppa))
  with_hppa64 = yes
endif

HOST_ARCHS_armhf = amd64 i386 x32 arm64
HOST_ARCHS_armel = amd64 i386 x32 arm64
HOST_ARCHS_arm64 = amd64 i386 x32
HOST_ARCHS_mips = amd64 i386 x32
HOST_ARCHS_mipsel = amd64 i386 x32
HOST_ARCHS_powerpc = amd64 i386 x32 ppc64el
HOST_ARCHS_ppc64el = amd64 i386 x32 ppc64
HOST_ARCHS_s390x = amd64 i386 x32

HOST_ARCHS_alpha = amd64 i386 x32
HOST_ARCHS_hppa = amd64 i386 x32
HOST_ARCHS_m68k = amd64 i386 x32
HOST_ARCHS_mips64 = amd64 i386 x32
HOST_ARCHS_mips64el = amd64 i386 x32
HOST_ARCHS_powerpcspe = amd64 i386 x32
HOST_ARCHS_ppc64 = amd64 i386 x32 ppc64el
HOST_ARCHS_sh4 = amd64 i386 x32
HOST_ARCHS_sparc64 = amd64 i386 x32

HOST_ARCHS_kfreebsd-amd64 = amd64 i386 x32
HOST_ARCHS_kfreebsd-i386 = amd64 i386 x32
HOST_ARCHS_hurd-i386 = amd64 i386 x32

ifneq (,$(filter $(DEB_HOST_ARCH), amd64 i386 x32))
  #
else ifeq ($(DEB_HOST_ARCH),arm64)
  CROSS_ARCHS = armel armhf
else ifeq ($(DEB_HOST_ARCH),ppc64)
  CROSS_ARCHS = ppc64el
else ifeq ($(DEB_HOST_ARCH),ppc64el)
  CROSS_ARCHS = powerpc ppc64
else
  CROSS_ARCHS =
endif
CROSS_ARCH	= $(subst .,,$(suffix $@))
_gnu_type	= $(shell dpkg-architecture -a$(1) -qDEB_HOST_GNU_TYPE -f 2>/dev/null)
_multiarch	= $(shell dpkg-architecture -a$(1) -qDEB_HOST_MULTIARCH -f 2>/dev/null)
CROSS_GNU_TYPE	= $(subst i586,i686,$(call _gnu_type,$(CROSS_ARCH)))
P_CROSS		= binutils-$(subst _,-,$(CROSS_GNU_TYPE))
D_CROSS		= debian/binutils-$(subst _,-,$(CROSS_GNU_TYPE))

# If $(TARGET) is not set, try reading debian/target
ifeq (,$(TARGET))
  ifneq (,$(wildcard debian/target))
    TARGET := $(shell cat debian/target 2>/dev/null)
  endif
endif

ifneq (,$(TARGET))
  with_native	=
  with_cross	=
  same_source	=
  # Support TARGET both as Debian architecture specification (e.g. arm),
  # and as the target name (e.g. arm-linux-gnu).
  try_convert := $(shell dpkg-architecture -f -a$(TARGET) -qDEB_HOST_GNU_TYPE 2>/dev/null)
  ifneq ($(try_convert),)
    override TARGET := $(try_convert)
  endif
  DPKG_TARGET_VARS := $(shell dpkg-architecture -f -t$(TARGET))
  DEB_TARGET_MULTIARCH = $(call vafilt,$(DPKG_TARGET_VARS),DEB_HOST_MULTIARCH)
  DEB_TARGET_ARCH      = $(call vafilt,$(DPKG_TARGET_VARS),DEB_HOST_ARCH)
else
  DEB_TARGET_MULTIARCH = $(DEB_HOST_MULTIARCH)
  DEB_TARGET_ARCH      = $(DEB_HOST_ARCH)
endif

ifneq (,$(TARGET))
  APPEND_TOOLLIBDIR=yes
  export APPEND_TOOLLIBDIR
endif

ifeq (,$(DEB_HOST_MULTIARCH))
  ifeq ($(DEB_HOST_ARCH),i386)
    DEB_HOST_MULTIARCH = i386-linux-gnu
  else
    DEB_HOST_MULTIARCH = $(DEB_HOST_GNU_TYPE)
  endif
endif

SHELL  = /bin/bash

gold_targets = \
	amd64 arm64 armel armhf i386 \
	mips mipsel mipsn32 mipsn32el mips64 mips64el \
	powerpc powerpcspe ppc64 ppc64el \
	x32 kfreebsd-amd64 kfreebsd-i386 hurd-i386

ifneq (,$(filter $(DEB_HOST_ARCH), $(gold_targets)))
  with_gold = yes
  gold_provides = -Vgold:Provides=binutils-gold
endif

with_multiarch := yes

CC     = gcc
CXX    = g++
CFLAGS = -g -O2 -Wno-format-security
STRIP  = strip --remove-section=.comment --remove-section=.note
CROSS :=
ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
  CROSS := $(DEB_HOST_GNU_TYPE)-
  CC   = $(CROSS)gcc
  CXX  = $(CROSS)g++
  STRIP= $(CURDIR)/debian/strip.cross
  install_binary = install -m 755 -s --strip-program="$(STRIP)"
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS = -g -O0
endif

# this outputs 0 or 1 depending on whether a macro appears in the *default* cpp
# -dM -P output; this is used to test the toolchain *default* configuration
check_cpp = $(shell $(CROSS)cpp -dM -P /dev/null | grep -q '^\#define $(1)' && echo 1 || echo 0)

# testsuite doesn't expect to be built with -mthumb
# TODO if the testsuite is only broken with Thumb-2 (and not with "Thumb-1"),
# we should test for __thumb2__ instead
ifneq (,$(filter $(DEB_HOST_ARCH), armel armhf))
  ifeq ($(call check_cpp,__thumb__),1)
    CFLAGS += -marm
  endif
endif

SPACE = $(EMPTY) $(EMPTY)
COMMA = ,
CHANGELOG_VARS := $(shell dpkg-parsechangelog | \
        sed -n 's/ /_/g;/^[^_]/s/^\([^:]*\):_\(.*\)/\1=\2/p')

DEB_VERSION	:= $(call vafilt,$(CHANGELOG_VARS),Version)
DEB_SVERSION	:= $(shell echo $(DEB_VERSION) | sed 's/+b[0-9][0-9]*$$//')
DEB_UPSTREAM	:= $(firstword $(subst -,$(SPACE),$(DEB_VERSION)))

VERSION		:= $(shell sed -n 's/^ *VERSION=\(.*\)/\1/p' bfd/configure | head -1 | tr -d \')
DATE		:= $(shell sed -n 's/.* \([0-9]*\)$$/\1/p' bfd/version.h)
ifneq (,$(DATE))
#  DATE_EXT	:= .$(DATE)
endif
BUILD_DATE := $(shell dpkg-parsechangelog | sed -n -e 's/^Date: //p')

is_rc = yes
is_rc =

ifneq ($(DEB_UPSTREAM),$(VERSION)$(DATE_EXT))
  $(error upstream ($(VERSION)$(DATE_EXT)) and debian ($(DEB_UPSTREAM)) version mismatch)
endif

SINGLE_VERSION= $(VERSION)-system
MULTI_VERSION = $(VERSION)-multiarch
HPPA64_VERSION= $(VERSION)-hppa64

ifeq (,$(filter $(distrelease),lenny etch squeeze wheezy dapper hardy jaunty karmic lucid maverick natty oneiric precise quantal raring saucy trusty utopic))
  DPKG_DEV = dpkg-dev (>= 1.17.11),
endif

NJOBS =
# Support parallel=<n> in DEB_BUILD_OPTIONS (see #209008)
ifneq (,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
  NJOBS := -j $(subst parallel=,,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
endif
ifneq (,$(findstring nogold,$(DEB_BUILD_OPTIONS)))
  with_gold = disabled in DEB_BUILD_OPTIONS
endif
ifneq (,$(findstring nomult,$(DEB_BUILD_OPTIONS)))
  with_multiarch = disabled in DEB_BUILD_OPTIONS
endif
ifneq (,$(findstring nocross,$(DEB_BUILD_OPTIONS)))
  with_cross = disabled in DEB_BUILD_OPTIONS
endif

# PF is the installation prefix for the package without the leading slash.
# It's "usr" for gcc releases, so use this if not explicitly set
ifeq ($(PF),)
  PF = usr
endif

# Don't include docs with GFDL invariant sections
GFDL_INVARIANT_FREE := yes
ifeq ($(distribution),Ubuntu)
  GFDL_INVARIANT_FREE := no
endif

gfdl_toplevel_texinfo_files = \
	bfd/doc/bfd.texinfo \
	bfd/doc/bfdint.texi \
	ld/ldint.texinfo

gfdl_generated_files = \
	bfd/doc/bfd.info

########################################

CONFARGS = \
	--enable-shared \
	--enable-plugins \
	--enable-threads \
	--with-system-zlib \
	--prefix=/$(PF) \
	--enable-deterministic-archives \
	--disable-compressed-debug-sections \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--host=$(DEB_HOST_GNU_TYPE) \
	--with-pkgversion="GNU Binutils for $(distribution)"

# not yet ready for GCC 4.9
CONFARGS += --disable-werror

DEB_TARGET_MULTIARCH32_amd64		= i386-linux-gnu
DEB_TARGET_MULTIARCHX32_amd64		= x86_64-linux-gnux32

DEB_TARGET_MULTIARCH32_x32		= i386-linux-gnu
DEB_TARGET_MULTIARCH64_x32		= x86_64-linux-gnu

DEB_TARGET_MULTIARCH32_powerpc		= powerpc-linux-gnu

DEB_TARGET_MULTIARCH32_s390x		= s390-linux-gnu

DEB_TARGET_MULTIARCH32_sparc64		= sparc-linux-gnu

DEB_TARGET_MULTIARCH32_kfreebsd-amd64	= i386-kfreebsd-gnu

DEB_TARGET_MULTIARCH64_i386		= x86_64-linux-gnu
DEB_TARGET_MULTIARCHX32_i386		= x86_64-linux-gnux32

DEB_TARGET_MULTIARCH64_powerpc		= powerpc64-linux-gnu

DEB_TARGET_MULTIARCH64_sparc		= sparc64-linux-gnu

DEB_TARGET_MULTIARCH64_s390		= s390x-linux-gnu

DEB_TARGET_MULTIARCH64_kfreebsd-i386	= x86_64-kfreebsd-gnu

DEB_TARGET_MULTIARCH64_mips		= mips64-linux-gnuabi64
DEB_TARGET_MULTIARCHN32_mips		= mips64-linux-gnuabin32

DEB_TARGET_MULTIARCH64_mipsel		= mips64el-linux-gnuabi64
DEB_TARGET_MULTIARCHN32_mipsel		= mips64el-linux-gnuabin32

DEB_TARGET_MULTIARCH64_mipsn32		= mips64-linux-gnuabi64
DEB_TARGET_MULTIARCH32_mipsn32		= mips-linux-gnu

DEB_TARGET_MULTIARCH64_mipsn32el 	= mips64el-linux-gnuabi64
DEB_TARGET_MULTIARCH32_mipsn32el 	= mipsel-linux-gnu

DEB_TARGET_MULTIARCH32_mips64		= mips-linux-gnu
DEB_TARGET_MULTIARCHN32_mips64		= mips64-linux-gnuabin32

DEB_TARGET_MULTIARCH32_mips64el		= mipsel-linux-gnu
DEB_TARGET_MULTIARCHN32_mipselel	= mips64el-linux-gnuabin32

DEB_TARGET_MULTIARCH32_arm64		= aarch64_ilp32-linux-gnu

SET_MULTIARCH_ENV = \
	DEB_TARGET_MULTIARCH=$(call _multiarch,$1) \
	$(if $(DEB_TARGET_MULTIARCH32_$1) ,DEB_TARGET_MULTIARCH32=$(DEB_TARGET_MULTIARCH32_$1)) \
	$(if $(DEB_TARGET_MULTIARCH64_$1) ,DEB_TARGET_MULTIARCH64=$(DEB_TARGET_MULTIARCH64_$1)) \
	$(if $(DEB_TARGET_MULTIARCHX32_$1),DEB_TARGET_MULTIARCHX32=$(DEB_TARGET_MULTIARCHX32_$1)) \
	$(if $(DEB_TARGET_MULTIARCHN32_$1),DEB_TARGET_MULTIARCHN32=$(DEB_TARGET_MULTIARCHN32_$1))

CONFARGS_TARGET_sparc		= --enable-targets=sparc64-linux-gnu
CONFLICTS_TARGET_sparc		= -VextraConflicts="libc6-dev-sparc64 (<< 2.2.5-7)"

CONFARGS_TARGET_sparc64		= --enable-targets=sparc-linux-gnu
CONFLICTS_TARGET_sparc64	= -VextraConflicts="libc6-dev-sparc64 (<< 2.2.5-7)"

CONFARGS_TARGET_powerpc		= --enable-targets=powerpc64-linux-gnu

CONFARGS_TARGET_ppc64		= --enable-targets=powerpc-linux-gnu

CONFARGS_TARGET_ppc64el		= --enable-targets=powerpc-linux-gnu

CONFARGS_TARGET_s390		= --enable-targets=s390x-linux-gnu

CONFARGS_TARGET_s390x		= --enable-targets=s390-linux-gnu

CONFARGS_TARGET_amd64		= --enable-targets=x86_64-linux-gnux32,x86_64-pep

CONFARGS_TARGET_i386		= --enable-targets=x86_64-linux-gnu,x86_64-linux-gnux32,x86_64-pep

CONFARGS_TARGET_x32		= --enable-targets=x86_64-linux-gnu,x86_64-pep

CONFLICTS_TARGET_amd64		= -VextraConflicts="binutils-mingw-w64-i686 (<< 2.23.52.20130612-1+3), binutils-mingw-w64-x86-64 (<< 2.23.52.20130612-1+3)"
CONFLICTS_TARGET_i386 = $(CONFLICTS_TARGET_amd64)
CONFLICTS_TARGET_x32  = $(CONFLICTS_TARGET_amd64)

CONFARGS_TARGET_kfreebsd-i386	= --enable-targets=x86_64-kfreebsd-gnu

CONFARGS_TARGET_mips		= --enable-targets=mips64-linux-gnuabi64,mips64-linux-gnuabin32

CONFARGS_TARGET_mipsel		= --enable-targets=mips64el-linux-gnuabi64,mips64el-linux-gnuabin32

CONFARGS_TARGET_mipsn32		= --enable-targets=mips64-linux-gnuabi64,mips-linux-gnu

CONFARGS_TARGET_mipsn32el	= --enable-targets=mips64el-linux-gnuabi64,mipsel-linux-gnu

CONFARGS_TARGET_mips64		= --enable-targets=mips64-linux-gnuabin32,mips-linux-gnu

CONFARGS_TARGET_mips64el	= --enable-targets=mips64el-linux-gnuabin32,mipsel-linux-gnu

CONFARGS_TARGET_aarch64		= --enable-targets=aarch64_be-linux-gnu

with_check := yes
ifneq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	with_check := disabled through DEB_BUILD_OPTIONS
endif
ifneq (,$(filter $(DEB_HOST_ARCH), armel armhf mips mipsel sparc))
	ignore_regressions := regressions ignored on architecture $(DEB_HOST_ARCH)
endif

ignore_regressions := regressions ignored on architecture $(DEB_HOST_ARCH)

with_strip := yes
ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	with_strip := disabled through DEB_BUILD_OPTIONS
endif

source_files = $(addprefix $(shell basename $(pwd))/, \
		 $(filter-out stamps .pc CVS debian builddir-% test-summary%, $(wildcard *)))

###############################################################################

#################
# patch targets #
#################

patch: stamps/patch
stamps/patch:
ifneq ($(PATCHED_SOURCES),yes)
	QUILT_PATCHES=$(CURDIR)/debian/patches \
	  quilt --quiltrc /dev/null push -a || test $$? = 2
endif
	@mkdir -p stamps
	touch $@

unpatch:
	QUILT_PATCHES=$(CURDIR)/debian/patches \
	  quilt --quiltrc /dev/null pop -a -R || test $$? = 2
	rm -rf .pc

update-patches:
	export QUILT_PATCHES=$(CURDIR)/debian/patches; \
	export QUILT_REFRESH_ARGS="--no-timestamps --no-index -pab"; \
	export QUILT_DIFF_ARGS="--no-timestamps --no-index -pab"; \
	while quilt push; do quilt refresh; done

###############################################################################

################
# clean target #
################

clean: unpatch
	$(checkdir)
	rm -rf stamps
	rm -rf builddir-*
	rm -rf stamps
	find . -name \*.gmo -o -name \*~ -o -name \*.info ! -name sysroff.info | xargs -r rm -f
	rm -f $(pwd)/test-summary*
	rm -rf $(d_bin) $(d_dev) $(d_mul) $(d_mdev) $(d_doc) $(d_hppa64) $(d_src)
	$(foreach ca,$(CROSS_ARCHS), rm -rf debian/binutils-$(shell echo $(call _gnu_type,$(ca)) | sed 's/_/-/g;s/i586/i686/'); )
	rm -rf debian/patched debian/tmp debian/files* debian/substvars
	rm -f debian/*.orig debian/*.rej
	rm -rf $(d_cross) debian/files debian/substvars
ifneq (,$(TARGET))
	rm -rf builddir-$(TARGET)
endif
	for i in debian/*.in; do \
	    case "$$i" in debian/control*.in) continue; esac; \
	    rm -f $${i%*.in}; \
	done

	rm -f *-stamp

###############################################################################

stamps/control: debian/control.in $(if $(TARGET),debian/control.cross.in) $(if $(filter yes, $(with_cross)),debian/control.cross.in)
ifneq (,$(TARGET))
	sed "s/@dpkg_dev@/$(DPKG_DEV)/;/^$$/ q" < debian/control.in > debian/control
	sed -e "s/@target@/$$(echo -n $(TARGET) | sed s/_/-/g)/" \
		< debian/control.cross.in >> debian/control
else
	sed -e 's/@dpkg_dev@/$(DPKG_DEV)/' \
		debian/control.in > debian/control
  ifeq ($(with_cross),yes)
	$(foreach ca,$(CROSS_ARCHS), \
	  sed \
	    -e 's/@host_archs@/$(HOST_ARCHS_$(ca))/g' \
	    -e 's/@target@/$(shell dpkg-architecture -a$(ca) -qDEB_HOST_GNU_TYPE 2>/dev/null | sed 's/_/-/g;s/i586/i686/')/g' \
	    -e 's/@binutils_alt_triplet@/$(if $(findstring i386,$(ca)),binutils-$(shell dpkg-architecture -a$(ca) -qDEB_HOST_GNU_TYPE 2>/dev/null | sed 's/i686/i586/g'))/g' \
	    debian/control.cross.in \
	    | $(if $(filter yes,$(same_source)), grep -v '^Built-Using', cat) \
	    >> debian/control; \
	)
  endif
endif
	@mkdir -p stamps
	touch $@

debian/strip.cross: debian/strip.cross.in
	sed -e "s/__TARGET__/$(CROSS)/" \
		< debian/strip.cross.in >> debian/strip.cross
	chmod 755 debian/strip.cross

#######################
# single-arch targets #
#######################

SINGLE_CONFARGS = $(CONFARGS) $(CONFARGS_TARGET_$(DEB_HOST_ARCH))
ifeq ($(with_gold),yes)
	SINGLE_CONFARGS += --enable-ld=default --enable-gold
endif

stamps/configure-single: stamps/patch
	$(checkdir)
	@echo BEGIN $@
ifeq ($(with_check),yes)
	@if echo "spawn true" | /usr/bin/expect -f - >/dev/null; then \
	  : ; \
	else \
	  echo "expect is failing on your system with the above error, which means the"; \
	  echo "testsuite will fail.  Please resolve the above issues and retry the build."; \
	  echo "-----------------------------------------------------------------------------"; \
	  exit 1; \
	fi
endif

	rm -rf stamps/configure-single builddir-single
	mkdir builddir-single
	cd builddir-single && env CC="$(CC)" CXX="$(CXX)" CFLAGS="$(CFLAGS)" \
		$(call SET_MULTIARCH_ENV,$(DEB_HOST_ARCH)) \
		../configure --with-sysroot=/ $(SINGLE_CONFARGS)
	$(MAKE) -C builddir-single configure-host
	touch $@

stamps/build-single: stamps/configure-single
	$(checkdir)
	@echo BEGIN $@
	env MAKE="$(MAKE) VERSION=$(SINGLE_VERSION)" \
	  $(call SET_MULTIARCH_ENV,$(DEB_HOST_ARCH)) \
	    $(MAKE) -C builddir-single/bfd headers \
		LDFLAGS="-Wl,-z,relro"
	env MAKE="$(MAKE) VERSION=$(SINGLE_VERSION)" \
	  $(call SET_MULTIARCH_ENV,$(DEB_HOST_ARCH)) \
	    $(MAKE) $(NJOBS) -C builddir-single \
		LDFLAGS="-Wl,-z,relro"
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
ifeq ($(with_check),yes)
	-env MAKE="$(MAKE) VERSION=$(SINGLE_VERSION)" \
	  $(call SET_MULTIARCH_ENV,$(DEB_HOST_ARCH)) \
	    $(MAKE) -C builddir-single -k check
	cat builddir-single/binutils/binutils.sum \
	    builddir-single/gas/testsuite/gas.sum \
	    builddir-single/ld/ld.sum >> $(pwd)/test-summary
	set -e; \
	if [ ! -f /usr/share/doc/binutils/test-summary.gz ]; then \
	  echo "No test results available for the installed binutils version"; \
	elif [ -x /usr/bin/python3 ]; then \
	  echo "Test results, compared with installed binutils:"; \
	  zcat /usr/share/doc/binutils/test-summary.gz > test-summary-installed; \
	  if python3 debian/test-suite-compare.py test-summary-installed test-summary; then \
	    : ; \
	  elif [ -n "$(ignore_regressions)" ]; then \
	    echo "$(ignore_regressions)"; \
	  else \
	    case "$(DEB_HOST_ARCH)" in \
	      powerpc) \
		echo "WARNING: ignoring test results";; \
	      *) \
		false;; \
	    esac; \
	  fi; \
	else \
	  echo "python3 not installed, not comparing test results."; \
	fi
endif
endif
	touch $@


###############################################################################

#####################
# multiarch targets #
#####################

multiarch_targets += \
	aarch64-linux-gnu \
	aarch64_be-linux-gnu \
	alpha-linux-gnu \
	hppa-linux-gnu \
	arm-linux-gnueabi \
	i686-linux-gnu \
	m32r-linux-gnu \
	m68k-linux-gnu \
	m68k-rtems \
	mips-linux-gnu \
	mipsel-linux-gnu \
	mips64-linux-gnu \
	mips64el-linux-gnu \
	powerpc-linux-gnu \
	powerpc64-linux-gnu \
	powerpc64le-linux-gnu \
	s390x-linux-gnu \
	sh-linux-gnu \
	sh64-linux-gnu \
	sparc-linux-gnu \
	sparc64-linux-gnu \
	x86_64-linux-gnu \
	x86_64-linux-gnux32 \
	x86_64-pep

# try to work around #758830
ifeq ($(DEB_HOST_ARCH),sh4)
  multiarch_targets := $(filter-out hppa-linux-gnu, $(multiarch_targets))
endif

stamps/configure-multi: stamps/patch
	$(checkdir)
	@echo BEGIN $@
	rm -rf stamps/configure-multi \
		builddir-multi
	mkdir builddir-multi
	cd builddir-multi \
	    && env CC="$(CC)" CXX="$(CXX)" CFLAGS="$(CFLAGS)" \
	        $(call SET_MULTIARCH_ENV,$(DEB_HOST_ARCH)) \
		../configure $(CONFARGS) \
		  --with-sysroot=/ \
		  --enable-targets=$(subst $(SPACE),$(COMMA),$(multiarch_targets))
	$(MAKE) -C builddir-multi configure-host
	touch $@

stamps/build-multi: stamps/configure-multi
	$(checkdir)
	@echo BEGIN $@
	$(MAKE) -C builddir-multi/bfd headers
	env MAKE="$(MAKE) VERSION=$(MULTI_VERSION)" \
	  $(call SET_MULTIARCH_ENV,$(DEB_HOST_ARCH)) \
	  $(MAKE) $(NJOBS) -C builddir-multi \
		LDFLAGS="-Wl,-z,relro"
	touch $@

###############################################################################

#################
# cross targets #
#################

CROSS_CONFARGS = $(CONFARGS) --enable-initfini-array
ifeq ($(with_gold),yes)
	CROSS_CONFARGS += --enable-ld=default --enable-gold
endif

stamps/configure.%: stamps/patch
	$(checkdir)
	@echo BEGIN $@
	rm -rf $@ builddir-$*
	mkdir builddir-$*
	cd builddir-$* \
	    && env CC="$(CC)" CXX="$(CXX)" \
	       $(call SET_MULTIARCH_ENV,$*) \
		../configure \
		  $(CROSS_CONFARGS) \
		  $(CONFARGS_TARGET_$(*)) \
		  --with-sysroot=/ \
		  $(if $(filter $*, $(gold_targets)),--enable-ld=default --enable-gold) \
		  --target=$(CROSS_GNU_TYPE)
	touch $@

stamps/build.%: stamps/configure.%
	$(checkdir)
	@echo BEGIN $@
	env MAKE="$(MAKE) VERSION=$(VERSION)-$*" \
	  $(call SET_MULTIARCH_ENV,$*) \
	  $(MAKE) -C builddir-$* $(NJOBS) \
		CFLAGS="$(CFLAGS)" \
		LDFLAGS="-Wl,-z,relro"
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
ifeq ($(with_check),yes)
	-env MAKE="$(MAKE) VERSION=$(VERSION)-$*" \
	  $(call SET_MULTIARCH_ENV,$*) \
	    $(MAKE) -C builddir-$* -k check
	cat builddir-$*/binutils/binutils.sum \
	    builddir-$*/gas/testsuite/gas.sum \
	    builddir-$*/ld/ld.sum >> test-summary-$*
endif
endif
	touch $@

stamps/install.%: stamps/build.% $(if $(CROSS),debian/strip.cross)
	$(checkdir)
	@echo BEGIN $@
	rm -rf $(D_CROSS)
	env MAKE="$(MAKE) VERSION=$(VERSION)-$*" \
	  $(MAKE) -C builddir-$* prefix=$(pwd)/$(D_CROSS)/$(PF) \
		mandir=$(pwd)/$(D_CROSS)/$(PF)/share/man install

	rm -rf \
		$(D_CROSS)/$(PF)/share/info \
		$(D_CROSS)/$(PF)/share/locale

	mkdir -p $(D_CROSS)/$(PF)/lib/$(DEB_HOST_MULTIARCH)
	set -e; \
	d_src=$(D_CROSS)/$(PF)/$(DEB_HOST_GNU_TYPE)/$(CROSS_GNU_TYPE)/lib; \
	d_dst=$(D_CROSS)/$(PF)/lib/$(DEB_HOST_MULTIARCH); \
	for lib in libbfd libopcodes; do \
	  so=$$(basename $$(echo $$d_src/$$lib*$**.so)); \
	  mv $$d_src/$$so $$d_dst/.; \
	  ln -sf ../../../lib/$(DEB_HOST_MULTIARCH)/$$so $$d_src/$$lib.so; \
	  mv $$d_src/$$lib.a $$d_dst/$${lib}-$(VERSION)-$*.a; \
	  ln -sf ../../../lib/$(DEB_HOST_MULTIARCH)/$$lib-$(VERSION)-$*.a $$d_src/$$lib.a; \
	done
	$(call strip_package, $(P_CROSS),$(D_CROSS),.)
	chmod ugo-x $(D_CROSS)/$(PF)/lib/$(DEB_HOST_MULTIARCH)/*.so

	: # Get rid of .la files since libtool obviously has no idea about transient paths
	rm -f $(D_CROSS)/$(PF)/$(DEB_HOST_GNU_TYPE)/$(CROSS_GNU_TYPE)/lib/*.la

	if which strip-nondeterminism >/dev/null 2>&1; then \
	  find $(D_CROSS) -name '*.a' -print0 \
	    | xargs -0r strip-nondeterminism --type ar; \
	fi

	: # no spu toolchain
	rm -f $(D_CROSS)/$(PF)/bin/*embedspu

	: # symlink man pages
	mv $(D_CROSS)/$(PF)/share/man/man1/$(CROSS_GNU_TYPE)-ld.1 \
	  $(D_CROSS)/$(PF)/share/man/man1/$(CROSS_GNU_TYPE)-ld.bfd.1
	gzip -9n $(D_CROSS)/$(PF)/share/man/man1/*
	for i in $(D_CROSS)/$(PF)/share/man/man1/*; do \
	  b=$$(basename $$i | sed 's/$(CROSS_GNU_TYPE)-//'); \
	  ln -sf $$b $$i; \
	done
	ln -sf $(CROSS_GNU_TYPE)-ld.bfd.1.gz \
	  $(D_CROSS)/$(PF)/share/man/man1/$(CROSS_GNU_TYPE)-ld.1.gz

	: # gold man pages
	$(if $(filter $*, $(gold_targets)), \
	  ln -sf ld.gold.1.gz $(D_CROSS)/$(PF)/share/man/man1/$(CROSS_GNU_TYPE)-ld.gold.1.gz; \
	  ln -sf dwp.1.gz $(D_CROSS)/$(PF)/share/man/man1/$(CROSS_GNU_TYPE)-dwp.1.gz; \
	)

	: # Don't want /usr/<arch>-linux to exist in any package, don't ship development files
	rm -rf $(D_CROSS)/$(PF)/$(DEB_HOST_GNU_TYPE)

	rm -f $(D_CROSS)/$(PF)/lib/*.a $(D_CROSS)/$(PF)/lib/*/*.a
	rm -f $(D_CROSS)/$(PF)/$(DEB_HOST_GNU_TYPE)/$(CROSS_GNU_TYPE)/lib/*.a

	: # remove RPATH
	chrpath -k -d $$(file $(D_CROSS)/usr/bin/*|awk -F: '/ ELF/ {print $$1}')
	ln -sf $(CROSS_GNU_TYPE)-ld.bfd $(D_CROSS)/usr/bin/$(CROSS_GNU_TYPE)-ld

	: # replace hard links with soft links
	for i in $(D_CROSS)/usr/$(CROSS_GNU_TYPE)/bin/*; do \
	  ln -sf ../../bin/$(CROSS_GNU_TYPE)-$$(basename $$i) $$i; \
	done

# temporary i586-* symlinks for stretch/sid
	compat=$$(echo $(CROSS_GNU_TYPE) | sed 's/i686/i586/'); \
	case "$(CROSS_GNU_TYPE)" in i686-*) \
	  for i in \
	    addr2line ar as c++filt elfedit gprof ld ld.bfd \
	    $(if $(filter $*, $(gold_targets)),ld.gold dwp) \
	    nm objcopy objdump ranlib readelf size strings strip; \
	  do \
	    ln -sf $(CROSS_GNU_TYPE)-$$i $(D_CROSS)/$(PF)/bin/$$compat-$$i; \
	    ln -sf $(CROSS_GNU_TYPE)-$$i.1.gz $(D_CROSS)/$(PF)/share/man/man1/$$compat-$$i.1.gz; \
	  done; \
	esac
	touch $@

###############################################################################

#################
# hppa64 target #
#################

stamps/configure-hppa64: stamps/patch
	$(checkdir)
	@echo BEGIN $@
	rm -rf stamps/configure-hppa64 \
		builddir-hppa64
	mkdir builddir-hppa64
	cd builddir-hppa64 \
	    && env CC="$(CC)" CXX="$(CXX)" CFLAGS="$(CFLAGS)" ../configure \
		$(filter-out --enable-targets=%, $(CONFARGS)) \
		--target=hppa64-linux-gnu
	$(MAKE) -C builddir-hppa64 configure-host
	touch $@

stamps/build-hppa64: stamps/configure-hppa64
	$(checkdir)
	@echo BEGIN $@
	$(MAKE) -C builddir-hppa64/bfd headers
	env MAKE="$(MAKE) VERSION=$(HPPA64_VERSION)" \
	  $(MAKE) $(NJOBS) -C builddir-hppa64 \
	    LDFLAGS="-Wl,-z,relro"
	touch $@

###############################################################################

pre-build:
#ifneq (,$(filter $(DEB_HOST_ARCH), amd64))
#	@echo Build it ...
#else
#	@echo Explicitely fail the build for architecture $(DEB_HOST_ARCH)
#	false
#endif

build_stamps = stamps/build-single
ifeq ($(with_multiarch),yes)
       build_stamps += stamps/build-multi
endif
ifeq ($(with_hppa64),yes)
       build_stamps += stamps/build-hppa64
endif
ifeq ($(with_cross),yes)
       build_stamps += $(foreach ca,$(CROSS_ARCHS), stamps/build.$(ca))
endif
ifneq (,$(TARGET))
  ifneq (,$(findstring static-cross,$(DEB_BUILD_OPTIONS)))
       build_stamps = stamps/build-static-cross
  else
       build_stamps = stamps/build-cross
  endif
endif
ifeq ($(BACKPORT),true)
    build_stamps :=
    with_check := no
endif

build: pre-build stamps/build
build-arch: pre-build stamps/build
build-indep: pre-build stamps/build
stamps/build: $(build_stamps)
	touch $@

###############################################################################

##################
# install target #
##################

install_stamps =
ifeq ($(with_hppa64),yes)
       install_stamps += stamps/install-hppa64
endif
ifeq ($(with_cross),yes)
       install_stamps += $(foreach ca,$(CROSS_ARCHS), stamps/install.$(ca))
endif

ifneq (,$(TARGET))
  ifneq (,$(findstring static-cross,$(DEB_BUILD_OPTIONS)))
        install_stamps = stamps/install-static-cross
  else
        install_stamp = stamps/install-cross
  endif
else
        install_stamp = stamps/install
endif
ifeq ($(BACKPORT),true)
    install_stamps :=
endif
install: $(install_stamp)
stamps/install: checkroot stamps/build $(install_stamps) $(if $(CROSS),debian/strip.cross)
	$(checkdir)

	rm -fr $(d_bin) $(d_dev) $(d_mul) $(d_mdev) $(d_doc) $(d_src)
	$(install_dir) $(d_bin) $(d_dev) $(d_mul) $(d_mdev) $(d_doc) $(d_src)

	: # install binutils and -dev stuff
	env MAKE="$(MAKE) VERSION=$(SINGLE_VERSION)" \
	  $(MAKE) -C builddir-single \
		CFLAGS="$(CFLAGS)" prefix=$(pwd)/$(d_bin)/$(PF) \
		mandir=$(pwd)/$(d_bin)/$(PF)/share/man \
		infodir=$(pwd)/$(d_doc)/$(PF)/share/info install

ifeq ($(with_multiarch),yes)
	: # now install binutils-multiarch stuff
	env MAKE="$(MAKE) VERSION=$(MULTI_VERSION)" \
          $(MAKE) -C builddir-multi \
		CFLAGS="$(CFLAGS)" \
		prefix=$(pwd)/$(d_mul)/$(PF) \
		mandir=$(pwd)/$(d_mul)/$(PF)/share/man \
		infodir=$(pwd)/$(d_doc)/$(PF)/share/info install
endif

	: # fix bfd.h, removing the safety inclusion guard
	awk '/PR 14072/,/^#endif/ {next} {print}' $(d_bin)/$(PF)/include/bfd.h \
	  > $(d_bin)/$(PF)/include/bfd.h.new
	mv $(d_bin)/$(PF)/include/bfd.h.new $(d_bin)/$(PF)/include/bfd.h

	: # fix multilib conflicts of generated values by __WORDSIZE-based expressions
	sed -i -e '/^#include "ansidecl.h"/{p;s~^.*$$~#include <bits/wordsize.h>~;}' \
	    -e 's/^#define BFD_DEFAULT_TARGET_SIZE \(32\|64\) *$$/#define BFD_DEFAULT_TARGET_SIZE __WORDSIZE/' \
	    -e 's/^#define BFD_HOST_64BIT_LONG [01] *$$/#define BFD_HOST_64BIT_LONG (__WORDSIZE == 64)/' \
	    -e 's/^#define BFD_HOST_64_BIT \(long \)\?long *$$/#if __WORDSIZE == 32\
#define BFD_HOST_64_BIT long long\
#else\
#define BFD_HOST_64_BIT long\
#endif/' \
	    -e 's/^#define BFD_HOST_U_64_BIT unsigned \(long \)\?long *$$/#define BFD_HOST_U_64_BIT unsigned BFD_HOST_64_BIT/' \
	    $(d_bin)/$(PF)/include/bfd.h

	: # copy plugin-api.h ...
	cp -f include/plugin-api.h $(d_bin)/$(PF)/include

	: # We don't need to distribute everything in binutils and -dev
	rm -rf $(d_bin)/$(PF)/include/obstack.h
	rm -f $(d_bin)/$(PF)/man/man1/configure.1
	rm -f $(d_doc)/$(PF)/share/info/configure.* $(d_doc)/$(PF)/share/info/standards.*
	: # *sigh*, bugs.debian.org/213524
	rm -f $(d_doc)/$(PF)/share/info/dir*

ifneq (,$(filter $(DEB_HOST_ARCH),powerpc ppc64 ppc64el))
	rm -f $(d_bin)/$(PF)/bin/embedspu
endif

ifeq ($(with_multiarch),yes)
	: # Now get rid of just about everything in binutils-multiarch
	rm -rf $(d_mul)/$(PF)/man $(d_mul)/$(PF)/info $(d_mul)/$(PF)/include
	rm -rf $(d_mul)/$(PF)/share/man $(d_mul)/$(PF)/share/info $(d_mul)/$(PF)/share/locale

	: # elfedit (even with its --input-mach option)
	: # is the same for all targets.
	rm -f $(d_mul)/$(PF)/bin/elfedit

	: # c++filt does not link to libbfd for anything more than
	: # the help message, and its behavior does not vary
	: # between arches aside from the --version message.
	rm -f $(d_mul)/$(PF)/bin/c++filt

	: # As gas/README points out (search for --enable-targets),
	: # multi-arch gas is not ready yet.
	rm -f $(d_mul)/$(PF)/bin/as
	rm -f $(d_mul)/$(PF)/bin/ld
	rm -f $(d_mul)/$(PF)/bin/ld.bfd
	rm -f $(d_mul)/$(PF)/bin/ld.gold

  ifneq (,$(filter $(DEB_HOST_ARCH),powerpc ppc64 ppc64el))
	rm -f $(d_mul)/$(PF)/bin/embedspu
  endif

# FIXME: the search paths for this config isn't correctly generated
  ifeq (0,1)
	: # move $(p_mul)'s ldscripts into $(p_bin)
	for i in $(d_mul)/$(PF)/lib/ldscripts/*; do \
	  b=$$(basename $$i); \
	  if [ -f $(d_bin)/$(PF)/lib/ldscripts/$$b ]; then \
	    if diff -u $(d_bin)/$(PF)/lib/ldscripts/$$b $$i; then \
	      rm -f $$i; \
	    else \
	      echo "ERROR: files differ: $$b"; \
	    fi; \
	  else \
	    mv $$i $(d_bin)/$(PF)/lib/ldscripts/.; \
	  fi; \
	done
	: # $(p_mul)'s ldscript should be empty
	ls -l $(d_mul)/$(PF)/lib/ldscripts
	rm -rf $(d_mul)/$(PF)/lib/ldscripts
  else
	rm -rf $(d_mul)/$(PF)/lib/ldscripts
  endif
endif

ifeq ($(with_hppa64),yes)
	: # move $(p_hppa64)'s ldscripts into $(p_bin)
	for i in $(d_hppa64)/$(PF)/lib/ldscripts/*; do \
	  b=$$(basename $$i); \
	  if [ -f $(d_bin)/$(PF)/lib/ldscripts/$$b ]; then \
	    if diff -u $(d_bin)/$(PF)/lib/ldscripts/$$b $$i; then \
	      rm -f $$i; \
	    else \
	      echo "ERROR: files differ: $$b"; \
	    fi; \
	  else \
	    mv $$i $(d_bin)/$(PF)/lib/ldscripts/.; \
	  fi; \
	done
	: # $(p_hppa64)'s ldscripts should be empty
	ls -l $(d_hppa64)/$(PF)/lib/ldscripts
	rm -rf $(d_hppa64)/$(PF)/lib/ldscripts
endif

# FIXME: some of these ldscripts are built for different cross targets
# and differ ...
ifeq (0,1)
	: # move cross ldscripts into $(p_bin)
	for i in debian/binutils-*-*-*/$(PF)/lib/ldscripts/*; do \
	  b=$$(basename $$i); \
	  if [ -f $(d_bin)/$(PF)/lib/ldscripts/$$b ]; then \
	    if diff -u $(d_bin)/$(PF)/lib/ldscripts/$$b $$i; then \
	      rm -f $$i; \
	    else \
	      echo "ERROR: files differ: $$b"; \
	    fi; \
	  else \
	    echo "ERROR: $$i should already exist"; \
	    mv $$i $(d_bin)/$(PF)/lib/ldscripts/.; \
	  fi; \
	done
	: # cross's ldscripts should be empty
	ls -l debian/binutils-*-*-*/$(PF)/lib/ldscripts
	rm -rf debian/binutils-*-*-*/$(PF)/lib/ldscripts
else
	rm -rf debian/binutils-*-*-*/$(PF)/lib/ldscripts
endif

	$(install_dir) $(d_dev)/$(PF)/include
	mv $(d_bin)/$(PF)/include/* $(d_dev)/$(PF)/include/

	$(install_dir) $(d_bin)/$(PF)/lib/$(DEB_HOST_MULTIARCH) $(d_dev)/$(PF)/lib/$(DEB_HOST_MULTIARCH)
	mv $(d_bin)/$(PF)/lib/*.a $(d_bin)/$(PF)/lib/libbfd.so $(d_bin)/$(PF)/lib/libopcodes.so \
	   $(d_dev)/$(PF)/lib/$(DEB_HOST_MULTIARCH)
	mv $(d_bin)/$(PF)/lib/*.so \
	   $(d_bin)/$(PF)/lib/$(DEB_HOST_MULTIARCH)

ifeq ($(with_multiarch),yes)
	$(install_dir) $(d_mul)/$(PF)/lib/$(DEB_HOST_MULTIARCH) $(d_mdev)/$(PF)/lib/$(DEB_HOST_MULTIARCH)
	mv $(d_mul)/$(PF)/lib/libbfd.so \
	   $(d_mdev)/$(PF)/lib/$(DEB_HOST_MULTIARCH)/libbfd-multiarch.so
	mv $(d_mul)/$(PF)/lib/libopcodes.so \
	   $(d_mdev)/$(PF)/lib/$(DEB_HOST_MULTIARCH)/libopcodes-multiarch.so
	mv $(d_mul)/$(PF)/lib/*.so \
	   $(d_mul)/$(PF)/lib/$(DEB_HOST_MULTIARCH)/
	rm -f $(d_mul)/$(PF)/lib/*.la $(d_mul)/$(PF)/lib/*.a
	rm -f $(d_mul)/$(PF)/lib*/libiberty*
endif

	: # Get rid of .la files since libtool obviously has no idea about transient paths
	rm -f $(d_bin)/$(PF)/lib/*.la

	chmod ugo-x $(d_bin)/$(PF)/lib/$(DEB_HOST_MULTIARCH)/*.so
	$(call strip_package, $(p_bin), $(d_bin))
ifeq ($(with_multiarch),yes)
	chmod ugo-x $(d_mul)/$(PF)/lib/$(DEB_HOST_MULTIARCH)/*.so
	$(call strip_package, $(p_mul), $(d_mul))
endif

	if which strip-nondeterminism >/dev/null 2>&1; then \
	  find $(d_dev) -name '*.a' -print0 \
	    | xargs -0r strip-nondeterminism --type ar; \
	fi

	: # Don't want /usr/<arch>-linux to exist in any package
	rm -rf $(d_bin)/$(PF)/$(DEB_HOST_GNU_TYPE)

	: # Remove windows related manpages
	rm -f $(d_bin)/$(PF)/share/man/man1/{dlltool,nlmconv,windmc,windres}.1

ifeq ($(with_multiarch),yes)
	rm -rf $(d_mul)/$(PF)/$(DEB_HOST_GNU_TYPE)
	rm -f $(d_mul)/$(PF)/share/man/man1/{dlltool,nlmconv,windmc,windres}.1
endif

	rm -f $(d_bin)/$(PF)/bin/ld.bfd
	mv $(d_bin)/$(PF)/bin/ld $(d_bin)/$(PF)/bin/ld.bfd
	mv $(d_bin)/$(PF)/share/man/man1/ld.1 \
		$(d_bin)/$(PF)/share/man/man1/ld.bfd.1
	ln -sf ld.bfd $(d_bin)/$(PF)/bin/ld
	ln -sf $(DEB_HOST_GNU_TYPE)-ld.bfd $(d_bin)/$(PF)/bin/$(DEB_HOST_GNU_TYPE)-ld
	ln -sf ld.bfd.1.fz $(d_bin)/$(PF)/share/man/man1/ld.1.gz
	ln -sf $(DEB_HOST_GNU_TYPE)-ld.bfd.1.gz $(d_bin)/$(PF)/share/man/man1/$(DEB_HOST_GNU_TYPE)-ld.1.gz
ifeq ($(with_gold),yes)
	ln -s ld.gold $(d_bin)/$(PF)/bin/gold
  ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	if which help2man >/dev/null 2>&1; then \
	  help2man -N -n 'The GNU ELF linker' $(d_bin)/$(PF)/bin/ld.gold \
	    | sed 's,$(d_bin)/$(PF)/bin/,,g' > debian/ld.gold.1; \
	  help2man -N -n 'The DWARF packaging utility' $(d_bin)/$(PF)/bin/dwp \
	    | sed 's,$(d_bin)/$(PF)/bin/,,g' > debian/dwp.1; \
	fi
  endif
	cp debian/dwp.1 $(d_bin)/$(PF)/share/man/man1/
	cp debian/ld.gold.1 $(d_bin)/$(PF)/share/man/man1/
	ln -s ld.gold.1.gz $(d_bin)/$(PF)/share/man/man1/gold.1.gz

	: # install a symlink for the gold linker
	$(install_dir) $(d_bin)/$(PF)/lib/gold-ld
	ln -s ../../bin/ld.gold $(d_bin)/$(PF)/lib/gold-ld/ld
endif
	: # install a symlink for the bfd linker
	$(install_dir) $(d_bin)/$(PF)/lib/compat-ld
	ln -s ../../bin/ld.bfd $(d_bin)/$(PF)/lib/compat-ld/ld

	: # Remove empty directory
	rmdir $(d_bin)/$(PF)/include/

	: # Rename non-prefixed binaries to $(DEB_HOST_GNU_TYPE)- prefixed binaries
	: # and provide symlinks to the prefixed binaries.
	for i in \
	  addr2line ar as c++filt elfedit gprof ld ld.bfd \
	  $(if $(with_gold),ld.gold dwp) \
	  nm objcopy objdump ranlib readelf size strings strip; \
	do \
	  mv $(d_bin)/$(PF)/bin/$$i $(d_bin)/$(PF)/bin/$(DEB_HOST_GNU_TYPE)-$$i; \
	  ln -sf $(DEB_HOST_GNU_TYPE)-$$i $(d_bin)/$(PF)/bin/$$i; \
	  mv $(d_bin)/$(PF)/share/man/man1/$$i.1 $(d_bin)/$(PF)/share/man/man1/$(DEB_HOST_GNU_TYPE)-$$i.1; \
	  ln -sf $(DEB_HOST_GNU_TYPE)-$$i.1.gz $(d_bin)/$(PF)/share/man/man1/$$i.1.gz; \
	done

ifneq (,$(filter $(DEB_HOST_ARCH),i386 kfreebsd-i386 hurd-i386))
# temporary i586-* symlinks for stretch/sid
  ifneq (,$(TRIPLET_SYMLINK))
	for i in \
	  addr2line ar as c++filt elfedit gprof ld ld.bfd \
	  $(if $(with_gold),ld.gold dwp) \
	  nm objcopy objdump ranlib readelf size strings strip; \
	do \
	  ln -sf $(DEB_HOST_GNU_TYPE)-$$i $(d_bin)/$(PF)/bin/$(TRIPLET_SYMLINK)-$$i; \
	  ln -sf $(DEB_HOST_GNU_TYPE)-$$i.1.gz $(d_bin)/$(PF)/share/man/man1/$(TRIPLET_SYMLINK)-$$i.1.gz; \
	done
  endif
# temporary i486-* symlinks for jessie/sid
  ifeq ($(DEB_HOST_GNU_CPU),i586)
	gnutype=$(subst i586,i486,$(DEB_HOST_GNU_TYPE)); \
	for i in \
	  addr2line ar as c++filt elfedit gprof ld ld.bfd \
	  $(if $(with_gold),ld.gold dwp) \
	  nm objcopy objdump ranlib readelf size strings strip; \
	do \
	  ln -sf $$i $(d_bin)/$(PF)/bin/$$gnutype-$$i; \
	  ln -sf $$i.1.gz $(d_bin)/$(PF)/share/man/man1/$$gnutype-$$i.1.gz; \
	done
  endif
# for backports to wheezy and squeeze
  ifeq ($(DEB_HOST_GNU_CPU),i486)
	gnutype=$(subst i486,i586,$(DEB_HOST_GNU_TYPE)); \
	for i in \
	  addr2line ar as c++filt elfedit gprof ld ld.bfd \
	  $(if $(with_gold),ld.gold dwp) \
	  nm objcopy objdump ranlib readelf size strings strip; \
	do \
	  ln -sf $(DEB_HOST_GNU_TYPE)-$$i $(d_bin)/$(PF)/bin/$$gnutype-$$i; \
	  ln -sf $(DEB_HOST_GNU_TYPE)-$$i.1.gz $(d_bin)/$(PF)/share/man/man1/$$gnutype-$$i.1.gz; \
	done
  endif
endif

	touch $@

stamps/install-hppa64: checkroot stamps/build-hppa64 $(if $(CROSS),debian/strip.cross)
	$(checkdir)

	rm -fr $(d_hppa64)
	$(install_dir) $(d_hppa64)
	$(install_dir) $(d_hppa64)/$(PF)/lib/$(DEB_HOST_MULTIARCH)

	: # install binutils-hppa64 stuff
	env MAKE="$(MAKE) VERSION=$(HPPA64_VERSION)" \
          $(MAKE) -C builddir-hppa64 \
		CFLAGS="$(CFLAGS)" \
		prefix=$(pwd)/$(d_hppa64)/$(PF)/ \
		mandir=$(pwd)/$(d_hppa64)/$(PF)/share/man \
		infodir=$(pwd)/$(d_hppa64)/$(PF)/share/info install

	: # move shared libs to the standard path
	mv $(d_hppa64)/$(PF)/$(DEB_HOST_GNU_TYPE)/hppa64-linux-gnu/lib/lib*-*.so \
		$(d_hppa64)/$(PF)/lib/$(DEB_HOST_MULTIARCH)/.

	: # Now get rid of just about everything in binutils-hppa64
	rm -rf $(d_hppa64)/$(PF)/man
	rm -rf $(d_hppa64)/$(PF)/info
	rm -rf $(d_hppa64)/$(PF)/include
	rm -rf $(d_hppa64)/$(PF)/share
	rm -rf $(d_hppa64)/$(PF)/hppa-linux-gnu
	rm -rf $(d_hppa64)/$(PF)/lib/libiberty.a

	$(call strip_package, $(p_hppa64), $(d_hppa64))
	chmod ugo-x $(d_hppa64)/$(PF)/lib/$(DEB_HOST_MULTIARCH)/*.so

	: # remove RPATH
	chrpath -k -d $$(file $(d_hppa64)/usr/bin/*|awk -F: '/ ELF/ {print $$1}')

	: # replace hard links with soft links
	for i in $(d_hppa64)/usr/hppa64-linux-gnu/bin/*; do \
	  ln -sf ../../bin/hppa64-linux-gnu-$$(basename $$i) $$i; \
	done

	: # Don't want /usr/<arch>-linux to exist in any package
	rm -rf $(d_hppa64)/$(PF)/$(DEB_HOST_GNU_TYPE)

	touch $@

###############################################################################

#######################
# binary-indep target #
#######################

binary-indep: checkroot build install
	$(checkdir)

	rm -f debian/files debian/substvars

# Cross builds do not have documentation packages
ifeq (,$(TARGET))

	$(install_dir) $(d_doc)/DEBIAN

ifeq ($(GFDL_INVARIANT_FREE),yes)
	rm -f $(d_doc)/$(PF)/share/info/bfd.info*
	rm -f $(d_doc)/$(PF)/share/info/bfdint.info*
	rm -f $(d_doc)/$(PF)/share/info/ldint.info*
endif

ifneq ($(BACKPORT),true)
	$(install_dir) $(d_doc)/$(PF)/share/doc/$(p_doc)/
	$(install_file)	debian/changelog $(d_doc)/$(PF)/share/doc/$(p_doc)/changelog.Debian
	$(install_file)	debian/copyright $(d_doc)/$(PF)/share/doc/$(p_doc)/
	for i in bfd gas gprof ld; do \
	  ln -sf ../$(p_bin)/$$i $(d_doc)/$(PF)/share/doc/$(p_doc)/$$i; \
	done
	find $(d_doc)/$(PF)/share/doc/$(p_doc) -maxdepth 1 -type f ! -name copyright | xargs gzip -9n
	gzip -9n $(d_doc)/$(PF)/share/info/*

	dpkg-gencontrol -P$(d_doc) -p$(p_doc)
	chown -R root:root $(d_doc)
	chmod -R go=rX  $(d_doc)
	find $(d_doc) -depth -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	dpkg --build $(d_doc) ..

	$(install_dir) $(d_src)/$(PF)/share/doc/$(p_src)/
	$(install_file)	debian/changelog $(d_src)/$(PF)/share/doc/$(p_src)/changelog.Debian
	$(install_file)	debian/copyright $(d_src)/$(PF)/share/doc/$(p_src)/
	find $(d_src)/$(PF)/share/doc/$(p_src) -maxdepth 1 -type f ! -name copyright | xargs gzip -9n
endif # ifndef BACKPORT

	$(install_dir) $(d_src)/DEBIAN
	$(install_dir) $(d_src)/$(PF)/src/binutils/patches
	$(install_file) debian/patches/* $(d_src)/$(PF)/src/binutils/patches/
	cd .. && find $(source_files) -depth -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)' && \
		find $(source_files) -type f -print0 | LC_ALL=C sort -z | \
		tar --null -T - -c --xz --exclude=CVS --mode=go=rX,u+rw,a-s \
		-f $(pwd)/$(d_src)/$(PF)/src/binutils/binutils-$(VERSION).tar.xz \
		$(source_files)

	tar cf - $$(find './debian' -mindepth 1 \( \
		-path './debian/binutils*' -type d -prune -o \
		-path './debian/patches' -prune -o \
		-path './debian/tmp*' -prune -o \
		-path './debian/.bzr*' -prune -o \
		-path './debian/files' -prune -o \
		-print \) ) \
	  | tar -x -C $(d_src)/$(PF)/src/binutils -f -
	-chmod 755 $(d_src)/$(PF)/src/binutils/debian/*.{pre,post}{inst,rm}
	chmod 755 $(d_src)/$(PF)/src/binutils/debian/test-suite-compare.py

	dpkg-gencontrol -P$(d_src) -p$(p_src)
	chown -R root:root $(d_src)
	chmod -R go=rX  $(d_src)
	find $(d_src) -depth -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	dpkg --build $(d_src) ..

endif

###############################################################################

#######################
# binary-arch target  #
#######################

binary.%: stamps/install.% install
	rm -rf $(D_CROSS)/$(PF)/share/info

	rm -rf $(D_CROSS)/DEBIAN

	: # make lintian happy
	$(install_dir) $(D_CROSS)/$(PF)/share/lintian/overrides
	sed 's/@PKG@/$(P_CROSS)/' debian/binutils-cross.overrides \
	  > $(D_CROSS)/$(PF)/share/lintian/overrides/$(P_CROSS)

	: # install maintainer scrtips
	$(install_dir) $(D_CROSS)/$(PF)/share/doc/$(P_CROSS)
	$(install_file)	debian/changelog \
	  $(D_CROSS)/$(PF)/share/doc/$(P_CROSS)/changelog.Debian
	$(install_file)	debian/copyright debian/README.cross \
	  $(D_CROSS)/$(PF)/share/doc/$(P_CROSS)/
	gzip -9nf $(D_CROSS)/$(PF)/share/doc/$(P_CROSS)/changelog.Debian

ifeq ($(with_check),yes)
	: # remove user and date from test-summary for reproducible builds
	sed -i -e '/Test Run By/d' test-summary-$*
	$(install_file) test-summary-$* \
	  $(D_CROSS)/$(PF)/share/doc/$(P_CROSS)/test-summary
	gzip -9nf $(D_CROSS)/$(PF)/share/doc/$(P_CROSS)/test-summary
endif

	for pkg in bfd gas gprof ld; do \
	  ln -sf ../binutils/$$pkg \
	    $(D_CROSS)/$(PF)/share/doc/$(P_CROSS)/$$pkg; \
	done

	$(install_dir) $(D_CROSS)/DEBIAN
	if [ "$(is_rc)" = yes ]; then \
	  nver=$$(echo $(DEB_UPSTREAM) | awk -F. '{ OFS="."; NF--; $$NF=$$NF+1; print }'); \
	else \
	  nver=$$(echo $(DEB_UPSTREAM) | awk -F. '{ OFS="."; $$NF=$$NF+1; print }'); \
	fi; \
	date_ext=$(DATE_EXT); \
	[ "$(is_rc)" = yes ] && date_ext=; \
	sed -e 's/@VER@/$(VERSION)/g' \
		-e 's/@DEB_VER@/$(DEB_VERSION)/g' \
		-e 's/@DEB_UVER@/$(DEB_UPSTREAM)/g' \
		-e "s/@DEB_NVER@/$$nver/g" \
		-e "s/@DATE_EXT@/$$date_ext/g" \
		-e 's/@ARCH@/$*/g' \
		-e "s/@PKG@/$(P_CROSS)/g" \
	    debian/binutils-cross.shlibs.in > $(D_CROSS)/DEBIAN/shlibs
	$(install_script) debian/binutils.postinst $(D_CROSS)/DEBIAN/postinst
	$(install_script) debian/binutils.postrm $(D_CROSS)/DEBIAN/postrm

	rm -f debian/substvars
	dpkg-shlibdeps $(D_CROSS)/$(PF)/bin/*
	dpkg-gencontrol -P$(D_CROSS) -p$(P_CROSS) \
		-VBuilt-Using="$(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W binutils-source)"
	chown -R root:root $(D_CROSS)
	chmod -R go=rX  $(D_CROSS)
	find $(D_CROSS) -depth -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	dpkg --build $(D_CROSS) ..

binary-arch: checkroot build install \
    $(if $(filter yes, $(with_cross)),$(foreach ca,$(CROSS_ARCHS), binary.$(ca)))
	$(checkdir)

ifneq ($(BACKPORT),true)
# Process the following only if $(TARGET) is set
ifneq (,$(TARGET))
	test "" != "$(TARGET)"

	rm -rf $(d_cross)/$(PF)/share/info $(d_cross)/$(PF)/share/man

	$(install_dir) $(d_cross)/DEBIAN

	$(install_dir) $(d_cross)/$(PF)/share/doc/$(p_cross)/
	$(install_file)	debian/changelog $(d_cross)/$(PF)/share/doc/$(p_cross)/changelog.Debian
	$(install_file)	debian/copyright debian/README.cross $(d_cross)/$(PF)/share/doc/$(p_cross)/
	gzip -9nf $(d_cross)/$(PF)/share/doc/$(p_cross)/changelog.Debian

	for pkg in bfd gas gprof ld; do \
	  ln -sf ../binutils/$$pkg $(d_cross)/$(PF)/share/doc/$(p_cross)/$$pkg; \
	done

	rm -f debian/substvars
	dpkg-shlibdeps $(d_cross)/$(PF)/bin/*
	dpkg-gencontrol -P$(d_cross) -p$(p_cross) \
		-VBuilt-Using="$(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W binutils-source)"
	find $(d_cross) -depth -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	dpkg --build $(d_cross) ..

else
	: # generate some control & helper files
	if [ "$(is_rc)" = yes ]; then \
	  nver=$$(echo $(DEB_UPSTREAM) | awk -F. '{ OFS="."; NF--; $$NF=$$NF+1; print }'); \
	else \
	  nver=$$(echo $(DEB_UPSTREAM) | awk -F. '{ OFS="."; $$NF=$$NF+1; print }'); \
	fi; \
	for i in debian/*.in; do \
	    case "$$i" in debian/control.in|debian/control.cross.in) continue; esac; \
	    date_ext=$(DATE_EXT); \
	    case "$$i" in debian/*.shlibs.in) [ "$(is_rc)" = yes ] && date_ext=; esac; \
	    sed -e 's/@VER@/$(VERSION)/g' \
		-e 's/@DEB_VER@/$(DEB_VERSION)/g' \
		-e 's/@DEB_SVER@/$(DEB_SVERSION)/g' \
		-e 's/@DEB_UVER@/$(DEB_UPSTREAM)/g' \
		-e "s/@DEB_NVER@/$$nver/g" \
		-e "s/@DATE_EXT@/$$date_ext/g" \
		-e 's/@DEB_ARCH@/$(DEB_HOST_ARCH)/g' \
		-e "s/@dpkg_dev@/$(DPKG_DEV)/" \
		$$i > $${i%*.in}; \
	    case "$$i" in *.post*|*.pre*) chmod 755 $${i%*.in}; esac; \
	done

	: # install bug reporting information
	$(install_file) -D debian/$(p_bin).presubj \
		$(d_bin)/$(PF)/share/bug/$(p_bin)/presubj
ifeq ($(with_multiarch),yes)
	$(install_dir) $(d_mul)/$(PF)/share/bug
	ln -sf $(p_bin) $(d_mul)/$(PF)/share/bug/$(p_mul)
endif
ifeq ($(with_hppa64),yes)
	$(install_dir) $(d_hppa64)/$(PF)/share/bug
	ln -sf $(p_bin) $(d_hppa64)/$(PF)/share/bug/$(p_hppa64)
endif

	: # make lintian happy
	$(install_file) -D debian/$(p_bin).overrides \
		$(d_bin)/$(PF)/share/lintian/overrides/$(p_bin)
ifeq ($(with_multiarch),yes)
	$(install_file) -D debian/$(p_mul).overrides \
		$(d_mul)/$(PF)/share/lintian/overrides/$(p_mul)
	$(install_file) -D debian/$(p_mdev).overrides \
		$(d_mdev)/$(PF)/share/lintian/overrides/$(p_mdev)
endif
ifeq ($(with_hppa64),yes)
	$(install_file) -D debian/$(p_hppa64).overrides \
		$(d_hppa64)/$(PF)/share/lintian/overrides/$(p_hppa64)
endif

	: # install maintainer scrtips
	$(install_dir) $(d_bin)/DEBIAN
	$(install_script) debian/binutils.postinst $(d_bin)/DEBIAN/postinst
	$(install_script) debian/binutils.postrm $(d_bin)/DEBIAN/postrm
	$(install_file) debian/binutils.shlibs $(d_bin)/DEBIAN/shlibs

	$(install_dir) $(d_dev)/DEBIAN

ifeq ($(with_multiarch),yes)
	$(install_dir) $(d_mul)/DEBIAN
	$(install_script) debian/binutils-multiarch.preinst $(d_mul)/DEBIAN/preinst
	$(install_script) debian/binutils-multiarch.postinst $(d_mul)/DEBIAN/postinst
	$(install_script) debian/binutils-multiarch.prerm $(d_mul)/DEBIAN/prerm
	$(install_script) debian/binutils-multiarch.postrm $(d_mul)/DEBIAN/postrm
	$(install_file) debian/binutils-multiarch.shlibs $(d_mul)/DEBIAN/shlibs
endif

ifeq ($(with_hppa64),yes)
	$(install_dir) $(d_hppa64)/DEBIAN
	$(install_script) debian/$(p_hppa64).postinst $(d_hppa64)/DEBIAN/postinst
	$(install_script) debian/$(p_hppa64).postrm $(d_hppa64)/DEBIAN/postrm
	$(install_file) debian/$(p_hppa64).shlibs $(d_hppa64)/DEBIAN/shlibs
endif

	: # install docs
	$(install_dir) $(d_bin)/$(PF)/share/doc/$(p_bin)/
	$(install_file)	debian/changelog $(d_bin)/$(PF)/share/doc/$(p_bin)/changelog.Debian
	$(install_file)	debian/copyright $(d_bin)/$(PF)/share/doc/$(p_bin)/

	$(install_dir) $(d_dev)/$(PF)/share/doc/
	ln -sf $(p_bin) $(d_dev)/$(PF)/share/doc/$(p_dev)
ifeq ($(with_multiarch),yes)
	$(install_dir) $(d_mul)/$(PF)/share/doc/
	ln -sf $(p_bin) $(d_mul)/$(PF)/share/doc/$(p_mul)
	$(install_dir) $(d_mdev)/$(PF)/share/doc/
	ln -sf $(p_mul) $(d_mdev)/$(PF)/share/doc/$(p_mdev)
endif
ifeq ($(with_hppa64),yes)
	$(install_dir) $(d_hppa64)/$(PF)/share/doc/
	ln -sf $(p_bin) $(d_hppa64)/$(PF)/share/doc/$(p_hppa64)
endif

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
ifeq ($(with_check),yes)
	: # remove user and date from test-summary for reproducible builds
	sed -i -e '/Test Run By/d' $(pwd)/test-summary
	$(install_file) $(pwd)/test-summary $(d_bin)/$(PF)/share/doc/$(p_bin)/
endif
endif
	$(install_file) binutils/NEWS debian/README.cross \
                        $(d_bin)/$(PF)/share/doc/$(p_bin)/

	$(install_file) binutils/ChangeLog $(d_bin)/$(PF)/share/doc/$(p_bin)/changelog

	for pkg in bfd gas gprof ld; do \
	  $(install_dir) $(d_bin)/$(PF)/share/doc/$(p_bin)/$$pkg; \
	done
	$(install_file) bfd/ChangeLog bfd/PORTING bfd/TODO \
			$(d_bin)/$(PF)/share/doc/$(p_bin)/bfd/
	$(install_file) gas/ChangeLog gas/NEWS $(d_bin)/$(PF)/share/doc/$(p_bin)/gas/
	$(install_file) gprof/ChangeLog gprof/TODO gprof/TEST \
			$(d_bin)/$(PF)/share/doc/$(p_bin)/gprof/
	$(install_file) ld/ChangeLog ld/TODO ld/NEWS \
			$(d_bin)/$(PF)/share/doc/$(p_bin)/ld/

	: # These only exist in H. J. Lu releases not GNU ones.
	for dir in binutils bfd gas gprof ld; do \
	  if [ -f $$dir/ChangeLog.linux ]; then \
	    $(install_file) $$dir/ChangeLog.linux $(d_bin)/$(PF)/share/doc/$(p_bin)/$$dir/; \
	  fi; \
	done

	: # Copy bbconv.pl to the doc dir for use by interested people
	$(install_file) gprof/bbconv.pl $(d_bin)/$(PF)/share/doc/$(p_bin)/gprof/.

	: # Compress stuff that needs it
	gzip -9n $(d_bin)/$(PF)/share/man/man1/*.1
	find $(d_bin)/$(PF)/share/doc/$(p_bin)/ -type f ! -name copyright -a ! -name bbconv.pl | xargs gzip -9n

	: # Finish it all up
	find $(d_bin) -type f | xargs file | grep ELF | cut -d: -f 1 | xargs dpkg-shlibdeps
	dpkg-gencontrol -P$(d_bin) -p$(p_bin) $(CONFLICTS_TARGET_$(DEB_HOST_ARCH)) $(gold_provides)
	cd $(d_bin) && find -type f  ! -regex './DEBIAN/.*' -printf '%P\n' | LC_ALL=C sort | xargs md5sum > DEBIAN/md5sums

	rm -f debian/substvars
	dpkg-gencontrol -P$(d_dev) -p$(p_dev)
	cd $(d_dev) && find -type f  ! -regex './DEBIAN/.*' -printf '%P\n' | LC_ALL=C sort | xargs md5sum > DEBIAN/md5sums

ifeq ($(with_multiarch),yes)
	rm -f debian/substvars
	find $(d_mul) -type f | xargs file | grep ELF | cut -d: -f 1 | xargs dpkg-shlibdeps
	dpkg-gencontrol -P$(d_mul) -p$(p_mul)
	cd $(d_mul) && find -type f  ! -regex './DEBIAN/.*' -printf '%P\n' | LC_ALL=C sort | xargs md5sum > DEBIAN/md5sums

	rm -f debian/substvars
	mkdir -p $(d_mdev)/DEBIAN
	dpkg-gencontrol -P$(d_mdev) -p$(p_mdev)
	cd $(d_mdev) && find -type f  ! -regex './DEBIAN/.*' -printf '%P\n' | LC_ALL=C sort | xargs -r md5sum > DEBIAN/md5sums
endif

ifeq ($(with_hppa64),yes)
	rm -f debian/substvars
	find $(d_hppa64) -type f | xargs file | grep ELF | cut -d: -f 1 | xargs dpkg-shlibdeps
	dpkg-gencontrol -P$(d_hppa64) -p$(p_hppa64)
	cd $(d_hppa64) && find -type f  ! -regex './DEBIAN/.*' -printf '%P\n' | LC_ALL=C sort | xargs md5sum > DEBIAN/md5sums
endif

	chown -R root:root $(d_bin) $(d_dev)
	chmod -R go=rX  $(d_bin) $(d_dev)
	find $(d_bin) -depth -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	dpkg --build $(d_bin) ..
	find $(d_dev) -depth -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	dpkg --build $(d_dev) ..
ifeq ($(with_multiarch),yes)
	chown -R root:root $(d_mul)
	chmod -R go=rX  $(d_mul)
	find $(d_mul) -depth -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	dpkg --build $(d_mul) ..

	chown -R root:root $(d_mdev)
	chmod -R go=rX  $(d_mdev)
	find $(d_mdev) -depth -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	dpkg --build $(d_mdev) ..
endif
ifeq ($(with_hppa64),yes)
	chown -R root:root $(d_hppa64)
	chmod -R go=rX  $(d_hppa64)
	find $(d_hppa64) -depth -newermt '$(BUILD_DATE)' -print0 | \
		xargs -0r touch --no-dereference --date='$(BUILD_DATE)'
	dpkg --build $(d_hppa64) ..
endif

endif # Process the following only if $(TARGET) is set
endif # ifndef BACKPORT
###############################################################################

#################
# cross targets #
#################

# Process the following only if $(TARGET) is set
ifneq (,$(TARGET))

p_cross = $(subst _,-,binutils-$(TARGET))
d_cross = debian/$(p_cross)

#-----------------------------------------------------------------
# sysroot options
ifdef WITH_SYSROOT
  with_sysroot = $(WITH_SYSROOT)
endif
ifdef WITH_BUILD_SYSROOT
  with_build_sysroot = $(WITH_BUILD_SYSROOT)
endif

ifneq ($(with_sysroot),)
  CONFARGS += --with-sysroot=$(with_sysroot)
endif
ifneq ($(with_build_sysroot),)
  CONFARGS += --with-build-sysroot=$(with_build_sysroot)
endif
ifeq ($(with_gold),yes)
  CONFARGS += --enable-ld=default --enable-gold
endif

stamps/configure-cross: stamps/patch
	$(checkdir)
	test "" != "$(TARGET)"
	rm -rf stamps/configure-cross builddir-$(TARGET)
	mkdir builddir-$(TARGET)
	cd builddir-$(TARGET) \
	    && env CC="$(CC)" CXX="$(CXX)" ../configure \
		$(CONFARGS) \
		--target=$(TARGET)
	touch $@

stamps/build-cross: stamps/configure-cross
	$(checkdir)
	test "" != "$(TARGET)"
	env MAKE="$(MAKE) VERSION=$(VERSION)-$(DEB_TARGET_ARCH)" \
	  $(MAKE) -C builddir-$(TARGET) $(NJOBS) CFLAGS="$(CFLAGS)"
	touch $@

stamps/install-cross: stamps/build-cross
	$(checkdir)
	test "" != "$(TARGET)"
	rm -rf $(d_cross)
	env MAKE="$(MAKE) VERSION=$(VERSION)-$(DEB_TARGET_ARCH)" \
	  $(MAKE) -C builddir-$(TARGET) prefix=$(pwd)/$(d_cross)/$(PF) \
		mandir=$(pwd)/$(d_cross)/$(PF)/share/man install
	rm -rf $(d_cross)/$(PF)/lib* $(d_cross)/$(PF)/info $(d_cross)/$(PF)/share/locale

	mkdir -p $(d_cross)/$(PF)/lib/$(DEB_HOST_MULTIARCH)
	mv $(d_cross)/$(PF)/$(DEB_HOST_GNU_TYPE)/$(TARGET)/lib/*.so \
	 	$(d_cross)/$(PF)/lib/$(DEB_HOST_MULTIARCH)
	$(call strip_package, $(p_cross),$(d_cross))
	chmod ugo-x $(d_cross)/$(PF)/lib/$(DEB_HOST_MULTIARCH)/*.so

	: # Get rid of .la files since libtool obviously has no idea about transient paths
	rm -f $(d_cross)/$(PF)/$(DEB_HOST_GNU_TYPE)/$(TARGET)/lib/*.la

	: # don't ship header files and static libs
	rm -rf $(d_cross)/$(PF)/$(DEB_HOST_GNU_TYPE)/$(TARGET)

	gzip -9n $(d_cross)/$(PF)/share/man/man1/*
	touch $@

stamps/configure-host-cross: stamps/configure-cross
	$(checkdir)
	test "" != "$(TARGET)"
	$(MAKE) configure-host -C builddir-$(TARGET) $(NJOBS) CFLAGS="$(CFLAGS)"
	touch $@

stamps/build-static-cross: stamps/configure-host-cross
	$(checkdir)
	test "" != "$(TARGET)"
	$(MAKE) -C builddir-$(TARGET) $(NJOBS) CFLAGS="$(CFLAGS)" LDFLAGS="-all-static"
	touch $@

stamps/install-static-cross: stamps/build-static-cross stamps/install-cross

binary-cross: 
	@echo "Please use dpkg-buildpackage instead of calling binary-cross directly; see README.cross"
	@false

endif # ifneq ($(TARGET),)

###############################################################################

define checkdir
        test -f bfd/elf32.c -a -f debian/rules
endef

ifeq ($(with_strip),yes)
# strip_package: <pkgname> <install-dir> <???>
define strip_package
	: # Strip shared libraries
	if which pkg_create_dbgsym >/dev/null 2>&1; then \
	  pkg_create_dbgsym $1 $2; \
	fi
	$(STRIP) --strip-unneeded $2/$(PF)/lib/$(DEB_HOST_MULTIARCH)/libbfd-*so
	$(STRIP) --strip-unneeded $2/$(PF)/lib/$(DEB_HOST_MULTIARCH)/libopcodes-*so
	$(STRIP) $$(file $2/$(PF)/bin/* |awk -F: '$$0 !~ /script/ {print $$1}')
endef
else
define strip_package
endef
endif

remove-gfdl-files:
ifeq ($(GFDL_INVARIANT_FREE),yes)
	for i in $(gfdl_toplevel_texinfo_files); do \
	  if [ -f $$i ]; then \
	    sed "s/@name@/$$(basename $$i)/g" debian/gfdl.texi > $$i; \
	  fi; \
	done
	rm -f $(gfdl_generated_files)
	rm -f zlib/contrib/dotzlib/DotZLib.chm
endif

# Below here is fairly generic really

binary:         binary-indep binary-arch

checkroot:
	$(checkdir)
	test root = "`whoami`"

.PHONY: binary binary-arch binary-indep clean checkroot

.PRECIOUS: stamps/patch stamps/configure.% stamps/build.% stamps/install.%
