Debian Codegraph @

m4/canonicalize.m4

raw
1 # canonicalize.m4
2 # serial 40
3
4 dnl Copyright (C) 2003-2007, 2009-2024 Free Software Foundation, Inc.
5
6 dnl This file is free software; the Free Software Foundation
7 dnl gives unlimited permission to copy and/or distribute it,
8 dnl with or without modifications, as long as this notice is preserved.
9 dnl This file is offered as-is, without any warranty.
10
11 # Provides canonicalize_file_name and canonicalize_filename_mode, but does
12 # not provide or fix realpath.
13 AC_DEFUN([gl_FUNC_CANONICALIZE_FILENAME_MODE],
14 [
15 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16 AC_REQUIRE([gl_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
17 AC_CHECK_FUNCS_ONCE([canonicalize_file_name])
18 gl_CHECK_FUNCS_ANDROID([faccessat], [[#include <unistd.h>]])
19 AC_REQUIRE([gl_DOUBLE_SLASH_ROOT])
20 AC_REQUIRE([gl_FUNC_REALPATH_WORKS])
21 if test $ac_cv_func_canonicalize_file_name = no; then
22 HAVE_CANONICALIZE_FILE_NAME=0
23 else
24 case "$gl_cv_func_realpath_works" in
25 *yes) ;;
26 *) REPLACE_CANONICALIZE_FILE_NAME=1 ;;
27 esac
28 fi
29 ])
30
31 # Provides canonicalize_file_name and realpath.
32 AC_DEFUN([gl_CANONICALIZE_LGPL],
33 [
34 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
35 AC_REQUIRE([gl_CANONICALIZE_LGPL_SEPARATE])
36 if test $ac_cv_func_canonicalize_file_name = no; then
37 HAVE_CANONICALIZE_FILE_NAME=0
38 if test $ac_cv_func_realpath = no; then
39 HAVE_REALPATH=0
40 else
41 case "$gl_cv_func_realpath_works" in
42 *yes) ;;
43 *) REPLACE_REALPATH=1 ;;
44 esac
45 fi
46 else
47 case "$gl_cv_func_realpath_works" in
48 *yes)
49 ;;
50 *)
51 REPLACE_CANONICALIZE_FILE_NAME=1
52 REPLACE_REALPATH=1
53 ;;
54 esac
55 fi
56 ])
57
58 # Like gl_CANONICALIZE_LGPL, except prepare for separate compilation
59 # (no REPLACE_CANONICALIZE_FILE_NAME, no REPLACE_REALPATH, no AC_LIBOBJ).
60 AC_DEFUN([gl_CANONICALIZE_LGPL_SEPARATE],
61 [
62 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
63 AC_REQUIRE([gl_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
64 AC_CHECK_FUNCS_ONCE([canonicalize_file_name])
65 gl_CHECK_FUNCS_ANDROID([faccessat], [[#include <unistd.h>]])
66
67 dnl On native Windows, we use _getcwd(), regardless whether getcwd() is
68 dnl available through the linker option '-loldnames'.
69 AC_REQUIRE([AC_CANONICAL_HOST])
70 case "$host_os" in
71 mingw* | windows*) ;;
72 *) AC_CHECK_FUNCS([getcwd]) ;;
73 esac
74
75 AC_REQUIRE([gl_DOUBLE_SLASH_ROOT])
76 AC_REQUIRE([gl_FUNC_REALPATH_WORKS])
77 AC_CHECK_HEADERS_ONCE([sys/param.h])
78 ])
79
80 # Check whether realpath works. Assume that if a platform has both
81 # realpath and canonicalize_file_name, but the former is broken, then
82 # so is the latter.
83 AC_DEFUN([gl_FUNC_REALPATH_WORKS],
84 [
85 AC_CHECK_FUNCS_ONCE([realpath lstat])
86 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
87 AC_CACHE_CHECK([whether realpath works], [gl_cv_func_realpath_works], [
88 rm -rf conftest.a conftest.d
89 touch conftest.a
90 # Assume that if we have lstat, we can also check symlinks.
91 if test $ac_cv_func_lstat = yes; then
92 ln -s conftest.a conftest.l
93 fi
94 mkdir conftest.d
95 AC_RUN_IFELSE([
96 AC_LANG_PROGRAM([[
97 ]GL_NOCRASH[
98 #include <errno.h>
99 #include <stdlib.h>
100 #include <string.h>
101 ]], [[
102 int result = 0;
103 /* This test fails on Solaris 10. */
104 {
105 char *name = realpath ("conftest.a", NULL);
106 if (!(name && *name == '/'))
107 result |= 1;
108 free (name);
109 }
110 /* This test fails on older versions of Cygwin. */
111 {
112 char *name = realpath ("conftest.b/../conftest.a", NULL);
113 if (name != NULL)
114 result |= 2;
115 free (name);
116 }
117 /* This test fails on macOS 14, Cygwin 2.9. */
118 #if HAVE_LSTAT
119 {
120 char *name = realpath ("conftest.l/../conftest.a", NULL);
121 if (name != NULL || errno != ENOTDIR)
122 result |= 4;
123 free (name);
124 }
125 #endif
126 /* This test fails on macOS 14, OpenBSD 6.0. */
127 {
128 char *name = realpath ("conftest.a/", NULL);
129 if (name != NULL)
130 result |= 8;
131 free (name);
132 }
133 /* This test fails on AIX 7, Solaris 10. */
134 {
135 char *name1 = realpath (".", NULL);
136 char *name2 = realpath ("conftest.d//./..", NULL);
137 if (! name1 || ! name2 || strcmp (name1, name2))
138 result |= 16;
139 free (name1);
140 free (name2);
141 }
142 #ifdef __linux__
143 /* On Linux, // is the same as /. See also double-slash-root.m4.
144 realpath() should respect this.
145 This test fails on musl libc 1.2.2. */
146 {
147 char *name = realpath ("//", NULL);
148 if (! name || strcmp (name, "/"))
149 result |= 32;
150 free (name);
151 }
152 #endif
153 return result;
154 ]])
155 ],
156 [gl_cv_func_realpath_works=yes],
157 [case $? in
158 32) gl_cv_func_realpath_works=nearly ;;
159 *) gl_cv_func_realpath_works=no ;;
160 esac
161 ],
162 [case "$host_os" in
163 # Guess yes on glibc systems.
164 *-gnu* | gnu*) gl_cv_func_realpath_works="guessing yes" ;;
165 # Guess 'nearly' on musl systems.
166 *-musl*) gl_cv_func_realpath_works="guessing nearly" ;;
167 # Guess no on macOS.
168 darwin*) gl_cv_func_realpath_works="guessing no" ;;
169 # Guess no on Cygwin.
170 cygwin*) gl_cv_func_realpath_works="guessing no" ;;
171 # Guess no on native Windows.
172 mingw* | windows*) gl_cv_func_realpath_works="guessing no" ;;
173 # If we don't know, obey --enable-cross-guesses.
174 *) gl_cv_func_realpath_works="$gl_cross_guess_normal" ;;
175 esac
176 ])
177 rm -rf conftest.a conftest.l conftest.d
178 ])
179 case "$gl_cv_func_realpath_works" in
180 *yes)
181 AC_DEFINE([FUNC_REALPATH_WORKS], [1],
182 [Define to 1 if realpath() can malloc memory, always gives an absolute path, and handles leading slashes and a trailing slash correctly.])
183 ;;
184 *nearly)
185 AC_DEFINE([FUNC_REALPATH_NEARLY_WORKS], [1],
186 [Define to 1 if realpath() can malloc memory, always gives an absolute path, and handles a trailing slash correctly.])
187 ;;
188 esac
189 ])