include_next.m4 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # include_next.m4 serial 14
  2. dnl Copyright (C) 2006-2009 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. AC_DEFUN([gl_INCLUDE_NEXT],
  26. [
  27. AC_LANG_PREPROC_REQUIRE()
  28. AC_CACHE_CHECK([whether the preprocessor supports include_next],
  29. [gl_cv_have_include_next],
  30. [rm -rf conftestd1a conftestd1b conftestd2
  31. mkdir conftestd1a conftestd1b conftestd2
  32. dnl IBM C 9.0, 10.1 (original versions, prior to the 2009-01 updates) on
  33. dnl AIX 6.1 support include_next when used as first preprocessor directive
  34. dnl in a file, but not when preceded by another include directive. Check
  35. dnl for this bug by including <stdio.h>.
  36. dnl Additionally, with this same compiler, include_next is a no-op when
  37. dnl used in a header file that was included by specifying its absolute
  38. dnl file name. Despite these two bugs, include_next is used in the
  39. dnl compiler's <math.h>. By virtue of the second bug, we need to use
  40. dnl include_next as well in this case.
  41. cat <<EOF > conftestd1a/conftest.h
  42. #define DEFINED_IN_CONFTESTD1
  43. #include_next <conftest.h>
  44. #ifdef DEFINED_IN_CONFTESTD2
  45. int foo;
  46. #else
  47. #error "include_next doesn't work"
  48. #endif
  49. EOF
  50. cat <<EOF > conftestd1b/conftest.h
  51. #define DEFINED_IN_CONFTESTD1
  52. #include <stdio.h>
  53. #include_next <conftest.h>
  54. #ifdef DEFINED_IN_CONFTESTD2
  55. int foo;
  56. #else
  57. #error "include_next doesn't work"
  58. #endif
  59. EOF
  60. cat <<EOF > conftestd2/conftest.h
  61. #ifndef DEFINED_IN_CONFTESTD1
  62. #error "include_next test doesn't work"
  63. #endif
  64. #define DEFINED_IN_CONFTESTD2
  65. EOF
  66. gl_save_CPPFLAGS="$CPPFLAGS"
  67. CPPFLAGS="$gl_save_CPPFLAGS -Iconftestd1b -Iconftestd2"
  68. AC_COMPILE_IFELSE([#include <conftest.h>],
  69. [gl_cv_have_include_next=yes],
  70. [CPPFLAGS="$gl_save_CPPFLAGS -Iconftestd1a -Iconftestd2"
  71. AC_COMPILE_IFELSE([#include <conftest.h>],
  72. [gl_cv_have_include_next=buggy],
  73. [gl_cv_have_include_next=no])
  74. ])
  75. CPPFLAGS="$gl_save_CPPFLAGS"
  76. rm -rf conftestd1a conftestd1b conftestd2
  77. ])
  78. PRAGMA_SYSTEM_HEADER=
  79. if test $gl_cv_have_include_next = yes; then
  80. INCLUDE_NEXT=include_next
  81. INCLUDE_NEXT_AS_FIRST_DIRECTIVE=include_next
  82. if test -n "$GCC"; then
  83. PRAGMA_SYSTEM_HEADER='#pragma GCC system_header'
  84. fi
  85. else
  86. if test $gl_cv_have_include_next = buggy; then
  87. INCLUDE_NEXT=include
  88. INCLUDE_NEXT_AS_FIRST_DIRECTIVE=include_next
  89. else
  90. INCLUDE_NEXT=include
  91. INCLUDE_NEXT_AS_FIRST_DIRECTIVE=include
  92. fi
  93. fi
  94. AC_SUBST([INCLUDE_NEXT])
  95. AC_SUBST([INCLUDE_NEXT_AS_FIRST_DIRECTIVE])
  96. AC_SUBST([PRAGMA_SYSTEM_HEADER])
  97. ])
  98. # gl_CHECK_NEXT_HEADERS(HEADER1 HEADER2 ...)
  99. # ------------------------------------------
  100. # For each arg foo.h, if #include_next works, define NEXT_FOO_H to be
  101. # '<foo.h>'; otherwise define it to be
  102. # '"///usr/include/foo.h"', or whatever other absolute file name is suitable.
  103. # Also, if #include_next works as first preprocessing directive in a file,
  104. # define NEXT_AS_FIRST_DIRECTIVE_FOO_H to be '<foo.h>'; otherwise define it to
  105. # be
  106. # '"///usr/include/foo.h"', or whatever other absolute file name is suitable.
  107. # That way, a header file with the following line:
  108. # #@INCLUDE_NEXT@ @NEXT_FOO_H@
  109. # or
  110. # #@INCLUDE_NEXT_AS_FIRST_DIRECTIVE@ @NEXT_AS_FIRST_DIRECTIVE_FOO_H@
  111. # behaves (after sed substitution) as if it contained
  112. # #include_next <foo.h>
  113. # even if the compiler does not support include_next.
  114. # The three "///" are to pacify Sun C 5.8, which otherwise would say
  115. # "warning: #include of /usr/include/... may be non-portable".
  116. # Use `""', not `<>', so that the /// cannot be confused with a C99 comment.
  117. # Note: This macro assumes that the header file is not empty after
  118. # preprocessing, i.e. it does not only define preprocessor macros but also
  119. # provides some type/enum definitions or function/variable declarations.
  120. AC_DEFUN([gl_CHECK_NEXT_HEADERS],
  121. [
  122. AC_REQUIRE([gl_INCLUDE_NEXT])
  123. AC_REQUIRE([AC_CANONICAL_HOST])
  124. AC_CHECK_HEADERS_ONCE([$1])
  125. m4_foreach_w([gl_HEADER_NAME], [$1],
  126. [AS_VAR_PUSHDEF([gl_next_header],
  127. [gl_cv_next_]m4_defn([gl_HEADER_NAME]))
  128. if test $gl_cv_have_include_next = yes; then
  129. AS_VAR_SET([gl_next_header], ['<'gl_HEADER_NAME'>'])
  130. else
  131. AC_CACHE_CHECK(
  132. [absolute name of <]m4_defn([gl_HEADER_NAME])[>],
  133. m4_defn([gl_next_header]),
  134. [AS_VAR_PUSHDEF([gl_header_exists],
  135. [ac_cv_header_]m4_defn([gl_HEADER_NAME]))
  136. if test AS_VAR_GET(gl_header_exists) = yes; then
  137. AC_LANG_CONFTEST(
  138. [AC_LANG_SOURCE(
  139. [[#include <]]m4_dquote(m4_defn([gl_HEADER_NAME]))[[>]]
  140. )])
  141. dnl AIX "xlc -E" and "cc -E" omit #line directives for header files
  142. dnl that contain only a #include of other header files and no
  143. dnl non-comment tokens of their own. This leads to a failure to
  144. dnl detect the absolute name of <dirent.h>, <signal.h>, <poll.h>
  145. dnl and others. The workaround is to force preservation of comments
  146. dnl through option -C. This ensures all necessary #line directives
  147. dnl are present. GCC supports option -C as well.
  148. case "$host_os" in
  149. aix*) gl_absname_cpp="$ac_cpp -C" ;;
  150. *) gl_absname_cpp="$ac_cpp" ;;
  151. esac
  152. dnl eval is necessary to expand gl_absname_cpp.
  153. dnl Ultrix and Pyramid sh refuse to redirect output of eval,
  154. dnl so use subshell.
  155. AS_VAR_SET([gl_next_header],
  156. ['"'`(eval "$gl_absname_cpp conftest.$ac_ext") 2>&AS_MESSAGE_LOG_FD |
  157. sed -n '\#/]m4_defn([gl_HEADER_NAME])[#{
  158. s#.*"\(.*/]m4_defn([gl_HEADER_NAME])[\)".*#\1#
  159. s#^/[^/]#//&#
  160. p
  161. q
  162. }'`'"'])
  163. else
  164. AS_VAR_SET([gl_next_header], ['<'gl_HEADER_NAME'>'])
  165. fi
  166. AS_VAR_POPDEF([gl_header_exists])])
  167. fi
  168. AC_SUBST(
  169. AS_TR_CPP([NEXT_]m4_defn([gl_HEADER_NAME])),
  170. [AS_VAR_GET([gl_next_header])])
  171. if test $gl_cv_have_include_next = yes || test $gl_cv_have_include_next = buggy; then
  172. # INCLUDE_NEXT_AS_FIRST_DIRECTIVE='include_next'
  173. gl_next_as_first_directive='<'gl_HEADER_NAME'>'
  174. else
  175. # INCLUDE_NEXT_AS_FIRST_DIRECTIVE='include'
  176. gl_next_as_first_directive=AS_VAR_GET([gl_next_header])
  177. fi
  178. AC_SUBST(
  179. AS_TR_CPP([NEXT_AS_FIRST_DIRECTIVE_]m4_defn([gl_HEADER_NAME])),
  180. [$gl_next_as_first_directive])
  181. AS_VAR_POPDEF([gl_next_header])])
  182. ])