1 #
   2 # Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
   3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4 #
   5 # This code is free software; you can redistribute it and/or modify it
   6 # under the terms of the GNU General Public License version 2 only, as
   7 # published by the Free Software Foundation.  Oracle designates this
   8 # particular file as subject to the "Classpath" exception as provided
   9 # by Oracle in the LICENSE file that accompanied this code.
  10 #
  11 # This code is distributed in the hope that it will be useful, but WITHOUT
  12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14 # version 2 for more details (a copy is included in the LICENSE file that
  15 # accompanied this code).
  16 #
  17 # You should have received a copy of the GNU General Public License version
  18 # 2 along with this work; if not, write to the Free Software Foundation,
  19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20 #
  21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22 # or visit www.oracle.com if you need additional information or have any
  23 # questions.
  24 #
  25 
  26 AC_DEFUN([UTIL_REWRITE_AS_UNIX_PATH],
  27 [
  28   windows_path="[$]$1"
  29   if test "x$OPENJDK_BUILD_OS_ENV" = "xwindows.cygwin"; then
  30     unix_path=`$CYGPATH -u "$windows_path"`
  31     $1="$unix_path"
  32   elif test "x$OPENJDK_BUILD_OS_ENV" = "xwindows.msys"; then
  33     unix_path=`$ECHO "$windows_path" | $SED -e 's,^\\(.\\):,/\\1,g' -e 's,\\\\,/,g'`
  34     $1="$unix_path"
  35   elif test "x$OPENJDK_BUILD_OS_ENV" = "xwindows.wsl"; then
  36     # wslpath does not check the input, only call if an actual windows path was
  37     # given.
  38     if $ECHO "$windows_path" | $GREP -q ["^[a-zA-Z]:[\\\\/]"]; then
  39       unix_path=`$WSLPATH -u "$windows_path"`
  40       $1="$unix_path"
  41     fi
  42   fi
  43 ])
  44 
  45 AC_DEFUN([UTIL_REWRITE_AS_WINDOWS_MIXED_PATH],
  46 [
  47   unix_path="[$]$1"
  48   if test "x$OPENJDK_BUILD_OS_ENV" = "xwindows.cygwin"; then
  49     windows_path=`$CYGPATH -m "$unix_path"`
  50     $1="$windows_path"
  51   elif test "x$OPENJDK_BUILD_OS_ENV" = "xwindows.msys"; then
  52     windows_path=`cmd //c echo $unix_path`
  53     $1="$windows_path"
  54   elif test "x$OPENJDK_BUILD_OS_ENV" = "xwindows.wsl"; then
  55     windows_path=`$WSLPATH -m "$unix_path"`
  56     $1="$windows_path"
  57   fi
  58 ])
  59 
  60 # Helper function which possibly converts a path using DOS-style short mode.
  61 # If so, the updated path is stored in $new_path.
  62 # $1: The path to check
  63 AC_DEFUN([UTIL_MAKE_WINDOWS_SPACE_SAFE_CYGWIN],
  64 [
  65   input_path="$1"
  66   # Check if we need to convert this using DOS-style short mode. If the path
  67   # contains just simple characters, use it. Otherwise (spaces, weird characters),
  68   # take no chances and rewrite it.
  69   # Note: m4 eats our [], so we need to use @<:@ and @:>@ instead.
  70   has_forbidden_chars=`$ECHO "$input_path" | $GREP @<:@^-._/a-zA-Z0-9@:>@`
  71   if test "x$has_forbidden_chars" != x; then
  72     # Now convert it to mixed DOS-style, short mode (no spaces, and / instead of \)
  73     shortmode_path=`$CYGPATH -s -m -a "$input_path"`
  74     path_after_shortmode=`$CYGPATH -u "$shortmode_path"`
  75     if test "x$path_after_shortmode" != "x$input_to_shortpath"; then
  76       # Going to short mode and back again did indeed matter. Since short mode is
  77       # case insensitive, let's make it lowercase to improve readability.
  78       shortmode_path=`$ECHO "$shortmode_path" | $TR 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
  79       # Now convert it back to Unix-style (cygpath)
  80       input_path=`$CYGPATH -u "$shortmode_path"`
  81       new_path="$input_path"
  82     fi
  83   fi
  84 
  85   test_cygdrive_prefix=`$ECHO $input_path | $GREP ^/cygdrive/`
  86   if test "x$test_cygdrive_prefix" = x; then
  87     # As a simple fix, exclude /usr/bin since it's not a real path.
  88     if test "x`$ECHO $1 | $GREP ^/usr/bin/`" = x; then
  89       # The path is in a Cygwin special directory (e.g. /home). We need this converted to
  90       # a path prefixed by /cygdrive for fixpath to work.
  91       new_path="$CYGWIN_ROOT_PATH$input_path"
  92     fi
  93   fi
  94 ])
  95 
  96 # Helper function which possibly converts a path using DOS-style short mode.
  97 # If so, the updated path is stored in $new_path.
  98 # $1: The path to check
  99 AC_DEFUN([UTIL_MAKE_WINDOWS_SPACE_SAFE_MSYS],
 100 [
 101   input_path="$1"
 102   # Check if we need to convert this using DOS-style short mode. If the path
 103   # contains just simple characters, use it. Otherwise (spaces, weird characters),
 104   # take no chances and rewrite it.
 105   # Note: m4 eats our [], so we need to use @<:@ and @:>@ instead.
 106   has_forbidden_chars=`$ECHO "$input_path" | $GREP @<:@^-_/:a-zA-Z0-9@:>@`
 107   if test "x$has_forbidden_chars" != x; then
 108     # Now convert it to mixed DOS-style, short mode (no spaces, and / instead of \)
 109     new_path=`cmd /c "for %A in (\"$input_path\") do @echo %~sA"|$TR \\\\\\\\ / | $TR 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
 110   fi
 111 ])
 112 
 113 # Helper function which possibly converts a path using DOS-style short mode.
 114 # If so, the updated path is stored in $new_path.
 115 # $1: The path to check
 116 AC_DEFUN([UTIL_MAKE_WINDOWS_SPACE_SAFE_WSL],
 117 [
 118   input_path="$1"
 119   # Check if we need to convert this using DOS-style short mode. If the path
 120   # contains just simple characters, use it. Otherwise (spaces, weird characters),
 121   # take no chances and rewrite it.
 122   # Note: m4 eats our [], so we need to use @<:@ and @:>@ instead.
 123   has_forbidden_chars=`$ECHO "$input_path" | $GREP [[^-_/:a-zA-Z0-9\\.]]`
 124   if test "x$has_forbidden_chars" != x; then
 125     # Now convert it to mixed DOS-style, short mode (no spaces, and / instead of \)
 126     TOPDIR_windows="$TOPDIR"
 127     UTIL_REWRITE_AS_WINDOWS_MIXED_PATH([TOPDIR_windows])
 128     # First convert to Windows path to make input valid for cmd
 129     UTIL_REWRITE_AS_WINDOWS_MIXED_PATH([input_path])
 130     # Reset PATH since it can contain a mix of WSL/linux paths and Windows paths from VS,
 131     # which, in combination with WSLENV, will make the WSL layer complain
 132     old_path="$PATH"
 133     PATH=
 134     new_path=`$CMD /c $TOPDIR_windows/make/scripts/windowsShortName.bat "$input_path" \
 135         | $SED -e 's|\r||g' \
 136         | $TR \\\\\\\\ / | $TR 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
 137     # Rewrite back to unix style
 138     PATH="$old_path"
 139     UTIL_REWRITE_AS_UNIX_PATH([new_path])
 140   fi
 141 ])
 142 
 143 # FIXME: The UTIL_FIXUP_*_CYGWIN/MSYS is most likely too convoluted
 144 # and could probably be heavily simplified. However, all changes in this
 145 # area tend to need lot of testing in different scenarios, and in lack of
 146 # proper unit testing, cleaning this up has not been deemed worth the effort
 147 # at the moment.
 148 
 149 AC_DEFUN([UTIL_FIXUP_PATH_CYGWIN],
 150 [
 151   # Input might be given as Windows format, start by converting to
 152   # unix format.
 153   path="[$]$1"
 154   new_path=`$CYGPATH -u "$path"`
 155 
 156   UTIL_ABSOLUTE_PATH(new_path)
 157 
 158   # Cygwin tries to hide some aspects of the Windows file system, such that binaries are
 159   # named .exe but called without that suffix. Therefore, "foo" and "foo.exe" are considered
 160   # the same file, most of the time (as in "test -f"). But not when running cygpath -s, then
 161   # "foo.exe" is OK but "foo" is an error.
 162   #
 163   # This test is therefore slightly more accurate than "test -f" to check for file precense.
 164   # It is also a way to make sure we got the proper file name for the real test later on.
 165   test_shortpath=`$CYGPATH -s -m "$new_path" 2> /dev/null`
 166   if test "x$test_shortpath" = x; then
 167     AC_MSG_NOTICE([The path of $1, which resolves as "$path", is invalid.])
 168     AC_MSG_ERROR([Cannot locate the the path of $1])
 169   fi
 170 
 171   # Call helper function which possibly converts this using DOS-style short mode.
 172   # If so, the updated path is stored in $new_path.
 173   UTIL_MAKE_WINDOWS_SPACE_SAFE_CYGWIN([$new_path])
 174 
 175   if test "x$path" != "x$new_path"; then
 176     $1="$new_path"
 177     AC_MSG_NOTICE([Rewriting $1 to "$new_path"])
 178   fi
 179 ])
 180 
 181 AC_DEFUN([UTIL_FIXUP_PATH_MSYS],
 182 [
 183   path="[$]$1"
 184   has_colon=`$ECHO $path | $GREP ^.:`
 185   new_path="$path"
 186   if test "x$has_colon" = x; then
 187     # Not in mixed or Windows style, start by that.
 188     new_path=`cmd //c echo $path`
 189   fi
 190 
 191   UTIL_ABSOLUTE_PATH(new_path)
 192 
 193   UTIL_MAKE_WINDOWS_SPACE_SAFE_MSYS([$new_path])
 194   UTIL_REWRITE_AS_UNIX_PATH(new_path)
 195   if test "x$path" != "x$new_path"; then
 196     $1="$new_path"
 197     AC_MSG_NOTICE([Rewriting $1 to "$new_path"])
 198   fi
 199 
 200   # Save the first 10 bytes of this path to the storage, so fixpath can work.
 201   all_fixpath_prefixes=("${all_fixpath_prefixes@<:@@@:>@}" "${new_path:0:10}")
 202 ])
 203 
 204 AC_DEFUN([UTIL_FIXUP_PATH_WSL],
 205 [
 206   # Input might be given as Windows format, start by converting to
 207   # unix format.
 208   new_path="[$]$1"
 209   UTIL_REWRITE_AS_UNIX_PATH([new_path])
 210 
 211   UTIL_ABSOLUTE_PATH(new_path)
 212 
 213   # Call helper function which possibly converts this using DOS-style short mode.
 214   # If so, the updated path is stored in $new_path.
 215   UTIL_MAKE_WINDOWS_SPACE_SAFE_WSL([$new_path])
 216 
 217   if test "x$path" != "x$new_path"; then
 218     $1="$new_path"
 219     AC_MSG_NOTICE([Rewriting $1 to "$new_path"])
 220   fi
 221 ])
 222 
 223 AC_DEFUN([UTIL_FIXUP_EXECUTABLE_CYGWIN],
 224 [
 225   # First separate the path from the arguments. This will split at the first
 226   # space.
 227   complete="[$]$1"
 228   path="${complete%% *}"
 229   tmp="$complete EOL"
 230   arguments="${tmp#* }"
 231 
 232   # Input might be given as Windows format, start by converting to
 233   # unix format.
 234   new_path=`$CYGPATH -u "$path"`
 235 
 236   # Now try to locate executable using which
 237   new_path=`$WHICH "$new_path" 2> /dev/null`
 238   # bat and cmd files are not always considered executable in cygwin causing which
 239   # to not find them
 240   if test "x$new_path" = x \
 241       && test "x`$ECHO \"$path\" | $GREP -i -e \"\\.bat$\" -e \"\\.cmd$\"`" != x \
 242       && test "x`$LS \"$path\" 2>/dev/null`" != x; then
 243     new_path=`$CYGPATH -u "$path"`
 244   fi
 245   if test "x$new_path" = x; then
 246     # Oops. Which didn't find the executable.
 247     # The splitting of arguments from the executable at a space might have been incorrect,
 248     # since paths with space are more likely in Windows. Give it another try with the whole
 249     # argument.
 250     path="$complete"
 251     arguments="EOL"
 252     new_path=`$CYGPATH -u "$path"`
 253     new_path=`$WHICH "$new_path" 2> /dev/null`
 254     # bat and cmd files are not always considered executable in cygwin causing which
 255     # to not find them
 256     if test "x$new_path" = x \
 257         && test "x`$ECHO \"$path\" | $GREP -i -e \"\\.bat$\" -e \"\\.cmd$\"`" != x \
 258         && test "x`$LS \"$path\" 2>/dev/null`" != x; then
 259       new_path=`$CYGPATH -u "$path"`
 260     fi
 261     if test "x$new_path" = x; then
 262       # It's still not found. Now this is an unrecoverable error.
 263       AC_MSG_NOTICE([The path of $1, which resolves as "$complete", is not found.])
 264       has_space=`$ECHO "$complete" | $GREP " "`
 265       if test "x$has_space" != x; then
 266         AC_MSG_NOTICE([You might be mixing spaces in the path and extra arguments, which is not allowed.])
 267       fi
 268       AC_MSG_ERROR([Cannot locate the the path of $1])
 269     fi
 270   fi
 271 
 272   # Cygwin tries to hide some aspects of the Windows file system, such that binaries are
 273   # named .exe but called without that suffix. Therefore, "foo" and "foo.exe" are considered
 274   # the same file, most of the time (as in "test -f"). But not when running cygpath -s, then
 275   # "foo.exe" is OK but "foo" is an error.
 276   #
 277   # This test is therefore slightly more accurate than "test -f" to check for file presence.
 278   # It is also a way to make sure we got the proper file name for the real test later on.
 279   test_shortpath=`$CYGPATH -s -m "$new_path" 2> /dev/null`
 280   if test "x$test_shortpath" = x; then
 281     # Short path failed, file does not exist as specified.
 282     # Try adding .exe or .cmd
 283     if test -f "${new_path}.exe"; then
 284       input_to_shortpath="${new_path}.exe"
 285     elif test -f "${new_path}.cmd"; then
 286       input_to_shortpath="${new_path}.cmd"
 287     else
 288       AC_MSG_NOTICE([The path of $1, which resolves as "$new_path", is invalid.])
 289       AC_MSG_NOTICE([Neither "$new_path" nor "$new_path.exe/cmd" can be found])
 290       AC_MSG_ERROR([Cannot locate the the path of $1])
 291     fi
 292   else
 293     input_to_shortpath="$new_path"
 294   fi
 295 
 296   # Call helper function which possibly converts this using DOS-style short mode.
 297   # If so, the updated path is stored in $new_path.
 298   new_path="$input_to_shortpath"
 299   UTIL_MAKE_WINDOWS_SPACE_SAFE_CYGWIN([$input_to_shortpath])
 300   # remove trailing .exe if any
 301   new_path="${new_path/%.exe/}"
 302 ])
 303 
 304 AC_DEFUN([UTIL_FIXUP_EXECUTABLE_MSYS],
 305 [
 306   # First separate the path from the arguments. This will split at the first
 307   # space.
 308   complete="[$]$1"
 309   path="${complete%% *}"
 310   tmp="$complete EOL"
 311   arguments="${tmp#* }"
 312 
 313   # Input might be given as Windows format, start by converting to
 314   # unix format.
 315   new_path="$path"
 316   UTIL_REWRITE_AS_UNIX_PATH(new_path)
 317 
 318   # Now try to locate executable using which
 319   new_path=`$WHICH "$new_path" 2> /dev/null`
 320 
 321   if test "x$new_path" = x; then
 322     # Oops. Which didn't find the executable.
 323     # The splitting of arguments from the executable at a space might have been incorrect,
 324     # since paths with space are more likely in Windows. Give it another try with the whole
 325     # argument.
 326     path="$complete"
 327     arguments="EOL"
 328     new_path="$path"
 329     UTIL_REWRITE_AS_UNIX_PATH(new_path)
 330 
 331     new_path=`$WHICH "$new_path" 2> /dev/null`
 332     # bat and cmd files are not always considered executable in MSYS causing which
 333     # to not find them
 334     if test "x$new_path" = x \
 335         && test "x`$ECHO \"$path\" | $GREP -i -e \"\\.bat$\" -e \"\\.cmd$\"`" != x \
 336         && test "x`$LS \"$path\" 2>/dev/null`" != x; then
 337       new_path="$path"
 338       UTIL_REWRITE_AS_UNIX_PATH(new_path)
 339     fi
 340 
 341     if test "x$new_path" = x; then
 342       # It's still not found. Now this is an unrecoverable error.
 343       AC_MSG_NOTICE([The path of $1, which resolves as "$complete", is not found.])
 344       has_space=`$ECHO "$complete" | $GREP " "`
 345       if test "x$has_space" != x; then
 346         AC_MSG_NOTICE([You might be mixing spaces in the path and extra arguments, which is not allowed.])
 347       fi
 348       AC_MSG_ERROR([Cannot locate the the path of $1])
 349     fi
 350   fi
 351 
 352   # Now new_path has a complete unix path to the binary
 353   if test "x`$ECHO $new_path | $GREP ^/bin/`" != x; then
 354     # Keep paths in /bin as-is, but remove trailing .exe if any
 355     new_path="${new_path/%.exe/}"
 356     # Do not save /bin paths to all_fixpath_prefixes!
 357   else
 358     # Not in mixed or Windows style, start by that.
 359     new_path=`cmd //c echo $new_path`
 360     UTIL_MAKE_WINDOWS_SPACE_SAFE_MSYS([$new_path])
 361     # Output is in $new_path
 362     UTIL_REWRITE_AS_UNIX_PATH(new_path)
 363     # remove trailing .exe if any
 364     new_path="${new_path/%.exe/}"
 365 
 366     # Save the first 10 bytes of this path to the storage, so fixpath can work.
 367     all_fixpath_prefixes=("${all_fixpath_prefixes@<:@@@:>@}" "${new_path:0:10}")
 368   fi
 369 ])
 370 
 371 AC_DEFUN([UTIL_FIXUP_EXECUTABLE_WSL],
 372 [
 373   # First separate the path from the arguments. This will split at the first
 374   # space.
 375   complete="[$]$1"
 376   path="${complete%% *}"
 377   tmp="$complete EOL"
 378   arguments="${tmp#* }"
 379 
 380   # Input might be given as Windows format, start by converting to
 381   # unix format.
 382   new_path="$path"
 383   UTIL_REWRITE_AS_UNIX_PATH([new_path])
 384 
 385   # Now try to locate executable using which
 386   new_path_bak="$new_path"
 387   new_path=`$WHICH "$new_path" 2> /dev/null`
 388   # bat and cmd files are not considered executable in WSL
 389   if test "x$new_path" = x \
 390       && test "x`$ECHO \"$path\" | $GREP -i -e \"\\.bat$\" -e \"\\.cmd$\"`" != x \
 391       && test "x`$LS \"$path\" 2>/dev/null`" != x; then
 392     new_path="$new_path_back"
 393   fi
 394   if test "x$new_path" = x; then
 395     # Oops. Which didn't find the executable.
 396     # The splitting of arguments from the executable at a space might have been incorrect,
 397     # since paths with space are more likely in Windows. Give it another try with the whole
 398     # argument.
 399     path="$complete"
 400     arguments="EOL"
 401     new_path="$path"
 402     UTIL_REWRITE_AS_UNIX_PATH([new_path])
 403     new_path_bak="$new_path"
 404     new_path=`$WHICH "$new_path" 2> /dev/null`
 405     # bat and cmd files are not considered executable in WSL
 406     if test "x$new_path" = x \
 407         && test "x`$ECHO \"$path\" | $GREP -i -e \"\\.bat$\" -e \"\\.cmd$\"`" != x \
 408         && test "x`$LS \"$path\" 2>/dev/null`" != x; then
 409       new_path="$new_path_bak"
 410     fi
 411     if test "x$new_path" = x; then
 412       # It's still not found. Now this is an unrecoverable error.
 413       AC_MSG_NOTICE([The path of $1, which resolves as "$complete", is not found.])
 414       has_space=`$ECHO "$complete" | $GREP " "`
 415       if test "x$has_space" != x; then
 416         AC_MSG_NOTICE([You might be mixing spaces in the path and extra arguments, which is not allowed.])
 417       fi
 418       AC_MSG_ERROR([Cannot locate the the path of $1])
 419     fi
 420   fi
 421 
 422   # In WSL, suffixes must be present for Windows executables
 423   if test ! -f "$new_path"; then
 424     # Try adding .exe or .cmd
 425     if test -f "${new_path}.exe"; then
 426       input_to_shortpath="${new_path}.exe"
 427     elif test -f "${new_path}.cmd"; then
 428       input_to_shortpath="${new_path}.cmd"
 429     else
 430       AC_MSG_NOTICE([The path of $1, which resolves as "$new_path", is invalid.])
 431       AC_MSG_NOTICE([Neither "$new_path" nor "$new_path.exe/cmd" can be found])
 432       AC_MSG_ERROR([Cannot locate the the path of $1])
 433     fi
 434   else
 435     input_to_shortpath="$new_path"
 436   fi
 437 
 438   # Call helper function which possibly converts this using DOS-style short mode.
 439   # If so, the updated path is stored in $new_path.
 440   new_path="$input_to_shortpath"
 441   UTIL_MAKE_WINDOWS_SPACE_SAFE_WSL([$input_to_shortpath])
 442 ])
 443