blob: ea00bae0792ee24453185114867c9a368270ef74 [file] [log] [blame]
Colin Crossfb6d7812019-01-09 22:17:55 -08001// Copyright 2019 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 java
16
17import (
Colin Crossfb6d7812019-01-09 22:17:55 -080018 "path/filepath"
19 "reflect"
20 "strings"
21 "testing"
22
23 "github.com/google/blueprint/proptools"
Colin Cross60405e52019-04-18 12:31:22 -070024
25 "android/soong/android"
26 "android/soong/java/config"
Colin Crossfb6d7812019-01-09 22:17:55 -080027)
28
Colin Crossfb6d7812019-01-09 22:17:55 -080029func TestClasspath(t *testing.T) {
Colin Cross3047fa22019-04-18 10:56:44 -070030 var classpathTestcases = []struct {
Colin Cross617b1452019-10-25 15:22:50 -070031 name string
32 unbundled bool
33 pdk bool
34 moduleType string
35 host android.OsClass
36 properties string
37
38 // for java 8
39 bootclasspath []string
40 java8classpath []string
41
42 // for java 9
43 system string
44 java9classpath []string
45
46 forces8 bool // if set, javac will always be called with java 8 arguments
47
48 aidl string
Colin Cross3047fa22019-04-18 10:56:44 -070049 }{
50 {
Colin Cross617b1452019-10-25 15:22:50 -070051 name: "default",
52 bootclasspath: config.DefaultBootclasspathLibraries,
53 system: config.DefaultSystemModules,
54 java8classpath: config.DefaultLibraries,
55 java9classpath: config.DefaultLibraries,
56 aidl: "-Iframework/aidl",
Colin Cross3047fa22019-04-18 10:56:44 -070057 },
58 {
Colin Cross617b1452019-10-25 15:22:50 -070059 name: `sdk_version:"core_platform"`,
60 properties: `sdk_version:"core_platform"`,
61 bootclasspath: config.DefaultBootclasspathLibraries,
62 system: config.DefaultSystemModules,
63 java8classpath: []string{},
64 aidl: "",
Paul Duffin50c217c2019-06-12 13:25:22 +010065 },
66 {
Colin Cross617b1452019-10-25 15:22:50 -070067 name: "blank sdk version",
68 properties: `sdk_version: "",`,
69 bootclasspath: config.DefaultBootclasspathLibraries,
70 system: config.DefaultSystemModules,
71 java8classpath: config.DefaultLibraries,
72 java9classpath: config.DefaultLibraries,
73 aidl: "-Iframework/aidl",
Colin Cross3047fa22019-04-18 10:56:44 -070074 },
75 {
76
Colin Cross617b1452019-10-25 15:22:50 -070077 name: "sdk v25",
78 properties: `sdk_version: "25",`,
79 bootclasspath: []string{`""`},
80 forces8: true,
81 java8classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
82 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -070083 },
84 {
85
86 name: "current",
87 properties: `sdk_version: "current",`,
88 bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
Colin Cross617b1452019-10-25 15:22:50 -070089 forces8: true,
Colin Cross3047fa22019-04-18 10:56:44 -070090 aidl: "-p" + buildDir + "/framework.aidl",
91 },
92 {
93
94 name: "system_current",
95 properties: `sdk_version: "system_current",`,
96 bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
Colin Cross617b1452019-10-25 15:22:50 -070097 forces8: true,
Colin Cross3047fa22019-04-18 10:56:44 -070098 aidl: "-p" + buildDir + "/framework.aidl",
99 },
100 {
101
Colin Cross617b1452019-10-25 15:22:50 -0700102 name: "system_25",
103 properties: `sdk_version: "system_25",`,
104 bootclasspath: []string{`""`},
105 forces8: true,
106 java8classpath: []string{"prebuilts/sdk/25/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
107 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700108 },
109 {
110
111 name: "test_current",
112 properties: `sdk_version: "test_current",`,
113 bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
Colin Cross617b1452019-10-25 15:22:50 -0700114 forces8: true,
Colin Cross3047fa22019-04-18 10:56:44 -0700115 aidl: "-p" + buildDir + "/framework.aidl",
116 },
117 {
118
119 name: "core_current",
120 properties: `sdk_version: "core_current",`,
121 bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
Colin Cross617b1452019-10-25 15:22:50 -0700122 forces8: true,
Colin Cross3047fa22019-04-18 10:56:44 -0700123 },
124 {
125
Colin Cross617b1452019-10-25 15:22:50 -0700126 name: "nostdlib",
127 properties: `sdk_version: "none", system_modules: "none"`,
128 system: "none",
129 bootclasspath: []string{`""`},
130 java8classpath: []string{},
Paul Duffin52d398a2019-06-11 12:31:14 +0100131 },
132 {
133
Colin Cross617b1452019-10-25 15:22:50 -0700134 name: "nostdlib system_modules",
135 properties: `sdk_version: "none", system_modules: "core-platform-api-stubs-system-modules"`,
136 system: "core-platform-api-stubs-system-modules",
137 bootclasspath: []string{"core-platform-api-stubs-system-modules-lib"},
138 java8classpath: []string{},
Paul Duffin52d398a2019-06-11 12:31:14 +0100139 },
140 {
141
Colin Cross617b1452019-10-25 15:22:50 -0700142 name: "host default",
143 moduleType: "java_library_host",
144 properties: ``,
145 host: android.Host,
146 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
147 java8classpath: []string{},
Colin Cross3047fa22019-04-18 10:56:44 -0700148 },
149 {
Colin Cross3047fa22019-04-18 10:56:44 -0700150
Colin Cross617b1452019-10-25 15:22:50 -0700151 name: "host supported default",
152 host: android.Host,
153 properties: `host_supported: true,`,
154 java8classpath: []string{},
155 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
Colin Cross3047fa22019-04-18 10:56:44 -0700156 },
157 {
Colin Cross617b1452019-10-25 15:22:50 -0700158 name: "host supported nostdlib",
159 host: android.Host,
160 properties: `host_supported: true, sdk_version: "none", system_modules: "none"`,
161 java8classpath: []string{},
Paul Duffin52d398a2019-06-11 12:31:14 +0100162 },
163 {
Colin Cross3047fa22019-04-18 10:56:44 -0700164
Colin Cross617b1452019-10-25 15:22:50 -0700165 name: "unbundled sdk v25",
166 unbundled: true,
167 properties: `sdk_version: "25",`,
168 bootclasspath: []string{`""`},
169 forces8: true,
170 java8classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
171 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700172 },
173 {
174
Colin Cross617b1452019-10-25 15:22:50 -0700175 name: "unbundled current",
176 unbundled: true,
177 properties: `sdk_version: "current",`,
178 bootclasspath: []string{`""`},
179 forces8: true,
180 java8classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
181 aidl: "-pprebuilts/sdk/current/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700182 },
183
184 {
Colin Cross617b1452019-10-25 15:22:50 -0700185 name: "pdk default",
186 pdk: true,
187 bootclasspath: []string{`""`},
188 forces8: true,
189 java8classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
190 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700191 },
192 {
Colin Cross617b1452019-10-25 15:22:50 -0700193 name: "pdk current",
194 pdk: true,
195 properties: `sdk_version: "current",`,
196 bootclasspath: []string{`""`},
197 forces8: true,
198 java8classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
199 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700200 },
201 {
Colin Cross617b1452019-10-25 15:22:50 -0700202 name: "pdk 25",
203 pdk: true,
204 properties: `sdk_version: "25",`,
205 bootclasspath: []string{`""`},
206 forces8: true,
207 java8classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
208 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700209 },
210 }
211
Colin Crossfb6d7812019-01-09 22:17:55 -0800212 for _, testcase := range classpathTestcases {
213 t.Run(testcase.name, func(t *testing.T) {
214 moduleType := "java_library"
215 if testcase.moduleType != "" {
216 moduleType = testcase.moduleType
217 }
218
Pete Gillinbdf5d712019-10-21 14:29:58 +0100219 props := `
Colin Crossfb6d7812019-01-09 22:17:55 -0800220 name: "foo",
221 srcs: ["a.java"],
Colin Cross3047fa22019-04-18 10:56:44 -0700222 target: {
223 android: {
224 srcs: ["bar-doc/IFoo.aidl"],
225 },
226 },
Pete Gillinbdf5d712019-10-21 14:29:58 +0100227 `
228 bp := moduleType + " {" + props + testcase.properties + `
229 }`
230 bpJava8 := moduleType + " {" + props + `java_version: "1.8",
Colin Crossfb6d7812019-01-09 22:17:55 -0800231 ` + testcase.properties + `
232 }`
233
234 variant := "android_common"
235 if testcase.host == android.Host {
236 variant = android.BuildOs.String() + "_common"
237 }
238
239 convertModulesToPaths := func(cp []string) []string {
240 ret := make([]string, len(cp))
241 for i, e := range cp {
242 ret[i] = moduleToPath(e)
243 }
244 return ret
245 }
246
247 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
Colin Cross617b1452019-10-25 15:22:50 -0700248 java8classpath := convertModulesToPaths(testcase.java8classpath)
249 java9classpath := convertModulesToPaths(testcase.java9classpath)
Colin Crossfb6d7812019-01-09 22:17:55 -0800250
Pete Gillinbdf5d712019-10-21 14:29:58 +0100251 bc := ""
252 var bcDeps []string
253 if len(bootclasspath) > 0 {
254 bc = "-bootclasspath " + strings.Join(bootclasspath, ":")
255 if bootclasspath[0] != `""` {
256 bcDeps = bootclasspath
257 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800258 }
259
Colin Cross617b1452019-10-25 15:22:50 -0700260 j8c := ""
261 if len(java8classpath) > 0 {
262 j8c = "-classpath " + strings.Join(java8classpath, ":")
263 }
264
265 j9c := ""
266 if len(java9classpath) > 0 {
267 j9c = "-classpath " + strings.Join(java9classpath, ":")
Colin Crossfb6d7812019-01-09 22:17:55 -0800268 }
Pete Gillinbdf5d712019-10-21 14:29:58 +0100269
Colin Crossfb6d7812019-01-09 22:17:55 -0800270 system := ""
Pete Gillinbdf5d712019-10-21 14:29:58 +0100271 var systemDeps []string
Colin Crossfb6d7812019-01-09 22:17:55 -0800272 if testcase.system == "none" {
273 system = "--system=none"
274 } else if testcase.system != "" {
Dan Willemsenff60a732019-06-13 16:52:01 +0000275 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system")
Pete Gillinbdf5d712019-10-21 14:29:58 +0100276 // The module-relative parts of these paths are hardcoded in system_modules.go:
277 systemDeps = []string{
278 filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system", "lib", "modules"),
279 filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system", "lib", "jrt-fs.jar"),
280 filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system", "release"),
281 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800282 }
283
Pete Gillinbdf5d712019-10-21 14:29:58 +0100284 checkClasspath := func(t *testing.T, ctx *android.TestContext, isJava8 bool) {
Colin Crossc0806172019-06-14 18:51:47 -0700285 foo := ctx.ModuleForTests("foo", variant)
286 javac := foo.Rule("javac")
Pete Gillinbdf5d712019-10-21 14:29:58 +0100287 var deps []string
Colin Crossc0806172019-06-14 18:51:47 -0700288
289 aidl := foo.MaybeRule("aidl")
Pete Gillinbdf5d712019-10-21 14:29:58 +0100290 if aidl.Rule != nil {
291 deps = append(deps, aidl.Output.String())
292 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800293
294 got := javac.Args["bootClasspath"]
Pete Gillinbdf5d712019-10-21 14:29:58 +0100295 expected := ""
Colin Cross617b1452019-10-25 15:22:50 -0700296 if isJava8 || testcase.forces8 {
Pete Gillinbdf5d712019-10-21 14:29:58 +0100297 expected = bc
298 deps = append(deps, bcDeps...)
299 } else {
300 expected = system
301 deps = append(deps, systemDeps...)
302 }
303 if got != expected {
304 t.Errorf("bootclasspath expected %q != got %q", expected, got)
Colin Crossfb6d7812019-01-09 22:17:55 -0800305 }
306
Colin Cross617b1452019-10-25 15:22:50 -0700307 if isJava8 || testcase.forces8 {
308 expected = j8c
309 deps = append(deps, java8classpath...)
310 } else {
311 expected = j9c
312 deps = append(deps, java9classpath...)
Colin Crossfb6d7812019-01-09 22:17:55 -0800313 }
Colin Cross617b1452019-10-25 15:22:50 -0700314 got = javac.Args["classpath"]
315 if got != expected {
316 t.Errorf("classpath expected %q != got %q", expected, got)
317 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800318
319 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
320 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
321 }
Colin Cross3047fa22019-04-18 10:56:44 -0700322 }
323
Pete Gillin1b3370f2019-10-01 13:57:31 +0100324 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillin0c2143e2019-05-02 15:32:11 +0100325 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillinbdf5d712019-10-21 14:29:58 +0100326 config := testConfig(nil)
Colin Cross3047fa22019-04-18 10:56:44 -0700327 if testcase.unbundled {
328 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
329 }
330 if testcase.pdk {
331 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
332 }
Pete Gillinbdf5d712019-10-21 14:29:58 +0100333 ctx := testContext(bpJava8, nil)
Colin Cross3047fa22019-04-18 10:56:44 -0700334 run(t, ctx, config)
335
Pete Gillinbdf5d712019-10-21 14:29:58 +0100336 checkClasspath(t, ctx, true /* isJava8 */)
Colin Cross3047fa22019-04-18 10:56:44 -0700337
338 if testcase.host != android.Host {
339 aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
340
Colin Crossc0806172019-06-14 18:51:47 -0700341 if g, w := aidl.RuleParams.Command, testcase.aidl+" -I."; !strings.Contains(g, w) {
342 t.Errorf("want aidl command to contain %q, got %q", w, g)
Colin Cross3047fa22019-04-18 10:56:44 -0700343 }
344 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800345 })
346
Pete Gillin1b3370f2019-10-01 13:57:31 +0100347 // Test with default javac -source 9 -target 9
Pete Gillin0c2143e2019-05-02 15:32:11 +0100348 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +0100349 config := testConfig(nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800350 if testcase.unbundled {
351 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
352 }
Colin Cross98fd5742019-01-09 23:04:25 -0800353 if testcase.pdk {
354 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
355 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700356 ctx := testContext(bp, nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800357 run(t, ctx, config)
358
Pete Gillinbdf5d712019-10-21 14:29:58 +0100359 checkClasspath(t, ctx, false /* isJava8 */)
360
361 if testcase.host != android.Host {
362 aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
363
364 if g, w := aidl.RuleParams.Command, testcase.aidl+" -I."; !strings.Contains(g, w) {
365 t.Errorf("want aidl command to contain %q, got %q", w, g)
366 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800367 }
368 })
Colin Crossff0daf42019-04-02 16:10:56 -0700369
Pete Gillinbdf5d712019-10-21 14:29:58 +0100370 // Test again with PLATFORM_VERSION_CODENAME=REL, javac -source 8 -target 8
371 t.Run("REL + Java language level 8", func(t *testing.T) {
372 config := testConfig(nil)
Colin Crossff0daf42019-04-02 16:10:56 -0700373 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
374 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
375
376 if testcase.unbundled {
377 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
378 }
379 if testcase.pdk {
380 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
381 }
Pete Gillinbdf5d712019-10-21 14:29:58 +0100382 ctx := testContext(bpJava8, nil)
Colin Crossff0daf42019-04-02 16:10:56 -0700383 run(t, ctx, config)
384
Pete Gillinbdf5d712019-10-21 14:29:58 +0100385 checkClasspath(t, ctx, true /* isJava8 */)
Colin Crossff0daf42019-04-02 16:10:56 -0700386 })
Pete Gillinbdf5d712019-10-21 14:29:58 +0100387
388 // TODO(b/142896162): Add a with PLATFORM_VERSION_CODENAME=REL, javac -source 9 -target 9, when that all works.
Colin Crossfb6d7812019-01-09 22:17:55 -0800389 })
390 }
391
392}