Merge trunk version 194962 into gupc branch.
[official-gcc.git] / libgupc / acinclude.m4
blobb158240e4dc39703e8bff83d0e3c258ef7ad17e6
1 dnl Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2004
2 dnl Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 dnl Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 dnl This program is distributed in the hope that it will be useful,
9 dnl but WITHOUT ANY WARRANTY, to the extent permitted by law; without
10 dnl even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11 dnl PARTICULAR PURPOSE.
13 dnl
14 dnl LIBGUPC_GCC_TLS_SUPPORTED
15 dnl
16 AC_DEFUN([LIBGUPC_GCC_TLS_SUPPORTED], [
17 AC_CACHE_CHECK([whether the GCC __threads extension is supported.],
18                upc_cv_gcc_tls_supported,
19 [SAVE_LIBS="$LIBS"
20 LIBS="$LIBS -lpthread"
21 AC_TRY_RUN(
22 changequote(<<,>>)dnl
23 <<#include <stdio.h>
24 #include <stddef.h>
25 #include <malloc.h>
26 #include <errno.h>
27 #include <pthread.h>
29 #define NTHREADS 5
31 pthread_t p[NTHREADS];
33 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
35 __thread long tlocal = 0;
37 void *
38 thread_start (void *arg)
40   int id = *((int *)arg);
41   int *return_val = malloc(sizeof(int));
42   if (pthread_mutex_lock (&lock))
43     { exit (2); }
44   /* if the value is truly thread-local, this assignment
45      will yield the value 1, for each thread. If tlocal
46      is instead a process global static value then tlocal
47      will be incremented by each thread, and its final
48      value will be the number of threads. */
49   tlocal += 1;
50   if (pthread_mutex_unlock (&lock))
51     { exit (2); }
52   *return_val = tlocal;
53   return return_val;
56 int
57 main()
59   int i;
60   for (i = 0; i < NTHREADS; ++i)
61     {
62       int *id = (int *)malloc(sizeof(int));
63       *id = i;
64       if (pthread_create(&p[i], NULL, thread_start, id))
65         { exit (2); }
66     }
67   for (i = 0; i < NTHREADS; ++i)
68     {
69       int *rc;
70       if (pthread_join (p[i], (void **)&rc))
71         { exit (2); }
72       if (*rc != 1)
73         { exit (1); }
74     }
75   return 0;
77 >>,
78 changequote([, ])dnl
79   [upc_cv_gcc_tls_supported="yes"],
80   [upc_cv_gcc_tls_supported="no"],
81   [upc_cv_gcc_tls_supported="no"])
82   LIBS="$SAVE_LIBS"]
84 dnl if test "$upc_cv_gcc_tls_supported"x = "yes"x ; then
85 dnl   AC_DEFINE([HAVE_GCC_TLS_SUPPORT], 1)
86 dnl fi
89 dnl ----------------------------------------------------------------------
90 dnl The following (to the end of file) is copied from libgomp.
91 dnl ----------------------------------------------------------------------
92 dnl This whole bit snagged from libgfortran.
94 dnl Check whether the target supports __sync_*_compare_and_swap.
95 AC_DEFUN([LIBGUPC_CHECK_SYNC_BUILTINS], [
96   AC_CACHE_CHECK([whether the target supports __sync_*_compare_and_swap],
97                  libgupc_cv_have_sync_builtins, [
98   AC_TRY_LINK([], [int foo; (void) __sync_val_compare_and_swap(&foo, 0, 1);],
99               libgupc_cv_have_sync_builtins=yes,
100               libgupc_cv_have_sync_builtins=no)])
101   if test $libgupc_cv_have_sync_builtins = yes; then
102     AC_DEFINE(HAVE_SYNC_BUILTINS, 1,
103               [Define to 1 if the target supports __sync_*_compare_and_swap.])
104   fi])
106 dnl Check whether the target supports hidden visibility.
107 AC_DEFUN([LIBGUPC_CHECK_ATTRIBUTE_VISIBILITY], [
108   AC_CACHE_CHECK([whether the target supports hidden visibility],
109                  libgupc_cv_have_attribute_visibility, [
110   save_CFLAGS="$CFLAGS"
111   CFLAGS="$CFLAGS -Werror"
112   AC_TRY_COMPILE([void __attribute__((visibility("hidden"))) foo(void) { }],
113                  [], libgupc_cv_have_attribute_visibility=yes,
114                  libgupc_cv_have_attribute_visibility=no)
115   CFLAGS="$save_CFLAGS"])
116   if test $libgupc_cv_have_attribute_visibility = yes; then
117     AC_DEFINE(HAVE_ATTRIBUTE_VISIBILITY, 1,
118       [Define to 1 if the target supports __attribute__((visibility(...))).])
119   fi])
121 dnl Check whether the target supports dllexport
122 AC_DEFUN([LIBGUPC_CHECK_ATTRIBUTE_DLLEXPORT], [
123   AC_CACHE_CHECK([whether the target supports dllexport],
124                  libgupc_cv_have_attribute_dllexport, [
125   save_CFLAGS="$CFLAGS"
126   CFLAGS="$CFLAGS -Werror"
127   AC_TRY_COMPILE([void __attribute__((dllexport)) foo(void) { }],
128                  [], libgupc_cv_have_attribute_dllexport=yes,
129                  libgupc_cv_have_attribute_dllexport=no)
130   CFLAGS="$save_CFLAGS"])
131   if test $libgupc_cv_have_attribute_dllexport = yes; then
132     AC_DEFINE(HAVE_ATTRIBUTE_DLLEXPORT, 1,
133       [Define to 1 if the target supports __attribute__((dllexport)).])
134   fi])
136 dnl Check whether the target supports symbol aliases.
137 AC_DEFUN([LIBGUPC_CHECK_ATTRIBUTE_ALIAS], [
138   AC_CACHE_CHECK([whether the target supports symbol aliases],
139                  libgupc_cv_have_attribute_alias, [
140   AC_TRY_LINK([
141 void foo(void) { }
142 extern void bar(void) __attribute__((alias("foo")));],
143     [bar();], libgupc_cv_have_attribute_alias=yes, libgupc_cv_have_attribute_alias=no)])
144   if test $libgupc_cv_have_attribute_alias = yes; then
145     AC_DEFINE(HAVE_ATTRIBUTE_ALIAS, 1,
146       [Define to 1 if the target supports __attribute__((alias(...))).])
147   fi])
149 sinclude(../libtool.m4)
150 dnl The lines below arrange for aclocal not to bring an installed
151 dnl libtool.m4 into aclocal.m4, while still arranging for automake to
152 dnl add a definition of LIBTOOL to Makefile.in.
153 ifelse(,,,[AC_SUBST(LIBTOOL)
154 AC_DEFUN([AM_PROG_LIBTOOL])
155 AC_DEFUN([AC_LIBTOOL_DLOPEN])
156 AC_DEFUN([AC_PROG_LD])
159 dnl ----------------------------------------------------------------------
160 dnl This whole bit snagged from libstdc++-v3.
163 dnl LIBGUPC_ENABLE
164 dnl    (FEATURE, DEFAULT, HELP-ARG, HELP-STRING)
165 dnl    (FEATURE, DEFAULT, HELP-ARG, HELP-STRING, permit a|b|c)
166 dnl    (FEATURE, DEFAULT, HELP-ARG, HELP-STRING, SHELL-CODE-HANDLER)
168 dnl See docs/html/17_intro/configury.html#enable for documentation.
170 m4_define([LIBGUPC_ENABLE],[dnl
171 m4_define([_g_switch],[--enable-$1])dnl
172 m4_define([_g_help],[AC_HELP_STRING(_g_switch$3,[$4 @<:@default=$2@:>@])])dnl
173  AC_ARG_ENABLE($1,_g_help,
174   m4_bmatch([$5],
175    [^permit ],
176      [[
177       case "$enableval" in
178        m4_bpatsubst([$5],[permit ])) ;;
179        *) AC_MSG_ERROR(Unknown argument to enable/disable $1) ;;
180           dnl Idea for future:  generate a URL pointing to
181           dnl "onlinedocs/configopts.html#whatever"
182       esac
183      ]],
184    [^$],
185      [[
186       case "$enableval" in
187        yes|no) ;;
188        *) AC_MSG_ERROR(Argument to enable/disable $1 must be yes or no) ;;
189       esac
190      ]],
191    [[$5]]),
192   [enable_]m4_bpatsubst([$1],-,_)[=][$2])
193 m4_undefine([_g_switch])dnl
194 m4_undefine([_g_help])dnl
199 dnl If GNU ld is in use, check to see if tricky linker opts can be used.  If
200 dnl the native linker is in use, all variables will be defined to something
201 dnl safe (like an empty string).
203 dnl Defines:
204 dnl  SECTION_LDFLAGS='-Wl,--gc-sections' if possible
205 dnl  OPT_LDFLAGS='-Wl,-O1' if possible
206 dnl  LD (as a side effect of testing)
207 dnl Sets:
208 dnl  with_gnu_ld
209 dnl  libgupc_ld_is_gold (possibly)
210 dnl  libgupc_gnu_ld_version (possibly)
212 dnl The last will be a single integer, e.g., version 1.23.45.0.67.89 will
213 dnl set libgupc_gnu_ld_version to 12345.  Zeros cause problems.
215 AC_DEFUN([LIBGUPC_CHECK_LINKER_FEATURES], [
216   # If we're not using GNU ld, then there's no point in even trying these
217   # tests.  Check for that first.  We should have already tested for gld
218   # by now (in libtool), but require it now just to be safe...
219   test -z "$SECTION_LDFLAGS" && SECTION_LDFLAGS=''
220   test -z "$OPT_LDFLAGS" && OPT_LDFLAGS=''
221   AC_REQUIRE([AC_PROG_LD])
222   AC_REQUIRE([AC_PROG_AWK])
224   # The name set by libtool depends on the version of libtool.  Shame on us
225   # for depending on an impl detail, but c'est la vie.  Older versions used
226   # ac_cv_prog_gnu_ld, but now it's lt_cv_prog_gnu_ld, and is copied back on
227   # top of with_gnu_ld (which is also set by --with-gnu-ld, so that actually
228   # makes sense).  We'll test with_gnu_ld everywhere else, so if that isn't
229   # set (hence we're using an older libtool), then set it.
230   if test x${with_gnu_ld+set} != xset; then
231     if test x${ac_cv_prog_gnu_ld+set} != xset; then
232       # We got through "ac_require(ac_prog_ld)" and still not set?  Huh?
233       with_gnu_ld=no
234     else
235       with_gnu_ld=$ac_cv_prog_gnu_ld
236     fi
237   fi
239   # Start by getting the version number.  I think the libtool test already
240   # does some of this, but throws away the result.
241   libgupc_ld_is_gold=no
242   if $LD --version 2>/dev/null | grep 'GNU gold'> /dev/null 2>&1; then
243     libgupc_ld_is_gold=yes
244   fi
245   changequote(,)
246   ldver=`$LD --version 2>/dev/null |
247          sed -e 's/GNU gold /GNU ld /;s/GNU ld version /GNU ld /;s/GNU ld ([^)]*) /GNU ld /;s/GNU ld \([0-9.][0-9.]*\).*/\1/; q'`
248   changequote([,])
249   libgupc_gnu_ld_version=`echo $ldver | \
250          $AWK -F. '{ if (NF<3) [$]3=0; print ([$]1*100+[$]2)*100+[$]3 }'`
252   # Set --gc-sections.
253   if test "$with_gnu_ld" = "notbroken"; then
254     # GNU ld it is!  Joy and bunny rabbits!
256     # All these tests are for C++; save the language and the compiler flags.
257     # Need to do this so that g++ won't try to link in libstdc++
258     ac_test_CFLAGS="${CFLAGS+set}"
259     ac_save_CFLAGS="$CFLAGS"
260     CFLAGS='-x c++  -Wl,--gc-sections'
262     # Check for -Wl,--gc-sections
263     # XXX This test is broken at the moment, as symbols required for linking
264     # are now in libsupc++ (not built yet).  In addition, this test has
265     # cored on solaris in the past.  In addition, --gc-sections doesn't
266     # really work at the moment (keeps on discarding used sections, first
267     # .eh_frame and now some of the glibc sections for iconv).
268     # Bzzzzt.  Thanks for playing, maybe next time.
269     AC_MSG_CHECKING([for ld that supports -Wl,--gc-sections])
270     AC_TRY_RUN([
271      int main(void)
272      {
273        try { throw 1; }
274        catch (...) { };
275        return 0;
276      }
277     ], [ac_sectionLDflags=yes],[ac_sectionLDflags=no], [ac_sectionLDflags=yes])
278     if test "$ac_test_CFLAGS" = set; then
279       CFLAGS="$ac_save_CFLAGS"
280     else
281       # this is the suspicious part
282       CFLAGS=''
283     fi
284     if test "$ac_sectionLDflags" = "yes"; then
285       SECTION_LDFLAGS="-Wl,--gc-sections $SECTION_LDFLAGS"
286     fi
287     AC_MSG_RESULT($ac_sectionLDflags)
288   fi
290   # Set linker optimization flags.
291   if test x"$with_gnu_ld" = x"yes"; then
292     OPT_LDFLAGS="-Wl,-O1 $OPT_LDFLAGS"
293   fi
295   AC_SUBST(SECTION_LDFLAGS)
296   AC_SUBST(OPT_LDFLAGS)
301 dnl Add version tags to symbols in shared library (or not), additionally
302 dnl marking other symbols as private/local (or not).
304 dnl --enable-symvers=style adds a version script to the linker call when
305 dnl       creating the shared library.  The choice of version script is
306 dnl       controlled by 'style'.
307 dnl --disable-symvers does not.
308 dnl  +  Usage:  LIBGUPC_ENABLE_SYMVERS[(DEFAULT)]
309 dnl       Where DEFAULT is either 'yes' or 'no'.  Passing `yes' tries to
310 dnl       choose a default style based on linker characteristics.  Passing
311 dnl       'no' disables versioning.
313 AC_DEFUN([LIBGUPC_ENABLE_SYMVERS], [
315 LIBGUPC_ENABLE(symvers,yes,[=STYLE],
316   [enables symbol versioning of the shared library],
317   [permit yes|no|gnu])
319 # If we never went through the LIBGUPC_CHECK_LINKER_FEATURES macro, then we
320 # don't know enough about $LD to do tricks...
321 AC_REQUIRE([LIBGUPC_CHECK_LINKER_FEATURES])
322 # FIXME  The following test is too strict, in theory.
323 if test $enable_shared = no ||
324         test "x$LD" = x ||
325         test x$libgupc_gnu_ld_version = x; then
326   enable_symvers=no
329 # Check to see if libgcc_s exists, indicating that shared libgcc is possible.
330 if test $enable_symvers != no; then
331   AC_MSG_CHECKING([for shared libgcc])
332   ac_save_CFLAGS="$CFLAGS"
333   CFLAGS=' -lgcc_s'
334   AC_TRY_LINK(, [return 0;], libgupc_shared_libgcc=yes, libgupc_shared_libgcc=no)
335   CFLAGS="$ac_save_CFLAGS"
336   if test $libgupc_shared_libgcc = no; then
337     cat > conftest.c <<EOF
338 int main (void) { return 0; }
340 changequote(,)dnl
341     libgupc_libgcc_s_suffix=`${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS \
342                              -shared -shared-libgcc -o conftest.so \
343                              conftest.c -v 2>&1 >/dev/null \
344                              | sed -n 's/^.* -lgcc_s\([^ ]*\) .*$/\1/p'`
345 changequote([,])dnl
346     rm -f conftest.c conftest.so
347     if test x${libgupc_libgcc_s_suffix+set} = xset; then
348       CFLAGS=" -lgcc_s$libgupc_libgcc_s_suffix"
349       AC_TRY_LINK(, [return 0;], libgupc_shared_libgcc=yes)
350       CFLAGS="$ac_save_CFLAGS"
351     fi
352   fi
353   AC_MSG_RESULT($libgupc_shared_libgcc)
356 # For GNU ld, we need at least this version.  The format is described in
357 # LIBGUPC_CHECK_LINKER_FEATURES above.
358 libgupc_min_gnu_ld_version=21400
359 # XXXXXXXXXXX libgupc_gnu_ld_version=21390
361 # Check to see if unspecified "yes" value can win, given results above.
362 # Change "yes" into either "no" or a style name.
363 if test $enable_symvers = yes; then
364   if test $with_gnu_ld = yes &&
365      test $libgupc_shared_libgcc = yes;
366   then
367     if test $libgupc_gnu_ld_version -ge $libgupc_min_gnu_ld_version ; then
368       enable_symvers=gnu
369     elif test $libgupc_ld_is_gold = yes ; then
370       enable_symvers=gnu
371     else
372       # The right tools, the right setup, but too old.  Fallbacks?
373       AC_MSG_WARN(=== Linker version $libgupc_gnu_ld_version is too old for)
374       AC_MSG_WARN(=== full symbol versioning support in this release of GCC.)
375       AC_MSG_WARN(=== You would need to upgrade your binutils to version)
376       AC_MSG_WARN(=== $libgupc_min_gnu_ld_version or later and rebuild GCC.)
377       if test $libgupc_gnu_ld_version -ge 21200 ; then
378         # Globbing fix is present, proper block support is not.
379         dnl AC_MSG_WARN([=== Dude, you are soooo close.  Maybe we can fake it.])
380         dnl enable_symvers=???
381         AC_MSG_WARN([=== Symbol versioning will be disabled.])
382         enable_symvers=no
383       else
384         # 2.11 or older.
385         AC_MSG_WARN([=== Symbol versioning will be disabled.])
386         enable_symvers=no
387       fi
388     fi
389   else
390     # just fail for now
391     AC_MSG_WARN([=== You have requested some kind of symbol versioning, but])
392     AC_MSG_WARN([=== either you are not using a supported linker, or you are])
393     AC_MSG_WARN([=== not building a shared libgcc_s (which is required).])
394     AC_MSG_WARN([=== Symbol versioning will be disabled.])
395     enable_symvers=no
396   fi
399 AC_CACHE_CHECK([whether the target supports .symver directive],
400                libgupc_cv_have_as_symver_directive, [
401   AC_TRY_COMPILE([void foo (void); __asm (".symver foo, bar@SYMVER");],
402                  [], libgupc_cv_have_as_symver_directive=yes,
403                  libgupc_cv_have_as_symver_directive=no)])
404 if test $libgupc_cv_have_as_symver_directive = yes; then
405   AC_DEFINE(HAVE_AS_SYMVER_DIRECTIVE, 1,
406     [Define to 1 if the target assembler supports .symver directive.])
409 AM_CONDITIONAL(LIBGUPC_BUILD_VERSIONED_SHLIB, test $enable_symvers != no)
410 AC_MSG_NOTICE(versioning on shared library symbols is $enable_symvers)