############################################
##         QSIEVE 2.94 - MAKEFILE         ##
##     2003-12 by Thorsten Reinecke       ##
############################################
# please modify this Makefile for proper   # 
# configuration...                         #
# ... then simply type "make" to compile   #
############################################

#
# if you're lazy and you want to compile qsieve using a gcc-compiler,
# you should try this first. If you encounter any problems compiling
# qsieve, you should disable this feature.
#
DEFINES += -DAUTODETECTION
 

#
# you can enable fibonacci-heap for sieving large primes, but probably
# the priority-queue given by C++-STL is faster...
#
# you can also try fakeheap, which is a vector that behaves like a heap,
# if certain conditions are fulfilled
#
#DEFINES += -DUSE_FIBHEAP
DEFINES += -DUSE_FAKEHEAP

#
# if you want to use i386 inline assembler code optimizations, you
# should enable "-DASM386", otherwise you should disable this flag.
# "-DASM386_CMOV" allows to use the conditional mov-instructions
# "-DASMATHLON" enables experimental ATHLON-assembler optimizations.
#
DEFINES += -DASM386
DEFINES += -DASM386_CMOV
DEFINES += -DASMATHLON


#
# These options enable sieving of squares larger than the sieve interval.
# Sieving with large squares takes a lot more time. On the other hand,
# using a short sieve interval and a small factorbase, you may have a
# chance to factorize more relations using these options.
#
#DEFINES += -DSIEVING_LARGE_SQUARES
#DEFINES += -DSIEVING_MORE_LARGE_SQUARES


#
# If you want to autostart a distributed factorization by the server,
# you should enable the following option. If the server is compiled
# with this define, he will execute a script called "autostart_clients.sh"
# before waiting for net-clients.
# Don't forget to customize "autostart_clients.sh"!
#
#DEFINES += -DCLIENT_AUTOSTART

# For elliptic-curve-method you can choose between two continuations.
# If this Flag is not defined, improved standard continuation will be used.
# The improved standard continuation should be faster in finding
# factors up to 25 decimal digits.
# If you want to factorize large numbers using lots of elliptic curves
# you may find it useful to switch to fft-continuation.
# The fft-continuation is asymptotically faster than the improved
# standard continuation since it takes advantage of fast polynomial
# evaluation schemes. If you run low on memory or if you want to
# use qsieve as background-process, it may be better to use
# improved standard continuation.
DEFINES += -DECM_FFT_CONTINUATION

# If this is *not* defined, fast polynomial evaluation will
# be done using only karatsuba polynomial multiplication instead
# of the discrete fast fourier transform.
# If it is defined, a mix of karatsuba and dft will be used gaining
# better performance for large search intervals.
# (see dft.cc & polynomial.cc for further information)
DEFINES += -DUSE_DFT

# If you're not using Linux (or you want to restrict the amount of memory
# consumed by fft-polynomial-evaluation), you can bypass the linux-specific
# function, which evaluates the available memory.
# This is done by defining FFT_MAX_MEM_USAGE with a constant value in KB.
# example: FFT_MAX_MEM_USAGE=512*1024 /* restricted to 512 MB */
# (see fft_param.cc for further information)
#DEFINES += -DFFT_MAX_MEM_USAGE=512*1024

# If you use newer versions of gmp (>=gmp-4.0.x), they have defined own
# output operators. This conflicts with our own wrappers. As a quick hack,
# you should modify the gmp.h-file and enable the cpp-operators only, if 
# __cplusplus is enabled and USER_GMP_WRAP is disabled.
DEFINES += -DUSER_GMP_WRAP

# Do you want to abort some long-running algorithms without changing the
# config file?
# You can send signals to qsieve! Simply activate the define REACT_ON_SIGUSR
# and you are able to send SIGUSR1 to abort the current algorithm or SIGUSR2
# to step right to the continuation phase.
# example: killall -i -s USR1 -v qsieve
DEFINES += -DREACT_ON_SIGUSR


#
# standard-standalone-factorization
#
PROGRAMS += qsieve

#
# network-server for distributed sieving (UNIX)
#
PROGRAMS += server

#
# network-client for distributed sieving (UNIX)
#
PROGRAMS += net-client

#
# offline-client for distributed sieving (UNIX)
#
PROGRAMS += file-client

# 
# communication between offline-client and online-server (UNIX)
#
PROGRAMS += transfer-client

#
# DO NOT EDIT THIS...
#
SERVER = -DIS_SERVER -DUSE_NETWORK -D_REENTRANT
CLIENT = -DIS_CLIENT


#
# path's to GNU-multiple-precision-library
# comment out the right ones (or define them yourself)
# (CPUFLAGS may be left undefined)
#

# for pentium-mmx on my system
#CPUFLAGS = -march=pentium-mmx
#GMP_inc_dir = /public/packages/libraries/gmp-4.1.2-pentiummmx/include
#GMP_lib_dir = /public/packages/libraries/gmp-4.1.2-pentiummmx/lib

# for pentium3 on my system
#CPUFLAGS = -march=pentium3
#GMP_inc_dir = /public/packages/libraries/gmp-4.1.2-pentium3/include
#GMP_lib_dir = /public/packages/libraries/gmp-4.1.2-pentium3/lib

