# Copyright (c) 1997-2005 by Wayne C. Gramlich.
# All rights reserved.

ROOT = ..

include $(ROOT)/Makefile_common.mk
include $(ROOT)/Makefile_private.mk

STC = ../stipple/stc/$(PLATFORM)/stc
STL = ../stipple/stc/$(PLATFORM)/stl

STC_INCLUDES =					\
	-I ../stipple/libstipple/$(PLATFORM)	\
	-I $(PLATFORM)
STC_FLAGS = $(STC_OPTIONS) $(STC_INCLUDES)

NSTC = ../stipple/nstc/$(PLATFORM)/nstc

MCS51_BASES :=		\
	bogus		\
	code		\
	code_types	\
	mcs51		\
	opcodes		\
	program		\
	types
MCS51_SOURCES := ${MCS51_BASES:%=%.sts}
MCS51_INTERFACES := ${MCS51_BASES:%=$(PLATFORM)/%.sti}
MCS51_OBJECTS := ${MCS51_BASES:%=$(PLATFORM)/%.o}
MCS51_HTMLS := ${MCS51_BASES:%=%.html}

TEST_BASES :=		\
	span
TEST_PROGRAMS := ${TEST_BASES:%=$(PLATFORM)/%_test}
TEST_TESTS := ${TEST_BASES:%=$(PLATFORM)/test_%}

PROGRAMS =		\
	${TEST_PROGRAMS}	

OTHER_SOURCES =			\
	Makefile
SOURCES =			\
	${MCS51_SOURCES}	\
	${MCS51_HTMLS}		\
	${OTHER_SOURCES}
ALL_SOURCES =			\
	${SOURCES}		\
	index.html		\
	source.html

# Some targets:

.PHONY: all deep everything_else clean site spell

all: $(PLATFORM) ${SOURCES} $(WAIT) $(PLATFORM)/libmcs51.stlib \
    $(WAIT) ${PROGRAMS} $(WAIT) ${MCS51_HTMLS} $(WAIT) source.html $(WAIT) \
    ${TEST_TESTS}

deep: everything_else $(WAIT) all $(WAIT)

everything_else:
	cd ../libutil ; $(MAKE)
	cd ../stipple ; $(MAKE)

# Test programs:

$(PLATFORM)/test_span: $(PLATFORM)/span_test
	$(PLATFORM)/span_test -l - > $(PLATFORM)/span_test.out 2>&1
	cmp $(PLATFORM)/span_test.out span_test.out
	touch $@

$(PLATFORM)/span_test: $(PLATFORM)/span_test.sti $(PLATFORM)/span_test.o \
    $(PLATFORM)/libmcs51.stlib
	rm -f $@
	$(STL) -o $@ -E $(PLATFORM) $(PLATFORM)/span_test.sti \
	    $(PLATFORM)/span_test.o `cat $(PLATFORM)/libmcs51.stlib`


# Library:

$(PLATFORM)/libmcs51.stlib: ${MCS51_INTERFACES} ${MCS51_OBJECTS}
	rm -f $@
	ls ${MCS51_INTERFACES} ${MCS51_OBJECTS} | sed "s,^,`pwd`/,g" > $@

#asm: $(ASSEMBLER)
#	ln -s $(ASSEMBLER)

#$(ASSEMBLER): ${ASSEMBLER_INTERFACES} $(WAIT) ${ASSEMBLER_OBJECTS}
#	$(STL) -o $@ -E $(PLATFORM) ${ASSEMBLER_INTERFACES} \
#		${ASSEMBLER_OBJECTS}

clean: site
	rm -f ${MCS51_HTMLS} source.html
	rm -f core `ls | grep "~$$"`

site:
	rm -rf $(PLATFORM) STDB

source.html: ${MCS51_SOURCES} ${MCS51_HTMLS} Makefile
	@echo -n "Generating $@..."
	@rm -f $@
	@echo '<HTML>' > $@
	@echo '<Head>' >> $@
	@echo '<Title>MCS-51 Library Source Files</Title>' >> $@
	@echo '</Head>' >> $@
	@echo '<Body>' >> $@
	@echo '<P>These are the source files for the' >> $@
	@echo '<A HRef="index.html">MCS-51 Library</A>.</P>' >> $@
	@echo '<H1>' >> $@
	@echo 'MCS-51 Library Source Files' >> $@
	@echo '</H1>' >> $@
	@echo '<P>The following extracted documentation is available:</P>'>> $@
	@echo '<UL>' >> $@
	@for i in ${MCS51_BASES} ; do					\
		echo "  <LI><A HRef='$$i.html'>$$i</A></LI>" >> $@ ;	\
		done
	@echo '</UL>' >> $@
	@echo '<P>The following source files are available:</P>' >> $@
	@echo '<UL>' >> $@
	@for i in Makefile ${MCS51_SOURCES} ; do			\
		echo "  <LI><A HRef='$$i'>$$i</A></LI>" >> $@ ;	\
		done
	@echo '</UL>' >> $@
	@echo '<HR>' >> $@
	@echo '<Address>' >> $@
	@echo '<A HRef="../../copyright.html">' >> $@
	@echo 'Copyright</A> (c) 1997, 1998 by' >> $@
	@echo '<A HRef="../../gramlich.html">' >> $@
	@echo 'Wayne Gramlich</A> All rights reserved.' >> $@
	@echo '</Address>' >> $@
	@echo '</Body>' >> $@
	@echo '</HTML>' >> $@
	@echo "Done"

