include_next.m4 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # include_next.m4 serial 23
  2. dnl Copyright (C) 2006-2017 Free Software Foundation, Inc.
  3. dnl This file is free software; the Free Software Foundation
  4. dnl gives unlimited permission to copy and/or distribute it,
  5. dnl with or without modifications, as long as this notice is preserved.
  6. dnl From Paul Eggert and Derek Price.
  7. dnl Sets INCLUDE_NEXT and PRAGMA_SYSTEM_HEADER.
  8. dnl
  9. dnl INCLUDE_NEXT expands to 'include_next' if the compiler supports it, or to
  10. dnl 'include' otherwise.
  11. dnl
  12. dnl INCLUDE_NEXT_AS_FIRST_DIRECTIVE expands to 'include_next' if the compiler
  13. dnl supports it in the special case that it is the first include directive in
  14. dnl the given file, or to 'include' otherwise.
  15. dnl
  16. dnl PRAGMA_SYSTEM_HEADER can be used in files that contain #include_next,
  17. dnl so as to avoid GCC warnings when the gcc option -pedantic is used.
  18. dnl '#pragma GCC system_header' has the same effect as if the file was found
  19. dnl through the include search path specified with '-isystem' options (as
  20. dnl opposed to the search path specified with '-I' options). Namely, gcc
  21. dnl does not warn about some things, and on some systems (Solaris and Interix)
  22. dnl __STDC__ evaluates to 0 instead of to 1. The latter is an undesired side
  23. dnl effect; we are therefore careful to use 'defined __STDC__' or '1' instead
  24. dnl of plain '__STDC__'.
  25. dnl
  26. dnl PRAGMA_COLUMNS can be used in files that override system header files, so
  27. dnl as to avoid compilation errors on HP NonStop systems when the gnulib file
  28. dnl is included by a system header file that does a "#pragma COLUMNS 80" (which
  29. dnl has the effect of truncating the lines of that file and all files that it
  30. dnl includes to 80 columns) and the gnulib file has lines longer than 80
  31. dnl columns.
  32. AC_DEFUN([gl_INCLUDE_NEXT],
  33. [
  34. AC_LANG_PREPROC_REQUIRE()
  35. AC_CACHE_CHECK([whether the preprocessor supports include_next],
  36. [gl_cv_have_include_next],
  37. [rm -rf conftestd1a conftestd1b conftestd2
  38. mkdir conftestd1a conftestd1b conftestd2
  39. dnl IBM C 9.0, 10.1 (original versions, prior to the 2009-01 updates) on
  40. dnl AIX 6.1 support include_next when used as first preprocessor directive
  41. dnl in a file, but not when preceded by another include directive. Check
  42. dnl for this bug by including <stdio.h>.
  43. dnl Additionally, with this same compiler, include_next is a no-op when
  44. dnl used in a header file that was included by specifying its absolute
  45. dnl file name. Despite these two bugs, include_next is used in the
  46. dnl compiler's <math.h>. By virtue of the second bug, we need to use
  47. dnl include_next as well in this case.
  48. cat <<EOF > conftestd1a/conftest.h
  49. #define DEFINED_IN_CONFTESTD1
  50. #include_next <conftest.h>
  51. #ifdef DEFINED_IN_CONFTESTD2
  52. int foo;
  53. #else
  54. #error "include_next doesn't work"
  55. #endif
  56. EOF
  57. cat <<EOF > conftestd1b/conftest.h
  58. #define DEFINED_IN_CONFTESTD1
  59. #include <stdio.h>
  60. #include_next <conftest.h>
  61. #ifdef DEFINED_IN_CONFTESTD2
  62. int foo;
  63. #else
  64. #error "include_next doesn't work"
  65. #endif
  66. EOF
  67. cat <<EOF > conftestd2/conftest.h
  68. #ifndef DEFINED_IN_CONFTESTD1
  69. #error "include_next test doesn't work"
  70. #endif
  71. #define DEFINED_IN_CONFTESTD2
  72. EOF
  73. gl_save_CPPFLAGS="$CPPFLAGS"
  74. CPPFLAGS="$gl_save_CPPFLAGS -Iconftestd1b -Iconftestd2"
  75. dnl We intentionally avoid using AC_LANG_SOURCE here.
  76. AC_COMPILE_IFELSE([AC_LANG_DEFINES_PROVIDED[#include <conftest.h>]],
  77. [gl_cv_have_include_next=yes],
  78. [CPPFLAGS="$gl_save_CPPFLAGS -Iconftestd1a -Iconftestd2"
  79. AC_COMPILE_IFELSE([AC_LANG_DEFINES_PROVIDED[#include <conftest.h>]],
  80. [gl_cv_have_include_next=buggy],
  81. [gl_cv_have_include_next=no])
  82. ])
  83. CPPFLAGS="$gl_save_CPPFLAGS"
  84. rm -rf conftestd1a conftestd1b conftestd2
  85. ])
  86. PRAGMA_SYSTEM_HEADER=
  87. if test $gl_cv_have_include_next = yes; then
  88. INCLUDE_NEXT=include_next
  89. INCLUDE_NEXT_AS_FIRST_DIRECTIVE=include_next
  90. if test -n "$GCC"; then
  91. PRAGMA_SYSTEM_HEADER='#pragma GCC system_header'
  92. fi
  93. else
  94. if test $gl_cv_have_include_next = buggy; then
  95. INCLUDE_NEXT=include
  96. INCLUDE_NEXT_AS_FIRST_DIRECTIVE=include_next
  97. else
  98. INCLUDE_NEXT=include
  99. INCLUDE_NEXT_AS_FIRST_DIRECTIVE=include
  100. fi
  101. fi
  102. AC_SUBST([INCLUDE_NEXT])
  103. AC_SUBST([INCLUDE_NEXT_AS_FIRST_DIRECTIVE])
  104. AC_SUBST([PRAGMA_SYSTEM_HEADER])
  105. AC_CACHE_CHECK([whether system header files limit the line length],
  106. [gl_cv_pragma_columns],
  107. [dnl HP NonStop systems, which define __TANDEM, have this misfeature.
  108. AC_EGREP_CPP([choke me],
  109. [
  110. #ifdef __TANDEM
  111. choke me
  112. #endif
  113. ],
  114. [gl_cv_pragma_columns=yes],
  115. [gl_cv_pragma_columns=no])
  116. ])
  117. if test $gl_cv_pragma_columns = yes; then
  118. PRAGMA_COLUMNS="#pragma COLUMNS 10000"
  119. else
  120. PRAGMA_COLUMNS=
  121. fi
  122. AC_SUBST([PRAGMA_COLUMNS])
  123. ])
  124. # gl_CHECK_NEXT_HEADERS(HEADER1 HEADER2 ...)
  125. # ------------------------------------------
  126. # For each arg foo.h, if #include_next works, define NEXT_FOO_H to be
  127. # '<foo.h>'; otherwise define it to be
  128. # '"///usr/include/foo.h"', or whatever other absolute file name is suitable.
  129. # Also, if #include_next works as first preprocessing directive in a file,
  130. # define NEXT_AS_FIRST_DIRECTIVE_FOO_H to be '<foo.h>'; otherwise define it to
  131. # be
  132. # '"///usr/include/foo.h"', or whatever other absolute file name is suitable.
  133. # That way, a header file with the following line:
  134. # #@INCLUDE_NEXT@ @NEXT_FOO_H@
  135. # or
  136. # #@INCLUDE_NEXT_AS_FIRST_DIRECTIVE@ @NEXT_AS_FIRST_DIRECTIVE_FOO_H@
  137. # behaves (after sed substitution) as if it contained
  138. # #include_next <foo.h>
  139. # even if the compiler does not support include_next.
  140. # The three "///" are to pacify Sun C 5.8, which otherwise would say
  141. # "warning: #include of /usr/include/... may be non-portable".
  142. # Use '""', not '<>', so that the /// cannot be confused with a C99 comment.
  143. # Note: This macro assumes that the header file is not empty after
  144. # preprocessing, i.e. it does not only define preprocessor macros but also
  145. # provides some type/enum definitions or function/variable declarations.
  146. #
  147. # This macro also checks whether each header exists, by invoking
  148. # AC_CHECK_HEADERS_ONCE or AC_CHECK_HEADERS on each argument.
  149. AC_DEFUN([gl_CHECK_NEXT_HEADERS],
  150. [
  151. gl_NEXT_HEADERS_INTERNAL([$1], [check])
  152. ])
  153. # gl_NEXT_HEADERS(HEADER1 HEADER2 ...)
  154. # ------------------------------------
  155. # Like gl_CHECK_NEXT_HEADERS, except do not check whether the headers exist.
  156. # This is suitable for headers like <stddef.h> that are standardized by C89
  157. # and therefore can be assumed to exist.
  158. AC_DEFUN([gl_NEXT_HEADERS],
  159. [
  160. gl_NEXT_HEADERS_INTERNAL([$1], [assume])
  161. ])
  162. # The guts of gl_CHECK_NEXT_HEADERS and gl_NEXT_HEADERS.
  163. AC_DEFUN([gl_NEXT_HEADERS_INTERNAL],
  164. [
  165. AC_REQUIRE([gl_INCLUDE_NEXT])
  166. AC_REQUIRE([AC_CANONICAL_HOST])
  167. m4_if([$2], [check],
  168. [AC_CHECK_HEADERS_ONCE([$1])
  169. ])
  170. dnl FIXME: gl_next_header and gl_header_exists must be used unquoted
  171. dnl until we can assume autoconf 2.64 or newer.
  172. m4_foreach_w([gl_HEADER_NAME], [$1],
  173. [AS_VAR_PUSHDEF([gl_next_header],
  174. [gl_cv_next_]m4_defn([gl_HEADER_NAME]))
  175. if test $gl_cv_have_include_next = yes; then
  176. AS_VAR_SET(gl_next_header, ['<'gl_HEADER_NAME'>'])
  177. else
  178. AC_CACHE_CHECK(
  179. [absolute name of <]m4_defn([gl_HEADER_NAME])[>],
  180. m4_defn([gl_next_header]),
  181. [m4_if([$2], [check],
  182. [AS_VAR_PUSHDEF([gl_header_exists],
  183. [ac_cv_header_]m4_defn([gl_HEADER_NAME]))
  184. if test AS_VAR_GET(gl_header_exists) = yes; then
  185. AS_VAR_POPDEF([gl_header_exists])
  186. ])
  187. gl_ABSOLUTE_HEADER_ONE(gl_HEADER_NAME)
  188. AS_VAR_COPY([gl_header], [gl_cv_absolute_]AS_TR_SH(gl_HEADER_NAME))
  189. AS_VAR_SET(gl_next_header, ['"'$gl_header'"'])
  190. m4_if([$2], [check],
  191. [else
  192. AS_VAR_SET(gl_next_header, ['<'gl_HEADER_NAME'>'])
  193. fi
  194. ])
  195. ])
  196. fi
  197. AC_SUBST(
  198. AS_TR_CPP([NEXT_]m4_defn([gl_HEADER_NAME])),
  199. [AS_VAR_GET(gl_next_header)])
  200. if test $gl_cv_have_include_next = yes || test $gl_cv_have_include_next = buggy; then
  201. # INCLUDE_NEXT_AS_FIRST_DIRECTIVE='include_next'
  202. gl_next_as_first_directive='<'gl_HEADER_NAME'>'
  203. else
  204. # INCLUDE_NEXT_AS_FIRST_DIRECTIVE='include'
  205. gl_next_as_first_directive=AS_VAR_GET(gl_next_header)
  206. fi
  207. AC_SUBST(
  208. AS_TR_CPP([NEXT_AS_FIRST_DIRECTIVE_]m4_defn([gl_HEADER_NAME])),
  209. [$gl_next_as_first_directive])
  210. AS_VAR_POPDEF([gl_next_header])])
  211. ])
  212. # Autoconf 2.68 added warnings for our use of AC_COMPILE_IFELSE;
  213. # this fallback is safe for all earlier autoconf versions.
  214. m4_define_default([AC_LANG_DEFINES_PROVIDED])