#Usage:
# make all | clean | <functionname>
#all builds all test routines and executables.
#clean cleans the test routine executables and intermediate files.
#<functionname> is the name of an spnbox function. This builds the test routines for that function.

#This variable controls whether or not the memwatch memory debugging routines are included in
#compilation. Set to "yes" to include them and "no" to leave them out.
USEMEMWATCH = no

#Assign the default compiler call and options.
COMPILER=gcc -g -lm -ldl

#Assign the compiler call that will affect the inclusion or exclusion of the memwatch routines.
ifeq ($(USEMEMWATCH),yes)
MEMWCOMP=$(COMPILER) -include string.h -include ../../third-party/memwatch-2.71/memwatch.h -DMEMWATCH -DMEMWATCH_STDIO
else
MEMWCOMP=$(COMPILER)
endif

#The dependencies common to all test executables. Include memwatch.o only if the memwatch routines should
# be included.
ifeq ($(USEMEMWATCH),yes)
COMMON=StructuredIO.o test.o MemoryManager.o general.o pns.o matrix.o matrixmath.o deallocation.o memwatch.o
else
COMMON=StructuredIO.o test.o MemoryManager.o general.o pns.o matrix.o matrixmath.o deallocation.o
endif

#These symbols define the dependencies of various test executables.
AVPR=avpr.o
TREE2SPEC=tree2spec.o
DISQ2DISJ=disq2disj.o
DISJ2NORMAL=disj2normal.o
DISJ2PN=disj2pn.o
EXTENDEDMATRIX=extendedmatrix.o
FVPR=fvpr.o
GCDV=gcdv.o
INVAR=invar.o $(GCDV) $(EXTENDEDMATRIX)
IPSLV=ipslv.o ../liblpsolve55.a
IPSOLVE=ipsolve.o $(IPSLV)
ISADM=isadm.o
ISSIPH=issiph.o
MSPLIT=msplit.o
PN2ACPN=pn2acpn.o 
SUPERVIS=supervis.o $(EXTENDEDMATRIX)
ACTN=actn.o $(EXTENDEDMATRIX) $(NLTRANS)
ADMCON=admcon.o $(EXTENDEDMATRIX) $(IPSOLVE)
ASIPH=asiph.o $(ACTN)
ILPADM=ilpadm.o $(IPSOLVE)
MROADM=mroadm.o $(EXTENDEDMATRIX) $(GCDV)
LINENF=linenf.o $(ILPADM) $(MROADM)
LINDISJ=linenf.o $(ILPADM) $(MROADM) $(DISJ2NORMAL) $(DISJ2PN)
LINDISQ=linenf.o $(ILPADM) $(MROADM) $(DISQ2DISJ) $(DISJ2NORMAL) $(DISJ2PN)
LINTREE=linenf.o $(ILPADM) $(MROADM) $(DISQ2DISJ) $(TREE2SPEC) $(DISJ2PN)
NLTRANS=nltrans.o $(IPSOLVE)
PN2EACPN=pn2eacpn.o $(NLTRANS)
REDUCE=reduce.o chkcons.o $(EXTENDEDMATRIX) $(IPSOLVE)
TACTN=tactn.o $(IPSOLVE)
DP=dp.o tactn.o reduce.o chkcons.o pn2eacpn.o nltrans.o asiph.o actn.o admcon.o supervis.o msplit.o issiph.o fvpr.o avpr.o  $(EXTENDEDMATRIX) $(IPSOLVE)
DP4=dp4.o tactn.o reduce.o chkcons.o pn2acpn.o nltrans.o asiph.o actn.o admcon.o supervis.o msplit.o issiph.o fvpr.o avpr.o  $(EXTENDEDMATRIX) $(IPSOLVE)

#Common test header dependencies.
COMMONHEADER=../spnbox.h test.h ../../pnheaders/general.h ../../pnheaders/pns.h ../../pnheaders/matrix.h ../matrixmath.h ../MemoryManager.h StructuredIO.h

#Targets
all: avpr dp dp4 extendedmatrix fvpr gcdv invar ipslv ipsolve isadm issiph msplit matrixmath pn2acpn supervis actn admcon asiph ilpadm mroadm linenf lindisj lindisq lintree nltrans pn2eacpn reduce tactn	
avpr: test-avpr.o $(AVPR) $(COMMON)
	$(COMPILER) -o avpr.exe test-avpr.o $(AVPR) $(COMMON)
extendedmatrix: test-extendedmatrix.o $(EXTENDEDMATRIX) $(COMMON)
	$(COMPILER) -o extendedmatrix.exe test-extendedmatrix.o $(EXTENDEDMATRIX) $(COMMON)
fvpr: test-fvpr.o $(FVPR) $(COMMON)
	$(COMPILER) -o fvpr.exe test-fvpr.o $(FVPR) $(COMMON)
