Refactor JDK 7 build to use the vendored version of it

Along the path, fix the build for JDK 7 and get rid of
most ugliness in the JDK 7 build. Now simply setting
JAVA_VERSION to 1.7 will build a JDK 7 compatible version.

Fixes #1159.

--
Change-Id: I9599283844a57d9e053f12d37445907f22a9232e
Reviewed-on: https://bazel-review.googlesource.com/#/c/3452
MOS_MIGRATED_REVID=120332747
diff --git a/scripts/bootstrap/bootstrap.sh b/scripts/bootstrap/bootstrap.sh
index 1394b62..8732d98 100755
--- a/scripts/bootstrap/bootstrap.sh
+++ b/scripts/bootstrap/bootstrap.sh
@@ -30,10 +30,17 @@
 
 : ${JAVA_VERSION:="1.8"}
 
-: ${BAZEL_ARGS:=--java_toolchain=//src/java_tools/buildjar:bootstrap_toolchain \
-      --strategy=Javac=worker --worker_quit_after_build \
-      --genrule_strategy=standalone --spawn_strategy=standalone \
-      "${EXTRA_BAZEL_ARGS:-}"}
+if [ "${JAVA_VERSION}" = "1.7" ]; then
+  : ${BAZEL_ARGS:=--java_toolchain=//src/java_tools/buildjar:bootstrap_toolchain_jdk7 \
+        --define JAVA_VERSION=1.7 \
+        --genrule_strategy=standalone --spawn_strategy=standalone \
+        "${EXTRA_BAZEL_ARGS:-}"}
+else
+  : ${BAZEL_ARGS:=--java_toolchain=//src/java_tools/buildjar:bootstrap_toolchain \
+        --strategy=Javac=worker --worker_quit_after_build \
+        --genrule_strategy=standalone --spawn_strategy=standalone \
+        "${EXTRA_BAZEL_ARGS:-}"}
+fi
 
 if [ -z "${BAZEL-}" ]; then
   function bazel_build() {
@@ -82,6 +89,7 @@
       ${EXTRA_BAZEL_ARGS-} \
       --strategy=Javac=worker --worker_quit_after_build \
       --fetch --nostamp \
+      --define "JAVA_VERSION=${JAVA_VERSION}" \
       --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
       ${BAZEL_TARGET} || return $?
   if [ -n "${BAZEL_SUM}" ]; then
diff --git a/scripts/ci/build.sh b/scripts/ci/build.sh
index df7e794..3d63948 100755
--- a/scripts/ci/build.sh
+++ b/scripts/ci/build.sh
@@ -98,45 +98,6 @@
   fi
 }
 
-# Set the various arguments when JDK 7 is required (deprecated).
-# This method is here to continue to build binary release of Bazel
-# for JDK 7. We will drop this method and JDK 7 support when our
-# ci system turn red on this one.
-function setup_jdk7() {
-  # This is a JDK 7 JavaBuilder from release 0.1.0.
-  local javabuilder_url="https://storage.googleapis.com/bazel/0.1.0/JavaBuilder_deploy.jar"
-  local javac_url="https://github.com/bazelbuild/bazel/blob/0.1.0/third_party/java/jdk/langtools/javac.jar?raw=true"
-  sed -i.bak 's/_version = "8"/_version = "7"/' tools/jdk/BUILD
-  rm -f tools/jdk/BUILD.bak
-  rm -f third_party/java/jdk/langtools/javac.jar
-  curl -Ls -o tools/jdk/JavaBuilder_deploy.jar "${javabuilder_url}"
-  curl -Ls -o third_party/java/jdk/langtools/javac.jar "${javac_url}"
-  # Do not use the skylark bootstrapped version of JavaBuilder
-  export BAZEL_ARGS="--singlejar_top=//src/java_tools/singlejar:bootstrap_deploy.jar \
-      --genclass_top=//src/java_tools/buildjar:bootstrap_genclass_deploy.jar \
-      --ijar_top=//third_party/ijar"
-  # Skip building JavaBuilder
-  export BAZEL_SKIP_TOOL_COMPILATION=tools/jdk/JavaBuilder_deploy.jar
-  # Ignore JDK8 tests
-  export BAZEL_TEST_FILTERS="-jdk8"
-  if ! grep -Fq 'RealJavaBuilder' src/java_tools/buildjar/BUILD; then
-    # And more ugly hack. Overwrite the BUILD file of JavaBuilder
-    # so we use the pre-built version in integration tests.
-    sed -i.bak 's/name = \"JavaBuilder\"/name = \"RealJavaBuilder\"/' \
-        src/java_tools/buildjar/BUILD
-    rm -f src/java_tools/buildjar/BUILD.bak
-    cat >>src/java_tools/buildjar/BUILD <<'EOF'
-genrule(
-    name = "JavaBuilder",
-    outs = ["JavaBuilder_deploy.jar"],
-    srcs = ["//tools/jdk:JavaBuilder_deploy.jar"],
-    cmd = "cp $< $@",
-    visibility = ["//visibility:public"],
-)
-EOF
-  fi
-}
-
 # Main entry point for building bazel.
 # It sets the embed label to the release name if any, calls the whole
 # test suite, compile the various packages, then copy the artifacts
@@ -151,7 +112,6 @@
 
   if [[ "${JAVA_VERSION-}" =~ ^(1\.)?7$ ]]; then
     JAVA_VERSION=1.7
-    setup_jdk7
     release_label="${release_label}-jdk7"
   else
     JAVA_VERSION=1.8
diff --git a/scripts/packages/BUILD b/scripts/packages/BUILD
index 2fba80f..e2f5efe 100644
--- a/scripts/packages/BUILD
+++ b/scripts/packages/BUILD
@@ -116,18 +116,13 @@
     stamp = 1,
 )
 
-config_setting(
-    name = "jdk7",
-    values = {"define": "JAVA_VERSION=1.7"},
-)
-
 pkg_deb(
     name = "bazel-debian",
     architecture = "amd64",
     built_using = "bazel (HEAD)",
     data = ":debian-data",
     depends = select({
-        ":jdk7": [
+        "//tools/jdk:jdk7": [
             "java7-jdk | java7-sdk",
         ],
         "//conditions:default": [