From b16a7e10a025e7862ecfb3cdb3a38de412122f46 Mon Sep 17 00:00:00 2001 From: Laszlo Csomor Date: Tue, 6 Jun 2017 10:51:45 -0400 Subject: java_stub_template: remove cygpath call Remove the cygpath call on non-Windows platforms, that was recently added by 102ce6ddc04063d26165c6a2167059f2348026bf Also add a test for Bazel's Java launcher. Also update the testenv.sh:cleanup method to wait for Bazel to shut down, don't give up immediately if it could not clean up the inner Bazel's temp dir. Fixes https://github.com/bazelbuild/bazel/issues/3092 See https://github.com/bazelbuild/bazel/issues/3069 Change-Id: I82b1026a60056f340caa53a59b6f2ec8a1397ef3 PiperOrigin-RevId: 158139846 --- .../lib/bazel/rules/java/java_stub_template.txt | 55 ++++++++++------ src/test/shell/bazel/BUILD | 8 +++ src/test/shell/bazel/java_launcher_test.sh | 76 ++++++++++++++++++++++ src/test/shell/testenv.sh | 13 +++- 4 files changed, 130 insertions(+), 22 deletions(-) create mode 100644 src/test/shell/bazel/java_launcher_test.sh (limited to 'src') diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt index 60e1f712d1..ba35a95e46 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt @@ -37,6 +37,10 @@ # --singlejar Start the program from the packed-up deployment # jar rather than from the classpath. # --print_javabin Print the location of java executable binary and exit. +# --classpath_limit= +# Specify the maximum classpath length. If the classpath +# is shorter, this script passes it to Java as a command +# line flag, otherwise it creates a classpath jar. # # The remainder of the command line is passed to the program. @@ -62,6 +66,11 @@ function process_wrapper_argument() { --jvm_flags=*) JVM_FLAGS_CMDLINE+=( ${1#--jvm_flags=} ) ;; --singlejar) SINGLEJAR=1 ;; --print_javabin) PRINT_JAVABIN=1 ;; + --classpath_limit=*) + CLASSPATH_LIMIT="${1#--classpath_limit=}" + echo "$CLASSPATH_LIMIT" | grep -q '^[0-9]\+$' || \ + die "ERROR: $self failed, --classpath_limit is not a number" + ;; *) return 1 ;; esac @@ -75,8 +84,7 @@ die() { # Windows function is_windows() { - # On windows, the shell test actually running on msys - [[ "${OSTYPE}" =~ msys* ]] + [[ "${OSTYPE}" =~ msys* ]] || [[ "${OSTYPE}" =~ cygwin* ]] } # macOS @@ -268,11 +276,16 @@ ARGS=( function create_and_run_classpath_jar() { # Build class path as one single string separated by spaces MANIFEST_CLASSPATH="" - IFS=';' - for x in $CLASSPATH - do - # Add file:/ prefix and escaped space characters, it should be a uri. - x="file:/${x// /%20}" + if is_windows; then + IFS=';' + URI_PREFIX="file:/" # e.g. "file:/C:/temp/foo.jar" + else + IFS=':' + URI_PREFIX="file:$(pwd)/" # e.g. "file:/usr/local/foo.jar" + fi + for x in $CLASSPATH; do + # Add file:/ prefix and escaped space characters, it should be a URI. + x="${URI_PREFIX}${x// /%20}" MANIFEST_CLASSPATH="$MANIFEST_CLASSPATH $x" done unset IFS @@ -294,27 +307,27 @@ function create_and_run_classpath_jar() { echo "Created-By: Bazel" >>$MANIFEST_FILE # Create classpath JAR file - MANIFEST_JAR_FILE="$1" - JARBIN=$2 - $JARBIN cvfm "$MANIFEST_JAR_FILE" "$MANIFEST_FILE" >/dev/null \ - || { echo >$2 "ERROR: $0 failed because $JARBIN failed" ; exit 1 ; } + MANIFEST_JAR_FILE="${self}-classpath.jar" + is_windows && MANIFEST_JAR_FILE="$(cygpath --windows "$MANIFEST_JAR_FILE")" + JARBIN="${JARBIN:-$(rlocation "$1")}" + $JARBIN cvfm "$MANIFEST_JAR_FILE" "$MANIFEST_FILE" >/dev/null || \ + die "ERROR: $self failed because $JARBIN failed" # Execute JAVA command - MANIFEST_JAR_FILE=$(cygpath --windows "$MANIFEST_JAR_FILE") $JAVABIN -classpath "$MANIFEST_JAR_FILE" "${ARGS[@]}" } - -if is_windows && (("${#CLASSPATH}" > 7000)); then +# If the user didn't specify a --classpath_limit, use the default value. +if [ -z "$CLASSPATH_LIMIT" ]; then # Windows per-arg limit MAX_ARG_STRLEN == 8k - create_and_run_classpath_jar \ - "$(cygpath --windows "${self}-classpath.jar")" \ - "${JARBIN:-$(rlocation local_jdk/bin/jar.exe)}" -elif (("${#CLASSPATH}" > 120000)); then # Linux per-arg limit MAX_ARG_STRLEN == 128k - create_and_run_classpath_jar \ - "${self}-classpath.jar" \ - "${JARBIN:-$(rlocation local_jdk/bin/jar)}" + is_windows && CLASSPATH_LIMIT=7000 || CLASSPATH_LIMIT=120000 +fi + +if is_windows && (("${#CLASSPATH}" > ${CLASSPATH_LIMIT} )); then + create_and_run_classpath_jar "local_jdk/bin/jar.exe" +elif (("${#CLASSPATH}" > ${CLASSPATH_LIMIT})); then + create_and_run_classpath_jar "local_jdk/bin/jar" else exec $JAVABIN -classpath $CLASSPATH "${ARGS[@]}" fi diff --git a/src/test/shell/bazel/BUILD b/src/test/shell/bazel/BUILD index 088c5a364f..30a19efa2f 100644 --- a/src/test/shell/bazel/BUILD +++ b/src/test/shell/bazel/BUILD @@ -453,6 +453,14 @@ sh_test( data = [":test-deps"], ) +sh_test( + name = "java_launcher_test", + size = "medium", + srcs = ["java_launcher_test.sh"], + args = ["$(JAVABASE)"], + data = [":test-deps"], +) + test_suite( name = "all_tests", visibility = ["//visibility:public"], diff --git a/src/test/shell/bazel/java_launcher_test.sh b/src/test/shell/bazel/java_launcher_test.sh new file mode 100644 index 0000000000..87afe7a585 --- /dev/null +++ b/src/test/shell/bazel/java_launcher_test.sh @@ -0,0 +1,76 @@ +#!/bin/bash +# +# Copyright 2017 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if ! type rlocation &> /dev/null; then + # rlocation is a function defined in testenv.sh + exit 1 +fi + +source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \ + || { echo "integration_test_setup.sh not found!" >&2; exit 1; } + +set -eu + +add_to_bazelrc "build --package_path=%workspace%" + + +function test_java_launcher_classpath_limit() { + local -r pkg="${FUNCNAME[0]}" + mkdir -p $pkg/java/hello || fail "Expected success" + cat > $pkg/java/hello/HelloLib.java < $pkg/java/hello/Hello.java < $pkg/java/hello/BUILD <& "$TEST_log" || \ + fail "expected success" + expect_log "Hello World!" + [ -e "${PRODUCT_NAME}-bin/$pkg/java/hello/hello-classpath.jar" ] && \ + fail "did not expect to create classpath jar" + + ${PRODUCT_NAME}-bin/$pkg/java/hello/hello --classpath_limit=0 >& "$TEST_log" || \ + fail "expected success" + expect_log "Hello World!" + [ -e "${PRODUCT_NAME}-bin/$pkg/java/hello/hello-classpath.jar" ] || \ + fail "expected to create classpath jar" +} + +run_suite "Java launcher tests" + diff --git a/src/test/shell/testenv.sh b/src/test/shell/testenv.sh index 26c4e9f2ef..b1099970e6 100755 --- a/src/test/shell/testenv.sh +++ b/src/test/shell/testenv.sh @@ -429,7 +429,18 @@ function cleanup_workspace() { # Clean-up the bazel install base function cleanup() { if [ -d "${BAZEL_INSTALL_BASE:-__does_not_exists__}" ]; then - rm -fr "${BAZEL_INSTALL_BASE}" + # Windows takes its time to shut down Bazel and we can't delete A-server.jar + # until then, so just give it time and keep trying for 2 minutes. + for i in {1..120}; do + if rm -fr "${BAZEL_INSTALL_BASE}" ; then + break + fi + if (( $i == 10 )) || (( $i == 30 )) || (( $i == 60 )) ; then + echo "Test cleanup: couldn't delete ${BAZEL_INSTALL_BASE} \ after $i seconds" + echo "(Timeout in $((120-$i)) seconds.)" + sleep 1 + fi + done fi } -- cgit v1.2.3