1 # include_next.m4 serial 8
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.
9 dnl Sets INCLUDE_NEXT and PRAGMA_SYSTEM_HEADER.
11 dnl INCLUDE_NEXT expands to 'include_next' if the compiler supports it, or to
12 dnl 'include' otherwise.
14 dnl PRAGMA_SYSTEM_HEADER can be used in files that contain #include_next,
15 dnl so as to avoid GCC warnings when the gcc option -pedantic is used.
16 dnl '#pragma GCC system_header' has the same effect as if the file was found
17 dnl through the include search path specified with '-isystem' options (as
18 dnl opposed to the search path specified with '-I' options). Namely, gcc
19 dnl does not warn about some things, and on some systems (Solaris and Interix)
20 dnl __STDC__ evaluates to 0 instead of to 1. The latter is an undesired side
21 dnl effect; we are therefore careful to use 'defined __STDC__' or '1' instead
22 dnl of plain '__STDC__'.
24 AC_DEFUN([gl_INCLUDE_NEXT],
26 AC_LANG_PREPROC_REQUIRE()
27 AC_CACHE_CHECK([whether the preprocessor supports include_next],
28 [gl_cv_have_include_next],
29 [rm -rf conftestd1 conftestd2
30 mkdir conftestd1 conftestd2
31 dnl The include of <stdio.h> is because IBM C 9.0 on AIX 6.1 supports
32 dnl include_next when used as first preprocessor directive in a file,
33 dnl but not when preceded by another include directive.
34 cat <<EOF > conftestd1/conftest.h
35 #define DEFINED_IN_CONFTESTD1
37 #include_next <conftest.h>
38 #ifdef DEFINED_IN_CONFTESTD2
41 #error "include_next doesn't work"
44 cat <<EOF > conftestd2/conftest.h
45 #ifndef DEFINED_IN_CONFTESTD1
46 #error "include_next test doesn't work"
48 #define DEFINED_IN_CONFTESTD2
50 save_CPPFLAGS="$CPPFLAGS"
51 CPPFLAGS="$CPPFLAGS -Iconftestd1 -Iconftestd2"
52 AC_COMPILE_IFELSE([#include <conftest.h>],
53 [gl_cv_have_include_next=yes],
54 [gl_cv_have_include_next=no])
55 CPPFLAGS="$save_CPPFLAGS"
56 rm -rf conftestd1 conftestd2
59 if test $gl_cv_have_include_next = yes; then
60 INCLUDE_NEXT=include_next
61 if test -n "$GCC"; then
62 PRAGMA_SYSTEM_HEADER='#pragma GCC system_header'
67 AC_SUBST([INCLUDE_NEXT])
68 AC_SUBST([PRAGMA_SYSTEM_HEADER])
71 # gl_CHECK_NEXT_HEADERS(HEADER1 HEADER2 ...)
72 # ------------------------------------------
73 # For each arg foo.h, if #include_next works, define NEXT_FOO_H to be
74 # '<foo.h>'; otherwise define it to be
75 # '"///usr/include/foo.h"', or whatever other absolute file name is suitable.
76 # That way, a header file with the following line:
77 # #@INCLUDE_NEXT@ @NEXT_FOO_H@
78 # behaves (after sed substitution) as if it contained
79 # #include_next <foo.h>
80 # even if the compiler does not support include_next.
81 # The three "///" are to pacify Sun C 5.8, which otherwise would say
82 # "warning: #include of /usr/include/... may be non-portable".
83 # Use `""', not `<>', so that the /// cannot be confused with a C99 comment.
84 # Note: This macro assumes that the header file is not empty after
85 # preprocessing, i.e. it does not only define preprocessor macros but also
86 # provides some type/enum definitions or function/variable declarations.
87 AC_DEFUN([gl_CHECK_NEXT_HEADERS],
89 AC_REQUIRE([gl_INCLUDE_NEXT])
90 AC_CHECK_HEADERS_ONCE([$1])
92 m4_foreach_w([gl_HEADER_NAME], [$1],
93 [AS_VAR_PUSHDEF([gl_next_header],
94 [gl_cv_next_]m4_quote(m4_defn([gl_HEADER_NAME])))
95 if test $gl_cv_have_include_next = yes; then
96 AS_VAR_SET([gl_next_header], ['<'gl_HEADER_NAME'>'])
99 [absolute name of <]m4_quote(m4_defn([gl_HEADER_NAME]))[>],
100 m4_quote(m4_defn([gl_next_header])),
101 [AS_VAR_PUSHDEF([gl_header_exists],
102 [ac_cv_header_]m4_quote(m4_defn([gl_HEADER_NAME])))
103 if test AS_VAR_GET(gl_header_exists) = yes; then
106 [[#include <]]m4_dquote(m4_defn([gl_HEADER_NAME]))[[>]]
108 dnl eval is necessary to expand ac_cpp.
109 dnl Ultrix and Pyramid sh refuse to redirect output of eval,
111 AS_VAR_SET([gl_next_header],
112 ['"'`(eval "$ac_cpp conftest.$ac_ext") 2>&AS_MESSAGE_LOG_FD |
113 sed -n '\#/]m4_quote(m4_defn([gl_HEADER_NAME]))[#{
114 s#.*"\(.*/]m4_quote(m4_defn([gl_HEADER_NAME]))[\)".*#\1#
120 AS_VAR_SET([gl_next_header], ['<'gl_HEADER_NAME'>'])
122 AS_VAR_POPDEF([gl_header_exists])])
125 AS_TR_CPP([NEXT_]m4_quote(m4_defn([gl_HEADER_NAME]))),
126 [AS_VAR_GET([gl_next_header])])
127 AS_VAR_POPDEF([gl_next_header])])