#!/usr/bin/make -f
include /usr/share/dpkg/pkg-info.mk
target != echo $(DEB_SOURCE) | sed s/gdb-//
gdb_dir=/usr/src

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

deb_upstream_version != dpkg-query -f'$${source:Upstream-Version}' -W gdb-source

# version number for our binary package: <gdb-source upstream>+<our native version>
deb_version = $(deb_upstream_version)+$(DEB_VERSION)

unpacked_dir=gdb
%:
	mkdir -p $(unpacked_dir)
	dh $@ -D$(unpacked_dir) -Bbuild

# Unpack the gdb-source tarball and apply local patches.  The tarball
# already carries the Debian-specific quilt series baked in, so
# debian/local-patches/ is reserved for changes that are local to this
# cross-toolchain (e.g. ISA extension support that is not yet in a
# Debian gdb release).
#
# This hook may be invoked more than once by dh during a single
# dpkg-buildpackage run, so the entire unpack+patch sequence is
# guarded by a stamp file to keep it idempotent (tar -x is harmless
# to repeat, but `patch` is not).  Defining a recipe on the hook
# target also stops the catch-all `%:` rule below from re-dispatching
# `dh execute_before_dh_update_autotools_config`, which would otherwise
# cause the hook to recurse.
execute_before_dh_update_autotools_config:
	mkdir -p $(unpacked_dir)
	if [ ! -e $(unpacked_dir)/.local-patches-applied ]; then \
		tar --strip-components=1 -C $(unpacked_dir) -xf $(gdb_dir)/gdb.tar.* ; \
		for patch in debian/local-patches/[0-9]*.patch; do \
			[ -e "$$patch" ] || continue; \
			echo "Applying local patch $$patch"; \
			patch -d $(unpacked_dir) --ignore-whitespace -p1 < "$$patch"; \
		done; \
		rej_files=$$(find $(unpacked_dir) -name '*.rej' -print); \
		if [ -n "$$rej_files" ]; then \
			echo "ERROR: Some local patches were rejected:"; \
			for rej in $$rej_files; do \
				echo "=== $$rej ==="; \
				cat "$$rej"; \
			done; \
			exit 1; \
		fi; \
		touch $(unpacked_dir)/.local-patches-applied; \
	fi

# gdb-source's tarball already ships a pre-generated `configure` script,
# so there is no need to run `autoreconf` at all.  Doing so would fail
# because gdb still requires autoconf 2.69 exactly, whereas modern
# debhelper drags in autoconf 2.72.
override_dh_autoreconf:

override_dh_auto_configure:
# Debhelper >= 11 passes --runstatedir to configure by default, but
# gdb's autoconf 2.69 era configure does not know that flag.  Force
# the debhelper compat level used by dh_auto_configure back to 10 so
# that --runstatedir is not injected.
	DH_COMPAT=10 dh_auto_configure -- \
		--target=$(target) \
		--with-bugurl="https://github.com/raspberrypi/pico-sdk/issues" \
		--with-pkgversion="$(deb_version)" \
		--with-expat \
		--with-system-zlib \
		--with-python=python3 \
		--disable-nls \
		--disable-werror \
		--infodir=/nulldir \
		--htmldir=/nulldir \
		--pdfdir=/nulldir \
		--mandir=/usr/share/man \

execute_before_dh_installdocs: debian/$(DEB_SOURCE).copyright
debian/$(DEB_SOURCE).copyright: debian/copyright /usr/share/doc/gdb-source/copyright
	cp debian/copyright $@
	printf -- "\n-----BEGIN GDB COPYRIGHT INFORMATION-----\n" >> $@
	cat /usr/share/doc/gdb-source/copyright >> $@
	printf -- "-----END GDB COPYRIGHT INFORMATION-----\n" >> $@

BUILT_USING != dpkg-query -f '$${source:Package} (= $${source:Version})' -W gdb-source
override_dh_gencontrol:
	dh_gencontrol -- -v$(deb_version) -VBuilt-Using="$(BUILT_USING)"

# Trim install tree of things that make no sense for a bare-metal cross
# debugger, or that would collide with the native gdb package:
#   * /usr/share/gdb/{python,syscalls,system-gdbinit}: arch-independent
#     runtime Python helpers, syscall XMLs, etc.  These are identical to
#     the ones shipped by the native `gdb` package (we Depends: on it),
#     so we defer to it rather than trying to co-install duplicates —
#     the same approach Debian takes for `gdb-multiarch`.
#   * unprefixed upstream man pages that would clash with `gdb`'s
#     (our own $(target)-* copies are installed from debian/);
#   * riscv32-pico-elf-gstack: shell wrapper that attaches gdb to a
#     local host process — impossible against a Pico target;
#   * libsim.a plus its headers under /usr/{lib,include}: only useful
#     for out-of-tree code linking gdb's simulator, and their placement
#     in the multiarch libdir is incorrect for a target-specific
#     artefact.
# Finally, strip the RUNPATH that gdb's configure bakes into the
# binaries; it just points at the default library search path anyway.
execute_after_dh_auto_install:
	install_dir=debian/$(DEB_SOURCE) ; \
	    rm -rf $$install_dir/usr/share/locale $$install_dir/nulldir \
	           $$install_dir/usr/share/gdb \
	           $$install_dir/usr/lib $$install_dir/usr/include ; \
	    rm -f $$install_dir/usr/share/man/man1/gdb.1 \
	          $$install_dir/usr/share/man/man1/gdb-add-index.1 \
	          $$install_dir/usr/share/man/man1/run.1 \
	          $$install_dir/usr/bin/$(target)-gstack ; \
	    for f in $$install_dir/usr/bin/*; do \
	        chrpath -d "$$f" 2>/dev/null || :; \
	    done

# gdb's testsuite requires either a native inferior or a target simulator
# harness, neither of which we have in a cross-only bare-metal configuration.
override_dh_auto_test:
