Move version checking from Make into soong_ui

When kati keeps state around, it has to regenerate the ninja file every
time the state is changed. So move the java version checking into
soong_ui, where we can parallelize it with other operations instead of
only checking it occasionally.

Bug: 35970961
Test: Put java7 in PATH, m -j
Test: Put java8-google in PATH, m -j
Test: Put a space in TOP, m -j
Test: OUT_DIR=<case-preserving fs> m -j
Test: OUT_DIR=<path with space> m -j
Test: DIST_DIR=<path with sapce> m -j
Change-Id: I3245c8dd6d856240d17d54cb05d593dc9df71a27
diff --git a/ui/build/build.go b/ui/build/build.go
index 6785082..b84dd7d 100644
--- a/ui/build/build.go
+++ b/ui/build/build.go
@@ -15,6 +15,7 @@
 package build
 
 import (
+	"io/ioutil"
 	"os"
 	"path/filepath"
 	"text/template"
@@ -59,6 +60,37 @@
 	BuildAll           = BuildProductConfig | BuildSoong | BuildKati | BuildNinja
 )
 
+func checkCaseSensitivity(ctx Context, config Config) {
+	outDir := config.OutDir()
+	lowerCase := filepath.Join(outDir, "casecheck.txt")
+	upperCase := filepath.Join(outDir, "CaseCheck.txt")
+	lowerData := "a"
+	upperData := "B"
+
+	err := ioutil.WriteFile(lowerCase, []byte(lowerData), 0777)
+	if err != nil {
+		ctx.Fatalln("Failed to check case sensitivity:", err)
+	}
+
+	err = ioutil.WriteFile(upperCase, []byte(upperData), 0777)
+	if err != nil {
+		ctx.Fatalln("Failed to check case sensitivity:", err)
+	}
+
+	res, err := ioutil.ReadFile(lowerCase)
+	if err != nil {
+		ctx.Fatalln("Failed to check case sensitivity:", err)
+	}
+
+	if string(res) != lowerData {
+		ctx.Println("************************************************************")
+		ctx.Println("You are building on a case-insensitive filesystem.")
+		ctx.Println("Please move your source tree to a case-sensitive filesystem.")
+		ctx.Println("************************************************************")
+		ctx.Fatalln("Case-insensitive filesystems not supported")
+	}
+}
+
 // Build the tree. The 'what' argument can be used to chose which components of
 // the build to run.
 func Build(ctx Context, config Config, what int) {
@@ -86,8 +118,13 @@
 		return
 	}
 
+	// Start getting java version as early as possible
+	getJavaVersions(ctx, config)
+
 	SetupOutDir(ctx, config)
 
+	checkCaseSensitivity(ctx, config)
+
 	if what&BuildProductConfig != 0 {
 		// Run make for product config
 		runMakeProductConfig(ctx, config)
@@ -99,6 +136,9 @@
 		runSoong(ctx, config)
 	}
 
+	// Check the java versions we read earlier
+	checkJavaVersion(ctx, config)
+
 	if what&BuildKati != 0 {
 		// Run ckati
 		runKati(ctx, config)