#! /usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# version number of the defaults package
VDEF	:= $(strip $(shell dpkg-parsechangelog | awk -F: '/^Version:/ {print $$NF}'))
SOURCE	:= $(strip $(shell dpkg-parsechangelog | awk -F: '/^Source:/ {print $$NF}'))

VMAJOR	:= $(shell echo $(VDEF) | awk -F. '{print $$1}')
VMINOR	:= $(shell echo $(VDEF) | awk -F. '{print $$2}' | sed -e 's/\([0-9]*\).*/\1/')
REL_EXT	:= $(shell echo $(VDEF) | sed -e 's/^$(VMAJOR)\.$(VMINOR)//')
SPACE	:= $(EMPTY) $(EMPTY)

# gcc-defaults 0.8 is the first version for 2.95.4.
#REL_NO_295	:= $(shell expr $(VMINOR) + 15)$(REL_EXT)
# gcc-defaults 0.6 is the first version with 2.96 support.
#REL_NO_296	:= $(shell expr $(VMINOR) + 17)$(REL_EXT)
# gcc-defaults 0.17 is the first version with 3.0.4 support.
#REL_NO_30	:= $(shell expr $(VMINOR) + 6)$(REL_EXT)
# gcc-defaults 1.0 is the first version with 3.1 support.
#REL_NO_31	:= $(shell expr $(VMINOR) + 1)$(REL_EXT)
# gcc-defaults 1.2 is the first version with 3.2 support.
#REL_NO_32	:= $(shell expr $(VMINOR) - 1)$(REL_EXT)
# gcc-defaults 1.3 is the first version with 3.2.2 support.
#REL_NO_322	:= $(shell expr $(VMINOR) - 3)$(REL_EXT)
# gcc-defaults 1.4 is the first version with 3.2.3 support.
#REL_NO_323	:= $(shell expr $(VMINOR) - 4)$(REL_EXT)
# gcc-defaults 1.5 is the first version with 3.3 support.
#REL_NO_33	:= $(shell expr $(VMINOR) - 5)$(REL_EXT)
# gcc-defaults 1.7 is the first version with 3.3.1 support.
#REL_NO_331	:= $(shell expr $(VMINOR) - 7)$(REL_EXT)
# gcc-defaults 1.10 is the first version with 3.3.2 support.
#REL_NO_332	:= $(shell expr $(VMINOR) - 9)$(REL_EXT)
# gcc-defaults 1.13 is the first version with 3.3.3 support.
#REL_NO_333	:= $(shell expr $(VMINOR) - 12)$(REL_EXT)
# gcc-defaults 1.16 is the first version with 3.3.4 support.
#REL_NO_334	:= $(shell expr $(VMINOR) - 15)$(REL_EXT)
# gcc-defaults 1.19 is the first version with 3.3.5 support.
#REL_NO_335	:= $(shell expr $(VMINOR) - 18)$(REL_EXT)
# gcc-defaults 1.22 is the first version with 3.3.6 support.
#REL_NO_336     := $(shell expr $(VMINOR) - 21)$(REL_EXT)

# gcc-defaults 1.22 is the first version with 3.4.3 support.
#REL_NO_343	:= $(shell expr $(VMINOR) - 21)$(REL_EXT)
# gcc-defaults 1.23 is the first version with 3.4.4 support.
#REL_NO_344	:= $(shell expr $(VMINOR) - 23)$(REL_EXT)
# gcc-defaults 1.30 is the first version with 3.4.5 support.
#REL_NO_345	:= $(shell expr $(VMINOR) - 29)$(REL_EXT)
# gcc-defaults 1.32 is the first version with 3.4.5 support.
#REL_NO_346	:= $(shell expr $(VMINOR) - 31)$(REL_EXT)

# gcc-defaults 1.23 is the first version with 4.0.0 support.
#REL_NO_400	:= $(shell expr $(VMINOR) - 22)$(REL_EXT)
# gcc-defaults 1.26 is the first version with 4.0.1 support.
#REL_NO_401	:= $(shell expr $(VMINOR) - 25)$(REL_EXT)
# gcc-defaults 1.29 is the first version with 4.0.2 support.
#REL_NO_402	:= $(shell expr $(VMINOR) - 28)$(REL_EXT)
# gcc-defaults 1.32 is the first version with 4.0.3 support.
#REL_NO_403	:= $(shell expr $(VMINOR) - 31)$(REL_EXT)

# gcc-defaults 1.33 is the first version with 4.1.0 support.
#REL_NO_410	:= $(shell expr $(VMINOR) - 33)$(REL_EXT)
# gcc-defaults 1.36 is the first version with 4.1.1 support.
#REL_NO_411	:= $(shell expr $(VMINOR) - 35)$(REL_EXT)
# gcc-defaults 1.51 is the first version with 4.1.2 support.
#REL_NO_412	:= $(shell expr $(VMINOR) - 50)$(REL_EXT)

# gcc-defaults 1.54 is the first version with 4.2.0 support.
#REL_NO_420	:= $(shell expr $(VMINOR) - 54)$(REL_EXT)
# gcc-defaults 1.56 is the first version with 4.2.1 support.
#REL_NO_421	:= $(shell expr $(VMINOR) - 55)$(REL_EXT)
# gcc-defaults 1.62 is the first version with 4.2.2 support.
#REL_NO_422	:= $(shell expr $(VMINOR) - 61)$(REL_EXT)
# gcc-defaults 1.63 is the first version with 4.2.3 support.
#REL_NO_423	:= $(shell expr $(VMINOR) - 62)$(REL_EXT)
# gcc-defaults 1.73 is the first version with 4.2.4 support.
#REL_NO_424	:= $(shell expr $(VMINOR) - 72)$(REL_EXT)

# gcc-defaults 1.63 is the first version with 4.3 support.
#REL_NO_43	:= $(shell expr $(VMINOR) - 62)$(REL_EXT)
# gcc-defaults 1.66 is the first version with 4.3 support.
#REL_NO_430	:= $(shell expr $(VMINOR) - 65)$(REL_EXT)
# gcc-defaults 1.74 is the first version with 4.3.1 support.
#REL_NO_431	:= $(shell expr $(VMINOR) - 73)$(REL_EXT)
# gcc-defaults 1.76 is the first version with 4.3.2 support.
#REL_NO_432	:= $(shell expr $(VMINOR) - 75)$(REL_EXT)
# gcc-defaults 1.79 is the first version with 4.3.3 support.
#REL_NO_433	:= $(shell expr $(VMINOR) - 78)$(REL_EXT)
# gcc-defaults 1.88 is the first version with 4.3.4 support.
#REL_NO_434	:= $(shell expr $(VMINOR) - 87)$(REL_EXT)
# gcc-defaults 1.94 is the first version with 4.3.5 support.
#REL_NO_435	:= $(shell expr $(VMINOR) - 93)$(REL_EXT)

# gcc-defaults 1.81 is the first version with 4.4.0 support.
#REL_NO_440	:= $(shell expr $(VMINOR) - 80)$(REL_EXT)
# gcc-defaults 1.88 is the first version with 4.4.1 support.
#REL_NO_441	:= $(shell expr $(VMINOR) - 87)$(REL_EXT)
# gcc-defaults 1.90 is the first version with 4.4.2 support.
#REL_NO_442	:= $(shell expr $(VMINOR) - 89)$(REL_EXT)
# gcc-defaults 1.93 is the first version with 4.4.3 support.
#REL_NO_443	:= $(shell expr $(VMINOR) - 92)$(REL_EXT)
# gcc-defaults 1.94 is the first version with 4.4.4 support.
#REL_NO_444	:= $(shell expr $(VMINOR) - 93)$(REL_EXT)
# gcc-defaults 1.96 is the first version with 4.4.5 support.
#REL_NO_445	:= $(shell expr $(VMINOR) - 95)$(REL_EXT)
# gcc-defaults 1.106 is the first version with 4.4.6 support.
#REL_NO_446	:= $(shell expr $(VMINOR) - 105)$(REL_EXT)

# gcc-defaults 1.98 is the first version with 4.5.2 support.
#REL_NO_452	:= $(shell expr $(VMINOR) - 97)$(REL_EXT)
# gcc-defaults 1.103 is the first version with 4.5.3 support.
#REL_NO_453	:= $(shell expr $(VMINOR) - 102)$(REL_EXT)

# gcc-defaults 1.100 is the first version with 4.6.0 support.
#REL_NO_460	:= $(shell expr $(VMINOR) - 99)$(REL_EXT)
# gcc-defaults 1.106 is the first version with 4.6.1 support.
#REL_NO_461	:= $(shell expr $(VMINOR) - 105)$(REL_EXT)
# gcc-defaults 1.109 is the first version with 4.6.2 support.
#REL_NO_462	:= $(shell expr $(VMINOR) - 108)$(REL_EXT)
# gcc-defaults 1.113 is the first version with 4.6.3 support.
#REL_NO_463	:= $(shell expr $(VMINOR) - 112)$(REL_EXT)
# gcc-defaults 1.121 is the first version with 4.6.4 support.
#REL_NO_464	:= $(shell expr $(VMINOR) - 120)$(REL_EXT)

# gcc-defaults 1.113 is the first version with 4.7.0 support.
#REL_NO_470	:= $(shell expr $(VMINOR) - 112)$(REL_EXT)
# gcc-defaults 1.119 is the first version with 4.7.1 support.
#REL_NO_471	:= $(shell expr $(VMINOR) - 118)$(REL_EXT)
# gcc-defaults 1.120 is the first version with 4.7.2 support.
#REL_NO_472	:= $(shell expr $(VMINOR) - 119)$(REL_EXT)
# gcc-defaults 1.121 is the first version with 4.7.3 support.
#REL_NO_473	:= $(shell expr $(VMINOR) - 120)$(REL_EXT)

REL_NO_481	:= $(shell expr $(VMINOR) - 120)$(REL_EXT)
REL_NO_482	:= $(shell expr $(VMINOR) - 123)$(REL_EXT)
REL_NO_483	:= $(shell expr $(VMINOR) - 127)$(REL_EXT)
REL_NO_484	:= $(shell expr $(VMINOR) - 135)$(REL_EXT)

REL_NO_490	:= $(shell expr $(VMINOR) - 125)$(REL_EXT)
REL_NO_491	:= $(shell expr $(VMINOR) - 129)$(REL_EXT)
REL_NO_492	:= $(shell expr $(VMINOR) - 134)$(REL_EXT)
REL_NO_493	:= $(shell expr $(VMINOR) - 141)$(REL_EXT)

REL_NO_50	:= $(shell expr $(VMINOR) - 138)$(REL_EXT)
REL_NO_52	:= $(shell expr $(VMINOR) - 141)$(REL_EXT)
REL_NO_53	:= $(shell expr $(VMINOR) - 149)$(REL_EXT)
REL_NO_54	:= $(shell expr $(VMINOR) - 163)$(REL_EXT)

REL_NO_60	:= $(shell expr $(VMINOR) - 159)$(REL_EXT)
REL_NO_61	:= $(shell expr $(VMINOR) - 162)$(REL_EXT)
REL_NO_62	:= $(shell expr $(VMINOR) - 163)$(REL_EXT)
REL_NO_63	:= $(shell expr $(VMINOR) - 164)$(REL_EXT)
REL_NO_64	:= $(shell expr $(VMINOR) - 170)$(REL_EXT)

