# tag: Makefile for the forth system
#
# Copyright (C) 2003 Patrick Mauritz, Stefan Reinauer
#
# See the file "COPYING" for further information about
# the copyright and warranty status of this work.
#

ARCH     := $(shell uname -m | sed -e s/i.86/x86/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/ -e s/x86_64/amd64/)
TOPDIR   := $(shell /bin/pwd)
BUILDDIR ?= $(TOPDIR)/obj-$(ARCH)
VPATH    := $(TOPDIR)/include:$(TOPDIR)/util:$(BUILDDIR)

include $(TOPDIR)/Rules.make

CFLAGS_DEBUG := -Wall -Wstrict-prototypes -Wshadow

CFLAGS       ?= -Os -fcaller-saves -fomit-frame-pointer $(CFLAGS_ARCH) $(CFLAGS_DEBUG)

export ARCH TOPDIR VPATH CFLAGS BUILDDIR

all: main unix dictionary $(ARCH) forth.html plugins
	@echo -e "\nOpenBIOS build finished for platform $(ARCH).\n"

.PHONY: kernel unix $(ARCH) plugins

types.h: types.sh
	@echo -n "Checking types..."
	@cd $(BUILDDIR) && $(TOPDIR)/util/types.sh

bin2hex: bin2hex.c
	@echo -n "Building binary converter..."
	@cd $(BUILDDIR) && $(CC) -o bin2hex $(TOPDIR)/util/bin2hex.c
	@echo -e "\t\tok"

kernel:
	@$(MAKE) -s -C kernel 

main:
	@echo -e "\nWelcome to OpenBIOS.."
	@test -r $(BUILDDIR) || ( mkdir -p $(BUILDDIR); \
	  echo -e "\nCreating build directory $(BUILDDIR)" )

$(ARCH): types.h bin2hex dictionary
	@$(MAKE) -s -C arch/$(ARCH)

unix: types.h kernel
	@$(MAKE) -s -C arch/unix

dictionary:
	@$(MAKE) -s -C forth

plugins:
	@$(MAKE) -s -C plugins
	
clean:
	@test ! -d $(BUILDDIR) && \
		echo "Architecture $(ARCH) is already clean." || \
		( 						 \
			echo "Cleaning up architecture $(ARCH)"; \
			rm -rf $(BUILDDIR)			 \
			rm forth.dict.core			 \
		)


forth.html: unix dictionary arch
	@echo -en "\nCreating forth.html..."
	@cd $(BUILDDIR) && (						\
	echo '<html><head><title>SOB - State of OpenBIOS</title></head><body style="background-color:#000000;color:#00ff00;font-size:10px"><pre>' >forth.html; \
	echo "`hostname`% ls -al openbios unix bootstrap.dict" >>forth.html; \
	ls -al openbios unix bootstrap.dict >> forth.html;		\
	echo "`hostname`% ./unix bootstrap.dict" >>forth.html;		\
	./unix bootstrap.dict < $(TOPDIR)/forth/hayes.fs | 		\
			fold -w80 >> forth.html;			\
	echo '</pre></body></html>' >> forth.html;			\
	)
	@echo -e "    \t\tok"

run: main unix dictionary
	@echo
	@cd $(BUILDDIR) && ./unix ./openfirmware.dict

indent:
	find -path './{arch}' -prune -o -name '*.[ch]' \
				-exec indent -kr -i8 {} \;
	find -name '*~' -exec rm {} \;

