#
# Copyright (c) 2006 Atmark Techno, Inc.  All Rights Reserved.
#
# The default target of this Makefile is...
all::

export TARGET ?= armadillo
export PROFILE ?= eabi-debug

export INSTALL := install
INSTALLPREFIX := /usr/local
MANPREFIX := $(INSTALLPREFIX)/man

#
# Fail to build unless 'make scrub' is run between targets.  This
# minimizes the chance that stale object files will be incorporated
# into a target build.
#
ifneq (,$(wildcard target-stamp))
	last-target := $(shell cat target-stamp)
	ifneq ($(TARGET), $(last-target))
		error := target-error
	endif
endif


HERMIT-VERSION-FILE: .FORCE-HERMIT-VERSION-FILE
	@$(SHELL_PATH) ./GIT-VERSION-GEN
-include HERMIT-VERSION-FILE
export HERMIT_VERSION

.PHONY: $(error) all host target .FORCE-HERMIT-VERSION-FILE
all:: $(error) host target

target-error:
	@echo "previous TARGET:" `cat target-stamp`
	@echo "target-stamp mismatch; 'make scrub' to switch targets"; false
target-stamp:
ifeq ($(TARGET), armadillo)
	@ln -s cs89712 include/arch
endif
ifeq ($(TARGET), armadilloj)
	@ln -s netarm include/arch
endif
ifeq ($(TARGET), armadillo9)
	@ln -s ep93xx include/arch
endif
ifeq ($(TARGET), armadillo2x0)
	@ln -s ep93xx include/arch
endif
ifeq ($(TARGET), armadillo3x0)
	@ln -s ns9750 include/arch
endif
ifeq ($(TARGET), armadillo5x0)
	@ln -s mx3 include/arch
endif
ifeq ($(TARGET), suzaku)
ifeq ($(PROFILE) , powerpc)
	@ln -s powerpc include/arch
else
	@ln -s microblaze include/arch
endif
endif
####### cleate stamp
	@echo $(TARGET) > $@
	@ln -s $(TARGET) src/target/arch


# No target-specific host parts yet (probably will be someday).
host: HERMIT-VERSION-FILE
	$(MAKE) -C src/host/common

target: build-common build-$(TARGET)
build-$(TARGET): build-common
build-%: HERMIT-VERSION-FILE target-stamp
	$(MAKE) -C src/target/$(subst build-,,$@) all

.PHONY: clean host-clean
clean: host-clean clean-common clean-$(TARGET)
	rm -f HERMIT-VERSION-FILE
host-clean:
	$(MAKE) -C src/host/common clean
clean-%:
	$(MAKE) -C src/target/$(subst clean-,,$@) clean

.PHONY: scrub host-scrub
scrub: host-scrub scrub-common scrub-$(TARGET)
	rm -f HERMIT-VERSION-FILE
	rm -f target-stamp
	rm -f src/target/arch include/arch
host-scrub:
	$(MAKE) -C src/host/common scrub
scrub-%:
	$(MAKE) -C src/target/$(subst scrub-,,$@) scrub

.PHOHY: distclean host-distclean
distclean: distclean-$(TARGET) host-distclean
	rm -f *~
host-distclean:
	$(MAKE) -C src/host/common distclean

distclean-%:
	$(MAKE) -C src/target/$(subst distclean-,,$@) distclean

# canonicalize install prefix
installpath := $(shell cd $(INSTALLPREFIX); pwd)

.PHONY: install host-install
install: all host-install install-$(TARGET)
#	set -e ; for man1page in doc/*.1 ; do			\
#		MAN1DIR=$(MANPREFIX)/man1 ;			\
#		$(INSTALL) -d $$MAN1DIR/ ;			\
#		$(INSTALL) -m 644 $$man1page $$MAN1DIR/ ;	\
#		gzip -9 $$MAN1DIR/`basename $$man1page` ;	\
#	done
host-install: host
	$(MAKE) -C src/host/common INSTALLPREFIX=$(installpath) install
install-%: build-%
	$(MAKE) -C src/target/$(subst install-,,$@) \
		INSTALLPREFIX=$(installpath) install

