Make determination of absolute name of header file work with AIX xlc.
[gnulib.git] / m4 / include_next.m4
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.
6
7 dnl From Paul Eggert and Derek Price.
8
9 dnl Sets INCLUDE_NEXT and PRAGMA_SYSTEM_HEADER.
10 dnl
11 dnl INCLUDE_NEXT expands to 'include_next' if the compiler supports it, or to
12 dnl 'include' otherwise.
13 dnl
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__'.
23
24 AC_DEFUN([gl_INCLUDE_NEXT],
25 [
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
36 #include <stdio.h>
37 #include_next <conftest.h>
38 #ifdef DEFINED_IN_CONFTESTD2
39 int foo;
40 #else
41 #error "include_next doesn't work"
42 #endif
43 EOF
44      cat <<EOF > conftestd2/conftest.h
45 #ifndef DEFINED_IN_CONFTESTD1
46 #error "include_next test doesn't work"
47 #endif
48 #define DEFINED_IN_CONFTESTD2
49 EOF
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
57     ])
58   PRAGMA_SYSTEM_HEADER=
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'
63     fi
64   else
65     INCLUDE_NEXT=include
66   fi
67   AC_SUBST([INCLUDE_NEXT])
68   AC_SUBST([PRAGMA_SYSTEM_HEADER])
69 ])
70
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],
88 [
89   AC_REQUIRE([gl_INCLUDE_NEXT])
90   AC_REQUIRE([AC_CANONICAL_HOST])
91   AC_CHECK_HEADERS_ONCE([$1])
92
93   m4_foreach_w([gl_HEADER_NAME], [$1],
94     [AS_VAR_PUSHDEF([gl_next_header],
95                     [gl_cv_next_]m4_quote(m4_defn([gl_HEADER_NAME])))
96      if test $gl_cv_have_include_next = yes; then
97        AS_VAR_SET([gl_next_header], ['<'gl_HEADER_NAME'>'])
98      else
99        AC_CACHE_CHECK(
100          [absolute name of <]m4_quote(m4_defn([gl_HEADER_NAME]))[>],
101          m4_quote(m4_defn([gl_next_header])),
102          [AS_VAR_PUSHDEF([gl_header_exists],
103                          [ac_cv_header_]m4_quote(m4_defn([gl_HEADER_NAME])))
104           if test AS_VAR_GET(gl_header_exists) = yes; then
105             AC_LANG_CONFTEST(
106               [AC_LANG_SOURCE(
107                  [[#include <]]m4_dquote(m4_defn([gl_HEADER_NAME]))[[>]]
108                )])
109             dnl AIX "xlc -E" and "cc -E" omit #line directives for header files
110             dnl that contain only a #include of other header files and no
111             dnl non-comment tokens of their own. This leads to a failure to
112             dnl detect the absolute name of <dirent.h>, <signal.h>, <poll.h>
113             dnl and others. The workaround is to force preservation of comments
114             dnl through option -C. This ensures all necessary #line directives
115             dnl are present. GCC supports option -C as well.
116             case "$host_os" in
117               aix*) gl_absname_cpp="$ac_cpp -C" ;;
118               *)    gl_absname_cpp="$ac_cpp" ;;
119             esac
120             dnl eval is necessary to expand gl_absname_cpp.
121             dnl Ultrix and Pyramid sh refuse to redirect output of eval,
122             dnl so use subshell.
123             AS_VAR_SET([gl_next_header],
124               ['"'`(eval "$gl_absname_cpp conftest.$ac_ext") 2>&AS_MESSAGE_LOG_FD |
125                sed -n '\#/]m4_quote(m4_defn([gl_HEADER_NAME]))[#{
126                  s#.*"\(.*/]m4_quote(m4_defn([gl_HEADER_NAME]))[\)".*#\1#
127                  s#^/[^/]#//&#
128                  p
129                  q
130                }'`'"'])
131           else
132             AS_VAR_SET([gl_next_header], ['<'gl_HEADER_NAME'>'])
133           fi
134           AS_VAR_POPDEF([gl_header_exists])])
135      fi
136      AC_SUBST(
137        AS_TR_CPP([NEXT_]m4_quote(m4_defn([gl_HEADER_NAME]))),
138        [AS_VAR_GET([gl_next_header])])
139      AS_VAR_POPDEF([gl_next_header])])
140 ])