#
#	This is a temporary native make environment while
#	Some technical problems with the full blown one is
#	worked out.
#
#	It incorporates the same concepts and is forward compatible
#	with the full blown one.  (i.e. the full blown one adds features
#	that this one lacks).  This contains MAKE INTERNAL and MAKE
#	only.
#
######################################################################
#
#	$Id: NMakeMeta,v 1.27 90/11/06 23:50:22 Kevin Exp Locker: Kevin $
#
#
######################################################################
#
#	In order to use this, you need the following assigns:
#
#	V:		Points to the current version tree on the server
#
#	INCLUDE:        Points to the directory that contains the include
#				files you wish to use.
#	LIB:		Points to the directory containing the library
#				files you wish to use.
#	SFD:		Points to the directory in which to place SFD
#				SFD files
#	INCLUDE.STRIP:	Point to the directory in which to place stripped
#				include files.
#	INCLUDE.NOSTRIP:Points to the directory in which to place
#				non-stripped include files.
#	DOC:		Points to the directory in which to place autodoc
#				files.
#
#	TMP:		Points to a temporary directory where short
#				temporary files can be stored.  Note
#				that this only has to be 10 or 20K.
#
#	NMM:		Points to the directory that contains all the
#				NativeMakeMeta scripts and such.  Normally
#				this SHOULD point to the server's
#				NativeMakeMeta directory, but you can
#				change that if you feel comfortable with
#				it.
#
######################################################################
#
#	Within your makefile you need to define the following macros:
#
#	ASSEMBLER	set this to ${CAPE}, ${AMANX3} (for the manx
#			3.x Assembler), ${AMANX} for Manx 5.x or greater,
#			${ALATTICE} (for the lattice
#			assembler) or the mnemonic for whatever
#			assembler you are using.  If your assembler isn't
#			supported, then please see the keeper of native
#			make meta to incorporate your assembler into the
#			standard list.
#
#	COMPILER	set this to either ${LATTICE}, ${MANX3}, or
#			${MANX} depending on your particular compiler.
#
#	LINKER		set this to the linker that you use.  Normally
#			this is ${LLATTICE}, ${LMANX3}, or ${LMANX},
#			but you can set this if you need something really
#			whacked
#
#			${LLATTICE} should be used if you're using the
#			latice compiler, ${LMANX3} should be specified if
#			you're using the MANX 3.x compiler, or ${LMANX}
#			if you're using something more recent.
#
#	CFLAGS		Set this to any special compiler flags you wish to
#			use over the standard ones set by native makemeta
#			for your compiler.
#
#	AFLAGS		set this to any special assembler flags you wish to
#			use over the standard ones set by native makemeta
#			for your assembler.
#
#	LFLAGS		set this to any special linker flags you wish to
#			use over the standard ones set for BLINK
#
#	MODULE		set this to the name of your module.  For example:
#
#				MODULE = workbench
#
#	MODULEDIR	The directory to which to release your module.
#
#	CFILES		set this to the list of C source files you use.
#
#	AFILES		set this to the list of assembler source files you
#			use.
#
#	OFILES		set this to the list of object files to be made.
#
#	HFILES		set this to the list of .H files you need are
#			responsible for.
#
#	IFILES		set this to the list of .I files that you are
#			responsible for.
#
#	EXTINCL		set this to the list of include files that you
#			wish to release to the public.
#
#	INTINCL		set this to the list of include files that you
#			wish to release to other members of the
#			development team.
#
#		***	Note that EXTINCL and INTINCL make contain entries
#			that are duplicates of those in HFILES and IFILES
#			It is assumed that the union of CFILES, AFILES,
#			HFILES, and IFILES are to be kept under RCS.  It
#			is further assumed that all other files can be
#			rebuilt from CFILES, AFILES, HFILES, IFILES, or
#			a combination of them, or an outside agency.
#
#	LIBS		any libs that you need included
#
#	CLEANUP		_OPTIONAL_ is passed to a delete command during a
#			make clean operation.  The exact command executed
#			is DELETE >NIL: ${CLEANUP}, so CLEANUP can be any
#			valid file specification you wish.
#
#	SUBDIRS		Sub directories to make as well.
#
#######################################################################
#
# Other defines
#
TARGET	=	${MODULE}.ld
#
# Disable the "default" command prefixes and suffixes in order to let me
# do various error processing.
#
# Uncomment the next line and comment the one after it for debugging
# information.
#
#CMDPREFIX =	TRACE INTERMEDIATES;
CMDPREFIX =
CMDSUFFIX =     
#
#######################################################################
#
#	Start defining rules for makemeta
#
#######################################################################
#
#	I thought about making ${TARGET} the default thing to make,
#	but that might hide a problem in the makefile, so the idea
#	was dropped.  If you want it to be made as a default target,
#	then make a line something like:
#
#		all : ${MODULE}.ld
#
#	and that will cause make to recreate your module as its default
#	behaviour.
#
#######################################################################

