#   ratpoints-2.1.3
#    - A program to find rational points on hyperelliptic curves
#   Copyright (C) 2008, 2009  Michael Stoll
#
#   This program is free software: you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation, either version 2 of
#   the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of version 2 of the GNU General
#   Public License along with this program.
#   If not, see <http://www.gnu.org/licenses/>.
#
#
#    Makefile
#
#    Michael Stoll, September 21, 2009

PRIME_SIZE = 7

CC = gcc
RM = rm -f
INSTALL = cp

INSTALL_DIR = /usr/local

CCFLAGS1 = -Wall -O2 -fomit-frame-pointer -DRATPOINTS_MAX_BITS_IN_PRIME=${PRIME_SIZE} -DUSE_SSE
# for gcc on Apple, may have to add '-fnested-functions' to CCFLAGS1
CCFLAGS2 = -lgmp -lgcc -lc -lm
CCFLAGS3 = -L. -lratpoints
CCFLAGS = 


DISTFILES = Makefile ratpoints.h rp-private.h primes.h \
            gen_find_points_h.c gen_init_sieve_h.c \
            sift.c init.c sturm.c find_points.c \
            main.c rptest.c testdata.h testbase ratpoints-doc.pdf \
            gpl-2.0.txt

TEMPFILES = sift.o init.o sturm.o find_points.o \
            sift.s init.s find_points.h init_sieve.h \
            gen_find_points_h gen_init_sieve_h \
            rptest.out sift-debug.o find_points-debug.o main.o

TARGETFILES = ratpoints libratpoints.a rptest ratpoints-debug

all: ratpoints libratpoints.a

test: rptest testbase
	time ./rptest > rptest.out
#	diff -q testbase rptest.out
	cmp -s testbase rptest.out || echo "Test failed!"

install-bin: ratpoints
	${INSTALL} ratpoints ${INSTALL_DIR}/bin/
	chmod 755 ${INSTALL_DIR}/bin/ratpoints

install-lib: ratpoints.h libratpoints.a
	${INSTALL} ratpoints.h ${INSTALL_DIR}/include/
	chmod 644 ${INSTALL_DIR}/include/ratpoints.h
	${INSTALL} libratpoints.a ${INSTALL_DIR}/lib/
	chmod 644 ${INSTALL_DIR}/lib/libratpoints.a

install: install-bin install-lib

dist: ${DISTFILES}
	mkdir -p ratpoints-2.1.3
	cp ${DISTFILES} ratpoints-2.1.3/
	tar --create --file=ratpoints-2.1.3-`date --rfc-3339=date`.tar.gz \
	    --gzip --dereference ratpoints-2.1.3
	rm -r ratpoints-2.1.3

clean: 
	${RM} ${TEMPFILES}

distclean: clean
	${RM} ${TARGETFILES}

debug: ratpoints-debug

libratpoints.a: sift.o init.o sturm.o find_points.o
	ar rs libratpoints.a sift.o init.o sturm.o find_points.o

ratpoints: libratpoints.a main.c ratpoints.h
	${CC} main.c -o ratpoints ${CCFLAGS1} ${CCFLAGS2} ${CCFLAGS3} ${CCFLAGS}

main.o: main.c ratpoints.h
	${CC} main.c -c -o main.o ${CCFLAGS1} -O3 ${CCFLAGS}

ratpoints-debug: sift-debug.o init.o sturm.o find_points-debug.o main.o
	${CC} sift-debug.o init.o sturm.o find_points-debug.o main.o \
              -o ratpoints-debug ${CCFLAGS1} ${CCFLAGS2} ${CCFLAGS}

sift.o: sift.c ratpoints.h rp-private.h
	${CC} sift.c -c -o sift.o ${CCFLAGS1} -funroll-loops ${CCFLAGS}

sift-debug.o: sift.c ratpoints.h rp-private.h
	${CC} sift.c -c -o sift-debug.o ${CCFLAGS1} -funroll-loops -DDEBUG ${CCFLAGS}

sift.s: sift.c ratpoints.h rp-private.h
	${CC} sift.c -S -o sift.s ${CCFLAGS1} -funroll-loops ${CCFLAGS}

sift.i: sift.c ratpoints.h rp-private.h
	${CC} sift.c -E -o sift.i ${CCFLAGS1} -funroll-loops ${CCFLAGS}

init.o: init.c ratpoints.h rp-private.h init_sieve.h
	${CC} init.c -c -o init.o ${CCFLAGS1} -funroll-loops -O3 ${CCFLAGS}

init.s: init.c ratpoints.h rp-private.h init_sieve.h
	${CC} init.c -S -o init.s ${CCFLAGS1} -funroll-loops -O3 ${CCFLAGS}

sturm.o: sturm.c ratpoints.h rp-private.h
	${CC} sturm.c -c -o sturm.o ${CCFLAGS1} ${CCFLAGS}

find_points.o: find_points.c ratpoints.h rp-private.h primes.h find_points.h
	${CC} find_points.c -c -o find_points.o ${CCFLAGS1} ${CCFLAGS}

find_points-debug.o: find_points.c ratpoints.h rp-private.h primes.h find_points.h
	${CC} find_points.c -c -o find_points-debug.o ${CCFLAGS1} -DDEBUG ${CCFLAGS}

rptest: libratpoints.a rptest.c ratpoints.h testdata.h
	${CC} rptest.c -o rptest ${CCFLAGS1} ${CCFLAGS2} ${CCFLAGS3} ${CCFLAGS}

gen_init_sieve_h: gen_init_sieve_h.c ratpoints.h rp-private.h primes.h
	${CC} gen_init_sieve_h.c -o gen_init_sieve_h  ${CCFLAGS1} ${CCFLAGS2} ${CCFLAGS}

gen_find_points_h: gen_find_points_h.c ratpoints.h rp-private.h primes.h
	${CC} gen_find_points_h.c -o gen_find_points_h  ${CCFLAGS1} ${CCFLAGS2} ${CCFLAGS}

init_sieve.h: gen_init_sieve_h
	./gen_init_sieve_h > init_sieve.h

find_points.h: gen_find_points_h
	./gen_find_points_h > find_points.h
