#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

%:
	dh $@

# qemu EFI roms shall be built with CONFIG=qemu
# This makes it apply src/config/*, src/config/qemu/*, src/config/local/*,
# src/config/local/qemu/* in this order.
src/bin-%.rom src/bin-%.efirom: export CONFIG=qemu

# Export CROSS prefix for building.
# This controls the architecture-specific build tools to be used.
src/bin-i386-pcbios%: export CROSS=x86_64-linux-gnu-
src/bin-i386-pcbios%: export DEB_HOST_ARCH=amd64

src/bin-x86_64-efi%: export CROSS=x86_64-linux-gnu-
src/bin-x86_64-efi%: export DEB_HOST_ARCH=amd64

src/bin-arm64-efi%: export CROSS=aarch64-linux-gnu-
src/bin-arm64-efi%: export DEB_HOST_ARCH=arm64

src/bin-riscv64-efi%: export CROSS=riscv64-linux-gnu-
src/bin-riscv64-efi%: export DEB_HOST_ARCH=riscv64

src/bin-loong64-efi%: export CROSS=loongarch64-linux-gnu-
src/bin-loong64-efi%: export DEB_HOST_ARCH=loong64

# For i386, use the i686-linux-gnu- toolchain when x86_64-linux-gnu- is not
# available. This is the case when building any package for i386 host.
ifeq ($(DEB_HOST_ARCH),i386)
ifneq ($(shell dpkg-query --show --showformat '$${db:Status-Status}' gcc-x86-64-linux-gnu),installed)
src/bin-i386-pcbios%: export CROSS=i686-linux-gnu-
src/bin-i386-pcbios%: export DEB_HOST_ARCH=i386
endif
endif

ifeq ($(DEB_BUILD_ARCH),i386)
export HOST_EFI_CFLAGS = -malign-double
endif

# used in (patched) src/Makefile
export HOST_CFLAGS = $(shell dpkg-buildflags --get CFLAGS_FOR_BUILD) $(shell dpkg-buildflags --get LDFLAGS_FOR_BUILD) -D__BLHC_IGNORE_HOSTCC

# 1. forcibly use cross tools and ignore DEB_HOST_ARCH
# 2. also setting CXX and PKG_CONFIG even if they are not used
#    to avoid confusing names in build logs
# 3. XFLAGS from env cannot be reused since DEB_HOST_ARCH is reset
# 4. save the files that are modified by the build, restore them when
#    next build of the same <arch>-<platform> is triggered, to prevent
#    unnecessary rebuilds
define run-dh_auto_build =
if [ -e "$(dir $@)/config.tar" ]; then \
	tar -C src -xf "$(dir $@)/config.tar"; \
fi
@if [ "$$DEB_HOST_ARCH" != "$(DEB_BUILD_ARCH)" ]; then \
	echo "blhc: ignore-line-regexp: .*$$CROSS.*"; \
fi
CC="$${CROSS}gcc" \
CXX="$${CROSS}g++" \
PKG_CONFIG="$${CROSS}pkg-config" \
CFLAGS="$$(dpkg-buildflags --get CFLAGS)" \
CPPFLAGS="$$(dpkg-buildflags --get CPPFLAGS)" \
LDFLAGS="$$(dpkg-buildflags --get LDFLAGS)" \
dh_auto_build --sourcedirectory=src -- \
	V=1 NO_WERROR=1 VERSION="$(DEB_VERSION)" $(subst src/,,$@)
tar -C src -cf "$(dir $@)/config.tar" config/local config/named.h include/compiler.h include/ipxe/profile.h include/assert.h
endef

src/bin-i386-pcbios%:
	$(run-dh_auto_build)

src/bin-x86_64-efi%:
	$(run-dh_auto_build)

src/bin-arm64-efi%:
	$(run-dh_auto_build)

src/bin-riscv64-efi%:
	$(run-dh_auto_build)

src/bin-loong64-efi%:
	$(run-dh_auto_build)

# efi roms are >256k since Bionic due to https + natural growth
# stay at 256k-512k after disabling https in efi via padding
define run-catrom =
@mkdir -p $(dir $@)
@echo "Combining $@ with:"
@for rom in $^; do \
	echo "  $$rom: $$(stat -c %s "$$rom" )"; \
done
src/util/catrom.pl $^ > $@
@echo "Size before padding: $$(stat -c %s $@)"
src/util/padimg.pl --blksize=524288 --byte=0xff --verbose $@
endef

src/bin-combined/%.efirom: src/bin-i386-pcbios-rom/%.rom src/bin-x86_64-efi-rom/%.efirom
	$(run-catrom)

src/bin-combined/%.iso: src/bin-i386-pcbios/%.lkrn src/bin-x86_64-efi/%.efi src/bin-arm64-efi/%.efi src/bin-riscv64-efi/%.efi src/bin-loong64-efi/%.efi
	@mkdir -p $(dir $@)
	debian/util/geniso $@ $^

define get_targets
$(shell \
  for pkg in $$(dh_listpackages $(1)); do \
    if [ -e "debian/$$pkg.$(2)" ]; then \
	  ( \
		if [ -x "debian/$$pkg.$(2)" ]; then \
		  dh-exec --with= "debian/$$pkg.$(2)"; \
		else \
		  cat "debian/$$pkg.$(2)"; \
		fi \
	  ) | grep -hoE 'src/bin\S+'; \
	fi; \
  done \
)
endef

# ================
# dh targets below
# ================

execute_before_dh_auto_build:
	@echo "blhc: ignore-line-regexp: .*__BLHC_IGNORE_HOSTCC.*"

override_dh_auto_build-indep: $(call get_targets,-i,install)
override_dh_auto_build-arch: $(call get_targets,-a,install)

execute_after_dh_install-indep:
	debian/util/check-rom-sizes

override_dh_auto_clean:
	dh_auto_clean --sourcedirectory=src -- veryclean
	rm -fr src/bin-*
	find src/config/local -type f -name '*.h' -size 0 -delete

ifeq (,$(filter nocheck, $(DEB_BUILD_OPTIONS)))

test_roms-indep = $(call get_targets,-i,testroms)
test_roms-arch = $(call get_targets,-a,testroms)

override_dh_auto_build-indep: $(test_roms-indep)
override_dh_auto_build-arch: $(test_roms-arch)

debian/self_tests/%: %
	./debian/tests/ipxe-tests $<
	mkdir -p $(dir $@) && touch $@

override_dh_auto_test-indep: $(addprefix debian/self_tests/,$(test_roms-indep))
override_dh_auto_test-arch: $(addprefix debian/self_tests/,$(test_roms-arch))

override_dh_auto_clean: clean_self_tests
clean_self_tests:
	rm -rf debian/self_tests

endif
