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

#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
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

# 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
define run-dh_auto_build =
@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/,,$@)
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)

# 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 $@)
src/util/catrom.pl $^ > $@
src/util/padimg.pl --blksize=524288 --byte=0xff --verbose $@
endef

x86-only-roms = eepro100 ns8390 pcnet32

$(addprefix src/bin-combined/,$(addsuffix .efirom,$(x86-only-roms))): src/bin-combined/%.efirom: src/bin-i386-pcbios/%.rom src/bin-x86_64-efi/%.efirom
	$(run-catrom)

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

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

define get_targets
$(shell \
  for pkg in $$(dh_listpackages $(1)); do \
    if [ -e "debian/$$pkg.install" ]; then \
	  ( \
		if [ -x "debian/$$pkg.install" ]; then \
		  dh-exec --with= "debian/$$pkg.install"; \
		else \
		  cat "debian/$$pkg.install"; \
		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)
override_dh_auto_build-arch: $(call get_targets,-a)

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

override_dh_auto_clean:
	dh_auto_clean --sourcedirectory=src -- veryclean
	rm -fr src/bin-*
