include_next.m4 6.5 KB

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