# Makefile for cddlib, cddmathlink and examples 
#
# see README
#
# make all        to produce cdd library (libcdd.a) and test programs 
#                 running with C double
# make gmp        to produce cdd library (libcddgmp.a) and test programs 
#                 running with GMP rational (mpq_t)
# make ml         to produce MathLink program cddmathlink with C double arithmetic
# make everything to do all three above 
# make clean      to clean all objects, library and executables 

# C-compiler
CC=gcc
CC=cc

# Location of GMP header file: gmp.h
GMPINCDIR = /usr/local/include

# Location of GMP library: libgmp.a
GMPLIBDIR = /usr/local/lib

# Archive maintainer ar
AR=ar

# Libtool ranlib
RL=ranlib

# ----------- cddlib Compilation Environment ---------- 
# Compiler options
CFLAGS = -g -O3
#CFLAGS = -g -DDEBUGMEM
#CFLAGS = -pg -Wall -O
#CFLAGS = -O3

# --------------- MathLink Environment --------------- 
#  Ignore this part if you are not using cddmathlink.
#
# MathLink Top Directory  
MLINKDIR = /usr/local

# Subdirectory containing mprep 
MLBINDIR = ${MLINKDIR}/bin

# Subdirectory containing the ML header file mathlink.h 
MLINCDIR = ${MLINKDIR}/include

# Subdirectory containing the Library libML.a 
MLLIBDIR = ${MLINKDIR}/lib

# MathLink Compilar Flags (check mcc scripts for appropriate flags)
#MLFLAGS = -lML -lm -lsocket -lnsl   #Solaris
MLFLAGS = -lML -lm                  #NEXTSTEP

ml : cddmathlink

cddmathlink : libcdd.a cddmathlinktm.o cddmathlink.o cddmlio.o
	${CC} -O3 -I${MLINCDIR} cddmlio.o cddmathlinktm.o cddmathlink.o -L${MLLIBDIR} ${MLFLAGS} -o cddmathlink $(LFLAGS) -lcdd

cddmathlinktm.c : cddmathlink.tm
	${MLBINDIR}/mprep $? > $@

########## You shouldn't have to change anything after this point ##########
# Location of include files for cddlib compilation
IFLAGS = -I.
GMPIFLAGS = -I. -I$(GMPINCDIR)

# C-Library location  for cddlib compilation
LFLAGS = -L.
GMPLFLAGS = -L. -L$(GMPLIBDIR)

# GMP flags for cddlib compilation with GMP rational
GMPFLAGS = -DGMPRATIONAL

# Library and Include FLAGS together
LIFLAGS = $(LFLAGS) $(IFLAGS) $(CFLAGS)
GMPLIFLAGS = $(GMPLFLAGS) $(GMPIFLAGS) $(CFLAGS)

HFILES = cdd.h cddtypes.h cddmp.h setoper.h
OFILES = cddio.o cddmp.o cddlib.o cddcore.o setoper.o cddlp.o
OFILESGMP = cddio_gmp.o cddmp_gmp.o cdd_gmp.o cddcore_gmp.o setoper.o cddlp_gmp.o

CDDTARGETS=libcdd.a scdd lcdd redcheck testcdd1 testcdd2 testshoot testlp1 testlp2 testlp3
GMPTARGETS=libcddgmp.a scdd_gmp lcdd_gmp redcheck_gmp testcdd1_gmp testshoot_gmp testcdd2_gmp testlp1_gmp testlp2_gmp testlp3_gmp

cddio.o: cddio.c $(HFILES) 
	$(CC) $(LIFLAGS) -c cddio.c

cddio_gmp.o: cddio.c $(HFILES) 
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) -c cddio.c -o cddio_gmp.o

cddmp.o: cddmp.c $(HFILES) 
	$(CC) $(LIFLAGS) -c cddmp.c

cddmp_gmp.o: cddmp.c $(HFILES) 
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) -c cddmp.c -o cddmp_gmp.o

cddmlio.o: cddmlio.c $(HFILES) 
	$(CC) $(LIFLAGS) -c cddmlio.c

cddcore.o: cddcore.c $(HFILES)
	$(CC) $(LIFLAGS) -c cddcore.c

cddcore_gmp.o: cddcore.c $(HFILES)
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) -c cddcore.c -o cddcore_gmp.o

