# GNU Makefile (for v3.76.1) for the Urania package, QVSOP library (1611)
# Use HP Tools v3.0.6 or higher!

SHELL = /bin/sh

# in .s files, allow #IF HP49 ... #ELSE ... #ENDIF
# call sasm with some repeated flags to keep it happy. The first entry counts!
SASM = sasm -e -DHP$(HPCALCNO)=1 -DHP48=0 -DHP49=0
RPLCOMP = rplcomp
MAKEROM = makerom
SLOAD = sload
ENTRIES = ../aux/SupRomEntr$(HPCALCNO).o

VERSION = 1.01
LIBNAME = QVSOP.$(HPCALCNO).$(VERSION).lib
OBJECTS = QVSOPsrc.o
SOURCES = QVSOPsrc.s
TABLES = QVHead$(HPCALCNO).o QVHash.o QVEnd.o

# delete targets that fail while being made
.DELETE_ON_ERROR:
.SUFFIXES:
.SUFFIXES: .s .a .o .ext .lr .l .lib .m .mn .tex

# don't delete the following intermediate files:
.PRECIOUS : %.a  QVentries.a
.INTERMEDIATE : %.o %.l %.lr QVSOP.m
# phony targets are those that are names, but not files
.PHONY : all lib entries clean 

help: 
	@echo "You must call it with \"make lib.48\", \"make lib.49\","  
	@echo "\"make entries.48\" or \"make entries.49\","
	@echo "or call it from above with env.var HPCALCNO set to 48 resp. 49." 

# lib.4[89] make also clean to prevent using the other calc's .o files
lib.48: 
	cp ../aux/CALCVERSION48.dep ../aux/CALCVERSION.dep
	$(MAKE) clean
	export HPCALCNO=48; $(MAKE) lib

lib.49: 
	cp ../aux/CALCVERSION49.dep ../aux/CALCVERSION.dep
	$(MAKE) clean
	export HPCALCNO=49; $(MAKE) lib

entries.48:
	export HPCALCNO=48; $(MAKE) entries

entries.49:
	export HPCALCNO=49; $(MAKE) entries


lib $(LIBNAME):  $(OBJECTS) $(TABLES) binhd$(HPCALCNO).o QVSOP$(HPCALCNO).m QV$(HPCALCNO).m 
	if [ -z $(HPCALCNO) ]; then $(MAKE) help; false; fi
	$(SLOAD) QVSOP$(HPCALCNO).m
	$(SLOAD) -H QV$(HPCALCNO).m

#install: $(LIBNAME)
#	mv $(LIBNAME) ../libs

clean:
	-rm -f  *.a *.l *.o *.ext *.lr *~ *.m *.mn *.h_ok

# This makes the MAKEROM config file. All $(OBJECTS) inserted in order! 
QVSOP$(HPCALCNO).mn : $(OBJECTS)
	@echo "Creating MAKEROM config file..."
	@echo "TITLE QVSOP/$(HPCALCNO) V$(VERSION) by Keith Farmer & Georg Zotti" > $@
	@echo "OUTPUT QVSOP$(HPCALCNO).o"                                        >> $@
	@echo "LLIST QVSOP$(HPCALCNO).lr"                                        >> $@
	@echo "SUPPRESS XREF"                                                    >> $@
#	@echo "CONFIGURE QVcfg"                                                   >> $@
	@echo "NAME QVSOP/$(HPCALCNO) $(VERSION) Quick VSOP 1998-2025 \A92000 K. Farmer & G. Zotti" >> $@
	@echo "ROMPHEAD QVHead$(HPCALCNO).a"                                     >> $@
	@for f in $(OBJECTS) ;  do \
                echo "REL $$f"                 >> $@ ; \
         done
	@echo "TABLE QVHash.a"                                   >> $@
	@echo "FINISH QVEnd.a"                                   >> $@
	@echo "END"                                              >> $@


QVSOP$(HPCALCNO).m: QVSOP$(HPCALCNO).mn $(OBJECTS)

$(TABLES): QVSOP$(HPCALCNO).m 
	$(SASM) -N $*.a

QVHash.a: QVSOP$(HPCALCNO).m

# This is the final SLOAD configuration file:
QV$(HPCALCNO).m: Makefile
	@echo "creating $@: SLOAD config file"
	@echo "TITLE QVSOP/$(HPCALCNO) V$(VERSION) speedup Library" > $@
	@echo "OUTPUT $(LIBNAME)"               >> $@
	@echo "OPTION CODE"                     >> $@
	@echo "LLIST QVlib$(HPCALCNO).lr"       >> $@
	@echo "**SUPPRESS XR"                   >> $@
	@echo "SEARCH $(ENTRIES)"               >> $@
	@echo "REL binhd$(HPCALCNO).o"          >> $@
	@echo "REL QVSOP$(HPCALCNO).o"          >> $@
	@echo "CK LIB64B SYSEND64B"             >> $@

# This makes a reusable file for programs using entries of QVSOP.lib
QVentries.a: QVHash.a
	@echo "Creating $@, to be used by external programs"
	@if [ -f $@.sav ] ; then echo "Will not overwrite existing $@.sav." ; fi
	@if [ -f $@ -a ! -f $@.sav ] ; then mv $@ $@.sav; fi 
	@echo "   TITLE QVentries V$(VERSION) - ROM pointer entries"      > $@
	@echo "* This table can be compiled and SEARCHed by SLOAD"       >> $@
	@echo "* for programs which use routines from QVSOP V$(VERSION)" >> $@
	@echo "* Automatically created -- EDITING IS USELESS!"           >> $@
	@sed -n '/^=~/p' QVHash.a                                        >> $@
# This will halt make if any differences occur! 
	@if [ -f $@.sav ] ; then cmp $@ $@.sav ; fi

entries QVentries.o: QVentries.a
	$(SASM) QVentries.a
	cp QVentries.o ../aux
	cp QVentries.a ../doc

# This makes the external declarations file
QVXDec.h: $(SOURCES) Makefile
	@echo "Creating $@: External Declarations file"
	@echo "* TITLE QVSOP External Declarations"              > $@
	@echo "* contains names of all programs for QVSOP"      >> $@
	@echo "* Automatically created -- EDITING IS USELESS!"  >> $@
	@for f in $(SOURCES) ; do \
               echo "******* Entries from $$f *******"          >> $@ ; \
               sed -n -e 's/^xNAME */EXTERNAL x/p'    \
                      -e 's/^NULLNAME */EXTERNAL /p'   $$f      >> $@ ; \
             done                          

binhd48.a: 
	@echo creating binary download header
	@echo "ASSEMBLE "             > $@
	@echo "  NIBASC  /HPHP48-P/" >> $@
	@echo "RPL "                 >> $@

binhd49.a: 
	@echo creating binary download header
	@echo "ASSEMBLE "             > $@
	@echo "  NIBASC  /HPHP49-C/" >> $@
	@echo "RPL "                 >> $@

%.o : %.s


%.a : %.s QVXDec.h 
	$(RPLCOMP) $< $@ $*.ext

%.o : %.a
	$(SASM) $<

%.m : %.mn
	$(MAKEROM) $*.mn $*.m

%.tex : %.s
	sed -n 's/^\*"//p' $*.s > $*.tex
	mv $*.tex ../doc/tex/


