2
# Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
3
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
26
###############################################################################
27
# Appends a string to a path variable, only adding the : when needed.
28
AC_DEFUN([UTIL_APPEND_TO_PATH],
30
if test "x$2" != x; then
31
if test "x[$]$1" = x; then
39
###############################################################################
40
# Prepends a string to a path variable, only adding the : when needed.
41
AC_DEFUN([UTIL_PREPEND_TO_PATH],
43
if test "x$2" != x; then
44
if test "x[$]$1" = x; then
52
###############################################################################
53
# This will make sure the given variable points to a full and proper
55
# 1) There will be no spaces in the path. On unix platforms,
56
# spaces in the path will result in an error. On Windows,
57
# the path will be rewritten using short-style to be space-free.
58
# 2) The path will be absolute, and it will be in unix-style (on
60
# $1: The name of the variable to fix
61
# $2: if NOFAIL, errors will be silently ignored
62
AC_DEFUN([UTIL_FIXUP_PATH],
64
# Only process if variable expands to non-empty
66
if test "x$path" != x; then
67
if test "x$OPENJDK_BUILD_OS" = "xwindows"; then
68
if test "x$2" = "xNOFAIL"; then
71
imported_path=`$FIXPATH_BASE $quiet_option import "$path"`
72
$FIXPATH_BASE verify "$imported_path"
73
if test $? -ne 0; then
74
if test "x$2" != "xNOFAIL"; then
75
AC_MSG_ERROR([The path of $1, which resolves as "$path", could not be imported.])
80
if test "x$imported_path" != "x$path"; then
84
[ if [[ "$path" =~ " " ]]; then ]
85
if test "x$2" != "xNOFAIL"; then
86
AC_MSG_NOTICE([The path of $1, which resolves as "$path", is invalid.])
87
AC_MSG_ERROR([Spaces are not allowed in this path.])
93
# Use eval to expand a potential ~.
95
if test ! -e "$new_path"; then
96
if test "x$2" != "xNOFAIL"; then
97
AC_MSG_ERROR([The path of $1, which resolves as "$new_path", is not found.])
103
# Make the path absolute
104
if test "x$new_path" != x; then
105
if test -d "$new_path"; then
106
path="`cd "$new_path"; pwd -L`"
108
dir="`$DIRNAME "$new_path"`"
109
base="`$BASENAME "$new_path"`"
110
path="`cd "$dir"; pwd -L`/$base"
121
##############################################################################
122
# Fixup path to be a Windows full long path
123
# Note: Only supported with cygwin/msys2 (cygpath tool)
124
AC_DEFUN([UTIL_FIXUP_WIN_LONG_PATH],
126
# Only process if variable expands to non-empty
128
if test "x$path" != x; then
129
if test "x$OPENJDK_BUILD_OS" = "xwindows"; then
130
win_path=$($PATHTOOL -wl "$path")
131
if test "x$win_path" != "x$path"; then
139
###############################################################################
140
# Check if the given file is a unix-style or windows-style executable, that is,
141
# if it expects paths in unix-style or windows-style.
142
# Returns "windows" or "unix" in $RESULT.
143
AC_DEFUN([UTIL_CHECK_WINENV_EXEC_TYPE],
145
# For cygwin and msys2, if it's linked with the correct helper lib, it
147
if test "x$OPENJDK_BUILD_OS_ENV" = "xwindows.cygwin" || \
148
test "x$OPENJDK_BUILD_OS_ENV" = "xwindows.msys2"; then
149
linked_libs=`$LDD $1 2>&1`
150
if test $? -ne 0; then
151
# Non-binary files (e.g. shell scripts) are unix files
154
[ if [[ "$linked_libs" =~ $WINENV_MARKER_DLL ]]; then ]
160
elif test "x$OPENJDK_BUILD_OS" = "xwindows"; then
161
# On WSL, we can check if it is a PE file
162
file_type=`$FILE -b $1 2>&1`
163
[ if [[ $file_type =~ PE.*Windows ]]; then ]
173
###############################################################################
174
# This will make sure the given variable points to a executable
175
# with a full and proper path. This means:
176
# 1) There will be no spaces in the path. On unix platforms,
177
# spaces in the path will result in an error. On Windows,
178
# the path will be rewritten using short-style to be space-free.
179
# 2) The path will be absolute, and it will be in unix-style (on
181
# Any arguments given to the executable is preserved.
182
# If the input variable does not have a directory specification, then
183
# it need to be in the PATH.
184
# $1: The name of the variable to fix
185
# $2: Where to look for the command (replaces $PATH)
186
# $3: set to NOFIXPATH to skip prefixing FIXPATH, even if needed on platform
187
AC_DEFUN([UTIL_FIXUP_EXECUTABLE],
191
# Only process if variable expands to non-empty
192
if test "x$input" != x; then
193
# First separate the path from the arguments. This will split at the first
195
[ if [[ "$OPENJDK_BUILD_OS" = "windows" && input =~ ^$FIXPATH ]]; then
196
line="${input#$FIXPATH }"
197
fixpath_prefix="$FIXPATH "
203
arguments="${line#"$path"}"
205
[ if ! [[ "$path" =~ /|\\ ]]; then ]
206
# This is a command without path (e.g. "gcc" or "echo")
207
command_type=`type -t "$path"`
208
if test "x$command_type" = xbuiltin || test "x$command_type" = xkeyword; then
209
# Shell builtin or keyword; we're done here
212
# Search in $PATH using bash built-in 'type -p'.
214
if test "x$2" != x; then
217
new_path=`type -p "$path"`
218
if test "x$new_path" = x && test "x$OPENJDK_BUILD_OS" = "xwindows"; then
219
# Try again with .exe
220
new_path="`type -p "$path.exe"`"
224
if test "x$new_path" = x; then
225
AC_MSG_NOTICE([The command for $1, which resolves as "$input", is not found in the PATH.])
226
AC_MSG_ERROR([Cannot locate $path])
230
# This is a path with slashes, don't look at $PATH
231
if test "x$OPENJDK_BUILD_OS" = "xwindows"; then
232
# fixpath.sh import will do all heavy lifting for us
233
new_path=`$FIXPATH_BASE import "$path"`
235
if test ! -e $new_path; then
236
# It failed, but maybe spaces were part of the path and not separating
237
# the command and argument. Retry using that assumption.
238
new_path=`$FIXPATH_BASE import "$input"`
239
if test ! -e $new_path; then
240
AC_MSG_NOTICE([The command for $1, which resolves as "$input", can not be found.])
241
AC_MSG_ERROR([Cannot locate $input])
243
# It worked, clear all "arguments"
249
UTIL_FIXUP_PATH($1, NOFAIL)
252
if test ! -e $new_path; then
253
AC_MSG_NOTICE([The command for $1, which resolves as "$input", is not found])
254
[ if [[ "$path" =~ " " ]]; then ]
255
AC_MSG_NOTICE([This might be caused by spaces in the path, which is not allowed.])
257
AC_MSG_ERROR([Cannot locate $path])
259
if test ! -x $new_path; then
260
AC_MSG_NOTICE([The command for $1, which resolves as "$input", is not executable.])
261
AC_MSG_ERROR([Cannot execute command at $path])
264
fi # end with or without slashes
266
# Now we have a usable command as new_path, with arguments in arguments
267
if test "x$OPENJDK_BUILD_OS" = "xwindows"; then
268
if test "x$fixpath_prefix" = x; then
269
# Only mess around if fixpath_prefix was not given
270
UTIL_CHECK_WINENV_EXEC_TYPE("$new_path")
271
if test "x$RESULT" = xwindows; then
272
fixpath_prefix="$FIXPATH "
273
# make sure we have an .exe suffix (but not two)
274
new_path="${new_path%.exe}.exe"
276
# If we have gotten a .exe suffix, remove it
277
new_path="${new_path%.exe}"
282
if test "x$3" = xNOFIXPATH; then
286
# Now join together the path and the arguments once again
287
new_complete="$fixpath_prefix$new_path$arguments"
292
###############################################################################
293
# Setup a tool for the given variable. If correctly specified by the user,
294
# use that value, otherwise search for the tool using the supplied code snippet.
296
# $2: code snippet to call to look for the tool
297
# $3: code snippet to call if variable was used to find tool
298
AC_DEFUN([UTIL_SETUP_TOOL],
300
# Publish this variable in the help.
301
AC_ARG_VAR($1, [Override default value for $1])
303
if [[ -z "${$1+x}" ]]; then
304
# The variable is not set by user, try to locate tool using the code snippet
307
# The variable is set, but is it from the command line or the environment?
309
# Try to remove the string !$1! from our list.
310
try_remove_var=${CONFIGURE_OVERRIDDEN_VARIABLES//!$1!/}
311
if test "x$try_remove_var" = "x$CONFIGURE_OVERRIDDEN_VARIABLES"; then
312
# If it failed, the variable was not from the command line. Ignore it,
313
# but warn the user (except for BASH, which is always set by the calling BASH).
314
if test "x$1" != xBASH; then
315
AC_MSG_WARN([Ignoring value of $1 from the environment. Use command line variables instead.])
317
# Try to locate tool using the code snippet
320
# If it succeeded, then it was overridden by the user. We will use it
323
# First remove it from the list of overridden variables, so we can test
324
# for unknown variables in the end.
325
CONFIGURE_OVERRIDDEN_VARIABLES="$try_remove_var"
329
# Check if we try to supply an empty value
330
if test "x$tool_override" = x; then
331
AC_MSG_CHECKING([for $1])
332
AC_MSG_RESULT([[[disabled by user]]])
334
# Split up override in command part and argument part
335
tool_and_args=($tool_override)
336
[ tool_command=${tool_and_args[0]} ]
337
[ unset 'tool_and_args[0]' ]
338
[ tool_args=${tool_and_args[@]} ]
340
# Check if the provided tool contains a complete path.
341
tool_basename="${tool_command##*/}"
342
if test "x$tool_basename" = "x$tool_command"; then
343
# A command without a complete path is provided, search $PATH.
344
AC_MSG_NOTICE([Will search for user supplied tool "$tool_basename"])
345
AC_PATH_PROGS($1, $tool_basename ${tool_basename}.exe)
347
if test "x$tool_command" = x; then
348
AC_MSG_ERROR([User supplied tool $1="$tool_basename" could not be found in PATH])
351
# Otherwise we believe it is a complete path. Use it as it is.
352
if test ! -x "$tool_command" && test ! -x "${tool_command}.exe"; then
353
AC_MSG_ERROR([User supplied tool $1="$tool_command" does not exist or is not executable])
355
if test ! -x "$tool_command"; then
356
tool_command="${tool_command}.exe"
360
if test "x$tool_args" != x; then
361
# If we got arguments, re-append them to the command after the fixup.
362
$1="[$]$1 $tool_args"
364
AC_MSG_CHECKING([for $1])
365
AC_MSG_RESULT([[$]$1 [[user supplied]]])
372
###############################################################################
373
# Locate a tool using proper methods.
375
# $2: executable name (or list of names) to look for
377
# $4: set to NOFIXPATH to skip prefixing FIXPATH, even if needed on platform
378
AC_DEFUN([UTIL_LOOKUP_PROGS],
380
UTIL_SETUP_TOOL($1, [
383
if test "x$3" != x; then
389
AC_MSG_CHECKING(for $name)
391
command_type=`type -t "$name"`
392
if test "x$command_type" = xbuiltin || test "x$command_type" = xkeyword; then
393
# Shell builtin or keyword; we're done here
396
AC_MSG_RESULT([[$full_path [builtin]]])
402
for elem in $PATH; do
404
if test "x$elem" = x; then
407
full_path="$elem/$name"
408
if test ! -e "$full_path" && test "x$OPENJDK_BUILD_OS" = "xwindows"; then
409
# Try again with .exe
410
full_path="$elem/$name.exe"
412
if test -x "$full_path" && test ! -d "$full_path" ; then
414
UTIL_FIXUP_EXECUTABLE($1, $3, $4)
417
# If we have FIXPATH enabled, strip all instances of it and prepend
418
# a single one, to avoid double fixpath prefixing.
419
if test "x$4" != xNOFIXPATH; then
420
[ if [[ $FIXPATH != "" && $result =~ ^"$FIXPATH " ]]; then ]
421
result="\$FIXPATH ${result#"$FIXPATH "}"
424
AC_MSG_RESULT([$result])
430
AC_MSG_RESULT([[[not found]]])
433
if test "x$3" != x; then
439
###############################################################################
440
# Call UTIL_SETUP_TOOL with AC_CHECK_TOOLS to locate the tool. This will look
441
# first for cross-compilation tools.
443
# $2: executable name (or list of names) to look for
445
AC_DEFUN([UTIL_LOOKUP_TOOLCHAIN_PROGS],
447
if test "x$ac_tool_prefix" = x; then
448
UTIL_LOOKUP_PROGS($1, $2, $3)
450
prefixed_names=$(for name in $2; do echo ${ac_tool_prefix}${name} $name; done)
451
UTIL_LOOKUP_PROGS($1, $prefixed_names, $3)
455
###############################################################################
456
# Test that variable $1 denoting a program is not empty. If empty, exit with an error.
457
# $1: variable to check
458
AC_DEFUN([UTIL_CHECK_NONEMPTY],
460
if test "x[$]$1" = x; then
461
AC_MSG_ERROR([Could not find required tool for $1])
465
###############################################################################
466
# Like UTIL_LOOKUP_PROGS but fails if no tool was found.
468
# $2: executable name (or list of names) to look for
470
AC_DEFUN([UTIL_REQUIRE_PROGS],
472
UTIL_LOOKUP_PROGS($1, $2, $3)
473
UTIL_CHECK_NONEMPTY($1)
476
###############################################################################
477
# Like UTIL_LOOKUP_PROGS but fails if no tool was found.
479
# $2: executable name (or list of names) to look for
481
AC_DEFUN([UTIL_REQUIRE_TOOLCHAIN_PROGS],
483
UTIL_LOOKUP_TOOLCHAIN_PROGS($1, $2, $3)
484
UTIL_CHECK_NONEMPTY($1)
488
###############################################################################
489
# Like UTIL_SETUP_TOOL but fails if no tool was found.
491
# $2: autoconf macro to call to look for the special tool
492
AC_DEFUN([UTIL_REQUIRE_SPECIAL],
494
UTIL_SETUP_TOOL($1, [$2])
495
UTIL_CHECK_NONEMPTY($1)
496
# The special macro will return an absolute path, and is only used for
497
# unix tools. No further processing needed.
500
###############################################################################
501
# Add FIXPATH prefix to variable. Normally this is done by UTIL_LOOKUP_PROGS
502
# or UTIL_FIXUP_EXECUTABLE, but in some circumstances this has to be done
503
# explicitly, such as when the command in question does not exist yet.
505
# $1: variable to add fixpath to
506
AC_DEFUN([UTIL_ADD_FIXPATH],
508
if test "x$FIXPATH" != x; then
513
###############################################################################
514
AC_DEFUN([UTIL_REMOVE_SYMBOLIC_LINKS],
516
if test "x$OPENJDK_BUILD_OS" != xwindows; then
517
# Follow a chain of symbolic links. Use readlink
518
# where it exists, else fall back to horribly
519
# complicated shell code.
520
if test "x$READLINK_TESTED" != yes; then
521
# On MacOSX there is a readlink tool with a different
522
# purpose than the GNU readlink tool. Check the found readlink.
523
READLINK_ISGNU=`$READLINK --version 2>&1 | $GREP GNU`
524
# If READLINK_ISGNU is empty, then it's a non-GNU readlink. Don't use it.
528
if test "x$READLINK" != x && test "x$READLINK_ISGNU" != x; then
529
$1=`$READLINK -f [$]$1`
531
# Save the current directory for restoring afterwards
534
sym_link_dir=`$DIRNAME [$]$1`
535
sym_link_file=`$BASENAME [$]$1`
537
# Use -P flag to resolve symlinks in directories.
539
sym_link_dir=`pwd -P`
540
# Resolve file symlinks
541
while test $COUNTER -lt 20; do
542
ISLINK=`$LS -l $sym_link_dir/$sym_link_file | $GREP -e '->' | $SED -e 's/.*-> \(.*\)/\1/'`
543
if test "x$ISLINK" == x; then
544
# This is not a symbolic link! We are done!
547
# Again resolve directory symlinks since the target of the just found
548
# link could be in a different directory
549
cd `$DIRNAME $ISLINK`
550
sym_link_dir=`pwd -P`
551
sym_link_file=`$BASENAME $ISLINK`
552
let COUNTER=COUNTER+1
555
$1=$sym_link_dir/$sym_link_file