gcdv: test-gcdv.o $(GCDV) $(COMMON)
	$(COMPILER) -o gcdv.exe test-gcdv.o $(GCDV) $(COMMON)
invar: test-invar.o $(INVAR) $(COMMON)
	$(COMPILER) -o invar.exe test-invar.o $(INVAR) $(COMMON)
ipslv: test-ipslv.o $(IPSLV) $(COMMON)
	$(COMPILER) -o ipslv.exe test-ipslv.o $(IPSLV) $(COMMON)
ipsolve: test-ipsolve.o $(COMMON) $(IPSOLVE)
	$(COMPILER) -o ipsolve.exe test-ipsolve.o $(COMMON) $(IPSOLVE)
isadm: test-isadm.o $(ISADM)
	$(COMPILER) -o isadm.exe test-isadm.o $(COMMON) $(ISADM)
issiph: test-issiph.o $(COMMON) $(ISSIPH)
	$(COMPILER) -o issiph.exe test-issiph.o $(COMMON) $(ISSIPH)
msplit: test-msplit.o $(COMMON) $(MSPLIT)
	$(COMPILER) -o msplit.exe test-msplit.o $(COMMON) $(MSPLIT)
matrixmath: test-matrixmath.o $(COMMON)
	$(COMPILER) -o matrixmath.exe test-matrixmath.o $(COMMON)
pn2acpn: test-pn2acpn.o $(COMMON) $(PN2ACPN)
	$(COMPILER) -o pn2acpn.exe test-pn2acpn.o $(COMMON) $(PN2ACPN)
supervis: test-supervis.o $(COMMON) $(SUPERVIS)
	$(COMPILER) -o supervis.exe test-supervis.o $(COMMON) $(SUPERVIS)	
actn: test-actn.o $(COMMON) $(ACTN)
	$(COMPILER) -o actn.exe test-actn.o $(COMMON) $(ACTN)
admcon: test-admcon.o $(COMMON) $(ADMCON)
	$(COMPILER) -o admcon.exe test-admcon.o $(COMMON) $(ADMCON)
asiph: test-asiph.o $(COMMON) $(ASIPH)
	$(COMPILER) -o asiph.exe test-asiph.o $(COMMON) $(ASIPH)
ilpadm: test-ilpadm.o $(COMMON) $(ILPADM)
	$(COMPILER) -o ilpadm.exe test-ilpadm.o $(COMMON) $(ILPADM)
mroadm: test-mroadm.o $(COMMON) $(MROADM)
	$(COMPILER) -o mroadm.exe test-mroadm.o $(COMMON) $(MROADM)
linenf: test-linenf.o $(COMMON) $(LINENF)
	$(COMPILER) -o linenf.exe test-linenf.o $(COMMON) $(LINENF)	
lindisj: test-lindisj.o $(COMMON) $(LINDISJ)
	$(COMPILER) -o lindisj.exe test-lindisj.o $(COMMON) $(LINDISJ)
lindisq: test-lindisq.o $(COMMON) $(LINDISQ)
	$(COMPILER) -o lindisq.exe test-lindisq.o $(COMMON) $(LINDISQ)
lintree: test-lintree.o $(COMMON) $(LINTREE)
	$(COMPILER) -o lintree.exe test-lintree.o $(COMMON) $(LINTREE)
nltrans: test-nltrans.o $(COMMON) $(NLTRANS)
	$(COMPILER) -o nltrans.exe test-nltrans.o $(COMMON) $(NLTRANS)	
pn2eacpn: test-pn2eacpn.o $(COMMON) $(PN2EACPN)
	$(COMPILER) -o pn2eacpn.exe test-pn2eacpn.o $(COMMON) $(PN2EACPN)	
reduce: test-reduce.o $(COMMON) $(REDUCE)
	$(COMPILER) -o reduce.exe test-reduce.o $(COMMON) $(REDUCE)
tactn: test-tactn.o $(COMMON) $(TACTN)
	$(COMPILER) -o tactn.exe test-tactn.o $(COMMON) $(TACTN)
dp: test-dp.o $(COMMON) $(DP)
	$(COMPILER) -o dp.exe test-dp.o $(COMMON) $(DP)
dp4: test-dp.o $(COMMON) $(DP4)
	$(COMPILER) -o dp4.exe test-dp.o $(COMMON) $(DP4)
clean:
	rm -fv *.o *.exe *.stackdump

#Rules for making the general object files.
actn.o: ../actn.c ../spnbox.h ../../pnheaders/matrix.h ../../pnheaders/pns.h ../matrixmath.h ../MemoryManager.h
	$(MEMWCOMP) -c ../actn.c