REL_NO_70	:= $(shell expr $(VMINOR) - 167)$(REL_EXT)
REL_NO_71	:= $(shell expr $(VMINOR) - 169)$(REL_EXT)
REL_NO_71	:= $(shell expr $(VMINOR) - 170)$(REL_EXT)
REL_NO_72	:= $(shell expr $(VMINOR) - 172)$(REL_EXT)
REL_NO_73	:= $(shell expr $(VMINOR) - 173)$(REL_EXT)

REL_NO_80	:= $(shell expr $(VMINOR) - 175)$(REL_EXT)
REL_NO_81	:= $(shell expr $(VMINOR) - 177)$(REL_EXT)
REL_NO_82	:= $(shell expr $(VMINOR) - 178)$(REL_EXT)
REL_NO_83	:= $(shell expr $(VMINOR) - 182)$(REL_EXT)

REL_NO_90	:= $(shell expr $(VMINOR) - 181)$(REL_EXT)
REL_NO_91	:= $(shell expr $(VMINOR) - 182)$(REL_EXT)
REL_NO_921	:= $(shell expr $(VMINOR) - 182)$(REL_EXT)

# minimum required gcc-X.Y versions
REQV_295	:= (>= 1:2.95.4-16)
REQV_296	:= 
REQV_30		:= (>= 1:3.0.4-12)
REQV_32		:= (>= 1:3.2.3-1)
REQV_33		:= (>= 1:3.3.6-1)
REQV_34		:= (>= 3.4.6-1)
REQV_40		:= (>= 4.0.3-1)
REQV_41		:= (>= 4.1.2-1)
REQV_42		:= (>= 4.2.4-1)
REQV_43		:= (>= 4.3.5-1)
REQV_44		:= (>= 4.4.7-1~)
REQV_45		:= (>= 4.5.3-1)
REQV_46		:= (>= 4.6.4-1~)
REQV_46_GDC	:= $(REQV_46)
REQV_47		:= (>= 4.7.3-1~)
REQV_48		:= (>= 4.8.4-1~)
REQV_49		:= (>= 4.9.3-10~)
REQV_5		:= (>= 5.4.1-2~)
REQV_6		:= (>= 6.4.0-1~)
REQV_7		:= (>= 7.3.0-11~)
REQV_8		:= (>= 8.3.0-6~)
REQV_9		:= (>= 9.2.1-1~)

# architecture dependent variables
DEB_HOST_ARCH		:= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifneq (,$(filter $(DEB_HOST_ARCH), i386 kfreebsd-i386 hurd-i386))
  DEB_HOST_GNU_TYPE := $(subst i586,i686,$(DEB_HOST_GNU_TYPE))
  i586_symlinks = yes
endif
DEB_HOST_MULTIARCH	:= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

all_archs_mips = mips mipsel mipsn32 mipsn32el mips64 mips64el mipsr6 mipsr6el mipsn32r6 mipsn32r6el mips64r6 mips64r6el

all_archs  = alpha amd64 armel armhf arm64 hppa i386 ia64 m68k or1k powerpc powerpcspe ppc64 ppc64el riscv64 s390 s390x sh4 sparc sparc64 x32 hurd-i386 kfreebsd-amd64 kfreebsd-i386 \
	     $(all_archs_mips)

gcc8_archs = powerpcspe
gcc9_archs  = alpha amd64 armel armhf arm64 hppa i386 ia64 m68k or1k powerpc ppc64 ppc64el riscv64 s390 s390x sh4 sparc sparc64 x32 hurd-i386 kfreebsd-amd64 kfreebsd-i386 \
	     $(all_archs_mips)

gnat_archs  = alpha amd64 armel armhf arm64 hppa i386 ia64 m68k or1k powerpc powerpcspe ppc64 ppc64el s390 s390x sh4 sparc sparc64 x32 hurd-i386 kfreebsd-amd64 kfreebsd-i386 \
	     $(all_archs_mips)
gnat8_archs  =

# CV_XXX is the complete version number, including the release, without epoch
CV_CPP		:= 9.2.1-$(REL_NO_921)
CV_GCC		:= 9.2.1-$(REL_NO_921)
CV_GPP		:= 9.2.1-$(REL_NO_921)
CV_GOBJC	:= 9.2.1-$(REL_NO_921)
CV_GOBJCXX	:= 9.2.1-$(REL_NO_921)
CV_GFORT	:= 9.2.1-$(REL_NO_921)
CV_GGO		:= 9.2.1-$(REL_NO_921)
CV_GNAT		:= 9.2.1-$(REL_NO_921)
CV_GDC		:= 9.2.1-$(REL_NO_921)
CV_GM2		:= 9.2.1-$(REL_NO_921)
CV_BRIG		:= 9.2.1-$(REL_NO_921)
CV_OFFL		:= 9.2.1-$(REL_NO_921)

$(foreach a, $(all_archs), \
  $(foreach v, CV_CPP CV_GCC CV_GPP CV_GOBJC CV_GOBJCXX CV_GFORT CV_GGO CV_GNAT CV_GDC CV_GM2 CV_BRIG CV_OFFL, \
    $(eval $(v)_$(a) = $(call $(v)))))

ifneq (,$(filter $(DEB_HOST_ARCH), $(gcc8_archs)))
  CV_CPP		:= 8.3.0-$(REL_NO_83)
  CV_GCC		:= 8.3.0-$(REL_NO_83)
  CV_GPP		:= 8.3.0-$(REL_NO_83)
  CV_GOBJC		:= 8.3.0-$(REL_NO_83)
  CV_GOBJCXX		:= 8.3.0-$(REL_NO_83)
  CV_GFORT		:= 8.3.0-$(REL_NO_83)
  CV_GGO		:= 8.3.0-$(REL_NO_83)
  CV_GDC		:= 8.3.0-$(REL_NO_83)
  CV_GM2		:= 8.3.0-$(REL_NO_83)
  CV_BRIG		:= 8.3.0-$(REL_NO_83)
  CV_OFFL		:= 8.3.0-$(REL_NO_83)
  $(foreach a, $(gcc8_archs), \
    $(foreach v, CV_CPP CV_GCC CV_GPP CV_GOBJC CV_GOBJCXX CV_GFORT CV_GGO CV_GDC CV_GM2 CV_BRIG CV_OFFL, \
      $(eval $(v)_$(a) = $(call $(v)))))
endif

ifneq (,$(filter $(DEB_HOST_ARCH), $(gnat8_archs)))
  CV_GNAT		:= 8.3.0-$(REL_NO_83)
  $(foreach a, $(gnat5_archs), \
    $(foreach v, CV_GNAT, \
      $(eval $(v)_$(a) = $(call $(v)))))
endif

REQV_CPP	= $(REQV_9)
REQV_GCC	= $(REQV_9)
REQV_GPP	= $(REQV_9)
REQV_GOBJC	= $(REQV_9)
REQV_GOBJCXX	= $(REQV_9)
REQV_GFORT	= $(REQV_9)
REQV_GGO	= $(REQV_9)
REQV_GNAT	= $(REQV_9)
REQV_GDC	= $(REQV_9)
REQV_GM2	= $(REQV_9)
REQV_BRIG	= $(REQV_9)
REQV_OFFL	= $(REQV_9)
$(foreach a, $(all_archs), \
  $(foreach v, REQV_CPP REQV_GCC REQV_GPP REQV_GOBJC REQV_GOBJCXX REQV_GFORT REQV_GGO REQV_GNAT REQV_GDC REQV_GM2 REQV_BRIG REQV_OFFL, \
    $(eval $(v)_$(a) = $(call $(v)))))

ifneq (,$(filter $(DEB_HOST_ARCH),$(gcc8_archs)))
  REQV_CPP	= $(REQV_8)
  REQV_GCC	= $(REQV_8)
  REQV_GPP	= $(REQV_8)
  REQV_GOBJC	= $(REQV_8)
  REQV_GOBJCXX	= $(REQV_8)
  REQV_GFORT	= $(REQV_8)
  REQV_GGO	= $(REQV_8)
  REQV_GNAT	= $(REQV_8)
  REQV_GDC	= $(REQV_8)
  REQV_GM2	= $(REQV_8)
  REQV_BRIG	= $(REQV_8)
  REQV_OFFL	= $(REQV_8)
  $(foreach a, $(gcc8_archs), \
    $(foreach v, REQV_CPP REQV_GCC REQV_GPP REQV_GOBJC REQV_GOBJCXX REQV_GFORT REQV_GGO REQV_GDC REQV_GM2 REQV_BRIG REQV_OFFL, \
      $(eval $(v)_$(a) = $(call $(v)))))
endif

ifneq (,$(filter $(DEB_HOST_ARCH),$(gnat8_archs)))
  REQV_GNAT	= $(REQV_8)
  $(foreach a, $(gnat8_archs), \
    $(foreach v, REQV_GNAT, \
      $(eval $(v)_$(a) = $(call $(v)))))
endif

EPOCH=4
OS_NAME	:= Linux
distribution := $(shell lsb_release -is)
distrelease  := $(shell lsb_release -cs)
vendor := $(shell if dpkg-vendor --derives-from Ubuntu; then echo Ubuntu; else echo Debian; fi)

lib32 = /usr/lib32

multilib_archs = $(sort \
	amd64 i386 kfreebsd-amd64 powerpc ppc64 s390 s390x sparc sparc64 x32 \
	$(all_archs_mips) \
	$(if $(filter $(vendor), Ubuntu), armel armhf))

go_archs = alpha amd64 arm64 armel armhf i386 ia64 \
	$(all_archs_mips) \
	powerpc powerpcspe ppc64 ppc64el riscv64 s390 s390x sparc sparc64 x32

go_multilib_archs = $(filter $(go_archs), $(filter-out armel armhf, $(multilib_archs)))

d_multilib_archs = $(filter-out armel, $(multilib_archs))

ada_archs = alpha amd64 arm64 armel armhf hppa i386 ia64 m68k \
	$(all_archs_mips) \
	powerpc ppc64 ppc64el riscv64 s390 s390x sh4 sparc sparc64 \
	kfreebsd-amd64 kfreebsd-i386 hurd-i386

nvptx_archs = amd64
brig_archs = amd64 i386 x32

hppa64_archs = amd64 hppa i386 x32
phobos_archs = amd64 armel armhf arm64 i386 x32 kfreebsd-amd64 kfreebsd-i386 \
	hppa riscv64 s390x \
	$(all_archs_mips)

m2_archs = alpha amd64 arm64 armel armhf i386 ia64 \
	$(all_archs_mips) \
	ppc64el riscv64 s390 s390x sparc64

