#
# Makefile
#
# Leendert van Doorn, leendert@watson.ibm.com
# Copyright (c) 2005, International Business Machines Corporation.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; If not, see <http://www.gnu.org/licenses/>.
#

XEN_ROOT = $(CURDIR)/../../..
include $(XEN_ROOT)/tools/firmware/Rules.mk

ld-option = $(shell if $(LD) -v $(1) >/dev/null 2>&1; then echo y; else echo n; fi)

# SMBIOS spec requires format mm/dd/yyyy
SMBIOS_REL_DATE ?= $(call date,"+%m/%d/%Y")

CFLAGS += $(CFLAGS_xeninclude) -fno-pic -mregparm=3

# We mustn't use tools-only public interfaces.
CFLAGS += -D__XEN_INTERFACE_VERSION__=__XEN_LATEST_INTERFACE_VERSION__

OBJS  = hvmloader.o mp_tables.o util.o smbios.o 
OBJS += smp.o cacheattr.o xenbus.o vnuma.o
OBJS += e820.o pci.o pir.o ctype.o
OBJS += hvm_param.o
OBJS += ovmf.o seabios.o
ifeq ($(debug),y)
OBJS += tests.o
endif

CIRRUSVGA_DEBUG ?= n

ROMBIOS_DIR := ../rombios

ifeq ($(CONFIG_ROMBIOS),y)
STDVGA_ROM    := ../vgabios/VGABIOS-lgpl-latest.bin
ifeq ($(CIRRUSVGA_DEBUG),y)
CIRRUSVGA_ROM := ../vgabios/VGABIOS-lgpl-latest.cirrus.debug.bin
else
CIRRUSVGA_ROM := ../vgabios/VGABIOS-lgpl-latest.cirrus.bin
endif
endif

ROMS := 
DSDT_FILES :=

ifeq ($(CONFIG_ROMBIOS),y)
OBJS += optionroms.o 32bitbios_support.o rombios.o
CFLAGS += -DENABLE_ROMBIOS
ROMBIOS_ROM := $(ROMBIOS_DIR)/BIOS-bochs-latest
ROMS += $(ROMBIOS_ROM) $(STDVGA_ROM) $(CIRRUSVGA_ROM)
DSDT_FILES += dsdt_anycpu.c dsdt_15cpu.c
endif

# Suppress the warning about LOAD segments with RWX permissions, as what we
# build isn't a normal user-mode executable.
LDFLAGS-$(call ld-option,--warn-rwx-segments) := --no-warn-rwx-segments

.PHONY: all
all: hvmloader

.PHONY: acpi
acpi:
	$(MAKE) -C $(ACPI_PATH)  ACPI_BUILD_DIR=$(CURDIR) DSDT_FILES="$(DSDT_FILES)"

rombios.o: roms.inc
smbios.o: CFLAGS += -D__SMBIOS_DATE__="\"$(SMBIOS_REL_DATE)\""

ACPI_PATH = ../../libacpi
DSDT_FILES += dsdt_anycpu_qemu_xen.c
ACPI_OBJS = $(patsubst %.c,%.o,$(DSDT_FILES)) build.o static_tables.o
$(ACPI_OBJS): CFLAGS += -iquote . -DLIBACPI_STDUTILS=\"$(CURDIR)/util.h\"
CFLAGS += -I$(ACPI_PATH)
vpath build.c $(ACPI_PATH)
vpath static_tables.c $(ACPI_PATH)
OBJS += $(ACPI_OBJS)

$(DSDT_FILES): acpi

# Add DSDT_FILES as a prerequisite of "build.o" so that make will also
# generate the "ssdt_*.h" headers needed by "build.o".
build.o: $(DSDT_FILES)

hvmloader: $(OBJS) hvmloader.lds
	$(LD) $(LDFLAGS_DIRECT) $(LDFLAGS-y) -N -T hvmloader.lds -o $@ $(OBJS)

roms.inc: $(ROMS)
	echo "/* Autogenerated file. DO NOT EDIT */" > $@.new

ifneq ($(ROMBIOS_ROM),)
	echo "#ifdef ROM_INCLUDE_ROMBIOS" >> $@.new
	$(SHELL) $(XEN_ROOT)/tools/misc/mkhex rombios $(ROMBIOS_ROM) >> $@.new
	echo "#endif" >> $@.new
endif

ifneq ($(STDVGA_ROM),)
	echo "#ifdef ROM_INCLUDE_VGABIOS" >> $@.new
	$(SHELL) $(XEN_ROOT)/tools/misc/mkhex vgabios_stdvga $(STDVGA_ROM) >> $@.new
	echo "#endif" >> $@.new
endif
ifneq ($(CIRRUSVGA_ROM),)
	echo "#ifdef ROM_INCLUDE_VGABIOS" >> $@.new
	$(SHELL) $(XEN_ROOT)/tools/misc/mkhex vgabios_cirrusvga $(CIRRUSVGA_ROM) >> $@.new
	echo "#endif" >> $@.new
endif
	mv -f $@.new $@

.PHONY: clean
clean:
	rm -f roms.inc roms.inc.new acpi.h
	rm -f hvmloader hvmloader.tmp *.o $(DEPS_RM)
	$(MAKE) -C $(ACPI_PATH)  ACPI_BUILD_DIR=$(CURDIR) clean

.PHONY: distclean
distclean: clean

-include $(DEPS_INCLUDE)
