# make template for src's subdirectories.  It contains definitions for
# standard targets.

M4=/local/gnu/m4
PERL=perl

AR=/usr/bin/ar
#ARL=l
RANLIB=/usr/bin/ranlib

SYSTEM=CFront

all: headers lib

# we will not use Sun's target definitions
###   include make/default.mk
.SUFFIXES: ${SUFFIXES} .C .m4c .m4h
OUTPUT_OPTION=
COMPILE.cc=$(CCC) $(CCFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
LINK.cc=$(CCC) $(CCFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)
.C:
	$(LINK.cc) -o $@ $< $(LDLIBS)
.C.o:
	$(COMPILE.cc) $(OUTPUT_OPTION) $<
.c:
	$(LINK.cc) -o $@ $< $(LDLIBS)
.c.o:
	$(COMPILE.cc) $(OUTPUT_OPTION) $<

ifeq (1,0)
.m4h.h:
	rm -f $@
	${M4} defines.m4h $< | cat -s > $@
	chmod a-w $@

.m4c.c:
	rm -f $@
	${M4} defines.m4c $< | cat -s > $@
	chmod a-w $@
endif

VISIONDIR=/cis/vision/src/iac++

INCDIR=${TOP}/include

EXPDIR=/cis/lightning2/thoth.aux/gcc-bunker
#CCC=${EXPDIR}/g++ -B${EXPDIR}/ -Wall
#CCC=g++ -Wall
#CCC=/local/bin/CC
#CCC=/usr/lang/CC-3.0.2/CC

ifeq (${SYSTEM},CFront)
CCC=/local/bin/CC-3.0.3
SYSFLAGS=-D__CFRONT__=3
else
ifeq (${SYSTEM},GNU)
CCC=/cis/research/iac++/gcc-bunker/bin/g++
SYSFLAGS=-fno-implicit-templates
else
CCC=/bin/false
endif	#GNU
endif	#CFront



ifeq (${SYSTEM},CFront)
CSOURCES=${patsubst %.o,%.c,${LOBJS} ${CLOSUREOBJS}}
else
CSOURCES=${patsubst %.o,%.c,${LOBJS}}
endif	#CFront

INCLUDEFLAGS= -I. -I${INCDIR} ${EXTRAINCLUDES}
CDEBUGFLAGS=-g
CCFLAGS= ${SYSFLAGS} ${CDEBUGFLAGS} ${INCLUDEFLAGS} ${XTRAFLAGS} ${XTRACCFLAGS}

.PHONY: all clean spotless headers

##
##

# GURK = RCS/%,v
# # there's a bloody comma in the pattern :(
# RCSFILES = ${patsubst ${GURK},%,${wildcard RCS/*,v}}

# checkout: ${RCSFILES}
#
# ${RCSFILES}: %: RCS/%,v
#	co $@

##
##

headers-force:: ${HEADERS}
	install -m0444 ${HEADERS} ${INCDIR}/

OTHERHEADERS = ${patsubst %,${INCDIR}/%,${HEADERS}}

headers: ${OTHERHEADERS}

ifneq (${OTHERHEADERS},)
${OTHERHEADERS}: ${INCDIR}/%: %
	rm -f $@
	install -m0444 $< $@
endif

##
##

WORKLIB = ${TOP}/lib/libiac++.a
ifneq (${LOBJS},)

lobjs: ${LOBJS}

lar: lobjs
	${AR} rv${ARL} ${WORKLIB} ${LOBJS}
	${RANLIB} ${WORKLIB}
	touch $@

lib: lar
	touch $@

endif

LDLIBS = -L${TOP}/lib ${OTHERLIBS} -liac++ \
	-lcomplex -lm

##
## How to generate closures of a library (pre-instantiate all the templates)
##

ifeq (${SYSTEM},CFront)

#
# CFront closure generation
#

CLOSURECC=${CCC} -ptk -ptmlinkmap -ptdclosure
CLOSURELIBS=-lcomplex -lm

closure: lobjs ${CLOSUREOBJS}
	if [ -d ptrepository ] ; then \
	( ${CLOSURECC} ${CLOSUREARGS} ${CCFLAGS} ${LOBJS} ${CLOSUREOBJS} ${CLOSURELIBS} ) ; \
	rm -f a.out ; \
	if [ ! -z "${BLASTABLE_PTFILES}" ] ; then \
		for i in ${BLASTABLE_PTFILES} nonexistent; do \
			rm -f ptrepository/$$i*.o; \
		done; \
	fi; \
	find ptrepository -name \*.o -print | sort | ${PERL} ${TOP}/bin/add-templobj.perl -${ARL} ${WORKLIB} ; \
	${RANLIB} ${WORKLIB} ; \
	fi
	touch closure

else	#CFront
ifeq (${SYSTEM},GNU)

#
# GNU C++ closure generation
#

INSTDESC=inst.desc
INSTDIR=g++inst

closure:	${INSTDESC}
	if [ -f ${INSTDESC} ]; then \
	g++ -E -x c++ ${INSTDESC} | CCC="${CCC}" ${PERL} ${TOP}/bin/g++-instantiate.perl -repository ${INSTDIR} ${CCFLAGS} && \
	find ${INSTDIR} -name \*.o -print | sort | ${PERL} ${TOP}/bin/add-templobj.perl -${ARL} ${WORKLIB} && \
	${RANLIB} ${WORKLIB} ; \
	fi
	touch closure

else	#GNU

#
# well, no way to make a closure
#

endif	#GNU
endif	#CFront


##
##
##

ifneq (${SIMPLETESTS},)
${SIMPLETESTS}: test%: test%.o lib
	${LINK.cc} -o $@ $< ${LDLIBS}
endif

testprogs: ${TESTS}

##
##
##

dist:
	cp ${DISTFILES} ${DISTDIR}/src/${SUBDIR}/

clean::
	rm -f ${LOBJS} *.o *~ core

spotless:: clean
	rm -f ${TESTS} lib lar lobjs closure *.bak make.log

ifeq (${SYSTEM},CFront)
spotless:: clean
	rm -rf ptrepository linkmap
else	#CFront
ifeq (${SYSTEM},GNU)
spotless:: clean
	rm -rf g++inst
else	#GNU
# do nothing
endif	#GNU
endif	#CFront


depend:
	awk '/^# dependencies/ { exit 0; } {print $0; } END{print "# dependencies";}' < GNUmakefile > GNUmakefile.new
	g++ -MM -I/local/include/CC/ ${INCLUDEFLAGS} ${CSOURCES} | \
		${PERL} -e '$$pattern="${INCDIR}"; while (<>) {s/$$pattern/\$${INCDIR}/g; print;}' >> GNUmakefile.new
	mv GNUmakefile GNUmakefile.bak
	mv GNUmakefile.new GNUmakefile
