make fetch
[autoconf.git] / tests / c.at
blob0c88490e717c4c8e85ccdbab6b83e8256278b5ca
1 #                                                       -*- Autotest -*-
3 AT_BANNER([C low level compiling/preprocessing macros.])
5 # Copyright (C) 2000-2006, 2008-2017, 2020-2024 Free Software
6 # Foundation, Inc.
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
22 # Since the macros which compile are required by most tests, check
23 # them first.  But remember that looking for a compiler is even more
24 # primitive, so check those first.
27 ## ------------ ##
28 ## Extensions.  ##
29 ## ------------ ##
31 AT_CHECK_CONFIGURE_AC([Object file extensions],
32 [[AC_PROG_CC
34 # As far as we know only 'foo', 'foo.exe' are possible executable,
35 # and 'foo.o', 'foo.obj' are possible object files.  Autoconf must not
36 # know that, but it is OK for the test suite to take this into account.
37 AS@&t@_CASE([$ac_exeext],
38   ['' | '.exe'],
39     [],
40     [AC_MSG_ERROR([suspicious executable suffix: $ac_exeext])])
42 AS@&t@_CASE([$ac_objext],
43   ['o' | 'obj'],
44     [],
45     [AC_MSG_ERROR([suspicious object suffix: $ac_objext])])
47 AC_OUTPUT
48 ]])
51 ## -------------------------- ##
52 ## Broken/missing compilers.  ##
53 ## -------------------------- ##
56 # Check that Autoconf correctly diagnoses broken compilers, and in
57 # particular, if it does not exit 77, the test suite is in trouble...
58 # FIXME: Once a precise message decided, check stderr of configure.
59 AT_SETUP([Broken/missing compilers])
61 AT_DATA([configure.ac],
62 [[AC_INIT
63 AC_PROG_CC
64 AC_OUTPUT
65 ]])
67 AT_CHECK_AUTOCONF
68 AT_CHECK_CONFIGURE([CC=no-such-compiler], 77, ignore, ignore)
70 AT_CLEANUP
73 ## ------------ ##
74 ## C keywords.  ##
75 ## ------------ ##
77 AT_CHECK_CONFIGURE_AC([C keywords],
78 [[AC_PROG_CC
80 AC_C_CONST
81 AC_C_TYPEOF
82 AC_C_VOLATILE
84 # If the C compiler is GCC, AC_C_{CONST,TYPEOF,VOLATILE} ought to all
85 # detect support for their respective keyword.
86 case $GCC,$ac_cv_c_const,$ac_cv_c_typeof,$ac_cv_c_volatile in
87  yes,*no*)
88    AC_MSG_ERROR([failed to detect 'const', 'typeof', or 'volatile' support]);;
89 esac
90 ]])
93 ## --------------------------------- ##
94 ## AC_PROG_CPP requires AC_PROG_CC.  ##
95 ## --------------------------------- ##
97 AT_CHECK_CONFIGURE_AC([AC_PROG_CPP requires AC_PROG_CC],
98 [[AC_PROG_CPP
100 # AC_PROG_CPP should have AC_REQUIREd AC_PROG_CC.
101 if test -z "$CC"; then
102    AC_MSG_ERROR([looked for a C preprocessor without looking for a compiler])
107 ## --------------------------- ##
108 ## AC_PROG_CPP with warnings.  ##
109 ## --------------------------- ##
112 # It's Ok for strict preprocessors to produce warnings.
114 AT_SETUP([AC_PROG_CPP with warnings])
116 AT_DATA([mycpp],
117 [[#! /bin/sh
118 echo noise >&2
119 exec "$@"
122 chmod +x mycpp
124 _AT_CHECK_AC_MACRO(
125 [[AC_PROG_CPP
126 # If the preprocessor is not strict, just ignore
127 test "x$ac_c_preproc_warn_flag" = xyes &&
128   AC_MSG_ERROR([preprocessor has no warning option], 77)
129 CPP="./mycpp $CPP"
131 # Exercise CPP.
133 AC_PREPROC_IFELSE([AC_LANG_SOURCE([[#include <float.h>]])],
134   [AC_DEFINE([HAVE_FLOAT_H], [1], [Define if you have float.h])])
135 AC_PREPROC_IFELSE([AC_LANG_SOURCE([[#include <autoconf_io.h>]])],
136   [AC_DEFINE([HAVE_AUTOCONF_IO_H], [1], [Define if you have autoconf_io.h])])
138 AT_CHECK_DEFINES(
139 [/* #undef HAVE_AUTOCONF_IO_H */
140 #define HAVE_FLOAT_H 1
143 AT_CLEANUP
146 ## ------------------------------ ##
147 ## AC_PROG_CPP without warnings.  ##
148 ## ------------------------------ ##
150 AT_SETUP([AC_PROG_CPP without warnings])
152 # Ignore if the cpp in $PATH doesn't work
153 AT_CHECK([[echo '#include <stdio.h>' | cpp || exit 77]],
154   [], [ignore], [ignore])
156 # A cpp which exit status is meaningless.
157 AT_DATA([mycpp],
158 [[#! /bin/sh
159 cpp "$@"
160 exit 0
163 chmod +x mycpp
165 _AT_CHECK_AC_MACRO(
166 [[CPP=./mycpp
167 AC_PROG_CPP
168 test "x$ac_c_preproc_warn_flag" != xyes &&
169   AC_MSG_ERROR([failed to detect preprocessor warning option])
171 # Exercise CPP.
172 AC_PREPROC_IFELSE([AC_LANG_SOURCE([[#include <float.h>]])],
173   [AC_DEFINE([HAVE_FLOAT_H], [1], [Define if you have float.h])])
174 AC_PREPROC_IFELSE([AC_LANG_SOURCE([[#include <autoconf_io.h>]])],
175   [AC_DEFINE([HAVE_AUTOCONF_IO_H], [1], [Define if you have autoconf_io.h])])
178 AT_CHECK_DEFINES(
179 [/* #undef HAVE_AUTOCONF_IO_H */
180 #define HAVE_FLOAT_H 1
183 AT_CLEANUP
187 ## -------------------- ##
188 ## AC_PROG_CPP via CC.  ##
189 ## -------------------- ##
192 # It's Ok for strict preprocessors to produce warnings.
194 AT_SETUP([AC_PROG_CPP via CC])
196 # Ignore if the cpp in $PATH doesn't work
197 AT_CHECK([[echo '#include <stdio.h>' | cpp || exit 77]],
198   [], [ignore], [ignore])
200 AT_DATA([mycc],
201 [[#! /bin/sh
202 echo "Annoying copyright message" >&2
203 exec "$@"
206 chmod +x mycc
208 # We go through the following contortions, in order to have the
209 # configure script go down the same codepaths as it would during a
210 # normal CPP selection check.  If we explicitly set CPP, it goes down
211 # a different codepath.
212 _AT_CHECK_AC_MACRO(
213 [[AC_PROG_CC
214 CC="./mycc $CC"
215 AC_PROG_CPP
216 # The test $CC compiler should have been selected.
217 test "$CPP" != "$CC -E" &&
218   AC_MSG_ERROR([error messages on stderr cause the preprocessor selection to fail])
220 # Exercise CPP.
221 AC_PREPROC_IFELSE([AC_LANG_SOURCE([[#include <float.h>]])],
222   [AC_DEFINE([HAVE_FLOAT_H], [1], [Define if you have float.h])])
223 AC_PREPROC_IFELSE([AC_LANG_SOURCE([[#include <autoconf_io.h>]])],
224   [AC_DEFINE([HAVE_AUTOCONF_IO_H], [1], [Define if you have autoconf_io.h])])
227 AT_CHECK_DEFINES(
228 [/* #undef HAVE_AUTOCONF_IO_H */
229 #define HAVE_FLOAT_H 1
232 AT_CLEANUP
235 ## ----------------------------------- ##
236 ## AC_PROG_EGREP and AC_EGREP_HEADER.  ##
237 ## ----------------------------------- ##
239 AT_SETUP([AC_PROG_EGREP and AC_EGREP_HEADER])
241 _AT_CHECK_AC_MACRO(
242 [[AC_PROG_CPP
243   AC_PROG_EGREP
245   # Although this code is incorrect (it should use AS_IF),
246   # it follows a too-common real world pattern.
247   # For now, test for it; we may remove this test later.
248   if false; then
249     AC_EGREP_HEADER([^], [limits.h])
250   fi
252   AC_EGREP_HEADER([$], [limits.h], [],
253     [AC_MSG_ERROR([[egrep-related macros do not tolerate misuse of 'if']])])
256 AT_CLEANUP
259 ## ------------------------------------ ##
260 ## AC_NO_EXECUTABLES (working linker).  ##
261 ## ------------------------------------ ##
263 AT_CHECK_CONFIGURE_AC([AC_NO_EXECUTABLES (working linker)],
264 [[AC_NO_EXECUTABLES
265 AC_PROG_CC
269 ## ----------------------------------- ##
270 ## AC_NO_EXECUTABLES (broken linker).  ##
271 ## ----------------------------------- ##
273 AT_CHECK_CONFIGURE_AC([AC_NO_EXECUTABLES (broken linker)],
274 [[LDFLAGS=-lnosuchlibrary
275 AC_NO_EXECUTABLES
276 AC_PROG_CC
280 ## -------------------------- ##
281 ## AC_USE_SYSTEM_EXTENSIONS.  ##
282 ## -------------------------- ##
284 AT_SETUP([AC_USE_SYSTEM_EXTENSIONS])
286 # Some existing configure.ac mixed AC_AIX (now an alias for
287 # AC_USE_SYSTEM_EXTENSIONS) and AC_DEFINE([__EXTENSIONS__]), which
288 # broke autoheader in 2.62.  Test that this is supported.
290 _AT_CHECK_AC_MACRO(
291 [[AC_AIX
292 AC_DEFINE([__EXTENSIONS__], [1], [Manually defined for Solaris])
293 ]], [], [-Wno-obsolete])
295 _AT_CHECK_AC_MACRO(
296 [[AC_USE_SYSTEM_EXTENSIONS
297 AC_DEFINE([__EXTENSIONS__], [1], [Manually defined for Solaris])
300 AT_CLEANUP
303 ## ----------------------- ##
304 ## AC_C_RESTRICT and C++.  ##
305 ## ----------------------- ##
307 AT_SETUP([AC_C_RESTRICT and C++])
309 # In some compiler suites, the left hand doesn't know about everything
310 # the right hand does; or the user mixes the C compiler from one suite
311 # with the C++ compiler from another.  In this case, Sun WorkShop CC
312 # not like the _Restrict accepted by cc.
314 AT_DATA([configure.ac],
315 [[AC_INIT
316 AC_PROG_CC
317 AC_PROG_CXX
318 AC_C_RESTRICT
319 AC_CONFIG_HEADERS([config.h])
320 AC_CONFIG_FILES([Makefile])
321 AC_OUTPUT
324 AT_DATA([Makefile.in],
325 [[CC = @CC@
326 CXX = @CXX@
327 CFLAGS = @CFLAGS@
328 CXXFLAGS = @CXXFLAGS@
329 CPPFLAGS = -I. @CPPFLAGS@
330 OBJEXT = @OBJEXT@
331 all: foo.$(OBJEXT) bar.$(OBJEXT)
332 cpp-works:
333         $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c cpp-works.cpp
334 foo.$(OBJEXT): foo.c
335         $(CC) $(CPPFLAGS) $(CFLAGS) -c foo.c
336 bar.$(OBJEXT): bar.cpp
337         $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c bar.cpp
340 AT_DATA([foo.c],
341 [[#include <config.h>
343 int foo (int * restrict i1, int * restrict i2)
345   return i1[0] + i2[0];
349 cp foo.c bar.cpp
351 AT_DATA([cpp-works.cpp],
352 [[// This file is just to test whether we have a working C++ compiler at all
353 class foo { int x; };
354 class foo foobar;
357 AT_CHECK_AUTOCONF
358 AT_CHECK_AUTOHEADER([], [restrict])
359 AT_CHECK_CONFIGURE
360 AT_CHECK_MAKE([cpp-works || exit 77])
361 AT_CHECK_MAKE
363 AT_CLEANUP
366 ## ---------------- ##
367 ## AC_OPENMP and C. ##
368 ## ---------------- ##
370 AT_SETUP([AC_OPENMP and C])
372 AT_DATA([configure.ac],
373 [[AC_INIT
374 AC_PROG_CC
375 AC_OPENMP
376 if test "X$ac_cv_prog_c_openmp" = Xunsupported; then
377   AS_EXIT([77])
379 CFLAGS="$CFLAGS $OPENMP_CFLAGS"
380 CPPFLAGS="$CPPFLAGS $OPENMP_CFLAGS"
381 AC_CONFIG_FILES([Makefile])
382 AC_OUTPUT
385 AT_DATA([Makefile.in],
386 [[foo@EXEEXT@: foo.@OBJEXT@
387         @CC@ @CFLAGS@ @LDFLAGS@ -o $@ foo.@OBJEXT@
389 foo.@OBJEXT@: foo.c
390         @CC@ @CPPFLAGS@ @CFLAGS@ -c foo.c
393 AT_DATA([foo.c],
394 [[#ifdef _OPENMP
395 #include <omp.h>
396 #endif
397 #include <stdio.h>
399 int main (void)
401 #ifdef _OPENMP
402 #pragma omp parallel
403   {
404     int id = omp_get_thread_num ();
405     printf ("hello omp world from %d\n", id);
406   }
407 #endif
408   return 0;
412 AT_CHECK_AUTOCONF
413 AT_CHECK_CONFIGURE
414 AT_CHECK_MAKE
416 AT_CLEANUP
419 ## ------------------ ##
420 ## AC_OPENMP anc C++. ##
421 ## ------------------ ##
423 AT_SETUP([AC_OPENMP and C++])
425 AT_DATA([configure.ac],
426 [[AC_INIT
427 AC_PROG_CXX
428 AC_LANG([C++])
429 AC_OPENMP
430 if test "X$ac_cv_prog_cxx_openmp" = Xunsupported; then
431   AS_EXIT([77])
433 CXXFLAGS="$CXXFLAGS $OPENMP_CXXFLAGS"
434 CPPFLAGS="$CPPFLAGS $OPENMP_CXXFLAGS"
435 AC_CONFIG_FILES([Makefile])
436 AC_OUTPUT
439 AT_DATA([Makefile.in],
440 [[foo@EXEEXT@: foo.@OBJEXT@
441         @CXX@ @CXXFLAGS@ @LDFLAGS@ -o $@ foo.@OBJEXT@
443 foo.@OBJEXT@: foo.cpp
444         @CXX@ @CPPFLAGS@ @CXXFLAGS@ -c foo.cpp
447 AT_DATA([foo.cpp],
448 [[int main (void)
450   return 0;
454 AT_CHECK_AUTOCONF
455 AT_CHECK_CONFIGURE
456 AT_CHECK_MAKE
458 AT_CLEANUP