cddlib.o: cddlib.c $(HFILES)
	$(CC) $(LIFLAGS) -c cddlib.c

cdd_gmp.o: cddlib.c $(HFILES)
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) -c cddlib.c -o cdd_gmp.o

cddlp.o: cddlp.c $(HFILES)
	$(CC) $(LIFLAGS) -c cddlp.c

cddlp_gmp.o: cddlp.c $(HFILES)
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) -c cddlp.c -o cddlp_gmp.o

libcdd.a: $(OFILES)
	$(AR) r libcdd.a $(OFILES)
	$(RL) libcdd.a

libcddgmp.a: $(OFILESGMP)
	$(AR) r libcddgmp.a $(OFILESGMP)
	$(RL) libcddgmp.a

setoper.o: setoper.c setoper.h
	$(CC) $(LIFLAGS) -c setoper.c

scdd: simplecdd.c libcdd.a
	$(CC) $(LIFLAGS) simplecdd.c -o scdd $(LFLAGS) -lcdd

scdd_gmp: simplecdd.c libcdd.a
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) simplecdd.c -o scdd_gmp $(LFLAGS) -lcddgmp -lgmp

lcdd: lcdd.c libcdd.a
	$(CC) $(LIFLAGS) lcdd.c -o lcdd $(LFLAGS) -lcdd
	./lcdd sample.ine

lcdd_gmp: lcdd.c libcddgmp.a
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) lcdd.c -o lcdd_gmp $(LFLAGS) -lcddgmp -lgmp
	./lcdd_gmp sample.ine

testcdd1: testcdd1.c libcdd.a
	$(CC) $(LIFLAGS) testcdd1.c -o testcdd1 $(LFLAGS) -lcdd

testcdd1_gmp: testcdd1.c libcddgmp.a
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) testcdd1.c -o testcdd1_gmp $(LFLAGS) -lcddgmp -lgmp

testcdd2: testcdd2.c libcdd.a
	$(CC) $(LIFLAGS) testcdd2.c -o testcdd2 $(LFLAGS) -lcdd
	./testcdd2

testcdd2_gmp: testcdd2.c libcddgmp.a
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) testcdd2.c -o testcdd2_gmp $(LFLAGS) -lcddgmp -lgmp
	./testcdd2_gmp

redcheck: redcheck.c libcdd.a
	$(CC) $(LIFLAGS) redcheck.c -o redcheck $(LFLAGS) -lcdd

redcheck_gmp: redcheck.c libcddgmp.a
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) redcheck.c -o redcheck_gmp $(LFLAGS) -lcddgmp -lgmp

testshoot: testshoot.c libcdd.a
	$(CC) $(LIFLAGS) testshoot.c -o testshoot $(LFLAGS) -lcdd

testshoot_gmp: testshoot.c libcddgmp.a
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) testshoot.c -o testshoot_gmp $(LFLAGS) -lcddgmp -lgmp

testlp1: testlp1.c libcdd.a
	$(CC) $(LIFLAGS) testlp1.c -o testlp1 $(LFLAGS) -lcdd

testlp1_gmp: testlp1.c libcddgmp.a
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) testlp1.c -o testlp1_gmp $(LFLAGS) -lcddgmp -lgmp

testlp2: testlp2.c libcdd.a
	$(CC) $(LIFLAGS) testlp2.c -o testlp2 $(LFLAGS) -lcdd
	./testlp2

testlp2_gmp: testlp2.c libcddgmp.a
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) testlp2.c -o testlp2_gmp $(LFLAGS) -lcddgmp -lgmp
	./testlp2_gmp

testlp3: testlp3.c libcdd.a
	$(CC) $(LIFLAGS) testlp3.c -o testlp3 $(LFLAGS) -lcdd
	./testlp3

testlp3_gmp: testlp3.c libcddgmp.a
	$(CC) $(GMPLIFLAGS) $(GMPFLAGS) testlp3.c -o testlp3_gmp $(LFLAGS) -lcddgmp -lgmp
	./testlp3_gmp

clean:
	rm -rf core *.out  *.o *.a *~ cddmathlinktm.* cddmathlink $(CDDTARGETS) $(GMPTARGETS)

all: $(CDDTARGETS)

gmp: $(GMPTARGETS)

everything: ml all gmp
