###########################################################
#	lmkmeta included part :ts=4

###########################################################
#	required volumes/assigns:
#	V36:		- should be official server V36 directory
#	VINCLUDE:	- (possibly local) assign for sys includes,
#					will be V36:include.strip for official build
#	VLIB:		- (possibly local or ram) assign for libs dir.
#					will be V36:lib for official build
#	MYINCLUDE:	- additional search path for temp include files which
#					have not yet been checked in.
#					will be *empty* for official build
#	QUAD:		- used for lattice, and by default precompiled includes.
#				  should be very fast access, e.g., RAM: or RAD:.

VERNUM=36
VERDIR=V$(VERNUM):

# make the standard default target "all"
default: all

###########################################################
#	tools
RX=rx
REXX=$(VERDIR)aug/rexx/
#REXX=work:amiga/native/rexx/

ASM=casm -a
BUMPREV=bumprev
LINK=blink
STRIP=blink QUIET NODEBUG from
# mminstall [strip=[<stripdir>]] dir=<destdir> files=[with <withfile>|{files}]
MMINSTALL=$(RX) $(REXX)mminstall.rexx
CC=lc
FD2PRAGMA=fd2pragma
RM=delete
AUTODOC=autodoc
VERMACRO=$(RX) $(REXX)vermacro.rexx

# a little weird
ZOOTMP=zoo.tmp
ZOOTMP2=zoo.tmp2
FEEDZOO=$(RX) >$(ZOOTMP2) $(REXX)feedzoo.rexx
#FEEDZOO=$(REXX)feedzoo.rexx >$(ZOOTMP2) 


###########################################################
#	standard build implicit rules
.c.o:
	@echo "$*.c: $(CFLAGS)" NOLINE
	@$(CC) $(CFLAGS) $(CINCL) $*

.asm.o:
	@echo "$*.asm: $(AFLAGS)"
	@$(ASM) $*.asm $(AFLAGS) $(AINCL) -o $*.o

#	disassembled listing; you must declare "foo.lst: foo.o"
.o.lst:
	omd >$*.lst $*.o $*.c

#	automatic revision bumping
#	doesn't pass all dependency files, ala makemeta
.rev.i .rev.h:
	$(BUMPREV) $(VERNUM) $* $*.rev $?

###########################################################
#	standard build file groups

HFILES= $(EXTERNHFILES) $(MYHFILES)
IFILES= $(EXTERNIFILES) $(MYIFILES)

#	everything copied to SRCPATH, and checked into RCS
ALLSRC=$(AFILES) $(CFILES) $(HFILES) $(IFILES) $(PROTO) \
	$(PRAGMA) $(MAKEFILE) $(MISCSRC) $(FDFILE)

###########################################################
#	standard build targets

LFILES=$(MYLIBS) VLIB:lattice/lc.lib VLIB:amiga.lib

$(SUBSYSNAME).with: $(MAKEFILE) $(METAFILE)
	@echo $(SUBSYSNAME).with:
	@copy to $(SUBSYSNAME).with <from <
	$(LFLAGS)
	from $(STARTUP) $(FIRSTO) $(OFILES)
	lib $(LFILES)
	to $(SUBSYSNAME).ld
	<

#	default executable build
$(SUBSYSNAME).ld: $(OFILES) $(FIRSTO) $(LFILES) $(SUBSYSNAME).with
	@echo "link $(SUBSYSNAME).ld"
	$(LINK) with $(SUBSYSNAME).with

$(SUBSYSNAME): $(SUBSYSNAME).ld
	@echo "strip $(SUBSYSNAME).ld to create $(SUBSYSNAME)."
	@$(STRIP) $(SUBSYSNAME).ld to $(SUBSYSNAME)

#	stripped library (of same name as executable)
$(SUBSYSNAME).library: $(SUBSYSNAME).ld
	@echo "strip $(SUBSYSNAME).ld to create $(SUBSYSNAME).library."
	@$(STRIP) $(SUBSYSNAME).ld to $(SUBSYSNAME).library

#	stripped device (of same name as executable)
$(SUBSYSNAME).device: $(SUBSYSNAME).ld
	@echo "strip $(SUBSYSNAME).ld to create $(SUBSYSNAME).device."
	@$(STRIP) $(SUBSYSNAME).ld to $(SUBSYSNAME).device

#	ram copy of precompiled include file
$(SYMDIR)$(SYM): $(SYM)
	copy $(SYM) $(SYMDIR)

#	disk copy of precompiled include file
#	ZZZ: would be better to also depend on $(HEADERUPDATE)
$(SYM): $(SYMSRC)
	@echo "	precompiling $(SYMSRC) to $(SYM)"
	lc1 -. $(CINCL) -ph -o$(SYM) $(SYMSRC)

#	revision bumping
$(SUBSYSNAME)_rev.h $(SUBSYSNAME)_rev.i: $(SUBSYSNAME)_rev.rev  $(OFILES)

$(SUBSYSNAME)_rev.rev:
	echo >$(SUBSYSNAME)_rev.rev 0

