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