1 # ===========================================================================
2 # http://www.gnu.org/software/autoconf-archive/ax_sys_weak_alias.html
3 # ===========================================================================
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.
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.
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!
30 # This scheme is in use by many compilers other than the GNU C compiler.
31 # It is also particularly easy to use, and fairly portable -- well, as
32 # portable as these things get. If this scheme is detected first, the
33 # preprocessor macro HAVE_SYS_WEAK_ALIAS_PRAGMA will be defined to 1. This
34 # scheme is used as in the following code fragment:
36 # extern void weakf(int c);
37 # #pragma weak weakf = __weakf
40 # /* Function definition... */
43 # 2. #pragma _HP_SECONDARY_DEF
45 # This scheme appears to be in use by the HP compiler. As it is rather
46 # specialized, this is one of the last schemes checked. If it is the first
47 # one detected, the preprocessor macro HAVE_SYS_WEAK_ALIAS_HPSECONDARY
48 # will be defined to 1. This scheme is used as in the following code
51 # extern void weakf(int c);
52 # #pragma _HP_SECONDARY_DEF __weakf weakf
55 # /* Function definition... */
58 # 3. #pragma _CRI duplicate
60 # This scheme appears to be in use by the Cray compiler. As it is rather
61 # specialized, it too is one of the last schemes checked. If it is the
62 # first one detected, the preprocessor macro
63 # HAVE_SYS_WEAK_ALIAS_CRIDUPLICATE will be defined to 1. This scheme is
64 # used as in the following code fragment:
66 # extern void weakf(int c);
67 # #pragma _CRI duplicate weakf as __weakf
70 # /* Function definition... */
73 # 4. Function attributes
75 # This scheme was first introduced by the GNU C compiler, and attaches
76 # attributes to particular functions. However, since some compilers
77 # simply ignore unsupported attributes, this scheme is tried last.
78 # If this scheme is detected, the preprocessor macro
79 # HAVE_SYS_WEAK_ALIAS_ATTRIBUTE will be defined to 1.
80 # This scheme is used as in the following code fragment:
84 # /* Function definition... */
87 # void weakf(int c) __attribute__((weak, alias("__weakf")));
89 # In addition to the preprocessor macros listed above, if any scheme is
90 # found, the preprocessor macro HAVE_SYS_WEAK_ALIAS will also be defined
93 # Once a weak aliasing scheme has been found, a check will be performed to
94 # see if weak aliases are honored across object file boundaries. If they
95 # are, the HAVE_SYS_WEAK_ALIAS_CROSSFILE preprocessor macro is defined to
98 # This Autoconf macro also makes two substitutions. The first, WEAK_ALIAS,
99 # contains the name of the scheme found (one of "attribute", "pragma",
100 # "hpsecondary", or "criduplicate"), or "no" if no weak aliasing scheme
101 # was found. The second, WEAK_ALIAS_CROSSFILE, is set to "yes" or "no"
102 # depending on whether or not weak aliases may cross object file
107 # Copyright (c) 2008 Kevin L. Mitchell <klmitch@mit.edu>
109 # Copying and distribution of this file, with or without modification, are
110 # permitted in any medium without royalty provided the copyright notice
111 # and this notice are preserved. This file is offered as-is, without any
116 AU_ALIAS([KLM_SYS_WEAK_ALIAS], [AX_SYS_WEAK_ALIAS])
117 AC_DEFUN([AX_SYS_WEAK_ALIAS], [
118 # starting point: no aliasing scheme yet...
121 # Figure out what kind of aliasing may be supported...
122 _AX_SYS_WEAK_ALIAS_PRAGMA
123 _AX_SYS_WEAK_ALIAS_HPSECONDARY
124 _AX_SYS_WEAK_ALIAS_CRIDUPLICATE
125 _AX_SYS_WEAK_ALIAS_ATTRIBUTE
127 # Do we actually support aliasing?
128 AC_CACHE_CHECK([how to create weak aliases with $CC],
129 [ax_cv_sys_weak_alias],
130 [ax_cv_sys_weak_alias=$ax_sys_weak_alias])
133 AS_IF([test $ax_cv_sys_weak_alias != no], [
134 AC_DEFINE([HAVE_SYS_WEAK_ALIAS], 1,
135 [Define this if your system can create weak aliases])
138 # Can aliases cross object file boundaries?
139 _AX_SYS_WEAK_ALIAS_CROSSFILE
141 # OK, remember the results
142 AC_SUBST([WEAK_ALIAS], [$ax_cv_sys_weak_alias])
143 AC_SUBST([WEAK_ALIAS_CROSSFILE], [$ax_cv_sys_weak_alias_crossfile])
146 AC_DEFUN([_AX_SYS_WEAK_ALIAS_ATTRIBUTE],
147 [ # Test whether compiler accepts __attribute__ form of weak aliasing
148 AC_CACHE_CHECK([whether $CC accepts function __attribute__((weak,alias()))],
149 [ax_cv_sys_weak_alias_attribute], [
150 # We add -Werror if it's gcc to force an error exit if the weak attribute
152 AS_IF([test $GCC = yes], [
156 # Try linking with a weak alias...
159 void __weakf(int c) {}
160 void weakf(int c) __attribute__((weak, alias("__weakf")));],
162 [ax_cv_sys_weak_alias_attribute=yes],
163 [ax_cv_sys_weak_alias_attribute=no])
165 # Restore original CFLAGS
166 AS_IF([test $GCC = yes], [
167 CFLAGS=$save_CFLAGS])
170 # What was the result of the test?
171 AS_IF([test $ax_sys_weak_alias = no &&
172 test $ax_cv_sys_weak_alias_attribute = yes], [
173 ax_sys_weak_alias=attribute
174 AC_DEFINE([HAVE_SYS_WEAK_ALIAS_ATTRIBUTE], 1,
175 [Define this if weak aliases may be created with __attribute__])
179 AC_DEFUN([_AX_SYS_WEAK_ALIAS_PRAGMA],
180 [ # Test whether compiler accepts #pragma form of weak aliasing
181 AC_CACHE_CHECK([whether $CC supports @%:@pragma weak],
182 [ax_cv_sys_weak_alias_pragma], [
184 # Try linking with a weak alias...
187 extern void weakf(int c);
188 @%:@pragma weak weakf = __weakf
189 void __weakf(int c) {}],
191 [ax_cv_sys_weak_alias_pragma=yes],
192 [ax_cv_sys_weak_alias_pragma=no])
195 # What was the result of the test?
196 AS_IF([test $ax_sys_weak_alias = no &&
197 test $ax_cv_sys_weak_alias_pragma = yes], [
198 ax_sys_weak_alias=pragma
199 AC_DEFINE([HAVE_SYS_WEAK_ALIAS_PRAGMA], 1,
200 [Define this if weak aliases may be created with @%:@pragma weak])
204 AC_DEFUN([_AX_SYS_WEAK_ALIAS_HPSECONDARY],
205 [ # Test whether compiler accepts _HP_SECONDARY_DEF pragma from HP...
206 AC_CACHE_CHECK([whether $CC supports @%:@pragma _HP_SECONDARY_DEF],
207 [ax_cv_sys_weak_alias_hpsecondary], [
209 # Try linking with a weak alias...
212 extern void weakf(int c);
213 @%:@pragma _HP_SECONDARY_DEF __weakf weakf
214 void __weakf(int c) {}],
216 [ax_cv_sys_weak_alias_hpsecondary=yes],
217 [ax_cv_sys_weak_alias_hpsecondary=no])
220 # What was the result of the test?
221 AS_IF([test $ax_sys_weak_alias = no &&
222 test $ax_cv_sys_weak_alias_hpsecondary = yes], [
223 ax_sys_weak_alias=hpsecondary
224 AC_DEFINE([HAVE_SYS_WEAK_ALIAS_HPSECONDARY], 1,
225 [Define this if weak aliases may be created with @%:@pragma _HP_SECONDARY_DEF])
229 AC_DEFUN([_AX_SYS_WEAK_ALIAS_CRIDUPLICATE],
230 [ # Test whether compiler accepts "_CRI duplicate" pragma from Cray
231 AC_CACHE_CHECK([whether $CC supports @%:@pragma _CRI duplicate],
232 [ax_cv_sys_weak_alias_criduplicate], [
234 # Try linking with a weak alias...
237 extern void weakf(int c);
238 @%:@pragma _CRI duplicate weakf as __weakf
239 void __weakf(int c) {}],
241 [ax_cv_sys_weak_alias_criduplicate=yes],
242 [ax_cv_sys_weak_alias_criduplicate=no])
245 # What was the result of the test?
246 AS_IF([test $ax_sys_weak_alias = no &&
247 test $ax_cv_sys_weak_alias_criduplicate = yes], [
248 ax_sys_weak_alias=criduplicate
249 AC_DEFINE([HAVE_SYS_WEAK_ALIAS_CRIDUPLICATE], 1,
250 [Define this if weak aliases may be created with @%:@pragma _CRI duplicate])
254 dnl Note: This macro is modeled closely on AC_LINK_IFELSE, and in fact
255 dnl depends on some implementation details of that macro, particularly
256 dnl its use of _AC_MSG_LOG_CONFTEST to log the failed test program and
257 dnl its use of ac_link for running the linker.
258 AC_DEFUN([_AX_SYS_WEAK_ALIAS_CROSSFILE],
259 [ # Check to see if weak aliases can cross object file boundaries
260 AC_CACHE_CHECK([whether $CC supports weak aliases across object file boundaries],
261 [ax_cv_sys_weak_alias_crossfile], [
262 AS_IF([test $ax_cv_sys_weak_alias = no],
263 [ax_cv_sys_weak_alias_crossfile=no], [
264 dnl Must build our own test files...
265 # conftest1 contains our weak alias definition...
266 cat >conftest1.$ac_ext <<_ACEOF
269 cat confdefs.h >>conftest1.$ac_ext
270 cat >>conftest1.$ac_ext <<_ACEOF
271 /* end confdefs.h. */
273 @%:@ifndef HAVE_SYS_WEAK_ALIAS_ATTRIBUTE
274 extern void weakf(int c);
276 @%:@if defined(HAVE_SYS_WEAK_ALIAS_PRAGMA)
277 @%:@pragma weak weakf = __weakf
278 @%:@elif defined(HAVE_SYS_WEAK_ALIAS_HPSECONDARY)
279 @%:@pragma _HP_SECONDARY_DEF __weakf weakf
280 @%:@elif defined(HAVE_SYS_WEAK_ALIAS_CRIDUPLICATE)
281 @%:@pragma _CRI duplicate weakf as __weakf
283 void __weakf(int c) {}
284 @%:@ifdef HAVE_SYS_WEAK_ALIAS_ATTRIBUTE
285 void weakf(int c) __attribute((weak, alias("__weakf")));
288 # And conftest2 contains our main routine that calls it
289 cat >conftest2.$ac_ext <<_ACEOF
292 cat confdefs.h >> conftest2.$ac_ext
293 cat >>conftest2.$ac_ext <<_ACEOF
294 /* end confdefs.h. */
296 extern void weakf(int c);
304 # We must remove the object files (if any) ourselves...
305 rm -f conftest2.$ac_objext conftest$ac_exeext
307 # Change ac_link to compile *2* files together
309 ac_link=`echo "$ac_link" | \
310 sed -e 's/conftest\(\.\$ac_ext\)/conftest1\1 conftest2\1/'`
311 dnl Substitute our own routine for logging the conftest
312 m4_pushdef([_AC_MSG_LOG_CONFTEST],
313 [echo "$as_me: failed program was:" >&AS_MESSAGE_LOG_FD
314 echo ">>> conftest1.$ac_ext" >&AS_MESSAGE_LOG_FD
315 sed "s/^/| /" conftest1.$ac_ext >&AS_MESSAGE_LOG_FD
316 echo ">>> conftest2.$ac_ext" >&AS_MESSAGE_LOG_FD
317 sed "s/^/| /" conftest2.$ac_ext >&AS_MESSAGE_LOG_FD
319 # Since we created the files ourselves, don't use SOURCE argument
320 AC_LINK_IFELSE(, [ax_cv_sys_weak_alias_crossfile=yes],
321 [ax_cv_sys_weak_alias_crossfile=no])
322 dnl Restore _AC_MSG_LOG_CONFTEST
323 m4_popdef([_AC_MSG_LOG_CONFTEST])dnl
327 # We must remove the object files (if any) and C files ourselves...
328 rm -f conftest1.$ac_ext conftest2.$ac_ext \
329 conftest1.$ac_objext conftest2.$ac_objext
333 # What were the results of the test?
334 AS_IF([test $ax_cv_sys_weak_alias_crossfile = yes], [
335 AC_DEFINE([HAVE_SYS_WEAK_ALIAS_CROSSFILE], 1,
336 [Define this if weak aliases in other files are honored])