PR target/65871
[official-gcc.git] / libgfortran / acinclude.m4
blob30b8b1a629198c7f5570798655dd1a3ef6adc1b3
1 m4_include(../config/acx.m4)
2 m4_include(../config/no-executables.m4)
3 m4_include(../config/math.m4)
5 dnl Check that we have a working GNU Fortran compiler
6 AC_DEFUN([LIBGFOR_WORKING_GFORTRAN], [
7 AC_MSG_CHECKING([whether the GNU Fortran compiler is working])
8 AC_LANG_PUSH([Fortran])
9 AC_COMPILE_IFELSE([[
10       program foo
11       real, parameter :: bar = sin (12.34 / 2.5)
12       end program foo]],
13     [AC_MSG_RESULT([yes])],
14     [AC_MSG_RESULT([no])
15      AC_MSG_ERROR([GNU Fortran is not working; please report a bug in http://gcc.gnu.org/bugzilla, attaching $PWD/config.log])
16     ])
17 AC_LANG_POP([Fortran])
21 sinclude(../libtool.m4)
22 dnl The lines below arrange for aclocal not to bring an installed
23 dnl libtool.m4 into aclocal.m4, while still arranging for automake to
24 dnl add a definition of LIBTOOL to Makefile.in.
25 ifelse(,,,[AC_SUBST(LIBTOOL)
26 AC_DEFUN([AM_PROG_LIBTOOL])
27 AC_DEFUN([AC_LIBTOOL_DLOPEN])
28 AC_DEFUN([AC_PROG_LD])
31 dnl Check whether the target supports hidden visibility.
32 AC_DEFUN([LIBGFOR_CHECK_ATTRIBUTE_VISIBILITY], [
33   AC_CACHE_CHECK([whether the target supports hidden visibility],
34                  libgfor_cv_have_attribute_visibility, [
35   save_CFLAGS="$CFLAGS"
36   CFLAGS="$CFLAGS -Werror"
37   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[void __attribute__((visibility("hidden"))) foo(void) { }]], [])],
38                     libgfor_cv_have_attribute_visibility=yes,
39                     libgfor_cv_have_attribute_visibility=no)
40   CFLAGS="$save_CFLAGS"])
41   if test $libgfor_cv_have_attribute_visibility = yes; then
42     AC_DEFINE(HAVE_ATTRIBUTE_VISIBILITY, 1,
43       [Define to 1 if the target supports __attribute__((visibility(...))).])
44   fi])
46 dnl Check whether the target supports dllexport
47 AC_DEFUN([LIBGFOR_CHECK_ATTRIBUTE_DLLEXPORT], [
48   AC_CACHE_CHECK([whether the target supports dllexport],
49                  libgfor_cv_have_attribute_dllexport, [
50   save_CFLAGS="$CFLAGS"
51   CFLAGS="$CFLAGS -Werror"
52   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[void __attribute__((dllexport)) foo(void) { }]], [])],
53                     libgfor_cv_have_attribute_dllexport=yes,
54                     libgfor_cv_have_attribute_dllexport=no)
55   CFLAGS="$save_CFLAGS"])
56   if test $libgfor_cv_have_attribute_dllexport = yes; then
57     AC_DEFINE(HAVE_ATTRIBUTE_DLLEXPORT, 1,
58       [Define to 1 if the target supports __attribute__((dllexport)).])
59   fi])
61 dnl Check whether the target supports symbol aliases.
62 AC_DEFUN([LIBGFOR_CHECK_ATTRIBUTE_ALIAS], [
63   AC_CACHE_CHECK([whether the target supports symbol aliases],
64                  libgfor_cv_have_attribute_alias, [
65   AC_LINK_IFELSE([AC_LANG_PROGRAM([[
66 void foo(void) { }
67 extern void bar(void) __attribute__((alias("foo")));]],
68     [[bar();]])], libgfor_cv_have_attribute_alias=yes, libgfor_cv_have_attribute_alias=no)])
69   if test $libgfor_cv_have_attribute_alias = yes; then
70     AC_DEFINE(HAVE_ATTRIBUTE_ALIAS, 1,
71       [Define to 1 if the target supports __attribute__((alias(...))).])
72   fi])
74 dnl Check whether the target supports __sync_fetch_and_add.
75 AC_DEFUN([LIBGFOR_CHECK_SYNC_FETCH_AND_ADD], [
76   AC_CACHE_CHECK([whether the target supports __sync_fetch_and_add],
77                  libgfor_cv_have_sync_fetch_and_add, [
78   AC_LINK_IFELSE([AC_LANG_PROGRAM([[int foovar = 0;]], [[
79 if (foovar <= 0) return __sync_fetch_and_add (&foovar, 1);
80 if (foovar > 10) return __sync_add_and_fetch (&foovar, -1);]])],
81               libgfor_cv_have_sync_fetch_and_add=yes, libgfor_cv_have_sync_fetch_and_add=no)])
82   if test $libgfor_cv_have_sync_fetch_and_add = yes; then
83     AC_DEFINE(HAVE_SYNC_FETCH_AND_ADD, 1,
84               [Define to 1 if the target supports __sync_fetch_and_add])
85   fi])
87 dnl Check for pragma weak.
88 AC_DEFUN([LIBGFOR_GTHREAD_WEAK], [
89   AC_CACHE_CHECK([whether pragma weak works],
90                  libgfor_cv_have_pragma_weak, [
91   gfor_save_CFLAGS="$CFLAGS"
92   CFLAGS="$CFLAGS -Wunknown-pragmas"
93   AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
94 void foo (void);
95 #pragma weak foo
96 ]], [[if (foo) foo ();]])],
97                     libgfor_cv_have_pragma_weak=yes, libgfor_cv_have_pragma_weak=no)])
98   if test $libgfor_cv_have_pragma_weak = yes; then
99     AC_DEFINE(SUPPORTS_WEAK, 1,
100               [Define to 1 if the target supports #pragma weak])
101   fi
102   case "$host" in
103     *-*-darwin* | *-*-hpux* | *-*-cygwin* | *-*-mingw* | *-*-musl* )
104       AC_DEFINE(GTHREAD_USE_WEAK, 0,
105                 [Define to 0 if the target shouldn't use #pragma weak])
106       ;;
107   esac])
109 dnl Check whether target can unlink a file still open.
110 AC_DEFUN([LIBGFOR_CHECK_UNLINK_OPEN_FILE], [
111   AC_CACHE_CHECK([whether the target can unlink an open file],
112                   libgfor_cv_have_unlink_open_file, [
113   AC_RUN_IFELSE([AC_LANG_SOURCE([[
114 #include <errno.h>
115 #include <fcntl.h>
116 #include <unistd.h>
117 #include <sys/stat.h>
119 int main ()
121   int fd;
123   fd = open ("testfile", O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
124   if (fd <= 0)
125     return 0;
126   if (unlink ("testfile") == -1)
127     return 1;
128   write (fd, "This is a test\n", 15);
129   close (fd);
131   if (open ("testfile", O_RDONLY) == -1 && errno == ENOENT)
132     return 0;
133   else
134     return 1;
135 }]])], libgfor_cv_have_unlink_open_file=yes, libgfor_cv_have_unlink_open_file=no, [
136 case "${target}" in
137   *mingw*) libgfor_cv_have_unlink_open_file=no ;;
138   *) libgfor_cv_have_unlink_open_file=yes;;
139 esac])])
140 if test x"$libgfor_cv_have_unlink_open_file" = xyes; then
141   AC_DEFINE(HAVE_UNLINK_OPEN_FILE, 1, [Define if target can unlink open files.])
142 fi])
144 dnl Check whether CRLF is the line terminator
145 AC_DEFUN([LIBGFOR_CHECK_CRLF], [
146   AC_CACHE_CHECK([whether the target has CRLF as line terminator],
147                   libgfor_cv_have_crlf, [
148   AC_RUN_IFELSE([AC_LANG_SOURCE([[
149 /* This test program should exit with status 0 if system uses a CRLF as
150    line terminator, and status 1 otherwise.  
151    Since it is used to check for mingw systems, and should return 0 in any
152    other case, in case of a failure we will not use CRLF.  */
153 #include <sys/stat.h>
154 #include <stdlib.h>
155 #include <fcntl.h>
156 #include <stdio.h>
158 int main ()
160 #ifndef O_BINARY
161   exit(1);
162 #else
163   int fd, bytes;
164   char buff[5];
166   fd = open ("foo", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
167   if (fd < 0)
168     exit(1);
169   if (write (fd, "\n", 1) < 0)
170     perror ("write");
171   
172   close (fd);
173   
174   if ((fd = open ("foo", O_RDONLY | O_BINARY, S_IRWXU)) < 0)
175     exit(1);
176   bytes = read (fd, buff, 5);
177   if (bytes == 2 && buff[0] == '\r' && buff[1] == '\n')
178     exit(0);
179   else
180     exit(1);
181 #endif
182 }]])], libgfor_cv_have_crlf=yes, libgfor_cv_have_crlf=no, [
183 case "${target}" in
184   *mingw*) libgfor_cv_have_crlf=yes ;;
185   *) libgfor_cv_have_crlf=no;;
186 esac])])
187 if test x"$libgfor_cv_have_crlf" = xyes; then
188   AC_DEFINE(HAVE_CRLF, 1, [Define if CRLF is line terminator.])
189 fi])
191 dnl Check whether the st_ino and st_dev stat fields taken together uniquely
192 dnl identify the file within the system. This is should be true for POSIX
193 dnl systems; it is known to be false on mingw32.
194 AC_DEFUN([LIBGFOR_CHECK_WORKING_STAT], [
195   AC_CACHE_CHECK([whether the target stat is reliable],
196                   libgfor_cv_have_working_stat, [
197   AC_RUN_IFELSE([AC_LANG_SOURCE([[
198 #include <stdio.h>
199 #include <sys/types.h>
200 #include <sys/stat.h>
201 #include <unistd.h>
203 int main ()
205   FILE *f, *g;
206   struct stat st1, st2;
208   f = fopen ("foo", "w");
209   g = fopen ("bar", "w");
210   if (stat ("foo", &st1) != 0 || stat ("bar", &st2))
211     return 1;
212   if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
213     return 1;
214   fclose(f);
215   fclose(g);
216   return 0;
217 }]])], libgfor_cv_have_working_stat=yes, libgfor_cv_have_working_stat=no, [
218 case "${target}" in
219   *mingw*) libgfor_cv_have_working_stat=no ;;
220   *) libgfor_cv_have_working_stat=yes;;
221 esac])])
222 if test x"$libgfor_cv_have_working_stat" = xyes; then
223   AC_DEFINE(HAVE_WORKING_STAT, 1, [Define if target has a reliable stat.])
224 fi])
226 dnl Checks for fpsetmask function.
227 AC_DEFUN([LIBGFOR_CHECK_FPSETMASK], [
228   AC_CACHE_CHECK([whether fpsetmask is present], libgfor_cv_have_fpsetmask, [
229     AC_LINK_IFELSE([AC_LANG_PROGRAM([[
230 #if HAVE_FLOATINGPOINT_H
231 # include <floatingpoint.h>
232 #endif /* HAVE_FLOATINGPOINT_H */
233 #if HAVE_IEEEFP_H
234 # include <ieeefp.h>
235 #endif /* HAVE_IEEEFP_H */]],[[fpsetmask(0);]])],
236     eval "libgfor_cv_have_fpsetmask=yes", eval "libgfor_cv_have_fpsetmask=no")
237   ])
238   if test x"$libgfor_cv_have_fpsetmask" = xyes; then
239     have_fpsetmask=yes
240     AC_DEFINE(HAVE_FPSETMASK, 1, [Define if you have fpsetmask.])
241   fi
244 dnl Check whether we have a mingw that provides a __mingw_snprintf function
245 AC_DEFUN([LIBGFOR_CHECK_MINGW_SNPRINTF], [
246   AC_CACHE_CHECK([whether __mingw_snprintf is present], libgfor_cv_have_mingw_snprintf, [
247     AC_LINK_IFELSE([AC_LANG_PROGRAM([[
248 #include <stdio.h>
249 extern int __mingw_snprintf (char *, size_t, const char *, ...);
250 ]],[[
251 __mingw_snprintf (NULL, 0, "%d\n", 1);
252 ]])],
253     eval "libgfor_cv_have_mingw_snprintf=yes", eval "libgfor_cv_have_mingw_snprintf=no")
254   ])
255   if test x"$libgfor_cv_have_mingw_snprintf" = xyes; then
256     AC_DEFINE(HAVE_MINGW_SNPRINTF, 1, [Define if you have __mingw_snprintf.])
257   fi
260 dnl Check whether we have a __float128 type
261 AC_DEFUN([LIBGFOR_CHECK_FLOAT128], [
262   LIBQUADSPEC=
264   if test "x$enable_libquadmath_support" != xno; then
266   AC_CACHE_CHECK([whether we have a usable __float128 type],
267                  libgfor_cv_have_float128, [
268    GCC_TRY_COMPILE_OR_LINK([
269     typedef _Complex float __attribute__((mode(TC))) __complex128;
271     __float128 foo (__float128 x)
272     {
274      __complex128 z1, z2;
276      z1 = x;
277      z2 = x / 7.Q;
278      z2 /= z1;
280      return (__float128) z2;
281     }
283     __float128 bar (__float128 x)
284     {
285       return x * __builtin_huge_valq ();
286     }
287   ],[
288     foo (1.2Q);
289     bar (1.2Q);
290   ],[
291     libgfor_cv_have_float128=yes
292   ],[
293     libgfor_cv_have_float128=no
294 ])])
296   if test "x$libgfor_cv_have_float128" = xyes; then
297     AC_DEFINE(HAVE_FLOAT128, 1, [Define if have a usable __float128 type.])
299     dnl Check whether -Wl,--as-needed resp. -Wl,-zignore is supported
300     dnl 
301     dnl Turn warnings into error to avoid testsuite breakage.  So enable
302     dnl AC_LANG_WERROR, but there's currently (autoconf 2.64) no way to turn
303     dnl it off again.  As a workaround, save and restore werror flag like
304     dnl AC_PATH_XTRA.
305     dnl Cf. http://gcc.gnu.org/ml/gcc-patches/2010-05/msg01889.html
306     ac_xsave_[]_AC_LANG_ABBREV[]_werror_flag=$ac_[]_AC_LANG_ABBREV[]_werror_flag
307     AC_CACHE_CHECK([whether --as-needed/-z ignore works],
308       [libgfor_cv_have_as_needed],
309       [
310       # Test for native Solaris options first.
311       # No whitespace after -z to pass it through -Wl.
312       libgfor_cv_as_needed_option="-zignore"
313       libgfor_cv_no_as_needed_option="-zrecord"
314       save_LDFLAGS="$LDFLAGS"
315       LDFLAGS="$LDFLAGS -Wl,$libgfor_cv_as_needed_option -lm -Wl,$libgfor_cv_no_as_needed_option"
316       libgfor_cv_have_as_needed=no
317       AC_LANG_WERROR
318       AC_LINK_IFELSE([AC_LANG_PROGRAM([])],
319                      [libgfor_cv_have_as_needed=yes],
320                      [libgfor_cv_have_as_needed=no])
321       LDFLAGS="$save_LDFLAGS"
322       if test "x$libgfor_cv_have_as_needed" = xno; then
323         libgfor_cv_as_needed_option="--as-needed"
324         libgfor_cv_no_as_needed_option="--no-as-needed"
325         save_LDFLAGS="$LDFLAGS"
326         LDFLAGS="$LDFLAGS -Wl,$libgfor_cv_as_needed_option -lm -Wl,$libgfor_cv_no_as_needed_option"
327         libgfor_cv_have_as_needed=no
328         AC_LANG_WERROR
329         AC_LINK_IFELSE([AC_LANG_PROGRAM([])],
330                        [libgfor_cv_have_as_needed=yes],
331                        [libgfor_cv_have_as_needed=no])
332         LDFLAGS="$save_LDFLAGS"
333       fi
334       ac_[]_AC_LANG_ABBREV[]_werror_flag=$ac_xsave_[]_AC_LANG_ABBREV[]_werror_flag
335     ])
337     dnl For static libgfortran linkage, depend on libquadmath only if needed.
338     if test "x$libgfor_cv_have_as_needed" = xyes; then
339       LIBQUADSPEC="%{static-libgfortran:$libgfor_cv_as_needed_option} -lquadmath %{static-libgfortran:$libgfor_cv_no_as_needed_option}"
340     else
341       LIBQUADSPEC="-lquadmath"
342     fi
343     if test -f ../libquadmath/libquadmath.la; then
344       LIBQUADLIB=../libquadmath/libquadmath.la
345       LIBQUADLIB_DEP=../libquadmath/libquadmath.la
346       LIBQUADINCLUDE='-I$(srcdir)/../libquadmath'
347     else
348       LIBQUADLIB="-lquadmath"
349       LIBQUADLIB_DEP=
350       LIBQUADINCLUDE=
351     fi
352   fi
353   else
354     # for --disable-quadmath
355     LIBQUADLIB=
356     LIBQUADLIB_DEP=
357     LIBQUADINCLUDE=
358   fi
360   dnl For the spec file
361   AC_SUBST(LIBQUADSPEC)
362   AC_SUBST(LIBQUADLIB)
363   AC_SUBST(LIBQUADLIB_DEP)
364   AC_SUBST(LIBQUADINCLUDE)
366   dnl We need a conditional for the Makefile
367   AM_CONDITIONAL(LIBGFOR_BUILD_QUAD, [test "x$libgfor_cv_have_float128" = xyes])
371 dnl Check whether we have strerror_r
372 AC_DEFUN([LIBGFOR_CHECK_STRERROR_R], [
373   dnl Check for three-argument POSIX version of strerror_r
374   ac_save_CFLAGS="$CFLAGS"
375   CFLAGS="-Wimplicit-function-declaration -Werror"
376   AC_TRY_COMPILE([#define _GNU_SOURCE 1
377                   #include <string.h>
378                   #include <locale.h>],
379                   [char s[128]; strerror_r(5, s, 128);],
380                   AC_DEFINE(HAVE_STRERROR_R, 1,
381                   [Define if strerror_r is available in <string.h>.]),)
382   CFLAGS="$ac_save_CFLAGS"
384   dnl Check for two-argument version of strerror_r (e.g. for VxWorks)
385   ac_save_CFLAGS="$CFLAGS"
386   CFLAGS="-Wimplicit-function-declaration -Werror"
387   AC_TRY_COMPILE([#define _GNU_SOURCE 1
388                   #include <string.h>
389                   #include <locale.h>],
390                   [char s[128]; strerror_r(5, s);],
391                   AC_DEFINE(HAVE_STRERROR_R_2ARGS, 1,
392                   [Define if strerror_r takes two arguments and is available in <string.h>.]),)
393   CFLAGS="$ac_save_CFLAGS"