# for athlon on my system
CPUFLAGS = -march=athlon
GMP_inc_dir = /public/packages/libraries/gmp-4.1.2-athlon/include
GMP_lib_dir = /public/packages/libraries/gmp-4.1.2-athlon/lib


# 
# For proper installation and code-optimization you could modify CFLAGS... 
# (depending on your compiler, less optimization produces less memory overhead
# and simplier code, which could be faster sometimes: so check out
# which optimization level works best on your system. On most systems
# -O2 would be a safe bet.
# Static linking (LDFLAGS: -static) may improve performance, too.
#
# It is safe to leave the other lines untouched unless you know
# exactly what you're doing...
#
OBJ = qsieve.o
DEBUG = -g
#DEBUG += -pg -ftest-coverage -DPROFILE
#DEBUG += -fprofile-arcs
CFLAGS = -O2 $(CPUFLAGS) -Wall -Wreorder $(DEFINES)
#CFLAGS +=  -Wsynth -fno-weak -fuse-cxa-atexit -fno-elide-constructors
CFLAGS += -W -Woverloaded-virtual -Wimplicit -Wcast-qual -Wcast-align
CFLAGS += -Wredundant-decls -Wdisabled-optimization -Wconversion -Wsign-promo
#CFLAGS += -Wwrite-strings
#CFLAGS += -Wfloat-equal -Winline
#CFLAGS += -Wpadded
#CFLAGS += -Weffc++
#CFLAGS += -Wold-style-cast
CFLAGS += -pedantic
CFLAGS += -Wno-long-long
#CFLAGS += -fbranch-probabilities
INCLUDES = -I. -I$(GMP_inc_dir)
#NETLIBS = -lsocket -lnsl
LDFLAGS = -L. -L$(GMP_lib_dir) -lgmp -lm $(NETLIBS)
LDFLAGS += -static

# pthread-library: (net-client uses threads now for sending relations...)
PTHREAD = -L/usr/lib -lpthread
#PTHREAD = -L/usr/lib /usr/lib/libpthread.a
#PTHREAD = -L/usr/lib /public/packages/programming/valgrind-2.0.0/lib/valgrind/libpthread.so /public/packages/programming/valgrind-2.0.0/lib/valgrind/valgrind.so /public/packages/programming/valgrind-2.0.0/lib/valgrind/vgskin_memcheck.so
#PTHREAD = -L/usr/lib /public/packages/programming/valgrind-2.1.0/lib/valgrind/libpthread.so /public/packages/programming/valgrind-2.1.0/lib/valgrind/valgrind.so /public/packages/programming/valgrind-2.1.0/lib/valgrind/vgskin_memcheck.so


ALL_FLAGS_NO_LD = $(DEBUG) $(CFLAGS) $(INCLUDES)
ALL_FLAGS = $(ALL_FLAGS_NO_LD) $(LDFLAGS)

.PHONY : Makefile Makefile.* clean debug-clean distclean doc

.EXPORT_ALL_VARIABLES : ;

all:
	$(MAKE) -C src
	ln -i $(addprefix src/, $(PROGRAMS)) .

# reference manual by given (default) sample file
doc:
	doxygen


# do we have an libstdc++.tag file? -> get it...
libstdc++.tag:
	wget http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/libstdc++.tag

# reference manual for standalone version of qsieve
refman-qsieve: libstdc++.tag
	sed s/@PROJECT_NAME@/"qsieve-standalone"/g Doxyfile.in > x.tmp
	sed s/@PREDEFINES@/"IS_STANDALONE"/g x.tmp > y.tmp
	sed s/@GENERIC_PREDEFINES@/"$(subst -D,,$(DEFINES))"/g y.tmp > x.tmp
	doxygen x.tmp

# reference manual for net-client
refman-net-client: libstdc++.tag
	sed s/@PROJECT_NAME@/"qsieve-net-client"/g Doxyfile.in > x.tmp
	sed s/@PREDEFINES@/"IS_CLIENT USE_NETWORK _REENTRANT"/g x.tmp > y.tmp
	sed s/@GENERIC_PREDEFINES@/"$(subst -D,,$(DEFINES))"/g y.tmp > x.tmp
	doxygen x.tmp

# reference manual for server
refman-server: libstdc++.tag
	sed s/@PROJECT_NAME@/"qsieve-server"/g Doxyfile.in > x.tmp
	sed s/@PREDEFINES@/"IS_SERVER USE_NETWORK _REENTRANT"/g x.tmp > y.tmp
	sed s/@GENERIC_PREDEFINES@/"$(subst -D,,$(DEFINES))"/g y.tmp > x.tmp
	doxygen x.tmp

clean:
	$(MAKE) -C src $@
	rm -f *.o $(PROGRAMS)

debug-clean:
	$(MAKE) -C src $@
	rm -f *.bb *.bbg *.out *.gcov *.da core

distclean: clean debug-clean
	$(MAKE) -C src $@
	rm -f *.d *.o *.dat *.tmp *~ factorizations.txt qsieve-fc.param*
	rm -f qsieve file-client server net-client transfer-client
	rm -f qsieve@* file-client@* server@* net-client@* transfer-client@*
	rm -rf doc

%: Makefile
	$(MAKE) -C src $@
	$(if $(findstring $@,$(PROGRAMS)),ln -i src/$(findstring $@,$(PROGRAMS)) .,echo "nothing to copy")
