* m4/include_next.m4 (gl_INCLUDE_NEXT): Define and AC_SUBST
[gnulib.git] / m4 / include_next.m4
1 # include_next.m4 serial 2
2 dnl Copyright (C) 2006, 2007 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 AC_DEFUN([gl_INCLUDE_NEXT],
10 [
11   AC_LANG_PREPROC_REQUIRE()
12   AC_CACHE_CHECK([whether the preprocessor supports include_next],
13     [gl_cv_have_include_next],
14     [AC_PREPROC_IFELSE([#include_next <stddef.h>],
15        [gl_cv_have_include_next=yes],
16        [gl_cv_have_include_next=no])])
17   if test $gl_cv_have_include_next = yes; then
18
19     dnl FIXME: Remove HAVE_INCLUDE_NEXT and update everything that uses it
20     dnl to use @INCLUDE_NEXT@ instead.
21     AC_DEFINE([HAVE_INCLUDE_NEXT], 1,
22               [Define if your compiler supports the #include_next directive.])
23
24     INCLUDE_NEXT=include_next
25   else
26     INCLUDE_NEXT=include
27   fi
28   AC_SUBST([INCLUDE_NEXT])
29 ])
30
31 # gl_CHECK_NEXT_HEADERS(HEADER1 HEADER2 ...)
32 # ------------------------------------------
33 # For each arg foo.h, if #include_next works, define NEXT_FOO_H to be
34 # '<foo.h>'; otherwise define it to be
35 # '"///usr/include/foo.h"', or whatever other absolute file name is suitable.
36 # That way, a header file with the following line:
37 #       #@INCLUDE_NEXT@ @NEXT_FOO_H@
38 # behaves (after sed substitution) as if it contained
39 #       #include_next <foo.h>
40 # even if the compiler does not support include_next.
41 # The three "///" are to pacify Sun C 5.8, which otherwise would say
42 # "warning: #include of /usr/include/... may be non-portable".
43 # Use `""', not `<>', so that the /// cannot be confused with a C99 comment.
44 AC_DEFUN([gl_CHECK_NEXT_HEADERS],
45 [
46   AC_REQUIRE([gl_INCLUDE_NEXT])
47   AC_CHECK_HEADERS_ONCE([$1])
48
49   AC_FOREACH([gl_HEADER_NAME], [$1],
50     [AS_VAR_PUSHDEF([gl_next_header],
51                     [gl_cv_next_]m4_quote(m4_defn([gl_HEADER_NAME])))
52      AS_VAR_SET([gl_next_header], ['<'gl_HEADER_NAME'>'])
53      if test $gl_cv_have_include_next = no; then
54        AC_CACHE_CHECK(
55          [absolute name of <]m4_quote(m4_defn([gl_HEADER_NAME]))[>],
56          m4_quote(m4_defn([gl_next_header])),
57          [if test AS_VAR_GET([ac_cv_header_]m4_quote(m4_defn([gl_HEADER_NAME]))) = yes; then
58             AC_LANG_CONFTEST(
59               [AC_LANG_SOURCE(
60                  [[#include <]]m4_dquote(m4_defn([gl_HEADER_NAME]))[[>]]
61                )])
62             dnl eval is necessary to expand ac_cpp.
63             dnl Ultrix and Pyramid sh refuse to redirect output of eval,
64             dnl so use subshell.
65             AS_VAR_SET([gl_next_header],
66               ['"'`(eval "$ac_cpp conftest.$ac_ext") 2>&AS_MESSAGE_LOG_FD |
67                sed -n '\#/]m4_quote(m4_defn([gl_HEADER_NAME]))[#{
68                  s#.*"\(.*/]m4_quote(m4_defn([gl_HEADER_NAME]))[\)".*#\1#
69                  s#^/[^/]#//&#
70                  p
71                  q
72                }'`'"'])
73           fi])
74      fi
75      AC_SUBST(
76        AS_TR_CPP([NEXT_]m4_quote(m4_defn([gl_HEADER_NAME]))),
77        [AS_VAR_GET([gl_next_header])])
78      AS_VAR_POPDEF([gl_next_header])])
79 ])