1#!/bin/sh
2
3# (C) 2007, Robert Bradshaw, William Hart, William Stein, Michael Abshoff
4# (C) 2011, William Hart
5# (C) 2012, William Hart, Jean-Pierre Flori, Thomas DuBuisson
6# (C) 2012, Jan Engelhardt
7# (C) 2017, Vincent Delecroix
8
9# soname version
10#
11# antic   => soname
12# 0.0.1   => 0.0.0
13# 0.2.0   => 0.0.0
14# 0.2.1   => 0.0.0
15# 0.2.2   => 0.0.0
16# 0.2.3   => 0.0.0
17# 0.2.4   => 0.2.4
18
19ANTIC_MAJOR=0
20ANTIC_MINOR=2
21ANTIC_PATCH=4
22
23PREFIX="/usr/local"
24GMP_DIR="/usr/local"
25MPFR_DIR="/usr/local"
26FLINT_DIR="/usr/local"
27NTL_DIR="/usr/local"
28GC_DIR="/usr/local"
29BLAS_DIR="/usr/local"
30WANT_NTL=0
31WANT_BLAS=0
32SHARED=1
33STATIC=1
34TLS=1
35PTHREAD=1
36REENTRANT=0
37WANT_GC=0
38WANT_TLS=0
39WANT_CXX=0
40ASSERT=0
41BUILD=
42EXTENSIONS=
43EXT_MODS=
44EXTRA_BUILD=
45
46usage()
47{
48   echo "Usage: ./configure <options> <args>"
49   echo "   where <options> may be"
50   echo "     -h display usage information"
51   echo "   where <args> may be:"
52   echo "     --prefix=<path>      Specify path to installation location (default: /usr/local)"
53   echo "     --with-mpir=<path>   Specify location of MPIR (default: /usr/local)"
54   echo "     --with-gmp=<path>    Specify location of GMP (default: /usr/local)"
55   echo "     --with-mpfr=<path>   Specify location of MPFR (default: /usr/local)"
56   echo "     --with-flint=<path>  Specify location of FLINT (default: /usr/local)"
57   echo "     --extensions=<path>  Specify location of extension modules"
58   echo "     --build=arch-os      Specify architecture/OS combination rather than use values from uname -m/-s"
59   echo "     --enable-shared      Build a shared library (default)"
60   echo "     --disable-shared     Do not build a shared library"
61   echo "     --enable-static      Build a static library (default)"
62   echo "     --disable-static     Do not build a static library"
63   echo "     --single             Faster [non-reentrant if tls or pthread not used] version of library (default)"
64   echo "     --reentrant          Build fully reentrant [with or without tls, with pthread] version of library"
65   echo "     --with-gc=<path>     GC safe build with path to gc"
66   echo "     --enable-pthread     Use pthread (default)"
67   echo "     --disable-pthread    Do not use pthread"
68   echo "     --enable-tls         Use thread-local storage (default)"
69   echo "     --disable-tls        Do not use thread-local storage"
70   echo "     --enable-assert      Enable use of asserts (use for debug builds only)"
71   echo "     --disable-assert     Disable use of asserts (default)"
72   echo "     --enable-cxx         Enable C++ wrapper tests"
73   echo "     --disable-cxx        Disable C++ wrapper tests (default)"
74   echo "     CC=<name>            Use the C compiler with the given name (default: gcc)"
75   echo "     CXX=<name>           Use the C++ compiler with the given name (default: g++)"
76   echo "     AR=<name>            Use the AR library builder with the given name (default: ar)"
77   echo "     CFLAGS=<flags>       Pass the given flags to the compiler"
78   echo "     ABI=[32|64]          Tell the compiler to use given ABI (default: empty)"
79}
80
81
82absolute_path(){
83   dirlist="$1"
84   retval=""
85   for dir in $dirlist; do
86      case $dir in
87        /*) dir=$dir;;
88        *) dir=$PWD/$dir;;
89      esac
90      retval=$retval" "$dir
91   done
92   echo $retval
93}
94
95while [ "$1" != "" ]; do
96   PARAM=`echo $1 | sed 's/=.*//'`
97   VALUE=`echo $1 | sed 's/[^=]*//; s/=//'`
98   case "$PARAM" in
99      -h|--help)
100         usage
101         exit 0
102         ;;
103      --with-mpir|--with-gmp)
104         GMP_DIR=$(absolute_path "$VALUE")
105         ;;
106      --with-mpfr)
107         MPFR_DIR=$(absolute_path "$VALUE")
108         ;;
109      --with-flint)
110         FLINT_DIR=$(absolute_path "$VALUE")
111         ;;
112      --extensions)
113         EXTENSIONS=$(absolute_path "$VALUE")
114         ;;
115      --build)
116         BUILD="$VALUE"
117         ;;
118      --prefix)
119         PREFIX=$VALUE
120         ;;
121      --enable-shared)
122         SHARED=1
123         ;;
124      --disable-shared)
125         SHARED=0
126         ;;
127      --enable-static)
128         STATIC=1
129         ;;
130      --disable-static)
131         STATIC=0
132         ;;
133      --single)
134         REENTRANT=0
135         ;;
136      --reentrant)
137         REENTRANT=1
138         ;;
139      --with-gc)
140	 WANT_GC=1
141         if [ ! -z "$VALUE" ]; then
142            GC_DIR="$VALUE"
143         fi
144         ;;
145      --enable-pthread)
146         PTHREAD=1
147         ;;
148      --disable-pthread)
149         PTHREAD=0
150         ;;
151      --enable-tls)
152         TLS=1
153         WANT_TLS=1;;
154      --disable-tls)
155         TLS=0
156         ;;
157      --enable-assert)
158         ASSERT=1
159         ;;
160      --disable-assert)
161         ASSERT=0
162         ;;
163      --enable-cxx)
164         WANT_CXX=1
165         ;;
166      --disable-cxx)
167         WANT_CXX=0
168         ;;
169      AR)
170         AR="$VALUE"
171         ;;
172      CC)
173         CC="$VALUE"
174         ;;
175      CXX)
176         CXX="$VALUE"
177         ;;
178      CFLAGS)
179         CFLAGS="$VALUE"
180         ;;
181      ABI)
182         ABI="$VALUE"
183         ;;
184      *)
185         usage
186         exit 1
187         ;;
188   esac
189   shift
190done
191
192#find dependencies
193
194LIBS="m"
195
196if [ -d "${GMP_DIR}/lib" ]; then
197   GMP_LIB_DIR="${GMP_DIR}/lib"
198   GMP_INC_DIR="${GMP_DIR}/include"
199elif [ -d "${GMP_DIR}/lib64" ]; then
200   GMP_LIB_DIR="${GMP_DIR}/lib64"
201   GMP_INC_DIR="${GMP_DIR}/include"
202elif [ -d "${GMP_DIR}/.libs" ]; then
203   GMP_LIB_DIR="${GMP_DIR}/.libs"
204   GMP_INC_DIR="${GMP_DIR}"
205else
206   echo "Invalid GMP directory"
207   exit 1
208fi
209LIB_DIRS="${LIB_DIRS} ${GMP_LIB_DIR}"
210INC_DIRS="${INC_DIRS} ${GMP_INC_DIR}"
211LIBS="${LIBS} gmp"
212
213if [ -d "${MPFR_DIR}/lib" ]; then
214   MPFR_LIB_DIR="${MPFR_DIR}/lib"
215   MPFR_INC_DIR="${MPFR_DIR}/include"
216elif [ -d "${MPFR_DIR}/lib64" ]; then
217   MPFR_LIB_DIR="${MPFR_DIR}/lib64"
218   MPFR_INC_DIR="${MPFR_DIR}/include"
219elif [ -d "${MPFR_DIR}/.libs" ]; then
220   MPFR_LIB_DIR="${MPFR_DIR}/.libs"
221   MPFR_INC_DIR="${MPFR_DIR}"
222elif [ -d "${MPFR_DIR}/src/.libs" ]; then
223   MPFR_LIB_DIR="${MPFR_DIR}/src/.libs"
224   MPFR_INC_DIR="${MPFR_DIR}/src"
225else
226   echo "Invalid MPFR directory"
227   exit 1
228fi
229LIB_DIRS="${LIB_DIRS} ${MPFR_LIB_DIR}"
230INC_DIRS="${INC_DIRS} ${MPFR_INC_DIR}"
231LIBS="${LIBS} mpfr"
232
233if [ -d "${FLINT_DIR}/lib" ]; then
234   FLINT_LIB_DIR="${FLINT_DIR}/lib"
235   FLINT_INC_DIR="${FLINT_DIR}/include"
236elif [ -d "${FLINT_DIR}/lib64" ]; then
237   FLINT_LIB_DIR="${FLINT_DIR}/lib64"
238   FLINT_INC_DIR="${FLINT_DIR}/include"
239elif [ -d "${FLINT_DIR}/.libs" ]; then
240   FLINT_LIB_DIR="${FLINT_DIR}/.libs"
241   FLINT_INC_DIR="${FLINT_DIR}"
242elif [ -d "${FLINT_DIR}" ]; then
243   FLINT_LIB_DIR="${FLINT_DIR}"
244   FLINT_INC_DIR="${FLINT_DIR}"
245else
246   echo "Invalid FLINT directory"
247   exit 1
248fi
249
250if [ -d "${FLINT_INC_DIR}/flint" ]; then
251   FLINT_INC_DIR="${FLINT_INC_DIR}"
252elif [ -f "${FLINT_INC_DIR}/flint.h" ]; then
253   mkdir -p build/include
254   ln -sf ${FLINT_INC_DIR} build/include/flint
255   FLINT_INC_DIR="${PWD}/build/include"
256fi
257
258echo "FLINT_LIB_DIR set to ${FLINT_LIB_DIR}"
259echo "FLINT_INC_DIR set to ${FLINT_INC_DIR}"
260
261LIB_DIRS="${LIB_DIRS} ${FLINT_LIB_DIR}"
262INC_DIRS="${INC_DIRS} ${FLINT_INC_DIR}"
263LIBS="${LIBS} flint"
264
265#configure extra libraries
266
267if [ "$WANT_GC" = "1" ]; then
268   if [ -d "${GC_DIR}" ]; then
269      GC_LIB_DIR="${GC_DIR}/lib"
270      GC_INC_DIR="${GC_DIR}/include"
271   else
272      echo "Invalid GC directory"
273      exit 1
274   fi
275   EXTRA_INC_DIRS="${EXTRA_INC_DIRS} ${GC_INC_DIR}"
276   EXTRA_LIB_DIRS="${EXTRA_LIB_DIRS} ${GC_LIB_DIR}"
277   EXTRA_LIBS="${EXTRA_LIBS} gc"
278fi
279CONFIG_GC="#define HAVE_GC ${WANT_GC}"
280
281# defaults for CC, CXX and AR
282
283if [ -z "$CC" ]; then
284   CC=gcc
285fi
286
287if [ -z "$CXX" ]; then
288   CXX=g++
289fi
290
291if [ -z "$AR" ]; then
292   AR=ar
293fi
294
295# Architecture handler
296
297KERNEL=`uname`
298
299if [ -z "$BUILD" ]; then
300   ARCH=`uname -m`
301
302   if [ "$(uname | cut -d_ -f1)" = "MINGW32" ]; then
303      if [ "$ABI" = "64" ]; then
304         OS="MINGW64"
305      else
306         OS="MINGW32"
307      fi
308   elif [ "$(uname | cut -d_ -f1)" = "CYGWIN" ]; then
309      if [ "$ARCH" = "x86_64" ]; then
310         if [ "$ABI" = "32" ]; then
311            OS="CYGWIN32"
312         else
313            OS="CYGWIN64"
314            ABI="64"
315         fi
316      else
317         OS="CYGWIN32"
318      fi
319   else
320      OS=`uname -s`
321   fi
322else
323   ARCH=`echo "$BUILD" | cut -d- -f1`
324   OS=`echo "$BUILD" | cut -d- -f2`
325fi
326
327case "$ARCH" in
328   x86_64 | amd64)
329      MACHINE="x86_64";;
330   x86 | i*86 | pc)
331      MACHINE="x86";;
332   ia64)
333      MACHINE="ia64";;
334   sparc | sun4*)
335      MACHINE="sparc";;
336   sparc64)
337      MACHINE="sparc64";;
338   ppc64 | powerpc64)
339      MACHINE="ppc64";;
340   ppc | powerpc | [P|p]ower*)
341      MACHINE="ppc";;
342   *)
343      MACHINE="unknown";;
344esac
345
346#ABI flag
347if [ "$ABI" = "32" ]; then
348   ABI_FLAG="-m32"
349   case "$MACHINE" in
350      x86_64)
351         MACHINE="x86";;
352      sparc64)
353         MACHINE="sparc";;
354      ppc64)
355         MACHINE="ppc";;
356      *)
357         ;;
358   esac
359elif [ "$ABI" = "64" ]; then
360   ABI_FLAG="-m64"
361   if [ "$MACHINE" = "sparc" ]; then
362      MACHINE="sparc64"
363   fi
364   if [ "$MACHINE" = "x86" ]; then
365      MACHINE="x86_64"
366   fi
367fi
368
369if [ "$MACHINE" = "sparc" ] || [ "$MACHINE" = "sparc64" ]; then
370   if [ "$CC" = "gcc" ]; then
371      CC="gcc -mno-relax"
372   fi
373fi
374
375echo "Configuring...${MACHINE}-${OS}"
376
377#name for ANTIC shared library
378
379ANTIC_SOLIB=0
380if [ -z "$ANTIC_LIB" ]; then
381   case "$OS" in
382      Darwin)
383         ANTIC_LIBNAME="libantic.dylib"
384	 ANTIC_LIB="libantic-$ANTIC_MAJOR.$ANTIC_MINOR.$ANTIC_PATCH.dylib"
385         EXTRA_SHARED_FLAGS="-install_name $PREFIX/lib/$ANTIC_LIB -compatibility_version $ANTIC_MAJOR.$ANTIC_MINOR -current_version $ANTIC_MAJOR.$ANTIC_MINOR.$ANTIC_PATCH";;
386      CYGWIN* | MINGW*)
387         ANTIC_LIBNAME="libantic.dll"
388	 ANTIC_LIB="libantic-$ANTIC_MAJOR.dll"
389	 EXTRA_SHARED_FLAGS="-static-libgcc -shared -Wl,--export-all-symbols -Wl,-soname,libantic-$ANTIC_MAJOR.dll.$ANTIC_MINOR.$ANTIC_PATCH";;
390      *)
391         ANTIC_LIBNAME="libantic.so"
392	 ANTIC_LIB="libantic.so.$ANTIC_MAJOR.$ANTIC_MINOR.$ANTIC_PATCH"
393	 EXTRA_SHARED_FLAGS="-Wl,-soname,libantic.so.$ANTIC_MAJOR"
394	 ANTIC_SOLIB=1;;
395   esac
396   EXTRA_SHARED_FLAGS="${EXTRA_SHARED_FLAGS} -Wl,-rpath,${GMP_LIB_DIR} -Wl,-rpath,${MPFR_LIB_DIR} -Wl,-rpath,${FLINT_LIB_DIR}"
397fi
398
399# sometimes LDCONFIG is not to be found in the path. Look at some common places.
400case "$OS" in
401    MINGW*|CYGWIN*|Darwin)
402	LDCONFIG="true";;
403    *)
404	if [ -z "$LDCONFIG" ]; then
405	    LDCONFIG="true"
406	    if [ "$ANTIC_SOLIB" = "1" ]; then
407		if command -v ldconfig > /dev/null; then
408		    LDCONFIG="ldconfig"
409		elif [ -x /sbin/ldconfig ]; then
410		    LDCONFIG="/sbin/ldconfig"
411		fi
412	    fi
413	fi;;
414esac
415
416#extension for executables
417
418if [ -z "$EXEEXT" ]; then
419   case "$OS" in
420      CYGWIN* | MINGW*)
421         EXEEXT=".exe";;
422      *)
423         EXEEXT="";;
424   esac
425fi
426
427#don't build both shared and static lib on MinGW and Cygwin
428
429case "$OS" in
430   CYGWIN* | MINGW*)
431      if [ "$STATIC" = "1" ] && [ "$SHARED" = "1" ]; then
432         echo "Building both static and shared versions of MPIR/GMP on $OS is currently"
433         echo "unsupported, and so is it for MPFR, FLINT and ANTIC."
434         echo "You should pass --disable-shared or --disable-static to configure"
435         echo "depending on the versions of MPIR/GMP, MPFR and FLINT you built."
436         exit 1
437      fi
438      ;;
439   *)
440      ;;
441esac
442
443#test for popcnt flag and set needed CFLAGS
444
445mkdir -p build
446rm -f build/test-popcnt > /dev/null 2>&1
447MSG="Testing __builtin_popcountl..."
448printf "%s" "$MSG"
449echo "int main(int argc, char ** argv) {
450#if defined(_WIN64)
451return __builtin_popcountll(argc) == 100;
452#else
453return __builtin_popcountl(argc) == 100;
454#endif
455}" > build/test-popcnt.c
456$CC build/test-popcnt.c -o ./build/test-popcnt > /dev/null 2>&1
457if [ $? -eq 0 ]; then
458   printf "%s\n" "yes"
459   CONFIG_POPCNT_INTRINSICS="#define POPCNT_INTRINSICS"
460
461   if [ "$MACHINE" = "x86_64" ]; then
462      MSG="Testing native popcount..."
463      printf "%s" "$MSG"
464      touch build/test-popcnt.c
465      rm build/test-popcnt
466      $CC -mpopcnt build/test-popcnt.c -o ./build/test-popcnt > /dev/null 2>&1
467      build/test-popcnt > /dev/null 2>&1
468      if [ $? -eq 0 ]; then
469         printf "%s\n" "yes"
470         POPCNT_FLAG="-mpopcnt"
471      else
472         printf "%s\n" "no"
473      fi
474      rm -f build/test-popcnt{,.c}
475   #in case -mpopcnt is not available, the test program will use an illegal
476   #instruction and that will print out something on stderr when the if
477   #construction is exited, whence the following "2> /dev/null"
478   fi 2> /dev/null
479else
480   rm -f build/test-popcnt.c
481   printf "%s\n" "no"
482fi
483
484#defaults for CFLAGS
485
486if [ -z "$CFLAGS" ]; then
487   if [ "$OS" = "MINGW64" ]; then
488      CFLAGS="-O2 -funroll-loops -g $POPCNT_FLAG $ABI_FLAG"
489   elif [ "$OS" = "CYGWIN64" ]; then
490      CFLAGS="-O2 -funroll-loops -g -D _WIN64 $POPCNT_FLAG $ABI_FLAG"
491   else
492      CFLAGS="-ansi -pedantic -Wall -O2 -funroll-loops -g $POPCNT_FLAG $ABI_FLAG"
493   fi
494fi
495
496#this is needed on PPC G5 and does not hurt on other OS Xes
497
498if [ "$KERNEL" = Darwin ]; then
499   CFLAGS="-fno-common $CFLAGS"
500fi
501
502#PIC flag
503
504if [ -z "$PIC_FLAG" ]; then
505   case "$OS" in
506      CYGWIN* | MINGW*)
507         ;;
508      *)
509         PIC_FLAG="-fPIC";;
510   esac
511fi
512
513#test support for thread-local storage
514
515CONFIG_TLS="#define HAVE_TLS 0"
516
517if [ "$TLS" = "1" ]; then
518   mkdir -p build
519   rm -f build/test-tls > /dev/null 2>&1
520   MSG="Testing __thread..."
521   printf "%s" "$MSG"
522   echo "__thread int x = 42; int main(int argc, char ** argv) { return x != 42; }" > build/test-tls.c
523   $CC build/test-tls.c -o ./build/test-tls > /dev/null 2>&1
524   if [ $? -eq 0 ]; then
525      build/test-tls > /dev/null 2>&1
526      if [ $? -eq 0 ]; then
527         printf "%s\n" "yes"
528         CONFIG_TLS="#define HAVE_TLS 1"
529      else
530         printf "%s\n" "no"
531      fi
532      rm -f build/test-tls{,.c}
533   else
534      rm -f build/test-tls.c
535      printf "%s\n" "no"
536   #build-tls can segfault on systems where tls is not available
537   fi 2> /dev/null
538fi
539
540#pthread configuration
541
542CONFIG_PTHREAD="#define HAVE_PTHREAD ${PTHREAD}"
543
544
545#pocess external modules
546
547EXTRA_INC_DIRS="${EXTRA_INC_DIRS} ${EXTENSIONS}"
548
549
550if [ -d "${EXTENSIONS}/examples" ]; then
551   cp ${EXTENSIONS}/examples/*.c ./examples/
552fi
553
554#include paths
555
556INCS="-I\$(CURDIR)"
557for INC_DIR in ${INC_DIRS} ${EXTRA_INC_DIRS}; do
558   INCS="${INCS} -I${INC_DIR}"
559done
560
561#library paths
562
563LLIBS="-L\$(CURDIR)"
564for LIB_DIR in ${LIB_DIRS} ${EXTRA_LIB_DIRS}; do
565   LLIBS="${LLIBS} -L${LIB_DIR}"
566done
567
568#linker params
569
570if [ "$PTHREAD" = "1" ]; then
571   lLIBS2="-lpthread ${lLIBS2}"
572fi
573
574
575for LIB in ${EXTRA_LIBS} ${LIBS}; do
576   lLIBS2="-l${LIB} ${lLIBS2}"
577done
578lLIBS="-lantic $lLIBS2"
579LIBS2="$LLIBS $lLIBS2"
580LIBS="$LLIBS $lLIBS"
581
582#paths for dynamic linker
583
584case "$OS" in
585   CYGWIN* | MINGW*)
586      DLPATH="PATH";;
587   Darwin)
588      DLPATH="DYLD_LIBRARY_PATH";;
589   sparc)
590      DLPATH="LD_LIBRARY_PATH32";;
591   sparc64)
592      DLPATH="LD_LIBRARY_PATH64";;
593   *)
594      DLPATH="LD_LIBRARY_PATH";;
595esac
596
597DLPATH_ADD="\$(CURDIR)"
598for LIB_DIR in ${LIB_DIRS} ${EXTRA_LIB_DIRS}; do
599   DLPATH_ADD="${DLPATH_ADD}:${LIB_DIR}"
600done
601
602#cxx
603
604if [ "$WANT_CXX" = "1" ]; then
605   EXTRA_BUILD="$EXTRA_BUILD anticxx"
606fi
607
608#write out Makefile
609
610echo "# This file is autogenerated by ./configure -- do not edit!" > Makefile
611echo "" >> Makefile
612echo "SHELL=/bin/sh" >> Makefile
613echo "" >> Makefile
614echo "ANTIC_STATIC=$STATIC" >> Makefile
615echo "ANTIC_SHARED=$SHARED" >> Makefile
616echo "ANTIC_LIB=$ANTIC_LIB" >> Makefile
617echo "ANTIC_LIBNAME=$ANTIC_LIBNAME" >> Makefile
618echo "ANTIC_MAJOR=$ANTIC_MAJOR" >> Makefile
619echo "ANTIC_SOLIB=$ANTIC_SOLIB" >> Makefile
620echo "EXEEXT=$EXEEXT" >> Makefile
621echo "PREFIX=$PREFIX" >> Makefile
622echo "" >> Makefile
623echo "WANT_NTL=$WANT_NTL" >> Makefile
624echo "" >> Makefile
625echo "INCS=$INCS" >> Makefile
626echo "LIBS=$LIBS" >> Makefile
627echo "LIBS2=$LIBS2" >> Makefile
628echo "" >> Makefile
629echo "CC=$CC" >> Makefile
630echo "CXX=$CXX" >> Makefile
631echo "AR=$AR" >> Makefile
632echo "LDCONFIG=$LDCONFIG" >> Makefile
633echo "" >> Makefile
634echo "CFLAGS=$CFLAGS" >> Makefile
635echo "ABI_FLAG=$ABI_FLAG" >> Makefile
636echo "PIC_FLAG=$PIC_FLAG" >> Makefile
637echo "EXTRA_SHARED_FLAGS=$EXTRA_SHARED_FLAGS" >> Makefile
638echo "" >> Makefile
639echo "DLPATH=$DLPATH" >> Makefile
640echo "DLPATH_ADD=$DLPATH_ADD" >> Makefile
641echo "EXTENSIONS=$EXTENSIONS" >> Makefile
642echo "EXTRA_BUILD_DIRS=$EXTRA_BUILD" >> Makefile
643echo "" >> Makefile
644
645cat Makefile.in >> Makefile
646
647echo "ANTIC was successfully configured."
648