include_next.m4 8.5 KB

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