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!
28 # 1. Function attributes
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:
38 # /* Function definition... */
41 # void weakf(int c) __attribute__((weak, alias("__weakf")));
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:
51 # extern void weakf(int c);
52 # #pragma weak weakf = __weakf
55 # /* Function definition... */
58 # 3. #pragma _HP_SECONDARY_DEF
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
66 # extern void weakf(int c);
67 # #pragma _HP_SECONDARY_DEF __weakf weakf
70 # /* Function definition... */
73 # 4. #pragma _CRI duplicate
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:
81 # extern void weakf(int c);
82 # #pragma _CRI duplicate weakf as __weakf
85 # /* Function definition... */
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
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
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
106 # Copyright (c) 2008 Kevin L. Mitchell <klmitch@mit.edu>
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
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...
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
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])
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])
137 # Can aliases cross object file boundaries?
138 _AX_SYS_WEAK_ALIAS_CROSSFILE
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])
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
151 AS_IF([test $GCC = yes], [
155 # Try linking with a weak alias...
158 void __weakf(int c) {}
159 void weakf(int c) __attribute__((weak, alias("__weakf")));],
161 [ax_cv_sys_weak_alias_attribute=yes],
162 [ax_cv_sys_weak_alias_attribute=no])
164 # Restore original CFLAGS
165 AS_IF([test $GCC = yes], [
166 CFLAGS=$save_CFLAGS])
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__])
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], [
182 # Try linking with a weak alias...
185 extern void weakf(int c);
186 @%:@pragma weak weakf = __weakf
187 void __weakf(int c) {}],
189 [ax_cv_sys_weak_alias_pragma=yes],
190 [ax_cv_sys_weak_alias_pragma=no])
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])
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], [
206 # Try linking with a weak alias...
209 extern void weakf(int c);
210 @%:@pragma _HP_SECONDARY_DEF __weakf weakf
211 void __weakf(int c) {}],
213 [ax_cv_sys_weak_alias_hpsecondary=yes],
214 [ax_cv_sys_weak_alias_hpsecondary=no])
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])
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], [
230 # Try linking with a weak alias...
233 extern void weakf(int c);
234 @%:@pragma _CRI duplicate weakf as __weakf
235 void __weakf(int c) {}],
237 [ax_cv_sys_weak_alias_criduplicate=yes],
238 [ax_cv_sys_weak_alias_criduplicate=no])
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])
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
264 cat confdefs.h >>conftest1.$ac_ext
265 cat >>conftest1.$ac_ext <<_ACEOF
266 /* end confdefs.h. */
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
278 void __weakf(int c) {}
279 @%:@ifdef HAVE_SYS_WEAK_ALIAS_ATTRIBUTE
280 void weakf(int c) __attribute((weak, alias("__weakf")));
283 # And conftest2 contains our main routine that calls it
284 cat >conftest2.$ac_ext <<_ACEOF
287 cat confdefs.h >> conftest2.$ac_ext
288 cat >>conftest2.$ac_ext <<_ACEOF
289 /* end confdefs.h. */
291 extern void weakf(int c);
299 # We must remove the object files (if any) ourselves...
300 rm -f conftest2.$ac_objext conftest$ac_exeext
302 # Change ac_link to compile *2* files together
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
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
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
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])