HOST_ARCHS_alpha = amd64 i386 x32
HOST_ARCHS_amd64 = arm64 i386 ppc64el x32
HOST_ARCHS_armhf = amd64 i386 x32 arm64 ppc64el
HOST_ARCHS_armel = amd64 i386 x32 arm64 ppc64el
HOST_ARCHS_arm64 = amd64 i386 x32 ppc64el
HOST_ARCHS_hppa = amd64 i386 x32
HOST_ARCHS_i386 = amd64 arm64 ppc64el x32
HOST_ARCHS_m68k = amd64 i386 x32
HOST_ARCHS_mips = amd64 i386 x32
HOST_ARCHS_mipsel = amd64 i386 x32
HOST_ARCHS_mipsn32 = amd64 i386 x32
HOST_ARCHS_mipsn32el = amd64 i386 x32
HOST_ARCHS_mips64 = amd64 i386 x32
HOST_ARCHS_mips64el = amd64 i386 x32
HOST_ARCHS_mipsr6 = amd64 i386 x32
HOST_ARCHS_mipsr6el = amd64 i386 x32
HOST_ARCHS_mipsn32r6 = amd64 i386 x32
HOST_ARCHS_mipsn32r6el = amd64 i386 x32
HOST_ARCHS_mips64r6 = amd64 i386 x32
HOST_ARCHS_mips64r6el = amd64 i386 x32
HOST_ARCHS_powerpc = amd64 i386 x32 ppc64el
HOST_ARCHS_powerpcspe = amd64 i386 x32
HOST_ARCHS_ppc64 = amd64 i386 x32 ppc64el
HOST_ARCHS_ppc64el = amd64 i386 x32 ppc64 arm64
HOST_ARCHS_riscv64 = amd64 i386 x32 arm64 ppc64el
HOST_ARCHS_s390x = amd64 i386 x32 arm64 ppc64el
HOST_ARCHS_sh4 = amd64 i386 x32
HOST_ARCHS_sparc64 = amd64 i386 x32
HOST_ARCHS_x32 = amd64 arm64 i386 ppc64el

ifeq (,$(CROSS_ARCHS))
  ifeq ($(SOURCE),gcc-defaults)
    ifneq (,$(filter $(DEB_HOST_ARCH),amd64 i386 x32))
      CROSS_ARCHS  = s390x ppc64el arm64 armhf armel \
	$(if $(filter $(vendor), Ubuntu), powerpc) \
        $(if $(filter $(DEB_HOST_ARCH), i386 x32), amd64) \
        $(if $(filter $(DEB_HOST_ARCH), amd64 x32), i386)
    else ifeq ($(DEB_HOST_ARCH),arm64)
      CROSS_ARCHS = amd64 armel armhf i386 ppc64el s390x
    else ifeq ($(DEB_HOST_ARCH),ppc64)
      CROSS_ARCHS = ppc64el
    else ifeq ($(DEB_HOST_ARCH),ppc64el)
      CROSS_ARCHS = amd64 i386 $(if $(filter $(vendor), Ubuntu), powerpc) \
	armel armhf arm64 s390x
    else
      CROSS_ARCHS =
    endif
  else ifeq ($(SOURCE),gcc-defaults-mipsen)
    ifneq (,$(filter $(DEB_HOST_ARCH),amd64 i386 x32))
      CROSS_ARCHS  ?= mips mipsel mips64el mips64 \
	mipsr6 mipsr6el mips64r6 mips64r6el # mipsn32 mipsn32el mipsn32r6 mipsn32r6el
    else
      CROSS_ARCHS =
    endif
  else # -ports package
    ifneq (,$(filter $(DEB_HOST_ARCH),amd64 i386 x32))
      CROSS_ARCHS  ?= alpha hppa m68k ppc64 riscv64 sh4 sparc64 \
		$(if $(filter $(vendor), Ubuntu),, powerpc) \
		$(if $(filter $(DEB_HOST_ARCH), amd64 i386), x32)
    else ifeq ($(DEB_HOST_ARCH),arm64)
      CROSS_ARCHS = x32 riscv64
    else ifeq ($(DEB_HOST_ARCH),ppc64el)
      CROSS_ARCHS = x32 riscv64 $(if $(filter $(vendor), Ubuntu),, powerpc)
    else
      CROSS_ARCHS =
    endif
  endif
endif

ifeq ($(SOURCE),gcc-defaults)
  with_native = yes
endif
ifeq ($(SOURCE),gcc-defaults-mipsen)
  ifneq (,$(filter $(DEB_HOST_ARCH), $(all_archs_mips)))
    with_native = yes
  endif
endif
ifneq (,$(CROSS_ARCHS))
  with_cross  = yes
endif

CROSS_ARCH	= $(subst .,,$(suffix $@))
_gnu_type	= $(shell dpkg-architecture -a$(1) -qDEB_HOST_GNU_TYPE -f)
CROSS_GNU_TYPE	= $(call _gnu_type,${CROSS_ARCH})
CROSS_PKG_GNU_TYPE = $(subst _,-,$(call _gnu_type,${CROSS_ARCH}))

all_packages    :=
ifeq ($(with_native),yes)
  # FIXME: not all packages ...
  all_packages	:= cpp gcc g++ gfortran gobjc gobjc++ gdc gccgo libgphobos-dev
  ifneq (,$(filter $(DEB_HOST_ARCH), $(hppa64_archs)))
    all_packages += gcc-hppa64-linux-gnu
  endif
  ifneq (,$(filter $(DEB_HOST_ARCH), $(nvptx_archs)))
    all_packages += gcc-offload-nvptx
  endif
  ifneq (,$(filter $(DEB_HOST_ARCH), $(brig_archs)))
    all_packages += gccbrig
  endif
  ifneq (,$(filter $(DEB_HOST_ARCH), $(m2_archs)))
    all_packages += gm2
  endif
endif
no_packages	:=

# TODO:
#  - setup packages, no_packages macro

# is this a multiarch-enabled build?
ifeq (,$(filter $(distrelease),lenny etch squeeze dapper hardy jaunty karmic lucid maverick))
  with_multiarch_lib := yes
endif

ifneq (,$(filter $(DEB_HOST_ARCH), $(multilib_archs)))
  with_multilib = yes
endif
ifneq (,$(filter $(DEB_HOST_ARCH), $(go_multilib_archs)))
  with_go_multilib = yes
endif

with_gfdl_docs := no
ifeq ($(distribution),Ubuntu)
  with_gfdl_docs := yes
endif

ifeq ($(with_native),yes)
  ifeq (,$(filter $(DEB_HOST_ARCH),$(go_archs)))
    no_packages += gccgo
  endif

  gdc_no_archs = s390
  ifneq (,$(filter $(DEB_HOST_ARCH),$(gdc_no_archs)))
    no_packages += gdc
  endif

  ifeq (,$(filter $(DEB_HOST_ARCH),$(phobos_archs)))
    no_packages += libgphobos-dev
  endif

  ifeq (,$(filter $(DEB_HOST_ARCH),$(m2_archs)))
    no_packages += gm2
  endif

  ifeq ($(SOURCE),gcc-defaults)
    ifeq (,$(filter $(DEB_HOST_ARCH),$(hppa64_archs)))
      no_packages += gcc-hppa64-linux-gnu
    endif
  else
    no_packages += gcc-hppa64-linux-gnu
  endif

  ifeq (,$(filter $(DEB_HOST_ARCH),$(ada_archs)))
    no_packages += gnat gnat-sjlj
  endif

  ifneq ($(with_gfdl_docs),yes)
    no_packages := $(sort $(no_packages) cpp-doc gcc-doc gfortran-doc)
  endif

endif

ifeq ($(DEB_HOST_ARCH),hurd-i386)
    OS_NAME	:= Hurd
endif

ifneq (,$(filter $(DEB_HOST_ARCH),kfreebsd-i386 kfreebsd-amd64))
    OS_NAME	:= kFreeBSD
endif

ifeq ($(DEB_HOST_ARCH),netbsd-i386)
    OS_NAME	:= NetBSD
endif

# packages which are in the control file, but never built ...
invalid_packages = \
	gccgo-hppa-linux-gnu \
	gccgo-m68k-linux-gnu \
	gccgo-sh4-linux-gnu \
	gccgo-multilib-arm-linux-gnueabi \
	gccgo-multilib-arm-linux-gnueabihf \
	gdc-multilib-arm-linux-gnueabi \
	gm2-powerpc-linux-gnu \
	gm2-powerpc64-linux-gnu \
	gm2-sh4-linux-gnu \
	gm2-x86-64-linux-gnux32 \

ifeq ($(with_cross),yes)
  cross_packages := $(filter-out $(invalid_packages) gcc-hppa64-linux-gnu, \
			$(shell dh_listpackages -a | egrep -- '-gnu|-kfreebsd'))
  all_packages := $(all_packages) $(cross_packages)
endif
packages := $(filter-out $(no_packages), $(all_packages))

# derived version number (without release, e.g. 4.9.3, 5.2.1)
$(foreach v, V_CPP V_GCC V_GPP V_GOBJC V_GOBJCXX V_GFORT V_GGO V_GNAT V_GDC V_GM2 V_BRIG V_OFFL, \
  $(eval $(v) = $(patsubst %-$(lastword $(subst -, ,$(call C$(v)))),%,$(call C$(v)))) \
)
$(foreach a, $(all_archs), \
  $(foreach v, V_CPP V_GCC V_GPP V_GOBJC V_GOBJCXX V_GFORT V_GGO V_GNAT V_GDC V_GM2 V_BRIG V_OFFL, \
    $(eval $(v)_$(a) = $(patsubst %-$(lastword $(subst -, ,$(call C$(v)_$(a)))),%,$(call C$(v)_$(a)))) \
))

# number for the package name (e.g. 4.9, 5, 6)
$(foreach v, V_CPP V_GCC V_GPP V_GOBJC V_GOBJCXX V_GFORT V_GGO V_GNAT V_GDC V_GM2 V_BRIG V_OFFL, \
  $(eval P$(v) = $(firstword $(subst -, ,$(subst ., ,$(call $(v)))))) \
)
$(foreach a, $(all_archs), \
  $(foreach v, V_CPP V_GCC V_GPP V_GOBJC V_GOBJCXX V_GFORT V_GGO V_GNAT V_GDC V_GM2 V_BRIG V_OFFL, \
    $(eval P$(v)_$(a) = $(firstword $(subst -,  ,$(subst ., ,$(call $(v)_$(a)))))) \
))

ifneq (,$(filter $(DEB_HOST_ARCH), $(gcc49_archs)))
  $(foreach v, V_CPP V_GCC V_GPP V_GOBJC V_GOBJCXX V_GFORT V_GGO V_GDC V_GM2 V_BRIG V_OFFL, \
    $(eval P$(v) = $(subst $(SPACE),.,$(wordlist 1,2,$(subst ., ,$(call $(v)))))) \
  )
  $(foreach a, $(gcc49_archs), \
    $(foreach v, V_CPP V_GCC V_GPP V_GOBJC V_GOBJCXX V_GFORT V_GGO V_GDC V_GM2 V_BRIG V_OFFL, \
      $(eval P$(v)_$(a) = $(subst $(SPACE),.,$(wordlist 1,2,$(subst ., ,$(call $(v)_$(a)))))) \
  ))
endif

