###############################################################
#
#  First, choose a C++ compiler, and set compiler flags.
#  This is done by setting the variables CXX and CXXFLAGS.
#
###############################################################



CXX=@{CXX}
# A C++ compiler, e.g., g++, CC, xlC

CXXFLAGS=@{CXXFLAGS}
# Flags for the C++ compiler

CXXAUTOFLAGS=@{CXXAUTOFLAGS}
# Flags for the C++ compiler, automatically generated by configuration script

NOCONTRACT=@{NOCONTRACT}


AR=@{AR}
# command to make a library

ARFLAGS=@{ARFLAGS}
# arguments for AR

RANLIB=@{RANLIB}
# set to echo if you want to disable it completely

LDFLAGS=@{LDFLAGS}
# flags for linking C++ programs

LDLIBS=@{LDLIBS}
# libraries for linking C++ programs

CPPFLAGS=@{CPPFLAGS}
# arguments for the C preprocessor

LIBTOOL=@{LIBTOOL}
# libtool command -- this is now built locally

LIBTOOL_LINK_FLAGS=@{LIBTOOL_LINK_FLAGS}
# flags to add to command line when building a shared library
# mainly used to pass the argument "-no-undefined" on cygwin

LIBTOOL_LINK_LIBS=@{LIBTOOL_LINK_LIBS}
# libraries to add to the command line when building a shared library
# mainly used to pass the argument "-lpthread", which NTL's
# configure script will do "automagically" unless user overrides
# that behavior

SYS_PREFIX=@{SYS_PREFIX}
DEF_PREFIX=@{DEF_PREFIX}

PREFIX=@{PREFIX}
LIBDIR=@{LIBDIR}
INCLUDEDIR=@{INCLUDEDIR}
DOCDIR=@{DOCDIR}
PKGDIR=@{PKGDIR}
# where to install NTL

DESTDIR=
# added to support standard package building techniques
# that install into a "staging area"

###############################################################
#
#  Second, if you want to use GMP (the GNU Multi-Precision library),
#  define the variables GMP_OPT_INCDIR, GMP_OPT_LIBDIR, GMP_OPT_LIB below.
#  You also will have to set either NTL_GMP_LIP or NTL_GMP_HACK
#  in the config.h file.
#
#  Using GMP can lead to significant performance gains on some
#  platforms.  You can obtain GMP from http://www.swox.com/gmp.
#  Once you unpack it into a directory, just execute
#     ./configure; make
#  in that directory.
#
###############################################################


GMP_PREFIX=@{GMP_PREFIX}

GMP_INCDIR=@{GMP_INCDIR}
# directory containing gmp.h if using GMP 

GMP_LIBDIR=@{GMP_LIBDIR}
# directory containing libgmp.a if using GMP 

GMP_OPT_INCDIR=@{GMPI}-I$(GMP_INCDIR) # GMPI
GMP_OPT_LIBDIR=@{GMPL}-L$(GMP_LIBDIR) # GMPL
GMP_OPT_LIB=@{GMP}-lgmp # GMP
# uncomment these if using GMP

GMP_OPT_RPATH=@{GMPL}@{RPATH}-Wl,-rpath,$(GMP_LIBDIR)
# uncomment this if using GMP 

GMP_OPT_LIBPATH=$(GMP_OPT_LIBDIR) $(GMP_OPT_RPATH)
# used when linking to GMP without libtool
# this ensures that we link to the right version of a shared GMP library



###############################################################
#
#  Third, if you want to use GF2X (a library for fast
#  multiplication over GF(2)[X]), you need to
#  define the variables GF2X_OPT_INCDIR, GF2X_OPT_LIBDIR, GF2X_OPT_LIB below.
#  You also will have to set NTL_GF2X_LIB 
#  in the config.h file.
#
###############################################################

GF2X_PREFIX=@{GF2X_PREFIX}

GF2X_INCDIR=@{GF2X_INCDIR}
# directory containing gf2x.h if using GF2X

GF2X_LIBDIR=@{GF2X_LIBDIR}
# directory containing libgf2x.a

GF2X_OPT_INCDIR=@{GF2XI}-I$(GF2X_INCDIR) # GF2X
GF2X_OPT_LIBDIR=@{GF2XL}-L$(GF2X_LIBDIR) # GF2X
GF2X_OPT_LIB=@{GF2X}-lgf2x # GF2X
# uncomment these if using GF2X

GF2X_OPT_RPATH=@{GF2XL}@{RPATH}-Wl,-rpath,$(GF2X_LIBDIR)
# uncomment this if using GF2X

GF2X_OPT_LIBPATH=$(GF2X_OPT_LIBDIR) $(GF2X_OPT_RPATH)
# used when linking to GF2X without libtool
# this ensures that we link to the right version of a shared GF2X library






###############################################################
#
# Fourth, if you do not want to run the wizard that automagically
# sets some performace related flags in config.h, set the flag below.
#
###############################################################


WIZARD=@{WIZARD}
# Set to off if you want to bypass the wizard; otherwise, set to on.


#################################################################
#
#  That's it!  You can ignore everything else in this file!
#
#################################################################


