diff options
author | Colin Cross <ccross@android.com> | 2017-07-19 23:08:34 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-07-19 23:08:34 +0000 |
commit | 975616896fad9ef57940f6aad516734b5cb8e089 (patch) | |
tree | c7461293b2ee9d5fafbccb26e2ade2e3a13aef89 | |
parent | 24baafa9e79716fc8f7588b59c53c4675c287265 (diff) | |
parent | 4fe760591d28d68999c9fbf438f2888c6fd2fa9f (diff) | |
download | soong-975616896fad9ef57940f6aad516734b5cb8e089.tar.gz |
Merge "Revert "Remove java version checks"" am: ec95f7f2f6 am: f1aba41add am: 698b3132ae
am: 4fe760591d
Change-Id: I5bae25cc7193b506549a7ce813bd00b40b0d26f4
-rw-r--r-- | ui/build/Android.bp | 1 | ||||
-rw-r--r-- | ui/build/build.go | 6 | ||||
-rw-r--r-- | ui/build/java.go | 163 |
3 files changed, 170 insertions, 0 deletions
diff --git a/ui/build/Android.bp b/ui/build/Android.bp index 548baeed4..23a98721f 100644 --- a/ui/build/Android.bp +++ b/ui/build/Android.bp @@ -27,6 +27,7 @@ bootstrap_go_package { "context.go", "environment.go", "exec.go", + "java.go", "kati.go", "make.go", "ninja.go", diff --git a/ui/build/build.go b/ui/build/build.go index 32f4ba579..1400c48e6 100644 --- a/ui/build/build.go +++ b/ui/build/build.go @@ -114,6 +114,9 @@ func Build(ctx Context, config Config, what int) { return } + // Start getting java version as early as possible + getJavaVersions(ctx, config) + // Make sure that no other Soong process is running with the same output directory buildLock := BecomeSingletonOrFail(ctx, config) defer buildLock.Unlock() @@ -145,6 +148,9 @@ func Build(ctx Context, config Config, what int) { runSoong(ctx, config) } + // Check the java versions we read earlier + checkJavaVersion(ctx, config) + if what&BuildKati != 0 { // Run ckati runKati(ctx, config) diff --git a/ui/build/java.go b/ui/build/java.go new file mode 100644 index 000000000..473af0122 --- /dev/null +++ b/ui/build/java.go @@ -0,0 +1,163 @@ +// Copyright 2017 Google Inc. 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. + +package build + +import ( + "regexp" + "runtime" + "strings" + "sync" +) + +const incompatibleJavacStr = "google" + +var javaVersionInfo = struct { + once sync.Once + startOnce sync.Once + + java_version_output string + javac_version_output string +}{} + +func getJavaVersions(ctx Context, config Config) { + javaVersionInfo.startOnce.Do(func() { + go func() { + if ctx.Tracer != nil { + thread := ctx.Tracer.NewThread("java_version") + ctx.Tracer.Begin("get version", thread) + defer ctx.Tracer.End(thread) + } + + getJavaVersionsImpl(ctx, config) + }() + }) +} + +func getJavaVersionsImpl(ctx Context, config Config) { + javaVersionInfo.once.Do(func() { + cmd := Command(ctx, config, "java", "java", "-version") + cmd.Environment.Unset("_JAVA_OPTIONS") + javaVersionInfo.java_version_output = string(cmd.CombinedOutputOrFatal()) + + cmd = Command(ctx, config, "javac", "javac", "-version") + cmd.Environment.Unset("_JAVA_OPTIONS") + javaVersionInfo.javac_version_output = string(cmd.CombinedOutputOrFatal()) + }) +} + +func checkJavaVersion(ctx Context, config Config) { + ctx.BeginTrace("java_version_check") + defer ctx.EndTrace() + + getJavaVersionsImpl(ctx, config) + + var required_java_version string + var java_version_regexp *regexp.Regexp + var javac_version_regexp *regexp.Regexp + + oj9_env, _ := config.Environment().Get("EXPERIMENTAL_USE_OPENJDK9") + experimental_use_openjdk9 := oj9_env != "" + + if experimental_use_openjdk9 { + required_java_version = "9" + java_version_regexp = regexp.MustCompile(`^java .* "9.*"`) + javac_version_regexp = regexp.MustCompile(`^javac 9`) + } else { + required_java_version = "1.8" + java_version_regexp = regexp.MustCompile(`[ "]1\.8[\. "$]`) + javac_version_regexp = java_version_regexp + } + + java_version := javaVersionInfo.java_version_output + javac_version := javaVersionInfo.javac_version_output + + found := false + for _, l := range strings.Split(java_version, "\n") { + if java_version_regexp.MatchString(l) { + java_version = l + found = true + break + } + } + if !found { + ctx.Println("***************************************************************") + ctx.Println("You are attempting to build with the incorrect version of java.") + ctx.Println() + ctx.Println("Your version is:", java_version) + ctx.Println("The required version is:", required_java_version+".x") + ctx.Println() + ctx.Println("Please follow the machine setup instructions at:") + ctx.Println(" https://source.android.com/source/initializing.html") + ctx.Println("***************************************************************") + ctx.Fatalln("stop") + } + + if runtime.GOOS == "linux" { + // Early access builds of OpenJDK 9 do not contain the string "openjdk" in the + // version name. TODO(tobiast): Reconsider once the OpenJDK 9 toolchain is stable. + // http://b/62123342 + if !strings.Contains(java_version, "openjdk") && !experimental_use_openjdk9 { + ctx.Println("*******************************************************") + ctx.Println("You are attempting to build with an unsupported JDK.") + ctx.Println() + ctx.Println("Only an OpenJDK based JDK is supported.") + ctx.Println() + ctx.Println("Please follow the machine setup instructions at:") + ctx.Println(" https://source.android.com/source/initializing.html") + ctx.Println("*******************************************************") + ctx.Fatalln("stop") + } + } else { // darwin + if strings.Contains(java_version, "openjdk") { + ctx.Println("*******************************************************") + ctx.Println("You are attempting to build with an unsupported JDK.") + ctx.Println() + ctx.Println("You use OpenJDK, but only Sun/Oracle JDK is supported.") + ctx.Println() + ctx.Println("Please follow the machine setup instructions at:") + ctx.Println(" https://source.android.com/source/initializing.html") + ctx.Println("*******************************************************") + ctx.Fatalln("stop") + } + } + + incompatible_javac := strings.Contains(javac_version, incompatibleJavacStr) + + found = false + for _, l := range strings.Split(javac_version, "\n") { + if javac_version_regexp.MatchString(l) { + javac_version = l + found = true + break + } + } + if !found || incompatible_javac { + ctx.Println("****************************************************************") + ctx.Println("You are attempting to build with the incorrect version of javac.") + ctx.Println() + ctx.Println("Your version is:", javac_version) + if incompatible_javac { + ctx.Println("The '" + incompatibleJavacStr + "' version is not supported for Android platform builds.") + ctx.Println("Use a publically available JDK and make sure you have run envsetup.sh / lunch.") + } else { + ctx.Println("The required version is:", required_java_version) + } + ctx.Println() + ctx.Println("Please follow the machine setup instructions at:") + ctx.Println(" https://source.android.com/source/initializing.html") + ctx.Println("****************************************************************") + ctx.Fatalln("stop") + } +} |