ifneq (,$(filter $(DEB_HOST_ARCH), $(gnat49_archs)))
  $(foreach v, V_GNAT, \
    $(eval P$(v) = $(subst $(SPACE),.,$(wordlist 1,2,$(subst ., ,$(call $(v)))))) \
  )
  $(foreach a, $(gnat49_archs), \
    $(foreach v, V_GNAT, \
      $(eval P$(v)_$(a) = $(subst $(SPACE),.,$(wordlist 1,2,$(subst ., ,$(call $(v)_$(a)))))) \
  ))
endif

MOD_VER := $(shell apt-cache show gfortran-$(PV_GFORT) \
		| sed -n '/^Provides/s/.*\(gfortran-mod-[0-9]*\).*/\1/p' \
		| head -1)

README:
	m4 -DPACKAGES="$(packages)" \
	   -DOS_NAME=$(OS_NAME) \
	   -DDEB_ARCH=$(DEB_HOST_ARCH) \
	   -DV_CPP=$(V_CPP)     -DPV_CPP=$(PV_CPP) \
	   -DV_GCC=$(V_GCC)     -DPV_GCC=$(PV_GCC) \
	   -DV_GPP=$(V_GPP)     -DPV_GPP=$(PV_GPP) \
	   -DV_GOBJC=$(V_GOBJC) -DPV_GOBJC=$(PV_GOBJC) \
	   -DV_GOBJCXX=$(V_GOBJCXX) -DPV_GOBJCXX=$(PV_GOBJCXX) \
	   -DV_GFORT=$(V_GFORT) -DPV_GFORT=$(PV_GFORT) \
	   -DV_GGO=$(V_GGO)     -DPV_GGO=$(PV_GGO) \
	   -DV_GNAT=$(V_GNAT)   -DPV_GNAT=$(PV_GNAT) \
	   -DV_GDC=$(V_GDC)     -DPV_GDC=$(PV_GDC) \
	   -DV_GM2=$(V_GM2)     -DPV_GM2=$(PV_GM2) \
	   -DV_BRIG=$(V_BRIG)     -DPV_BRIG=$(PV_BRIG) \
	   -DV_OFFL=$(V_OFFL)     -DPV_OFFL=$(PV_OFFL) \
	   $(if $(filter yes,$(with_gfdl_docs)), -DGFDL) \
		debian/README.Debian.m4 > debian/README.Debian.tmp2
	uniq debian/README.Debian.tmp2 > debian/README.Debian.tmp
	rm -f debian/README.Debian.tmp2
	[ -e debian/README.Debian ] \
	  && cmp -s debian/README.Debian debian/README.Debian.tmp \
	  && rm -f debian/README.Debian.tmp && exit 0; \
	  mv debian/README.Debian.tmp debian/README.Debian

substvars: substvars-native $(foreach a,$(CROSS_ARCHS),substvars.$(a))
substvars-native:
	( \
	  echo 'version:cpp=$(EPOCH):$(CV_CPP)'; \
	  echo 'version:gcc=$(EPOCH):$(CV_GCC)'; \
	  echo 'version:gpp=$(EPOCH):$(CV_GPP)'; \
	  echo 'version:gobjc=$(EPOCH):$(CV_GOBJC)'; \
	  echo 'version:gobjcxx=$(EPOCH):$(CV_GOBJCXX)'; \
	  echo 'version:gfort=$(EPOCH):$(CV_GFORT)'; \
	  echo 'version:ggo=$(EPOCH):$(CV_GGO)'; \
	  echo 'version:gnat=$(EPOCH):$(CV_GNAT)'; \
	  echo 'version:gdc=$(CV_GDC)'; \
	  echo 'version:gm2=$(CV_GM2)'; \
	  echo 'version:brig=$(CV_BRIG)'; \
	  echo 'version:offl=$(CV_OFFL)'; \
	  echo 'pv:cpp=$(PV_CPP)'; \
	  echo 'pv:gcc=$(PV_GCC)'; \
	  echo 'pv:gpp=$(PV_GPP)'; \
	  echo 'pv:gobjc=$(PV_GOBJC)'; \
	  echo 'pv:gobjcxx=$(PV_GOBJCXX)'; \
	  echo 'pv:gfort=$(PV_GFORT)'; \
	  echo 'pv:ggo=$(PV_GGO)'; \
	  echo 'pv:gnat=$(PV_GNAT)'; \
	  echo 'pv:gdc=$(PV_GDC)'; \
	  echo 'pv:gm2=$(PV_GM2)'; \
	  echo 'pv:brig=$(PV_BRIG)'; \
	  echo 'pv:offl=$(PV_OFFL)'; \
	  echo 'reqv:cpp=$(REQV_CPP)'; \
	  echo 'reqv:gcc=$(REQV_GCC)'; \
	  echo 'reqv:gpp=$(REQV_GPP)'; \
	  echo 'reqv:gobjc=$(REQV_GOBJC)'; \
	  echo 'reqv:gobjcxx=$(REQV_GOBJCXX)'; \
	  echo 'reqv:gfort=$(REQV_GFORT)'; \
	  echo 'reqv:ggo=$(REQV_GGO)'; \
	  echo 'reqv:gnat=$(REQV_GNAT)'; \
	  echo 'reqv:gdc=$(REQV_GDC)'; \
	  echo 'reqv:gm2=$(REQV_GM2)'; \
	  echo 'reqv:brig=$(REQV_BRIG)'; \
	  echo 'reqv:offl=$(REQV_OFFL)'; \
	  echo 'gcc:triplet=gcc-$(subst _,-,$(DEB_HOST_GNU_TYPE)) (= $(EPOCH):$(CV_GCC))'; \
	  echo 'gpp:triplet=g++-$(subst _,-,$(DEB_HOST_GNU_TYPE)) (= $(EPOCH):$(CV_GPP))'; \
	) > debian/substvars.native
ifeq ($(with_multilib),yes)
	( \
	  echo 'pkgmulti:gcc=gcc-multilib'; \
	  echo 'pkgmulti:gpp=g++-multilib'; \
	  echo 'pkgmulti:gobjc=gobjc-multilib'; \
	  echo 'pkgmulti:gobjcxx=gobjc++-multilib'; \
	  echo 'pkgmulti:gfort=gfortran-multilib'; \
	  echo 'pkgmulti:ggo=gccgo-multilib'; \
	  echo 'pkgmulti:gdc=gdc-multilib'; \
	) >> debian/substvars.native
endif
ifneq (,$(MOD_VER))
	echo 'fortran:mod-version=$(MOD_VER)' >> debian/substvars.native
endif
ifneq (,$(filter $(DEB_HOST_ARCH),$(phobos_archs)))
	echo 'dep:libgphobos=libgphobos-dev (= $${version:gdc})' >> debian/substvars.native
endif

substvars.%:
	( \
	  echo 'version:cpp=$(EPOCH):$(CV_CPP_$(CROSS_ARCH))'; \
	  echo 'version:gcc=$(EPOCH):$(CV_GCC_$(CROSS_ARCH))'; \
	  echo 'version:gpp=$(EPOCH):$(CV_GPP_$(CROSS_ARCH))'; \
	  echo 'version:gobjc=$(EPOCH):$(CV_GOBJC_$(CROSS_ARCH))'; \
	  echo 'version:gobjcxx=$(EPOCH):$(CV_GOBJCXX_$(CROSS_ARCH))'; \
	  echo 'version:gfort=$(EPOCH):$(CV_GFORT_$(CROSS_ARCH))'; \
	  echo 'version:ggo=$(EPOCH):$(CV_GGO_$(CROSS_ARCH))'; \
	  echo 'version:gdc=$(CV_GDC_$(CROSS_ARCH))'; \
	  echo 'version:gm2=$(CV_GM2_$(CROSS_ARCH))'; \
	  echo 'pv:cpp=$(PV_CPP_$(CROSS_ARCH))'; \
	  echo 'pv:gcc=$(PV_GCC_$(CROSS_ARCH))'; \
	  echo 'pv:gpp=$(PV_GPP_$(CROSS_ARCH))'; \
	  echo 'pv:gobjc=$(PV_GOBJC_$(CROSS_ARCH))'; \
	  echo 'pv:gobjcxx=$(PV_GOBJCXX_$(CROSS_ARCH))'; \
	  echo 'pv:gfort=$(PV_GFORT_$(CROSS_ARCH))'; \
	  echo 'pv:ggo=$(PV_GGO_$(CROSS_ARCH))'; \
	  echo 'pv:gdc=$(PV_GDC_$(CROSS_ARCH))'; \
	  echo 'pv:gm2=$(PV_GM2_$(CROSS_ARCH))'; \
	  echo 'reqv:cpp=$(REQV_CPP_$(CROSS_ARCH))'; \
	  echo 'reqv:gcc=$(REQV_GCC_$(CROSS_ARCH))'; \
	  echo 'reqv:gpp=$(REQV_GPP_$(CROSS_ARCH))'; \
	  echo 'reqv:gobjc=$(REQV_GOBJC_$(CROSS_ARCH))'; \
	  echo 'reqv:gobjcxx=$(REQV_GOBJCXX_$(CROSS_ARCH))'; \
	  echo 'reqv:gfort=$(REQV_GFORT_$(CROSS_ARCH))'; \
	  echo 'reqv:ggo=$(REQV_GGO_$(CROSS_ARCH))'; \
	  echo 'reqv:gdc=$(REQV_GDC_$(CROSS_ARCH))'; \
	  echo 'reqv:gm2=$(REQV_GM2_$(CROSS_ARCH))'; \
	) > debian/substvars.$*
	$(if $(filter $(CROSS_ARCH),$(multilib_archs)), \
	  ( \
	    echo 'pkgmulti:gcc=gcc-multilib'; \
	    echo 'pkgmulti:gpp=g++-multilib'; \
	    echo 'pkgmulti:gobjc=gobjc-multilib'; \
	    echo 'pkgmulti:gobjcxx=gobjc++-multilib'; \
	    echo 'pkgmulti:gfort=gfortran-multilib'; \
	    echo 'pkgmulti:ggo=gccgo-multilib'; \
	    echo 'pkgmulti:gdc=gdc-multilib'; \
	    echo 'pkgmulti:gm2=gm2-multilib'; \
	  ) >> debian/substvars.$*; \
	)
ifneq (,$(MOD_VER))
	echo 'fortran:mod-version=$(MOD_VER)' >> debian/substvars.$*
endif
	$(if $(filter $(CROSS_ARCH),$(phobos_archs)), \
	  echo 'dep:libgphobos=libgphobos-dev (= $${version:gdc})' >> debian/substvars.$* \
	)

control:
ifeq ($(with_native),yes)
	sed \
	  -e "s/@SOURCE@/$(SOURCE)/g" \
	  debian/control.source  > debian/control
  ifeq ($(SOURCE),gcc-defaults)
	sed \
	  -e "s/@multilib_archs@/$(multilib_archs)/g" \
	  -e "s/@go_multilib_archs@/$(go_multilib_archs)/g" \
	  -e "s/@d_multilib_archs@/$(d_multilib_archs)/g" \
	  debian/control.native.in >> debian/control
	# debian/control.host.in cantains packages that only in gcc-default
	cat debian/control.host.in >> debian/control
  endif
else
	  sed \
	    -e "s/@SOURCE@/$(SOURCE)/g" debian/control.source \
	    | grep -v '^Build-Depends-Indep:' \
	    > debian/control