# object files
OBJ=FFT.o FacVec.o GF2.o GF2E.o GF2EX.o GF2EXFactoring.o GF2X.o GF2X1.o  \
	GF2XFactoring.o GF2XVec.o GetTime.o GetPID.o HNF.o ctools.o LLL.o LLL_FP.o  \
	LLL_QP.o LLL_RR.o LLL_XD.o RR.o WordVector.o ZZ.o ZZVec.o ZZX.o ZZX1.o  \
	ZZXCharPoly.o ZZXFactoring.o ZZ_p.o ZZ_pE.o ZZ_pEX.o ZZ_pEXFactoring.o ZZ_pX.o  \
	ZZ_pX1.o ZZ_pXCharPoly.o ZZ_pXFactoring.o fileio.o lip.o lzz_p.o lzz_pE.o  \
	lzz_pEX.o lzz_pEXFactoring.o lzz_pX.o lzz_pX1.o lzz_pXCharPoly.o  \
	lzz_pXFactoring.o mat_GF2.o mat_GF2E.o mat_RR.o mat_ZZ.o mat_ZZ_p.o mat_ZZ_pE.o  \
	mat_lzz_p.o mat_lzz_pE.o mat_poly_ZZ.o mat_poly_ZZ_p.o mat_poly_lzz_p.o  \
	quad_float.o quad_float1.o tools.o vec_GF2.o vec_GF2E.o vec_RR.o vec_ZZ.o vec_ZZ_p.o  \
	vec_ZZ_pE.o vec_lzz_p.o vec_lzz_pE.o xdouble.o G_LLL_FP.o G_LLL_QP.o G_LLL_XD.o  \
	G_LLL_RR.o thread.o BasicThreadPool.o MatPrime.o pd_FFT.o

# library source files
SRC=FFT.cpp FacVec.cpp GF2.cpp GF2E.cpp GF2EX.cpp GF2EXFactoring.cpp GF2X.cpp  \
	GF2X1.cpp GF2XFactoring.cpp GF2XVec.cpp HNF.cpp ctools.cpp LLL.cpp LLL_FP.cpp  \
	LLL_QP.cpp LLL_RR.cpp LLL_XD.cpp RR.cpp WordVector.cpp ZZ.cpp ZZVec.cpp ZZX.cpp  \
	ZZX1.cpp ZZXCharPoly.cpp ZZXFactoring.cpp ZZ_p.cpp ZZ_pE.cpp ZZ_pEX.cpp  \
	ZZ_pEXFactoring.cpp ZZ_pX.cpp ZZ_pX1.cpp ZZ_pXCharPoly.cpp ZZ_pXFactoring.cpp  \
	fileio.cpp lip.cpp lzz_p.cpp lzz_pE.cpp lzz_pEX.cpp lzz_pEXFactoring.cpp  \
	lzz_pX.cpp lzz_pX1.cpp lzz_pXCharPoly.cpp lzz_pXFactoring.cpp mat_GF2.cpp  \
	mat_GF2E.cpp mat_RR.cpp mat_ZZ.cpp mat_ZZ_p.cpp mat_ZZ_pE.cpp mat_lzz_p.cpp  \
	mat_lzz_pE.cpp mat_poly_ZZ.cpp mat_poly_ZZ_p.cpp mat_poly_lzz_p.cpp  \
	quad_float.cpp quad_float1.cpp tools.cpp vec_GF2.cpp vec_GF2E.cpp vec_RR.cpp vec_ZZ.cpp  \
	vec_ZZ_p.cpp vec_ZZ_pE.cpp vec_lzz_p.cpp vec_lzz_pE.cpp xdouble.cpp  \
	G_LLL_FP.cpp G_LLL_QP.cpp G_LLL_XD.cpp G_LLL_RR.cpp thread.cpp  \
	BasicThreadPool.cpp MatPrime.cpp pd_FFT.cpp



# library header files
INCL=FFT.h FFT_impl.h FacVec.h GF2.h GF2E.h GF2EX.h GF2EXFactoring.h GF2X.h  \
	GF2XFactoring.h GF2XVec.h HNF.h ctools.h LLL.h RR.h WordVector.h  \
	ZZ.h ZZ_limbs.h sp_arith.h ZZVec.h ZZX.h ZZXFactoring.h ZZ_p.h ZZ_pE.h ZZ_pEX.h  \
	ZZ_pEXFactoring.h ZZ_pX.h ZZ_pXFactoring.h fileio.h lip.h lzz_p.h lzz_pE.h  \
	lzz_pEX.h lzz_pEXFactoring.h lzz_pX.h lzz_pXFactoring.h mat_GF2.h mat_GF2E.h  \
	mat_RR.h mat_ZZ.h mat_ZZ_p.h mat_ZZ_pE.h mat_lzz_p.h mat_lzz_pE.h mat_poly_ZZ.h  \
	mat_poly_ZZ_p.h mat_poly_lzz_p.h matrix.h pair.h vector.h pair_GF2EX_long.h  \
	pair_GF2X_long.h pair_ZZX_long.h pair_ZZ_pEX_long.h pair_ZZ_pX_long.h  \
	pair_lzz_pEX_long.h pair_lzz_pX_long.h quad_float.h tools.h vec_GF2.h  \
	vec_GF2E.h vec_GF2XVec.h vec_RR.h vec_ZZ.h vec_ZZVec.h vec_ZZ_p.h vec_ZZ_pE.h  \
	vec_double.h vec_long.h vec_lzz_p.h vec_lzz_pE.h vec_quad_float.h vec_vec_GF2.h  \
	vec_vec_GF2E.h vec_vec_RR.h vec_vec_ZZ.h vec_vec_ZZ_p.h vec_vec_ZZ_pE.h  \
	vec_vec_long.h vec_vec_lzz_p.h vec_vec_lzz_pE.h vec_xdouble.h xdouble.h  \
	version.h new.h vec_ulong.h vec_vec_ulong.h SmartPtr.h  \
	Lazy.h LazyTable.h thread.h  BasicThreadPool.h MatPrime.h PD.h pd_FFT.h  \
	linux_s390x.h simde_avx.h simde_fma.h simde_pclmul.h



