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