1#!/bin/bash
2#
3# Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
4# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5#
6# This code is free software; you can redistribute it and/or modify it
7# under the terms of the GNU General Public License version 2 only, as
8# published by the Free Software Foundation.
9#
10# This code is distributed in the hope that it will be useful, but WITHOUT
11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13# version 2 for more details (a copy is included in the LICENSE file that
14# accompanied this code).
15#
16# You should have received a copy of the GNU General Public License version
17# 2 along with this work; if not, write to the Free Software Foundation,
18# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21# or visit www.oracle.com if you need additional information or have any
22# questions.
23#
24
25if test "x$1" != xCHECKME; then
26  echo "ERROR: Calling this wrapper script directly is not supported."
27  echo "Use the 'configure' script in the top-level directory instead."
28  exit 1
29fi
30
31# The next argument is the absolute top-level directory path.
32# The TOPDIR variable is passed on to configure.ac.
33TOPDIR="$2"
34# Remove these two arguments to get to the user supplied arguments
35shift
36shift
37
38if test "x$BASH" = x; then
39  echo "Error: This script must be run using bash." 1>&2
40  exit 1
41fi
42# Force autoconf to use bash. This also means we must disable autoconf re-exec.
43export CONFIG_SHELL=$BASH
44export _as_can_reexec=no
45
46# Make sure all shell commands are executed with the C locale
47export LC_ALL=C
48
49if test "x$CUSTOM_CONFIG_DIR" != x; then
50  custom_hook=$CUSTOM_CONFIG_DIR/custom-hook.m4
51  if test ! -e $custom_hook; then
52    echo "CUSTOM_CONFIG_DIR not pointing to a proper custom config dir."
53    echo "Error: Cannot continue" 1>&2
54    exit 1
55  fi
56fi
57
58CURRENT_DIR=`pwd`
59if test "x$CURRENT_DIR" = "x$TOPDIR"; then
60  # We are running configure from the src root.
61  # Create '.configure-support' under $TOPDIR/build
62  build_support_dir="$TOPDIR/build/.configure-support"
63elif test "x$CURRENT_DIR" = "x$CUSTOM_ROOT"; then
64  # We are running configure from the custom root.
65  # Create '.configure-support' under $CUSTOM_ROOT/build
66  build_support_dir="$CUSTOM_ROOT/build/.configure-support"
67else
68  # We are running configure from outside of the src dir.
69  # Create 'build_support_dir' in the current directory.
70  build_support_dir="$CURRENT_DIR/configure-support"
71fi
72
73conf_script_dir="$TOPDIR/make/autoconf"
74generated_script="$build_support_dir/generated-configure.sh"
75
76###
77### Use autoconf to create a runnable configure script, if needed
78###
79
80autoconf_missing_help() {
81  APT_GET="`which apt-get 2> /dev/null | grep -v '^no apt-get in'`"
82  YUM="`which yum 2> /dev/null | grep -v '^no yum in'`"
83  BREW="`which brew 2> /dev/null | grep -v '^no brew in'`"
84  ZYPPER="`which zypper 2> /dev/null | grep -v '^no zypper in'`"
85  CYGWIN="`which cygpath 2> /dev/null | grep -v '^no cygpath in'`"
86
87  if test "x$ZYPPER" != x; then
88    PKGHANDLER_COMMAND="sudo zypper install autoconf"
89  elif test "x$APT_GET" != x; then
90    PKGHANDLER_COMMAND="sudo apt-get install autoconf"
91  elif test "x$YUM" != x; then
92    PKGHANDLER_COMMAND="sudo yum install autoconf"
93  elif test "x$BREW" != x; then
94    PKGHANDLER_COMMAND="brew install autoconf"
95  elif test "x$CYGWIN" != x; then
96    PKGHANDLER_COMMAND="( cd <location of cygwin setup.exe> && cmd /c setup -q -P autoconf )"
97  fi
98
99  if test "x$PKGHANDLER_COMMAND" != x; then
100    echo "You might be able to fix this by running '$PKGHANDLER_COMMAND'."
101  fi
102}
103
104generate_configure_script() {
105  if test "x$AUTOCONF" != x; then
106    if test ! -x "$AUTOCONF"; then
107      echo
108      echo "The specified AUTOCONF variable does not point to a valid autoconf executable:"
109      echo "AUTOCONF=$AUTOCONF"
110      echo "Error: Cannot continue" 1>&2
111      exit 1
112    fi
113  else
114    AUTOCONF="`which autoconf 2> /dev/null | grep -v '^no autoconf in'`"
115    if test "x$AUTOCONF" = x; then
116      echo
117      echo "Autoconf is not found on the PATH, and AUTOCONF is not set."
118      echo "You need autoconf to be able to generate a runnable configure script."
119      autoconf_missing_help
120      echo "Error: Cannot find autoconf" 1>&2
121      exit 1
122    fi
123  fi
124
125  autoconf_version=`$AUTOCONF --version | head -1`
126  echo "Using autoconf at ${AUTOCONF} [$autoconf_version]"
127
128  if test "x$CUSTOM_CONFIG_DIR" != x; then
129    # Generate configure script with custom hooks compiled in.
130    custom_patcher='sed -e "s|#CUSTOM_AUTOCONF_INCLUDE|m4_include([$custom_hook])|"'
131    custom_script_dir_include="-I$CUSTOM_CONFIG_DIR"
132  else
133    custom_patcher='cat'
134    custom_script_dir_include=""
135  fi
136
137  mkdir -p $build_support_dir
138  # Call autoconf but replace the "magic" variable in configure.ac if requested.
139
140  cat $conf_script_dir/configure.ac | eval $custom_patcher | \
141      ${AUTOCONF} -W all $custom_script_dir_include -I$conf_script_dir - \
142      > $generated_script
143  rm -rf autom4te.cache
144
145  # Sanity check
146  if test ! -s $generated_script; then
147    echo "Error: Failed to generate runnable configure script" 1>&2
148    rm -f $generated_script
149    exit 1
150  fi
151}
152
153test_generated_up_to_date() {
154  conf_source_files="$conf_script_dir/configure.ac $conf_script_dir/*.m4"
155  if test "x$CUSTOM_CONFIG_DIR" != x; then
156    conf_custom_source_files="$CUSTOM_CONFIG_DIR/*.m4"
157  else
158    conf_custom_source_files=""
159  fi
160
161  for file in $conf_source_files $conf_custom_source_files ; do
162    if test $file -nt $generated_script; then
163      return 0
164    fi
165  done
166  return 1
167}
168
169run_autoconf=false
170if test "x$1" = xautogen; then
171  # User called us as "configure autogen", so force regeneration
172  run_autoconf=true
173  shift
174fi
175
176if test ! -s $generated_script; then
177  # Generated script is missing, so we need to create it
178  echo "Runnable configure script is not present"
179  run_autoconf=true
180else
181  # File is present, but is it up to date?
182  if test_generated_up_to_date; then
183    echo "Runnable configure script is not up to date"
184    run_autoconf=true
185  fi
186fi
187
188if test "x$run_autoconf" = xtrue; then
189  echo "Generating runnable configure script at $generated_script"
190  generate_configure_script
191fi
192
193# Autoconf calls the configure script recursively sometimes.
194# Don't start logging twice in that case
195if test "x$conf_debug_configure" = xtrue; then
196  conf_debug_configure=recursive
197fi
198
199###
200### Process command-line arguments
201###
202
203# Returns a shell-escaped version of the argument given.
204function shell_quote() {
205  if [[ -n "$1" ]]; then
206    # Uses only shell-safe characters?  No quoting needed.
207    # '=' is a zsh meta-character, but only in word-initial position.
208    if echo "$1" | grep '^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\.:,%/+=_-]\{1,\}$' > /dev/null \
209        && ! echo "$1" | grep '^=' > /dev/null; then
210      quoted="$1"
211    else
212      if echo "$1" | grep "[\'!]" > /dev/null; then
213        # csh does history expansion within single quotes, but not
214        # when backslash-escaped!
215        local quoted_quote="'\\''" quoted_exclam="'\\!'"
216        word="${1//\'/${quoted_quote}}"
217        word="${1//\!/${quoted_exclam}}"
218      fi
219      quoted="'$1'"
220    fi
221    echo "$quoted"
222  fi
223}
224
225conf_processed_arguments=()
226conf_quoted_arguments=()
227conf_openjdk_target=
228
229for conf_option
230do
231
232  # Process (and remove) our own extensions that will not be passed to autoconf
233  case $conf_option in
234    --openjdk-target=*)
235      conf_openjdk_target=`expr "X$conf_option" : '[^=]*=\(.*\)'`
236      ;;
237    --debug-configure)
238      if test "x$conf_debug_configure" != xrecursive; then
239        conf_debug_configure=true
240        export conf_debug_configure
241      fi
242      ;;
243    *)
244      conf_processed_arguments=("${conf_processed_arguments[@]}" "$conf_option")
245      ;;
246  esac
247
248  # Store all variables overridden on the command line
249  case $conf_option in
250    [^-]*=*)
251      # Add name of variable to CONFIGURE_OVERRIDDEN_VARIABLES list inside !...!.
252      conf_env_var=`expr "x$conf_option" : 'x\([^=]*\)='`
253      CONFIGURE_OVERRIDDEN_VARIABLES="$CONFIGURE_OVERRIDDEN_VARIABLES!$conf_env_var!"
254      ;;
255  esac
256
257  # Save the arguments, intelligently quoted for CONFIGURE_COMMAND_LINE.
258  case $conf_option in
259    *=*)
260      conf_option_name=`expr "x$conf_option" : 'x\([^=]*\)='`
261      conf_option_name=$(shell_quote "$conf_option_name")
262      conf_option_value=`expr "x$conf_option" : 'x[^=]*=\(.*\)'`
263      conf_option_value=$(shell_quote "$conf_option_value")
264      conf_quoted_arguments=("${conf_quoted_arguments[@]}" "$conf_option_name=$conf_option_value")
265      ;;
266    *)
267      conf_quoted_arguments=("${conf_quoted_arguments[@]}" "$(shell_quote "$conf_option")")
268      ;;
269  esac
270
271  # Check for certain autoconf options that require extra action
272  case $conf_option in
273    -build | --build | --buil | --bui | --bu |-build=* | --build=* | --buil=* | --bui=* | --bu=*)
274      conf_legacy_crosscompile="$conf_legacy_crosscompile $conf_option" ;;
275    -target | --target | --targe | --targ | --tar | --ta | --t | -target=* | --target=* | --targe=* | --targ=* | --tar=* | --ta=* | --t=*)
276      conf_legacy_crosscompile="$conf_legacy_crosscompile $conf_option" ;;
277    -host | --host | --hos | --ho | -host=* | --host=* | --hos=* | --ho=*)
278      conf_legacy_crosscompile="$conf_legacy_crosscompile $conf_option" ;;
279    -help | --help | --hel | --he | -h)
280      conf_print_help=true ;;
281  esac
282done
283
284# Save the quoted command line
285CONFIGURE_COMMAND_LINE="${conf_quoted_arguments[@]}"
286
287if test "x$conf_legacy_crosscompile" != "x"; then
288  if test "x$conf_openjdk_target" != "x"; then
289    echo "Error: Specifying --openjdk-target together with autoconf"
290    echo "legacy cross-compilation flags is not supported."
291    echo "You specified: --openjdk-target=$conf_openjdk_target and $conf_legacy_crosscompile."
292    echo "The recommended use is just --openjdk-target."
293    exit 1
294  else
295    echo "Warning: You are using legacy autoconf cross-compilation flags."
296    echo "It is recommended that you use --openjdk-target instead."
297    echo ""
298  fi
299fi
300
301if test "x$conf_openjdk_target" != "x"; then
302  conf_build_platform=`sh $conf_script_dir/build-aux/config.guess`
303  conf_processed_arguments=("--build=$conf_build_platform" "--host=$conf_openjdk_target" "--target=$conf_openjdk_target" "${conf_processed_arguments[@]}")
304fi
305
306# Make configure exit with error on invalid options as default.
307# Can be overridden by --disable-option-checking, since we prepend our argument
308# and later options override earlier.
309conf_processed_arguments=("--enable-option-checking=fatal" "${conf_processed_arguments[@]}")
310
311###
312### Call the configure script
313###
314if test "x$conf_debug_configure" != x; then
315  # Turn on shell debug output if requested (initial or recursive)
316  set -x
317fi
318
319# Now transfer control to the script generated by autoconf. This is where the
320# main work is done.
321RCDIR=`mktemp -dt jdk-build-configure.tmp.XXXXXX` || exit $?
322trap "rm -rf \"$RCDIR\"" EXIT
323conf_logfile=./configure.log
324(exec 3>&1 ; ((. $generated_script "${conf_processed_arguments[@]}" 2>&1 1>&3 ) \
325    ; echo $? > "$RCDIR/rc" ) \
326    | tee -a $conf_logfile 1>&2 ; exec 3>&-) | tee -a $conf_logfile
327
328conf_result_code=`cat "$RCDIR/rc"`
329###
330### Post-processing
331###
332
333if test $conf_result_code -eq 0; then
334  if test "x$conf_print_help" = xtrue; then
335    cat <<EOT
336
337Additional (non-autoconf) OpenJDK Options:
338  --openjdk-target=TARGET cross-compile with TARGET as target platform
339                          (i.e. the one you will run the resulting binary on).
340                          Equivalent to --host=TARGET --target=TARGET
341                          --build=<current platform>
342  --debug-configure       Run the configure script with additional debug
343                          logging enabled.
344
345EOT
346
347    # Print additional help, e.g. a list of toolchains and JVM features.
348    # This must be done by the autoconf script.
349    ( CONFIGURE_PRINT_ADDITIONAL_HELP=true . $generated_script PRINTF=printf )
350
351    cat <<EOT
352
353Please be aware that, when cross-compiling, the OpenJDK configure script will
354generally use 'target' where autoconf traditionally uses 'host'.
355
356Also note that variables must be passed on the command line. Variables in the
357environment will generally be ignored, unlike traditional autoconf scripts.
358EOT
359  fi
360else
361  echo configure exiting with result code $conf_result_code
362fi
363
364exit $conf_result_code
365