endif
ifeq ($(SOURCE),gcc-defaults)
	sed \
	  -e "s/@hppa64_archs@/$(hppa64_archs)/g" \
	  debian/control.hppa64.in  >> debian/control
endif
ifeq ($(SOURCE),gcc-defaults-mipsen)
	sed \
	  -e "s/@multilib_archs@/$(all_archs_mips)/g" \
	  -e "s/@go_multilib_archs@/$(all_archs_mips)/g" \
	  -e "s/@d_multilib_archs@/$(all_archs_mips)/g" \
	  -e "s/^Architecture: any/Architecture: $(all_archs_mips)/g" \
	  debian/control.native.in >> debian/control
	sed -i 's/^Uploaders: \(.*\)/Uploaders: \1, YunQiang Su <syq@debian.org>/g' debian/control
endif
ifeq ($(with_cross),yes)
	: # FIXME: filter-out some gdc and gccgo multilib configs
	$(foreach a, $(if $(filter gcc-defaults, $(SOURCE)),amd64) $(CROSS_ARCHS), \
	  sed \
	    -e "s/CROSS_ARCH/$(a)/g" \
	    -e 's/CROSS_GNU_TYPE/$(subst _,-,$(call _gnu_type,$(a)))/g' \
	    -e 's/@host_archs@/$(HOST_ARCHS_$(a))/g' \
	    debian/control.cross.in \
	    $(if $(filter $(a),$(multilib_archs)), debian/control.cross-ml.in) \
	    $(if $(filter $(vendor),Ubuntu), debian/control.pkg-config.in) \
	    | awk '/^Package: gccgo-(hppa|m68k|sh)/ {skip=1} skip == 0 {print } /^$$/ && skip == 1 {skip=0}' \
	    >> debian/control; \
	)
endif
ifeq ($(SOURCE)-$(with_gfdl_docs),gcc-defaults-yes)
	cat debian/control.doc >> debian/control
endif

build-arch: build
build-indep: build-stamp
build: README build-stamp
build-stamp:
	dh_testdir
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp
	rm -rf build
	rm -f debian/control.tmp*
	rm -f debian/substvars.*
	rm -f debian/gcc.postinst debian/g++.postinst debian/gfortran.postinst
	dh_clean

pre-install: build substvars
	dh_testdir
	dh_testroot
	dh_prep

install: pre-install install-native $(foreach a,$(CROSS_ARCHS),install.$(a))

install-native: pre-install
ifeq ($(with_native),yes)
	for p in $(filter-out $(cross_packages), $(shell dh_listpackages)); do \
	  cp -l debian/substvars.native debian/$$p.substvars; \
	done

	: # cpp
	dh_installdirs -pcpp \
		lib usr/bin \
		usr/share/man/man1 usr/share/man/man7 usr/share/doc/cpp
	dh_link -pcpp \
	  /usr/bin/cpp-$(PV_CPP) /usr/bin/cpp \
	  /usr/bin/cpp-$(PV_CPP) /usr/bin/$(DEB_HOST_GNU_TYPE)-cpp

  ifeq ($(with_gfdl_docs),yes)
	for i in gfdl.7 gpl.7 fsf-funding.7; do \
	  cp -p $$i debian/cpp/usr/share/man/man7/$${i}gcc; \
	done
	dh_link -pcpp \
	  /usr/share/man/man1/cpp-$(PV_CPP).1.gz /usr/share/man/man1/cpp.1.gz \
	  /usr/share/man/man1/cpp-$(PV_CPP).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-cpp.1.gz

	: # cpp-doc
	dh_installdirs -pcpp-doc \
		usr/share/doc usr/share/info
	dh_link -pcpp-doc \
	  /usr/share/doc/gcc-$(PV_CPP)-base/cpp.html /usr/share/doc/cpp-doc/cpp.html \
	  /usr/share/doc/gcc-$(PV_CPP)-base/cppinternals.html /usr/share/doc/cpp-doc/cppinternals.html \
	  /usr/share/info/cpp-$(PV_CPP).info.gz /usr/share/info/cpp.info.gz \
	  /usr/share/info/cppinternals-$(PV_CPP).info.gz /usr/share/info/cppinternals.info.gz
  endif

	: # gcc
	dh_installdirs -pgcc usr/bin usr/share/man/man1 usr/share/doc
	dh_link -pgcc \
	  /usr/share/doc/cpp /usr/share/doc/gcc \
	  /usr/bin/gcc-$(PV_GCC) /usr/bin/gcc \
	  /usr/bin/gcc-$(PV_GCC) /usr/bin/$(DEB_HOST_GNU_TYPE)-gcc \
	  /usr/bin/gcov-$(PV_GCC) /usr/bin/gcov \
	  /usr/bin/gcov-$(PV_GCC) /usr/bin/$(DEB_HOST_GNU_TYPE)-gcov \
	  /usr/lib/gcc/$(DEB_HOST_GNU_TYPE)/$(PV_GCC)/liblto_plugin.so /usr/lib/bfd-plugins/liblto_plugin.so \
	  /usr/share/doc/gcc-$(PV_GCC)/README.Bugs /usr/share/doc/cpp/README.Bugs \
	  $(if $(filter $(DEB_HOST_ARCH), $(gcc49_archs)),, \
	    /usr/bin/gcov-dump-$(PV_GCC) /usr/bin/gcov-dump \
	    /usr/bin/gcov-dump-$(PV_GCC) /usr/bin/$(DEB_HOST_GNU_TYPE)-gcov-dump \
	    /usr/bin/gcov-tool-$(PV_GCC) /usr/bin/gcov-tool \
	    /usr/bin/gcov-tool-$(PV_GCC) /usr/bin/$(DEB_HOST_GNU_TYPE)-gcov-tool \
	  )
	dh_link -pgcc \
	  /usr/bin/gcc-ar-$(PV_GCC) /usr/bin/gcc-ar \
	  /usr/bin/gcc-nm-$(PV_GCC) /usr/bin/gcc-nm \
	  /usr/bin/gcc-ranlib-$(PV_GCC) /usr/bin/gcc-ranlib \
	  /usr/bin/gcc-ar-$(PV_GCC) /usr/bin/$(DEB_HOST_GNU_TYPE)-gcc-ar \
	  /usr/bin/gcc-nm-$(PV_GCC) /usr/bin/$(DEB_HOST_GNU_TYPE)-gcc-nm \
	  /usr/bin/gcc-ranlib-$(PV_GCC) /usr/bin/$(DEB_HOST_GNU_TYPE)-gcc-ranlib \
	  /usr/share/man/man1/gcc-ar-$(PV_GCC).1.gz /usr/share/man/man1/gcc-ar.1.gz \
	  /usr/share/man/man1/gcc-ar-$(PV_GCC).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gcc-ar.1.gz \
	  /usr/share/man/man1/gcc-nm-$(PV_GCC).1.gz /usr/share/man/man1/gcc-nm.1.gz \
	  /usr/share/man/man1/gcc-nm-$(PV_GCC).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gcc-nm.1.gz \
	  /usr/share/man/man1/gcc-ranlib-$(PV_GCC).1.gz /usr/share/man/man1/gcc-ranlib.1.gz \
	  /usr/share/man/man1/gcc-ranlib-$(PV_GCC).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gcc-ranlib.1.gz
	install -m 755 -p c89 debian/gcc/usr/bin/c89-gcc
	install -m 644 -p c89.1 debian/gcc/usr/share/man/man1/c89-gcc.1
	install -m 755 -p c99 debian/gcc/usr/bin/c99-gcc
	install -m 644 -p c99.1 debian/gcc/usr/share/man/man1/c99-gcc.1
	sed 's/@VER@/$(PV_GCC)/g' c99.1 > debian/gcc/usr/share/man/man1/c99-gcc.1

	sed -e 's/@GFDL@/$(if $(filter-out yes,$(with_gfdl_docs)),\#)/g' \
		debian/gcc.postinst.in > debian/gcc.postinst

  ifeq ($(with_gfdl_docs),yes)
	dh_link -pgcc \
	  /usr/share/man/man1/gcc-$(PV_GCC).1.gz /usr/share/man/man1/gcc.1.gz \
	  /usr/share/man/man1/gcc-$(PV_GCC).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gcc.1.gz \
	  /usr/share/man/man1/gcov-$(PV_GCC).1.gz /usr/share/man/man1/gcov.1.gz \
	  /usr/share/man/man1/gcov-$(PV_GCC).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gcov.1.gz \
	  $(if $(filter $(DEB_HOST_ARCH), $(gcc49_archs)),, \
	    /usr/share/man/man1/gcov-tool-$(PV_GCC).1.gz /usr/share/man/man1/gcov-tool.1.gz \
	    /usr/share/man/man1/gcov-tool-$(PV_GCC).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gcov-tool.1.gz \
	    /usr/share/man/man1/gcov-dump-$(PV_GCC).1.gz /usr/share/man/man1/gcov-dump.1.gz \
	    /usr/share/man/man1/gcov-dump-$(PV_GCC).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gcov-dump.1.gz \
	  )
	: # gcc-doc
	dh_installdirs -pgcc-doc \
		usr/share/doc usr/share/info
	dh_link -pgcc-doc \
	  /usr/share/doc/gcc-$(PV_GCC)-base/gcc.html /usr/share/doc/gcc-doc/gcc.html \
	  /usr/share/doc/gcc-$(PV_GCC)-base/gccint.html /usr/share/doc/gcc-doc/gccint.html \
	  /usr/share/doc/gcc-$(PV_GCC)-base/libgomp.html /usr/share/doc/gcc-doc/libgomp.html \
	  /usr/share/info/gcc-$(PV_GCC).info.gz /usr/share/info/gcc.info.gz \
	  /usr/share/info/gccint-$(PV_GCC).info.gz /usr/share/info/gccint.info.gz \
	  /usr/share/info/libgomp-$(PV_GCC).info.gz /usr/share/info/libgomp.info.gz 
  endif

ifeq ($(with_multiarch_lib),yes)
  ifeq ($(with_multilib),yes)
	: # gcc-multilib
	# multilib needs to provide a compat symlink pointing to the asm
	# header directory of the primary architecture, since we may not
	# have separate asm headers installed for our non-default targets.
	dh_link -pgcc-multilib \
	  /usr/include/$(DEB_HOST_MULTIARCH)/asm /usr/include/asm
  endif
endif

ifneq (,$(filter gdc, $(packages)))
	: # gdc
	dh_installdirs -pgdc
	dh_installdocs -pgdc debian/README.Debian

	dh_link -pgdc \
	  /usr/bin/gdc-$(PV_GDC) /usr/bin/gdc \
	  /usr/bin/gdc-$(PV_GDC) /usr/bin/$(DEB_HOST_GNU_TYPE)-gdc
	dh_link -pgdc \
	  /usr/share/man/man1/gdc-$(PV_GDC).1.gz /usr/share/man/man1/gdc.1.gz \
	  /usr/share/man/man1/gdc-$(PV_GDC).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gdc.1.gz

  ifneq (,$(filter libgphobos-dev, $(packages)))
	: # gdc
	dh_installdirs -plibgphobos-dev
	dh_installdocs -plibgphobos-dev
  endif
endif