# test data
TD=BerlekampTestIn BerlekampTestOut CanZassTestIn CanZassTestOut   \
	ZZXFacTestIn ZZXFacTestOut MoreFacTestIn LLLTestIn LLLTestOut RRTestIn RRTestOut   \
	MatrixTestIn MatrixTestOut CharPolyTestIn    \
	CharPolyTestOut QuadTestIn QuadTestOut


# test source files
TS=QuickTest.cpp ZZTest.cpp SSMulTest.cpp ZZ_pXTest.cpp lzz_pXTest.cpp BerlekampTest.cpp \
	CanZassTest.cpp ZZXFacTest.cpp  MoreFacTest.cpp \
	LLLTest.cpp subset.cpp MatrixTest.cpp mat_lzz_pTest.cpp  \
	CharPolyTest.cpp RRTest.cpp QuadTest.cpp GF2XTest.cpp GF2EXTest.cpp GF2EXGCDTest.cpp  \
	BitMatTest.cpp ZZ_pEXTest.cpp ZZ_pEXGCDTest.cpp lzz_pEXTest.cpp lzz_pEXGCDTest.cpp  \
	Timing.cpp ThreadTest.cpp ExceptionTest.cpp

# aux source to help compute crossovers
CROSS=GF2EXDivCross.cpp GF2EXGCDCross.cpp GF2EXKarCross.cpp GF2EXModCross.cpp


# scripts
SCRIPTS=MakeGetTime MakeGetPID MakeCheckFeatures ResetFeatures CopyFeatures  \
	TestScript dosify unixify RemoveProg configure DoConfig mfile cfile ppscript


# auxilliary source
MD=MakeDesc.cpp MakeDescAux.cpp newnames.cpp gen_gmp_aux.cpp gf2x_version_1_2_or_later_required.cpp
GT=GetTime0.cpp GetTime1.cpp GetTime2.cpp GetTime3.cpp GetTime4.cpp GetTime5.cpp TestGetTime.cpp
GP=GetPID1.cpp GetPID2.cpp TestGetPID.cpp
CH=CheckCompile.cpp GenConfigInfo.cpp CheckContract.cpp  \
	CheckThreads.cpp CheckGMP.cpp 

AUXPROGS = TestGetTime TestGetPID CheckFeatures CheckCompile GenConfigInfo CheckContract \
	CheckThreads

FEATURES=ALIGNED_ARRAY BUILTIN_CLZL LL_TYPE SSSE3 AVX PCLMUL AVX2 FMA AVX512F \
	COPY_TRAITS1 COPY_TRAITS2 CHRONO_TIME MACOS_TIME POSIX_TIME AES_NI KMA


# documentation


DFILES=copying.txt BasicThreadPool.txt GF2.txt GF2E.txt GF2EX.txt  \
	GF2EXFactoring.txt GF2X.txt GF2XFactoring.txt GF2XVec.txt HNF.txt Lazy.txt  \
	LazyTable.txt LLL.txt RR.txt SmartPtr.txt ZZ.txt ZZ_limbs.txt ZZVec.txt ZZX.txt  \
	ZZXFactoring.txt ZZ_p.txt ZZ_pE.txt ZZ_pEX.txt ZZ_pEXFactoring.txt ZZ_pX.txt  \
	ZZ_pXFactoring.txt conversions.txt flags.txt lzz_p.txt lzz_pE.txt lzz_pEX.txt  \
	lzz_pEXFactoring.txt lzz_pX.txt lzz_pXFactoring.txt mat_GF2.txt mat_GF2E.txt  \
	mat_RR.txt mat_ZZ.txt mat_ZZ_p.txt mat_ZZ_pE.txt mat_lzz_p.txt mat_lzz_pE.txt  \
	mat_poly_ZZ.txt mat_poly_ZZ_p.txt mat_poly_lzz_p.txt matrix.txt pair.txt  \
	vector.txt quad_float.txt sedscript.txt tools.txt vec_GF2.txt vec_GF2E.txt  \
	vec_RR.txt vec_ZZ.txt vec_ZZ_p.txt vec_ZZ_pE.txt vec_lzz_p.txt vec_lzz_pE.txt  \
	xdouble.txt names.txt tour-ack.html tour-intro.html tour-time.html  \
	tour-changes.html tour-modules.html tour-unix.html tour-examples.html  \
	tour-roadmap.html tour-win.html tour-impl.html tour-struct.html tour.html  \
	tour-ex1.html tour-ex2.html tour-ex3.html tour-ex4.html tour-ex5.html  \
	tour-ex6.html tour-ex7.html arrow1.gif arrow2.gif arrow3.gif tour-gmp.html  \
	tour-gf2x.html tour-tips.html config.txt version.txt



