#!/usr/bin/make -f

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

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

gnu_type := or1k-elf

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

# Version of the GCC source Debian package.
gcc-version := $(shell dpkg-query -Wf'$${source:Version}' gcc-$(gcc-version-major)-source)

gcc_libdir          := usr/lib/gcc/$(gnu_type)/$(gcc-version-major)
gcc_libexecdir      := usr/libexec/gcc/$(gnu_type)/$(gcc-version-major)
static_archives     := $(addprefix $(gcc_libdir)/,libgcc.a libgcov.a)

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

execute_before_dh_autoreconf: src
src:
	tar -xf /usr/src/gcc-$(gcc-version-major)/gcc-$(gcc-version-major)*.tar.xz
	mv gcc-$(gcc-version-major)* src
	patch -p1 < /usr/src/gcc-$(gcc-version-major)/patches/git-updates.diff
	patch -p1 < /usr/src/gcc-$(gcc-version-major)/patches/gcc-gfdl-build.diff

# By default, autoreconf only updates a few subdirs, but an explicit
# subdir list fails because gcc-10 is not ready for autotools-2.69.

# Avoid dh_auto_*, see
# https://gcc.gnu.org/legacy-ml/gcc/2013-04/msg00171.html.

# libssp: libssp requires glibc
# gcc-major-version: for consistency with native gcc
override_dh_auto_configure:
	mkdir -p bld
	cd bld && ../src/configure \
	  --build=$(DEB_BUILD_GNU_TYPE) \
	  --host=$(DEB_HOST_GNU_TYPE) \
	  --target=$(gnu_type) \
	  --prefix=/usr \
	  --disable-shared \
	  --disable-multilib \
	  --disable-maintainer-mode \
	  --disable-bootstrap \
	  --enable-languages=c \
	  --disable-libssp \
	  --disable-werror \
	  --with-gcc-major-version-only \
	  --without-included-gettext \
	  --with-pkgversion='GCC $(gcc-version) Debian $(DEB_VERSION)' \
	  --with-system-zlib

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

# Skip *long* tests.
override_dh_auto_test:

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

override_dh_install:
	dh_install --autodest \
	  usr/bin/$(gnu_type)-cpp \
	  usr/bin/$(gnu_type)-gcc \
	  usr/bin/$(gnu_type)-gcc-ar \
	  usr/bin/$(gnu_type)-gcc-nm \
	  usr/bin/$(gnu_type)-gcc-ranlib \
	  $(gcc_libdir)/include/\*.h \
	  $(gcc_libdir)/include-fixed/\*.h \
	  $(gcc_libexecdir)/cc1 \
	  $(gcc_libexecdir)/collect2 \
	  $(gcc_libexecdir)/liblto_plugin.so\* \
	  $(gcc_libexecdir)/lto-wrapper \
	  $(gcc_libexecdir)/lto1 \
	  $(static_archives)

override_dh_link:
	dh_link $(foreach tool,ar as ld nm objcopy objdump ranlib readelf strip, \
	  usr/bin/$(gnu_type)-$(tool) $(gcc_libdir)/$(tool))

execute_before_dh_missing: debian/not-installed
debian/not-installed:
	echo \
	  usr/bin/$(gnu_type)-gcc-$(gcc-version-major) \
	  usr/bin/$(gnu_type)-gcov\* \
	  usr/bin/$(gnu_type)-lto-\* \
	  usr/lib/libcc1.\* \
	  $(gcc_libdir)/\*.o \
	  $(gcc_libdir)/include-fixed/README \
	  $(gcc_libdir)/install-tools/ \
	  $(gcc_libdir)/plugin/ \
	  $(gcc_libexecdir)/install-tools/ \
	  $(gcc_libexecdir)/liblto_plugin.la \
	  $(gcc_libexecdir)/plugin/ \
	  usr/share/info/ \
	  usr/share/locale/ \
	  usr/share/man/ \
	  > $@

override_dh_strip:
	dh_strip $(addprefix --exclude=,$(static_archives))
	$(gnu_type)-strip --remove-section=.comment --remove-section=.note \
	  $(addprefix debian/gcc-$(gnu_type)/,$(static_archives))

# The private shared library does not need ldconfig triggers.
override_dh_makeshlibs:
	dh_makeshlibs --no-scripts

override_dh_gencontrol:
	dh_gencontrol -- \
	  -v$(gcc-version)+$(DEB_VERSION) \
	  -Vgcc-version=$(gcc-version) \
	  -Vgcc-version-major=$(gcc-version-major)

override_dh_clean:
	dh_clean bld/ debian/not-installed src/