ifneq (,$(filter gm2, $(packages)))
	: # gm2
	dh_installdirs -pgm2
	dh_installdocs -pgm2 debian/README.Debian

	dh_link -pgm2 \
	  /usr/bin/gm2-$(PV_GDC) /usr/bin/gm2 \
	  /usr/bin/gm2-$(PV_GDC) /usr/bin/$(DEB_HOST_GNU_TYPE)-gm2
	dh_link -pgm2 \
	  /usr/share/man/man1/gm2-$(PV_GDC).1.gz /usr/share/man/man1/gm2.1.gz \
	  /usr/share/man/man1/gm2-$(PV_GDC).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gm2.1.gz
endif

ifneq (,$(filter gobjc, $(packages)))
	: # gobjc
	dh_installdirs -pgobjc
	dh_link -pgobjc \
	  /usr/share/doc/cpp /usr/share/doc/gobjc
endif

ifneq (,$(filter gobjc++, $(packages)))
	: # gobjc++
	dh_installdirs -pgobjc++
	dh_link -pgobjc++ \
	  /usr/share/doc/cpp /usr/share/doc/gobjc++
endif

ifneq (,$(filter g++, $(packages)))
	: # g++
	dh_installdirs -pg++
	dh_link -pg++ \
	  /usr/share/doc/cpp /usr/share/doc/g++ \
	  /usr/bin/g++-$(PV_GPP) /usr/bin/g++ \
	  /usr/bin/g++-$(PV_GPP) /usr/bin/$(DEB_HOST_GNU_TYPE)-g++ \
	  $(if $(filter yes, $(with_gfdl_docs)), \
	    /usr/share/man/man1/g++-$(PV_GPP).1.gz /usr/share/man/man1/g++.1.gz \
	    /usr/share/man/man1/g++-$(PV_GPP).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-g++.1.gz \
	  )
	sed -e 's/@GFDL@/$(if $(filter-out yes,$(with_gfdl_docs)),\#)/g' \
		debian/g++.postinst.in > debian/g++.postinst
endif

ifneq (,$(filter gfortran, $(packages)))
	: # gfortran
	dh_installdirs -pgfortran usr/bin /usr/share/man/man1 usr/share/doc
	dh_link -pgfortran \
	  /usr/share/doc/cpp /usr/share/doc/gfortran \
	  /usr/bin/gfortran-$(PV_GFORT) /usr/bin/gfortran \
	  /usr/bin/gfortran-$(PV_GFORT) /usr/bin/$(DEB_HOST_GNU_TYPE)-gfortran \
	  $(if $(filter yes, $(with_gfdl_docs)), \
	    /usr/share/man/man1/gfortran-$(PV_GFORT).1.gz /usr/share/man/man1/gfortran.1.gz \
	    /usr/share/man/man1/gfortran-$(PV_GFORT).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gfortran.1.gz \
	  )

  ifeq ($(with_gfdl_docs),yes)
	: # gfortran-doc
	dh_installdirs -pgfortran-doc \
		usr/share/doc usr/share/info
	dh_link -pgfortran-doc \
	  /usr/share/doc/gcc-$(PV_GFORT)-base/fortran/gfortran.html \
	    /usr/share/doc/gfortran-doc/fortran/gfortran.html \
	  /usr/share/info/gfortran-$(PV_GFORT).info.gz /usr/share/info/gfortran.info.gz
  endif
	sed -e 's/@GFDL@/$(if $(filter-out yes,$(with_gfdl_docs)),\#)/g' \
		debian/gfortran.postinst.in > debian/gfortran.postinst
endif

ifneq (,$(filter gccgo, $(packages)))
	: # Go
	dh_installdirs -pgccgo usr/share/doc
	dh_link -pgccgo \
	  /usr/share/doc/cpp /usr/share/doc/gccgo \
	  /usr/bin/gccgo-$(PV_GGO) /usr/bin/gccgo \
	  /usr/bin/gccgo-$(PV_GGO) /usr/bin/$(DEB_HOST_GNU_TYPE)-gccgo \
	  $(if $(filter yes, $(with_gfdl_docs)), \
	    /usr/share/man/man1/gccgo-$(PV_GGO).1.gz /usr/share/man/man1/gccgo.1.gz \
	    /usr/share/man/man1/gccgo-$(PV_GGO).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gccgo.1.gz \
	  )

  ifeq ($(with_gfdl_docs),yes)
	: # gccgo-doc
	dh_installdirs -pgccgo-doc \
		usr/share/doc usr/share/info
	dh_link -pgccgo-doc \
	  /usr/share/doc/gcc-$(PV_GGO)-base/gccgo.html /usr/share/doc/gccgo-doc/gccgo.html \
	  /usr/share/info/gccgo-$(PV_GGO).info.gz /usr/share/info/gccgo.info.gz
  endif
endif

ifneq (,$(filter gccbrig, $(packages)))
	: # gccbrig
	dh_installdirs -pgccbrig
	dh_link -pgccbrig \
	  /usr/share/doc/cpp /usr/share/doc/gccbrig \
	  /usr/bin/gccbrig-$(PV_BRIG) /usr/bin/gccbrig \
	  /usr/bin/gccbrig-$(PV_BRIG) /usr/bin/$(DEB_HOST_GNU_TYPE)-gccbrig \
	  $(if $(filter yes, $(with_gfdl_docs)), \
	    /usr/share/man/man1/gccbrig-$(PV_BRIG).1.gz /usr/share/man/man1/gccbrig.1.gz \
	    /usr/share/man/man1/gccbrig-$(PV_BRIG).1.gz /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-gccbrig.1.gz \
	  )
endif

ifneq (,$(filter gcc-offload-nvptx, $(packages)))
	: # gcc-offload-nvptx
	dh_installdirs -pgcc-offload-nvptx
	dh_link -pgcc-offload-nvptx \
	  /usr/share/doc/cpp /usr/share/doc/gcc-offload-nvptx \
	  /usr/bin/$(DEB_HOST_GNU_TYPE)-accel-nvptx-none-gcc-$(PV_OFFL) \
	    /usr/bin/$(DEB_HOST_GNU_TYPE)-accel-nvptx-none-gcc \
	  $(if $(filter yes, $(with_gfdl_docs)), \
	    /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-accel-nvptx-none-$(PV_OFFL).1.gz \
	      /usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-accel-nvptx-none.1.gz \
	  )
endif

ifneq (,$(filter gcc-hppa64-linux-gnu, $(packages)))
	: # gcc-hppa64-linux-gnu
	dh_installdirs -pgcc-hppa64-linux-gnu
	dh_link -pgcc-hppa64-linux-gnu \
	  /usr/bin/hppa64-linux-gnu-gcc-$(PV_GCC) /usr/bin/hppa64-linux-gnu-gcc \
	  /usr/bin/hppa64-linux-gnu-gcc-ar-$(PV_GCC) /usr/bin/hppa64-linux-gnu-gcc-ar \
	  /usr/bin/hppa64-linux-gnu-gcc-nm-$(PV_GCC) /usr/bin/hppa64-linux-gnu-gcc-nm \
	  /usr/bin/hppa64-linux-gnu-gcc-ranlib-$(PV_GCC) /usr/bin/hppa64-linux-gnu-gcc-ranlib \
	  /usr/share/man/man1/hppa64-linux-gnu-gcc-ar-$(PV_GCC).1.gz /usr/share/man/man1/hppa64-linux-gnu-gcc-ar.1.gz \
	  /usr/share/man/man1/hppa64-linux-gnu-gcc-nm-$(PV_GCC).1.gz /usr/share/man/man1/hppa64-linux-gnu-gcc-nm.1.gz \
	  /usr/share/man/man1/hppa64-linux-gnu-gcc-ranlib-$(PV_GCC).1.gz /usr/share/man/man1/hppa64-linux-gnu-gcc-ranlib.1.gz \
	  $(if $(filter yes, $(with_gfdl_docs)), \
	    /usr/share/man/man1/hppa64-linux-gnu-gcc-$(PV_GCC).1.gz /usr/share/man/man1/hppa64-linux-gnu-gcc.1.gz \
	  )
endif

  ifeq ($(i586_symlinks),yes)
	cd debian; \
	for x in $$(find $(filter-out $(cross_packages), $(all_packages)) -type l -name 'i686-*'); do \
	  link=$$(echo $$x | sed 's/i686-/i586-/'); \
	  tgt=$$(basename $$x); \
	  echo "Adding symlink: $$link -> $$tgt"; \
	  rm -f $$link; cp -a $$x $$link; \
	done
  endif
endif # with_native

