# Makefile for compiling and installing the Mathomatic Prime Number Tools
# under a Unix-like system.
# See file README.txt for instructions.

SHELL		= /bin/sh # from http://www.gnu.org/prep/standards/
CC		?= gcc # C compiler to use
INSTALL		?= install # installer to use
INSTALL_PROGRAM	?= $(INSTALL) # command to install program files
INSTALL_DATA	?= $(INSTALL) -m 0644 # command to install data files

OPTFLAGS	?= -O3 -Wall -Wshadow -std=gnu99 # gcc specific flags; comment out for other C compilers.
CFLAGS		+= $(OPTFLAGS)
LDLIBS		+= -lm

# Install directories follow, installs everything in /usr/local by default:
prefix		?= /usr/local
bindir		?= $(prefix)/bin
mandir		?= $(prefix)/share/man

# The executables to create:
TARGETS		= matho-primes matho-pascal matho-sumsq
# The Python scripts to install:
PYTHON_SCRIPTS	= primorial matho-mult matho-sum
# The executables to install:
EXECUTABLES	= $(TARGETS) $(PYTHON_SCRIPTS)
# The man pages to install:
MAN1		= matho-primes.1 matho-pascal.1 matho-sumsq.1 primorial.1 matho-mult.1 matho-sum.1

all: $(TARGETS) # make these by default

matho-sumsq: matho-sumsq.o lsqrt.o

check test:
	time ./matho-primes 10000000000000 10000000300000 twin >test.out && diff -u twins.out test.out
	@rm test.out
	@echo Test passed.

bigtest:
	time ./matho-primes 100000000000000000 100000000000300000 twin >test.out && diff -u bigtwins.out test.out
	@rm test.out
	@echo Test passed.

install:
	$(INSTALL) -d $(bindir)
	$(INSTALL) -d $(mandir)/man1
	$(INSTALL_PROGRAM) $(EXECUTABLES) $(bindir)
	$(INSTALL_DATA) $(MAN1) $(mandir)/man1
	@echo The Prime Number Tools are installed.

uninstall:
	cd $(bindir) && rm -f $(EXECUTABLES)
	cd $(mandir)/man1 && rm -f $(MAN1)
	@echo Uninstall completed.

clean flush distclean maintainer-clean:
	rm -f *.o
	rm -f $(TARGETS)
	rm -f test.out
