blob: 481a68054fc4db280892cc073924c0dca0633646 [file] [log] [blame]
Dan Willemsendb8457c2017-05-12 16:38:17 -07001// 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
15package build
16
17import (
18 "regexp"
19 "runtime"
20 "strings"
21 "sync"
22)
23
24const incompatibleJavacStr = "google"
25
26var javaVersionInfo = struct {
27 once sync.Once
28 startOnce sync.Once
29
30 java_version_output string
31 javac_version_output string
32}{}
33
34func 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
48func 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
60func 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
Tobias Thierer8b4319c2017-05-23 14:20:03 +010069 required_java_version = "1.8"
70 java_version_regexp = regexp.MustCompile(`[ "]1\.8[\. "$]`)
71 javac_version_regexp = java_version_regexp
Dan Willemsendb8457c2017-05-12 16:38:17 -070072
73 java_version := javaVersionInfo.java_version_output
74 javac_version := javaVersionInfo.javac_version_output
75
76 found := false
77 for _, l := range strings.Split(java_version, "\n") {
78 if java_version_regexp.MatchString(l) {
79 java_version = l
80 found = true
81 break
82 }
83 }
84 if !found {
85 ctx.Println("***************************************************************")
86 ctx.Println("You are attempting to build with the incorrect version of java.")
87 ctx.Println()
88 ctx.Println("Your version is:", java_version)
89 ctx.Println("The required version is:", required_java_version+".x")
90 ctx.Println()
91 ctx.Println("Please follow the machine setup instructions at:")
92 ctx.Println(" https://source.android.com/source/initializing.html")
93 ctx.Println("***************************************************************")
94 ctx.Fatalln("stop")
95 }
96
97 if runtime.GOOS == "linux" {
98 if !strings.Contains(java_version, "openjdk") {
99 ctx.Println("*******************************************************")
100 ctx.Println("You are attempting to build with an unsupported JDK.")
101 ctx.Println()
102 ctx.Println("Only an OpenJDK based JDK is supported.")
103 ctx.Println()
104 ctx.Println("Please follow the machine setup instructions at:")
105 ctx.Println(" https://source.android.com/source/initializing.html")
106 ctx.Println("*******************************************************")
107 ctx.Fatalln("stop")
108 }
109 } else { // darwin
110 if strings.Contains(java_version, "openjdk") {
111 ctx.Println("*******************************************************")
112 ctx.Println("You are attempting to build with an unsupported JDK.")
113 ctx.Println()
114 ctx.Println("You use OpenJDK, but only Sun/Oracle JDK is supported.")
115 ctx.Println()
116 ctx.Println("Please follow the machine setup instructions at:")
117 ctx.Println(" https://source.android.com/source/initializing.html")
118 ctx.Println("*******************************************************")
119 ctx.Fatalln("stop")
120 }
121 }
122
123 incompatible_javac := strings.Contains(javac_version, incompatibleJavacStr)
124
125 found = false
126 for _, l := range strings.Split(javac_version, "\n") {
127 if javac_version_regexp.MatchString(l) {
128 javac_version = l
129 found = true
130 break
131 }
132 }
133 if !found || incompatible_javac {
134 ctx.Println("****************************************************************")
135 ctx.Println("You are attempting to build with the incorrect version of javac.")
136 ctx.Println()
137 ctx.Println("Your version is:", javac_version)
138 if incompatible_javac {
139 ctx.Println("The '" + incompatibleJavacStr + "' version is not supported for Android platform builds.")
140 ctx.Println("Use a publically available JDK and make sure you have run envsetup.sh / lunch.")
141 } else {
142 ctx.Println("The required version is:", required_java_version)
143 }
144 ctx.Println()
145 ctx.Println("Please follow the machine setup instructions at:")
146 ctx.Println(" https://source.android.com/source/initializing.html")
147 ctx.Println("****************************************************************")
148 ctx.Fatalln("stop")
149 }
150}