# Makefile.build.in COPYRIGHT FUJITSU LIMITED 2015-2016
ARCH = @ARCH@
VPATH=@abs_srcdir@
SRC=$(VPATH)
IHKDIR=$(IHKBASE)/$(TARGETDIR)
OBJS = init.o mem.o debug.o mikc.o listeners.o ap.o syscall.o cls.o host.o
OBJS += process.o copy.o waitq.o futex.o timer.o plist.o fileobj.o shmobj.o
OBJS += zeroobj.o procfs.o devobj.o sysfs.o xpmem.o profile.o freeze.o
OBJS += rbtree.o
OBJS += pager.o
# POSTK_DEBUG_ARCH_DEP_18 coredump arch separation.
DEPSRCS=$(wildcard $(SRC)/*.c)

# OBJS added gencore.o
ifeq ($(ARCH), arm64)
OBJS += gencore.o
DEPSRCS += $(SRC)/../arch/arm64/kernel/gencore.c
endif

CFLAGS += -I$(SRC)/include -I@abs_builddir@/../ -I@abs_builddir@/include -D__KERNEL__  -g -fno-omit-frame-pointer -fno-inline -fno-inline-small-functions
ifneq ($(ARCH), arm64)
CFLAGS += -mcmodel=large -mno-red-zone
endif
LDFLAGS += -e arch_start
IHKOBJ = ihk/ihk.o

# POSTK_DEBUG_ARCH_DEP_24
ifeq ($(ARCH), arm64)
default: all
endif

ifeq ($(ARCH), arm64)
include @abs_builddir@/config/config.$(TARGET)
else
include $(SRC)/config/config.$(TARGET)
endif
include @abs_builddir@/../../ihk/cokernel/Makefile.common

# CFLAGS += -I$(SRC)/../arch/$(IHKARCH)/kernel/include -I$(SRC)/../lib/include

OBJDUMP ?= objdump
OBJCOPY ?= objcopy

# POSTK_DEBUG_ARCH_DEP_26
ifeq ($(ARCH), arm64)
SUBCMD_OPTS = TARGET=$(TARGET) O=$(CURDIR)/ihk CC=$(CC) LD=$(LD) OBJCOPY=$(OBJCOPY) SRC=$(SRC) ARCH=$(ARCH)
else
SUBCMD_OPTS = TARGET=$(TARGET) O=$(CURDIR)/ihk CC=$(CC) LD=$(LD) SRC=$(SRC)
endif

ld_kern_cmd_base = $(LD) $(LDFLAGS) -o $@.elf $^
mkimage_cmd_base = [ -f $(SRC)/script/mkimage.$(TARGET) ] && CC=$(CC) LD=$(LD) LDFLAGS="$(LDFLAGS_MKIMAGE)" OBJDUMP=$(OBJDUMP) OBJCOPY=$(OBJCOPY) sh $(SRC)/script/mkimage.$(TARGET) '$@.elf' '$@' '$(SRC)' || cp $@.elf $@

ld_kern_cmd = $(call echo_cmd,LDKERN,$@)$(ld_kern_cmd_base)
mkimage_cmd = $(call echo_cmd,MKIMAGE,$@)$(mkimage_cmd_base)

all: depend kernel.img

kernel.img: $(OBJS) $(IHKOBJ)
	$(ld_kern_cmd)
	$(mkimage_cmd)

clean:
	$(rm_cmd) $(OBJS) kernel.img kernel.img.elf Makefile.dep
	@$(submake) -C $(IHKBASE) $(SUBCMD_OPTS) clean

depend: Makefile.dep

Makefile.dep:
	$(call dep_cmd,$(DEPSRCS))

$(IHKOBJ): FORCE
	@mkdir -p $(dir $(IHKOBJ))
	+$(call echo_cmd,BUILD IHK,$(TARGET))$(submake) -C $(IHKBASE) $(SUBCMD_OPTS) prepare
	+$(call echo_cmd,BUILD IHK,$(TARGET))$(submake) -C $(IHKBASE) $(SUBCMD_OPTS)

%.o: $(SRC)/%.c
	$(cc_cmd)

gencore.o: ../arch/arm64/kernel/gencore.c
	$(cc_cmd)

FORCE:

-include Makefile.dep
