#!/usr/bin/make -f
#export DH_VERBOSE=1

target=bpf

GCC_VERSION=15
GCC_PACKAGE=gcc-$(GCC_VERSION)
GCC_DIR := /usr/src/gcc-15
GCC_TARBALL := $(notdir $(wildcard $(GCC_DIR)/gcc-*.tar.*))

gcclibdir = usr/lib/gcc/$(target)/$(GCC_VERSION)

unexport CPPFLAGS CFLAGS CXXFLAGS LDFLAGS

package=gcc-bpf
d=debian/tmp
top_dir=$(shell pwd)
gcc_dir=/usr/src/$(GCC_PACKAGE)
doc_dir=/usr/share/doc/$(package)
build_doc_dir=$(top_dir)/debian/$(package)$(doc_dir)
stampdir=debian/stamp

source_version := $(shell dpkg-query -W -f="\$${Version}\n" $(GCC_PACKAGE)-source)

deb_release := $(shell expr $(shell dpkg-parsechangelog -S Version) - 16)
deb_version := $(source_version)+$(deb_release)

deb_upstream_version := $(shell echo $(deb_version) | cut -d- -f1)
base_version := $(shell echo $(deb_version) | sed -e 's/\([1-9]\.[0-9]\).*-.*/\1/')

BUILT_USING := $(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W $(GCC_PACKAGE)-source)

unpack_stamp=$(stampdir)/unpack
tar_stamp=$(stampdir)/tar

build_dir=build

target_tools=\
	AR_FOR_TARGET=$(target)-ar \
	AS_FOR_TARGET=$(target)-as \
	LD_FOR_TARGET=$(target)-ld \
	NM_FOR_TARGET=$(target)-nm \
	OBJDUMP_FOR_TARGET=$(target)-objdump \
	RANLIB_FOR_TARGET=$(target)-ranlib \
	READELF_FOR_TARGET=$(target)-readelf \
	STRIP_FOR_TARGET=$(target)-strip

configure_flags = \
	--enable-languages=c \
	--prefix=/usr \
	--libdir=/usr/lib \
	--libexecdir=/usr/lib \
	--with-system-zlib \
	--disable-werror \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--host=$(DEB_HOST_GNU_TYPE) \
	--target=$(target) \
	--with-gnu-as \
	--with-gnu-ld \
	--with-pkgversion='$(deb_version)' \
	--with-gcc-major-version-only \
	--without-included-gettext \
	--disable-gcov \
	$(target_tools)

%:
	dh $@ -Dgcc-source -B$(build_dir)

$(unpack_stamp): $(tar_stamp)
	set -ex; \
	  cd $(top_dir)/gcc-source; \
	  for patch in $(shell grep -v '^#' debian/debian_patches.txt); do \
	    echo Applying patch "$$patch"; \
	    patch -p2 < $(gcc_dir)/patches/$$patch; \
	  done
	set -ex; \
	  cd gcc-*; \
	    for patch in ../debian/local-patches/[0-9]*.diff; do \
	      [ -f $$patch ] || continue; \
	      echo Applying patch "$$patch"; \
	      patch -p1 < "$$patch"; \
	    done
	touch $@

$(tar_stamp):
	tar xf $(GCC_DIR)/$(GCC_TARBALL)
	ln -s gcc-[1-9]* gcc-source
	mkdir -p $(stampdir)
	cp /usr/share/doc/$(GCC_PACKAGE)-source/copyright debian/copyright-gcc
	touch $@

override_dh_autoreconf: $(unpack_stamp)
	#autoreconf -f -i gcc-source

# dh_auto_configure is brain dead, adding duplicate flags which are then
# seen in the gcc -v output
override_dh_auto_configure: $(unpack_stamp)
	mkdir -p $(build_dir)
	cd $(build_dir) && ../gcc-source/configure $(configure_flags)

override_dh_auto_clean:
	rm -rf gcc-* $(build_dir) debian/tmp debian/copyright-gcc $(stampdir)
	dh_autoreconf_clean

override_dh_auto_test:
	@echo "no testing, that's way too painful"
#	dh_auto_test -Dgcc-source -B$(build_dir) --max-parallel=1

override_dh_gencontrol:
	dh_gencontrol -a -- \
		-v$(deb_version) \
		-Vlocal:Version=$(deb_upstream_version) \
		-Vgcc:Version=$(source_version) \
		-VBuilt-Using="$(BUILT_USING)"

override_dh_auto_install:
	mkdir -p $(top_dir)/debian/tmp
	cd $(top_dir)/build && make install DESTDIR=$(top_dir)/$(d)
#	mv $(d)/$(gcclibdir)/include-fixed/*.h $(d)/$(gcclibdir)/include/.
	rm -rf $(d)/$(gcclibdir)/include-fixed
	rm -rf $(d)/$(gcclibdir)/install-tools
	rm -rf $(d)/$(gcclibdir)/plugin
	rm -f $(d)/$(gcclibdir)/lto1
	rm -rf $(d)/usr/share
	rm -f $(d)/usr/bin/$(target)-gcc-$(GCC_VERSION)
	rm -f $(d)/usr/bin/$(target)-gcov*
	rm -f $(d)/usr/bin/$(target)-lto-*
	for i in ar as ld nm objcopy objdump ranlib readelf strip; do \
	  ln -sf ../../../../bin/$(target)-$$i $(d)/$(gcclibdir)/$$i; \
	done

override_dh_installchangelogs:
	dh_installchangelogs gcc-source/ChangeLog

override_dh_compress:
	dh_compress -Xexamples/

override_dh_strip:
	dh_strip -X.a
