#!/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

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 = 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) KCONFIG_CONFIG=$(CURDIR)/$$d/.config OUT=$$d/ oldnoconfig; \
fi; \
$(MAKE) KCONFIG_CONFIG=$(CURDIR)/$$d/.config OUT=$$d/ EXTRAVERSION="-${DEB_VERSION_RELEASE}" $$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 USB_XHCI=n USB_UAS=n XEN=n SDCARD=n TCGBIOS=n MPT_SCSI=n)
build/bios-256k.bin:
	$(call build-bios,bios,QEMU=y ROM_SIZE=256)

# upstream ships .hex files in tarball, result of iasl compilation
# ensure it is actually built during build
FWS = acpi-dsdt q35-acpi-dsdt ssdt-misc ssdt-pcihp ssdt-proc
FW_FILES = $(addsuffix .hex,$(addprefix src/fw/,${FWS}))

${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
# move out shipped .hex files and rebuild them
	mkdir -p build/src/fw
	for f in ${FW_FILES}; do [ -f $$f.sav -o ! -f $$f ] || mv $$f $$f.sav; done
	rm -f ${FW_FILES}
	make OUT=build/ ${FW_FILES}
	touch $@

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 scripts/*pyc
	for f in src/fw/*.hex.sav; do [ ! -f $$f ] || mv -f $$f $${f%.sav}; done
# one file isn't shipped by upstream
	rm -f src/fw/q35-acpi-dsdt.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
