CC = gcc
CFLAGS = -O3 -fomit-frame-pointer -ffast-math -DHAVE_CONFIG_H -I$(common) -I. -I$(srcdir)
F77 = gfortran
FFLAGS = -g -O2
LIBS = -lm
INSTALL = /usr/bin/install -c
INSTALL_DATA = ${INSTALL} -m 644
INSTALL_PROGRAM = ${INSTALL}
RANLIB = ranlib
EXE = 

prefix = /usr/local
exec_prefix = ${prefix}
srcdir = .
bindir = ${exec_prefix}/bin
libdir = ${exec_prefix}/lib
includedir = ${prefix}/include
datarootdir = ${prefix}/share
datadir = ${datarootdir}

TOOLS_DEFAULT = tools
DEMO_FORTRAN_DEFAULT = demo-fortran$(EXE)

MCC = CC="$(CC)" PATH="$(PATH):$(srcdir)/tools" mcc

TARDIR = Cuba-1.4
TARFILE = $(TARDIR).tar.gz


HEADERS = cuba.h
LIB = libcuba.a
MATH = Vegas$(EXE) Suave$(EXE) Divonne$(EXE) Cuhre$(EXE)
TOOLS = partview$(EXE)


all: lib demos math $(TOOLS_DEFAULT)

lib: $(LIB)

demos: demo-c$(EXE) $(DEMO_FORTRAN_DEFAULT)

math: $(MATH)

tools: $(TOOLS)


install: lib math $(TOOLS_DEFAULT)
	$(INSTALL) -m 755 -d $(DESTDIR)$(libdir) \
	                     $(DESTDIR)$(includedir) \
	                     $(DESTDIR)$(bindir) \
	                     $(DESTDIR)$(datadir)
	$(INSTALL_DATA) $(LIB) $(DESTDIR)$(libdir)
	for f in $(HEADERS) ; do \
	  $(INSTALL_DATA) `test -f $$f || echo $(srcdir)/`$$f $(DESTDIR)$(includedir); \
	done
	-$(INSTALL_PROGRAM) $(MATH) $(DESTDIR)$(bindir)
	-$(INSTALL_PROGRAM) $(TOOLS) $(DESTDIR)$(bindir)
	-$(INSTALL_DATA) $(srcdir)/cuba.pdf $(DESTDIR)$(datadir)


check: demo-c
	./demo-c | grep RESULT > demo-c.out
	@echo diff demo-c.out $(demo)/demo-c.out
	@diff demo-c.out $(demo)/demo-c.out || \
	echo "Note: This is serious only if the results deviate significantly."

.SUFFIXES:
.SUFFIXES: .c .f .tm

common = $(srcdir)/src/common
vegas = $(srcdir)/src/vegas
suave = $(srcdir)/src/suave
divonne = $(srcdir)/src/divonne
cuhre = $(srcdir)/src/cuhre
demo = $(srcdir)/demo
tools = $(srcdir)/tools


VEGAS_C = $(vegas)/Vegas.c
VEGAS_TM = $(vegas)/Vegas.tm
VEGAS_H = $(vegas)/decl.h $(common)/stddecl.h
VEGAS_SRCS = $(VEGAS_H) $(common)/debug.c \
  $(common)/Random.c $(common)/ChiSquare.c $(common)/Erf.c \
  $(vegas)/util.c $(vegas)/common.c $(vegas)/Grid.c $(vegas)/Integrate.c
VEGAS = $(VEGAS_C) $(VEGAS_TM) $(VEGAS_SRCS)

$(LIB)(Vegas.o): $(VEGAS_C) $(VEGAS_SRCS)
	$(CC) $(CFLAGS) -c -o Vegas.o $(VEGAS_C)
	$(AR) $(ARFLAGS) $(LIB) Vegas.o
	$(RM) Vegas.o

$(LIB)(llVegas.o): $(VEGAS_C) $(VEGAS_SRCS)
	$(CC) $(CFLAGS) -DLONGLONGINT -c -o llVegas.o $(VEGAS_C)
	$(AR) $(ARFLAGS) $(LIB) llVegas.o
	$(RM) llVegas.o

$(LIB)(Vegas_.o): $(VEGAS_C) $(VEGAS_SRCS)
	$(CC) $(CFLAGS) -DUNDERSCORE -c -o Vegas_.o $(VEGAS_C)
	$(AR) $(ARFLAGS) $(LIB) Vegas_.o
	$(RM) Vegas_.o

$(LIB)(llVegas_.o): $(VEGAS_C) $(VEGAS_SRCS)
	$(CC) $(CFLAGS) -DLONGLONGINT -DUNDERSCORE -c -o llVegas_.o $(VEGAS_C)
	$(AR) $(ARFLAGS) $(LIB) llVegas_.o
	$(RM) llVegas_.o

Vegas$(EXE): $(VEGAS_TM) $(VEGAS_SRCS)
	$(MCC) $(CFLAGS) -o Vegas$(EXE) $(VEGAS_TM)
	strip Vegas$(EXE)