TXFILES=GF2.txt GF2E.txt GF2EX.txt GF2EXFactoring.txt GF2X.txt  \
	GF2XFactoring.txt GF2XVec.txt HNF.txt Lazy.txt LazyTable.txt LLL.txt RR.txt  \
	SmartPtr.txt ZZ.txt ZZ_limbs.txt ZZVec.txt ZZX.txt ZZXFactoring.txt ZZ_p.txt ZZ_pE.txt  \
	ZZ_pEX.txt ZZ_pEXFactoring.txt ZZ_pX.txt ZZ_pXFactoring.txt lzz_p.txt  \
	lzz_pE.txt lzz_pEX.txt lzz_pEXFactoring.txt lzz_pX.txt lzz_pXFactoring.txt  \
	mat_GF2.txt mat_GF2E.txt mat_RR.txt mat_ZZ.txt mat_ZZ_p.txt mat_ZZ_pE.txt  \
	mat_lzz_p.txt mat_lzz_pE.txt mat_poly_ZZ.txt mat_poly_ZZ_p.txt  \
	mat_poly_lzz_p.txt matrix.txt pair.txt quad_float.txt tools.txt vec_GF2.txt  \
	vec_GF2E.txt vec_RR.txt vec_ZZ.txt vec_ZZ_p.txt vec_ZZ_pE.txt vec_lzz_p.txt  \
	vec_lzz_pE.txt vector.txt version.txt xdouble.txt BasicThreadPool.txt


HTFILES=GF2.cpp.html GF2E.cpp.html GF2EX.cpp.html GF2EXFactoring.cpp.html  \
	GF2X.cpp.html GF2XFactoring.cpp.html GF2XVec.cpp.html HNF.cpp.html  \
	Lazy.cpp.html LazyTable.cpp.html LLL.cpp.html RR.cpp.html SmartPtr.cpp.html  \
	ZZ.cpp.html ZZ_limbs.cpp.html ZZVec.cpp.html ZZX.cpp.html ZZXFactoring.cpp.html ZZ_p.cpp.html  \
	ZZ_pE.cpp.html ZZ_pEX.cpp.html ZZ_pEXFactoring.cpp.html ZZ_pX.cpp.html  \
	ZZ_pXFactoring.cpp.html lzz_p.cpp.html lzz_pE.cpp.html lzz_pEX.cpp.html  \
	lzz_pEXFactoring.cpp.html lzz_pX.cpp.html lzz_pXFactoring.cpp.html  \
	mat_GF2.cpp.html mat_GF2E.cpp.html mat_RR.cpp.html mat_ZZ.cpp.html  \
	mat_ZZ_p.cpp.html mat_ZZ_pE.cpp.html mat_lzz_p.cpp.html mat_lzz_pE.cpp.html  \
	mat_poly_ZZ.cpp.html mat_poly_ZZ_p.cpp.html mat_poly_lzz_p.cpp.html  \
	matrix.cpp.html pair.cpp.html quad_float.cpp.html tools.cpp.html  \
	vec_GF2.cpp.html vec_GF2E.cpp.html vec_RR.cpp.html vec_ZZ.cpp.html  \
	vec_ZZ_p.cpp.html vec_ZZ_pE.cpp.html vec_lzz_p.cpp.html vec_lzz_pE.cpp.html  \
	vector.cpp.html version.cpp.html xdouble.cpp.html BasicThreadPool.cpp.html



DOC = $(DFILES) $(HTFILES) TFT-time.jpg zmulrat.jpg flintrat.jpg


# test program executables
PROGS=QuickTest ZZTest SSMulTest ZZ_pXTest lzz_pXTest BerlekampTest CanZassTest \
	ZZXFacTest MoreFacTest LLLTest  \
	BitMatTest MatrixTest mat_lzz_pTest CharPolyTest RRTest QuadTest GF2XTest  \
	GF2EXTest GF2EXGCDTest subset ZZ_pEXTest ZZ_pEXGCDTest lzz_pEXTest lzz_pEXGCDTest \
	Timing ThreadTest

# things to save to a tar file
SFILES=$(SRC) $(SCRIPTS) $(MD) $(GT) $(GP) $(CH) $(TS) $(TD) $(CROSS) mach_desc.win   \
	Poly1TimeTest.cpp Poly2TimeTest.cpp Poly3TimeTest.cpp GF2XTimeTest.cpp    \
	InitSettings.cpp DispSettings.cpp  WizardAux Wizard 


#################################################################
#
#  Rules for compiling the library
#
#################################################################


NTL_INCLUDE = -I../include -I.
# NTL needs this to find its include files

COMPILE = $(CXX) $(NTL_INCLUDE) $(CPPFLAGS) $(CXXFLAGS) $(CXXAUTOFLAGS)  -c

LINK = $(CXX) $(NTL_INCLUDE) $(CPPFLAGS) $(CXXFLAGS) $(CXXAUTOFLAGS) $(LDFLAGS)



# 'make' or 'make all' does a complete make, including additional
# setup not done in configure.

# The file setup-phase is removed by the configure script
# when it terminates successfully.

# The file need-to-run-configure is removed by the configure script
# before making any changes to makefile/config.h and is recreated
# when it terminates successfully.

all: setup-phase
	$(MAKE) ntl.a

setup-phase: need-to-run-configure
	$(MAKE) clobber
	$(MAKE) setup1
	$(MAKE) setup2
	$(MAKE) setup3
	$(MAKE) setup4
	touch setup-phase


# setup1 generates the file ../incluse/NTL/mach_desc.h

setup1: 
	$(COMPILE) MakeDescAux.cpp
	$(LINK) -o MakeDesc MakeDesc.cpp MakeDescAux.o $(LDLIBS)
	./MakeDesc
	mv mach_desc.h ../include/NTL/mach_desc.h


# setup2 does some dynamic checks for GetTime, GetPID, and other features

