blob: c815fe3c1df73fc4a2aabc15b7915a6ac635f186 [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 Cross74362a42019-10-28 08:05:02 -070077 name: "sdk v29",
78 properties: `sdk_version: "29",`,
Colin Cross617b1452019-10-25 15:22:50 -070079 bootclasspath: []string{`""`},
80 forces8: true,
Colin Cross74362a42019-10-28 08:05:02 -070081 java8classpath: []string{"prebuilts/sdk/29/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
82 aidl: "-pprebuilts/sdk/29/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -070083 },
84 {
85
Colin Cross6cef4812019-10-17 14:23:50 -070086 name: "current",
87 properties: `sdk_version: "current",`,
88 bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
89 system: "core-current-stubs-system-modules",
90 java9classpath: []string{"android_stubs_current"},
91 aidl: "-p" + buildDir + "/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -070092 },
93 {
94
Colin Cross6cef4812019-10-17 14:23:50 -070095 name: "system_current",
96 properties: `sdk_version: "system_current",`,
97 bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
98 system: "core-current-stubs-system-modules",
99 java9classpath: []string{"android_system_stubs_current"},
100 aidl: "-p" + buildDir + "/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700101 },
102 {
103
Colin Cross74362a42019-10-28 08:05:02 -0700104 name: "system_29",
105 properties: `sdk_version: "system_29",`,
Colin Cross617b1452019-10-25 15:22:50 -0700106 bootclasspath: []string{`""`},
107 forces8: true,
Colin Cross74362a42019-10-28 08:05:02 -0700108 java8classpath: []string{"prebuilts/sdk/29/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
109 aidl: "-pprebuilts/sdk/29/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700110 },
111 {
112
Colin Cross6cef4812019-10-17 14:23:50 -0700113 name: "test_current",
114 properties: `sdk_version: "test_current",`,
115 bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
116 system: "core-current-stubs-system-modules",
117 java9classpath: []string{"android_test_stubs_current"},
118 aidl: "-p" + buildDir + "/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700119 },
120 {
121
Colin Cross6cef4812019-10-17 14:23:50 -0700122 name: "core_current",
123 properties: `sdk_version: "core_current",`,
124 bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
125 system: "core-current-stubs-system-modules",
126 java9classpath: []string{"core.current.stubs"},
Colin Cross3047fa22019-04-18 10:56:44 -0700127 },
128 {
129
Colin Cross617b1452019-10-25 15:22:50 -0700130 name: "nostdlib",
131 properties: `sdk_version: "none", system_modules: "none"`,
132 system: "none",
133 bootclasspath: []string{`""`},
134 java8classpath: []string{},
Paul Duffin52d398a2019-06-11 12:31:14 +0100135 },
136 {
137
Colin Cross617b1452019-10-25 15:22:50 -0700138 name: "nostdlib system_modules",
139 properties: `sdk_version: "none", system_modules: "core-platform-api-stubs-system-modules"`,
140 system: "core-platform-api-stubs-system-modules",
141 bootclasspath: []string{"core-platform-api-stubs-system-modules-lib"},
142 java8classpath: []string{},
Paul Duffin52d398a2019-06-11 12:31:14 +0100143 },
144 {
145
Colin Cross617b1452019-10-25 15:22:50 -0700146 name: "host default",
147 moduleType: "java_library_host",
148 properties: ``,
149 host: android.Host,
150 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
151 java8classpath: []string{},
Colin Cross3047fa22019-04-18 10:56:44 -0700152 },
153 {
Colin Cross3047fa22019-04-18 10:56:44 -0700154
Colin Cross617b1452019-10-25 15:22:50 -0700155 name: "host supported default",
156 host: android.Host,
157 properties: `host_supported: true,`,
158 java8classpath: []string{},
159 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
Colin Cross3047fa22019-04-18 10:56:44 -0700160 },
161 {
Colin Cross617b1452019-10-25 15:22:50 -0700162 name: "host supported nostdlib",
163 host: android.Host,
164 properties: `host_supported: true, sdk_version: "none", system_modules: "none"`,
165 java8classpath: []string{},
Paul Duffin52d398a2019-06-11 12:31:14 +0100166 },
167 {
Colin Cross3047fa22019-04-18 10:56:44 -0700168
Colin Cross74362a42019-10-28 08:05:02 -0700169 name: "unbundled sdk v29",
Colin Cross617b1452019-10-25 15:22:50 -0700170 unbundled: true,
Colin Cross74362a42019-10-28 08:05:02 -0700171 properties: `sdk_version: "29",`,
Colin Cross617b1452019-10-25 15:22:50 -0700172 bootclasspath: []string{`""`},
173 forces8: true,
Colin Cross74362a42019-10-28 08:05:02 -0700174 java8classpath: []string{"prebuilts/sdk/29/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
175 aidl: "-pprebuilts/sdk/29/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700176 },
177 {
178
Colin Cross617b1452019-10-25 15:22:50 -0700179 name: "unbundled current",
180 unbundled: true,
181 properties: `sdk_version: "current",`,
182 bootclasspath: []string{`""`},
183 forces8: true,
184 java8classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
185 aidl: "-pprebuilts/sdk/current/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700186 },
187
188 {
Colin Cross617b1452019-10-25 15:22:50 -0700189 name: "pdk default",
190 pdk: true,
191 bootclasspath: []string{`""`},
192 forces8: true,
Colin Cross74362a42019-10-28 08:05:02 -0700193 java8classpath: []string{"prebuilts/sdk/29/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
194 aidl: "-pprebuilts/sdk/29/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700195 },
196 {
Colin Cross617b1452019-10-25 15:22:50 -0700197 name: "pdk current",
198 pdk: true,
199 properties: `sdk_version: "current",`,
200 bootclasspath: []string{`""`},
201 forces8: true,
Colin Cross74362a42019-10-28 08:05:02 -0700202 java8classpath: []string{"prebuilts/sdk/29/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
203 aidl: "-pprebuilts/sdk/29/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700204 },
205 {
Colin Cross74362a42019-10-28 08:05:02 -0700206 name: "pdk 29",
Colin Cross617b1452019-10-25 15:22:50 -0700207 pdk: true,
Colin Cross74362a42019-10-28 08:05:02 -0700208 properties: `sdk_version: "29",`,
Colin Cross617b1452019-10-25 15:22:50 -0700209 bootclasspath: []string{`""`},
210 forces8: true,
Colin Cross74362a42019-10-28 08:05:02 -0700211 java8classpath: []string{"prebuilts/sdk/29/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
212 aidl: "-pprebuilts/sdk/29/public/framework.aidl",
Colin Cross3047fa22019-04-18 10:56:44 -0700213 },
Jiyong Park50146e92020-01-30 18:00:15 +0900214 {
215
216 name: "module_current",
217 properties: `sdk_version: "module_current",`,
218 bootclasspath: []string{"android_module_lib_stubs_current", "core-lambda-stubs"},
219 system: "core-current-stubs-system-modules",
220 java9classpath: []string{"android_module_lib_stubs_current"},
221 aidl: "-p" + buildDir + "/framework.aidl",
222 },
Colin Cross3047fa22019-04-18 10:56:44 -0700223 }
224
Colin Crossfb6d7812019-01-09 22:17:55 -0800225 for _, testcase := range classpathTestcases {
226 t.Run(testcase.name, func(t *testing.T) {
227 moduleType := "java_library"
228 if testcase.moduleType != "" {
229 moduleType = testcase.moduleType
230 }
231
Pete Gillinbdf5d712019-10-21 14:29:58 +0100232 props := `
Colin Crossfb6d7812019-01-09 22:17:55 -0800233 name: "foo",
234 srcs: ["a.java"],
Colin Cross3047fa22019-04-18 10:56:44 -0700235 target: {
236 android: {
237 srcs: ["bar-doc/IFoo.aidl"],
238 },
239 },
Pete Gillinbdf5d712019-10-21 14:29:58 +0100240 `
241 bp := moduleType + " {" + props + testcase.properties + `
242 }`
243 bpJava8 := moduleType + " {" + props + `java_version: "1.8",
Colin Crossfb6d7812019-01-09 22:17:55 -0800244 ` + testcase.properties + `
245 }`
246
247 variant := "android_common"
248 if testcase.host == android.Host {
249 variant = android.BuildOs.String() + "_common"
250 }
251
252 convertModulesToPaths := func(cp []string) []string {
253 ret := make([]string, len(cp))
254 for i, e := range cp {
255 ret[i] = moduleToPath(e)
256 }
257 return ret
258 }
259
260 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
Colin Cross617b1452019-10-25 15:22:50 -0700261 java8classpath := convertModulesToPaths(testcase.java8classpath)
262 java9classpath := convertModulesToPaths(testcase.java9classpath)
Colin Crossfb6d7812019-01-09 22:17:55 -0800263
Pete Gillinbdf5d712019-10-21 14:29:58 +0100264 bc := ""
265 var bcDeps []string
266 if len(bootclasspath) > 0 {
267 bc = "-bootclasspath " + strings.Join(bootclasspath, ":")
268 if bootclasspath[0] != `""` {
269 bcDeps = bootclasspath
270 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800271 }
272
Colin Cross617b1452019-10-25 15:22:50 -0700273 j8c := ""
274 if len(java8classpath) > 0 {
275 j8c = "-classpath " + strings.Join(java8classpath, ":")
276 }
277
278 j9c := ""
279 if len(java9classpath) > 0 {
280 j9c = "-classpath " + strings.Join(java9classpath, ":")
Colin Crossfb6d7812019-01-09 22:17:55 -0800281 }
Pete Gillinbdf5d712019-10-21 14:29:58 +0100282
Colin Crossfb6d7812019-01-09 22:17:55 -0800283 system := ""
Pete Gillinbdf5d712019-10-21 14:29:58 +0100284 var systemDeps []string
Colin Crossfb6d7812019-01-09 22:17:55 -0800285 if testcase.system == "none" {
286 system = "--system=none"
287 } else if testcase.system != "" {
Dan Willemsenff60a732019-06-13 16:52:01 +0000288 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system")
Pete Gillinbdf5d712019-10-21 14:29:58 +0100289 // The module-relative parts of these paths are hardcoded in system_modules.go:
290 systemDeps = []string{
291 filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system", "lib", "modules"),
292 filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system", "lib", "jrt-fs.jar"),
293 filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system", "release"),
294 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800295 }
296
Pete Gillinbdf5d712019-10-21 14:29:58 +0100297 checkClasspath := func(t *testing.T, ctx *android.TestContext, isJava8 bool) {
Colin Crossc0806172019-06-14 18:51:47 -0700298 foo := ctx.ModuleForTests("foo", variant)
299 javac := foo.Rule("javac")
Pete Gillinbdf5d712019-10-21 14:29:58 +0100300 var deps []string
Colin Crossc0806172019-06-14 18:51:47 -0700301
302 aidl := foo.MaybeRule("aidl")
Pete Gillinbdf5d712019-10-21 14:29:58 +0100303 if aidl.Rule != nil {
304 deps = append(deps, aidl.Output.String())
305 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800306
307 got := javac.Args["bootClasspath"]
Pete Gillinbdf5d712019-10-21 14:29:58 +0100308 expected := ""
Colin Cross617b1452019-10-25 15:22:50 -0700309 if isJava8 || testcase.forces8 {
Pete Gillinbdf5d712019-10-21 14:29:58 +0100310 expected = bc
311 deps = append(deps, bcDeps...)
312 } else {
313 expected = system
314 deps = append(deps, systemDeps...)
315 }
316 if got != expected {
317 t.Errorf("bootclasspath expected %q != got %q", expected, got)
Colin Crossfb6d7812019-01-09 22:17:55 -0800318 }
319
Colin Cross617b1452019-10-25 15:22:50 -0700320 if isJava8 || testcase.forces8 {
321 expected = j8c
322 deps = append(deps, java8classpath...)
323 } else {
324 expected = j9c
325 deps = append(deps, java9classpath...)
Colin Crossfb6d7812019-01-09 22:17:55 -0800326 }
Colin Cross617b1452019-10-25 15:22:50 -0700327 got = javac.Args["classpath"]
328 if got != expected {
329 t.Errorf("classpath expected %q != got %q", expected, got)
330 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800331
332 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
333 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
334 }
Colin Cross3047fa22019-04-18 10:56:44 -0700335 }
336
Pete Gillin1b3370f2019-10-01 13:57:31 +0100337 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillin0c2143e2019-05-02 15:32:11 +0100338 t.Run("Java language level 8", func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800339 config := testConfig(nil, bpJava8, nil)
Colin Cross3047fa22019-04-18 10:56:44 -0700340 if testcase.unbundled {
341 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
342 }
343 if testcase.pdk {
344 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
345 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800346 ctx := testContext()
Colin Cross3047fa22019-04-18 10:56:44 -0700347 run(t, ctx, config)
348
Pete Gillinbdf5d712019-10-21 14:29:58 +0100349 checkClasspath(t, ctx, true /* isJava8 */)
Colin Cross3047fa22019-04-18 10:56:44 -0700350
351 if testcase.host != android.Host {
352 aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
353
Colin Crossc0806172019-06-14 18:51:47 -0700354 if g, w := aidl.RuleParams.Command, testcase.aidl+" -I."; !strings.Contains(g, w) {
355 t.Errorf("want aidl command to contain %q, got %q", w, g)
Colin Cross3047fa22019-04-18 10:56:44 -0700356 }
357 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800358 })
359
Pete Gillin1b3370f2019-10-01 13:57:31 +0100360 // Test with default javac -source 9 -target 9
Pete Gillin0c2143e2019-05-02 15:32:11 +0100361 t.Run("Java language level 9", func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800362 config := testConfig(nil, bp, nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800363 if testcase.unbundled {
364 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
365 }
Colin Cross98fd5742019-01-09 23:04:25 -0800366 if testcase.pdk {
367 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
368 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800369 ctx := testContext()
Colin Crossfb6d7812019-01-09 22:17:55 -0800370 run(t, ctx, config)
371
Pete Gillinbdf5d712019-10-21 14:29:58 +0100372 checkClasspath(t, ctx, false /* isJava8 */)
373
374 if testcase.host != android.Host {
375 aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
376
377 if g, w := aidl.RuleParams.Command, testcase.aidl+" -I."; !strings.Contains(g, w) {
378 t.Errorf("want aidl command to contain %q, got %q", w, g)
379 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800380 }
381 })
Colin Crossff0daf42019-04-02 16:10:56 -0700382
Pete Gillinbdf5d712019-10-21 14:29:58 +0100383 // Test again with PLATFORM_VERSION_CODENAME=REL, javac -source 8 -target 8
384 t.Run("REL + Java language level 8", func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800385 config := testConfig(nil, bpJava8, nil)
Colin Crossff0daf42019-04-02 16:10:56 -0700386 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
387 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
388
389 if testcase.unbundled {
390 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
391 }
392 if testcase.pdk {
393 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
394 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800395 ctx := testContext()
Colin Crossff0daf42019-04-02 16:10:56 -0700396 run(t, ctx, config)
397
Pete Gillinbdf5d712019-10-21 14:29:58 +0100398 checkClasspath(t, ctx, true /* isJava8 */)
Colin Crossff0daf42019-04-02 16:10:56 -0700399 })
Pete Gillinbdf5d712019-10-21 14:29:58 +0100400
Colin Cross6cef4812019-10-17 14:23:50 -0700401 // Test again with PLATFORM_VERSION_CODENAME=REL, javac -source 9 -target 9
402 t.Run("REL + Java language level 9", func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800403 config := testConfig(nil, bp, nil)
Colin Cross6cef4812019-10-17 14:23:50 -0700404 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
405 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
406
407 if testcase.unbundled {
408 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
409 }
410 if testcase.pdk {
411 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
412 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800413 ctx := testContext()
Colin Cross6cef4812019-10-17 14:23:50 -0700414 run(t, ctx, config)
415
416 checkClasspath(t, ctx, false /* isJava8 */)
417 })
Colin Crossfb6d7812019-01-09 22:17:55 -0800418 })
419 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800420}