SUAVE_C = $(suave)/Suave.c
SUAVE_TM = $(suave)/Suave.tm
SUAVE_H = $(suave)/decl.h $(common)/stddecl.h
SUAVE_SRCS = $(SUAVE_H) $(common)/debug.c \
  $(common)/Random.c $(common)/ChiSquare.c $(common)/Erf.c \
  $(suave)/util.c $(suave)/common.c $(suave)/Grid.c \
  $(suave)/Fluct.c $(suave)/Sample.c $(suave)/Integrate.c
SUAVE = $(SUAVE_C) $(SUAVE_TM) $(SUAVE_SRCS)

$(LIB)(Suave.o): $(SUAVE_C) $(SUAVE_SRCS)
	$(CC) $(CFLAGS) -c -o Suave.o $(SUAVE_C)
	$(AR) $(ARFLAGS) $(LIB) Suave.o
	$(RM) Suave.o

$(LIB)(llSuave.o): $(SUAVE_C) $(SUAVE_SRCS)
	$(CC) $(CFLAGS) -DLONGLONGINT -c -o llSuave.o $(SUAVE_C)
	$(AR) $(ARFLAGS) $(LIB) llSuave.o
	$(RM) llSuave.o

$(LIB)(Suave_.o): $(SUAVE_C) $(SUAVE_SRCS)
	$(CC) $(CFLAGS) -DUNDERSCORE -c -o Suave_.o $(SUAVE_C)
	$(AR) $(ARFLAGS) $(LIB) Suave_.o
	$(RM) Suave_.o

$(LIB)(llSuave_.o): $(SUAVE_C) $(SUAVE_SRCS)
	$(CC) $(CFLAGS) -DLONGLONGINT -DUNDERSCORE -c -o llSuave_.o $(SUAVE_C)
	$(AR) $(ARFLAGS) $(LIB) llSuave_.o
	$(RM) llSuave_.o

Suave$(EXE): $(SUAVE_TM) $(SUAVE_SRCS)
	$(MCC) $(CFLAGS) -o Suave$(EXE) $(SUAVE_TM)
	strip Suave$(EXE)


DIVONNE_C = $(divonne)/Divonne.c
DIVONNE_F = $(divonne)/divonne-f.c
DIVONNE_TM = $(divonne)/Divonne.tm
DIVONNE_H = $(divonne)/decl.h $(common)/stddecl.h
DIVONNE_SRCS = $(DIVONNE_H) $(common)/debug.c \
  $(common)/Random.c $(common)/ChiSquare.c $(common)/Erf.c \
  $(divonne)/util.c $(divonne)/common.c $(divonne)/KorobovCoeff.c \
  $(divonne)/Rule.c $(divonne)/Sample.c $(divonne)/FindMinimum.c \
  $(divonne)/Explore.c $(divonne)/Split.c $(divonne)/Integrate.c
DIVONNE = $(DIVONNE_C) $(DIVONNE_F) $(DIVONNE_TM) $(DIVONNE_SRCS)

$(LIB)(Divonne.o): $(DIVONNE_C) $(DIVONNE_SRCS)
	$(CC) $(CFLAGS) -c -o Divonne.o $(DIVONNE_C)
	$(AR) $(ARFLAGS) $(LIB) Divonne.o
	$(RM) Divonne.o

$(LIB)(llDivonne.o): $(DIVONNE_C) $(DIVONNE_SRCS)
	$(CC) $(CFLAGS) -DLONGLONGINT -c -o llDivonne.o $(DIVONNE_C)
	$(AR) $(ARFLAGS) $(LIB) llDivonne.o
	$(RM) llDivonne.o

$(LIB)(Divonne_.o): $(DIVONNE_C) $(DIVONNE_SRCS)
	$(CC) $(CFLAGS) -DUNDERSCORE -c -o Divonne_.o $(DIVONNE_C)
	$(AR) $(ARFLAGS) $(LIB) Divonne_.o
	$(RM) Divonne_.o

$(LIB)(llDivonne_.o): $(DIVONNE_C) $(DIVONNE_SRCS)
	$(CC) $(CFLAGS) -DLONGLONGINT -DUNDERSCORE -c -o llDivonne_.o $(DIVONNE_C)
	$(AR) $(ARFLAGS) $(LIB) llDivonne_.o
	$(RM) llDivonne_.o

Divonne$(EXE): $(DIVONNE_TM) $(DIVONNE_SRCS)
	$(MCC) $(CFLAGS) -o Divonne$(EXE) $(DIVONNE_TM)
	strip Divonne$(EXE)


CUHRE_C = $(cuhre)/Cuhre.c
CUHRE_F = $(cuhre)/cuhre-f.c
CUHRE_TM = $(cuhre)/Cuhre.tm
CUHRE_H = $(cuhre)/decl.h $(common)/stddecl.h
CUHRE_SRCS = $(CUHRE_H) $(common)/debug.c \
  $(common)/ChiSquare.c $(common)/Erf.c \
  $(cuhre)/util.c $(cuhre)/common.c $(cuhre)/Rule.c $(cuhre)/Integrate.c
