##########################################################################
#
# $Header: makemeta,v 0.8 85/11/13 12:49:54 neil Exp $
#
# $Locker:  $
#
# $Log:	makemeta,v $
#	Revision 0.8  85/11/13  12:49:54  neil
#	VERSION and VERNUM separated
#	
#	Revision 0.7  85/10/09  18:25:56  kodiak
#	add *.cat *.xref to make clean::
#	
#	Revision 0.6  85/10/01  18:54:45  kodiak
#	remove .crec & .srec warning messages
#	
#	Revision 0.5  85/10/01  18:13:05  kodiak
#	provide else clause so no error is generated for conditional CAPP step
#	
#	Revision 0.4  85/10/01  14:25:41  kodiak
#	add CAPP step for rom users
#	
#	Revision 0.3  85/09/17  17:07:29  kodiak
#	fix reference to imprintlis in print command
#	
#	Revision 0.1  85/08/26  10:27:13  kodiak
#	upgrade to version 0 (from version 29)
#	
#
##########################################################################

#
#	Define the following to use this makefile
#
#  SUBSYSNAME	The name that will be associated with the .ld, .srec,
#		... files to be produced.
#
#  SSTART	The start address for creation of $SUBSYSNAME.srec files
#
#  AFILES	All the assembler source files
#
#  CFILES	All the C source files
#
#  FIRSTO	The first object file to appear in library or the load 
#		file generated by this makefile, but the last dependency
#		to be resolved.  This means the source for this file will
#		be compiled or assembled last.  Not to be confused with
#		STARTUP, which goes before this if it exists, but will
#		always be an .obj file that is not compiled or assembled
#		here.
#  OFILES	All the object files that make either the library or the
#		load file generated by this makefile, whose source appears
#		in this directory, with the exception of FIRSTO.
#  MAKEFILE	The name of the Makefile that contains the above items,
#		so that a new $SUBSYSNAME.with file can be generated when
#		the Makefile changes.
#
#		
#	Define the following to generate public code (e.g. roms)
#
#  SUBSYSGROUP	The group within .../include/ that the following external
#		include files should be placed in
#
#  EXTERNIFILES	All the assembler include files released to others in
#		INCLDIR
#
#  IFILES	All the private assembler include files used by this code
#		(i.e. found in this directory but not released to others),
#		except any _rev.i files
#
#  EXTERNHFILES	All the C include files released to others in INCLDIR
#		(e.g. ${VERDIR}/${TERNAL}/include/exec)
#
#  HFILES	All the private C include files used by this code (i.e.
#		found in this directory but not released to others),
#		except any _rev.h files
#
#
#	Define the following variables to futher effect the rules:
#
#  SRCDIRPATH	The major directory name that the SRCDIR is within,
#		i.e. a directory at the nominal SRCDIR level that is
#		the parent of this SRCDIR, used for source storage
#		only.
#  CSTART	The start address for creation of $SUBSYSNAME.crec files,
#		if not equal to SSTART-$C0000
#  MYLIBS	Additional libraries to search before the system library
#		to resolve dependencies
#  MYINCL	An additional include directory to search after the
#		current directory but before the default system directory.
#		Only one directory can be specified.
#  CFLAGS	Additional options for C compilation.  Use MYINCL= and
#		SYSINCL= to change -I flags.
#  AFLAGS	Additional options to the Assembler. For example
#		"-l $*.lis" to generate a listing file.  Use MYINCL= and
#		SYSINCL= to change -I flags.
#  CAFLAGS	Additional options for C compilation, so that specific
#		assembler flags may be passed through to the assembler. (@@)
#  LFLAGS	Additional options to the linker.
#		For example "map=$*.map" or "xref=$*.xref".
#  MUNGEFLAGS	Additional options to mungesrec.
#		For example "-k 256"
#  STARTUP	An .obj file to be linked to the start of the link.  Use
#		${LIBDIR}/startup.obj for C
#		running under DOS, nodos_startup.obj when not under DOS,
#		and wbstartup.obj when running under WorkBench.
#
#	Override the following variables from the make command itself:
#
#  VERSION	The version of code being produced, and the suffix of 
#		the VERDIR directory /usr/commodore/amiga/V${VERSION}
#  VERNUM	The software version that should be in libraries and
#		devices.  This specifies software compatibility.
#		Anyone who uses the automatic <subsys>_rev.i file
#		can pick this up by automatically.  Others should use
#		use this variable to define something in their subsystem
#		explicitly.
#  VERDIR	The directory within which SRCDIR, INCLDIR, LIBDIR,
#		ROMDIR, and CARTDIR live, and which contains the system
#		include directory
#  TERNAL	Whether to use .../internal/... or .../external/...
#  SYSINCL	The system include directory
#  SYMBOLOPT	By default, we have symbolic hunks turned on in the
#		assembler.  Override SYMBOLOPT to turn this off
#		(it makes your object modules 60% smaller).
#