admcon.o: ../admcon.c ../spnbox.h ../matrixmath.h ../extendedmatrix.h ../../pnheaders/general.h ../../pnheaders/matrix.h
	$(MEMWCOMP) -c ../admcon.c
asiph.o: ../asiph.c ../spnbox.h ../../pnheaders/general.h ../../pnheaders/matrix.h ../../pnheaders/pns.h ../extendedmatrix.h ../MemoryManager.h
	$(MEMWCOMP) -c ../asiph.c
avpr.o: ../avpr.c ../../pnheaders/general.h ../../pnheaders/matrix.h
	$(MEMWCOMP) -c ../avpr.c
chkcons.o: ../spnbox.h ../../pnheaders/matrix.h ../../pnheaders/pns.h ../chkcons.c
	$(MEMWCOMP) -c ../chkcons.c
deallocation.o: ../deallocation.c ../spnbox.h
	$(MEMWCOMP) -c ../deallocation.c
disj2normal.o: ../spnbox.h ../matrixmath.h ../MemoryManager.h
	$(MEMWCOMP) -c ../disj2normal.c
disq2disj.o: ../spnbox.h ../matrixmath.h ../MemoryManager.h
	$(MEMWCOMP) -c ../disq2disj.c
disj2pn.o: ../spnbox.h ../matrixmath.h ../MemoryManager.h
	$(MEMWCOMP) -c ../disj2pn.c
dp.o: ../dp.c ../spnbox.h ../matrixmath.h ../extendedmatrix.h ../../pnheaders/general.h ../../pnheaders/matrix.h ../../pnheaders/pns.h
	$(MEMWCOMP) -c ../dp.c
dp4.o: ../dp4.c ../spnbox.h ../matrixmath.h ../extendedmatrix.h ../../pnheaders/general.h ../../pnheaders/matrix.h ../../pnheaders/pns.h
	$(MEMWCOMP) -c ../dp4.c
extendedmatrix.o: ../extendedmatrix.c ../extendedmatrix.h ../../pnheaders/matrix.h ../../pnheaders/general.h
	$(MEMWCOMP) -c ../extendedmatrix.c
fvpr.o: ../fvpr.c ../../pnheaders/general.h ../../pnheaders/matrix.h
	$(MEMWCOMP) -c ../fvpr.c
gcdv.o: ../gcdv.c ../../pnheaders/general.h ../../pnheaders/matrix.h
	$(MEMWCOMP) -c ../gcdv.c
general.o: ../../pnheaders/general.c ../../pnheaders/general.h
	$(MEMWCOMP) -c ../../pnheaders/general.c
ilpadm.o: ../spnbox.h ../matrixmath.h ../../pnheaders/general.h ../../pnheaders/matrix.h ../MemoryManager.h ../ilpadm.c
	$(MEMWCOMP) -c ../ilpadm.c
invar.o: ../invar.c ../spnbox.h ../matrixmath.h ../MemoryManager.h ../extendedmatrix.h  ../../pnheaders/general.h ../../pnheaders/matrix.h
	$(MEMWCOMP) -c ../invar.c
isadm.o: ../isadm.c ../spnbox.h ../../pnheaders/general.h ../MemoryManager.h ../../pnheaders/pns.h ../matrixmath.h
	$(MEMWCOMP) -c ../isadm.c
issiph.o: ../issiph.c ../spnbox.h ../../pnheaders/general.h ../../pnheaders/matrix.h
	$(MEMWCOMP) -c ../issiph.c
ipslv.o: ../spnbox.h ../ipslv.c
	$(MEMWCOMP) -c ../ipslv.c
ipsolve.o: ../spnbox.h ../matrixmath.h ../../pnheaders/general.h ../../pnheaders/matrix.h ../MemoryManager.h ../ipsolve.c
	$(MEMWCOMP) -c ../ipsolve.c
linenf.o: ../spnbox.h ../matrixmath.h ../../pnheaders/general.h ../../pnheaders/matrix.h ../MemoryManager.h ../linenf.c
	$(MEMWCOMP) -c ../linenf.c
matrix.o: ../../pnheaders/matrix.c ../../pnheaders/matrix.h ../../pnheaders/pns.h
	$(MEMWCOMP) -c ../../pnheaders/matrix.c
matrixmath.o: ../matrixmath.c ../matrixmath.h ../../pnheaders/general.h ../../pnheaders/matrix.h
	$(MEMWCOMP) -c ../matrixmath.c
MemoryManager.o: ../MemoryManager.h ../MemoryManager.c ../../pnheaders/general.h ../../pnheaders/matrix.h
	$(MEMWCOMP) -c ../MemoryManager.c
mroadm.o: ../mroadm.c ../spnbox.h ../../pnheaders/matrix.h ../../pnheaders/general.h ../matrixmath.h
	$(MEMWCOMP) -c ../mroadm.c
