immutable: Add tests.
[gnulib.git] / m4 / canonicalize.m4
blob475fa15d6bdf5a95412b89c987bf02680bd0308f
1 # canonicalize.m4 serial 35
3 dnl Copyright (C) 2003-2007, 2009-2021 Free Software Foundation, Inc.
5 dnl This file is free software; the Free Software Foundation
6 dnl gives unlimited permission to copy and/or distribute it,
7 dnl with or without modifications, as long as this notice is preserved.
9 # Provides canonicalize_file_name and canonicalize_filename_mode, but does
10 # not provide or fix realpath.
11 AC_DEFUN([gl_FUNC_CANONICALIZE_FILENAME_MODE],
13   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
14   AC_REQUIRE([gl_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
15   AC_CHECK_FUNCS_ONCE([canonicalize_file_name faccessat])
16   AC_REQUIRE([gl_DOUBLE_SLASH_ROOT])
17   AC_REQUIRE([gl_FUNC_REALPATH_WORKS])
18   if test $ac_cv_func_canonicalize_file_name = no; then
19     HAVE_CANONICALIZE_FILE_NAME=0
20   else
21     case "$gl_cv_func_realpath_works" in
22       *yes) ;;
23       *)    REPLACE_CANONICALIZE_FILE_NAME=1 ;;
24     esac
25   fi
28 # Provides canonicalize_file_name and realpath.
29 AC_DEFUN([gl_CANONICALIZE_LGPL],
31   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
32   AC_REQUIRE([gl_CANONICALIZE_LGPL_SEPARATE])
33   if test $ac_cv_func_canonicalize_file_name = no; then
34     HAVE_CANONICALIZE_FILE_NAME=0
35     if test $ac_cv_func_realpath = no; then
36       HAVE_REALPATH=0
37     else
38       case "$gl_cv_func_realpath_works" in
39         *yes) ;;
40         *)    REPLACE_REALPATH=1 ;;
41       esac
42     fi
43   else
44     case "$gl_cv_func_realpath_works" in
45       *yes)
46         ;;
47       *)
48         REPLACE_CANONICALIZE_FILE_NAME=1
49         REPLACE_REALPATH=1
50         ;;
51     esac
52   fi
55 # Like gl_CANONICALIZE_LGPL, except prepare for separate compilation
56 # (no REPLACE_CANONICALIZE_FILE_NAME, no REPLACE_REALPATH, no AC_LIBOBJ).
57 AC_DEFUN([gl_CANONICALIZE_LGPL_SEPARATE],
59   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
60   AC_REQUIRE([gl_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
61   AC_CHECK_FUNCS_ONCE([canonicalize_file_name faccessat])
63   dnl On native Windows, we use _getcwd(), regardless whether getcwd() is
64   dnl available through the linker option '-loldnames'.
65   AC_REQUIRE([AC_CANONICAL_HOST])
66   case "$host_os" in
67     mingw*) ;;
68     *)      AC_CHECK_FUNCS([getcwd]) ;;
69   esac
71   AC_REQUIRE([gl_DOUBLE_SLASH_ROOT])
72   AC_REQUIRE([gl_FUNC_REALPATH_WORKS])
73   AC_CHECK_HEADERS_ONCE([sys/param.h])
76 # Check whether realpath works.  Assume that if a platform has both
77 # realpath and canonicalize_file_name, but the former is broken, then
78 # so is the latter.
79 AC_DEFUN([gl_FUNC_REALPATH_WORKS],
81   AC_CHECK_FUNCS_ONCE([realpath])
82   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
83   AC_CACHE_CHECK([whether realpath works], [gl_cv_func_realpath_works], [
84     rm -rf conftest.a conftest.d
85     touch conftest.a
86     mkdir conftest.d
87     AC_RUN_IFELSE([
88       AC_LANG_PROGRAM([[
89         ]GL_NOCRASH[
90         #include <stdlib.h>
91         #include <string.h>
92       ]], [[
93         int result = 0;
94         {
95           char *name = realpath ("conftest.a", NULL);
96           if (!(name && *name == '/'))
97             result |= 1;
98           free (name);
99         }
100         {
101           char *name = realpath ("conftest.b/../conftest.a", NULL);
102           if (name != NULL)
103             result |= 2;
104           free (name);
105         }
106         {
107           char *name = realpath ("conftest.a/", NULL);
108           if (name != NULL)
109             result |= 4;
110           free (name);
111         }
112         {
113           char *name1 = realpath (".", NULL);
114           char *name2 = realpath ("conftest.d//./..", NULL);
115           if (! name1 || ! name2 || strcmp (name1, name2))
116             result |= 8;
117           free (name1);
118           free (name2);
119         }
120         return result;
121       ]])
122      ],
123      [gl_cv_func_realpath_works=yes],
124      [gl_cv_func_realpath_works=no],
125      [case "$host_os" in
126                        # Guess yes on glibc systems.
127         *-gnu* | gnu*) gl_cv_func_realpath_works="guessing yes" ;;
128                        # Guess yes on musl systems.
129         *-musl*)       gl_cv_func_realpath_works="guessing yes" ;;
130                        # Guess no on native Windows.
131         mingw*)        gl_cv_func_realpath_works="guessing no" ;;
132                        # If we don't know, obey --enable-cross-guesses.
133         *)             gl_cv_func_realpath_works="$gl_cross_guess_normal" ;;
134       esac
135      ])
136     rm -rf conftest.a conftest.d
137   ])
138   case "$gl_cv_func_realpath_works" in
139     *yes)
140       AC_DEFINE([FUNC_REALPATH_WORKS], [1], [Define to 1 if realpath()
141         can malloc memory, always gives an absolute path, and handles
142         trailing slash correctly.])
143       ;;
144   esac