VERSION=	33
VERNUM=		1
VERDIR=		/usr/commodore/amiga/V${VERSION}
TERNAL=		internal
RELNODE=	amiga

SRCPATH=	${VERDIR}/${TERNAL}/src/${SRCDIRPATH}/${SRCDIR}
INCLDIR=	${VERDIR}/${TERNAL}/include/${SUBSYSGROUP}
LIBDIR=		${VERDIR}/${TERNAL}/lib
ROMDIR=		${VERDIR}/${TERNAL}/roms
DOCDIR=		${VERDIR}/${TERNAL}/doc
DISKDIR=	${VERDIR}/${TERNAL}/disk

LFILES=		${MYLIBS} \
		${VERDIR}/${TERNAL}/lib/amiga.lib

SYSINCL=	${VERDIR}/${TERNAL}/include

AINCL=		-I -I${MYINCL} -I${SYSINCL}
CINCL=		-I -I${MYINCL} -I${SYSINCL}

SYMBOLOPT=	,S
#
# where to find commands
#
CPP=		/lib/cpp
ASM=		assem
CC=		metacc
LINK=		/usr/commodore/bin/alink
CONVERT=	convert
SREC=		absload
MAKE=		make
MAKEVERS=	makevers
MUNGE=		mungesrec
NEWROMS=	/usr/commodore/bin/newroms

.SUFFIXES:
.SUFFIXES:	.h .i .fd .rev .odd .even .smap .srec .cmap .crec .dl \
		.ld .lmap .obj .c .asm

.INCLUDE=${SUBSYSNAME}.depend

#
# generic rules for how to make files
#
.c.obj:
	@echo ${CFLAGS} $<:
	@${CC} -S -X70 ${CFLAGS} ${CINCL} $*.c
	-@if test "${CAPP}"; \
	    then ${CAPP} < $*.asm > $*.asm.TMP; \
	    else cp $*.asm $*.asm.TMP; \
	    fi
	@${ASM} $*.asm.TMP -o $*.obj.TMP -c q,a${SYMBOLOPT} ${CAFLAGS}
	@rm -f $*.asm $*.asm.TMP
	@mv -f $*.obj.TMP $@

.asm.obj:
	@echo $<:
	@rm -f $*.asm.TMP
	-@if test "${APP}"; \
	    then ${APP} < $< > $*.asm.TMP; \
	    else cp $< $*.asm.TMP; \
	    fi
	@${ASM} $*.asm.TMP -o $*.obj.TMP -c q,a${SYMBOLOPT} ${AFLAGS} \
	    -i \"${MYINCL} ${SYSINCL}\"
	@rm -f $*.asm.TMP
	@mv -f $*.obj.TMP $@

.ld.dl:
	@rm -f $@
	${CONVERT} <$< >$@

.ld.srec .ld.smap:
	${SREC} -m -o $*.srec -T ${SSTART} -x -s $< >$*.smap

.ld.crec .ld.cmap:
	@echo "[+]sa16o16i${SSTART} _C0000 ${CSTART}z2=apq" >dc.TMP
	@echo "${SREC} -m -o $*.crec -T `dc dc.TMP` -x -s $< >$*.cmap"
	@${SREC} -m -o $*.crec -T `dc dc.TMP` -x -s $< >$*.cmap
	@rm dc.TMP

.srec.even:
	${MUNGE} -s ${START} ${MUNGEFLAGS} -e $< > $@.TMP
	mv $@.TMP $@

.srec.odd:
	${MUNGE} -s ${START} ${MUNGEFLAGS} -o $< > $@.TMP
	mv $@.TMP $@

.rev.i .rev.h:
	@rm -f $@
	${MAKEVERS} ${VERNUM} `basename $* _rev` _rev

.fd.i:
	@echo $@:
	@rm -f $@
	@emacs -l${VERDIR}/tools/gen-lib -egen-lib $* i

clean::	${SUBDIRS}
	rm -f *.obj *.ld *.[cs]rec *TMP[' '0-9] *.lis *.xr0 *.[csl]map \
	    *.even *.odd *.CKP *.BAK *.TMP *.TMP? awkvers.* *.with *.depend \
	    *.cat *.xref
	rm -rf TMPD