#######################################################################
#
#	Start defining dependencies
#
#######################################################################

.c.o :
	@say "---> Compiling $*"
	@SIGNAL ON ERROR; '${COMPILER} ${CFLAGS} $*.c '; ERROR: EXIT RC

.asm.o :
	@say "---> Assembling $*"
	@SIGNAL ON ERROR; '${ASSEMBLER} $* ${AFLAGS}' ; ERROR: EXIT RC

#
#	Person didn't specify a default target.  OOPS!
#

default :
	@say ""
	@say "Error.  Possible cause - no target specified before native"
	@say "make meta included in your makefile.  Please check the"
	@say "number and dial again."
	@say ""

${MODULE}_rev.rev : version

#
#	How to make internal (i.e. do a release)
#

internal :
	@say "---> Now starting make internal.  This could take a while."
	@say "---> Getting current revision number."
	@OPTIONS RESULTS;\
	IF (OPEN('TheRevFile', '${MODULE}_rev.rev','R')) THEN DO;\
		CurrentLabel = "REV_" || READLN('TheRevFile');\
		IF(CLOSE('TheRevFile')) THEN DO; END;\
	END;\
	ELSE DO;\
		SAY 'Could not open ${MODULE}_rev.rev file.  Aborting...';\
		EXIT(255);\
	END;\
	say "---> Checking if everything's checked in.";\
	RcsFiles = "${RFILES}";\
	IF(RcsFiles ~= "") THEN DO WHILE RcsFiles ~= "";\
		PARSE VAR RcsFiles aFile RcsFiles;\
		IF(~EXISTS(AFile)) THEN DO;\
			'co -u' AFile;\
			IF (RC > 0) THEN DO;\
				SAY '---> ERROR: Could not find file:' AFile;\
				EXIT 255;\
			END;\
		END;\
		'rcsdiff >NIL: -q ' aFile;\
		IF(RC == 1) THEN DO;\
			SAY '---> ERROR:' AFile 'is not checked in.';\
			SAY 'Check it in?';\
			PULL Answer;\
			IF(LEFT(Answer,1) == 'Y') THEN 'ci -u' AFile;\
			ELSE EXIT(255);\
			DROP Answer;\
			END;\
		ELSE IF(RC==2) THEN DO;\
			SAY '---> ERROR: rcsdiff returned error code 2';\
			say "           (Don't ask me why - talk to Ray)";\
		END;\
		'rlog >tmp:NMake001 -h' aFile;\
		IF(OPEN('InfoFile', 'tmp:NMake001', 'R')) THEN DO;\
			RCSHead.AFile = READLN('InfoFile');\
			RCSHead.AFile = READLN('InfoFile');\
			RCSHead.AFile = READLN('InfoFile');\
			PARSE VAR RCSHead.AFile Trash RCSHead.AFile;\
			RCSHEad.AFile = COMPRESS(RCSHead.AFile);\
			IF(CLOSE('InfoFile')) THEN DO; END;\
			DROP Trash;\
		END;\
	END;\
	RcsFiles = "${RFILES}";\
	DO WHILE RcsFiles ~= "";\
		PARSE VAR RcsFiles AFile RcsFiles;\
		'rcs >NIL: -q -N' || CurrentLabel || ':' || RCSHead.AFile AFile;\
	END;\
	say "--->Copying files to the server.";\
	'delete >NIL: ${MODULEDIR}/#?';\
	'list >TMP:NMake001 ${MODULEDIR}/RCS/#? LFORMAT "protect >NIL: ${MODULEDIR}/RCS/%s RWED"';\
	'execute TMP:NMake001';\
	'list >TMP:NMake001 ${MODULEDIR}/#? LFORMAT "delete >NIL: ${MODULEDIR}/%s all"';\
	'execute tmp:NMake001';\
	TheFiles = "${HFILES} ${IFILES} ${OFILES} ${CFILES} ${AFILES} ${TARGET}";\
	DO WHILE TheFiles ~= "";\
		PARSE VAR TheFiles aFile, TheFiles;\
		IF index(aFile, "/") ~= 0 THEN 'copy' aFile 'INCLUDE.STRIP:' || aFile;\
		ELSE 'copy' aFile '${MODULEDIR}/' || aFile;\
	END;\
	'copy RCS/#?' ${MODULEDIR} || '/RCS';\
	say "--->Make internal complete.";\
	'delete >NIL: TMP:NMake001';\
	EXIT(0)


