AR=ar cru
CC=gcc
COV=gcov
RANLIB=ranlib
RM=rm -f
CFLAGS=-g -DMAY_WANT_ASSERT
FAST_CFLAGS=-fexceptions -O3 -fomit-frame-pointer -funroll-loops -ffast-math -march=native -ffunction-sections -fdata-sections
XCFLAGS=-Wall -Wmissing-prototypes -Wwrite-strings -W -std=gnu99 -Wno-format

GMP=
MPFR=
PREFIX=/usr/local

####################### INTERNAL ###############################

VERSION=0.7.3

.SUFFIXES: .c .o

TESTS=t-charge.c t-pika.c t-test.c t-ihm.c
SOURCES=const.c dump.c eval.c expand.c heap.c parser.c predicate.c diff.c subs.c num.c cmp.c set.c get.c get_str.c io.c name.c hash.c range.c ifactor.c trig.c kernel.c may-list.c trigh.c approx.c hold.c sqrtsimp.c gcd1.c match.c rewrite.c data.c rectform.c version.c comdenom.c divexact.c lcm1.c get_domain.c degree.c taylor.c divqr.c gcd2.c collect.c polvar.c extension.c texpand.c rationalize.c elist.c error.c logging.c evalfunc.c sqrfree.c transform.c recursive.c bintree.c smod.c ratfactor.c iterator.c eseries.c combine.c normalsign.c copy.c extract.c karatsuba.c antidiff.c gcdex.c partfrac.c
HEADERS=may.h may-impl.h
DIST=$(SOURCES) $(HEADERS) $(TESTS) Makefile TODO .gdbinit may.pdf may.texi COPYING.txt COPYING.LESSER.txt

GMP_DIR=$(shell (test -f $(GMP)/include/gmp.h && echo $(GMP)) || (test -f /usr/local/include/gmp.h && echo /usr/local) || (test -f /usr/include/gmp.h && echo /usr))
MPFR_DIR=$(shell (test -f $(MPFR)/include/mpfr.h && echo $(MPFR)) || (test -f $(GMP_DIR)/include/mpfr.h && echo $(GMP_DIR)))
INCLUDES=-I. -I$(MPFR_DIR)/include -I$(GMP_DIR)/include -I$(PREFIX)/include
LIB_MPFR=$(shell (test -f $(MPFR_DIR)/lib/libmpfr.a && echo $(MPFR_DIR)/lib/libmpfr.a) || echo "-L$(MPFR_DIR)/lib -lmpfr")
LIB_GMP=$(shell (test -f $(GMP_DIR)/lib/libgmp.a && echo $(GMP_DIR)/lib/libgmp.a) || echo "-L$(GMP_DIR)/lib -lgmp")
LIBS=$(LIB_MPFR) $(LIB_GMP)

DEFS=-DCC="$(CC)" -DCFLAGS="$(CFLAGS)"
TARGETS=$(TESTS:.c=)
OBJECTS=$(SOURCES:.c=.o)

%.o: %.c $(HEADERS)
	$(CC) $(CPPFLAGS) $(INCLUDES) $(CFLAGS) $(XCFLAGS) $(DEFS) -c $<

t-%: t-%.c libmay.a
	$(CC) $(CPPFLAGS) $(LDFLAGS) $(INCLUDES) $(CFLAGS) -std=gnu99  $< -o $@ libmay.a $(LIBS)

all: t-pika may.pdf may.info

check:  t-test
	@echo "Starting tests..." && ./t-test && echo "All tests passed"

charge: fast t-charge
	@./t-charge

cachegrind: fast t-charge
	@valgrind --tool=cachegrind --branch-sim=yes ./t-pika "(1+x+y+z)^10*(1+(x+y+z+1)^10)" -oexpand

icc:
	make CC=icc CFLAGS="-fast -fomit-frame-pointer -march=pentium3 -fexceptions -wd810 -wd981 -wd1229" clean $(OBJECTS)
	icc -fast -fomit-frame-pointer -march=pentium3 -g t-charge.c $(OBJECTS) $(LIBS) -o t-charge
	icc -fast -fomit-frame-pointer -march=pentium3 -g t-pika.c $(OBJECTS) $(LIBS) -o t-pika
	@./t-charge

makefile-tmp.cflags:
	@echo "Computing the CFLAGS to use..."
	@echo "int x;" > makefile-tmp.c
	@for i in $(FAST_CFLAGS) ; do $(CC) -c makefile-tmp.c $$i 2> /dev/null && echo $$i ; done > makefile-tmp.cflags

fast: makefile-tmp.cflags
	@make CFLAGS="$(shell cat makefile-tmp.cflags)" clean t-pika

coverage: clean coverage2

coverage2:
	@make CFLAGS="-fprofile-arcs -ftest-coverage -g" check
	@echo "============================== COVERAGE REPORT ================================="
	@for i in $(SOURCES) ; do printf "%20.20s: %s\n" "$$i" "$$( $(COV) $$i 2> /dev/null | grep -i Lines | tail -n 1 ) " ; done

help:
	@echo "make [GMP=GMP Install Directory] [MPFR=MPFR Install Directory] [PREFIX=Install Directory]"
	@echo "make target with target can be:"
	@echo " all:        Command Line evaluator"
	@echo " check:      Check MAYLIB consistency"
	@echo " charge:     Check MAYLIB efficiency"
	@echo " coverage:   Check MAYLIB coverage"
	@echo " cachegrind: Chech MAYLIB cache efficiency"
	@echo " fast:       Generate MAYLIB with Optimized flags"
	@echo " install:    Install MAYLIB in PREFIX [Default: /usr/local ]"
	@echo " clean:      Clean up build and temporary files"

clean:
	$(RM) $(OBJECTS) libmay.a $(TARGETS) *.s *~ *.gcov *.gcda *.gcno *.bb *.da *.bbg may.aux may.cp may.cps may.dvi may.fn may.ky may.log may.pg may.toc may.tp may.vr may.info may.dvi may.fns may.vrs gmon.out cachegrind.out.* makefile-tmp.* *.exe *.stackdump

libmay.a: $(OBJECTS) may.h may-impl.h
	$(AR) libmay.a $(OBJECTS)
	$(RANLIB) libmay.a

dist: $(DIST)
	rm -fr may-$(VERSION)
	mkdir may-$(VERSION)
	cp $(DIST) may-$(VERSION)
	rcs2log -h yaronet.com > may-$(VERSION)/ChangeLog.txt
	tar cf may-$(VERSION).tar may-$(VERSION)
	bzip2 --best may-$(VERSION).tar
	rm -fr may-$(VERSION)

may.info:	may.texi
	makeinfo may.texi -o may.info

pdf:	may.pdf
may.pdf:	may.texi
	texi2dvi --pdf may.texi

html:	may.html
may.html: may.texi
	makeinfo --html --no-split --number-sections may.texi

install: may.h may.info libmay.a
	test -z "$(PREFIX)/lib" || mkdir -p -- "$(PREFIX)/lib"
	install -c libmay.a "$(PREFIX)/lib/libmay.a"
	test -z "$(PREFIX)/include" || mkdir -p -- "$(PREFIX)/include"
	install -c may.h "$(PREFIX)/include/may.h"
	test -z "$(PREFIX)/info" || mkdir -p -- "$(PREFIX)/info"
	install -c may.info "$(PREFIX)/info/may.info"
