#
# Copyright (c) 2003-2005 Atmark Techno, Inc.  All Rights Reserved.
#

#
# Guarantee that the top-level TARGET variable, if set, is microblaze.
#
ifeq (, $(TARGET))
	TARGET := suzaku
endif
ifeq (, $(PROFILE))
	.PROFILE := microblaze
else
	.PROFILE := $(PROFILE)
endif

error :=

#
# Configure settings based on the target passed from the top-level
# makefile.  The PROFILE := suzaku line is just a default and can
# be overridden from the command line.
#
NCVT = ../../../tools/ncvt
current-profile := $(shell $(NCVT) $(TARGET) $(.PROFILE))
ifneq (,$(wildcard profile-stamp))
	previous-profile := $(shell cat profile-stamp)
	ifneq ($(current-profile), $(previous-profile))
		error := profile-error
	endif
endif

BUILD_NAME := $(shell $(NCVT) -c "-" loader $(TARGET) $(.PROFILE) v$(HERMIT_VERSION))
SREC_4M := $(shell $(NCVT) -c "-" $(BUILD_NAME) 4M).srec
SREC_8M := $(shell $(NCVT) -c "-" $(BUILD_NAME) 8M).srec
BUILD_TARGETS := $(BUILD_NAME).bin

#
# Configuration for SUZAKU
#

-include Makefile.$(.PROFILE)

ifneq ($(profile_detect), yes)
error := profile-unknown
endif

GCC_VER := $(shell $(CROSS)gcc -dumpversion | sed "s/\..*//")

CC := $(CROSS)gcc
CFLAGS := -Wall -O2 -fomit-frame-pointer -I../../../include -nostdlib \
	  -DHERMIT_VERSION='"$(HERMIT_VERSION)"' \
	  -static $(EXTRA_ASM_FLAGS)
ifeq ($(.PROFILE),microblaze)
CFLAGS += -mxl-barrel-shift -mno-xl-soft-div -mno-xl-soft-mul \
	  -mxl-pattern-compare
else
CFLAGS += -m32 -mmultiple -mstring -Wa,-m405
endif
CFLAGS += -DTARGET_PROFILE=\"$(current-profile)\"
CFLAGS += -DSUZAKU

OBJSECTIONS := .boot .text .data .bss
OBJCOPYFLAGS := -O binary -S $(addprefix -j ,$(OBJSECTIONS))

ASMS := boot-$(.PROFILE).S
SRCS := entry.c loader.c memmap.c linux.c mode.c flash_region.c medium_io.c cache.c
SRCS += ../memzero.c ../medium.c ../frob.c ../gunzip.c
SRCS += ../flash/flash_core.c ../flash/flash_amd.c ../flash/flash_spi.c
SRCS += ../md5sum.c

ifeq ($(GCC_VER), 2)
	SRCS += ../flash/flash_intel_dummy.c
endif

OBJS := $(SRCS:.c=.o)
DEPS := $(SRCS:.c=.d)
LIBS := -ltarget -lgcc

all: $(error) profile-stamp $(BUILD_TARGETS) s-record

profile-error:
	@echo "profile-stamp mismatch; 'make scrub' to switch profiles"
	@echo "  previous PROFILE:" $(previous-profile)
	@echo "   current PROFILE:" $(current-profile)
	@false
profile-unknown:
	@echo "unknown PROFILE: $(.PROFILE)"
	@false
profile-stamp:
	@echo "$(current-profile)" > $@

# Hermit second stage loader (ELF image)
$(BUILD_NAME).elf: $(ASMS) $(OBJS) ../common/libtarget.a Makefile $(BUILD_NAME).lds
	$(CC) $(CFLAGS) -L. -L../common \
		-Wl,-Map,$(BUILD_NAME).map \
		-Wl,-T$(BUILD_NAME).lds -o $@ \
		$(ASMS) $(OBJS) \
		-Wl,--start-group $(LIBS) -Wl,--end-group

$(BUILD_NAME).lds: loader.lds
	$(CPP) -P -x assembler-with-cpp -I../../../include $< > $@

%.bin: %.elf
	$(CROSS)objcopy $(OBJCOPYFLAGS) $< $@

$(SREC_4M): $(BUILD_NAME).bin
	$(CROSS)objcopy -I binary -O srec --adjust-vma=$(SREC_4M_VMA) $< $@
$(SREC_8M): $(BUILD_NAME).bin
	$(CROSS)objcopy -I binary -O srec --adjust-vma=$(SREC_8M_VMA) $< $@

# automated dependency checking
ifeq (,$(filter clean scrub,$(MAKECMDGOALS)))
-include $(DEPS)

DEPENDS := ../../../depend.sh
%.d: %.c
	env CC=$(CC) $(DEPENDS) $< $@ $(CFLAGS)
endif

# installation
PHONY: install
install: install-s-record
	$(INSTALL) -m 644 -o root -g root \
                $(BUILD_TARGETS) \
                $(INSTALLPREFIX)/lib/hermit/

# housecleaning
.PHONY: clean
clean:
	rm -f $(OBJS) loader-*.lds *.elf *.bin *.map *.srec
scrub: clean
	rm -f profile-stamp 
	rm -f *.d ../*.d ../flash/*.d
	rm -f *.o ../*.o ../flash/*.o
distclean: scrub
	rm -f *~