#
#	The target make rule
#

${TARGET} : ${SUBDIRS} ${OFILES} ${LIBS}
	@'amake version'
	@say "---> Linking together ${TARGET} from ${OFILES} ${LIBS}"
	@SIGNAL ON ERROR; '${LINKER}'; ERROR: EXIT RC

#
# make the _rev.rev file.
#

version :
	@SAY '---> Bumping revision number in ${MODULE}_rev.rev'
	@IF(EXISTS('${MODULE}_rev.rev')) THEN DO;\
		IF(Open('aFile','${MODULE}_rev.rev','R')) THEN DO;\
			CurrentRev = READLN('aFile');\
			IF(Close('aFile')) THEN DO; END;\
			CurrentRev = CurrentRev + 1;\
			'echo >${MODULE}_rev.rev' CurrentRev;\
		END;\
		ELSE DO;\
			SAY "Could not open ${MODULE}_rev.rev for input.";\
			EXIT(20);\
		END;\
	END;\
	ELSE DO;\
		SAY "${MODULE}_rev.rev doesn't exist.  Starting it over from 1.";\
		'echo >${MODULE}_rev.rev 1';\
	END

#
# clean up the source directory
#

clean :
	@-TheFiles = '${OFILES}' ;\
	DO WHILE TheFiles ~= ""  ;\
		PARSE VAR TheFiles AFile TheFiles ;\
		'delete >NIL:' AFile ;\
	END ;\
	'delete >NIL: ${TARGET}' ;\
	IF("${CLEANUP}" ~= "") THEN DO ;\
		'delete >NIL: (${CLEANUP})' ;\
	END ;\
	EXIT(0)

#
# Check in all the moduls that are checked out
#

ciall : ${RFILES}
        @LineNumber = 0 ;\
	SAY 'Message for checked in files : ' ;\
	DO WHILE aLine.LineNumber ~= '' ;\
		LineNumber = LineNumber + 1 ;\
		PULL aLine.LineNumber ;\
	END ;\
	SAY '---> Checking in any files still checked out.' ;\
	RcsFiles = "${RFILES}" ;\
	NChecked = "" ;\
	IF(RcsFiles ~= "") THEN DO WHILE RcsFiles ~= "";\
		PARSE VAR RcsFiles aFile RcsFiles;\
		IF(~EXISTS(AFile)) THEN DO;\
			'co -u' AFile;\
			IF (RC > 0) THEN DO;\
				SAY '---> ERROR: Could not find file:' AFile;\
				EXIT 255;\
			END;\
		END;\
		'rcsdiff >NIL: -q ' aFile;\
		IF(RC == 1) THEN NChecked = NChecked AFile ;\
		ELSE IF(RC==2) THEN DO;\
			SAY '---> ERROR: rcsdiff returned error code 2';\
			say "           (Don't ask me why - talk to Ray)";\
		END;\
	END;\
	DO WHILE NChecked ~= "" ;\
		PARSE VAR NChecked AFile NCHecked ;\
		Temp = LineNumber ;\
		DO WHILE Temp > 0 ;\
			QUEUE aLine.LineNumber ;\
		END ;\
		QUEUE '.' ;\
		'ci -u' AFile ;\
	END ;\
	EXIT 0

#
# Make the sub directories.
#

MakeMetaBogusTarget ${SUBDIRS} ::
	@TheSubDirs = "${SUBDIRS}";\
	DO WHILE TheSubDirs ~= "";\
		PARSE VAR TheSubDirs aDir TheSubDirs;\
		'echo >>TMP:NMakeMeta002 "cd' aDir '"';\
		'echo >>TMP:NMakeMeta002 "make";\
		'echo >>TMP:NMakeMeta002 "cd /";\
	END;\
	'EXECUTE TMP:NMakeMeta002'