install.%: pre-install
	for p in $(filter %-$(CROSS_PKG_GNU_TYPE), $(all_packages)); do \
	  cp -l debian/substvars.$(CROSS_ARCH) debian/$$p.substvars; \
	done

	: # cpp-$(CROSS_PKG_GNU_TYPE)
	dh_installdirs -pcpp-$(CROSS_PKG_GNU_TYPE)
	dh_link -pcpp-$(CROSS_PKG_GNU_TYPE) \
	  /usr/bin/$(CROSS_GNU_TYPE)-cpp-$(PV_CPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-cpp

	$(if $(filter yes, $(with_gfdl_docs)), \
	  dh_link -pcpp-$(CROSS_PKG_GNU_TYPE) \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-cpp-$(PV_CPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-cpp.1.gz \
	)

	: # gcc-$(CROSS_PKG_GNU_TYPE)
	dh_installdirs -pgcc-$(CROSS_PKG_GNU_TYPE)
	dh_link -pgcc-$(CROSS_PKG_GNU_TYPE) \
	  /usr/share/doc/cpp-$(CROSS_PKG_GNU_TYPE) /usr/share/doc/gcc-$(CROSS_PKG_GNU_TYPE) \
	  /usr/bin/$(CROSS_GNU_TYPE)-gcc-$(PV_GCC_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gcc \
	  /usr/bin/$(CROSS_GNU_TYPE)-gcov-$(PV_GCC_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gcov \
	  /usr/share/doc/gcc-$(PV_GCC)-$(CROSS_PKG_GNU_TYPE)-base/README.Bugs /usr/share/doc/cpp-$(CROSS_PKG_GNU_TYPE)/README.Bugs \
	  $(if $(filter $(CROSS_ARCH), $(gcc49_archs)),, \
	    /usr/bin/$(CROSS_GNU_TYPE)-gcov-dump-$(PV_GCC_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gcov-dump \
	    /usr/bin/$(CROSS_GNU_TYPE)-gcov-tool-$(PV_GCC_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gcov-tool \
	  )
	dh_link -pgcc-$(CROSS_PKG_GNU_TYPE) \
	  /usr/bin/$(CROSS_GNU_TYPE)-gcc-ar-$(PV_GCC_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gcc-ar \
	  /usr/bin/$(CROSS_GNU_TYPE)-gcc-nm-$(PV_GCC_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gcc-nm \
	  /usr/bin/$(CROSS_GNU_TYPE)-gcc-ranlib-$(PV_GCC_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gcc-ranlib \
	  /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcc-ar-$(PV_GCC_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcc-ar.1.gz \
	  /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcc-nm-$(PV_GCC_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcc-nm.1.gz \
	  /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcc-ranlib-$(PV_GCC_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcc-ranlib.1.gz
	: # install -m 755 -p c89 debian/gcc/usr/bin/c89-gcc
	: # install -m 644 -p c89.1 debian/gcc/usr/share/man/man1/c89-gcc.1
	: # install -m 755 -p c99 debian/gcc/usr/bin/c99-gcc
	: # install -m 644 -p c99.1 debian/gcc/usr/share/man/man1/c99-gcc.1
	: # sed 's/@VER@/$(PV_GCC)/g' c99.1 > debian/gcc/usr/share/man/man1/c99-gcc.1

	: # sed -e 's/@GFDL@/$(if $(filter-out yes,$(with_gfdl_docs)),\#)/g' \
	: #	debian/gcc.postinst.in > debian/gcc.postinst

	$(if $(filter yes, $(with_gfdl_docs)), \
	  dh_link -pgcc-$(CROSS_PKG_GNU_TYPE) \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcc-$(PV_GCC_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcc.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcov-$(PV_GCC_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcov.1.gz \
	    $(if $(filter $(CROSS_ARCH), $(gcc49_archs)),, \
	      /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcov-tool-$(PV_GCC_$(CROSS_ARCH)).1.gz \
	      /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcov-tool.1.gz \
	      /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcov-dump-$(PV_GCC_$(CROSS_ARCH)).1.gz \
	      /usr/share/man/man1/$(CROSS_GNU_TYPE)-gcov-dump.1.gz) \
	)

	: # gdc-$(CROSS_PKG_GNU_TYPE)
	$(if $(filter gdc-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_installdirs -pgdc-$(CROSS_PKG_GNU_TYPE); \
	  dh_installdocs -pgdc-$(CROSS_PKG_GNU_TYPE) \
	    debian/README.Debian; \
	  dh_link -pgdc-$(CROSS_PKG_GNU_TYPE) \
	    /usr/bin/$(CROSS_GNU_TYPE)-gdc-$(PV_GDC_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gdc \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gdc-$(PV_GDC_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gdc.1.gz; \
	)

	: # gm2-$(CROSS_PKG_GNU_TYPE)
	$(if $(filter gm2-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_installdirs -pgm2-$(CROSS_PKG_GNU_TYPE); \
	  dh_installdocs -pgm2-$(CROSS_PKG_GNU_TYPE) \
	    debian/README.Debian; \
	  dh_link -pgm2-$(CROSS_PKG_GNU_TYPE) \
	    /usr/bin/$(CROSS_GNU_TYPE)-gm2-$(PV_GM2_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gm2 \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gm2-$(PV_GM2_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gm2.1.gz; \
	)

	: # gobjc-$(CROSS_PKG_GNU_TYPE)
	$(if $(filter gobjc-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_installdirs -pgobjc-$(CROSS_PKG_GNU_TYPE); \
	  dh_link -pgobjc-$(CROSS_PKG_GNU_TYPE) \
	    /usr/share/doc/cpp-$(CROSS_PKG_GNU_TYPE) /usr/share/doc/gobjc-$(CROSS_PKG_GNU_TYPE); \
	)

	: # gobjc++-$(CROSS_PKG_GNU_TYPE)
	$(if $(filter gobjc++-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_installdirs -pgobjc++-$(CROSS_PKG_GNU_TYPE); \
	  dh_link -pgobjc++-$(CROSS_PKG_GNU_TYPE) \
	    /usr/share/doc/cpp-$(CROSS_PKG_GNU_TYPE) /usr/share/doc/gobjc++-$(CROSS_PKG_GNU_TYPE); \
	)

	: # g++-$(CROSS_PKG_GNU_TYPE)
	$(if $(filter g++-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_installdirs -pg++-$(CROSS_PKG_GNU_TYPE); \
	  dh_link -pg++-$(CROSS_PKG_GNU_TYPE) \
	    /usr/share/doc/cpp-$(CROSS_PKG_GNU_TYPE) /usr/share/doc/g++-$(CROSS_PKG_GNU_TYPE) \
	    /usr/bin/$(CROSS_GNU_TYPE)-g++-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-g++ \
	    $(if $(filter yes, $(with_gfdl_docs)), \
	      /usr/share/man/man1/$(CROSS_GNU_TYPE)-g++-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-g++.1.gz \
	    ); \
	  : # sed -e 's/@GFDL@/$(if $(filter-out yes,$(with_gfdl_docs)),\#)/g' \
	  : #	debian/g++.postinst.in > debian/g++.postinst; \
	)

	: # gfortran-$(CROSS_PKG_GNU_TYPE)
	$(if $(filter gfortran-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_installdirs -pgfortran-$(CROSS_PKG_GNU_TYPE); \
	  dh_link -pgfortran-$(CROSS_PKG_GNU_TYPE) \
	    /usr/share/doc/cpp-$(CROSS_PKG_GNU_TYPE) /usr/share/doc/gfortran-$(CROSS_PKG_GNU_TYPE) \
	    /usr/bin/$(CROSS_GNU_TYPE)-gfortran-$(PV_GFORT_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gfortran \
	    $(if $(filter yes, $(with_gfdl_docs)), \
	      /usr/share/man/man1/$(CROSS_GNU_TYPE)-gfortran-$(PV_GFORT_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gfortran.1.gz \
	    ); \
	  : # sed -e 's/@GFDL@/$(if $(filter-out yes,$(with_gfdl_docs)),\#)/g' \
	  : #	debian/gfortran.postinst.in > debian/gfortran.postinst; \
	)

	: # gccgo-$(CROSS_PKG_GNU_TYPE)
	$(if $(filter gccgo-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_installdirs -pgccgo-$(CROSS_PKG_GNU_TYPE); \
	  dh_link -pgccgo-$(CROSS_PKG_GNU_TYPE) \
	    /usr/share/doc/cpp-$(CROSS_PKG_GNU_TYPE) /usr/share/doc/gccgo-$(CROSS_PKG_GNU_TYPE) \
	    /usr/bin/$(CROSS_GNU_TYPE)-gccgo-$(PV_GGO_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gccgo \
	    $(if $(filter yes, $(with_gfdl_docs)), \
	      /usr/share/man/man1/$(CROSS_GNU_TYPE)-gccgo-$(PV_GGO_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gccgo.1.gz \
	    ); \
	)

	: # gnat-$(CROSS_PKG_GNU_TYPE)
	$(if $(filter gnat-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_installdirs -pgnat-$(CROSS_PKG_GNU_TYPE); \
	  dh_link -pgnat-$(CROSS_PKG_GNU_TYPE) \
	    /usr/share/doc/cpp-$(CROSS_PKG_GNU_TYPE) /usr/share/doc/gnat-$(CROSS_PKG_GNU_TYPE) \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnat-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnat \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnatbind-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnatbind \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnatchop-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnatchop \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnatclean-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnatclean \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnatfind-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnatfind \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnatkr-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnatkr \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnatlink-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnatlink \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnatls-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnatls \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnatmake-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnatmake \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnatprep-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnatprep \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnatxref-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnatxref \
	    /usr/bin/$(CROSS_GNU_TYPE)-gnathtml-$(PV_GPP_$(CROSS_ARCH)) /usr/bin/$(CROSS_GNU_TYPE)-gnathtml \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnat-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnat.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatbind-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatbind.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatchop-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatchop.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatclean-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatclean.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatfind-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatfind.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatkr-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatkr.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatlink-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatlink.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatls-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatls.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatmake-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatmake.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatprep-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatprep.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatxref-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnatxref.1.gz \
	    /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnathtml-$(PV_GPP_$(CROSS_ARCH)).1.gz /usr/share/man/man1/$(CROSS_GNU_TYPE)-gnathtml.1.gz; \
	  dh_installdirs -pgnat-sjlj-$(CROSS_PKG_GNU_TYPE); \
	  dh_link -pgnat-sjlj-$(CROSS_PKG_GNU_TYPE) \
	    /usr/share/doc/cpp-$(CROSS_GNU_TYPE) /usr/share/doc/gnat-sjlj-$(CROSS_GNU_TYPE); \
	)

nopkgs_native := $(foreach p, $(filter $(shell dh_listpackages -a), $(sort $(invalid_packages) $(no_packages) $(cross_packages))),-N$(p))

binary-indep: build-indep install
ifeq ($(with_native),yes)
	dh_testdir
	dh_testroot
	dh_installchangelogs -i
	dh_installdocs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i
endif

binary-arch: binary-native $(foreach a,$(CROSS_ARCHS),binary.$(a))
binary-native: build-arch install
ifeq ($(with_native),yes)
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs -pcpp
	for p in `dh_listpackages $(nopkgs_native) -Ncpp $(if $(filter yes,$(with_gfdl_docs)),-Ncpp-doc -Ngcc-doc -Ngfortran-doc -Ngccgo-doc) -Ngdc -Nlibgphobos-dev`; do \
	  case "$$p" in \
	    *-gnu*|*-kfreebsd*|gcc-hppa64-linux-gnu) continue;; \
	    *) t=cpp; \
	  esac; \
	  echo ln -sf $$t debian/$$p/usr/share/doc/$$p; \
	  mkdir -p debian/$$p/usr/share/doc; \
	  rm -rf debian/$$p/usr/share/doc/$$p; \
	  ln -sf $$t debian/$$p/usr/share/doc/$$p; \
	done

	dh_installchangelogs -pcpp -pgdc -plibgphobos-dev
	for i in cpp gdc libgphobos-dev; do \
	  mv debian/$$i/usr/share/doc/$$i/changelog \
	    debian/$$i/usr/share/doc/$$i/changelog.Debian; \
	done
ifeq ($(with_gfdl_docs),yes)
	dh_installdocs -pcpp-doc -pgcc-doc -pgfortran-doc -pgccgo-doc
	dh_installchangelogs -pcpp-doc -pgcc-doc -pgfortran-doc -pgccgo-doc
	for i in cpp-doc gcc-doc gfortran-doc gccgo-doc; do \
	  mv debian/$$i/usr/share/doc/$$i/changelog \
	    debian/$$i/usr/share/doc/$$i/changelog.Debian; \
	done
endif
ifneq (,$(filter gcc-hppa64-linux-gnu, $(packages)))
	dh_installdocs -pgcc-hppa64-linux-gnu
	dh_installchangelogs -pgcc-hppa64-linux-gnu
	mv debian/gcc-hppa64-linux-gnu/usr/share/doc/gcc-hppa64-linux-gnu/changelog \
	  debian/gcc-hppa64-linux-gnu/usr/share/doc/gcc-hppa64-linux-gnu/changelog.Debian
endif

	dh_compress -a $(nopkgs_native) -XREADME.Debian

ifneq (,$(filter $(DEB_HOST_ARCH),i386 kfreebsd-i386 hurd-i386))
# temporary i486-* symlinks for jessie/sid
  ifeq ($(DEB_HOST_GNU_CPU)),i586)
	gnutype=$(subst i586,i486,$(DEB_HOST_GNU_TYPE)); \
	for i in $$(find debian -name 'i586-*'); do \
	  cp -a $$i $$(echo $$i | sed "s,$(DEB_HOST_GNU_TYPE),$$gnutype,"); \
	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 $$(find debian -name 'i486-*'); do \
	  cp -a $$i $$(echo $$i | sed "s,$(DEB_HOST_GNU_TYPE),$$gnutype,"); \
	done
  endif
endif

	for p in $$(dh_listpackages -a); do \
	  case $$p in \
	    cpp|g++|gcc|gccgo|gfortran|gcc-hppa64-linux-gnu) \
	      [ "$(with_gfdl_docs)" = yes ] && continue; \
	      mkdir -p -m 755 debian/$$p/usr/share/lintian/overrides; \
	      echo "$$p binary: binary-without-manpage" \
	        >> debian/$$p/usr/share/lintian/overrides/$$p;; \
	    cpp-doc|gcc-doc|gccgo-doc|gfortran-doc) \
	      mkdir -p -m 755 debian/$$p/usr/share/lintian/overrides; \
	      echo "$$p binary: documentation-package-not-architecture-independent" \
	        >> debian/$$p/usr/share/lintian/overrides/$$p;; \
	  esac; \
	done

	dh_fixperms -a $(nopkgs_native)
	dh_installdeb -a $(nopkgs_native)
	dh_gencontrol -pcpp \
	  $(if $(filter yes, $(with_gfdl_docs)),-pcpp-doc) \
	  -u-v$(EPOCH):$(CV_CPP)
	dh_gencontrol -pgcc \
	  $(if $(filter yes,$(with_multilib)),-pgcc-multilib) \
	  $(if $(filter yes, $(with_gfdl_docs)),-pgcc-doc) \
	  -u-v$(EPOCH):$(CV_GCC)
ifneq (,$(filter gdc, $(packages)))
	dh_gencontrol -pgdc $(if $(filter yes,$(with_multilib)),-pgdc-multilib) \
	  -u-v$(EPOCH):$(CV_GDC)
endif
ifneq (,$(filter libgphobos-dev, $(packages)))
	dh_gencontrol -plibgphobos-dev -u-v$(CV_GDC)
endif
ifneq (,$(filter gm2, $(packages)))
	dh_gencontrol -pgm2 \
	  -u-v$(EPOCH):$(CV_GM2)
endif
ifneq (,$(filter g++, $(packages)))
	dh_gencontrol -pg++ $(if $(filter yes,$(with_multilib)),-pg++-multilib) \
	  -u-v$(EPOCH):$(CV_GPP)
endif
ifneq (,$(filter gobjc, $(packages)))
	dh_gencontrol -pgobjc $(if $(filter yes,$(with_multilib)),-pgobjc-multilib) \
	  -u-v$(EPOCH):$(CV_GOBJC)
endif
ifneq (,$(filter gobjc++, $(packages)))
	dh_gencontrol -pgobjc++ $(if $(filter yes,$(with_multilib)),-pgobjc++-multilib) \
	  -u-v$(EPOCH):$(CV_GOBJCXX)
endif
ifneq (,$(filter gfortran, $(packages)))
	dh_gencontrol -pgfortran $(if $(filter yes,$(with_multilib)),-pgfortran-multilib) \
	  $(if $(filter yes, $(with_gfdl_docs)),-pgfortran-doc) \
	  -u-v$(EPOCH):$(CV_GFORT)
endif
ifneq (,$(filter gccgo, $(packages)))
	dh_gencontrol -pgccgo \
	  $(if $(filter yes,$(with_go_multilib)),-pgccgo-multilib) \
	  $(if $(filter yes, $(with_gfdl_docs)),-pgccgo-doc) \
	  -u-v$(EPOCH):$(CV_GGO)
endif
ifneq (,$(filter gccbrig, $(packages)))
	dh_gencontrol -pgccbrig \
	  -u-v$(EPOCH):$(CV_BRIG)
endif
ifneq (,$(filter gcc-offload-nvptx, $(packages)))
	dh_gencontrol -pgcc-offload-nvptx \
	  -u-v$(EPOCH):$(CV_OFFL)
endif
ifneq (,$(filter gcc-hppa64-linux-gnu, $(packages)))
	dh_gencontrol -pgcc-hppa64-linux-gnu \
	  -u-v$(EPOCH):$(CV_GCC)
endif
	dh_md5sums -a $(nopkgs_native)
	dh_builddeb -a $(nopkgs_native)
endif # with_native

binary.%: build-arch install
	dh_testdir
	dh_testroot
	dh_installdocs -pcpp-$(CROSS_PKG_GNU_TYPE) \
	  $(if $(filter pkg-config-$(CROSS_PKG_GNU_TYPE), $(packages)),-ppkg-config-$(CROSS_PKG_GNU_TYPE))

	for p in $(filter %-$(CROSS_PKG_GNU_TYPE),$(cross_packages)); do \
	  case "$$p" in \
	    cpp-*|pkg-config-*) continue;; \
	    *-gnu*|*-kfreebsd*) t=cpp-$(CROSS_PKG_GNU_TYPE);; \
	    *) continue;; \
	  esac; \
	  echo ln -sf $$t debian/$$p/usr/share/doc/$$p; \
	  mkdir -p debian/$$p/usr/share/doc; \
	  rm -rf debian/$$p/usr/share/doc/$$p; \
	  ln -sf $$t debian/$$p/usr/share/doc/$$p; \
	done

	dh_installchangelogs -pcpp-$(CROSS_PKG_GNU_TYPE) \
	  $(if $(filter pkg-config-$(CROSS_PKG_GNU_TYPE), $(packages)),-ppkg-config-$(CROSS_PKG_GNU_TYPE))
	for i in cpp-$(CROSS_PKG_GNU_TYPE) $(if $(filter pkg-config-$(CROSS_PKG_GNU_TYPE), $(packages)),pkg-config-$(CROSS_PKG_GNU_TYPE)); do \
	  mv debian/$$i/usr/share/doc/$$i/changelog \
		debian/$$i/usr/share/doc/$$i/changelog.Debian; \
	done
	dh_compress $(foreach p,$(filter %-$(CROSS_PKG_GNU_TYPE),$(cross_packages)),-p$(p)) \
	  -XREADME.Debian
	$(if $(filter pkg-config-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_link -ppkg-config-$(CROSS_PKG_GNU_TYPE) \
	  /usr/share/pkg-config-crosswrapper /usr/bin/${CROSS_GNU_TYPE}-pkg-config \
	)

	for p in $(filter %-$(CROSS_PKG_GNU_TYPE),$(cross_packages)); do \
	  case $$p in \
	    cpp-$(CROSS_PKG_GNU_TYPE)|g++-$(CROSS_PKG_GNU_TYPE)|gcc-$(CROSS_PKG_GNU_TYPE)|gccgo-$(CROSS_PKG_GNU_TYPE)|gfortran-$(CROSS_PKG_GNU_TYPE)) \
	      [ "$(with_gfdl_docs)" = yes ] && continue; \
	      mkdir -p -m 755 debian/$$p/usr/share/lintian/overrides; \
	      echo "$$p binary: binary-without-manpage" \
	        >> debian/$$p/usr/share/lintian/overrides/$$p;; \
	  esac; \
	done

	dh_fixperms $(foreach p,$(filter %-$(CROSS_PKG_GNU_TYPE),$(cross_packages)),-p$(p))
	dh_installdeb $(foreach p,$(filter %-$(CROSS_PKG_GNU_TYPE),$(cross_packages)),-p$(p))
	dh_gencontrol -pcpp-$(CROSS_PKG_GNU_TYPE) '-u-v$(EPOCH):$(CV_CPP_$(CROSS_ARCH))'
	dh_gencontrol -pgcc-$(CROSS_PKG_GNU_TYPE) \
	    $(if $(filter $(CROSS_ARCH),$(multilib_archs)),-pgcc-multilib-$(CROSS_PKG_GNU_TYPE)) \
	  '-u-v$(EPOCH):$(CV_GCC_$(CROSS_ARCH))'

	$(if $(filter gdc-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_gencontrol -pgdc-$(CROSS_PKG_GNU_TYPE) \
	    $(if $(filter $(CROSS_ARCH),$(d_multilib_archs)),-pgdc-multilib-$(CROSS_PKG_GNU_TYPE)) \
	    -u-v$(EPOCH):$(CV_GDC_$(CROSS_ARCH)) \
	)

	$(if $(filter gm2-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_gencontrol -pgm2-$(CROSS_PKG_GNU_TYPE) \
	    -u-v$(EPOCH):$(CV_GM2_$(CROSS_ARCH)) \
	)

	$(if $(filter g++-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_gencontrol -pg++-$(CROSS_PKG_GNU_TYPE) \
	    $(if $(filter $(CROSS_ARCH),$(multilib_archs)),-pg++-multilib-$(CROSS_PKG_GNU_TYPE)) \
	    -u-v$(EPOCH):$(CV_GPP_$(CROSS_ARCH)) \
	)

	$(if $(filter gobjc++-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_gencontrol -pgobjc-$(CROSS_PKG_GNU_TYPE) \
	    $(if $(filter $(CROSS_ARCH),$(multilib_archs)),-pgobjc-multilib-$(CROSS_PKG_GNU_TYPE)) \
	    -u-v$(EPOCH):$(CV_GOBJC_$(CROSS_ARCH)) \
	)

	$(if $(filter gobjc++-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_gencontrol -pgobjc++-$(CROSS_PKG_GNU_TYPE) \
	    $(if $(filter $(CROSS_ARCH),$(multilib_archs)),-pgobjc++-multilib-$(CROSS_PKG_GNU_TYPE)) \
	    -u-v$(EPOCH):$(CV_GOBJCXX_$(CROSS_ARCH)) \
	)

	$(if $(filter gfortran-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_gencontrol -pgfortran-$(CROSS_PKG_GNU_TYPE) \
	    $(if $(filter $(CROSS_ARCH),$(multilib_archs)),-pgfortran-multilib-$(CROSS_PKG_GNU_TYPE)) \
	    -u-v$(EPOCH):$(CV_GFORT_$(CROSS_ARCH)) \
	)

	$(if $(filter gccgo-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_gencontrol -pgccgo-$(CROSS_PKG_GNU_TYPE) \
	    $(if $(filter $(CROSS_ARCH),$(go_multilib_archs)),-pgccgo-multilib-$(CROSS_PKG_GNU_TYPE)) \
	    -u-v$(EPOCH):$(CV_GGO_$(CROSS_ARCH)) \
	)

	$(if $(filter gnat-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_gencontrol -pgnat-$(CROSS_PKG_GNU_TYPE) -pgnat-sjlj-$(CROSS_PKG_GNU_TYPE) \
		-u-v$(EPOCH):$(CV_GNAT_$(CROSS_ARCH)) \
	)

	$(if $(filter pkg-config-$(CROSS_PKG_GNU_TYPE), $(packages)), \
	  dh_gencontrol -ppkg-config-$(CROSS_PKG_GNU_TYPE) \
		-u-v$(EPOCH):$(CV_GCC_$(CROSS_ARCH)) \
	)

	dh_md5sums $(foreach p,$(filter %-$(CROSS_PKG_GNU_TYPE),$(cross_packages)),-p$(p))
	dh_builddeb $(foreach p,$(filter %-$(CROSS_PKG_GNU_TYPE),$(cross_packages)),-p$(p))

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install substvars