#	pragma from .fd
$(SUBSYSNAME)_pragmas.h: $(FDFILE)
	$(FD2PRAGMA)  $(FDFILE) $(SUBSYSNAME)_pragmas.h

#	zoo file
# if this depends on ALLSRC
$(SUBSYSNAME).zoo: FRC
	-@$(RM) $(SUBSYSNAME).zoo
	@copy to zoo.tmp <from <
	$(ALLSRC)
	$(BINFILES)
	<
	@$(FEEDZOO) zoo.tmp 
	@zoo	<zoo.tmp2 aI $(SUBSYSNAME).zoo
	@$(RM) zoo.tmp
	@$(RM) zoo.tmp2

zoo: $(SUBSYSNAME).zoo


#	autodocs
$(SUBSYSNAME).doc: FRC
	@echo "not making autodocs yet"

doc: $(SUBSYSNAME).doc

#	force target never exists
FRC:
	@echo "" NOLINE

###########################################################
#	installation directories

# initial path components
SYSINCL=$(VERDIR)include.strip/
SYSINCLNS=$(VERDIR)include/
LIBDIR=$(VERDIR)lib/

# directory path terminators
AUTOFD=$(LIBDIR)autofd
SRCPATH=$(VERDIR)$(SRCDIR)
PROTODIR=$(SYSINCL)clib
PROTODIRNS=$(SYSINCLNS)clib
PRAGMADIR=$(VERDIR)inc.lattice.strip/pragma
PRAGMADIRNS=$(VERDIR)inc.lattice/pragma

###########################################################
#	installation targets

backup:
	@$(VERMACRO) BACKUPDIR $(BACKUPDIR) files=FRC
	copy \#? $(BACKUPDIR)

clean:
	$(RM) \#?.o \#?.sym \#?.lst \#?.obj \#?.bak \#?.tmp

#	check in includes, fd, protos/pragmas
#	strip must be in mminstall
headers:
	@$(VERMACRO) INCDIR $(INCDIR) files=$(EXTERNHFILES) $(EXTERNIFILES)
	@$(MMINSTALL) strip=$(SYSINCL)$(INCDIR) dir=$(SYSINCLNS)$(INCDIR)  \
	files= $(EXTERNHFILES) $(EXTERNIFILES)
	@$(MMINSTALL) dir=$(AUTOFD) files=$(FDFILE)
	@$(MMINSTALL) strip=$(PROTODIR) dir=$(PROTODIRNS) files=$(PROTO)

# the official copy is now built from the .fd
#	@$(MMINSTALL) strip=$(PRAGMADIR) dir=$(PRAGMADIRNS) files=$(PRAGMA)

#	check in source, executable, fd, includes, protos, pragmas, docs
#
#	check RCS (ZZZ not implemented)
#	copy source, .ld, .library, fd, includes, to source dir
#	copy docs
#	copy protos/pragmas to include/clib (strip and nostrip) (warn if missing?)
#	copy external includes, stripped and nostrip
internal: $(PRAGMA) $(BINFILES)
	@$(VERMACRO) SRCDIR $(SRCDIR) files=source_is
	@echo "copying source to $(SRCPATH)"
	@$(MMINSTALL) dir=$(SRCPATH) files=<with <
	$(ALLSRC)
	<

	@$(VERMACRO) BINDIR $(BINDIR) files=$(BINFILES)
	@echo "copying binaries to $(VERDIR)$(BINDIR)"
	@$(MMINSTALL) dir=$(VERDIR)$(BINDIR) files=$(BINFILES)

	@$(VERMACRO) SRCDIR $(SRCDIR) files=$(SUBSYSNAME).doc $(MISCDOC)
	@echo "copying docs to $(SRCPATH)"
	@$(MMINSTALL) dir=$(SRCPATH) files= $(SUBSYSNAME).doc $(MISCDOC)

	@$(VERMACRO) DOCDIR $(DOCDIR) files=$(SUBSYSNAME).doc $(MISCDOC)
	@echo "copying docs to $(VERDIR)$(DOCDIR)"
	@$(MMINSTALL) dir=$(VERDIR)$(DOCDIR) files= $(SUBSYSNAME).doc $(MISCDOC)

	@$(VERMACRO) INCDIR $(INCDIR) files=$(EXTERNHFILES) $(EXTERNIFILES)
	@echo "copying external includes"
	@$(MMINSTALL) strip=$(SYSINCL)$(INCDIR) dir=$(SYSINCLNS)$(INCDIR) \
	files= $(EXTERNHFILES) $(EXTERNIFILES)

	@echo "copying .fd file (if any)"
	@$(MMINSTALL) dir=$(AUTOFD) files=$(FDFILE)

	@echo "copying protos (not pragmas) to server"
	@$(MMINSTALL) strip=$(PROTODIR) dir=$(PROTODIRNS) files=$(PROTO)

# the official copy is now built from the .fd
#	@$(MMINSTALL) strip=$(PRAGMADIR) dir=$(PRAGMADIRNS) files=$(PRAGMA)


