include_rules
export SYSLINUX

BOOT_MODULES = \
	config \
	$(GENODE_DIR)/repos/os/src/init/init \
	$(GENODE_DIR)/repos/base/src/test/log/test-log \

ifeq (@(TUP_ARCH),i386)
	ADDR_TYPE = .long
endif
ifeq (@(TUP_ARCH),x86_64)
	ADDR_TYPE = .quad
endif

AWK_IT = awk -v RS=" " -v ADDR_TYPE="$(ADDR_TYPE)"

: $(BOOT_MODULES) \
|> \
	echo -n %f | $(AWK_IT) -f a.awk >> %o; \
	echo -n %f | $(AWK_IT) -f b.awk >> %o; \
	echo -n %f | $(AWK_IT) -f c.awk >> %o; \
|> modules.s

: modules.s | $(BOOT_MODULES) |> $(CC) $(CC_MARCH) -c %f -o %o |> boot_modules.o

LINK_ADDRESS = 0x01000000

: boot_modules.o | $(GENODE_DIR)/repos/base-nova/<core> \
|> \
	$(CXX) $(CC_MARCH) -nostdlib \
	-Wl,-T -Wl,$(GENODE_DIR)/repos/base/src/ld/genode.ld \
	-Wl,-z -Wl,max-page-size=0x1000 \
	-Wl,-Ttext=$(LINK_ADDRESS) -Wl,-gc-sections \
	-Wl,-nostdlib \
	-Wl,--whole-archive -Wl,--start-group \
	%<core> %f \
	-Wl,--no-whole-archive \
	-Wl,--end-group \
	`$(CXX) $(CC_MARCH) -print-libgcc-file-name` \
	-o %o; \
	strip %o; \
|> image.elf

: $(GENODE_DIR)/repos/base-nova/src/NOVA/build/hypervisor-x86_64 image.elf |> \
	mkdir -p tmp/syslinux; \
	cp \
		$SYSLINUX/share/syslinux/isolinux.bin \
		$SYSLINUX/share/syslinux/ldlinux.c32 \
		$SYSLINUX/share/syslinux/libcom32.c32 \
		$SYSLINUX/share/syslinux/mboot.c32 \
		isolinux.cfg \
		tmp/syslinux; \
	cp %f tmp; \
	chmod -R 777 tmp; \
	mkisofs -o %o \
		-b syslinux/isolinux.bin -c syslinux/boot.cat \
		-no-emul-boot -boot-load-size 4 -boot-info-table \
		-iso-level 2 \
		tmp; \
	rm -r tmp; \
|> output.iso