msplit.o: ../spnbox.h ../../pnheaders/matrix.h ../matrixmath.h ../MemoryManager.h ../msplit.c
	$(MEMWCOMP) -c ../msplit.c
nltrans.o: ../nltrans.c ../spnbox.h ../../pnheaders/matrix.h ../matrixmath.h ../MemoryManager.h
	$(MEMWCOMP) -c ../nltrans.c
pn2acpn.o: ../spnbox.h ../../pnheaders/matrix.h ../MemoryManager.h ../pn2acpn.c
	$(MEMWCOMP) -c ../pn2acpn.c
pn2eacpn.o: ../spnbox.h ../../pnheaders/matrix.h ../MemoryManager.h ../pn2eacpn.c
	$(MEMWCOMP) -c ../pn2eacpn.c
pns.o: ../../pnheaders/pns.c ../../pnheaders/pns.h ../../pnheaders/general.h ../../pnheaders/matrix.h
	$(MEMWCOMP) -c ../../pnheaders/pns.c
reduce.o: ../spnbox.h ../extendedmatrix.h ../../pnheaders/matrix.h ../reduce.c
	$(MEMWCOMP) -c ../reduce.c
supervis.o: ../spnbox.h ../matrixmath.h ../../pnheaders/general.h ../../pnheaders/ ../../pnheaders/matrix.h ../supervis.c ../extendedmatrix.h
	$(MEMWCOMP) -c ../supervis.c
StructuredIO.o: ../MemoryManager.h ../../pnheaders/matrix.h StructuredIO.h StructuredIO.c
	$(MEMWCOMP) -c StructuredIO.c
tactn.o: ../tactn.c ../spnbox.h ../../pnheaders/matrix.h ../../pnheaders/pns.h ../matrixmath.h ../MemoryManager.h
	$(MEMWCOMP) -c ../tactn.c
test.o: test.c test.h ../../pnheaders/pns.h ../../pnheaders/matrix.h ../matrixmath.h ../spnbox.h ../MemoryManager.h StructuredIO.h
	$(MEMWCOMP) -c test.c
tree2spec.o: ../spnbox.h ../matrixmath.h ../MemoryManager.h
	$(MEMWCOMP) -c ../tree2spec.c

#Rules for making the test executable object files.	
test-actn.o: test-actn.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-actn.c
test-admcon.o: test-admcon.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-admcon.c
test-asiph.o: test-asiph.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-asiph.c
test-avpr.o: test-avpr.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-avpr.c
test-extendedmatrix.o: test-extendedmatrix.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-extendedmatrix.c
test-dp.o: test-dp.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-dp.c
test-fvpr.o: test-fvpr.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-fvpr.c
test-gcdv.o: test-gcdv.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-gcdv.c
test-ilpadm.o: test-ilpadm.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-ilpadm.c
test-invar.o: test-invar.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-invar.c
test-ipslv.o: test-ipslv.c
	$(MEMWCOMP) -c test-ipslv.c
test-ipsolve.o: test-ipsolve.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-ipsolve.c
test-isadm.o: test-isadm.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-isadm.c
test-linenf.o: test-linenf.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-linenf.c
test-lindisj.o: test-lindisj.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-lindisj.c
test-lindisq.o: test-lindisq.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-lindisq.c
test-lintree.o: test-lintree.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-lintree.c
test-matrixmath.o: test-matrixmath.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-matrixmath.c
test-mroadm.o: test-mroadm.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-mroadm.c
test-msplit.o: test-msplit.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-msplit.c
test-nltrans.o: test-nltrans.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-nltrans.c
test-pn2acpn.o: test-pn2acpn.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-pn2acpn.c
test-pn2eacpn.o: test-pn2eacpn.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-pn2eacpn.c
test-supervis.o: test-supervis.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-supervis.c
test-reduce.o: test-reduce.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-reduce.c
test-tactn.o: test-tactn.c $(COMMONHEADER)
	$(MEMWCOMP) -c test-tactn.c

#Rules for making the liblpsolve55.a static library for use with ipsolve.
../liblpsolve55.a: ../../third-party/lp_solve_5.5/lpsolve55/liblpsolve55.a
	cp ../../third-party/lp_solve_5.5/lpsolve55/liblpsolve55.a ../liblpsolve55.a

../../third-party/lp_solve_5.5/lpsolve55/liblpsolve55.a:
	cd ../../third-party/lp_solve_5.5; make -f Makefile.linux lib

#The memwatch libraries.
memwatch.o: ../../third-party/memwatch-2.71/memwatch.c ../../third-party/memwatch-2.71/memwatch.h
	$(COMPILER) -c ../../third-party/memwatch-2.71/memwatch.c
