1#
2# Copyright (c) 2011, 2018, 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########################################################################
27# This file is responsible for detecting, verifying and setting up the
28# toolchain, i.e. the compiler, linker and related utilities. It will setup
29# proper paths to the binaries, but it will not setup any flags.
30#
31# The binaries used is determined by the toolchain type, which is the family of
32# compilers and related tools that are used.
33########################################################################
34
35
36# All valid toolchains, regardless of platform (used by help.m4)
37VALID_TOOLCHAINS_all="gcc clang solstudio xlc microsoft"
38
39# These toolchains are valid on different platforms
40VALID_TOOLCHAINS_linux="gcc clang"
41VALID_TOOLCHAINS_solaris="solstudio"
42VALID_TOOLCHAINS_macosx="gcc clang"
43VALID_TOOLCHAINS_bsd="gcc clang"
44VALID_TOOLCHAINS_aix="xlc"
45VALID_TOOLCHAINS_windows="microsoft"
46
47# Toolchain descriptions
48TOOLCHAIN_DESCRIPTION_clang="clang/LLVM"
49TOOLCHAIN_DESCRIPTION_gcc="GNU Compiler Collection"
50TOOLCHAIN_DESCRIPTION_microsoft="Microsoft Visual Studio"
51TOOLCHAIN_DESCRIPTION_solstudio="Oracle Solaris Studio"
52TOOLCHAIN_DESCRIPTION_xlc="IBM XL C/C++"
53
54# Minimum supported versions, empty means unspecified
55TOOLCHAIN_MINIMUM_VERSION_clang="3.2"
56TOOLCHAIN_MINIMUM_VERSION_gcc="4.8"
57TOOLCHAIN_MINIMUM_VERSION_microsoft="16.00.30319.01" # VS2010
58TOOLCHAIN_MINIMUM_VERSION_solstudio="5.13"
59TOOLCHAIN_MINIMUM_VERSION_xlc=""
60
61# Minimum supported linker versions, empty means unspecified
62TOOLCHAIN_MINIMUM_LD_VERSION_gcc="2.18"
63
64# Prepare the system so that TOOLCHAIN_CHECK_COMPILER_VERSION can be called.
65# Must have CC_VERSION_NUMBER and CXX_VERSION_NUMBER.
66# $1 - optional variable prefix for compiler and version variables (BUILD_)
67# $2 - optional variable prefix for comparable variable (OPENJDK_BUILD_)
68AC_DEFUN([TOOLCHAIN_PREPARE_FOR_VERSION_COMPARISONS],
69[
70  if test "x[$]$1CC_VERSION_NUMBER" != "x[$]$1CXX_VERSION_NUMBER"; then
71    AC_MSG_WARN([C and C++ compiler have different version numbers, [$]$1CC_VERSION_NUMBER vs [$]$1CXX_VERSION_NUMBER.])
72    AC_MSG_WARN([This typically indicates a broken setup, and is not supported])
73  fi
74
75  # We only check CC_VERSION_NUMBER since we assume CXX_VERSION_NUMBER is equal.
76  if [ [[ "[$]$1CC_VERSION_NUMBER" =~ (.*\.){4} ]] ]; then
77    AC_MSG_WARN([C compiler version number has more than four parts (W.X.Y.Z): [$]$1CC_VERSION_NUMBER. Comparisons might be wrong.])
78  fi
79
80  if [ [[  "[$]$1CC_VERSION_NUMBER" =~ [0-9]{6} ]] ]; then
81    AC_MSG_WARN([C compiler version number has a part larger than 99999: [$]$1CC_VERSION_NUMBER. Comparisons might be wrong.])
82  fi
83
84  $2COMPARABLE_ACTUAL_VERSION=`$AWK -F. '{ printf("%05d%05d%05d%05d\n", [$]1, [$]2, [$]3, [$]4) }' <<< "[$]$1CC_VERSION_NUMBER"`
85])
86
87# Check if the configured compiler (C and C++) is of a specific version or
88# newer. TOOLCHAIN_PREPARE_FOR_VERSION_COMPARISONS must have been called before.
89#
90# Arguments:
91#   VERSION:   The version string to check against the found version
92#   IF_AT_LEAST:   block to run if the compiler is at least this version (>=)
93#   IF_OLDER_THAN:   block to run if the compiler is older than this version (<)
94#   PREFIX:   Optional variable prefix for compiler to compare version for (OPENJDK_BUILD_)
95BASIC_DEFUN_NAMED([TOOLCHAIN_CHECK_COMPILER_VERSION],
96    [*VERSION PREFIX IF_AT_LEAST IF_OLDER_THAN], [$@],
97[
98  # Need to assign to a variable since m4 is blocked from modifying parts in [].
99  REFERENCE_VERSION=ARG_VERSION
100
101  if [ [[ "$REFERENCE_VERSION" =~ (.*\.){4} ]] ]; then
102    AC_MSG_ERROR([Internal error: Cannot compare to ARG_VERSION, only four parts (W.X.Y.Z) is supported])
103  fi
104
105  if [ [[ "$REFERENCE_VERSION" =~ [0-9]{6} ]] ]; then
106    AC_MSG_ERROR([Internal error: Cannot compare to ARG_VERSION, only parts < 99999 is supported])
107  fi
108
109  # Version comparison method inspired by http://stackoverflow.com/a/24067243
110  COMPARABLE_REFERENCE_VERSION=`$AWK -F. '{ printf("%05d%05d%05d%05d\n", [$]1, [$]2, [$]3, [$]4) }' <<< "$REFERENCE_VERSION"`
111
112  if test [$]ARG_PREFIX[COMPARABLE_ACTUAL_VERSION] -ge $COMPARABLE_REFERENCE_VERSION ; then
113    :
114    ARG_IF_AT_LEAST
115  else
116    :
117    ARG_IF_OLDER_THAN
118  fi
119])
120
121# Prepare the system so that TOOLCHAIN_CHECK_COMPILER_VERSION can be called.
122# Must have LD_VERSION_NUMBER.
123# $1 - optional variable prefix for compiler and version variables (BUILD_)
124# $2 - optional variable prefix for comparable variable (OPENJDK_BUILD_)
125AC_DEFUN([TOOLCHAIN_PREPARE_FOR_LD_VERSION_COMPARISONS],
126[
127  if [ [[ "[$]$1LD_VERSION_NUMBER" =~ (.*\.){4} ]] ]; then
128    AC_MSG_WARN([Linker version number has more than four parts (W.X.Y.Z): [$]$1LD_VERSION_NUMBER. Comparisons might be wrong.])
129  fi
130
131  if [ [[  "[$]$1LD_VERSION_NUMBER" =~ [0-9]{6} ]] ]; then
132    AC_MSG_WARN([Linker version number has a part larger than 99999: [$]$1LD_VERSION_NUMBER. Comparisons might be wrong.])
133  fi
134
135  $2COMPARABLE_ACTUAL_LD_VERSION=`$AWK -F. '{ printf("%05d%05d%05d%05d\n", [$]1, [$]2, [$]3, [$]4) }' <<< "[$]$1LD_VERSION_NUMBER"`
136])
137
138# Check if the configured linker is of a specific version or
139# newer. TOOLCHAIN_PREPARE_FOR_LD_VERSION_COMPARISONS must have been called before.
140#
141# Arguments:
142#   VERSION:   The version string to check against the found version
143#   IF_AT_LEAST:   block to run if the compiler is at least this version (>=)
144#   IF_OLDER_THAN:   block to run if the compiler is older than this version (<)
145#   PREFIX:   Optional variable prefix for compiler to compare version for (OPENJDK_BUILD_)
146BASIC_DEFUN_NAMED([TOOLCHAIN_CHECK_LINKER_VERSION],
147    [*VERSION PREFIX IF_AT_LEAST IF_OLDER_THAN], [$@],
148[
149  # Need to assign to a variable since m4 is blocked from modifying parts in [].
150  REFERENCE_VERSION=ARG_VERSION
151
152  if [ [[ "$REFERENCE_VERSION" =~ (.*\.){4} ]] ]; then
153    AC_MSG_ERROR([Internal error: Cannot compare to ARG_VERSION, only four parts (W.X.Y.Z) is supported])
154  fi
155
156  if [ [[ "$REFERENCE_VERSION" =~ [0-9]{6} ]] ]; then
157    AC_MSG_ERROR([Internal error: Cannot compare to ARG_VERSION, only parts < 99999 is supported])
158  fi
159
160  # Version comparison method inspired by http://stackoverflow.com/a/24067243
161  COMPARABLE_REFERENCE_VERSION=`$AWK -F. '{ printf("%05d%05d%05d%05d\n", [$]1, [$]2, [$]3, [$]4) }' <<< "$REFERENCE_VERSION"`
162
163  if test [$]ARG_PREFIX[COMPARABLE_ACTUAL_LD_VERSION] -ge $COMPARABLE_REFERENCE_VERSION ; then
164    :
165    ARG_IF_AT_LEAST
166  else
167    :
168    ARG_IF_OLDER_THAN
169  fi
170])
171
172# Setup a number of variables describing how native output files are
173# named on this platform/toolchain.
174AC_DEFUN([TOOLCHAIN_SETUP_FILENAME_PATTERNS],
175[
176  # Define filename patterns
177  if test "x$OPENJDK_TARGET_OS" = xwindows; then
178    LIBRARY_PREFIX=
179    SHARED_LIBRARY_SUFFIX='.dll'
180    STATIC_LIBRARY_SUFFIX='.lib'
181    SHARED_LIBRARY='[$]1.dll'
182    STATIC_LIBRARY='[$]1.lib'
183    OBJ_SUFFIX='.obj'
184  else
185    LIBRARY_PREFIX=lib
186    SHARED_LIBRARY_SUFFIX='.so'
187    STATIC_LIBRARY_SUFFIX='.a'
188    SHARED_LIBRARY='lib[$]1.so'
189    STATIC_LIBRARY='lib[$]1.a'
190    OBJ_SUFFIX='.o'
191    if test "x$OPENJDK_TARGET_OS" = xmacosx; then
192      # For full static builds, we're overloading the SHARED_LIBRARY
193      # variables in order to limit the amount of changes required.
194      # It would be better to remove SHARED and just use LIBRARY and
195      # LIBRARY_SUFFIX for libraries that can be built either
196      # shared or static and use STATIC_* for libraries that are
197      # always built statically.
198      if test "x$STATIC_BUILD" = xtrue; then
199        SHARED_LIBRARY='lib[$]1.a'
200        SHARED_LIBRARY_SUFFIX='.a'
201      else
202        SHARED_LIBRARY='lib[$]1.dylib'
203        SHARED_LIBRARY_SUFFIX='.dylib'
204      fi
205    fi
206  fi
207
208  AC_SUBST(LIBRARY_PREFIX)
209  AC_SUBST(SHARED_LIBRARY_SUFFIX)
210  AC_SUBST(STATIC_LIBRARY_SUFFIX)
211  AC_SUBST(SHARED_LIBRARY)
212  AC_SUBST(STATIC_LIBRARY)
213  AC_SUBST(OBJ_SUFFIX)
214])
215
216# Determine which toolchain type to use, and make sure it is valid for this
217# platform. Setup various information about the selected toolchain.
218AC_DEFUN_ONCE([TOOLCHAIN_DETERMINE_TOOLCHAIN_TYPE],
219[
220  AC_ARG_WITH(toolchain-type, [AS_HELP_STRING([--with-toolchain-type],
221      [the toolchain type (or family) to use, use '--help' to show possible values @<:@platform dependent@:>@])])
222
223  # Use indirect variable referencing
224  toolchain_var_name=VALID_TOOLCHAINS_$OPENJDK_BUILD_OS
225  VALID_TOOLCHAINS=${!toolchain_var_name}
226
227  if test "x$OPENJDK_TARGET_OS" = xmacosx; then
228    if test -n "$XCODEBUILD"; then
229      # On Mac OS X, default toolchain to clang after Xcode 5
230      XCODE_VERSION_OUTPUT=`"$XCODEBUILD" -version 2>&1 | $HEAD -n 1`
231      $ECHO "$XCODE_VERSION_OUTPUT" | $GREP "Xcode " > /dev/null
232      if test $? -ne 0; then
233        AC_MSG_NOTICE([xcodebuild output: $XCODE_VERSION_OUTPUT])
234        AC_MSG_ERROR([Failed to determine Xcode version.])
235      fi
236      XCODE_MAJOR_VERSION=`$ECHO $XCODE_VERSION_OUTPUT | \
237          $SED -e 's/^Xcode \(@<:@1-9@:>@@<:@0-9.@:>@*\)/\1/' | \
238          $CUT -f 1 -d .`
239      AC_MSG_NOTICE([Xcode major version: $XCODE_MAJOR_VERSION])
240      if test $XCODE_MAJOR_VERSION -ge 5; then
241          DEFAULT_TOOLCHAIN="clang"
242      else
243          DEFAULT_TOOLCHAIN="gcc"
244      fi
245    else
246      # If Xcode is not installed, but the command line tools are
247      # then we can't run xcodebuild. On these systems we should
248      # default to clang
249      DEFAULT_TOOLCHAIN="clang"
250    fi
251  else
252    # First toolchain type in the list is the default
253    DEFAULT_TOOLCHAIN=${VALID_TOOLCHAINS%% *}
254  fi
255
256  if test "x$with_toolchain_type" = xlist; then
257    # List all toolchains
258    AC_MSG_NOTICE([The following toolchains are valid on this platform:])
259    for toolchain in $VALID_TOOLCHAINS; do
260      toolchain_var_name=TOOLCHAIN_DESCRIPTION_$toolchain
261      TOOLCHAIN_DESCRIPTION=${!toolchain_var_name}
262      $PRINTF "  %-10s  %s\n" $toolchain "$TOOLCHAIN_DESCRIPTION"
263    done
264
265    exit 0
266  elif test "x$with_toolchain_type" != x; then
267    # User override; check that it is valid
268    if test "x${VALID_TOOLCHAINS/$with_toolchain_type/}" = "x${VALID_TOOLCHAINS}"; then
269      AC_MSG_NOTICE([Toolchain type $with_toolchain_type is not valid on this platform.])
270      AC_MSG_NOTICE([Valid toolchains: $VALID_TOOLCHAINS.])
271      AC_MSG_ERROR([Cannot continue.])
272    fi
273    TOOLCHAIN_TYPE=$with_toolchain_type
274  else
275    # No flag given, use default
276    TOOLCHAIN_TYPE=$DEFAULT_TOOLCHAIN
277  fi
278  AC_SUBST(TOOLCHAIN_TYPE)
279
280  # on AIX, check for xlclang++ on the PATH and TOOLCHAIN_PATH and use it if it is available
281  if test "x$OPENJDK_TARGET_OS" = xaix; then
282    if test "x$TOOLCHAIN_PATH" != x; then
283      XLC_TEST_PATH=${TOOLCHAIN_PATH}/
284    fi
285
286    XLCLANG_VERSION_OUTPUT=`${XLC_TEST_PATH}xlclang++ -qversion 2>&1 | $HEAD -n 1`
287    $ECHO "$XLCLANG_VERSION_OUTPUT" | $GREP "IBM XL C/C++ for AIX" > /dev/null
288    if test $? -eq 0; then
289      AC_MSG_NOTICE([xlclang++ output: $XLCLANG_VERSION_OUTPUT])
290      XLC_USES_CLANG=true
291    fi
292  fi
293
294  TOOLCHAIN_CC_BINARY_clang="clang"
295  TOOLCHAIN_CC_BINARY_gcc="gcc"
296  TOOLCHAIN_CC_BINARY_microsoft="cl$EXE_SUFFIX"
297  TOOLCHAIN_CC_BINARY_solstudio="cc"
298  TOOLCHAIN_CC_BINARY_xlc="xlc_r"
299
300  TOOLCHAIN_CXX_BINARY_clang="clang++"
301  TOOLCHAIN_CXX_BINARY_gcc="g++"
302  TOOLCHAIN_CXX_BINARY_microsoft="cl$EXE_SUFFIX"
303  TOOLCHAIN_CXX_BINARY_solstudio="CC"
304  TOOLCHAIN_CXX_BINARY_xlc="xlC_r"
305
306  if test "x$OPENJDK_TARGET_OS" = xaix; then
307    if test "x$XLC_USES_CLANG" = xtrue; then
308      AC_MSG_NOTICE([xlclang++ detected, using it])
309      TOOLCHAIN_CC_BINARY_xlc="xlclang"
310      TOOLCHAIN_CXX_BINARY_xlc="xlclang++"
311    fi
312  fi
313
314  # Use indirect variable referencing
315  toolchain_var_name=TOOLCHAIN_DESCRIPTION_$TOOLCHAIN_TYPE
316  TOOLCHAIN_DESCRIPTION=${!toolchain_var_name}
317  toolchain_var_name=TOOLCHAIN_MINIMUM_VERSION_$TOOLCHAIN_TYPE
318  TOOLCHAIN_MINIMUM_VERSION=${!toolchain_var_name}
319  toolchain_var_name=TOOLCHAIN_MINIMUM_LD_VERSION_$TOOLCHAIN_TYPE
320  TOOLCHAIN_MINIMUM_LD_VERSION=${!toolchain_var_name}
321  toolchain_var_name=TOOLCHAIN_CC_BINARY_$TOOLCHAIN_TYPE
322  TOOLCHAIN_CC_BINARY=${!toolchain_var_name}
323  toolchain_var_name=TOOLCHAIN_CXX_BINARY_$TOOLCHAIN_TYPE
324  TOOLCHAIN_CXX_BINARY=${!toolchain_var_name}
325
326  TOOLCHAIN_SETUP_FILENAME_PATTERNS
327
328  if test "x$TOOLCHAIN_TYPE" = "x$DEFAULT_TOOLCHAIN"; then
329    AC_MSG_NOTICE([Using default toolchain $TOOLCHAIN_TYPE ($TOOLCHAIN_DESCRIPTION)])
330  else
331    AC_MSG_NOTICE([Using user selected toolchain $TOOLCHAIN_TYPE ($TOOLCHAIN_DESCRIPTION). Default toolchain is $DEFAULT_TOOLCHAIN.])
332  fi
333])
334
335# Before we start detecting the toolchain executables, we might need some
336# special setup, e.g. additional paths etc.
337AC_DEFUN_ONCE([TOOLCHAIN_PRE_DETECTION],
338[
339  # FIXME: Is this needed?
340  AC_LANG(C++)
341
342  # Store the CFLAGS etc passed to the configure script.
343  ORG_CFLAGS="$CFLAGS"
344  ORG_CXXFLAGS="$CXXFLAGS"
345
346  # autoconf magic only relies on PATH, so update it if tools dir is specified
347  OLD_PATH="$PATH"
348
349  # On Windows, we need to detect the visual studio installation first.
350  # This will change the PATH, but we need to keep that new PATH even
351  # after toolchain detection is done, since the compiler (on x86) uses
352  # it for DLL resolution in runtime.
353  if test "x$OPENJDK_BUILD_OS" = "xwindows" \
354      && test "x$TOOLCHAIN_TYPE" = "xmicrosoft"; then
355    TOOLCHAIN_SETUP_VISUAL_STUDIO_ENV
356    if test "x$OPENJDK_BUILD_OS_ENV" = "xwindows.wsl"; then
357      # Append VS_PATH. In WSL, VS_PATH will not contain the WSL env path needed
358      # for using basic Unix tools, so need to keep the original PATH.
359      BASIC_APPEND_TO_PATH(PATH, $VS_PATH)
360      BASIC_APPEND_TO_PATH(WSLENV, "PATH/l:LIB:INCLUDE")
361      export WSLENV
362    else
363      # Reset path to VS_PATH. It will include everything that was on PATH at the time we
364      # ran TOOLCHAIN_SETUP_VISUAL_STUDIO_ENV.
365      PATH="$VS_PATH"
366    fi
367    # The microsoft toolchain also requires INCLUDE and LIB to be set.
368    export INCLUDE="$VS_INCLUDE"
369    export LIB="$VS_LIB"
370  else
371    if test "x$XCODE_VERSION_OUTPUT" != x; then
372      # For Xcode, we set the Xcode version as TOOLCHAIN_VERSION
373      TOOLCHAIN_VERSION=`$ECHO $XCODE_VERSION_OUTPUT | $CUT -f 2 -d ' '`
374      TOOLCHAIN_DESCRIPTION="$TOOLCHAIN_DESCRIPTION from Xcode $TOOLCHAIN_VERSION"
375    else
376      # Currently we do not define this for other toolchains. This might change as the need arise.
377      TOOLCHAIN_VERSION=
378    fi
379  fi
380  AC_SUBST(TOOLCHAIN_VERSION)
381
382  # Finally add TOOLCHAIN_PATH at the beginning, to allow --with-tools-dir to
383  # override all other locations.
384  if test "x$TOOLCHAIN_PATH" != x; then
385    PATH=$TOOLCHAIN_PATH:$PATH
386  fi
387])
388
389# Restore path, etc
390AC_DEFUN_ONCE([TOOLCHAIN_POST_DETECTION],
391[
392  # Restore old path, except for the microsoft toolchain, which requires VS_PATH
393  # to remain in place. Otherwise the compiler will not work in some siutations
394  # in later configure checks.
395  if test "x$TOOLCHAIN_TYPE" != "xmicrosoft"; then
396    PATH="$OLD_PATH"
397  fi
398
399  # Restore the flags to the user specified values.
400  # This is necessary since AC_PROG_CC defaults CFLAGS to "-g -O2"
401  CFLAGS="$ORG_CFLAGS"
402  CXXFLAGS="$ORG_CXXFLAGS"
403])
404
405# Check if a compiler is of the toolchain type we expect, and save the version
406# information from it. If the compiler does not match the expected type,
407# this function will abort using AC_MSG_ERROR. If it matches, the version will
408# be stored in CC_VERSION_NUMBER/CXX_VERSION_NUMBER (as a dotted number), and
409# the full version string in CC_VERSION_STRING/CXX_VERSION_STRING.
410#
411# $1 = compiler to test (CC or CXX)
412# $2 = human readable name of compiler (C or C++)
413AC_DEFUN([TOOLCHAIN_EXTRACT_COMPILER_VERSION],
414[
415  COMPILER=[$]$1
416  COMPILER_NAME=$2
417
418  if test "x$TOOLCHAIN_TYPE" = xsolstudio; then
419    # cc -V output typically looks like
420    #     cc: Sun C 5.12 Linux_i386 2011/11/16
421    # or
422    #     cc: Studio 12.5 Sun C 5.14 SunOS_sparc 2016/05/31
423    COMPILER_VERSION_OUTPUT=`$COMPILER -V 2>&1`
424    # Check that this is likely to be the Solaris Studio cc.
425    $ECHO "$COMPILER_VERSION_OUTPUT" | $GREP "^.* Sun $COMPILER_NAME" > /dev/null
426    if test $? -ne 0; then
427      ALT_VERSION_OUTPUT=`$COMPILER --version 2>&1`
428      AC_MSG_NOTICE([The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required $TOOLCHAIN_TYPE compiler.])
429      AC_MSG_NOTICE([The result from running with -V was: "$COMPILER_VERSION_OUTPUT"])
430      AC_MSG_NOTICE([The result from running with --version was: "$ALT_VERSION_OUTPUT"])
431      AC_MSG_ERROR([A $TOOLCHAIN_TYPE compiler is required. Try setting --with-tools-dir.])
432    fi
433    # Remove usage instructions (if present), and
434    # collapse compiler output into a single line
435    COMPILER_VERSION_STRING=`$ECHO $COMPILER_VERSION_OUTPUT | \
436        $SED -e 's/ *@<:@Uu@:>@sage:.*//'`
437    COMPILER_VERSION_NUMBER=`$ECHO $COMPILER_VERSION_OUTPUT | \
438        $SED -e "s/^.*@<:@ ,\t@:>@$COMPILER_NAME@<:@ ,\t@:>@\(@<:@1-9@:>@\.@<:@0-9@:>@@<:@0-9@:>@*\).*/\1/"`
439  elif test  "x$TOOLCHAIN_TYPE" = xxlc; then
440    # xlc -qversion output typically looks like
441    #     IBM XL C/C++ for AIX, V11.1 (5724-X13)
442    #     Version: 11.01.0000.0015
443    COMPILER_VERSION_OUTPUT=`$COMPILER -qversion 2>&1`
444    # Check that this is likely to be the IBM XL C compiler.
445    $ECHO "$COMPILER_VERSION_OUTPUT" | $GREP "IBM XL C" > /dev/null
446    if test $? -ne 0; then
447      ALT_VERSION_OUTPUT=`$COMPILER --version 2>&1`
448      AC_MSG_NOTICE([The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required $TOOLCHAIN_TYPE compiler.])
449      AC_MSG_NOTICE([The result from running with -qversion was: "$COMPILER_VERSION_OUTPUT"])
450      AC_MSG_NOTICE([The result from running with --version was: "$ALT_VERSION_OUTPUT"])
451      AC_MSG_ERROR([A $TOOLCHAIN_TYPE compiler is required. Try setting --with-tools-dir.])
452    fi
453    # Collapse compiler output into a single line
454    COMPILER_VERSION_STRING=`$ECHO $COMPILER_VERSION_OUTPUT`
455    COMPILER_VERSION_NUMBER=`$ECHO $COMPILER_VERSION_OUTPUT | \
456        $SED -e 's/^.*, V\(@<:@1-9@:>@@<:@0-9.@:>@*\).*$/\1/'`
457  elif test  "x$TOOLCHAIN_TYPE" = xmicrosoft; then
458    # There is no specific version flag, but all output starts with a version string.
459    # First line typically looks something like:
460    # Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
461    # but the compiler name may vary depending on locale.
462    COMPILER_VERSION_OUTPUT=`"$COMPILER" 2>&1 | $GREP -v 'ERROR.*UtilTranslatePathList' | $HEAD -n 1 | $TR -d '\r'`
463    # Check that this is likely to be Microsoft CL.EXE.
464    $ECHO "$COMPILER_VERSION_OUTPUT" | $GREP "Microsoft" > /dev/null
465    if test $? -ne 0; then
466      AC_MSG_NOTICE([The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required $TOOLCHAIN_TYPE compiler.])
467      AC_MSG_NOTICE([The result from running it was: "$COMPILER_VERSION_OUTPUT"])
468      AC_MSG_ERROR([A $TOOLCHAIN_TYPE compiler is required. Try setting --with-tools-dir.])
469    fi
470    # Collapse compiler output into a single line
471    COMPILER_VERSION_STRING=`$ECHO $COMPILER_VERSION_OUTPUT`
472    COMPILER_VERSION_NUMBER=`$ECHO $COMPILER_VERSION_OUTPUT | \
473        $SED -e 's/^.*ersion.\(@<:@1-9@:>@@<:@0-9.@:>@*\) .*$/\1/'`
474  elif test  "x$TOOLCHAIN_TYPE" = xgcc; then
475    # gcc --version output typically looks like
476    #     gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
477    #     Copyright (C) 2013 Free Software Foundation, Inc.
478    #     This is free software; see the source for copying conditions.  There is NO
479    #     warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
480    COMPILER_VERSION_OUTPUT=`$COMPILER --version 2>&1`
481    # Check that this is likely to be GCC.
482    $ECHO "$COMPILER_VERSION_OUTPUT" | $GREP "Free Software Foundation" > /dev/null
483    if test $? -ne 0; then
484      AC_MSG_NOTICE([The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required $TOOLCHAIN_TYPE compiler.])
485      AC_MSG_NOTICE([The result from running with --version was: "$COMPILER_VERSION"])
486      AC_MSG_ERROR([A $TOOLCHAIN_TYPE compiler is required. Try setting --with-tools-dir.])
487    fi
488    # Remove Copyright and legalese from version string, and
489    # collapse into a single line
490    COMPILER_VERSION_STRING=`$ECHO $COMPILER_VERSION_OUTPUT | \
491        $SED -e 's/ *Copyright .*//'`
492    COMPILER_VERSION_NUMBER=`$ECHO $COMPILER_VERSION_OUTPUT | \
493        $SED -e 's/^.* \(@<:@1-9@:>@<:@0-9@:>@*\.@<:@0-9.@:>@*\)@<:@^0-9.@:>@.*$/\1/'`
494  elif test  "x$TOOLCHAIN_TYPE" = xclang; then
495    # clang --version output typically looks like
496    #    Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
497    #    clang version 3.3 (tags/RELEASE_33/final)
498    # or
499    #    Debian clang version 3.2-7ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)
500    #    Target: x86_64-pc-linux-gnu
501    #    Thread model: posix
502    COMPILER_VERSION_OUTPUT=`$COMPILER --version 2>&1`
503    # Check that this is likely to be clang
504    $ECHO "$COMPILER_VERSION_OUTPUT" | $GREP "clang" > /dev/null
505    if test $? -ne 0; then
506      AC_MSG_NOTICE([The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required $TOOLCHAIN_TYPE compiler.])
507      AC_MSG_NOTICE([The result from running with --version was: "$COMPILER_VERSION_OUTPUT"])
508      AC_MSG_ERROR([A $TOOLCHAIN_TYPE compiler is required. Try setting --with-tools-dir.])
509    fi
510    # Collapse compiler output into a single line
511    COMPILER_VERSION_STRING=`$ECHO $COMPILER_VERSION_OUTPUT`
512    COMPILER_VERSION_NUMBER=`$ECHO $COMPILER_VERSION_OUTPUT | \
513        $SED -e 's/^.* version \(@<:@1-9@:>@@<:@0-9.@:>@*\).*$/\1/'`
514  else
515      AC_MSG_ERROR([Unknown toolchain type $TOOLCHAIN_TYPE.])
516  fi
517  # This sets CC_VERSION_NUMBER or CXX_VERSION_NUMBER. (This comment is a grep marker)
518  $1_VERSION_NUMBER="$COMPILER_VERSION_NUMBER"
519  # This sets CC_VERSION_STRING or CXX_VERSION_STRING. (This comment is a grep marker)
520  $1_VERSION_STRING="$COMPILER_VERSION_STRING"
521
522  AC_MSG_NOTICE([Using $TOOLCHAIN_TYPE $COMPILER_NAME compiler version $COMPILER_VERSION_NUMBER @<:@$COMPILER_VERSION_STRING@:>@])
523])
524
525# Try to locate the given C or C++ compiler in the path, or otherwise.
526#
527# $1 = compiler to test (CC or CXX)
528# $2 = human readable name of compiler (C or C++)
529# $3 = compiler name to search for
530AC_DEFUN([TOOLCHAIN_FIND_COMPILER],
531[
532  COMPILER_NAME=$2
533  SEARCH_LIST="$3"
534
535  if test "x[$]$1" != x; then
536    # User has supplied compiler name already, always let that override.
537    AC_MSG_NOTICE([Will use user supplied compiler $1=[$]$1])
538    if test "x`basename [$]$1`" = "x[$]$1"; then
539      # A command without a complete path is provided, search $PATH.
540
541      AC_PATH_PROGS(POTENTIAL_$1, [$]$1)
542      if test "x$POTENTIAL_$1" != x; then
543        $1=$POTENTIAL_$1
544      else
545        AC_MSG_ERROR([User supplied compiler $1=[$]$1 could not be found])
546      fi
547    else
548      # Otherwise it might already be a complete path
549      if test ! -x "[$]$1"; then
550        AC_MSG_ERROR([User supplied compiler $1=[$]$1 does not exist])
551      fi
552    fi
553  else
554    # No user supplied value. Locate compiler ourselves.
555
556    # If we are cross compiling, assume cross compilation tools follows the
557    # cross compilation standard where they are prefixed with the autoconf
558    # standard name for the target. For example the binary
559    # i686-sun-solaris2.10-gcc will cross compile for i686-sun-solaris2.10.
560    # If we are not cross compiling, then the default compiler name will be
561    # used.
562
563    $1=
564    # If TOOLCHAIN_PATH is set, check for all compiler names in there first
565    # before checking the rest of the PATH.
566    # FIXME: Now that we prefix the TOOLS_DIR to the PATH in the PRE_DETECTION
567    # step, this should not be necessary.
568    if test -n "$TOOLCHAIN_PATH"; then
569      PATH_save="$PATH"
570      PATH="$TOOLCHAIN_PATH"
571      AC_PATH_TOOL(TOOLCHAIN_PATH_$1, $SEARCH_LIST)
572      $1=$TOOLCHAIN_PATH_$1
573      PATH="$PATH_save"
574    fi
575
576    # AC_PATH_TOOL can't be run multiple times with the same variable,
577    # so create a new name for this run.
578    if test "x[$]$1" = x; then
579      AC_PATH_TOOL(POTENTIAL_$1, $SEARCH_LIST)
580      $1=$POTENTIAL_$1
581    fi
582
583    if test "x[$]$1" = x; then
584      HELP_MSG_MISSING_DEPENDENCY([devkit])
585      AC_MSG_ERROR([Could not find a $COMPILER_NAME compiler. $HELP_MSG])
586    fi
587  fi
588
589  # Now we have a compiler binary in $1. Make sure it's okay.
590  BASIC_FIXUP_EXECUTABLE($1)
591  TEST_COMPILER="[$]$1"
592
593  AC_MSG_CHECKING([resolved symbolic links for $1])
594  SYMLINK_ORIGINAL="$TEST_COMPILER"
595  BASIC_REMOVE_SYMBOLIC_LINKS(SYMLINK_ORIGINAL)
596  if test "x$TEST_COMPILER" = "x$SYMLINK_ORIGINAL"; then
597    AC_MSG_RESULT([no symlink])
598  else
599    AC_MSG_RESULT([$SYMLINK_ORIGINAL])
600
601    # We can't handle ccache by gcc wrappers, since we need to know if we're
602    # using ccache. Instead ccache usage must be controlled by a configure option.
603    COMPILER_BASENAME=`$BASENAME "$SYMLINK_ORIGINAL"`
604    if test "x$COMPILER_BASENAME" = "xccache"; then
605      AC_MSG_NOTICE([Please use --enable-ccache instead of providing a wrapped compiler.])
606      AC_MSG_ERROR([$TEST_COMPILER is a symbolic link to ccache. This is not supported.])
607    fi
608  fi
609
610  TOOLCHAIN_EXTRACT_COMPILER_VERSION([$1], [$COMPILER_NAME])
611])
612
613# Retrieve the linker version number and store it in LD_VERSION_NUMBER
614# (as a dotted number), and
615# the full version string in LD_VERSION_STRING.
616#
617# $1 = linker to test (LD or BUILD_LD)
618# $2 = human readable name of linker (Linker or BuildLinker)
619AC_DEFUN([TOOLCHAIN_EXTRACT_LD_VERSION],
620[
621  LINKER=[$]$1
622  LINKER_NAME="$2"
623
624  if test "x$TOOLCHAIN_TYPE" = xsolstudio; then
625    # cc -Wl,-V output typically looks like
626    #   ld: Software Generation Utilities - Solaris Link Editors: 5.11-1.2329
627
628    # solstudio cc requires us to have an existing file to pass as argument,
629    # but it need not be a syntactically correct C file, so just use
630    # ourself. :) The intermediate 'cat' is needed to stop ld from leaving
631    # a lingering a.out (!).
632    LINKER_VERSION_STRING=`$LD -Wl,-V $TOPDIR/configure 2>&1 | $CAT | $HEAD -n 1 | $SED -e 's/ld: //'`
633    # Extract version number
634    [ LINKER_VERSION_NUMBER=`$ECHO $LINKER_VERSION_STRING | \
635        $SED -e 's/.* \([0-9][0-9]*\.[0-9][0-9]*\)-\([0-9][0-9]*\.[0-9][0-9]*\)/\1.\2/'` ]
636  elif test  "x$TOOLCHAIN_TYPE" = xxlc; then
637    LINKER_VERSION_STRING="Unknown"
638    LINKER_VERSION_NUMBER="0.0"
639  elif test  "x$TOOLCHAIN_TYPE" = xmicrosoft; then
640    # There is no specific version flag, but all output starts with a version string.
641    # First line typically looks something like:
642    #   Microsoft (R) Incremental Linker Version 12.00.31101.0
643    LINKER_VERSION_STRING=`$LD 2>&1 | $HEAD -n 1 | $TR -d '\r'`
644    # Extract version number
645    [ LINKER_VERSION_NUMBER=`$ECHO $LINKER_VERSION_STRING | \
646        $SED -e 's/.* \([0-9][0-9]*\(\.[0-9][0-9]*\)*\).*/\1/'` ]
647  elif test  "x$TOOLCHAIN_TYPE" = xgcc; then
648    # gcc -Wl,-version output typically looks like:
649    #   GNU ld (GNU Binutils for Ubuntu) 2.26.1
650    #   Copyright (C) 2015 Free Software Foundation, Inc.
651    #   This program is free software; [...]
652    # If using gold it will look like:
653    #   GNU gold (GNU Binutils 2.30) 1.15
654    LINKER_VERSION_STRING=`$LD -Wl,--version 2> /dev/null | $HEAD -n 1`
655    # Extract version number
656    if [ [[ "$LINKER_VERSION_STRING" == *gold* ]] ]; then
657      [ LINKER_VERSION_NUMBER=`$ECHO $LINKER_VERSION_STRING | \
658          $SED -e 's/.* \([0-9][0-9]*\(\.[0-9][0-9]*\)*\).*) .*/\1/'` ]
659    else
660      [ LINKER_VERSION_NUMBER=`$ECHO $LINKER_VERSION_STRING | \
661          $SED -e 's/.* \([0-9][0-9]*\(\.[0-9][0-9]*\)*\).*/\1/'` ]
662    fi
663  elif test  "x$TOOLCHAIN_TYPE" = xclang; then
664    # clang -Wl,-v output typically looks like
665    #   @(#)PROGRAM:ld  PROJECT:ld64-305
666    #   configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
667    #   Library search paths: [...]
668    # or
669    #   GNU ld (GNU Binutils for Ubuntu) 2.26.1
670
671    LINKER_VERSION_STRING=`$LD -Wl,-v 2>&1 | $HEAD -n 1`
672    # Check if we're using the GNU ld
673    $ECHO "$LINKER_VERSION_STRING" | $GREP "GNU" > /dev/null
674    if test $? -eq 0; then
675      # Extract version number
676      [ LINKER_VERSION_NUMBER=`$ECHO $LINKER_VERSION_STRING | \
677          $SED -e 's/.* \([0-9][0-9]*\(\.[0-9][0-9]*\)*\).*/\1/'` ]
678    else
679      # Extract version number
680      [ LINKER_VERSION_NUMBER=`$ECHO $LINKER_VERSION_STRING | \
681          $SED -e 's/.*-\([0-9][0-9]*\)/\1/'` ]
682    fi
683  fi
684
685  $1_VERSION_NUMBER="$LINKER_VERSION_NUMBER"
686  $1_VERSION_STRING="$LINKER_VERSION_STRING"
687
688  AC_MSG_NOTICE([Using $TOOLCHAIN_TYPE $LINKER_NAME version $LINKER_VERSION_NUMBER @<:@$LINKER_VERSION_STRING@:>@])
689])
690
691# Detect the core components of the toolchain, i.e. the compilers (CC and CXX),
692# preprocessor (CPP and CXXCPP), the linker (LD), the assembler (AS) and the
693# archiver (AR). Verify that the compilers are correct according to the
694# toolchain type.
695AC_DEFUN_ONCE([TOOLCHAIN_DETECT_TOOLCHAIN_CORE],
696[
697  #
698  # Setup the compilers (CC and CXX)
699  #
700  TOOLCHAIN_FIND_COMPILER([CC], [C], $TOOLCHAIN_CC_BINARY)
701  # Now that we have resolved CC ourself, let autoconf have its go at it
702  AC_PROG_CC([$CC])
703
704  TOOLCHAIN_FIND_COMPILER([CXX], [C++], $TOOLCHAIN_CXX_BINARY)
705  # Now that we have resolved CXX ourself, let autoconf have its go at it
706  AC_PROG_CXX([$CXX])
707
708  # This is the compiler version number on the form X.Y[.Z]
709  AC_SUBST(CC_VERSION_NUMBER)
710  AC_SUBST(CXX_VERSION_NUMBER)
711
712  TOOLCHAIN_PREPARE_FOR_VERSION_COMPARISONS
713
714  if test "x$TOOLCHAIN_MINIMUM_VERSION" != x; then
715    TOOLCHAIN_CHECK_COMPILER_VERSION(VERSION: $TOOLCHAIN_MINIMUM_VERSION,
716        IF_OLDER_THAN: [
717          AC_MSG_WARN([You are using $TOOLCHAIN_TYPE older than $TOOLCHAIN_MINIMUM_VERSION. This is not a supported configuration.])
718        ]
719    )
720  fi
721
722  #
723  # Setup the preprocessor (CPP and CXXCPP)
724  #
725  AC_PROG_CPP
726  BASIC_FIXUP_EXECUTABLE(CPP)
727  AC_PROG_CXXCPP
728  BASIC_FIXUP_EXECUTABLE(CXXCPP)
729
730  #
731  # Setup the linker (LD)
732  #
733  if test "x$TOOLCHAIN_TYPE" = xmicrosoft; then
734    # In the Microsoft toolchain we have a separate LD command "link".
735    # Make sure we reject /usr/bin/link (as determined in CYGWIN_LINK), which is
736    # a cygwin program for something completely different.
737    AC_CHECK_PROG([LD], [link$EXE_SUFFIX],[link$EXE_SUFFIX],,, [$CYGWIN_LINK])
738    BASIC_FIXUP_EXECUTABLE(LD)
739    # Verify that we indeed succeeded with this trick.
740    AC_MSG_CHECKING([if the found link.exe is actually the Visual Studio linker])
741    "$LD" --version > /dev/null
742    if test $? -eq 0 ; then
743      AC_MSG_RESULT([no])
744      AC_MSG_ERROR([This is the Cygwin link tool. Please check your PATH and rerun configure.])
745    else
746      AC_MSG_RESULT([yes])
747    fi
748    LDCXX="$LD"
749    # jaotc being a windows program expects the linker to be supplied with exe suffix.
750    LD_JAOTC="$LD$EXE_SUFFIX"
751  else
752    # All other toolchains use the compiler to link.
753    LD="$CC"
754    LDCXX="$CXX"
755    # jaotc expects 'ld' as the linker rather than the compiler.
756    BASIC_CHECK_TOOLS([LD_JAOTC], ld)
757    BASIC_FIXUP_EXECUTABLE(LD_JAOTC)
758  fi
759  AC_SUBST(LD)
760  AC_SUBST(LD_JAOTC)
761  # FIXME: it should be CXXLD, according to standard (cf CXXCPP)
762  AC_SUBST(LDCXX)
763
764  TOOLCHAIN_EXTRACT_LD_VERSION([LD], [linker])
765  TOOLCHAIN_PREPARE_FOR_LD_VERSION_COMPARISONS
766
767  if test "x$TOOLCHAIN_MINIMUM_LD_VERSION" != x; then
768    TOOLCHAIN_CHECK_LINKER_VERSION(VERSION: $TOOLCHAIN_MINIMUM_LD_VERSION,
769        IF_OLDER_THAN: [
770          AC_MSG_WARN([You are using a linker older than $TOOLCHAIN_MINIMUM_LD_VERSION. This is not a supported configuration.])
771        ]
772    )
773  fi
774
775  #
776  # Setup the assembler (AS)
777  #
778  if test "x$OPENJDK_TARGET_OS" = xsolaris; then
779    BASIC_PATH_PROGS(AS, as)
780    BASIC_FIXUP_EXECUTABLE(AS)
781    if test "x$AS" = x; then
782      AC_MSG_ERROR([Solaris assembler (as) is required. Please install via "pkg install pkg:/developer/assembler".])
783    fi
784  else
785    # FIXME: is this correct for microsoft?
786    AS="$CC -c"
787  fi
788  AC_SUBST(AS)
789
790  #
791  # Setup the archiver (AR)
792  #
793  if test "x$TOOLCHAIN_TYPE" = xmicrosoft; then
794    # The corresponding ar tool is lib.exe (used to create static libraries)
795    AC_CHECK_PROG([AR], [lib$EXE_SUFFIX],[lib$EXE_SUFFIX],,,)
796  elif test "x$TOOLCHAIN_TYPE" = xgcc; then
797    BASIC_CHECK_TOOLS(AR, ar gcc-ar)
798  else
799    BASIC_CHECK_TOOLS(AR, ar)
800  fi
801  BASIC_FIXUP_EXECUTABLE(AR)
802])
803
804# Setup additional tools that is considered a part of the toolchain, but not the
805# core part. Many of these are highly platform-specific and do not exist,
806# and/or are not needed on all platforms.
807AC_DEFUN_ONCE([TOOLCHAIN_DETECT_TOOLCHAIN_EXTRA],
808[
809  if test "x$OPENJDK_TARGET_OS" = "xmacosx"; then
810    BASIC_PATH_PROGS(LIPO, lipo)
811    BASIC_FIXUP_EXECUTABLE(LIPO)
812    BASIC_REQUIRE_PROGS(OTOOL, otool)
813    BASIC_FIXUP_EXECUTABLE(OTOOL)
814    BASIC_REQUIRE_PROGS(INSTALL_NAME_TOOL, install_name_tool)
815    BASIC_FIXUP_EXECUTABLE(INSTALL_NAME_TOOL)
816  fi
817
818  if test "x$TOOLCHAIN_TYPE" = xmicrosoft; then
819    AC_CHECK_PROG([MT], [mt$EXE_SUFFIX], [mt$EXE_SUFFIX],,, [/usr/bin/mt])
820    BASIC_FIXUP_EXECUTABLE(MT)
821    # Setup the resource compiler (RC)
822    AC_CHECK_PROG([RC], [rc$EXE_SUFFIX], [rc$EXE_SUFFIX],,, [/usr/bin/rc])
823    BASIC_FIXUP_EXECUTABLE(RC)
824    AC_CHECK_PROG([DUMPBIN], [dumpbin$EXE_SUFFIX], [dumpbin$EXE_SUFFIX],,,)
825    BASIC_FIXUP_EXECUTABLE(DUMPBIN)
826    # We need to check for 'msbuild.exe' because at the place where we expect to
827    # find 'msbuild.exe' there's also a directory called 'msbuild' and configure
828    # won't find the 'msbuild.exe' executable in that case (and the
829    # 'ac_executable_extensions' is unusable due to performance reasons).
830    # Notice that we intentionally don't fix up the path to MSBUILD because we
831    # will call it in a DOS shell during freetype detection on Windows (see
832    # 'LIB_SETUP_FREETYPE' in "libraries.m4"
833    AC_CHECK_PROG([MSBUILD], [msbuild$EXE_SUFFIX], [msbuild$EXE_SUFFIX],,,)
834  fi
835
836  if test "x$OPENJDK_TARGET_OS" = xsolaris; then
837    BASIC_PATH_PROGS(STRIP, strip)
838    BASIC_FIXUP_EXECUTABLE(STRIP)
839    BASIC_PATH_PROGS(NM, nm)
840    BASIC_FIXUP_EXECUTABLE(NM)
841    BASIC_PATH_PROGS(GNM, gnm)
842    BASIC_FIXUP_EXECUTABLE(GNM)
843  elif test "x$OPENJDK_TARGET_OS" != xwindows; then
844    # FIXME: we should unify this with the solaris case above.
845    BASIC_CHECK_TOOLS(STRIP, strip)
846    BASIC_FIXUP_EXECUTABLE(STRIP)
847    if test "x$TOOLCHAIN_TYPE" = xgcc; then
848      BASIC_CHECK_TOOLS(NM, nm gcc-nm)
849    else
850      BASIC_CHECK_TOOLS(NM, nm)
851    fi
852    BASIC_FIXUP_EXECUTABLE(NM)
853    GNM="$NM"
854    AC_SUBST(GNM)
855  fi
856
857  # objcopy is used for moving debug symbols to separate files when
858  # full debug symbols are enabled.
859  if test "x$OPENJDK_TARGET_OS" = xsolaris || test "x$OPENJDK_TARGET_OS" = xlinux || test "x$OPENJDK_TARGET_OS" = xbsd; then
860    BASIC_CHECK_TOOLS(OBJCOPY, [gobjcopy objcopy])
861    # Only call fixup if objcopy was found.
862    if test -n "$OBJCOPY"; then
863      BASIC_FIXUP_EXECUTABLE(OBJCOPY)
864      if test "x$OPENJDK_BUILD_OS" = xsolaris; then
865        # objcopy prior to 2.21.1 on solaris is broken and is not usable.
866        # Rewrite objcopy version output to VALID_VERSION or BAD_VERSION.
867        # - version number is last blank separate word on first line
868        # - version number formats that have been seen:
869        #   - <major>.<minor>
870        #   - <major>.<minor>.<micro>
871        OBJCOPY_VERSION=`$OBJCOPY --version | $HEAD -n 1`
872        # The outer [ ] is to prevent m4 from eating the [] in the sed expression.
873        [ OBJCOPY_VERSION_CHECK=`$ECHO $OBJCOPY_VERSION | $SED -n \
874              -e 's/.* //' \
875              -e '/^[01]\./b bad' \
876              -e '/^2\./{' \
877              -e '  s/^2\.//' \
878              -e '  /^[0-9]$/b bad' \
879              -e '  /^[0-9]\./b bad' \
880              -e '  /^1[0-9]$/b bad' \
881              -e '  /^1[0-9]\./b bad' \
882              -e '  /^20\./b bad' \
883              -e '  /^21\.0$/b bad' \
884              -e '  /^21\.0\./b bad' \
885              -e '}' \
886              -e ':good' \
887              -e 's/.*/VALID_VERSION/p' \
888              -e 'q' \
889              -e ':bad' \
890              -e 's/.*/BAD_VERSION/p' \
891              -e 'q'` ]
892        if test "x$OBJCOPY_VERSION_CHECK" = xBAD_VERSION; then
893          OBJCOPY=
894          AC_MSG_WARN([Ignoring found objcopy since it is broken (prior to 2.21.1). No debug symbols will be generated.])
895          AC_MSG_NOTICE([objcopy reports version $OBJCOPY_VERSION])
896          AC_MSG_NOTICE([Note: patch 149063-01 or newer contains the correct Solaris 10 SPARC version])
897          AC_MSG_NOTICE([Note: patch 149064-01 or newer contains the correct Solaris 10 X86 version])
898          AC_MSG_NOTICE([Note: Solaris 11 Update 1 contains the correct version])
899        fi
900      fi
901    fi
902  fi
903
904  BASIC_CHECK_TOOLS(OBJDUMP, [gobjdump objdump])
905  if test "x$OBJDUMP" != x; then
906    # Only used for compare.sh; we can live without it. BASIC_FIXUP_EXECUTABLE
907    # bails if argument is missing.
908    BASIC_FIXUP_EXECUTABLE(OBJDUMP)
909  fi
910
911  case $TOOLCHAIN_TYPE in
912    gcc|clang|solstudio)
913      BASIC_CHECK_TOOLS(CXXFILT, [c++filt])
914      BASIC_CHECK_NONEMPTY(CXXFILT)
915      BASIC_FIXUP_EXECUTABLE(CXXFILT)
916      ;;
917  esac
918])
919
920# Setup the build tools (i.e, the compiler and linker used to build programs
921# that should be run on the build platform, not the target platform, as a build
922# helper). Since the non-cross-compile case uses the normal, target compilers
923# for this, we can only do this after these have been setup.
924AC_DEFUN_ONCE([TOOLCHAIN_SETUP_BUILD_COMPILERS],
925[
926  if test "x$COMPILE_TYPE" = "xcross"; then
927    # Now we need to find a C/C++ compiler that can build executables for the
928    # build platform. We can't use the AC_PROG_CC macro, since it can only be
929    # used once. Also, we need to do this without adding a tools dir to the
930    # path, otherwise we might pick up cross-compilers which don't use standard
931    # naming.
932
933    OLDPATH="$PATH"
934
935    AC_ARG_WITH(build-devkit, [AS_HELP_STRING([--with-build-devkit],
936        [Devkit to use for the build platform toolchain])])
937    if test "x$with_build_devkit" = "xyes"; then
938      AC_MSG_ERROR([--with-build-devkit must have a value])
939    elif test -n "$with_build_devkit"; then
940      if test ! -d "$with_build_devkit"; then
941        AC_MSG_ERROR([--with-build-devkit points to non existing dir: $with_build_devkit])
942      else
943        BASIC_FIXUP_PATH([with_build_devkit])
944        BUILD_DEVKIT_ROOT="$with_build_devkit"
945        # Check for a meta data info file in the root of the devkit
946        if test -f "$BUILD_DEVKIT_ROOT/devkit.info"; then
947          # Process devkit.info so that existing devkit variables are not
948          # modified by this
949          $SED -e "s/^DEVKIT_/BUILD_DEVKIT_/g" \
950              -e "s/\$DEVKIT_ROOT/\$BUILD_DEVKIT_ROOT/g" \
951              -e "s/\$host/\$build/g" \
952              $BUILD_DEVKIT_ROOT/devkit.info \
953              > $CONFIGURESUPPORT_OUTPUTDIR/build-devkit.info
954          . $CONFIGURESUPPORT_OUTPUTDIR/build-devkit.info
955          # This potentially sets the following:
956          # A descriptive name of the devkit
957          BASIC_EVAL_DEVKIT_VARIABLE([BUILD_DEVKIT_NAME])
958          # Corresponds to --with-extra-path
959          BASIC_EVAL_DEVKIT_VARIABLE([BUILD_DEVKIT_EXTRA_PATH])
960          # Corresponds to --with-toolchain-path
961          BASIC_EVAL_DEVKIT_VARIABLE([BUILD_DEVKIT_TOOLCHAIN_PATH])
962          # Corresponds to --with-sysroot
963          BASIC_EVAL_DEVKIT_VARIABLE([BUILD_DEVKIT_SYSROOT])
964          # Skip the Window specific parts
965        fi
966
967        AC_MSG_CHECKING([for build platform devkit])
968        if test "x$BUILD_DEVKIT_NAME" != x; then
969          AC_MSG_RESULT([$BUILD_DEVKIT_NAME in $BUILD_DEVKIT_ROOT])
970        else
971          AC_MSG_RESULT([$BUILD_DEVKIT_ROOT])
972        fi
973
974        BUILD_SYSROOT="$BUILD_DEVKIT_SYSROOT"
975
976         # Fallback default of just /bin if DEVKIT_PATH is not defined
977        if test "x$BUILD_DEVKIT_TOOLCHAIN_PATH" = x; then
978          BUILD_DEVKIT_TOOLCHAIN_PATH="$BUILD_DEVKIT_ROOT/bin"
979        fi
980        PATH="$BUILD_DEVKIT_TOOLCHAIN_PATH:$BUILD_DEVKIT_EXTRA_PATH"
981      fi
982    fi
983
984    # FIXME: we should list the discovered compilers as an exclude pattern!
985    # If we do that, we can do this detection before POST_DETECTION, and still
986    # find the build compilers in the tools dir, if needed.
987    BASIC_REQUIRE_PROGS(BUILD_CC, [cl cc gcc])
988    BASIC_FIXUP_EXECUTABLE(BUILD_CC)
989    BASIC_REQUIRE_PROGS(BUILD_CXX, [cl CC g++])
990    BASIC_FIXUP_EXECUTABLE(BUILD_CXX)
991    BASIC_PATH_PROGS(BUILD_NM, nm gcc-nm)
992    BASIC_FIXUP_EXECUTABLE(BUILD_NM)
993    BASIC_PATH_PROGS(BUILD_AR, ar gcc-ar)
994    BASIC_FIXUP_EXECUTABLE(BUILD_AR)
995    BASIC_PATH_PROGS(BUILD_OBJCOPY, objcopy)
996    BASIC_FIXUP_EXECUTABLE(BUILD_OBJCOPY)
997    BASIC_PATH_PROGS(BUILD_STRIP, strip)
998    BASIC_FIXUP_EXECUTABLE(BUILD_STRIP)
999    # Assume the C compiler is the assembler
1000    BUILD_AS="$BUILD_CC -c"
1001    # Just like for the target compiler, use the compiler as linker
1002    BUILD_LD="$BUILD_CC"
1003    BUILD_LDCXX="$BUILD_CXX"
1004
1005    PATH="$OLDPATH"
1006
1007    TOOLCHAIN_EXTRACT_COMPILER_VERSION(BUILD_CC, [BuildC])
1008    TOOLCHAIN_EXTRACT_COMPILER_VERSION(BUILD_CXX, [BuildC++])
1009    TOOLCHAIN_PREPARE_FOR_VERSION_COMPARISONS([BUILD_], [OPENJDK_BUILD_])
1010    TOOLCHAIN_EXTRACT_LD_VERSION(BUILD_LD, [build linker])
1011    TOOLCHAIN_PREPARE_FOR_LD_VERSION_COMPARISONS([BUILD_], [OPENJDK_BUILD_])
1012  else
1013    # If we are not cross compiling, use the normal target compilers for
1014    # building the build platform executables.
1015    BUILD_CC="$CC"
1016    BUILD_CXX="$CXX"
1017    BUILD_LD="$LD"
1018    BUILD_LDCXX="$LDCXX"
1019    BUILD_NM="$NM"
1020    BUILD_AS="$AS"
1021    BUILD_OBJCOPY="$OBJCOPY"
1022    BUILD_STRIP="$STRIP"
1023    BUILD_AR="$AR"
1024
1025    TOOLCHAIN_PREPARE_FOR_VERSION_COMPARISONS([], [OPENJDK_BUILD_])
1026    TOOLCHAIN_PREPARE_FOR_LD_VERSION_COMPARISONS([BUILD_], [OPENJDK_BUILD_])
1027  fi
1028
1029  AC_SUBST(BUILD_CC)
1030  AC_SUBST(BUILD_CXX)
1031  AC_SUBST(BUILD_LD)
1032  AC_SUBST(BUILD_LDCXX)
1033  AC_SUBST(BUILD_NM)
1034  AC_SUBST(BUILD_AS)
1035  AC_SUBST(BUILD_AR)
1036])
1037
1038# Do some additional checks on the detected tools.
1039AC_DEFUN_ONCE([TOOLCHAIN_MISC_CHECKS],
1040[
1041  # Check for extra potential brokenness.
1042  if test  "x$TOOLCHAIN_TYPE" = xmicrosoft; then
1043    # On Windows, double-check that we got the right compiler.
1044    CC_VERSION_OUTPUT=`$CC 2>&1 | $GREP -v 'ERROR.*UtilTranslatePathList' | $HEAD -n 1 | $TR -d '\r'`
1045    COMPILER_CPU_TEST=`$ECHO $CC_VERSION_OUTPUT | $SED -n "s/^.* \(.*\)$/\1/p"`
1046    if test "x$OPENJDK_TARGET_CPU" = "xx86"; then
1047      if test "x$COMPILER_CPU_TEST" != "x80x86" -a "x$COMPILER_CPU_TEST" != "xx86"; then
1048        AC_MSG_ERROR([Target CPU mismatch. We are building for $OPENJDK_TARGET_CPU but CL is for "$COMPILER_CPU_TEST"; expected "80x86" or "x86".])
1049      fi
1050    elif test "x$OPENJDK_TARGET_CPU" = "xx86_64"; then
1051      if test "x$COMPILER_CPU_TEST" != "xx64"; then
1052        AC_MSG_ERROR([Target CPU mismatch. We are building for $OPENJDK_TARGET_CPU but CL is for "$COMPILER_CPU_TEST"; expected "x64".])
1053      fi
1054    fi
1055  fi
1056
1057  if test "x$TOOLCHAIN_TYPE" = xgcc; then
1058    # If this is a --hash-style=gnu system, use --hash-style=both, why?
1059    HAS_GNU_HASH=`$CC -dumpspecs 2>/dev/null | $GREP 'hash-style=gnu'`
1060    # This is later checked when setting flags.
1061  fi
1062
1063  if test "x$TOOLCHAIN_TYPE" = xgcc || test "x$TOOLCHAIN_TYPE" = xclang; then
1064    # Check if linker has -z noexecstack.
1065    HAS_NOEXECSTACK=`$CC -Wl,--help 2>/dev/null | $GREP 'z noexecstack'`
1066    # This is later checked when setting flags.
1067  fi
1068
1069  # Setup hotspot lecagy names for toolchains
1070  HOTSPOT_TOOLCHAIN_TYPE=$TOOLCHAIN_TYPE
1071  if test "x$TOOLCHAIN_TYPE" = xclang; then
1072    HOTSPOT_TOOLCHAIN_TYPE=gcc
1073  elif test "x$TOOLCHAIN_TYPE" = xmicrosoft; then
1074    HOTSPOT_TOOLCHAIN_TYPE=visCPP
1075  fi
1076  AC_SUBST(HOTSPOT_TOOLCHAIN_TYPE)
1077])
1078
1079# Setup the JTReg Regression Test Harness.
1080AC_DEFUN_ONCE([TOOLCHAIN_SETUP_JTREG],
1081[
1082  AC_ARG_WITH(jtreg, [AS_HELP_STRING([--with-jtreg],
1083      [Regression Test Harness @<:@probed@:>@])])
1084
1085  if test "x$with_jtreg" = xno; then
1086    # jtreg disabled
1087    AC_MSG_CHECKING([for jtreg test harness])
1088    AC_MSG_RESULT([no, disabled])
1089  elif test "x$with_jtreg" != xyes && test "x$with_jtreg" != x; then
1090    # An explicit path is specified, use it.
1091    JT_HOME="$with_jtreg"
1092    BASIC_FIXUP_PATH([JT_HOME])
1093    if test ! -d "$JT_HOME"; then
1094      AC_MSG_ERROR([jtreg home directory from --with-jtreg=$with_jtreg does not exist])
1095    fi
1096
1097    if test ! -e "$JT_HOME/lib/jtreg.jar"; then
1098      AC_MSG_ERROR([jtreg home directory from --with-jtreg=$with_jtreg is not a valid jtreg home])
1099    fi
1100
1101    JTREGEXE="$JT_HOME/bin/jtreg"
1102    if test ! -x "$JTREGEXE"; then
1103      AC_MSG_ERROR([jtreg home directory from --with-jtreg=$with_jtreg does not contain valid jtreg executable])
1104    fi
1105
1106    AC_MSG_CHECKING([for jtreg test harness])
1107    AC_MSG_RESULT([$JT_HOME])
1108  else
1109    # Try to locate jtreg
1110    if test "x$JT_HOME" != x; then
1111      # JT_HOME set in environment, use it
1112      if test ! -d "$JT_HOME"; then
1113        AC_MSG_WARN([Ignoring JT_HOME pointing to invalid directory: $JT_HOME])
1114        JT_HOME=
1115      else
1116        if test ! -e "$JT_HOME/lib/jtreg.jar"; then
1117          AC_MSG_WARN([Ignoring JT_HOME which is not a valid jtreg home: $JT_HOME])
1118          JT_HOME=
1119        elif test ! -x "$JT_HOME/bin/jtreg"; then
1120          AC_MSG_WARN([Ignoring JT_HOME which does not contain valid jtreg executable: $JT_HOME])
1121          JT_HOME=
1122        else
1123          JTREGEXE="$JT_HOME/bin/jtreg"
1124          AC_MSG_NOTICE([Located jtreg using JT_HOME from environment])
1125        fi
1126      fi
1127    fi
1128
1129    if test "x$JT_HOME" = x; then
1130      # JT_HOME is not set in environment, or was deemed invalid.
1131      # Try to find jtreg on path
1132      BASIC_PATH_PROGS(JTREGEXE, jtreg)
1133      if test "x$JTREGEXE" != x; then
1134        # That's good, now try to derive JT_HOME
1135        JT_HOME=`(cd $($DIRNAME $JTREGEXE)/.. && pwd)`
1136        if test ! -e "$JT_HOME/lib/jtreg.jar"; then
1137          AC_MSG_WARN([Ignoring jtreg from path since a valid jtreg home cannot be found])
1138          JT_HOME=
1139          JTREGEXE=
1140        else
1141          AC_MSG_NOTICE([Located jtreg using jtreg executable in path])
1142        fi
1143      fi
1144    fi
1145
1146    AC_MSG_CHECKING([for jtreg test harness])
1147    if test "x$JT_HOME" != x; then
1148      AC_MSG_RESULT([$JT_HOME])
1149    else
1150      AC_MSG_RESULT([no, not found])
1151
1152      if test "x$with_jtreg" = xyes; then
1153        AC_MSG_ERROR([--with-jtreg was specified, but no jtreg found.])
1154      fi
1155    fi
1156  fi
1157
1158  BASIC_FIXUP_EXECUTABLE(JTREGEXE)
1159  BASIC_FIXUP_PATH(JT_HOME)
1160  AC_SUBST(JT_HOME)
1161  AC_SUBST(JTREGEXE)
1162])
1163
1164# Setup the JIB dependency resolver
1165AC_DEFUN_ONCE([TOOLCHAIN_SETUP_JIB],
1166[
1167  AC_ARG_WITH(jib, [AS_HELP_STRING([--with-jib],
1168      [Jib dependency management tool @<:@not used@:>@])])
1169
1170  if test "x$with_jib" = xno || test "x$with_jib" = x; then
1171    # jib disabled
1172    AC_MSG_CHECKING([for jib])
1173    AC_MSG_RESULT(no)
1174  elif test "x$with_jib" = xyes; then
1175    AC_MSG_ERROR([Must supply a value to --with-jib])
1176  else
1177    JIB_HOME="${with_jib}"
1178    AC_MSG_CHECKING([for jib])
1179    AC_MSG_RESULT(${JIB_HOME})
1180    if test ! -d "${JIB_HOME}"; then
1181      AC_MSG_ERROR([--with-jib must be a directory])
1182    fi
1183    JIB_JAR=$(ls ${JIB_HOME}/lib/jib-*.jar)
1184    if test ! -f "${JIB_JAR}"; then
1185      AC_MSG_ERROR([Could not find jib jar file in ${JIB_HOME}])
1186    fi
1187  fi
1188
1189  AC_SUBST(JIB_HOME)
1190])
1191