Optionally support building and running with locally installed JDK

Change-Id: Ie08a4bf7952c0ed62dbc31dba0faa33eae9b133d
diff --git a/.bazelrc b/.bazelrc
index 548a092..64c7384 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -1,5 +1,18 @@
+# By default, we build and run ONOS using the Bazel-provided remote JDK. The
+# specific JDK version is defined in tools/build/jdk/BUILD.
+#
+# If you prefer to use your locally installed JDK, replace the --javabase and
+# --host_javabase arguments with the following lines. ABSOLUTE_JAVABASE should
+# point to the "JAVA_HOME" of your JDK.
+#
+# build --javabase=@bazel_tools//tools/jdk:absolute_javabase
+# build --host_javabase=@bazel_tools//tools/jdk:absolute_javabase
+# build --define=ABSOLUTE_JAVABASE=<path/to/jdk>
+# build --define=RUN_WITH_ABSOLUTE_JAVABASE=true
+
 build --javabase=@org_onosproject_onos//tools/build/jdk:default_jdk
 build --host_javabase=@org_onosproject_onos//tools/build/jdk:default_jdk
+
 build --java_toolchain=@org_onosproject_onos//tools/build/jdk:default_toolchain
 build --host_java_toolchain=@org_onosproject_onos//tools/build/jdk:default_toolchain
 
@@ -16,5 +29,5 @@
 test --test_output=errors
 test --test_verbose_timeout_warnings
 
-#build --disk_cache=~/bazel-cache
-#build --remote_http_cache=http://10.1.10.224:80/cache
+# build --disk_cache=~/bazel-cache
+# build --remote_http_cache=http://10.1.10.224:80/cache
diff --git a/BUILD b/BUILD
index 1226cc2..54c52d9 100644
--- a/BUILD
+++ b/BUILD
@@ -118,20 +118,60 @@
 )
 
 # Runs ONOS as a single instance from the /tmp directory
-genrule(
+alias(
     name = "onos-local",
+    actual = select({
+        ":run_with_absolute_javabase": ":onos-local_absolute-javabase",
+        "//conditions:default": ":onos-local_current-jdk",
+    }),
+)
+
+config_setting(
+    name = "run_with_absolute_javabase",
+    define_values = {
+        "RUN_WITH_ABSOLUTE_JAVABASE": "true",
+    },
+)
+
+# Run onos-local with JAVA_HOME set to ABSOLUTE_JAVABASE (see .bazelrc)
+genrule(
+    name = "onos-local_absolute-javabase",
+    srcs = [":onos-local-base"],
+    outs = ["onos-runner_absolute-javabase"],
+    cmd = "sed \"s#ABSOLUTE_JAVABASE=#ABSOLUTE_JAVABASE=$(ABSOLUTE_JAVABASE)#\" " +
+          "$(location onos-local-base) > $(location onos-runner_absolute-javabase)",
+    executable = True,
+    output_to_bindir = True,
+    visibility = ["//visibility:private"],
+)
+
+# Run onos-local with the same JDK used for the build, packaged in a tarball.
+genrule(
+    name = "onos-local_current-jdk",
+    srcs = [
+        ":onos-local-base",
+        "//tools/build/jdk:current_jdk_tar",
+    ],
+    outs = ["onos-runner_current-jdk"],
+    cmd = "sed \"s#JDK_TAR=#JDK_TAR=$(location //tools/build/jdk:current_jdk_tar)#\" " +
+          "$(location :onos-local-base) > $(location onos-runner_current-jdk); ",
+    executable = True,
+    output_to_bindir = True,
+    visibility = ["//visibility:private"],
+)
+
+# Create an onos-runner script based on onos-run-karaf
+genrule(
+    name = "onos-local-base",
     srcs = [
         ":onos-package",
-        "//tools/build/jdk:default_jdk_tar",
         "tools/package/onos-run-karaf",
     ] + glob(["tools/package/config/**"]),
     outs = ["onos-runner"],
-    cmd = "sed \"s#ONOS_TAR=#ONOS_TAR=$(location :onos-package)#\" $(location tools/package/onos-run-karaf) > foo; " +
-          "sed \"s#JDK_TAR=#JDK_TAR=$(location //tools/build/jdk:default_jdk_tar)#\" foo > $(location onos-runner); " +
+    cmd = "sed \"s#ONOS_TAR=#ONOS_TAR=$(location :onos-package)#\" " +
+          "$(location tools/package/onos-run-karaf) > $(location onos-runner); " +
           "chmod +x $(location onos-runner)",
-    executable = True,
-    output_to_bindir = True,
-    visibility = ["//visibility:public"],
+    visibility = ["//visibility:private"],
 )
 
 load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
diff --git a/tools/build/bazel/jdk_genrule.bzl b/tools/build/bazel/jdk_genrule.bzl
index 14e5aa2..5d9bae5 100644
--- a/tools/build/bazel/jdk_genrule.bzl
+++ b/tools/build/bazel/jdk_genrule.bzl
@@ -29,7 +29,14 @@
         **kwargs):
     new_tools = tools + ["@bazel_tools//tools/jdk:current_java_runtime"]
     new_toolchains = toolchains + ["@bazel_tools//tools/jdk:current_java_runtime"]
