canonicalize.m4 4.4 KB

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