setup2:
	echo "*** CheckFeatures log ***" > CheckFeatures.log
	sh MakeGetTime "$(LINK)" "$(LDLIBS)"
	sh MakeGetPID  "$(LINK)" "$(LDLIBS)"
	sh MakeCheckFeatures "$(FEATURES)" "$(LINK)" "$(LDLIBS)"

# NOTE: to add a feature XXX:
#   * add a program CheckXXX.cpp which returns 0 if XXX works, -1 otherwise
#   * add XXX to the FEATURES variable

# setup3 generates the file ../include/NTL/gmp_aux.h
# The file ../include/NTL/gmp_aux.h is included in ../include/NTL/lip.h
# when NTL_GMP_LIP is set.
# When this flag is not set, an empty files produced.
# This also checks that the right version of gf2x library.

setup3:
	$(LINK) $(GMP_OPT_INCDIR) -o gen_gmp_aux gen_gmp_aux.cpp $(GMP_OPT_LIBPATH) $(GMP_OPT_LIB) $(LDLIBS)
	./gen_gmp_aux > ../include/NTL/gmp_aux.h 
	$(LINK) $(GF2X_OPT_INCDIR) -o gf2x_version_1_2_or_later_required gf2x_version_1_2_or_later_required.cpp $(GF2X_OPT_LIBPATH) $(GF2X_OPT_LIB) $(LDLIBS) 

# setup4 runs the wizard

setup4:
	sh Wizard $(WIZARD) "$(MAKE)" "$(FEATURES)"

#### commands to build things

@{STAT}LIBTOOL_LINK_AS=-static
@{SHAR}LIBTOOL_LINK_AS=-version-info `cat VERSION_INFO`

ntl.a:	$(OBJ) 
@{NOLTOOL}	$(AR) $(ARFLAGS) ntl.a $(OBJ) #NOLTOOL
@{NOLTOOL}	- $(RANLIB) ntl.a #NOLTOOL
@{LTOOL}	$(LIBTOOL) --tag=CXX --mode=link $(LINK) $(LIBTOOL_LINK_FLAGS) -o libntl.la $(OBJ:.o=.lo) $(GMP_OPT_LIBDIR) $(GMP_OPT_LIB) $(GF2X_OPT_LIBDIR) $(GF2X_OPT_LIB) $(LDLIBS) $(LIBTOOL_LINK_LIBS) -rpath $(LIBDIR) $(LIBTOOL_LINK_AS) #LTOOL

LCOMP=@{LTOOL}$(LIBTOOL) --tag=CXX --mode=compile #LTOOL
LCOMPX=@{LTOOL}@{PPIC} -prefer-pic #LTOOL&PPIC 

LCOMPILE=$(LCOMP) $(COMPILE) $(LCOMPX)

lip.o:	lip.cpp 
	$(LCOMPILE) $(GMP_OPT_INCDIR) lip.cpp

GF2X.o:	GF2X.cpp
	$(LCOMPILE) $(GF2X_OPT_INCDIR) GF2X.cpp

mat_lzz_p.o:    mat_lzz_p.cpp
	$(LCOMPILE) mat_lzz_p.cpp

quad_float.o:	quad_float.cpp
	$(LCOMPILE) $(NOCONTRACT) quad_float.cpp

pd_FFT.o: pd_FFT.cpp
	$(LCOMPILE) $(NOCONTRACT) pd_FFT.cpp

TESTRPATH=@{TESTRPATH}
CheckCompile: CheckCompile.cpp
	$(LINK) -o  CheckCompile CheckCompile.cpp $(TESTRPATH) $(LDLIBS) 

GenConfigInfo: GenConfigInfo.cpp
	$(LINK) -o  GenConfigInfo GenConfigInfo.cpp $(LDLIBS)

CheckContract: CheckContract.cpp MakeDescAux.cpp
	$(LINK) $(NOCONTRACT) -o  CheckContract CheckContract.cpp MakeDescAux.cpp $(LDLIBS)

CheckThreads: CheckThreads.cpp
	$(LINK) -o  CheckThreads CheckThreads.cpp $(LDLIBS)

CheckGMP: CheckGMP.cpp
	$(LINK) $(GMP_OPT_INCDIR) -o  CheckGMP CheckGMP.cpp $(GMP_OPT_LIBPATH) $(GMP_OPT_LIB) $(LDLIBS)



.cpp.o: 
	$(LCOMPILE) $<

.cpp: 
@{NOLTOOL}	$(LINK) -o $@ $< ntl.a $(GMP_OPT_LIBPATH) $(GMP_OPT_LIB) $(GF2X_OPT_LIBPATH) $(GF2X_OPT_LIB) $(LDLIBS) #NOLTOOL
@{LTOOL}	$(LIBTOOL) --tag=CXX --mode=link $(LINK) -o $@ $< libntl.la #LTOOL

#### auxilliary stuff to help build USER_MAKEFILE.txt ntl.pc

INSTALLED_LIBDIR=@{NTLL}-L$(LIBDIR) 
INSTALLED_RPATH=@{SHAR}@{RPATH}@{NTLL}-Wl,-rpath,$(LIBDIR)
INSTALLED_LIBPATH=$(INSTALLED_LIBDIR) $(INSTALLED_RPATH)

INSTALLED_INCLUDE=@{NTLI}-I$(INCLUDEDIR) 

INSTALLED_LINK = $(CXX) $(INSTALLED_INCLUDE) $(CPPFLAGS) $(CXXFLAGS) $(CXXAUTOFLAGS) $(LDFLAGS)


