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