CUHRE = $(CUHRE_C) $(CUHRE_F) $(CUHRE_TM) $(CUHRE_SRCS)

$(LIB)(Cuhre.o): $(CUHRE_C) $(CUHRE_SRCS)
	$(CC) $(CFLAGS) -c -o Cuhre.o $(CUHRE_C)
	$(AR) $(ARFLAGS) $(LIB) Cuhre.o
	$(RM) Cuhre.o

$(LIB)(llCuhre.o): $(CUHRE_C) $(CUHRE_SRCS)
	$(CC) $(CFLAGS) -DLONGLONGINT -c -o llCuhre.o $(CUHRE_C)
	$(AR) $(ARFLAGS) $(LIB) llCuhre.o
	$(RM) llCuhre.o

$(LIB)(Cuhre_.o): $(CUHRE_C) $(CUHRE_SRCS)
	$(CC) $(CFLAGS) -DUNDERSCORE -c -o Cuhre_.o $(CUHRE_C)
	$(AR) $(ARFLAGS) $(LIB) Cuhre_.o
	$(RM) Cuhre_.o

$(LIB)(llCuhre_.o): $(CUHRE_C) $(CUHRE_SRCS)
	$(CC) $(CFLAGS) -DLONGLONGINT -DUNDERSCORE -c -o llCuhre_.o $(CUHRE_C)
	$(AR) $(ARFLAGS) $(LIB) llCuhre_.o
	$(RM) llCuhre_.o

Cuhre$(EXE): $(CUHRE_TM) $(CUHRE_SRCS)
	$(MCC) $(CFLAGS) -o Cuhre$(EXE) $(CUHRE_TM)
	strip Cuhre$(EXE)


$(LIB): $(LIB)(Vegas.o)     $(LIB)(llVegas.o)    \
        $(LIB)(Vegas_.o)    $(LIB)(llVegas_.o)   \
        $(LIB)(Suave.o)     $(LIB)(llSuave.o)    \
        $(LIB)(Suave_.o)    $(LIB)(llSuave_.o)   \
        $(LIB)(Divonne.o)   $(LIB)(llDivonne.o)  \
        $(LIB)(Divonne_.o)  $(LIB)(llDivonne_.o) \
        $(LIB)(Cuhre.o)     $(LIB)(llCuhre.o)    \
        $(LIB)(Cuhre_.o)    $(LIB)(llCuhre_.o)
	-$(RANLIB) $(LIB)


DEMO_F = $(demo)/demo-fortran.F
DEMO_C = $(demo)/demo-c.c
DEMO_TM = $(demo)/demo-math.m $(demo)/testsuite.m
DEMO = $(DEMO_F) $(DEMO_C) $(DEMO_TM) $(demo)/demo-c.out $(demo)/cuba.F

demo-fortran$(EXE): $(DEMO_F) $(LIB)
	$(F77) $(FFLAGS) -o demo-fortran$(EXE) $(DEMO_F) $(LIB) $(LIBS)

demo-c$(EXE): $(DEMO_C) $(srcdir)/cuba.h $(LIB)
	$(CC) $(CFLAGS) -o demo-c$(EXE) $(DEMO_C) $(LIB) $(LIBS)


PARTVIEW = $(tools)/partview.cpp $(tools)/partview.pro \
  $(tools)/quit.xpm $(tools)/print.xpm

partview$(EXE): $(PARTVIEW)
	qmake -o makefile.partview $(tools)/partview.pro
	$(MAKE) -f makefile.partview
	strip partview$(EXE)


TARCONTENTS = $(srcdir)/ChangeLog $(srcdir)/configure \
  $(srcdir)/configure.ac $(srcdir)/install-sh \
  $(srcdir)/makefile.in $(srcdir)/config.h.in \
  $(srcdir)/cuba.pdf $(srcdir)/cuba.h \
  $(VEGAS) $(SUAVE) $(DIVONNE) $(CUHRE) \
  $(DEMO) $(PARTVIEW) $(tools)/partview.m $(tools)/mcc


dist:
	ln -s . $(TARDIR)
	tar cvfzh $(TARFILE) --owner=root --group=root \
	  $(patsubst $(srcdir)/%,$(TARDIR)/%, $(sort $(TARCONTENTS)))
	$(RM) $(TARDIR)

pub: dist
	mv -f $(TARFILE) web/
	./mkwebpage

clean:
	-$(RM) $(TARFILE) $(TARDIR) demo-c.out \
	  partview.moc partview.o makefile.partview \
	  *.o */*.o */*.tm.c

distclean: clean
	-$(RM) $(LIB) demo-c demo-fortran $(MATH) $(TOOLS) \
	  config.h config.log config.status makefile