user_makefile:
#
#common
#
	@printf "# This is a sample makefile that can be used to compile programs using \n"
	@printf "# an installed version of NTL.\n"
	@printf "# Using this ensures that your programs compile with flags consistent\n"
	@printf "# with the build, and link to the right libraries.\n"
	@printf "#\n"
	@printf "# The basic functionality provided by this makefile is to allow\n"
	@printf "# you to compile 'foo.cpp' into the executable file 'foo' by executing\n"
	@printf "#    make foo   \n"
	@printf "# You can adapt this to the needs of your own project as necessary.\n"
	@printf "\n\n";
#
#static
#
@{STAT}	@printf '.cpp:\n'
@{STAT}	@printf '\t$(INSTALLED_LINK) $$< -o $$@  $(INSTALLED_LIBPATH) -lntl $(GMP_OPT_LIBPATH) $(GMP_OPT_LIB) $(GF2X_OPT_LIBPATH) $(GF2X_OPT_LIB) $(LDLIBS)\n'
@{STAT}	@printf '\n'
@{STAT}	@printf '# ntl was installed as a static library with dependencies:'
#
#shared
#
@{SHAR}	@printf '.cpp:\n'
@{SHAR}	@printf '\t$(INSTALLED_LINK) -o $$@ $$< $(INSTALLED_LIBPATH) -lntl\n'
@{SHAR}	@printf '\n'
@{SHAR}	@printf '# ntl was installed as a shared library with dependencies:'
#
#common
#
@{GMP}	@printf ' gmp'
@{GF2X}	@printf ' gf2x'
	@printf '\n\n'
	@printf '# COMPILER OPTIONS\n'
@{NTLI}	@printf '# "-I$(INCLUDEDIR)" ensures ntl header files found at compile time\n'
	@case '$(CXXFLAGS)' in *' -g '*|'-g '*|*' -g'|'-g') printf '# "-g" enables the debugger\n' ;; esac
	@case '$(CXXFLAGS)' in *'-O2'*) printf '# "-O2" enables (level 2) optimization (highly recommend not changing)\n' ;; esac
	@case '$(CXXAUTOFLAGS)' in *'-std=c++11'*) printf '# "-std=c++11" enables the C++11 standard\n' ;; esac
	@case '$(CXXAUTOFLAGS)' in *'-std=c++14'*) printf '# "-std=c++14" enables the C++14 standard\n' ;; esac
	@case '$(CXXAUTOFLAGS)' in *'-pthread'*) printf '# "-pthread" enables multi-threading (also needed for linking)\n' ;; esac
	@case '$(CXXAUTOFLAGS)' in *'-march=native'*) printf '# "-march=native" targets compilation to the native instruction set\n' ;; esac
	@case '$(CXXAUTOFLAGS)' in *'-mcpu=native'*) printf '# "-mcpu=native" targets compilation to the native instruction set\n#    (preferred over -march=native for arm platforms)\n' ;; esac
	@case '$(CXXAUTOFLAGS)' in *'-fp-model precise'*) printf '# "-fp-model precise" needed in icc compiler to keep floating point accurate\n' ;; esac
	@printf '# "$$<" expands to "foo.cpp" when run as "make foo"\n'
	@printf '\n# LINKER OPTIONS\n'
	@printf '# "-o $$@" expands to "-o foo" when run as "make foo"\n'
#
#static
#
@{STAT}@{NTLL}	@printf '# "-L$(LIBDIR)" ensures ntl is found at link time\n'
@{STAT}	@printf '# "-lntl" ensures that program will link to ntl\n'
@{STAT}@{GMPL}	@printf '# "-L$(GMP_LIBDIR)" ensures that gmp is found at link time\n' 
@{STAT}@{GMPL}@{RPATH}	@printf '# "-Wl,-rpath,$(GMP_LIBDIR)" ensures gmp is found at runtime\n#    if it is a shared lib\n#    alternatively, add $(GMP_LIBDIR) to LD_LIBRARY_PATH\n' 
@{STAT}@{GMPL}	@printf '# "-lgmp" ensures that program will link to gmp\n' 
@{STAT}@{GF2XL}	@printf '# "-L$(GF2X_LIBDIR)" ensures that gf2x is found at link time\n' 
@{STAT}@{GF2XL}@{RPATH}	@printf '# "-Wl,-rpath,$(GF2X_LIBDIR)" ensures gf2x is found at runtime\n#    if it is a shared lib\n#    alternatively, add $(GF2X_LIBDIR) to LD_LIBRARY_PATH\n' 
@{STAT}@{GF2XL}	@printf '# "-lgf2x" ensures that program will link to gf2x\n' 
#
#shared
#
@{SHAR}@{NTLL}	@printf '# "-L$(LIBDIR)" ensures ntl is found at link time\n'
@{SHAR}@{RPATH}@{NTLL}	@printf '# "-Wl,-rpath,$(LIBDIR)" ensures ntl is found at runtime\n#    alternatively, add $(LIBDIR) to LD_LIBRARY_PATH\n' 
@{SHAR}	@printf '# "-lntl" ensures that program will link to ntl\n'
#
#common
#
@{LTOOL}	@printf '\n# ntl was installed using libtool\n'
@{LTOOL}	@printf '\n# in your project, you could alternatively compile and link as follows:\n'
@{LTOOL}	@printf '#\tlibtool --tag=CXX --mode=link $(INSTALLED_LINK) -o $$@ $$< $(INSTALLED_LIBDIR) -lntl\n'
@{LTOOL}	@printf '# which ensures ntl all dependencies are properly linked and accessible at runtime\n'
@{LTOOL}	@printf '# and may offer convenient access to more linkage options via libtool\n'
@{LTOOL}@{DARWIN}	@printf '# - on macOS, you may need to use someling like glibtool installed by (say)\n# - homebrew, and not the system libtool\n'




