#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

MAKEVARS = PYTHON="python3 -B" EXTRAVERSION="-${DEB_VERSION_RELEASE}"

dpkg_late_eval ?= $(or $(value DPKG_CACHE_$(1)),$(eval DPKG_CACHE_$(1) := $(shell $(2)))$(value DPKG_CACHE_$(1)))
DEB_VERSION = $(call dpkg_late_eval,DEB_VERSION,dpkg-parsechangelog -SVersion)
# everything after last minus sign
DEB_VERSION_RELEASE = $(call dpkg_late_eval,DEB_VERSION_RELEASE,echo '$(DEB_VERSION)' | sed -e 's/^.*-//')

VGABIOSES = ati cirrus stdvga virtio vmware qxl isavga
VGABIOS_TARGETS = $(addprefix build/vgabios-, $(addsuffix .bin, $(VGABIOSES)))
BIOS_TARGETS = build/bios.bin build/bios-256k.bin ${VGABIOS_TARGETS}
BUILD_TARGETS = $(BIOS_TARGETS) build/optionrom-stamp

build build-arch build-indep: $(BUILD_TARGETS)

# common rule to build any bios.
# $1: build target: bios or vgabios, builds $(OUT)/$1
# $2: config options to put into .config without CONFIG_ prefix
# $@: where to put output; build dir will be $(basename $@)
define build-bios
set -e; d=$(basename $@); \
if [ ! -e $$d/.config.old ]; then \
 rm -rf $$d; mkdir -p $$d; \
 for x in $2; do \
  case $$x in \
   (*=n) echo "# CONFIG_$${x%=*} is not set";; \
   (*) echo CONFIG_$$x;; \
  esac; \
 done > $$d/.config; \
 $(MAKE) ${MAKEVARS} KCONFIG_CONFIG=$(CURDIR)/$$d/.config OUT=$$d/ oldnoconfig; \
fi; \
$(MAKE) ${MAKEVARS} KCONFIG_CONFIG=$(CURDIR)/$$d/.config OUT=$$d/ $$d/$1.bin; \
rm -f $@; ln $$d/$1.bin $@
endef

build/bios.bin:
# A stripped-down version of bios, to fit in 128Kb, for qemu <= 1.7
	$(call build-bios,bios,QEMU=y ROM_SIZE=128 PVSCSI=n BOOTSPLASH=n XEN=n USB_OHCI=n USB_XHCI=n USB_UAS=n SDCARD=n TCGBIOS=n MPT_SCSI=n NVME=n USE_SMM=n VGAHOOKS=n)
build/bios-256k.bin:
	$(call build-bios,bios,QEMU=y ROM_SIZE=256)

${BIOS_TARGETS}: build/env-stamp
build/env-stamp:
# upstream checks for .git and .version in scripts/buildversion.py
# .version comes in upstream tarball, and .git should not be there
	[ ! -d .git ] || { echo "Directory .git exists, aborting" >&2; exit 1; }
	[ -f .version ] || echo ${DEB_VERSION} | sed 's/-.*//' > .version
	mkdir -p build
# regenerate shipped .hex files (iasl-compiled .dsl)
	rm -f src/fw/*.hex
# make iasl unnecessarily generates .config
	make ${MAKEVARS} OUT=build/ KCONFIG_CONFIG=$(CURDIR)/build/.fakeconfig iasl
	touch $@

build/vgabios-ati.bin:
	$(call build-bios,vgabios,BUILD_VGABIOS=y VGA_ATI=y VGA_PCI=y)
build/vgabios-bochs-display.bin:
	$(call build-bios,vgabios,BUILD_VGABIOS=y DISPLAY_BOCHS=y VGA_PCI=y)
build/vgabios-cirrus.bin:
	$(call build-bios,vgabios,BUILD_VGABIOS=y VGA_CIRRUS=y VGA_PCI=y)
build/vgabios-stdvga.bin:
	$(call build-bios,vgabios,BUILD_VGABIOS=y VGA_BOCHS=y VGA_PCI=y)
build/vgabios-virtio.bin:
	$(call build-bios,vgabios,BUILD_VGABIOS=y VGA_BOCHS=y VGA_PCI=y \
		OVERRIDE_PCI_ID=y VGA_VID=0x1af4 VGA_DID=0x1050)
build/vgabios-vmware.bin:
	$(call build-bios,vgabios,BUILD_VGABIOS=y VGA_BOCHS=y VGA_PCI=y \
		OVERRIDE_PCI_ID=y VGA_VID=0x15ad VGA_DID=0x0405)
build/vgabios-qxl.bin:
	$(call build-bios,vgabios,BUILD_VGABIOS=y VGA_BOCHS=y VGA_PCI=y \
		OVERRIDE_PCI_ID=y VGA_VID=0x1b36 VGA_DID=0x0100)
build/vgabios-isavga.bin:
	$(call build-bios,vgabios,BUILD_VGABIOS=y VGA_BOCHS=y VGA_PCI=n)

build/optionrom-stamp:
	$(MAKE) -C debian/optionrom/
	chmod -x debian/optionrom/*.bin
	ln -s kvmvapic.bin debian/optionrom/vapic.bin
	mkdir -p $(dir $@) && touch $@

clean:
	dh_testdir
	rm -rf build/
	rm -f src/fw/*.hex
	$(MAKE) -C debian/optionrom/ clean
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	dh_install

# Build architecture-independent files here.
binary-arch: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installman
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
