Revert "Use java prebuilts"

This reverts commit 193175094081e9cc3c44e2b3ee290195b1b89613.

Change-Id: I7a99fd6c53d35a2a674f2d60b113a727f3c453ce
diff --git a/core/combo/javac.mk b/core/combo/javac.mk
index 6d05e06..5c2668c 100644
--- a/core/combo/javac.mk
+++ b/core/combo/javac.mk
@@ -16,20 +16,16 @@
 
 common_jdk_flags := -Xmaxerrs 9999999
 
-ANDROID_JAVA_HOME := prebuilts/jdk/jdk8/$(HOST_PREBUILT_TAG)
-ANDROID_JAVA_TOOLCHAIN := $(ANDROID_JAVA_HOME)/bin
-export JAVA_HOME := $(ANDROID_JAVA_HOME)
-
 # Use the indexer wrapper to index the codebase instead of the javac compiler
 ifeq ($(ALTERNATE_JAVAC),)
-JAVACC := $(ANDROID_JAVA_TOOLCHAIN)/javac
+JAVACC := javac
 else
 JAVACC := $(ALTERNATE_JAVAC)
 endif
 
-JAVA := $(ANDROID_JAVA_TOOLCHAIN)/java
-JAVADOC := $(ANDROID_JAVA_TOOLCHAIN)/javadoc
-JAR := $(ANDROID_JAVA_TOOLCHAIN)/jar
+JAVA := java
+JAVADOC := javadoc
+JAR := jar
 
 # The actual compiler can be wrapped by setting the JAVAC_WRAPPER var.
 ifdef JAVAC_WRAPPER
@@ -38,8 +34,16 @@
     endif
 endif
 
+# Whatever compiler is on this system.
 COMMON_JAVAC := $(JAVACC) -J-Xmx2048M $(common_jdk_flags)
 
+# Eclipse.
+ifeq ($(CUSTOM_JAVA_COMPILER), eclipse)
+    COMMON_JAVAC := java -Xmx256m -jar prebuilt/common/ecj/ecj.jar -5 \
+        -maxProblems 9999999 -nowarn
+    $(info CUSTOM_JAVA_COMPILER=eclipse)
+endif
+
 GLOBAL_JAVAC_DEBUG_FLAGS := -g
 
 HOST_JAVAC ?= $(COMMON_JAVAC)
diff --git a/core/config.mk b/core/config.mk
index 00a3c3b..f839d3d 100644
--- a/core/config.mk
+++ b/core/config.mk
@@ -675,13 +675,22 @@
 
 COLUMN:= column
 
+# We may not have the right JAVA_HOME/PATH set up yet when this is run from envsetup.sh.
+ifneq ($(CALLED_FROM_SETUP),true)
+
 # Path to tools.jar, or empty if EXPERIMENTAL_USE_OPENJDK9 is set
 HOST_JDK_TOOLS_JAR :=
 # TODO: Remove HOST_JDK_TOOLS_JAR and all references to it once OpenJDK 8
 # toolchains are no longer supported (i.e. when what is now
 # EXPERIMENTAL_USE_OPENJDK9 becomes the standard). http://b/38418220
 ifeq ($(EXPERIMENTAL_USE_OPENJDK9),)
-HOST_JDK_TOOLS_JAR := $(ANDROID_JAVA_TOOLCHAIN)/../lib/tools.jar
+HOST_JDK_TOOLS_JAR := $(shell $(BUILD_SYSTEM)/find-jdk-tools-jar.sh)
+
+ifneq ($(HOST_JDK_TOOLS_JAR),)
+ifeq ($(wildcard $(HOST_JDK_TOOLS_JAR)),)
+$(error Error: could not find jdk tools.jar at $(HOST_JDK_TOOLS_JAR), please check if your JDK was installed correctly)
+endif
+endif
 endif # ifeq ($(EXPERIMENTAL_USE_OPENJDK9),)
 
 # Is the host JDK 64-bit version?
@@ -689,6 +698,7 @@
 ifneq ($(filter 64-Bit, $(shell $(JAVA) -version 2>&1)),)
 HOST_JDK_IS_64BIT_VERSION := true
 endif
+endif  # CALLED_FROM_SETUP not true
 
 # It's called md5 on Mac OS and md5sum on Linux
 ifeq ($(HOST_OS),darwin)