REQUIRES_GMP=@{GMP}gmp >= 5.0
REQUIRES_GF2X=@{GF2X}gf2x >= 1.2
# these version numbers are generic minimal requirments and do not
# reflect the versions detected during the build

pkg_config:
	@printf 'includedir=$(INCLUDEDIR)\n'
	@printf 'libdir=$(LIBDIR)\n'
	@printf 'Name: NTL\n'
	@printf 'Description: A Library for doing Number Theory\n'
	@printf "Version: $$(sed 's/^ntl-//' DIRNAME)\n"
	@printf 'Cflags: -I$${includedir}'
	@case '$(CXXAUTOFLAGS)' in *'-std=c++11'*) printf ' -std=c++11' ;; esac
	@case '$(CXXAUTOFLAGS)' in *'-std=c++14'*) printf ' -std=c++14' ;; esac
	@case '$(CXXAUTOFLAGS)' in *'-pthread'*) printf ' -pthread' ;; esac
	@case '$(CXXAUTOFLAGS)' in *'-fp-model precise'*) printf ' -fp-model precise' ;; esac
	@printf '\n'
	@printf 'Libs: -L$${libdir} -lntl' 
	@case '$(CXXAUTOFLAGS)' in *'-pthread'*) printf ' -pthread' ;; esac
	@printf '\n'
	@printf 'Requires.private: $(REQUIRES_GMP) $(REQUIRES_GF2X)\n' 




#################################################################
#
# Rule for running tests
# make check runs a series of tests
#
#################################################################

check:
	sh RemoveProg $(PROGS)
	$(MAKE) QuickTest
	./QuickTest
	sh RemoveProg QuickTest
	sh TestScript "$(MAKE)"

#################################################################
#
# Rule for installing
# make install just does a simple copy of the include file
#   and library.  The -p option is used to preserve file attributes.
#   This helps avoid some problems (especially when copying ntl.a).
#   Also, an attempt is made to make everything that is
#   installed readable by everyone.
#
# make uninstall removes these files
#   
#################################################################