${SUBSYSNAME}.with with:	${SUBDIRS} ${MAKEFILE}
	@echo ${SUBSYSNAME}.with:
	-@if test -z "${WITH}"; \
	    then( \
	    cat < /dev/null > TMP1; \
	    for i in ${STARTUP} ${FIRSTO} ${OFILES} TMP1 ; do \
		( echo $$i | sed -n -e "s/\.obj$$/.obj,*/p" >> TMP1 ); done; \
	    sed -e "\$$s/,\*$$//" TMP1 > TMP2; \
	    echo "FROM *" > TMP1; \
	    cat TMP1 TMP2 > ${SUBSYSNAME}.with.new; \
	    cat < /dev/null > TMP1; \
	    for i in ${LFILES} TMP1 ; do \
		( echo $$i | sed -n -e "s/\.lib$$/.lib,*/p" >> TMP1 ); done; \
	    sed -e "\$$s/,\*$$//" TMP1 > TMP2; \
	    echo "LIBRARY *" > TMP1; \
	    cat TMP1 TMP2 >> ${SUBSYSNAME}.with.new; \
	    rm -f TMP1 TMP2 ${SUBSYSNAME}.with.new; \
	    ); \
	    else( \
	    cat ${WITH} > TMP1; \
	    cat TMP1 > ${SUBSYSNAME}.with; \
	    rm -f TMP1 \
	    ); fi

${SUBSYSNAME}.depend depend::	${SUBDIRS}
	@echo ${SUBSYSNAME}.depend:
	@echo "# DO NOT CHANGE THIS FILE" > ${SUBSYSNAME}.depend.new
	@rm -f TMP2
	@makeinclude -a ${AFILES} TMP2 -i . ${MYINCL} ${SYSINCL} | \
	    sed -e "s/\.asm:/.obj:/" > TMP1
	@makeinclude -c ${CFILES} TMP2 -i . ${MYINCL} ${SYSINCL} | \
	    sed -e "s/\.c:/.obj:/" >> TMP1
	@echo "DEPENDS= " > TMP2
	@sed -e "s/^.*:[ 	]/	/" TMP1 | sort -u >> TMP2
	@sed -e "\$$!s/$$/ \\\\/" TMP2 >> ${SUBSYSNAME}.depend.new
	@cat TMP1 >> ${SUBSYSNAME}.depend.new
	@echo '# DO NOT ADD DEPENDANCIES HERE!' >> ${SUBSYSNAME}.depend.new
	@echo '# THEY WILL BE REMOVED.' >> ${SUBSYSNAME}.depend.new
	@rm -f TMP1 TMP2
	@mv -f ${SUBSYSNAME}.depend.new ${SUBSYSNAME}.depend

headers::	${SUBDIRS}
#	# copy over the external includes
	-rsh ${RELNODE} mkdir ${INCLDIR}
	diffinstall ${RELNODE} ${INCLDIR} ${EXTERNHFILES} ${EXTERNIFILES}
#	# now, if this is a library, copy over the library interface function
#	# description.  Note that this will not make it
	-if test -r ${SUBSYSNAME}_lib.fd; \
	    then ( \
	    rsh ${RELNODE} mkdir ${LIBDIR}/${SUBSYSNAME}; \
	    diffinstall ${RELNODE} ${LIBDIR}/${SUBSYSNAME} \
		${SUBSYSNAME}_lib.fd \
	    ); fi

internal::	${SUBDIRS}
#	# make sure the necessary macros are defined
	if test "${SRCDIR}"; \
	    then echo make internal: ${SRCDIRPATH}/${SRCDIR}; \
	    else echo make internal: no SRCDIR macro defined; \
	    fi;
#	# make sure everything is checked in and properly labeled
	checklocks ${AFILES} ${CFILES} ${IFILES} ${HFILES} ${MAKEFILE}
	revlabel L${VERNUM}_`cat ${SUBSYSNAME}_rev.rev` \
	    ${AFILES} ${CFILES} ${IFILES} ${HFILES} ${MAKEFILE}
#	# copy over all files to the source directory
	-rsh ${RELNODE} mkdir ${SRCPATH} ${INCLDIR}
	diffinstall ${RELNODE} ${SRCPATH} \
	    ${AFILES} ${CFILES} ${HFILES} ${IFILES} ${MAKEFILE} \
	    ${SUBSYSNAME}_rev.rev ${SUBSYSNAME}_lib.fd ${SUBSYSNAME}.lib
