Debian Codegraph @

m4/include_next.m4

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