Import Upstream version 1.2.2
[quagga-debian.git] / m4 / ax_sys_weak_alias.m4
1 # ===========================================================================
2 # http://www.gnu.org/software/autoconf-archive/ax_sys_weak_alias.html
3 # ===========================================================================
4 #
5 # SYNOPSIS
6 #
7 #   AX_SYS_WEAK_ALIAS
8 #
9 # DESCRIPTION
10 #
11 #   Determines whether weak aliases are supported on the system, and if so,
12 #   what scheme is used to declare them. Also checks to see if aliases can
13 #   cross object file boundaries, as some systems don't permit them to.
14 #
15 #   Most systems permit something called a "weak alias" or "weak symbol."
16 #   These aliases permit a library to provide a stub form of a routine
17 #   defined in another library, thus allowing the first library to operate
18 #   even if the other library is not linked. This macro will check for
19 #   support of weak aliases, figure out what schemes are available, and
20 #   determine some characteristics of the weak alias support -- primarily,
21 #   whether a weak alias declared in one object file may be referenced from
22 #   another object file.
23 #
24 #   There are four known schemes of declaring weak symbols; each scheme is
25 #   checked in turn, and the first one found is prefered. Note that only one
26 #   of the mentioned preprocessor macros will be defined!
27 #
28 #   1. Function attributes
29 #
30 #   This scheme was first introduced by the GNU C compiler, and attaches
31 #   attributes to particular functions. It is among the easiest to use, and
32 #   so is the first one checked. If this scheme is detected, the
33 #   preprocessor macro HAVE_SYS_WEAK_ALIAS_ATTRIBUTE will be defined to 1.
34 #   This scheme is used as in the following code fragment:
35 #
36 #     void __weakf(int c)
37 #     {
38 #       /* Function definition... */
39 #     }
40 #
41 #     void weakf(int c) __attribute__((weak, alias("__weakf")));
42 #
43 #   2. #pragma weak
44 #
45 #   This scheme is in use by many compilers other than the GNU C compiler.
46 #   It is also particularly easy to use, and fairly portable -- well, as
47 #   portable as these things get. If this scheme is detected first, the
48 #   preprocessor macro HAVE_SYS_WEAK_ALIAS_PRAGMA will be defined to 1. This
49 #   scheme is used as in the following code fragment:
50 #
51 #     extern void weakf(int c);
52 #     #pragma weak weakf = __weakf
53 #     void __weakf(int c)
54 #     {
55 #       /* Function definition... */
56 #     }
57 #
58 #   3. #pragma _HP_SECONDARY_DEF
59 #
60 #   This scheme appears to be in use by the HP compiler. As it is rather
61 #   specialized, this is one of the last schemes checked. If it is the first
62 #   one detected, the preprocessor macro HAVE_SYS_WEAK_ALIAS_HPSECONDARY
63 #   will be defined to 1. This scheme is used as in the following code
64 #   fragment:
65 #
66 #     extern void weakf(int c);
67 #     #pragma _HP_SECONDARY_DEF __weakf weakf
68 #     void __weakf(int c)
69 #     {
70 #       /* Function definition... */
71 #     }
72 #
73 #   4. #pragma _CRI duplicate
74 #
75 #   This scheme appears to be in use by the Cray compiler. As it is rather
76 #   specialized, it too is one of the last schemes checked. If it is the
77 #   first one detected, the preprocessor macro
78 #   HAVE_SYS_WEAK_ALIAS_CRIDUPLICATE will be defined to 1. This scheme is
79 #   used as in the following code fragment:
80 #
81 #     extern void weakf(int c);
82 #     #pragma _CRI duplicate weakf as __weakf
83 #     void __weakf(int c)
84 #     {
85 #       /* Function definition... */
86 #     }
87 #
88 #   In addition to the preprocessor macros listed above, if any scheme is
89 #   found, the preprocessor macro HAVE_SYS_WEAK_ALIAS will also be defined
90 #   to 1.
91 #
92 #   Once a weak aliasing scheme has been found, a check will be performed to
93 #   see if weak aliases are honored across object file boundaries. If they
94 #   are, the HAVE_SYS_WEAK_ALIAS_CROSSFILE preprocessor macro is defined to
95 #   1.
96 #
97 #   This Autoconf macro also makes two substitutions. The first, WEAK_ALIAS,
98 #   contains the name of the scheme found (one of "attribute", "pragma",
99 #   "hpsecondary", or "criduplicate"), or "no" if no weak aliasing scheme
100 #   was found. The second, WEAK_ALIAS_CROSSFILE, is set to "yes" or "no"
101 #   depending on whether or not weak aliases may cross object file
102 #   boundaries.
103 #
104 # LICENSE
105 #
106 #   Copyright (c) 2008 Kevin L. Mitchell <klmitch@mit.edu>
107 #
108 #   Copying and distribution of this file, with or without modification, are
109 #   permitted in any medium without royalty provided the copyright notice
110 #   and this notice are preserved. This file is offered as-is, without any
111 #   warranty.
112
113 #serial 6
114
115 AU_ALIAS([KLM_SYS_WEAK_ALIAS], [AX_SYS_WEAK_ALIAS])
116 AC_DEFUN([AX_SYS_WEAK_ALIAS], [
117   # starting point: no aliasing scheme yet...
118   ax_sys_weak_alias=no
119
120   # Figure out what kind of aliasing may be supported...
121   _AX_SYS_WEAK_ALIAS_ATTRIBUTE
122   _AX_SYS_WEAK_ALIAS_PRAGMA
123   _AX_SYS_WEAK_ALIAS_HPSECONDARY
124   _AX_SYS_WEAK_ALIAS_CRIDUPLICATE
125
126   # Do we actually support aliasing?
127   AC_CACHE_CHECK([how to create weak aliases with $CC],
128                  [ax_cv_sys_weak_alias],
129  [ax_cv_sys_weak_alias=$ax_sys_weak_alias])
130
131   # OK, set a #define
132   AS_IF([test $ax_cv_sys_weak_alias != no], [
133     AC_DEFINE([HAVE_SYS_WEAK_ALIAS], 1,
134               [Define this if your system can create weak aliases])
135   ])
136
137   # Can aliases cross object file boundaries?
138   _AX_SYS_WEAK_ALIAS_CROSSFILE
139
140   # OK, remember the results
141   AC_SUBST([WEAK_ALIAS], [$ax_cv_sys_weak_alias])
142   AC_SUBST([WEAK_ALIAS_CROSSFILE], [$ax_cv_sys_weak_alias_crossfile])
143 ])
144
145 AC_DEFUN([_AX_SYS_WEAK_ALIAS_ATTRIBUTE],
146 [ # Test whether compiler accepts __attribute__ form of weak aliasing
147   AC_CACHE_CHECK([whether $CC accepts function __attribute__((weak,alias()))],
148   [ax_cv_sys_weak_alias_attribute], [
149     # We add -Werror if it's gcc to force an error exit if the weak attribute
150     # isn't understood
151     AS_IF([test $GCC = yes], [
152       save_CFLAGS=$CFLAGS
153       CFLAGS=-Werror])
154
155     # Try linking with a weak alias...
156     AC_LINK_IFELSE([
157       AC_LANG_PROGRAM([
158 void __weakf(int c) {}
159 void weakf(int c) __attribute__((weak, alias("__weakf")));],
160         [weakf(0)])],
161       [ax_cv_sys_weak_alias_attribute=yes],
162       [ax_cv_sys_weak_alias_attribute=no])
163
164     # Restore original CFLAGS
165     AS_IF([test $GCC = yes], [
166       CFLAGS=$save_CFLAGS])
167   ])
168
169   # What was the result of the test?
170   AS_IF([test $ax_cv_sys_weak_alias_attribute = yes], [
171     test $ax_sys_weak_alias = no && ax_sys_weak_alias=attribute
172     AC_DEFINE([HAVE_SYS_WEAK_ALIAS_ATTRIBUTE], 1,
173               [Define this if weak aliases may be created with __attribute__])
174   ])
175 ])
176
177 AC_DEFUN([_AX_SYS_WEAK_ALIAS_PRAGMA],
178 [ # Test whether compiler accepts #pragma form of weak aliasing
179   AC_CACHE_CHECK([whether $CC supports @%:@pragma weak],
180   [ax_cv_sys_weak_alias_pragma], [
181
182     # Try linking with a weak alias...
183     AC_LINK_IFELSE([
184       AC_LANG_PROGRAM([
185 extern void weakf(int c);
186 @%:@pragma weak weakf = __weakf
187 void __weakf(int c) {}],
188         [weakf(0)])],
189       [ax_cv_sys_weak_alias_pragma=yes],
190       [ax_cv_sys_weak_alias_pragma=no])
191   ])
192
193   # What was the result of the test?
194   AS_IF([test $ax_cv_sys_weak_alias_pragma = yes], [
195     test $ax_sys_weak_alias = no && ax_sys_weak_alias=pragma
196     AC_DEFINE([HAVE_SYS_WEAK_ALIAS_PRAGMA], 1,
197               [Define this if weak aliases may be created with @%:@pragma weak])
198   ])
199 ])
200
201 AC_DEFUN([_AX_SYS_WEAK_ALIAS_HPSECONDARY],
202 [ # Test whether compiler accepts _HP_SECONDARY_DEF pragma from HP...
203   AC_CACHE_CHECK([whether $CC supports @%:@pragma _HP_SECONDARY_DEF],
204   [ax_cv_sys_weak_alias_hpsecondary], [
205
206     # Try linking with a weak alias...
207     AC_LINK_IFELSE([
208       AC_LANG_PROGRAM([
209 extern void weakf(int c);
210 @%:@pragma _HP_SECONDARY_DEF __weakf weakf
211 void __weakf(int c) {}],
212         [weakf(0)])],
213       [ax_cv_sys_weak_alias_hpsecondary=yes],
214       [ax_cv_sys_weak_alias_hpsecondary=no])
215   ])
216
217   # What was the result of the test?
218   AS_IF([test $ax_cv_sys_weak_alias_hpsecondary = yes], [
219     test $ax_sys_weak_alias = no && ax_sys_weak_alias=hpsecondary
220     AC_DEFINE([HAVE_SYS_WEAK_ALIAS_HPSECONDARY], 1,
221               [Define this if weak aliases may be created with @%:@pragma _HP_SECONDARY_DEF])
222   ])
223 ])
224
225 AC_DEFUN([_AX_SYS_WEAK_ALIAS_CRIDUPLICATE],
226 [ # Test whether compiler accepts "_CRI duplicate" pragma from Cray
227   AC_CACHE_CHECK([whether $CC supports @%:@pragma _CRI duplicate],
228   [ax_cv_sys_weak_alias_criduplicate], [
229
230     # Try linking with a weak alias...
231     AC_LINK_IFELSE([
232       AC_LANG_PROGRAM([
233 extern void weakf(int c);
234 @%:@pragma _CRI duplicate weakf as __weakf
235 void __weakf(int c) {}],
236         [weakf(0)])],
237       [ax_cv_sys_weak_alias_criduplicate=yes],
238       [ax_cv_sys_weak_alias_criduplicate=no])
239   ])
240
241   # What was the result of the test?
242   AS_IF([test $ax_cv_sys_weak_alias_criduplicate = yes], [
243     test $ax_sys_weak_alias = no && ax_sys_weak_alias=criduplicate
244     AC_DEFINE([HAVE_SYS_WEAK_ALIAS_CRIDUPLICATE], 1,
245               [Define this if weak aliases may be created with @%:@pragma _CRI duplicate])
246   ])
247 ])
248
249 dnl Note: This macro is modeled closely on AC_LINK_IFELSE, and in fact
250 dnl depends on some implementation details of that macro, particularly
251 dnl its use of _AC_MSG_LOG_CONFTEST to log the failed test program and
252 dnl its use of ac_link for running the linker.
253 AC_DEFUN([_AX_SYS_WEAK_ALIAS_CROSSFILE],
254 [ # Check to see if weak aliases can cross object file boundaries
255   AC_CACHE_CHECK([whether $CC supports weak aliases across object file boundaries],
256   [ax_cv_sys_weak_alias_crossfile], [
257     AS_IF([test $ax_cv_sys_weak_alias = no],
258           [ax_cv_sys_weak_alias_crossfile=no], [
259 dnl Must build our own test files...
260       # conftest1 contains our weak alias definition...
261       cat >conftest1.$ac_ext <<_ACEOF
262 /* confdefs.h.  */
263 _ACEOF
264       cat confdefs.h >>conftest1.$ac_ext
265       cat >>conftest1.$ac_ext <<_ACEOF
266 /* end confdefs.h.  */
267
268 @%:@ifndef HAVE_SYS_WEAK_ALIAS_ATTRIBUTE
269 extern void weakf(int c);
270 @%:@if defined(HAVE_SYS_WEAK_ALIAS_PRAGMA)
271 @%:@pragma weak weakf = __weakf
272 @%:@elif defined(HAVE_SYS_WEAK_ALIAS_HPSECONDARY)
273 @%:@pragma _HP_SECONDARY_DEF __weakf weakf
274 @%:@elif defined(HAVE_SYS_WEAK_ALIAS_CRIDUPLICATE)
275 @%:@pragma _CRI duplicate weakf as __weakf
276 @%:@endif
277 @%:@endif
278 void __weakf(int c) {}
279 @%:@ifdef HAVE_SYS_WEAK_ALIAS_ATTRIBUTE
280 void weakf(int c) __attribute((weak, alias("__weakf")));
281 @%:@endif
282 _ACEOF
283       # And conftest2 contains our main routine that calls it
284       cat >conftest2.$ac_ext <<_ACEOF
285 /* confdefs.h.  */
286 _ACEOF
287       cat confdefs.h >> conftest2.$ac_ext
288       cat >>conftest2.$ac_ext <<_ACEOF
289 /* end confdefs.h.  */
290
291 extern void weakf(int c);
292 int
293 main ()
294 {
295   weakf(0);
296   return 0;
297 }
298 _ACEOF
299       # We must remove the object files (if any) ourselves...
300       rm -f conftest2.$ac_objext conftest$ac_exeext
301
302       # Change ac_link to compile *2* files together
303       save_aclink=$ac_link
304       ac_link=`echo "$ac_link" | \
305                sed -e 's/conftest\(\.\$ac_ext\)/conftest1\1 conftest2\1/'`
306 dnl Substitute our own routine for logging the conftest
307 m4_pushdef([_AC_MSG_LOG_CONFTEST],
308 [echo "$as_me: failed program was:" >&AS_MESSAGE_LOG_FD
309 echo ">>> conftest1.$ac_ext" >&AS_MESSAGE_LOG_FD
310 sed "s/^/| /" conftest1.$ac_ext >&AS_MESSAGE_LOG_FD
311 echo ">>> conftest2.$ac_ext" >&AS_MESSAGE_LOG_FD
312 sed "s/^/| /" conftest2.$ac_ext >&AS_MESSAGE_LOG_FD
313 ])dnl
314       # Since we created the files ourselves, don't use SOURCE argument
315       AC_LINK_IFELSE(, [ax_cv_sys_weak_alias_crossfile=yes],
316                      [ax_cv_sys_weak_alias_crossfile=no])
317 dnl Restore _AC_MSG_LOG_CONFTEST
318 m4_popdef([_AC_MSG_LOG_CONFTEST])dnl
319       # Restore ac_link
320       ac_link=$save_aclink
321
322       # We must remove the object files (if any) and C files ourselves...
323       rm -f conftest1.$ac_ext conftest2.$ac_ext \
324             conftest1.$ac_objext conftest2.$ac_objext
325     ])
326   ])
327
328   # What were the results of the test?
329   AS_IF([test $ax_cv_sys_weak_alias_crossfile = yes], [
330     AC_DEFINE([HAVE_SYS_WEAK_ALIAS_CROSSFILE], 1,
331               [Define this if weak aliases in other files are honored])
332   ])
333 ])