Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame^] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
| 18 | "regexp" |
| 19 | "runtime" |
| 20 | "strings" |
| 21 | "sync" |
| 22 | ) |
| 23 | |
| 24 | const incompatibleJavacStr = "google" |
| 25 | |
| 26 | var javaVersionInfo = struct { |
| 27 | once sync.Once |
| 28 | startOnce sync.Once |
| 29 | |
| 30 | java_version_output string |
| 31 | javac_version_output string |
| 32 | }{} |
| 33 | |
| 34 | func getJavaVersions(ctx Context, config Config) { |
| 35 | javaVersionInfo.startOnce.Do(func() { |
| 36 | go func() { |
| 37 | if ctx.Tracer != nil { |
| 38 | thread := ctx.Tracer.NewThread("java_version") |
| 39 | ctx.Tracer.Begin("get version", thread) |
| 40 | defer ctx.Tracer.End(thread) |
| 41 | } |
| 42 | |
| 43 | getJavaVersionsImpl(ctx, config) |
| 44 | }() |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | func getJavaVersionsImpl(ctx Context, config Config) { |
| 49 | javaVersionInfo.once.Do(func() { |
| 50 | cmd := Command(ctx, config, "java", "java", "-version") |
| 51 | cmd.Environment.Unset("_JAVA_OPTIONS") |
| 52 | javaVersionInfo.java_version_output = string(cmd.CombinedOutputOrFatal()) |
| 53 | |
| 54 | cmd = Command(ctx, config, "javac", "javac", "-version") |
| 55 | cmd.Environment.Unset("_JAVA_OPTIONS") |
| 56 | javaVersionInfo.javac_version_output = string(cmd.CombinedOutputOrFatal()) |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | func checkJavaVersion(ctx Context, config Config) { |
| 61 | ctx.BeginTrace("java_version_check") |
| 62 | defer ctx.EndTrace() |
| 63 | |
| 64 | getJavaVersionsImpl(ctx, config) |
| 65 | |
| 66 | var required_java_version string |
| 67 | var java_version_regexp *regexp.Regexp |
| 68 | var javac_version_regexp *regexp.Regexp |
| 69 | if legacy, _ := config.Environment().Get("LEGACY_USE_JAVA7"); legacy != "" { |
| 70 | required_java_version = "1.7" |
| 71 | java_version_regexp = regexp.MustCompile(`^java .*[ "]1\.7[\. "$]`) |
| 72 | javac_version_regexp = regexp.MustCompile(`[ "]1\.7[\. "$]`) |
| 73 | } else { |
| 74 | required_java_version = "1.8" |
| 75 | java_version_regexp = regexp.MustCompile(`[ "]1\.8[\. "$]`) |
| 76 | javac_version_regexp = java_version_regexp |
| 77 | } |
| 78 | |
| 79 | java_version := javaVersionInfo.java_version_output |
| 80 | javac_version := javaVersionInfo.javac_version_output |
| 81 | |
| 82 | found := false |
| 83 | for _, l := range strings.Split(java_version, "\n") { |
| 84 | if java_version_regexp.MatchString(l) { |
| 85 | java_version = l |
| 86 | found = true |
| 87 | break |
| 88 | } |
| 89 | } |
| 90 | if !found { |
| 91 | ctx.Println("***************************************************************") |
| 92 | ctx.Println("You are attempting to build with the incorrect version of java.") |
| 93 | ctx.Println() |
| 94 | ctx.Println("Your version is:", java_version) |
| 95 | ctx.Println("The required version is:", required_java_version+".x") |
| 96 | ctx.Println() |
| 97 | ctx.Println("Please follow the machine setup instructions at:") |
| 98 | ctx.Println(" https://source.android.com/source/initializing.html") |
| 99 | ctx.Println("***************************************************************") |
| 100 | ctx.Fatalln("stop") |
| 101 | } |
| 102 | |
| 103 | if runtime.GOOS == "linux" { |
| 104 | if !strings.Contains(java_version, "openjdk") { |
| 105 | ctx.Println("*******************************************************") |
| 106 | ctx.Println("You are attempting to build with an unsupported JDK.") |
| 107 | ctx.Println() |
| 108 | ctx.Println("Only an OpenJDK based JDK is supported.") |
| 109 | ctx.Println() |
| 110 | ctx.Println("Please follow the machine setup instructions at:") |
| 111 | ctx.Println(" https://source.android.com/source/initializing.html") |
| 112 | ctx.Println("*******************************************************") |
| 113 | ctx.Fatalln("stop") |
| 114 | } |
| 115 | } else { // darwin |
| 116 | if strings.Contains(java_version, "openjdk") { |
| 117 | ctx.Println("*******************************************************") |
| 118 | ctx.Println("You are attempting to build with an unsupported JDK.") |
| 119 | ctx.Println() |
| 120 | ctx.Println("You use OpenJDK, but only Sun/Oracle JDK is supported.") |
| 121 | ctx.Println() |
| 122 | ctx.Println("Please follow the machine setup instructions at:") |
| 123 | ctx.Println(" https://source.android.com/source/initializing.html") |
| 124 | ctx.Println("*******************************************************") |
| 125 | ctx.Fatalln("stop") |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | incompatible_javac := strings.Contains(javac_version, incompatibleJavacStr) |
| 130 | |
| 131 | found = false |
| 132 | for _, l := range strings.Split(javac_version, "\n") { |
| 133 | if javac_version_regexp.MatchString(l) { |
| 134 | javac_version = l |
| 135 | found = true |
| 136 | break |
| 137 | } |
| 138 | } |
| 139 | if !found || incompatible_javac { |
| 140 | ctx.Println("****************************************************************") |
| 141 | ctx.Println("You are attempting to build with the incorrect version of javac.") |
| 142 | ctx.Println() |
| 143 | ctx.Println("Your version is:", javac_version) |
| 144 | if incompatible_javac { |
| 145 | ctx.Println("The '" + incompatibleJavacStr + "' version is not supported for Android platform builds.") |
| 146 | ctx.Println("Use a publically available JDK and make sure you have run envsetup.sh / lunch.") |
| 147 | } else { |
| 148 | ctx.Println("The required version is:", required_java_version) |
| 149 | } |
| 150 | ctx.Println() |
| 151 | ctx.Println("Please follow the machine setup instructions at:") |
| 152 | ctx.Println(" https://source.android.com/source/initializing.html") |
| 153 | ctx.Println("****************************************************************") |
| 154 | ctx.Fatalln("stop") |
| 155 | } |
| 156 | } |