1 # include_next.m4 serial 9
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.
7 dnl From Paul Eggert and Derek Price.
11 dnl Sets INCLUDE_NEXT and PRAGMA_SYSTEM_HEADER.
13 dnl INCLUDE_NEXT expands to 'include_next' if the compiler supports it, or to
14 dnl 'include' otherwise.
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__'.
26 AC_DEFUN([gl_INCLUDE_NEXT],
28 AC_LANG_PREPROC_REQUIRE()
29 AC_CACHE_CHECK([whether the preprocessor supports include_next],
30 [gl_cv_have_include_next],
31 [rm -rf conftestd1 conftestd2
32 mkdir conftestd1 conftestd2
33 dnl The include of <stdio.h> is because IBM C 9.0 on AIX 6.1 supports
34 dnl include_next when used as first preprocessor directive in a file,
35 dnl but not when preceded by another include directive.
36 cat <<EOF > conftestd1/conftest.h
37 #define DEFINED_IN_CONFTESTD1
39 #include_next <conftest.h>
40 #ifdef DEFINED_IN_CONFTESTD2
43 #error "include_next doesn't work"
46 cat <<EOF > conftestd2/conftest.h
47 #ifndef DEFINED_IN_CONFTESTD1
48 #error "include_next test doesn't work"
50 #define DEFINED_IN_CONFTESTD2
52 save_CPPFLAGS="$CPPFLAGS"
53 CPPFLAGS="$CPPFLAGS -Iconftestd1 -Iconftestd2"
54 AC_COMPILE_IFELSE([#include <conftest.h>],
55 [gl_cv_have_include_next=yes],
56 [gl_cv_have_include_next=no])
57 CPPFLAGS="$save_CPPFLAGS"
58 rm -rf conftestd1 conftestd2
61 if test $gl_cv_have_include_next = yes; then
62 INCLUDE_NEXT=include_next
63 if test -n "$GCC"; then
64 dnl GCC 3.0 or newer supports #pragma GCC system_header.
65 PRAGMA_SYSTEM_HEADER='#if __GNUC__ >= 3
66 #pragma GCC system_header
72 AC_SUBST([INCLUDE_NEXT])
73 AC_SUBST([PRAGMA_SYSTEM_HEADER])
76 # gl_CHECK_NEXT_HEADERS(HEADER1 HEADER2 ...)
77 # ------------------------------------------
78 # For each arg foo.h, if #include_next works, define NEXT_FOO_H to be
79 # '<foo.h>'; otherwise define it to be
80 # '"///usr/include/foo.h"', or whatever other absolute file name is suitable.
81 # That way, a header file with the following line:
82 # #@INCLUDE_NEXT@ @NEXT_FOO_H@
83 # behaves (after sed substitution) as if it contained
84 # #include_next <foo.h>
85 # even if the compiler does not support include_next.
86 # The three "///" are to pacify Sun C 5.8, which otherwise would say
87 # "warning: #include of /usr/include/... may be non-portable".
88 # Use `""', not `<>', so that the /// cannot be confused with a C99 comment.
89 # Note: This macro assumes that the header file is not empty after
90 # preprocessing, i.e. it does not only define preprocessor macros but also
91 # provides some type/enum definitions or function/variable declarations.
92 AC_DEFUN([gl_CHECK_NEXT_HEADERS],
94 AC_REQUIRE([gl_INCLUDE_NEXT])
95 AC_CHECK_HEADERS_ONCE([$1])
97 m4_foreach_w([gl_HEADER_NAME], [$1],
98 [AS_VAR_PUSHDEF([gl_next_header],
99 [gl_cv_next_]m4_quote(m4_defn([gl_HEADER_NAME])))
100 if test $gl_cv_have_include_next = yes; then
101 AS_VAR_SET([gl_next_header], ['<'gl_HEADER_NAME'>'])
104 [absolute name of <]m4_quote(m4_defn([gl_HEADER_NAME]))[>],
105 m4_quote(m4_defn([gl_next_header])),
106 [AS_VAR_PUSHDEF([gl_header_exists],
107 [ac_cv_header_]m4_quote(m4_defn([gl_HEADER_NAME])))
108 if test AS_VAR_GET(gl_header_exists) = yes; then
111 [[#include <]]m4_dquote(m4_defn([gl_HEADER_NAME]))[[>]]
113 dnl eval is necessary to expand ac_cpp.
114 dnl Ultrix and Pyramid sh refuse to redirect output of eval,
116 AS_VAR_SET([gl_next_header],
117 ['"'`(eval "$ac_cpp conftest.$ac_ext") 2>&AS_MESSAGE_LOG_FD |
118 sed -n '\#/]m4_quote(m4_defn([gl_HEADER_NAME]))[#{
119 s#.*"\(.*/]m4_quote(m4_defn([gl_HEADER_NAME]))[\)".*#\1#
125 AS_VAR_SET([gl_next_header], ['<'gl_HEADER_NAME'>'])
127 AS_VAR_POPDEF([gl_header_exists])])
130 AS_TR_CPP([NEXT_]m4_quote(m4_defn([gl_HEADER_NAME]))),
131 [AS_VAR_GET([gl_next_header])])
132 AS_VAR_POPDEF([gl_next_header])])