-    new_cmd = "echo \"export PATH=$$PWD/$(JAVABASE)/bin:$$PATH:\" > jdk_genrule_setup.sh; " + \
+
+    # Add JAVABASE/bin to the PATH env.
+    # Prepend PWD (sandbox path) if JAVABASE is not an absolute path.
+    new_cmd = "echo 'if [[ \"$(JAVABASE)\" = /* ]]; then " + \
+              "JHOME=$(JAVABASE); " + \
+              "else JHOME=$$PWD/$(JAVABASE); " + \
+              "fi' > jdk_genrule_setup.sh; " + \
+              "echo 'export PATH=$$JHOME/bin:$$PATH:' >> jdk_genrule_setup.sh; " + \
               "source jdk_genrule_setup.sh; " + cmd
 
     native.genrule(
diff --git a/tools/build/jdk/BUILD b/tools/build/jdk/BUILD
index 84856ee..59934fc 100644
--- a/tools/build/jdk/BUILD
+++ b/tools/build/jdk/BUILD
@@ -28,16 +28,16 @@
 #  produced JDK tar.
 
 genrule(
-    name = "default_jdk_tar",
+    name = "current_jdk_tar",
     srcs = [
         "@bazel_tools//tools/jdk:current_java_runtime",
         ":conf-security",
     ],
-    outs = ["default_jdk.tar.gz"],
-    cmd = "mkdir default_jdk && " +
-          "mv $(JAVABASE)/* default_jdk/ && " +
-          "mv tools/build/jdk/* default_jdk/ && " +
-          "tar -hczf $(location default_jdk.tar.gz) default_jdk",
+    outs = ["current_jdk.tar.gz"],
+    cmd = "mkdir current_jdk && " +
+          "cp -r $(JAVABASE)/* current_jdk/ && " +
+          "cp -r tools/build/jdk/* current_jdk/ && " +
+          "tar -hczf $(location current_jdk.tar.gz) current_jdk",
     output_to_bindir = True,
     toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
     visibility = ["//visibility:public"],
diff --git a/tools/package/onos-run-karaf b/tools/package/onos-run-karaf
index e898006..6fce6e8 100755
--- a/tools/package/onos-run-karaf
+++ b/tools/package/onos-run-karaf
@@ -5,6 +5,7 @@
 
 ONOS_TAR=
 JDK_TAR=
+ABSOLUTE_JAVABASE=
 
 [ -f $ONOS_TAR ] || (echo "$ONOS_TAR not found" && exit 1)
 
@@ -28,8 +29,11 @@
   *)        md5cmd='md5sum';;
 esac
 
-# Use provided JDK as JAVA_HOME if given
-if [[ -f ${JDK_TAR} ]]; then
+# Either use the given ABSOLUTE_JAVABASE as JAVA_HOME, or extract the passed
+# JDK_TAR and use that.
+if [[ -d ${ABSOLUTE_JAVABASE} ]]; then
+    export JAVA_HOME=${ABSOLUTE_JAVABASE}
+elif [[ -f ${JDK_TAR} ]]; then
     JDK_DIR=$ONOS_DIR-jdk
     JDK_MD5=$JDK_DIR/CHECKSUM