1# include_next.m4 serial 26
2dnl Copyright (C) 2006-2021 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7dnl From Paul Eggert and Derek Price.
8
9dnl Sets INCLUDE_NEXT, INCLUDE_NEXT_AS_FIRST_DIRECTIVE, PRAGMA_SYSTEM_HEADER,
10dnl and PRAGMA_COLUMNS.
11dnl
12dnl INCLUDE_NEXT expands to 'include_next' if the compiler supports it, or to
13dnl 'include' otherwise.
14dnl
15dnl INCLUDE_NEXT_AS_FIRST_DIRECTIVE expands to 'include_next' if the compiler
16dnl supports it in the special case that it is the first include directive in
17dnl the given file, or to 'include' otherwise.
18dnl
19dnl PRAGMA_SYSTEM_HEADER can be used in files that contain #include_next,
20dnl so as to avoid GCC warnings when the gcc option -pedantic is used.
21dnl '#pragma GCC system_header' has the same effect as if the file was found
22dnl through the include search path specified with '-isystem' options (as
23dnl opposed to the search path specified with '-I' options). Namely, gcc
24dnl does not warn about some things, and on some systems (Solaris and Interix)
25dnl __STDC__ evaluates to 0 instead of to 1. The latter is an undesired side
26dnl effect; we are therefore careful to use 'defined __STDC__' or '1' instead
27dnl of plain '__STDC__'.
28dnl
29dnl PRAGMA_COLUMNS can be used in files that override system header files, so
30dnl as to avoid compilation errors on HP NonStop systems when the gnulib file
31dnl is included by a system header file that does a "#pragma COLUMNS 80" (which
32dnl has the effect of truncating the lines of that file and all files that it
33dnl includes to 80 columns) and the gnulib file has lines longer than 80
34dnl columns.
35
36AC_DEFUN([gl_INCLUDE_NEXT],
37[
38  AC_LANG_PREPROC_REQUIRE()
39  AC_CACHE_CHECK([whether the preprocessor supports include_next],
40    [gl_cv_have_include_next],
41    [rm -rf conftestd1a conftestd1b conftestd2
42     mkdir conftestd1a conftestd1b conftestd2
43     dnl IBM C 9.0, 10.1 (original versions, prior to the 2009-01 updates) on
44     dnl AIX 6.1 support include_next when used as first preprocessor directive
45     dnl in a file, but not when preceded by another include directive. Check
46     dnl for this bug by including <stdio.h>.
47     dnl Additionally, with this same compiler, include_next is a no-op when
48     dnl used in a header file that was included by specifying its absolute
49     dnl file name. Despite these two bugs, include_next is used in the
50     dnl compiler's <math.h>. By virtue of the second bug, we need to use
51     dnl include_next as well in this case.
52     cat <<EOF > conftestd1a/conftest.h
53#define DEFINED_IN_CONFTESTD1
54#include_next <conftest.h>
55#ifdef DEFINED_IN_CONFTESTD2
56int foo;
57#else
58#error "include_next doesn't work"
59#endif
60EOF
61     cat <<EOF > conftestd1b/conftest.h
62#define DEFINED_IN_CONFTESTD1
63#include <stdio.h>
64#include_next <conftest.h>
65#ifdef DEFINED_IN_CONFTESTD2
66int foo;
67#else
68#error "include_next doesn't work"
69#endif
70EOF
71     cat <<EOF > conftestd2/conftest.h
72#ifndef DEFINED_IN_CONFTESTD1
73#error "include_next test doesn't work"
74#endif
75#define DEFINED_IN_CONFTESTD2
76EOF
77     gl_save_CPPFLAGS="$CPPFLAGS"
78     CPPFLAGS="$gl_save_CPPFLAGS -Iconftestd1b -Iconftestd2"
79dnl We intentionally avoid using AC_LANG_SOURCE here.
80     AC_COMPILE_IFELSE([AC_LANG_DEFINES_PROVIDED[#include <conftest.h>]],
81       [gl_cv_have_include_next=yes],
82       [CPPFLAGS="$gl_save_CPPFLAGS -Iconftestd1a -Iconftestd2"
83        AC_COMPILE_IFELSE([AC_LANG_DEFINES_PROVIDED[#include <conftest.h>]],
84          [gl_cv_have_include_next=buggy],
85          [gl_cv_have_include_next=no])
86       ])
87     CPPFLAGS="$gl_save_CPPFLAGS"
88     rm -rf conftestd1a conftestd1b conftestd2
89    ])
90  PRAGMA_SYSTEM_HEADER=
91  if test $gl_cv_have_include_next = yes; then
92    INCLUDE_NEXT=include_next
93    INCLUDE_NEXT_AS_FIRST_DIRECTIVE=include_next
94    if test -n "$GCC"; then
95      PRAGMA_SYSTEM_HEADER='#pragma GCC system_header'
96    fi
97  else
98    if test $gl_cv_have_include_next = buggy; then
99      INCLUDE_NEXT=include
100      INCLUDE_NEXT_AS_FIRST_DIRECTIVE=include_next
101    else
102      INCLUDE_NEXT=include
103      INCLUDE_NEXT_AS_FIRST_DIRECTIVE=include
104    fi
105  fi
106  AC_SUBST([INCLUDE_NEXT])
107  AC_SUBST([INCLUDE_NEXT_AS_FIRST_DIRECTIVE])
108  AC_SUBST([PRAGMA_SYSTEM_HEADER])
109
110  dnl HP NonStop systems, which define __TANDEM, limit the line length
111  dnl after including some system header files.
112  AC_CACHE_CHECK([whether source code line length is unlimited],
113    [gl_cv_source_line_length_unlimited],
114    [AC_EGREP_CPP([choke me],
115       [
116#ifdef __TANDEM
117choke me
118#endif
119       ],
120       [gl_cv_source_line_length_unlimited=no],
121       [gl_cv_source_line_length_unlimited=yes])
122    ])
123  if test $gl_cv_source_line_length_unlimited = no; then
124    PRAGMA_COLUMNS="#pragma COLUMNS 10000"
125  else
126    PRAGMA_COLUMNS=
127  fi
128  AC_SUBST([PRAGMA_COLUMNS])
129])
130
131# gl_CHECK_NEXT_HEADERS(HEADER1 HEADER2 ...)
132# ------------------------------------------
133# For each arg foo.h, if #include_next works, define NEXT_FOO_H to be
134# '<foo.h>'; otherwise define it to be
135# '"///usr/include/foo.h"', or whatever other absolute file name is suitable.
136# Also, if #include_next works as first preprocessing directive in a file,
137# define NEXT_AS_FIRST_DIRECTIVE_FOO_H to be '<foo.h>'; otherwise define it to
138# be
139# '"///usr/include/foo.h"', or whatever other absolute file name is suitable.
140# That way, a header file with the following line:
141#       #@INCLUDE_NEXT@ @NEXT_FOO_H@
142# or
143#       #@INCLUDE_NEXT_AS_FIRST_DIRECTIVE@ @NEXT_AS_FIRST_DIRECTIVE_FOO_H@
144# behaves (after sed substitution) as if it contained
145#       #include_next <foo.h>
146# even if the compiler does not support include_next.
147# The three "///" are to pacify Sun C 5.8, which otherwise would say
148# "warning: #include of /usr/include/... may be non-portable".
149# Use '""', not '<>', so that the /// cannot be confused with a C99 comment.
150# Note: This macro assumes that the header file is not empty after
151# preprocessing, i.e. it does not only define preprocessor macros but also
152# provides some type/enum definitions or function/variable declarations.
153#
154# This macro also checks whether each header exists, by invoking
155# AC_CHECK_HEADERS_ONCE or AC_CHECK_HEADERS on each argument.
156AC_DEFUN([gl_CHECK_NEXT_HEADERS],
157[
158  gl_NEXT_HEADERS_INTERNAL([$1], [check])
159])
160
161# gl_NEXT_HEADERS(HEADER1 HEADER2 ...)
162# ------------------------------------
163# Like gl_CHECK_NEXT_HEADERS, except do not check whether the headers exist.
164# This is suitable for headers like <stddef.h> that are standardized by C89
165# and therefore can be assumed to exist.
166AC_DEFUN([gl_NEXT_HEADERS],
167[
168  gl_NEXT_HEADERS_INTERNAL([$1], [assume])
169])
170
171# The guts of gl_CHECK_NEXT_HEADERS and gl_NEXT_HEADERS.
172AC_DEFUN([gl_NEXT_HEADERS_INTERNAL],
173[
174  AC_REQUIRE([gl_INCLUDE_NEXT])
175  AC_REQUIRE([AC_CANONICAL_HOST])
176
177  m4_if([$2], [check],
178    [AC_CHECK_HEADERS_ONCE([$1])
179    ])
180
181  m4_foreach_w([gl_HEADER_NAME], [$1],
182    [AS_VAR_PUSHDEF([gl_next_header],
183                    [gl_cv_next_]m4_defn([gl_HEADER_NAME]))
184     if test $gl_cv_have_include_next = yes; then
185       AS_VAR_SET([gl_next_header], ['<'gl_HEADER_NAME'>'])
186     else
187       AC_CACHE_CHECK(
188         [absolute name of <]m4_defn([gl_HEADER_NAME])[>],
189         [gl_next_header],
190         [m4_if([$2], [check],
191            [AS_VAR_PUSHDEF([gl_header_exists],
192                            [ac_cv_header_]m4_defn([gl_HEADER_NAME]))
193             if test AS_VAR_GET([gl_header_exists]) = yes; then
194             AS_VAR_POPDEF([gl_header_exists])
195            ])
196           gl_ABSOLUTE_HEADER_ONE(gl_HEADER_NAME)
197           AS_VAR_COPY([gl_header], [gl_cv_absolute_]AS_TR_SH(gl_HEADER_NAME))
198           AS_VAR_SET([gl_next_header], ['"'$gl_header'"'])
199          m4_if([$2], [check],
200            [else
201               AS_VAR_SET([gl_next_header], ['<'gl_HEADER_NAME'>'])
202             fi
203            ])
204         ])
205     fi
206     AC_SUBST(
207       AS_TR_CPP([NEXT_]m4_defn([gl_HEADER_NAME])),
208       [AS_VAR_GET([gl_next_header])])
209     if test $gl_cv_have_include_next = yes || test $gl_cv_have_include_next = buggy; then
210       # INCLUDE_NEXT_AS_FIRST_DIRECTIVE='include_next'
211       gl_next_as_first_directive='<'gl_HEADER_NAME'>'
212     else
213       # INCLUDE_NEXT_AS_FIRST_DIRECTIVE='include'
214       gl_next_as_first_directive=AS_VAR_GET([gl_next_header])
215     fi
216     AC_SUBST(
217       AS_TR_CPP([NEXT_AS_FIRST_DIRECTIVE_]m4_defn([gl_HEADER_NAME])),
218       [$gl_next_as_first_directive])
219     AS_VAR_POPDEF([gl_next_header])])
220])
221
222# Autoconf 2.68 added warnings for our use of AC_COMPILE_IFELSE;
223# this fallback is safe for all earlier autoconf versions.
224m4_define_default([AC_LANG_DEFINES_PROVIDED])
225