install:
	mkdir -p -m 755 $(DESTDIR)$(INCLUDEDIR)
	rm -rf $(DESTDIR)$(INCLUDEDIR)/NTL
	mkdir -m 755 $(DESTDIR)$(INCLUDEDIR)/NTL
	cp -p ../include/NTL/*.h $(DESTDIR)$(INCLUDEDIR)/NTL
	cp -p ../include/NTL/*.txt $(DESTDIR)$(INCLUDEDIR)/NTL
	- chmod -R a+r $(DESTDIR)$(INCLUDEDIR)/NTL
	mkdir -p -m 755 $(DESTDIR)$(DOCDIR)
	rm -rf $(DESTDIR)$(DOCDIR)/NTL
	mkdir -m 755 $(DESTDIR)$(DOCDIR)/NTL
	cp -p ../doc/*.txt $(DESTDIR)$(DOCDIR)/NTL
	cp -p ../doc/*.html $(DESTDIR)$(DOCDIR)/NTL
	cp -p ../doc/*.gif $(DESTDIR)$(DOCDIR)/NTL
	- chmod -R a+r $(DESTDIR)$(DOCDIR)/NTL
	mkdir -p -m 755 $(DESTDIR)$(PKGDIR)
	cp -p ../lib/pkgconfig/ntl.pc $(DESTDIR)$(PKGDIR)
	mkdir -p -m 755 $(DESTDIR)$(LIBDIR)
@{NOLTOOL}	cp -p ntl.a $(DESTDIR)$(LIBDIR)/libntl.a #NOLTOOL
@{NOLTOOL}	- chmod a+r $(DESTDIR)$(LIBDIR)/libntl.a #NOLTOOL
@{LTOOL}	$(LIBTOOL) --mode=install cp -p libntl.la $(DESTDIR)$(LIBDIR) #LTOOL
	@echo 
	@echo '*** SUMMARY ***'
	@echo '* include files installed to $(DESTDIR)$(INCLUDEDIR)/NTL'
	@echo '* library files installed to $(DESTDIR)$(LIBDIR)'
	@echo '* doc files installed to $(DESTDIR)$(DOCDIR)/NTL'
	@echo '* pkg-config file installed to $(DESTDIR)$(PKGDIR)/ntl.pc'
	@echo '** see $(DESTDIR)$(INCLUDEDIR)/NTL/CONFIG_LOG.txt for a summary of the configuration'
	@echo '** see $(DESTDIR)$(INCLUDEDIR)/NTL/USER_MAKEFILE.txt for an example makefile to build user programs'


uninstall:
@{NOLTOOL}	rm -f $(DESTDIR)$(LIBDIR)/libntl.a #NOLTOOL
@{LTOOL}	$(LIBTOOL) --mode=uninstall rm -f $(DESTDIR)$(LIBDIR)/libntl.la #LTOOL
	rm -rf $(DESTDIR)$(INCLUDEDIR)/NTL
	rm -rf $(DESTDIR)$(DOCDIR)/NTL
	rm -f $(DESTDIR)$(PKGDIR)/ntl.pc

#################################################################
#
#  Rules for cleaning up
#
#  make clobber removes *everything* created by make, except
#    config.h is left alone
#
#  make clean tidies up a bit
#
#################################################################

clobber:	
	rm -f ntl.a GetTime.cpp GetPID.cpp
	rm -f mach_desc.h ../include/NTL/mach_desc.h  
	rm -f ../include/NTL/gmp_aux.h
	sh ResetFeatures '..' "$(FEATURES)" 
	sh RemoveProg $(PROGS) MakeDesc $(AUXPROGS) gen_gmp_aux gf2x_version_1_2_or_later_required
	rm -f *.o
	rm -rf small
	rm -rf .libs *.lo libntl.la
	rm -f setup-phase

clean:
	sh RemoveProg $(PROGS) MakeDesc $(AUXPROGS) gen_gmp_aux gf2x_version_1_2_or_later_required
	rm -f *.o 
	rm -rf small
@{LTOOL}	- $(LIBTOOL) --mode=clean rm -f libntl.la *.lo #LTOOL

wclean:
	rm -f *.o 

#################################################################
#
#  Rules for making tar and zip files
# 
#  make libtool-gen-origin generates the directory 
#     libtool-origin used to include in the distribution
#     - this only needs to be run very occasionally, to keep
#       libtool relatively up-to-date
#     - it must be run on a machine with autotools
#
#  make ppdoc creates pretty-printed versions of some documentation
#     - run before make package or make winpack
#
#  make package creates a tar.gz file suitable for Unix
#
#  make winpack creates a zip file suitable for Windows
#
#################################################################

libtool-gen-origin:
	rm -rf libtool-origin-tmp && \
	   cp -R libtool-seed libtool-origin-tmp && \
	   cd libtool-origin-tmp && autoreconf -fiv && rm -rf autom4te.cache
	rm -rf libtool-origin && cp -R libtool-origin-tmp libtool-origin 

ppdoc:
	sh ppscript "$(TXFILES)"

ppclean:
	rm -f ../doc/*.cpp


TAR=tar 
# on macOS, should use gtar to avoid xattr data being included
# no need to make this a config option: just run "make package TAR=gtar"

package:
	sh unixify "$(SFILES) DIRNAME WINDIR VERSION_INFO NOTES" "$(INCL)" "$(DOC)" "$(FEATURES)"
	rm -rf `cat DIRNAME`
	rm -f `cat DIRNAME`.tar
	rm -f `cat DIRNAME`.tar.gz
	mv unix `cat DIRNAME`
	chmod -R a+rX `cat DIRNAME`
	$(TAR) -cvf `cat DIRNAME`.tar `cat DIRNAME`
	gzip `cat DIRNAME`.tar
	rm -rf `cat DIRNAME`

winpack:
	./configure --nowrite NTL_GMP_LIP=off NTL_TLS_HACK=off ENABLE_RPATH=off
	sh dosify "$(SRC)" "$(INCL)" "$(DOC)" "$(TS)" "$(TD)" "$(FEATURES)"
	rm -rf `cat WINDIR`
	rm -f `cat WINDIR`.zip 
	mv dos `cat WINDIR`
	chmod -R a+rX `cat WINDIR`
	find ./`cat WINDIR` '!' '(' -name '*.gif' -o -name '*.jpg' ')'  -print | zip -l `cat WINDIR` -@
	find ./`cat WINDIR` -name '*.gif' -o -name '*.jpg' -print | zip -u `cat WINDIR` -@
	rm -rf `cat WINDIR`


######################################################################
#
#  config wizard related stuff
#
######################################################################

WOBJ=FFT.o GetTime.o GetPID.o ctools.o ZZ.o ZZVec.o ZZ_p.o ZZ_pX.o ZZ_pX1.o  \
	lip.o tools.o vec_ZZ.o vec_ZZ_p.o GF2.o WordVector.o vec_GF2.o GF2X.o GF2X1.o  \
	thread.o BasicThreadPool.o fileio.o


#@{LTOOL}wntl.a: LCOMP= #LTOOL
#@{LTOOL}wntl.a: LCOMPX= #LTOOL
wntl.a:	$(WOBJ) 
	$(AR) $(ARFLAGS) wntl.a $(WOBJ)
	- $(RANLIB) wntl.a


Poly1TimeTest:
	$(LINK) -o Poly1TimeTest Poly1TimeTest.cpp wntl.a  $(GMP_OPT_LIBPATH) $(GMP_OPT_LIB) $(LDLIBS)
Poly2TimeTest:
	$(LINK) -o Poly2TimeTest Poly2TimeTest.cpp wntl.a  $(GMP_OPT_LIBPATH) $(GMP_OPT_LIB) $(LDLIBS)
Poly3TimeTest:
	$(LINK) -o Poly3TimeTest Poly3TimeTest.cpp wntl.a  $(GMP_OPT_LIBPATH) $(GMP_OPT_LIB) $(LDLIBS)


GF2XTimeTest:
	$(LINK) -o GF2XTimeTest GF2XTimeTest.cpp wntl.a  $(GMP_OPT_LIBPATH) $(GMP_OPT_LIB) $(LDLIBS)

InitSettings:
	$(LINK) -o InitSettings InitSettings.cpp  $(LDLIBS)


DispSettings:
	$(LINK) -o DispSettings DispSettings.cpp $(LDLIBS)

