#This is a make file with common rules for both x86 & x86-64

all: directories test_cases

cflatobjs += lib/pci.o
cflatobjs += lib/pci-edu.o
cflatobjs += lib/x86/setup.o
cflatobjs += lib/x86/io.o
cflatobjs += lib/x86/smp.o
cflatobjs += lib/x86/vm.o
cflatobjs += lib/x86/fwcfg.o
cflatobjs += lib/x86/apic.o
cflatobjs += lib/x86/atomic.o
cflatobjs += lib/x86/desc.o
cflatobjs += lib/x86/isr.o
cflatobjs += lib/x86/acpi.o
cflatobjs += lib/x86/stack.o

OBJDIRS += lib/x86

$(libcflat): LDFLAGS += -nostdlib
$(libcflat): CFLAGS += -ffreestanding -I $(SRCDIR)/lib -I lib

COMMON_CFLAGS += -m$(bits)
COMMON_CFLAGS += -O1
COMMON_CFLAGS += -fno-stack-protector

# stack.o relies on frame pointers.
KEEP_FRAME_POINTER := y

libgcc := $(shell $(CC) -m$(bits) --print-libgcc-file-name)

# We want to keep intermediate file: %.elf and %.o 
.PRECIOUS: %.elf %.o

FLATLIBS = lib/libcflat.a $(libgcc)
%.elf: %.o $(FLATLIBS) $(SRCDIR)/x86/flat.lds $(cstart.o)
	$(CC) $(CFLAGS) -nostdlib -o $@ -Wl,-T,$(SRCDIR)/x86/flat.lds \
		$(filter %.o, $^) $(FLATLIBS)

%.flat: %.elf
	$(OBJCOPY) -O elf32-i386 $^ $@

tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \
               $(TEST_DIR)/smptest.flat  $(TEST_DIR)/port80.flat \
               $(TEST_DIR)/realmode.flat $(TEST_DIR)/msr.flat \
               $(TEST_DIR)/hypercall.flat $(TEST_DIR)/sieve.flat \
               $(TEST_DIR)/kvmclock_test.flat  $(TEST_DIR)/eventinj.flat \
               $(TEST_DIR)/s3.flat $(TEST_DIR)/pmu.flat $(TEST_DIR)/setjmp.flat \
               $(TEST_DIR)/tsc_adjust.flat $(TEST_DIR)/asyncpf.flat \
               $(TEST_DIR)/init.flat $(TEST_DIR)/smap.flat \
               $(TEST_DIR)/hyperv_synic.flat $(TEST_DIR)/hyperv_stimer.flat \
               $(TEST_DIR)/hyperv_connections.flat \
               $(TEST_DIR)/pae.flat \

ifdef API
tests-api = api/api-sample api/dirty-log api/dirty-log-perf

OBJDIRS += api
endif

test_cases: $(tests-common) $(tests) $(tests-api)

$(TEST_DIR)/%.o: CFLAGS += -std=gnu99 -ffreestanding -I $(SRCDIR)/lib -I $(SRCDIR)/lib/x86 -I lib

$(TEST_DIR)/realmode.elf: $(TEST_DIR)/realmode.o
	$(CC) -m32 -nostdlib -o $@ -Wl,-T,$(SRCDIR)/$(TEST_DIR)/realmode.lds $^

$(TEST_DIR)/realmode.o: bits = 32

$(TEST_DIR)/kvmclock_test.elf: $(TEST_DIR)/kvmclock.o

$(TEST_DIR)/hyperv_synic.elf: $(TEST_DIR)/hyperv.o

$(TEST_DIR)/hyperv_stimer.elf: $(TEST_DIR)/hyperv.o

$(TEST_DIR)/hyperv_connections.elf: $(TEST_DIR)/hyperv.o

arch_clean:
	$(RM) $(TEST_DIR)/*.o $(TEST_DIR)/*.flat $(TEST_DIR)/*.elf \
	$(TEST_DIR)/.*.d lib/x86/.*.d \
	$(tests-api) api/*.o api/*.a api/.*.d

api/%.o: CXXFLAGS += -m32 -std=gnu++11

api/%: LDLIBS += -lstdc++ -lpthread -lrt
api/%: LDFLAGS += -m32

api/libapi.a: api/kvmxx.o api/identity.o api/exception.o api/memmap.o
	$(AR) rcs $@ $^

$(tests-api) : % : %.o api/libapi.a
