summaryrefslogtreecommitdiff
path: root/gnulib-m4/error_h.m4
blob: 91a8fc6897ad782fe33906a0595aa5ef4713c268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# error_h.m4
# serial 5
dnl Copyright (C) 1996-2024 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

dnl From Bruno Haible.
dnl Provide a working <error.h>.

AC_DEFUN_ONCE([gl_ERROR_H],
[
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles

  gl_CHECK_NEXT_HEADERS([error.h])
  if test $ac_cv_header_error_h = yes; then
    HAVE_ERROR_H=1
  else
    HAVE_ERROR_H=0
  fi
  AC_SUBST([HAVE_ERROR_H])

  REPLACE_ERROR=0

  gl_CHECK_FUNCS_ANDROID([error], [[#include <error.h>]])
  if test $ac_cv_func_error = yes; then
    HAVE_ERROR=1
  else
    HAVE_ERROR=0
    case "$gl_cv_onwards_func_error" in
      future*) REPLACE_ERROR=1 ;;
    esac
  fi

  dnl We don't use AC_FUNC_ERROR_AT_LINE any more, because it is no longer
  dnl maintained in Autoconf and because it invokes AC_LIBOBJ.
  dnl We need to notice a missing declaration, like gl_CHECK_FUNCS_ANDROID does.
  AC_CHECK_DECL([error_at_line], , , [[#include <error.h>]])
  if test $ac_cv_have_decl_error_at_line = yes; then
    AC_CACHE_CHECK([for error_at_line], [ac_cv_lib_error_at_line],
      [AC_LINK_IFELSE(
         [AC_LANG_PROGRAM(
            [[#include <error.h>]],
            [[error_at_line (0, 0, "", 0, "an error occurred");]])],
         [ac_cv_lib_error_at_line=yes],
         [ac_cv_lib_error_at_line=no])])
  else
    ac_cv_lib_error_at_line=no
  fi
  if test $ac_cv_lib_error_at_line = yes; then
    HAVE_ERROR_AT_LINE=1
  else
    HAVE_ERROR_AT_LINE=0
  fi
  REPLACE_ERROR_AT_LINE=0

  if test $ac_cv_func_error = yes && test $ac_cv_lib_error_at_line = yes; then
    dnl On Android 11, when error_print_progname is set, the output of the
    dnl error() function contains an extra space.
    AC_CACHE_CHECK([for working error function],
      [gl_cv_func_working_error],
      [if test $cross_compiling != yes; then
         AC_LINK_IFELSE(
           [AC_LANG_PROGRAM([[
              #include <error.h>
              static void print_no_progname (void) {}
            ]], [[
              error_print_progname = print_no_progname;
              error (0, 0, "foo");
            ]])
           ],
           [rm -f conftest.out
            if test -s conftest$ac_exeext \
               && ./conftest$ac_exeext 2> conftest.out; then
              if grep ' ' conftest.out >/dev/null; then
                gl_cv_func_working_error=no
              else
                gl_cv_func_working_error=yes
              fi
            else
              gl_cv_func_working_error=no
            fi
            rm -f conftest.out
           ],
           [gl_cv_func_working_error=no])
       else
         AC_COMPILE_IFELSE(
           [AC_LANG_PROGRAM([[
              #include <error.h>
            ]], [[
              error (0, 0, "foo");
            ]])
           ],
           [case "$host_os" in
                               # Guess yes on glibc systems.
              *-gnu* | gnu*)   gl_cv_func_working_error="guessing yes" ;;
                               # Guess no on Android.
              linux*-android*) gl_cv_func_working_error="guessing no" ;;
                               # If we don't know, obey --enable-cross-guesses.
              *)               gl_cv_func_working_error="$gl_cross_guess_normal" ;;
            esac
           ],
           [gl_cv_func_working_error=no])
       fi
      ])
    case "$gl_cv_func_working_error" in
      *no)
        REPLACE_ERROR=1
        REPLACE_ERROR_AT_LINE=1
        ;;
    esac
  fi

  m4_ifdef([gl_HAVE_MODULE_VERROR],
    [COMPILE_ERROR_C=1],
    [if test $HAVE_ERROR = 0 || test $REPLACE_ERROR = 1 \
        || test $HAVE_ERROR_AT_LINE = 0 \
        || test $REPLACE_ERROR_AT_LINE = 1; then
       COMPILE_ERROR_C=1
     else
       COMPILE_ERROR_C=0
     fi])

  AC_SUBST([HAVE_ERROR])
  AC_SUBST([HAVE_ERROR_AT_LINE])
  AC_SUBST([REPLACE_ERROR])
  AC_SUBST([REPLACE_ERROR_AT_LINE])
])