ax_sys_weak_alias.m4 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #serial 6
  113. AU_ALIAS([KLM_SYS_WEAK_ALIAS], [AX_SYS_WEAK_ALIAS])
  114. AC_DEFUN([AX_SYS_WEAK_ALIAS], [
  115. # starting point: no aliasing scheme yet...
  116. ax_sys_weak_alias=no
  117. # Figure out what kind of aliasing may be supported...
  118. _AX_SYS_WEAK_ALIAS_ATTRIBUTE
  119. _AX_SYS_WEAK_ALIAS_PRAGMA
  120. _AX_SYS_WEAK_ALIAS_HPSECONDARY
  121. _AX_SYS_WEAK_ALIAS_CRIDUPLICATE
  122. # Do we actually support aliasing?
  123. AC_CACHE_CHECK([how to create weak aliases with $CC],
  124. [ax_cv_sys_weak_alias],
  125. [ax_cv_sys_weak_alias=$ax_sys_weak_alias])
  126. # OK, set a #define
  127. AS_IF([test $ax_cv_sys_weak_alias != no], [
  128. AC_DEFINE([HAVE_SYS_WEAK_ALIAS], 1,
  129. [Define this if your system can create weak aliases])
  130. ])
  131. # Can aliases cross object file boundaries?
  132. _AX_SYS_WEAK_ALIAS_CROSSFILE
  133. # OK, remember the results
  134. AC_SUBST([WEAK_ALIAS], [$ax_cv_sys_weak_alias])
  135. AC_SUBST([WEAK_ALIAS_CROSSFILE], [$ax_cv_sys_weak_alias_crossfile])
  136. ])
  137. AC_DEFUN([_AX_SYS_WEAK_ALIAS_ATTRIBUTE],
  138. [ # Test whether compiler accepts __attribute__ form of weak aliasing
  139. AC_CACHE_CHECK([whether $CC accepts function __attribute__((weak,alias()))],
  140. [ax_cv_sys_weak_alias_attribute], [
  141. # We add -Werror if it's gcc to force an error exit if the weak attribute
  142. # isn't understood
  143. AS_IF([test $GCC = yes], [
  144. save_CFLAGS=$CFLAGS
  145. CFLAGS=-Werror])
  146. # Try linking with a weak alias...
  147. AC_LINK_IFELSE([
  148. AC_LANG_PROGRAM([
  149. void __weakf(int c) {}
  150. void weakf(int c) __attribute__((weak, alias("__weakf")));],
  151. [weakf(0)])],
  152. [ax_cv_sys_weak_alias_attribute=yes],
  153. [ax_cv_sys_weak_alias_attribute=no])
  154. # Restore original CFLAGS
  155. AS_IF([test $GCC = yes], [
  156. CFLAGS=$save_CFLAGS])
  157. ])
  158. # What was the result of the test?
  159. AS_IF([test $ax_cv_sys_weak_alias_attribute = yes], [
  160. test $ax_sys_weak_alias = no && ax_sys_weak_alias=attribute
  161. AC_DEFINE([HAVE_SYS_WEAK_ALIAS_ATTRIBUTE], 1,
  162. [Define this if weak aliases may be created with __attribute__])
  163. ])
  164. ])
  165. AC_DEFUN([_AX_SYS_WEAK_ALIAS_PRAGMA],
  166. [ # Test whether compiler accepts #pragma form of weak aliasing
  167. AC_CACHE_CHECK([whether $CC supports @%:@pragma weak],
  168. [ax_cv_sys_weak_alias_pragma], [
  169. # Try linking with a weak alias...
  170. AC_LINK_IFELSE([
  171. AC_LANG_PROGRAM([
  172. extern void weakf(int c);
  173. @%:@pragma weak weakf = __weakf
  174. void __weakf(int c) {}],
  175. [weakf(0)])],
  176. [ax_cv_sys_weak_alias_pragma=yes],
  177. [ax_cv_sys_weak_alias_pragma=no])
  178. ])
  179. # What was the result of the test?
  180. AS_IF([test $ax_cv_sys_weak_alias_pragma = yes], [
  181. test $ax_sys_weak_alias = no && ax_sys_weak_alias=pragma
  182. AC_DEFINE([HAVE_SYS_WEAK_ALIAS_PRAGMA], 1,
  183. [Define this if weak aliases may be created with @%:@pragma weak])
  184. ])
  185. ])
  186. AC_DEFUN([_AX_SYS_WEAK_ALIAS_HPSECONDARY],
  187. [ # Test whether compiler accepts _HP_SECONDARY_DEF pragma from HP...
  188. AC_CACHE_CHECK([whether $CC supports @%:@pragma _HP_SECONDARY_DEF],
  189. [ax_cv_sys_weak_alias_hpsecondary], [
  190. # Try linking with a weak alias...
  191. AC_LINK_IFELSE([
  192. AC_LANG_PROGRAM([
  193. extern void weakf(int c);
  194. @%:@pragma _HP_SECONDARY_DEF __weakf weakf
  195. void __weakf(int c) {}],
  196. [weakf(0)])],
  197. [ax_cv_sys_weak_alias_hpsecondary=yes],
  198. [ax_cv_sys_weak_alias_hpsecondary=no])
  199. ])
  200. # What was the result of the test?
  201. AS_IF([test $ax_cv_sys_weak_alias_hpsecondary = yes], [
  202. test $ax_sys_weak_alias = no && ax_sys_weak_alias=hpsecondary
  203. AC_DEFINE([HAVE_SYS_WEAK_ALIAS_HPSECONDARY], 1,
  204. [Define this if weak aliases may be created with @%:@pragma _HP_SECONDARY_DEF])
  205. ])
  206. ])
  207. AC_DEFUN([_AX_SYS_WEAK_ALIAS_CRIDUPLICATE],
  208. [ # Test whether compiler accepts "_CRI duplicate" pragma from Cray
  209. AC_CACHE_CHECK([whether $CC supports @%:@pragma _CRI duplicate],
  210. [ax_cv_sys_weak_alias_criduplicate], [
  211. # Try linking with a weak alias...
  212. AC_LINK_IFELSE([
  213. AC_LANG_PROGRAM([
  214. extern void weakf(int c);
  215. @%:@pragma _CRI duplicate weakf as __weakf
  216. void __weakf(int c) {}],
  217. [weakf(0)])],
  218. [ax_cv_sys_weak_alias_criduplicate=yes],
  219. [ax_cv_sys_weak_alias_criduplicate=no])
  220. ])
  221. # What was the result of the test?
  222. AS_IF([test $ax_cv_sys_weak_alias_criduplicate = yes], [
  223. test $ax_sys_weak_alias = no && ax_sys_weak_alias=criduplicate
  224. AC_DEFINE([HAVE_SYS_WEAK_ALIAS_CRIDUPLICATE], 1,
  225. [Define this if weak aliases may be created with @%:@pragma _CRI duplicate])
  226. ])
  227. ])
  228. dnl Note: This macro is modeled closely on AC_LINK_IFELSE, and in fact
  229. dnl depends on some implementation details of that macro, particularly
  230. dnl its use of _AC_MSG_LOG_CONFTEST to log the failed test program and
  231. dnl its use of ac_link for running the linker.
  232. AC_DEFUN([_AX_SYS_WEAK_ALIAS_CROSSFILE],
  233. [ # Check to see if weak aliases can cross object file boundaries
  234. AC_CACHE_CHECK([whether $CC supports weak aliases across object file boundaries],
  235. [ax_cv_sys_weak_alias_crossfile], [
  236. AS_IF([test $ax_cv_sys_weak_alias = no],
  237. [ax_cv_sys_weak_alias_crossfile=no], [
  238. dnl Must build our own test files...
  239. # conftest1 contains our weak alias definition...
  240. cat >conftest1.$ac_ext <<_ACEOF
  241. /* confdefs.h. */
  242. _ACEOF
  243. cat confdefs.h >>conftest1.$ac_ext
  244. cat >>conftest1.$ac_ext <<_ACEOF
  245. /* end confdefs.h. */
  246. @%:@ifndef HAVE_SYS_WEAK_ALIAS_ATTRIBUTE
  247. extern void weakf(int c);
  248. @%:@if defined(HAVE_SYS_WEAK_ALIAS_PRAGMA)
  249. @%:@pragma weak weakf = __weakf
  250. @%:@elif defined(HAVE_SYS_WEAK_ALIAS_HPSECONDARY)
  251. @%:@pragma _HP_SECONDARY_DEF __weakf weakf
  252. @%:@elif defined(HAVE_SYS_WEAK_ALIAS_CRIDUPLICATE)
  253. @%:@pragma _CRI duplicate weakf as __weakf
  254. @%:@endif
  255. @%:@endif
  256. void __weakf(int c) {}
  257. @%:@ifdef HAVE_SYS_WEAK_ALIAS_ATTRIBUTE
  258. void weakf(int c) __attribute((weak, alias("__weakf")));
  259. @%:@endif
  260. _ACEOF
  261. # And conftest2 contains our main routine that calls it
  262. cat >conftest2.$ac_ext <<_ACEOF
  263. /* confdefs.h. */
  264. _ACEOF
  265. cat confdefs.h >> conftest2.$ac_ext
  266. cat >>conftest2.$ac_ext <<_ACEOF
  267. /* end confdefs.h. */
  268. extern void weakf(int c);
  269. int
  270. main ()
  271. {
  272. weakf(0);
  273. return 0;
  274. }
  275. _ACEOF
  276. # We must remove the object files (if any) ourselves...
  277. rm -f conftest2.$ac_objext conftest$ac_exeext
  278. # Change ac_link to compile *2* files together
  279. save_aclink=$ac_link
  280. ac_link=`echo "$ac_link" | \
  281. sed -e 's/conftest\(\.\$ac_ext\)/conftest1\1 conftest2\1/'`
  282. dnl Substitute our own routine for logging the conftest
  283. m4_pushdef([_AC_MSG_LOG_CONFTEST],
  284. [echo "$as_me: failed program was:" >&AS_MESSAGE_LOG_FD
  285. echo ">>> conftest1.$ac_ext" >&AS_MESSAGE_LOG_FD
  286. sed "s/^/| /" conftest1.$ac_ext >&AS_MESSAGE_LOG_FD
  287. echo ">>> conftest2.$ac_ext" >&AS_MESSAGE_LOG_FD
  288. sed "s/^/| /" conftest2.$ac_ext >&AS_MESSAGE_LOG_FD
  289. ])dnl
  290. # Since we created the files ourselves, don't use SOURCE argument
  291. AC_LINK_IFELSE(, [ax_cv_sys_weak_alias_crossfile=yes],
  292. [ax_cv_sys_weak_alias_crossfile=no])
  293. dnl Restore _AC_MSG_LOG_CONFTEST
  294. m4_popdef([_AC_MSG_LOG_CONFTEST])dnl
  295. # Restore ac_link
  296. ac_link=$save_aclink
  297. # We must remove the object files (if any) and C files ourselves...
  298. rm -f conftest1.$ac_ext conftest2.$ac_ext \
  299. conftest1.$ac_objext conftest2.$ac_objext
  300. ])
  301. ])
  302. # What were the results of the test?
  303. AS_IF([test $ax_cv_sys_weak_alias_crossfile = yes], [
  304. AC_DEFINE([HAVE_SYS_WEAK_ALIAS_CROSSFILE], 1,
  305. [Define this if weak aliases in other files are honored])
  306. ])
  307. ])