1dnl Process this file with autoconf to produce a configure script.
2
3dnl WARNING! C code starting with # (preprocessor directives) must not
4dnl be indented!
5
6AC_COPYRIGHT([
7Copyright 1999-2023 Free Software Foundation, Inc.
8Contributed by the AriC and Caramba projects, INRIA.
9
10This file is part of the GNU MPFR Library.
11
12The GNU MPFR Library is free software; you can redistribute it and/or modify
13it under the terms of the GNU Lesser General Public License as published
14by the Free Software Foundation; either version 3 of the License, or (at
15your option) any later version.
16
17The GNU MPFR Library is distributed in the hope that it will be useful, but
18WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
20License for more details.
21
22You should have received a copy of the GNU Lesser General Public License
23along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
24https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2551 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
26])
27
28dnl Add check-news when it checks for more than 15 lines
29AC_INIT([MPFR],[4.2.1])
30
31dnl AC_CANONICAL_HOST is needed by this configure.ac file.
32dnl AC_CANONICAL_TARGET is not explicitly needed, but may be required by
33dnl some other macros (e.g. AX_PTHREAD 29, committed on 2021-02-19) and
34dnl must be called quite early in order to avoid the following warning:
35dnl   warning: AC_ARG_PROGRAM was called before AC_CANONICAL_TARGET
36dnl (we don't use AC_ARG_PROGRAM explicitly, but again, it is invoked by
37dnl another macro, apparently AM_INIT_AUTOMAKE). This has eventually been
38dnl fixed in AX_PTHREAD 31.
39dnl Due to this bug in AX_PTHREAD 29, AC_CANONICAL_HOST was moved here and
40dnl AC_CANONICAL_TARGET was added in r14481. However, it is unlikely that
41dnl we need AC_CANONICAL_TARGET in the future; see the comments at
42dnl   https://github.com/autoconf-archive/autoconf-archive/commit/2567e0ce0f3a11b535c6b527386197fb49ff172b
43dnl (introducing AX_PTHREAD 29). Thus it is probably better to avoid it,
44dnl as it could cause confusion. But let's keep AC_CANONICAL_HOST here,
45dnl since this may be a better place than later.
46AC_CANONICAL_HOST
47
48dnl Older Automake versions than 1.13 may still be supported, but no longer
49dnl tested, and many things have changed in 1.13. Moreover the INSTALL file
50dnl and MPFR manual assume that MPFR has been built using Automake 1.13+
51dnl (due to parallel tests, introduced by default in Automake 1.13).
52dnl The subdir-objects option is needed due to configuration related to
53dnl mini-gmp, which has sources in an external directory.
54AM_INIT_AUTOMAKE([1.13 no-define dist-bzip2 dist-xz dist-zip subdir-objects])
55AM_MAINTAINER_MODE(enable)
56
57AC_CONFIG_MACRO_DIR([m4])
58
59dnl Some AC_RUN_IFELSE programs need to be able to return several values
60dnl (e.g., in a format detection, one for each possible format). But the
61dnl Autoconf manual says: "This exit status might be that of a failed
62dnl compilation, or it might be that of a failed program execution."
63dnl Unfortunately, we cannot know whether a non-zero exit status comes
64dnl from a failed compilation, so that the detection may be incorrect.
65dnl Since failures generally occur with a small exit status, the value 77
66dnl is reserved for skipped tests by Autoconf, and values larger than 125
67dnl have special meanings in POSIX[*], good candidates for success are 0
68dnl and values from 80 to 125.
69dnl https://tldp.org/LDP/abs/html/exitcodes.html suggests the range 64-113
70dnl but note that /usr/include/sysexits.h now allocates previously unused
71dnl exit codes from 64 - 78 (for various kinds of errors).
72dnl
73dnl Alternatively, the test program could output the result to a file and
74dnl return with the 0 exit status if it could do that successfully.
75dnl
76dnl [*] 2.8.2 Exit Status for Commands
77dnl https://pubs.opengroup.org/onlinepubs/9699919799//utilities/V3_chap02.html#tag_18_08_02
78
79test_CFLAGS=${CFLAGS+set}
80
81dnl Check if user request its CC and CFLAGS
82if test -n "$CFLAGS" || test -n "$CC" ; then
83 user_redefine_cc=yes
84fi
85
86dnl Basic Autoconf macros. At this point, they must not make Autoconf
87dnl choose a compiler because of the CC and CFLAGS setup from gmp.h!
88
89AC_PROG_EGREP
90AC_PROG_SED
91
92dnl To use a separate config header.
93dnl There is still some problem with GMP's HAVE_CONFIG
94dnl AC_CONFIG_HEADERS([mpfrconf.h:mpfrconf.in])
95
96unset gmp_lib_path GMP_CFLAGS GMP_CC
97
98unset dev_version
99if $EGREP -q -e '-dev$' $srcdir/VERSION; then
100  dev_version=1
101fi
102
103dnl ********************************************************************
104dnl Extra arguments to configure (AC_ARG_WITH and AC_ARG_ENABLE)
105
106AC_ARG_WITH(gmp_include,
107   [  --with-gmp-include=DIR  GMP include directory ],
108   MPFR_PARSE_DIRECTORY(["$withval"],[withval])
109   CPPFLAGS="$CPPFLAGS -I$withval")
110
111AC_ARG_WITH(gmp_lib,
112   [  --with-gmp-lib=DIR      GMP lib directory ], [
113   MPFR_PARSE_DIRECTORY(["$withval"],[withval])
114   LDFLAGS="$LDFLAGS -L$withval"
115   gmp_lib_path="$withval"
116  ])
117
118AC_ARG_WITH(gmp,
119   [  --with-gmp=DIR          GMP install directory ], [
120   MPFR_PARSE_DIRECTORY(["$withval"],[withval])
121   if test -z "$with_gmp_lib" && test -z "$with_gmp_include" ; then
122      CPPFLAGS="$CPPFLAGS -I$withval/include"
123      LDFLAGS="$LDFLAGS -L$withval/lib"
124      gmp_lib_path="$withval/lib"
125   else
126      AC_MSG_FAILURE([Do not use --with-gmp and --with-gmp-include/--with-gmp-lib options simultaneously.])
127   fi
128  ])
129
130AC_ARG_WITH(gmp_build,
131   [  --with-gmp-build=DIR    GMP build directory (please read INSTALL file)],
132   [
133   MPFR_PARSE_DIRECTORY(["$withval"],[withval])
134   if test -z "$gmp_lib_path" && test -z "$with_gmp_include" ; then
135      CPPFLAGS="$CPPFLAGS -I$withval -I$withval/tune"
136      LDFLAGS="$LDFLAGS -L$withval -L$withval/.libs -L$withval/tune"
137      gmp_lib_path="$withval$PATH_SEPARATOR$withval/.libs$PATH_SEPARATOR$withval/tune"
138      if test -r $withval/Makefile ; then
139         GMP_CFLAGS=`$SED -n 's/^CFLAGS = //p' $withval/Makefile`
140         GMP_CC=`$SED -n 's/^CC = //p' $withval/Makefile`
141         GMP_SOURCE=`$SED -n 's/^srcdir = *//p' $withval/Makefile`
142         case "$GMP_SOURCE" in
143           .)  GMP_SOURCE="" ;;
144           /*) ;;
145           ?*) GMP_SOURCE="$withval/$GMP_SOURCE" ;;
146         esac
147         if test -d "$GMP_SOURCE" ; then
148            CPPFLAGS="$CPPFLAGS -I$GMP_SOURCE -I$GMP_SOURCE/tune"
149         fi
150      fi
151      use_gmp_build=yes
152   else
153      AC_MSG_FAILURE([Do not use --with-gmp-build and other --with-gmp options simultaneously.])
154   fi
155   ])
156
157AC_ARG_WITH(mini_gmp,
158   [  --with-mini-gmp=DIR     use mini-gmp (sources in DIR) instead of GMP
159                          (experimental, please read doc/mini-gmp file)],
160   [
161   MPFR_PARSE_DIRECTORY(["$withval"],[withval])
162   if test -z "$gmp_lib_path" && test -z "$with_gmp_include" && \
163      test -z "$use_gmp_build"; then
164     if test -f "$withval/mini-gmp.c" && test -f "$withval/mini-gmp.h"; then
165       AC_DEFINE([MPFR_USE_MINI_GMP],1,[Use mini-gmp])
166       mini_gmp_path="$withval"
167       AC_SUBST(mini_gmp_path)
168     else
169       AC_MSG_FAILURE([mini-gmp.{c,h} not found in $withval])
170     fi
171   else
172     AC_MSG_FAILURE([Do not use --with-mini-gmp and other --with-gmp options simultaneously.])
173   fi
174  ])
175
176AC_ARG_WITH(mulhigh_size,
177   [  --with-mulhigh-size=NUM internal threshold table for mulhigh],
178   AC_DEFINE_UNQUOTED([MPFR_MULHIGH_SIZE],$withval, [Mulhigh size]))
179
180AC_ARG_ENABLE(gmp-internals,
181   [  --enable-gmp-internals  enable use of GMP undocumented functions [[default=no]]],
182   [ case $enableval in
183      yes) AC_DEFINE([WANT_GMP_INTERNALS],1,[Want GMP internals]) ;;
184      no)  ;;
185      *) AC_MSG_ERROR([bad value for --enable-gmp-internals: yes or no]) ;;
186     esac])
187
188AC_ARG_ENABLE(assert,
189   [  --enable-assert         enable ASSERT checking [[default=no]]],
190   [ case $enableval in
191      yes) AC_DEFINE([MPFR_WANT_ASSERT],1,[Want all simple assertions]) ;;
192      none) AC_DEFINE([MPFR_WANT_ASSERT],-1,[Do no want any assertion]) ;;
193      no)  ;;
194      full) AC_DEFINE([MPFR_WANT_ASSERT],2,[Want full assertions]) ;;
195      *) AC_MSG_ERROR([bad value for --enable-assert: yes, no, none or full]) ;;
196     esac])
197
198AC_ARG_ENABLE(logging,
199   [  --enable-logging        enable MPFR logging (needs nested functions
200                          and the 'cleanup' attribute) [[default=no]]],
201   [ case $enableval in
202      yes) AC_DEFINE([MPFR_USE_LOGGING],1,[Enable MPFR logging support]) ;;
203      no)  ;;
204      *)   AC_MSG_ERROR([bad value for --enable-logging: yes or no]) ;;
205     esac])
206
207AC_ARG_ENABLE(thread-safe,
208   [  --disable-thread-safe   explicitly disable TLS support
209  --enable-thread-safe    build MPFR as thread safe, i.e. with TLS support
210                          (the system must support it) [[default=autodetect]]],
211   [ case $enableval in
212      yes) ;;
213      no)  ;;
214      *)   AC_MSG_ERROR([bad value for --enable-thread-safe: yes or no]) ;;
215     esac])
216
217AC_ARG_ENABLE(shared-cache,
218   [  --enable-shared-cache   enable use of caches shared by all threads,
219                          for all MPFR constants.  It usually makes MPFR
220                          dependent on PTHREAD [[default=no]]],
221   [ case $enableval in
222      yes)
223         AC_DEFINE([MPFR_WANT_SHARED_CACHE],1,[Want shared cache]) ;;
224      no)  ;;
225      *) AC_MSG_ERROR([bad value for --enable-shared-cache: yes or no]) ;;
226     esac])
227
228AC_ARG_ENABLE(warnings,
229   [  --enable-warnings       allow MPFR to output warnings to stderr [[default=no]]],
230   [ case $enableval in
231      yes) AC_DEFINE([MPFR_USE_WARNINGS],1,[Allow MPFR to output warnings to stderr]) ;;
232      no)  ;;
233      *)   AC_MSG_ERROR([bad value for --enable-warnings: yes or no]) ;;
234     esac])
235
236dnl For timeout support (in the testsuite), the case "yes" and absent
237dnl --enable-tests-timeout option will be handled in a consistent way
238dnl in MPFR_CONFIGS (see acinclude.m4).
239AC_ARG_ENABLE(tests-timeout,
240   [  --enable-tests-timeout=NUM
241                          [[for developers]] enable timeout for test programs
242                          (NUM seconds, <= 9999) [[default=no]]; if this is
243                          enabled, the environment variable $MPFR_TESTS_TIMEOUT
244                          overrides NUM (0: no timeout)],
245   [ case $enableval in
246      no)   ;;
247      yes)  ;;
248      [[0-9]]|[[0-9]][[0-9]]|[[0-9]][[0-9]][[0-9]]|[[0-9]][[0-9]][[0-9]][[0-9]])
249       AC_DEFINE_UNQUOTED([MPFR_TESTS_TIMEOUT], $enableval, [timeout limit]) ;;
250      *)    AC_MSG_ERROR([bad value for --enable-tests-timeout]) ;;
251     esac])
252
253AC_ARG_ENABLE(tune-for-coverage,
254   [  --enable-tune-for-coverage
255                          [[for developers]] tune MPFR for coverage tests],
256   [ case $enableval in
257      no)   ;;
258      yes)  AC_DEFINE([MPFR_TUNE_COVERAGE], 1, [tune for coverage]) ;;
259      *)    AC_MSG_ERROR([bad value for --enable-tune-for-coverage]) ;;
260     esac])
261
262dnl Support for _Decimal64 and _Decimal128 (ISO/IEC TS 18661).
263dnl See acinclude.m4 for more information and tests.
264dnl FIXME: differentiate the support of _Decimal64 and _Decimal128, e.g.
265dnl   --enable-decimal64  for _Decimal64
266dnl   --enable-decimal128 for _Decimal128
267dnl   --enable-decimal-float would explicitly enable both (or fail).
268dnl   --disable-decimal-float would explicitly disable both.
269AC_ARG_ENABLE(decimal-float,
270   [  --disable-decimal-float explicitly disable decimal floats support
271  --enable-decimal-float  build conversion functions from/to decimal floats
272                          (see INSTALL file for details) [[default=auto]]],
273   [ case $enableval in
274      yes|no|auto|bid|dpd|generic) ;;
275      *) AC_MSG_ERROR([bad value for --enable-decimal-float]) ;;
276     esac])
277
278dnl Warning! Not to be confused with _Decimal128. Thus it is better
279dnl to say binary128 in the description. It can correspond to either
280dnl _Float128 (ISO/IEC TS 18661) or __float128 (old type name).
281AC_ARG_ENABLE(float128,
282   [  --disable-float128      explicitly disable binary128 support
283  --enable-float128       build conversion functions from/to binary128
284                          (_Float128 or __float128) [[default=autodetect]]],
285   [ case $enableval in
286      yes) ;;
287      no)  ;;
288      *)   AC_MSG_ERROR([bad value for --enable-float128: yes or no]) ;;
289     esac])
290
291AC_ARG_ENABLE(debug-prediction,
292   [  --enable-debug-prediction
293                          [[for developers]] enable debug of branch prediction
294                          (for x86 and x86-64 with GCC, static libs)],
295   [ case $enableval in
296      yes)  if test "$enable_shared" != no; then
297              AC_MSG_ERROR([--enable-debug-prediction can only work in static mode (--disable-shared)])
298            fi
299            AC_DEFINE([MPFR_DEBUG_PREDICTION],1,
300	       [Enable debug of branch prediction]) ;;
301      no)   ;;
302      *)    AC_MSG_ERROR([bad value for --enable-debug-prediction: yes or no]) ;;
303     esac])
304
305AC_ARG_ENABLE(lto,
306   [  --enable-lto            build MPFR with link-time-optimization
307                          (experimental) [[default: no]]],
308   [ case $enableval in
309      yes)  if test "$enable_shared" != "no"; then
310              AC_MSG_ERROR([--enable-lto can only work in static mode (--disable-shared)])
311            fi
312            enable_lto=yes
313            ;;
314      no)   ;;
315      *)    AC_MSG_ERROR([bad value for --enable-lto: yes or no]) ;;
316     esac])
317
318AC_ARG_ENABLE(formally-proven-code,
319   [  --enable-formally-proven-code
320                          use formally proven code when available
321                          (needs a C99 compiler) [[default=no]]],
322   [ case $enableval in
323      yes) AC_DEFINE([MPFR_WANT_PROVEN_CODE],1,[Want formally proven code]) ;;
324      no)  ;;
325      *) AC_MSG_ERROR([bad value for --enable-formally-proven-code: yes or no]) ;;
326     esac])
327
328dnl Makefile.am files can use "if MINI_GMP" / ... / "endif".
329AM_CONDITIONAL([MINI_GMP], [test -n "$mini_gmp_path"])
330
331dnl First, detect incompatibilities between the above configure options.
332AC_MSG_CHECKING([whether configure options are compatible])
333if test "$enable_logging" = yes; then
334  if test "$enable_thread_safe" = yes; then
335    AC_MSG_RESULT([no])
336    AC_MSG_ERROR([enable either logging or thread-safe, not both])
337  fi
338dnl The following test is done only to output a specific error message,
339dnl as there would otherwise be an error due to enable_thread_safe=no.
340  if test "$enable_shared_cache" = yes; then
341    AC_MSG_RESULT([no])
342    AC_MSG_ERROR([shared cache does not work with logging support])
343  fi
344  enable_thread_safe=no
345fi
346if test "$enable_shared_cache" = yes; then
347  if test "$enable_thread_safe" = no; then
348    AC_MSG_RESULT([no])
349    AC_MSG_ERROR([shared cache needs thread-safe support])
350  fi
351  enable_thread_safe=yes
352fi
353AC_MSG_RESULT([yes])
354
355
356dnl
357dnl Setup CC and CFLAGS
358dnl
359
360dnl ********************************************************************
361dnl Check for CC and CFLAGS in gmp.h
362
363dnl Warning! The following tests must be done before Autoconf selects
364dnl a compiler. This means that some macros such as AC_PROG_CC and
365dnl AM_PROG_AR must be put after the following code.
366
367dnl We do not do this check if mini-gmp is used.
368
369if test -z "$user_redefine_cc" && \
370   test "$cross_compiling" != yes && \
371   test "${with_mini_gmp+set}" != set ; then
372
373dnl We need to guess the C preprocessor instead of using AC_PROG_CPP,
374dnl since AC_PROG_CPP implies AC_PROG_CC, which chooses a compiler
375dnl (before we have the chance to get it from gmp.h) and does some
376dnl checking related to this compiler (such as dependency tracking
377dnl options); if the compiler changes due to __GMP_CC in gmp.h, one
378dnl would have incorrect settings.
379if test -z "$GMP_CC$GMP_CFLAGS" ; then
380   AC_MSG_CHECKING(for CC and CFLAGS in gmp.h)
381   GMP_CC=__GMP_CC
382   GMP_CFLAGS=__GMP_CFLAGS
383   # /lib/cpp under Solaris doesn't support some environment variables
384   # used by GCC, such as C_INCLUDE_PATH. If the user has set up such
385   # environment variables, he probably wants to use them. So, let us
386   # prefer cpp and gcc to /lib/cpp. However, note that this won't
387   # work if GCC has been installed with versioned names only (e.g.
388   # with cpp-5 and gcc-5).
389   for cpp in "cpp -P" "cpp" "gcc -P -E" "gcc -E" "/lib/cpp -P" "/lib/cpp" "cc -P -E" "cc -E" "c99 -P -E" "c99 -E" "clang -E" "cl -E" "icl -E"
390   do
391     # Get CC
392     echo "#include \"gmp.h\"" > conftest.c
393     echo "MPFR_OPTION __GMP_CC" >> conftest.c
394     $cpp $CPPFLAGS conftest.c 2> /dev/null > conftest.txt
395     test $? -ne 0 && continue
396     GMP_CC=`$EGREP MPFR_OPTION conftest.txt | $SED -e 's/MPFR_OPTION //g;s/ *" *//g'`
397     # Get CFLAGS
398     echo "#include \"gmp.h\"" >  conftest.c
399     echo "MPFR_OPTION __GMP_CFLAGS" >> conftest.c
400     $cpp $CPPFLAGS conftest.c 2> /dev/null > conftest.txt
401     test $? -ne 0 && continue
402     [GMP_CFLAGS=`$EGREP MPFR_OPTION conftest.txt | $SED -e 's/[ "]-pedantic[ "]/ /g;s/MPFR_OPTION //g;s/ *" *//g'`]
403     break
404   done
405   rm -f conftest*
406   if test "x$GMP_CC" = "x__GMP_CC" || test "x$GMP_CFLAGS" = "x__GMP_CFLAGS" ; then
407      AC_MSG_RESULT(failed)
408      GMP_CFLAGS=
409      GMP_CC=
410   else
411      AC_MSG_RESULT([CC=$GMP_CC CFLAGS=$GMP_CFLAGS])
412   fi
413fi
414
415dnl But these variables may be invalid, so we must check them first.
416dnl Note: we do not use AC_RUN_IFELSE, as it implies AC_PROG_CC.
417if test -n "$GMP_CC$GMP_CFLAGS" ; then
418   AC_MSG_CHECKING(for CC=$GMP_CC and CFLAGS=$GMP_CFLAGS)
419   echo "int main (void) { return 0; }" > conftest.c
420   if $GMP_CC $GMP_CFLAGS -o conftest conftest.c 2> /dev/null ; then
421     AC_MSG_RESULT(yes)
422     CFLAGS=$GMP_CFLAGS
423     CC=$GMP_CC
424   else
425     AC_MSG_RESULT(no)
426   fi
427   rm -f conftest*
428fi
429
430fi
431
432dnl ********************************************************************
433
434AC_PROG_CC
435AC_PROG_CPP
436AC_LANG(C)
437
438dnl AM_PROG_AR is needed so that ar-lib (wrapper for Microsoft lib.exe)
439dnl gets installed by "automake -i" (if absent, "automake --warnings=all"
440dnl gives a warning).
441dnl However, if GMP chooses a non-default mode (a.k.a. ABI), this test may
442dnl fail. The reason is that GMP provides GMP_CC and GMP_CFLAGS to get the
443dnl right commands to compile (and link?) in the chosen non-default mode,
444dnl but does not provide a macro for the archiver (if such a special command
445dnl is needed). In case of failure, the user needs to either force GMP to
446dnl use the default mode or provide an AR variable for MPFR's configure.
447dnl Example: on power-aix, "gcc -c -O2 -maix64 -mpowerpc64 -mtune=power7"
448dnl is used, but "ar cru" fails. Solution: use AR="ar -X64" NM="nm -B -X64"
449dnl as said in the INSTALL file ("Notes on AIX/PowerPC").
450AM_PROG_AR
451
452dnl This must done before MPFR_CONFIGS.
453LT_INIT(win32-dll)
454
455dnl Warning: This test is *only* for CFLAGS settings.
456dnl If the compiler is ICC, add some specific flags, except on MS-Windows.
457dnl Don't add warnings flags (Otherwise you'll get more than 20000 warnings).
458dnl Add -long_double flags? Don't use -pc64 !
459dnl Notes (VL):
460dnl   * With icc 10.1 20080212 on itanium, the __ICC macro is not defined,
461dnl     even when the -icc option is used (contrary to what is documented
462dnl     on the icc man page).
463dnl   * When ICC is correctly detected (__ICC macro defined), unsetting
464dnl     the GCC variable confuses libtool. See:
465dnl       https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=485421
466dnl   * If need be, the gcc predefined macros __GNUC_* can be disabled
467dnl     thanks to the -no-gcc option.
468dnl   * Now use -mieee-fp instead of -mp (ICC 13 says: option '-mp' is
469dnl     deprecated and will be removed in a future release.). According
470dnl     to "icc -help", both options are equivalent; it also suggests
471dnl     to use -fp-model <arg> instead of -mp, but what is <arg>? Anyway
472dnl     -mieee-fp solves the tset_d failure due to a negative zero. This
473dnl     option has been introduced in ICC 9.0 for Linux (2005-06):
474dnl     https://climserv.ipsl.polytechnique.fr/documentation/intel-icc/ReleaseNotes.htm
475AC_MSG_CHECKING(for non-MS-Windows ICC)
476AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
477#if (!defined(__ICC) && !defined(__INTEL_COMPILER)) || defined(_WIN32)
478# error "Not a non-MS-Windows ICC"
479error
480#endif
481]], [[]])],[
482 AC_MSG_RESULT(yes)
483 CFLAGS="-fp_port -mieee-fp -wd1572 -wd265 -wd186 -wd239 $CFLAGS"
484],[AC_MSG_RESULT(no)])
485
486dnl If CFLAGS has not been set explicitly and the compiler is GCC, then
487dnl use some specific flags. But don't touch user other flags.
488dnl Note: This is done even when CFLAGS has been set from GMP's CFLAGS
489dnl (__GMP_CFLAGS macro in gmp.h) above. The consequence is that this
490dnl might yield a compilation failure if the -Werror option appears in
491dnl __GMP_CFLAGS. But in this case, since -Werror is not used by default,
492dnl one may expect that the user would also set CFLAGS for MPFR.
493if test "$test_CFLAGS" != set && test -n "$GCC"; then
494  CFLAGS="-Wpointer-arith $CFLAGS"
495  AC_MSG_CHECKING(whether the selected language is C++)
496  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
497#if defined (__cplusplus)
498# error "C++"
499error
500#endif
501  ]], [[]])],[
502    AC_MSG_RESULT(no)
503    dnl The option -Wc++-compat is available in GCC 4.1.0 and newer.
504    [
505    case `$CC --version | $SED -e 's/^[^ ]* //' -e 's/([^)]*) //' -e 1q` in
506      2.*|3.*|4.0*) ;;
507      *) CFLAGS="-Wc++-compat $CFLAGS" ;;
508    esac
509    CFLAGS="-Wmissing-prototypes $CFLAGS"
510    ]
511  ],[
512    AC_MSG_RESULT(yes)
513    CFLAGS="-Wmissing-declarations -Wno-sign-compare $CFLAGS"
514  ])
515  CFLAGS="-Wall $CFLAGS"
516  AC_MSG_NOTICE([using CFLAGS="$CFLAGS"])
517fi
518
519AM_PROG_CC_C_O
520
521case $host in
522  *-apple-darwin*)
523dnl This allows to take the first GMP library in the library paths,
524dnl whether it is dynamic or static. This behavior is more sensible,
525dnl in particular because it is the only way to link with a version
526dnl only available in static form when another version is available
527dnl in dynamic, and also for consistency, because the compiler will
528dnl take the first gmp.h found in the include paths (so, we need to
529dnl take a library that corresponds to this header file). This is a
530dnl common problem with darwin.
531    MPFR_LD_SEARCH_PATHS_FIRST ;;
532esac
533
534AC_C_CONST
535AC_C_VOLATILE
536dnl Determine the endianness of integer. But since MPFR doesn't use
537dnl AC_CONFIG_HEADERS, configure cannot provide such information for
538dnl universal binaries containing variants with different endianness
539dnl (i.e. generic code must be used).
540AC_C_BIGENDIAN([AC_DEFINE(HAVE_BIG_ENDIAN)],[AC_DEFINE(HAVE_LITTLE_ENDIAN)],
541[true],[true])
542
543# (Based on GMP 5.1)
544# clock_gettime is in librt on *-*-osf5.1 and on glibc < 2.17, so add -lrt to
545# TUNE_LIBS if needed (e.g. if clock_gettime is not already in the C library).
546# On linux (tested on x86_32, 2.6.26), clock_getres reports ns accuracy,
547# while in a quick test on osf, clock_getres said only 1 millisecond.
548old_LIBS="$LIBS"
549AC_SEARCH_LIBS(clock_gettime, rt, [
550  AC_DEFINE([HAVE_CLOCK_GETTIME],1,[Define to 1 if you have the `clock_gettime' function])])
551TUNE_LIBS="$LIBS"
552LIBS="$old_LIBS"
553AC_SUBST(TUNE_LIBS)
554
555dnl Under Linux, make sure that the old dtags are used if LD_LIBRARY_PATH
556dnl is defined. The issue is that with the new dtags, LD_LIBRARY_PATH has
557dnl the precedence over the run path, so that if a compatible MPFR library
558dnl is installed in some directory from $LD_LIBRARY_PATH, then the tested
559dnl MPFR library will be this library instead of the MPFR library from the
560dnl build tree. Other OS with the same issue might be added later.
561dnl
562dnl References:
563dnl   https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=859732
564dnl   https://lists.gnu.org/archive/html/libtool/2017-05/msg00000.html
565dnl
566dnl We need to check whether --disable-new-dtags is supported as alternate
567dnl linkers may be used (e.g., with tcc: CC=tcc LD=tcc).
568dnl
569dnl [2023-08]
570dnl On Android/Termux, -Wl,--disable-new-dtags is supported by clang, but
571dnl the DT_RPATH directories are stripped away when running the program.
572dnl Moreover, config.guess identifies the host as GNU/Linux (*-*-linux-gnu)
573dnl instead of Android (linux*android*), so that we need to explicitly
574dnl exclude Termux.
575dnl
576case $host in
577  *-*-linux-gnu)
578    if test -n "$LD_LIBRARY_PATH" && test -z "$TERMUX_VERSION"; then
579      saved_LDFLAGS="$LDFLAGS"
580      LDFLAGS="$LDFLAGS -Wl,--disable-new-dtags"
581      AC_MSG_CHECKING(whether --disable-new-dtags is supported by the linker)
582      AC_LINK_IFELSE([AC_LANG_SOURCE([[
583int main (void) { return 0; }
584      ]])],
585      [AC_MSG_RESULT(yes (use it since LD_LIBRARY_PATH is set))],
586      [AC_MSG_RESULT(no)
587       LDFLAGS="$saved_LDFLAGS"
588      ])
589    fi
590    ;;
591esac
592
593dnl MPFR_CONFIGS uses LDFLAGS, thus must be invoked after LDFLAGS has
594dnl been determined completely.
595MPFR_CONFIGS
596
597dnl
598dnl For mpfr-longlong.h - TODO: should be replaced (see acinclude.m4).
599dnl
600
601GMP_C_ATTRIBUTE_MODE
602
603
604dnl
605dnl Setup related to GMP / mini-gmp
606dnl
607
608if test -z "$mini_gmp_path" ; then
609
610dnl Setup for GMP
611
612dnl Check GMP Header
613AC_MSG_CHECKING(for gmp.h)
614AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
615#include "gmp.h"
616]])],[AC_MSG_RESULT(yes)],[
617 AC_MSG_RESULT(no)
618 AC_MSG_ERROR([gmp.h can't be found, or is unusable.])
619])
620
621dnl Configs for Windows DLLs.
622dnl libtool requires "-no-undefined" for win32 dll
623dnl
624dnl "-Wl,output-def" is used to get a .def file for use by MS lib to make
625dnl a .lib import library, described in the manual.
626dnl
627dnl Incidentally, libtool does generate an import library libmpfr.dll.a,
628dnl but it's "ar" format and cannot be used by the MS linker.  There
629dnl doesn't seem to be any GNU tool for generating or converting to .lib.
630dnl
631dnl The DLL version (the number in libmpfr-*.dll.def below) must be the
632dnl version of the oldest supported interface (i.e. CURRENT - AGE in
633dnl src/Makefile.am).
634AC_SUBST(MPFR_LDFLAGS)
635AC_SUBST(LIBMPFR_LDFLAGS)
636case $host in
637  *-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2*)
638   AC_MSG_CHECKING(for DLL/static GMP)
639   if test "$enable_shared" = yes; then
640     MPFR_LDFLAGS="$MPFR_LDFLAGS -no-undefined"
641     LIBMPFR_LDFLAGS="$LIBMPFR_LDFLAGS -Wl,--output-def,.libs/libmpfr-6.dll.def"
642     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
643#include "gmp.h"
644#if !__GMP_LIBGMP_DLL
645# error "Dead man"
646error
647#endif
648     ]], [[]])],[AC_MSG_RESULT(DLL)],[
649  AC_MSG_RESULT(static)
650  AC_MSG_ERROR([libgmp isn't provided as a DLL: use --enable-static --disable-shared]) ])
651   else
652     AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
653#include "gmp.h"
654#if __GMP_LIBGMP_DLL
655# error "Dead man"
656error
657#endif
658     ]], [[]])],[AC_MSG_RESULT(static)],[
659  AC_MSG_RESULT(DLL)
660  AC_MSG_ERROR([libgmp is provided as a DLL: use --disable-static --enable-shared]) ])
661  fi
662  ;;
663esac
664
665dnl Check minimal GMP version
666dnl We only guarantee that with a *functional* and recent enough GMP version,
667dnl MPFR will compile; we do not guarantee that GMP will compile.
668dnl In particular fat builds are broken in GMP 4.3.2 and GMP 5.0.0
669dnl (at least on 64-bit Core 2 under Linux),
670dnl see <https://gmplib.org/list-archives/gmp-bugs/2011-August/002345.html>.
671AC_MSG_CHECKING(for recent GMP)
672AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
673#include "gmp.h"
674#if (__GNU_MP_VERSION*100+__GNU_MP_VERSION_MINOR*10 < 500)
675# error "GMP 5.0.0 or newer is required"
676error
677#endif
678  ]])],[AC_MSG_RESULT(yes)],[
679   AC_MSG_RESULT(no)
680   AC_MSG_ERROR([GMP 5.0.0 or newer is required])
681])
682
683dnl Check if gmp.h is usable at link time; this may detect errors such as
684dnl with GMP 4.1, which uses "extern __inline__" unconditionally with all
685dnl GCC versions, which breaks by default with GCC 5 (this problem with
686dnl GMP 4.1 is no longer possible as we now require GMP 5.0.0 or newer,
687dnl but the same kind of problem may occur in the future).
688dnl Note: No linking is done against the GMP library at this time, as we
689dnl do not use any GMP symbol. The goal of this test is to avoid obscure
690dnl errors with the following gmp.h tests.
691AC_MSG_CHECKING(usable gmp.h at link time)
692AC_LINK_IFELSE([AC_LANG_PROGRAM([[
693#include "gmp.h"
694]],[[]])],[AC_MSG_RESULT(yes)],[
695 AC_MSG_RESULT(no)
696 AC_MSG_ERROR([there is an incompatibility between gmp.h and the compiler.
697See 'config.log' for details.])
698])
699
700dnl Check if we can use internal header files of GMP (only --with-gmp-build)
701if test "$use_gmp_build" = yes ; then
702   AC_MSG_CHECKING(for gmp internal files)
703   AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
704   #include "gmp.h"
705   #include "gmp-impl.h"
706   #include "longlong.h"
707   ]])],[
708     AC_MSG_RESULT(yes)
709     AC_DEFINE([MPFR_HAVE_GMP_IMPL],1,[Use GMP Internal Files])
710   ],[
711     AC_MSG_ERROR([header files gmp-impl.h and longlong.h not found])
712   ])
713fi
714
715dnl Check for GMP_NUMB_BITS and sizeof(mp_limb_t) consistency.
716dnl Problems may occur if gmp.h was generated with some ABI
717dnl and is used with another ABI (or if nails are used).
718dnl This test doesn't need to link with libgmp (at least it shouldn't).
719AC_MSG_CHECKING(for GMP_NUMB_BITS and sizeof(mp_limb_t) consistency)
720AC_RUN_IFELSE([AC_LANG_PROGRAM([[
721#include <stdio.h>
722#include <limits.h>
723#include "gmp.h"
724]], [[
725  if (GMP_NUMB_BITS == sizeof(mp_limb_t) * CHAR_BIT)
726    return 0;
727  fprintf (stderr, "GMP_NUMB_BITS     = %ld\n", (long) GMP_NUMB_BITS);
728  fprintf (stderr, "sizeof(mp_limb_t) = %ld\n", (long) sizeof(mp_limb_t));
729  fprintf (stderr, "sizeof(mp_limb_t) * CHAR_BIT = %ld != GMP_NUMB_BITS\n",
730           (long) (sizeof(mp_limb_t) * CHAR_BIT));
731  return 1;
732]])], [AC_MSG_RESULT(yes)], [
733       AC_MSG_RESULT(no)
734       AC_MSG_ERROR([GMP_NUMB_BITS and sizeof(mp_limb_t) are not consistent.
735You probably need to change some of the GMP or MPFR compile options.
736See 'config.log' for details (search for GMP_NUMB_BITS).])],
737       [AC_MSG_RESULT([cannot test])])
738
739MPFR_CHECK_MP_LIMB_T_VS_LONG
740MPFR_CHECK_MP_LIMB_T_VS_INTMAX
741
742dnl Now we will need the GMP library (not just gmp.h).
743dnl Do not use AC_CHECK_LIB as it is based on a deprecated ISO C feature.
744LIBS="-lgmp $LIBS"
745
746dnl Check if we can link with GMP
747AC_MSG_CHECKING(if we can link with GMP)
748AC_LINK_IFELSE([AC_LANG_PROGRAM([[
749#include "gmp.h"
750mpz_t m;
751]],[[
752mpz_init (m);
753]])],[AC_MSG_RESULT(yes)],[
754 AC_MSG_RESULT(no)
755 AC_MSG_ERROR([libgmp not found or uses a different ABI (including static vs shared).
756Please read the INSTALL file -- see "In case of problem".])
757])
758
759dnl Check for corresponding 'gmp.h' and libgmp
760AC_MSG_CHECKING(if gmp.h version and libgmp version are the same)
761dnl We do not set LD_LIBRARY_PATH, as it is not possible to set it just
762dnl before the test program is run, and we do not want to affect other
763dnl programs (such as the compiler), because the behavior could be
764dnl incorrect and even have security implications.
765dnl WARNING! LD_RUN_PATH is not taken into account by the GNU gold ld,
766dnl e.g. from binutils-gold 2.22-5 under Debian; see
767dnl   https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=660813
768dnl   https://sourceware.org/bugzilla/show_bug.cgi?id=13764
769saved_LD_RUN_PATH="$LD_RUN_PATH"
770LD_RUN_PATH="${LD_RUN_PATH:+$LD_RUN_PATH$PATH_SEPARATOR}$gmp_lib_path"
771export LD_RUN_PATH
772AC_RUN_IFELSE([AC_LANG_PROGRAM([[
773#include <stdio.h>
774#include <string.h>
775#include "gmp.h"
776  ]], [[
777  char buffer[100];
778  sprintf (buffer, "%d.%d.%d", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR,
779           __GNU_MP_VERSION_PATCHLEVEL);
780  printf ("(%s/%s) ", buffer, gmp_version);
781  fflush (stdout);
782  /* Also put information in config.log (stderr) */
783  fprintf (stderr, "gmp.h: %s / libgmp: %s\n", buffer, gmp_version);
784  if (strcmp (buffer, gmp_version) == 0)
785    return 0;
786  if (__GNU_MP_VERSION_PATCHLEVEL != 0)
787    return 1;
788  sprintf (buffer, "%d.%d", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR);
789  return (strcmp (buffer, gmp_version) != 0) ? 1 : 0;
790  ]])],
791   [AC_MSG_RESULT(yes)
792    MPFR_CHECK_GMP
793    MPFR_CHECK_DBL2INT_BUG
794    MPFR_CHECK_PRINTF_SPEC
795    MPFR_CHECK_PRINTF_GROUPFLAG],
796   [AC_MSG_RESULT(no)
797    AC_MSG_WARN([==========================================================])
798    AC_MSG_WARN(['gmp.h' and 'libgmp' seem to have different versions or])
799    AC_MSG_WARN([we cannot run a program linked with GMP (if you cannot])
800    AC_MSG_WARN([see the version numbers above). A cause may be different])
801    AC_MSG_WARN([GMP versions with different ABI's or the use of --with-gmp])
802    AC_MSG_WARN([or --with-gmp-include with a system include directory])
803    AC_MSG_WARN([(such as /usr/include or /usr/local/include). Another])
804    AC_MSG_WARN([cause may be that LD_RUN_PATH is not honored (problem])
805    AC_MSG_WARN([seen under OpenBSD 6.6 and with the GNU gold linker).])
806    AC_MSG_WARN([However, since we can't use 'libtool' inside the configure,])
807    AC_MSG_WARN([we can't be sure. See 'config.log' for details.])
808    AC_MSG_WARN([CC="$CC"])
809    AC_MSG_WARN([CFLAGS="$CFLAGS"])
810    AC_MSG_WARN([CPPFLAGS="$CPPFLAGS"])
811    AC_MSG_WARN([LDFLAGS="$LDFLAGS"])
812    AC_MSG_WARN([LIBS="$LIBS"])
813    AC_MSG_WARN([Temporary LD_RUN_PATH was "$LD_RUN_PATH".])
814    AC_MSG_WARN([==========================================================])
815    ],AC_MSG_RESULT([cannot test])
816  )
817LD_RUN_PATH="$saved_LD_RUN_PATH"
818
819dnl __gmpn_sbpi1_divappr_q is an internal GMP symbol; thus its behavior
820dnl may change or this symbol may be removed in the future (without being
821dnl handled by the library versioning system, which is even worse, as this
822dnl can mean undetected incompatibilities in case of GMP library upgrade,
823dnl without rebuilding MPFR). So, this symbol must not be used, unless
824dnl WANT_GMP_INTERNALS is defined. Only the GMP public API should be used
825dnl by default, in particular by binary distributions. Moreover the check
826dnl below may yield an incorrect result as libtool isn't used in configure
827dnl (see above).
828AC_CHECK_FUNCS([__gmpn_sbpi1_divappr_q])
829dnl same for other GMP internal functions
830AC_CHECK_FUNCS([__gmpn_invert_limb])
831dnl mpn_rsblsh1_n(rp, up, vp, n) does {rp, n} <- 2 * {vp, n} - {up, n}
832AC_CHECK_FUNCS([__gmpn_rsblsh1_n])
833
834else
835
836dnl Setup for mini-gmp
837
838dnl Let us make the configure script clean up the mini-gmp.* files
839dnl and add the symbolic links to mini-gmp.{c,h} instead of relying
840dnl on timestamp-based make rules, which may not work when testing
841dnl several mini-gmp versions (whose source can go back in time).
842mpfr_build_src="${ac_top_build_prefix}src"
843mkdir -p "$mpfr_build_src"
844rm -f -- "$mpfr_build_src"/mini-gmp.*
845for i in c h
846do
847  ln -s -- "$mini_gmp_path/mini-gmp.$i" "$mpfr_build_src/mini-gmp.$i"
848done
849
850dnl First check whether mini-gmp defines GMP_NUMB_BITS. If it doesn't,
851dnl then guess the value from the size of mp_limb_t.
852AC_MSG_CHECKING(for GMP_NUMB_BITS)
853how="from mini-gmp.h"
854saved_CPPFLAGS="$CPPFLAGS"
855CPPFLAGS="$CPPFLAGS -I$mpfr_build_src"
856AC_COMPUTE_INT(mini_gmp_numb_bits, [(GMP_NUMB_BITS)],
857   [#include <mini-gmp.h>],
858   [how="not in mini-gmp.h; guessed"
859    AC_COMPUTE_INT(mini_gmp_numb_bits, [(sizeof(mp_limb_t) * CHAR_BIT)],
860       [#include <limits.h>
861        #include <mini-gmp.h>],
862       [AC_MSG_FAILURE([cannot define GMP_NUMB_BITS])])
863    AC_DEFINE_UNQUOTED([GMP_NUMB_BITS], $mini_gmp_numb_bits, [number of bits in a limb])
864  ])
865CPPFLAGS="$saved_CPPFLAGS"
866AC_MSG_RESULT([$mini_gmp_numb_bits bits ($how)])
867
868fi
869
870dnl End of setup related to GMP / mini-gmp
871
872dnl The getrusage function is needed for MPFR bench (cf tools/bench)
873AC_CHECK_FUNCS([getrusage])
874
875dnl Remove also many macros (AC_DEFINE), which are unused by MPFR and
876dnl pollute (and slow down because libtool has to parse them) the build.
877if test -f confdefs.h; then
878  for i in PACKAGE_ HAVE_STRING HAVE_DLFCN_H HAVE_MEM STDC_HEADERS \
879           HAVE_STDLIB_H HAVE_UNISTD_H HAVE_STDC_HEADERS \
880           HAVE_SYS_STAT_H HAVE_SYS_TYPES_H PROTOTYPES __PROTOTYPES
881  do
882    $SED "/#define $i/d" < confdefs.h > confdefs.tmp
883    mv confdefs.tmp confdefs.h
884  done
885fi
886
887if test -n "$dev_version"; then
888  AC_SUBST([DATAFILES])dnl
889  DATAFILES=`echo \`$SED -n \
890    's/^ *data_check *("\(data\/[[^"]]*\)".*/tests\/\1/p' \
891    $srcdir/tests/*.c\``
892fi
893
894dnl Output
895AC_CONFIG_FILES([Makefile mpfr.pc doc/Makefile src/Makefile tests/Makefile tune/Makefile src/mparam.h:src/mparam_h.in tools/bench/Makefile])
896AC_OUTPUT
897
898dnl NEWS README AUTHORS Changelog
899