@@ -838,17 +848,6 @@
 unexport JAVA_HOME
 export ANDROID_BUILD_PATHS:=$(abspath $(BUILD_SYSTEM)/no_java_path):$(ANDROID_BUILD_PATHS)
 export PATH:=$(abspath $(BUILD_SYSTEM)/no_java_path):$(PATH)
-else
-  # Put java first on the path
-  # TODO(ccross): remove this once tools run during the build no longer depend on
-  # finding java in the path
-  ifeq (,$(strip $(CALLED_FROM_SETUP)))
-    ifneq ($(shell which java),$(abspath $(ANDROID_JAVA_TOOLCHAIN)/java))
-      $(warning Found incorrect java $(shell which java) in $$PATH)
-      $(warning Adding $(abspath $(ANDROID_JAVA_TOOLCHAIN)) to $$PATH)
-      export PATH:=$(abspath $(ANDROID_JAVA_TOOLCHAIN)):$(PATH)
-    endif
-  endif
 endif
 
 # Projects clean of compiler warnings should be compiled with -Werror.
diff --git a/core/find-jdk-tools-jar.sh b/core/find-jdk-tools-jar.sh
new file mode 100755
index 0000000..ac0b3b6
--- /dev/null
+++ b/core/find-jdk-tools-jar.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+if [ "x$ANDROID_JAVA_HOME" != x ] && [ -e "$ANDROID_JAVA_HOME/lib/tools.jar" ] ; then
+    echo $ANDROID_JAVA_HOME/lib/tools.jar
+else
+    JAVAC=$(realpath $(which javac) 2>/dev/null)
+    if [ -z "$JAVAC" ]; then
+        JAVAC=$(readlink -f $(which javac) 2>/dev/null)
+    fi
+    if [ -z "$JAVAC" ]; then
+        JAVAC=$(which javac)
+    fi
+    if [ -z "$JAVAC" ] ; then
+        exit 1
+    fi
+    while [ -L "$JAVAC" ] ; do
+        LSLINE=$(ls -l "$JAVAC")
+        JAVAC=$(echo -n "$LSLINE" | sed -e "s/.* -> //")
+    done
+    echo $JAVAC | sed -e 's:\(.*\)/javac$:\1/../lib/tools.jar:'
+fi
diff --git a/envsetup.sh b/envsetup.sh
index dca817a..d2bcd36 100644
--- a/envsetup.sh
+++ b/envsetup.sh
@@ -248,11 +248,13 @@
     export PATH=$ANDROID_BUILD_PATHS$PATH
     export PYTHONPATH=$T/development/python-packages:$PYTHONPATH
 
-    export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
-    export JAVA_HOME=$ANDROID_JAVA_HOME
-    export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
-    export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
-    export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
+    unset ANDROID_JAVA_TOOLCHAIN
+    unset ANDROID_PRE_BUILD_PATHS
+    if [ -n "$JAVA_HOME" ]; then
+        export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
+        export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
+        export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
+    fi
 
     unset ANDROID_PRODUCT_OUT
     export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
@@ -285,6 +287,7 @@
 function set_stuff_for_environment()
 {
     settitle
+    set_java_home
     setpaths
     set_sequence_number
 
@@ -1525,6 +1528,46 @@
     \cd $T/$pathname
 }
 
+# Force JAVA_HOME to point to java 1.7/1.8 if it isn't already set.
+function set_java_home() {
+    # Clear the existing JAVA_HOME value if we set it ourselves, so that
+    # we can reset it later, depending on the version of java the build
+    # system needs.
+    #
+    # If we don't do this, the JAVA_HOME value set by the first call to
+    # build/envsetup.sh will persist forever.
+    if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
+      export JAVA_HOME=""
+    fi
+
+    if [ ! "$JAVA_HOME" ]; then
+      if [ -n "$LEGACY_USE_JAVA7" ]; then
+        echo Warning: Support for JDK 7 will be dropped. Switch to JDK 8.
+        case `uname -s` in
+            Darwin)
+                export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
+                ;;
+            *)
+                export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
+                ;;
+        esac
+      else
+        case `uname -s` in
+            Darwin)
+                export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
+                ;;
+            *)
+                export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
+                ;;
+        esac
+      fi
+
+      # Keep track of the fact that we set JAVA_HOME ourselves, so that
+      # we can change it on the next envsetup.sh, if required.
+      export ANDROID_SET_JAVA_HOME=true
+    fi
+}
+
 # Print colored exit condition
 function pez {
     "$@"