#!/usr/bin/make -f

UPSTREAM_VERSION=$(shell dpkg-parsechangelog | sed -e '/^Version/!d' -e 's/^Version: //g' -e 's/-.*//g')
UPSTREAM_REVISION=$(shell echo $(UPSTREAM_VERSION) | sed -e 's/.*svn/-r/')
UPSTREAM_SVN=svn://openbios.org/openbios/trunk/openbios-devel
UPSTREAM_CHECKOUT=openbios-sparc-$(UPSTREAM_VERSION)

get-orig-source:
	svn export $(UPSTREAM_REVISION) $(UPSTREAM_SVN) $(UPSTREAM_CHECKOUT)
	rm -fr openbios-$(UPSTREAM_VERSION)/utils/iso
	tar --owner=root --group=root -Jcf ../openbios_$(UPSTREAM_VERSION).orig.tar.xz $(UPSTREAM_CHECKOUT)
	rm -rf $(UPSTREAM_CHECKOUT)

# We build a minimalistic cross powerpc-elf toolchain in order to be able
# to build the firmware on all architectures. It might be possible to
# use the native toolchain when building on a powerpc machine, but we don't
# that to make debian/rules simpler, but also because we want the build to
# be reproducible.
include debian/cross-toolchain.mk
export PATH=$(toolchain_dir)/bin:/usr/bin:/bin

# We build a firmware, so we do not link to the ssp libraries
export DEB_BUILD_MAINT_OPTIONS = hardening=-stackprotector

%:
	dh $@ --parallel

override_dh_auto_configure: $(stamp)gcc_powerpc-elf $(stamp)gcc_sparc-elf $(stamp)gcc_sparc64-elf
	config/scripts/switch-arch builtin-ppc builtin-sparc32 builtin-sparc64

override_dh_auto_build-indep:
	$(MAKE) -C obj-ppc
	$(MAKE) -C obj-sparc32
	$(MAKE) -C obj-sparc64

override_dh_auto_clean:
	dh_clean
	rm -rf $(toolchain_dir)
	rm -rf obj-*
	rm -f config-host.mak config.xml rules.xml

override_dh_auto_install-indep:
	mkdir -p $(CURDIR)/debian/openbios-ppc/usr/share/openbios
	install -m644 obj-ppc/openbios-qemu.elf $(CURDIR)/debian/openbios-ppc/usr/share/openbios/openbios-ppc

	mkdir -p $(CURDIR)/debian/openbios-sparc/usr/share/openbios
	install -m644 obj-sparc32/openbios-builtin.elf $(CURDIR)/debian/openbios-sparc/usr/share/openbios/openbios-sparc32
	install -m644 obj-sparc32/QEMU,tcx.bin $(CURDIR)/debian/openbios-sparc/usr/share/openbios/QEMU,tcx.bin
	install -m644 obj-sparc64/openbios-builtin.elf $(CURDIR)/debian/openbios-sparc/usr/share/openbios/openbios-sparc64

override_dh_installchangelogs:
	dh_installchangelogs Documentation/ChangeLog.arch
