#
# Copyright (c) 2001 Atmark Techno, Inc.  All Rights Reserved.
#

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

.PROFILE := $(PROFILE)

status :=
error :=

#
# Configure settings based on the target passed from the top-level
# makefile.  The PROFILE := armadillo 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

#
# Configuration for standalone, optimized ARM7TDMI cross-compile.
#
BUILD_NAME := $(shell $(NCVT) -c "-" loader $(TARGET) $(.PROFILE) v$(HERMIT_VERSION))
BUILD_TARGETS := $(BUILD_NAME).bin

CROSS_COMPILE ?= arm-linux-
GCC_VER := $(shell $(CROSS_COMPILE)gcc -dumpversion | sed "s/\..*//")
INCLUDEDIR := ../../../include
CC := $(CROSS_COMPILE)gcc
CFLAGS := -Wall -O2 -fomit-frame-pointer -I$(INCLUDEDIR) -nostdlib \
	-mcpu=arm7tdmi -static
CFLAGS += -DARMADILLO -DENABLE_MEDIUM_ETHERNET
CFLAGS += -DTARGET_PROFILE=\"$(current-profile)\"

#
# The LDSCRIPT variable can be overridden based on the target-derived
# settings.
#
LDSCRIPT := flash
OBJSECTIONS := .boot .text .data .bss
OBJCOPYFLAGS := -O binary -S $(addprefix -j ,$(OBJSECTIONS))

#ASMS := boot.S lib1funcs.S
ASMS := boot.S
SRCS := entry.c loader.c memmap.c linux.c eth.c ide.c medium_io.c
SRCS += ../memzero.c ../medium.c ../frob.c ../setenv.c ../gunzip.c
SRCS += ../flash/flash_core.c ../flash/flash_amd.c

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

ifeq (, $(.PROFILE))
	ASMS += lib1funcs.S
	CFLAGS += -DCONSOLE=0 -DRELOCATE -DSTANDALONE
	status := profile-detect
endif

ifeq (eabi-debug, $(.PROFILE))
	CFLAGS += -gdwarf-2
	CFLAGS += -DCONSOLE=0 -DRELOCATE -DSTANDALONE
	status := profile-detect
endif

ifeq (ttyAM1, $(.PROFILE))
	ASMS += lib1funcs.S
	CFLAGS += -DCONSOLE=1 -DRELOCATE -DSTANDALONE
	status := profile-detect
endif

ifeq (notty, $(.PROFILE))
	ASMS += lib1funcs.S
	CFLAGS += -DRELOCATE -DSTANDALONE
	status := profile-detect
endif

ifeq (boot, $(.PROFILE))
	ASMS := init.S lib1funcs.S
	CFLAGS += -DCONSOLE=0 -DBOOTMODE -DSTANDALONE
	LDSCRIPT := bootmode
	status := profile-detect
endif

ifeq (, $(error))
ifeq (, $(status))
        error := profile-unknown
endif
endif

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

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

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: $(wildcard loader.lds.*)
	cp loader.lds.$(LDSCRIPT) $@

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

# 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) -m 644 -o root -g root \
                $(BUILD_TARGETS) \
                $(INSTALLPREFIX)/lib/hermit/

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