#	# now copy over the external includes
	diffinstall ${RELNODE} ${INCLDIR} ${EXTERNHFILES} ${EXTERNIFILES}
#
#	# now copy over the autodoc file, if it exists
	diffinstall ${RELNODE} ${DOCDIR} ${SUBSYSNAME}.doc
#
#	# now, if this is a library, copy over the library interface function
#	# description.  Note that this will not make it
	-if test -r ${SUBSYSNAME}_lib.fd; \
	    then ( \
	    rsh ${RELNODE} mkdir ${LIBDIR}/${SUBSYSNAME}; \
	    diffinstall ${RELNODE} ${LIBDIR}/${SUBSYSNAME} \
		${SUBSYSNAME}_lib.fd \
	    ); fi
#	# now release the executable files and associated map files to the
#	# proper directories.
	-if test "${DISKPATH}"; \
	    then ( \
	    rcp ${SUBSYSNAME}.ld \
		${RELNODE}:${DISKDIR}/${DISKPATH}.`cat \
		${SUBSYSNAME}_rev.rev`; \
	    ); \
	    elif test -r ${SUBSYSNAME}.ld; \
		then ( \
		rcp ${SUBSYSNAME}.ld \
		    ${RELNODE}:${ROMDIR}/${SUBSYSNAME}`cat \
		    ${SUBSYSNAME}_rev.rev`.ld; \
	    ); fi

print::	${SUBDIRS}
	-@for i in ${HFILES} ${IFILES} ${CFILES} ; do \
	    (echo imprint ${PFLAGS} -2 -O -L -J $$i; \
	    imprint ${PFLAGS} -2 -O -L -J $$i); done
	-@${VERDIR}/tools/imprintlis \
	    ${PFLAGS} -2 -O -L -J ${AFILES}
	-@if test -r ${SUBSYSNAME}.doc; then \
	    (echo imprint ${PFLAGS} -2 -O -L ${SUBSYSNAME}.doc; \
	    imprint ${PFLAGS} -2 -O -L ${SUBSYSNAME}.doc); fi

${SUBSYSNAME}_rev.i ${SUBSYSNAME}_rev.h: ${SUBSYSNAME}_rev.rev ${DEPENDS}

${SUBSYSNAME}_rev.rev:
	echo 0 > ${SUBSYSNAME}_rev.rev
	
${SUBSYSNAME}.ld ${SUBSYSNAME}.lmap: ${OFILES} ${FIRSTO} ${LFILES} \
	    ${SUBSYSNAME}.with
#This addition should make the images load into CHIP memory
#	atom atomfile.obj atomfile.obj.TMP -pc -cdb
#	mv atomfile.obj.TMP atomfile.obj

	${LINK} with=${SUBSYSNAME}.with ${LFLAGS} to=${SUBSYSNAME}.ld.TMP
	@mv ${SUBSYSNAME}.ld.TMP ${SUBSYSNAME}.ld

${SUBSYSNAME}_lib.lib:	${OFILES} ${FIRSTO}
	rm -f ${SUBSYSNAME}_lib.lib
	cat ${FIRSTO} ${OFILES} > ${SUBSYSNAME}_lib.lib

${SUBSYSNAME}.lib:	${OFILES} ${FIRSTO}
	rm -f ${SUBSYSNAME}.lib
	cat ${FIRSTO} ${OFILES} > ${SUBSYSNAME}.lib

${SUBSYSNAME}.doc:	${SUBSYSNAME}.cat
	@echo ${SUBSYSNAME}.doc
	@rm -f ${SUBSYSNAME}.doc
	autodoc ${SUBSYSNAME}.cat > ${SUBSYSNAME}.doc

${SUBSYSNAME}.cat:	FRC
	@echo ${SUBSYSNAME}.cat
	@rm -f ${SUBSYSNAME}.cat
	@mkdir TMPD
	@for i in ${SUBDIRS} TMPD ; do \
	    ( ( cd $$i; cat /dev/null `ls *.cat` ) >> ${SUBSYSNAME}.cat \
	    ); done
	@cat ${AFILES} ${CFILES} >> ${SUBSYSNAME}.cat
	@rmdir TMPD

cat:			${SUBDIRS} ${SUBSYSNAME}.cat

TMPD ${SUBDIRS}: FRC
	cd $@; ${MAKE} ${MFLAGS} ${MARGS}

FRC:
