#!/usr/bin/make -f

# https://wiki.debian.org/PackagingLessCommonBinutilsTargets

include /usr/share/dpkg/buildopts.mk
include /usr/share/dpkg/pkg-info.mk

# In gcc-10&11, libcpp/expr.c contains format errors.
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-format

gnu_type := or1k-elf
# Not deducible from package name because of possible underscore characters.

gcc-version-major := $(shell sed '/^ gcc-\(.*\)-source,$$/!d;s//\1/;q' debian/control)
# The Debian packaging calls this BV.

gcc-version != dpkg-query -Wf'$${Version}' gcc-$(gcc-version-major)-source

# https://gcc.gnu.org/install/configure.html recommends out-of-tree builds.
%:
	dh $@

execute_before_dh_update_autotools_config: src
tar_dir = /usr/src/gcc-$(gcc-version-major)
src:
	tar -xf $(tar_dir)/gcc-*.tar.xz --transform='s|[^/]*|src|'
	patch -p1 < $(tar_dir)/patches/git-updates.diff
	patch -p1 < $(tar_dir)/patches/gcc-gfdl-build.diff
	patch -p1 < debian/fix-autoreconf.diff

# _GCC_AUTOCONF_VERSION_CHECK in config/override.m4 requires 2.69.
override_dh_autoreconf:
	dh_autoreconf autoreconf2.69 -- -f -i -v `grep -v \# debian/autoreconf`

# libssp: libssp requires glibc
# Install to the same path hierarchy than other GCCs: neither
# multiarch components nor GCC minor versions.
override_dh_auto_configure:
  # Imitate dh_auto_configure -- $(confargs), but without
  # --runstatedir as long as libiberty/configure cannot be refreshed.
	install -d bld
	cd bld && ../src/configure --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info --sysconfdir=/etc --localstatedir=/var --disable-option-checking --disable-silent-rules --libdir=/usr/lib/$(DEB_HOST_GNU_TYPE) --disable-maintainer-mode --disable-dependency-tracking \
	  --target=$(gnu_type) \
	  --disable-shared \
	  --disable-multilib \
	  --disable-bootstrap \
	  --enable-languages=c \
	  --disable-libssp \
	  --libdir=\$${prefix}/lib \
	  --with-gcc-major-version-only \
	  --without-included-gettext \
	  --with-pkgversion='GCC $(gcc-version) Debian $(DEB_VERSION)' \
	  --with-system-zlib

# Prevent dh_auto_{build,test,install} from parsing Makefile, see
# https://gcc.gnu.org/legacy-ml/gcc/2013-04/msg00171.html
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100933

override_dh_auto_build:
	cd bld && $(MAKE) $(addprefix -j,$(DEB_BUILD_OPTION_PARALLEL))

override_dh_auto_test:
	cd bld && $(MAKE) check

# Even if building one package, install in two steps and display
# explicit install/missing lists.
override_dh_auto_install:
	cd bld && $(MAKE) install DESTDIR='$(CURDIR)/debian/tmp' AM_UPDATE_INFO_DIR=no

execute_before_dh_fixperms:
	chmod 644 debian/tmp/usr/libexec/gcc/$(gnu_type)/$(gcc-version-major)/liblto_plugin.so

override_dh_strip:
	dh_strip --exclude=.a
	$(gnu_type)-strip --remove-section=.comment --remove-section=.note \
	  debian/gcc-*/usr/lib/gcc/*/*/lib*.a

# The private shared library needs no ldconfig trigger.
override_dh_makeshlibs:
	dh_makeshlibs --no-scripts

gencontrol_format = \
  -v$${Version}+$(DEB_VERSION) \
  -VBuilt-Using="$${source:Package} (= $${source:Version})"
gencontrol_args != dpkg-query -Wf'$(gencontrol_format)' \
                   gcc-$(gcc-version-major)-source
override_dh_gencontrol:
	dh_gencontrol -- $(gencontrol_args) \
	  -Vgcc-version-major=$(gcc-version-major)
