blob: fd47d81c96a6a6129681387e4c17783117620bd6 [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 {
31 name string
32 unbundled bool
33 pdk bool
34 moduleType string
35 host android.OsClass
36 properties string
37 bootclasspath []string
38 system string
39 classpath []string
40 aidl string
41 }{
42 {
43 name: "default",
44 bootclasspath: config.DefaultBootclasspathLibraries,
45 system: config.DefaultSystemModules,
46 classpath: config.DefaultLibraries,
47 aidl: "-Iframework/aidl",
48 },
49 {
Paul Duffin50c217c2019-06-12 13:25:22 +010050 name: `sdk_version:"core_platform"`,
51 properties: `sdk_version:"core_platform"`,
52 bootclasspath: config.DefaultBootclasspathLibraries,
53 system: config.DefaultSystemModules,
54 classpath: []string{},
55 aidl: "",
56 },
57 {
Colin Cross3047fa22019-04-18 10:56:44 -070058 name: "blank sdk version",
59 properties: `sdk_version: "",`,
60 bootclasspath: config.DefaultBootclasspathLibraries,
61 system: config.DefaultSystemModules,
62 classpath: config.DefaultLibraries,
63 aidl: "-Iframework/aidl",
64 },
65 {
66
67 name: "sdk v25",
68 properties: `sdk_version: "25",`,
69 bootclasspath: []string{`""`},
70 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
71 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
72 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
73 },
74 {
75
76 name: "current",
77 properties: `sdk_version: "current",`,
78 bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
79 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
80 aidl: "-p" + buildDir + "/framework.aidl",
81 },
82 {
83
84 name: "system_current",
85 properties: `sdk_version: "system_current",`,
86 bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
87 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
88 aidl: "-p" + buildDir + "/framework.aidl",
89 },
90 {
91
92 name: "system_25",
93 properties: `sdk_version: "system_25",`,
94 bootclasspath: []string{`""`},
95 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
96 classpath: []string{"prebuilts/sdk/25/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
97 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
98 },
99 {
100
101 name: "test_current",
102 properties: `sdk_version: "test_current",`,
103 bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
104 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
105 aidl: "-p" + buildDir + "/framework.aidl",
106 },
107 {
108
109 name: "core_current",
110 properties: `sdk_version: "core_current",`,
111 bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
112 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
113 },
114 {
115
Paul Duffin52d398a2019-06-11 12:31:14 +0100116 name: "nostdlib",
117 properties: `sdk_version: "none", system_modules: "none"`,
118 system: "none",
119 bootclasspath: []string{`""`},
120 classpath: []string{},
121 },
122 {
123
Paul Duffin52d398a2019-06-11 12:31:14 +0100124 name: "nostdlib system_modules",
125 properties: `sdk_version: "none", system_modules: "core-platform-api-stubs-system-modules"`,
126 system: "core-platform-api-stubs-system-modules",
Paul Duffin68289b02019-09-20 13:50:52 +0100127 bootclasspath: []string{"core-platform-api-stubs-system-modules-lib"},
Paul Duffin52d398a2019-06-11 12:31:14 +0100128 classpath: []string{},
129 },
130 {
131
Colin Cross3047fa22019-04-18 10:56:44 -0700132 name: "host default",
133 moduleType: "java_library_host",
134 properties: ``,
135 host: android.Host,
136 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
137 classpath: []string{},
138 },
139 {
Colin Cross3047fa22019-04-18 10:56:44 -0700140
141 name: "host supported default",
142 host: android.Host,
143 properties: `host_supported: true,`,
144 classpath: []string{},
145 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
146 },
147 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100148 name: "host supported nostdlib",
149 host: android.Host,
150 properties: `host_supported: true, sdk_version: "none", system_modules: "none"`,
151 classpath: []string{},
152 },
153 {
Colin Cross3047fa22019-04-18 10:56:44 -0700154
155 name: "unbundled sdk v25",
156 unbundled: true,
157 properties: `sdk_version: "25",`,
158 bootclasspath: []string{`""`},
159 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
160 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
161 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
162 },
163 {
164
165 name: "unbundled current",
166 unbundled: true,
167 properties: `sdk_version: "current",`,
168 bootclasspath: []string{`""`},
169 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
170 classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
171 aidl: "-pprebuilts/sdk/current/public/framework.aidl",
172 },
173
174 {
175 name: "pdk default",
176 pdk: true,
177 bootclasspath: []string{`""`},
178 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
179 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
180 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
181 },
182 {
183 name: "pdk current",
184 pdk: true,
185 properties: `sdk_version: "current",`,
186 bootclasspath: []string{`""`},
187 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
188 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
189 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
190 },
191 {
192 name: "pdk 25",
193 pdk: true,
194 properties: `sdk_version: "25",`,
195 bootclasspath: []string{`""`},
196 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
197 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
198 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
199 },
200 }
201
Colin Crossfb6d7812019-01-09 22:17:55 -0800202 for _, testcase := range classpathTestcases {
203 t.Run(testcase.name, func(t *testing.T) {
204 moduleType := "java_library"
205 if testcase.moduleType != "" {
206 moduleType = testcase.moduleType
207 }
208
Pete Gillinbdf5d712019-10-21 14:29:58 +0100209 props := `
Colin Crossfb6d7812019-01-09 22:17:55 -0800210 name: "foo",
211 srcs: ["a.java"],
Colin Cross3047fa22019-04-18 10:56:44 -0700212 target: {
213 android: {
214 srcs: ["bar-doc/IFoo.aidl"],
215 },
216 },
Pete Gillinbdf5d712019-10-21 14:29:58 +0100217 `
218 bp := moduleType + " {" + props + testcase.properties + `
219 }`
220 bpJava8 := moduleType + " {" + props + `java_version: "1.8",
Colin Crossfb6d7812019-01-09 22:17:55 -0800221 ` + testcase.properties + `
222 }`
223
224 variant := "android_common"
225 if testcase.host == android.Host {
226 variant = android.BuildOs.String() + "_common"
227 }
228
229 convertModulesToPaths := func(cp []string) []string {
230 ret := make([]string, len(cp))
231 for i, e := range cp {
232 ret[i] = moduleToPath(e)
233 }
234 return ret
235 }
236
237 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
238 classpath := convertModulesToPaths(testcase.classpath)
239
Pete Gillinbdf5d712019-10-21 14:29:58 +0100240 bc := ""
241 var bcDeps []string
242 if len(bootclasspath) > 0 {
243 bc = "-bootclasspath " + strings.Join(bootclasspath, ":")
244 if bootclasspath[0] != `""` {
245 bcDeps = bootclasspath
246 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800247 }
248
Pete Gillinbdf5d712019-10-21 14:29:58 +0100249 c := ""
250 if len(classpath) > 0 {
251 c = "-classpath " + strings.Join(classpath, ":")
Colin Crossfb6d7812019-01-09 22:17:55 -0800252 }
Pete Gillinbdf5d712019-10-21 14:29:58 +0100253
Colin Crossfb6d7812019-01-09 22:17:55 -0800254 system := ""
Pete Gillinbdf5d712019-10-21 14:29:58 +0100255 var systemDeps []string
Colin Crossfb6d7812019-01-09 22:17:55 -0800256 if testcase.system == "none" {
257 system = "--system=none"
Pete Gillinbdf5d712019-10-21 14:29:58 +0100258 } else if testcase.system == "bootclasspath" {
259 system = bc
260 systemDeps = bcDeps
Colin Crossfb6d7812019-01-09 22:17:55 -0800261 } else if testcase.system != "" {
Dan Willemsenff60a732019-06-13 16:52:01 +0000262 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system")
Pete Gillinbdf5d712019-10-21 14:29:58 +0100263 // The module-relative parts of these paths are hardcoded in system_modules.go:
264 systemDeps = []string{
265 filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system", "lib", "modules"),
266 filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system", "lib", "jrt-fs.jar"),
267 filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system", "release"),
268 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800269 }
270
Pete Gillinbdf5d712019-10-21 14:29:58 +0100271 checkClasspath := func(t *testing.T, ctx *android.TestContext, isJava8 bool) {
Colin Crossc0806172019-06-14 18:51:47 -0700272 foo := ctx.ModuleForTests("foo", variant)
273 javac := foo.Rule("javac")
Pete Gillinbdf5d712019-10-21 14:29:58 +0100274 var deps []string
Colin Crossc0806172019-06-14 18:51:47 -0700275
276 aidl := foo.MaybeRule("aidl")
Pete Gillinbdf5d712019-10-21 14:29:58 +0100277 if aidl.Rule != nil {
278 deps = append(deps, aidl.Output.String())
279 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800280
281 got := javac.Args["bootClasspath"]
Pete Gillinbdf5d712019-10-21 14:29:58 +0100282 expected := ""
283 if isJava8 {
284 expected = bc
285 deps = append(deps, bcDeps...)
286 } else {
287 expected = system
288 deps = append(deps, systemDeps...)
289 }
290 if got != expected {
291 t.Errorf("bootclasspath expected %q != got %q", expected, got)
Colin Crossfb6d7812019-01-09 22:17:55 -0800292 }
293
294 got = javac.Args["classpath"]
295 if got != c {
296 t.Errorf("classpath expected %q != got %q", c, got)
297 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800298 deps = append(deps, classpath...)
299
300 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
301 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
302 }
Colin Cross3047fa22019-04-18 10:56:44 -0700303 }
304
Pete Gillin1b3370f2019-10-01 13:57:31 +0100305 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillin0c2143e2019-05-02 15:32:11 +0100306 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillinbdf5d712019-10-21 14:29:58 +0100307 config := testConfig(nil)
Colin Cross3047fa22019-04-18 10:56:44 -0700308 if testcase.unbundled {
309 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
310 }
311 if testcase.pdk {
312 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
313 }
Pete Gillinbdf5d712019-10-21 14:29:58 +0100314 ctx := testContext(bpJava8, nil)
Colin Cross3047fa22019-04-18 10:56:44 -0700315 run(t, ctx, config)
316
Pete Gillinbdf5d712019-10-21 14:29:58 +0100317 checkClasspath(t, ctx, true /* isJava8 */)
Colin Cross3047fa22019-04-18 10:56:44 -0700318
319 if testcase.host != android.Host {
320 aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
321
Colin Crossc0806172019-06-14 18:51:47 -0700322 if g, w := aidl.RuleParams.Command, testcase.aidl+" -I."; !strings.Contains(g, w) {
323 t.Errorf("want aidl command to contain %q, got %q", w, g)
Colin Cross3047fa22019-04-18 10:56:44 -0700324 }
325 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800326 })
327
Pete Gillin1b3370f2019-10-01 13:57:31 +0100328 // Test with default javac -source 9 -target 9
Pete Gillin0c2143e2019-05-02 15:32:11 +0100329 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +0100330 config := testConfig(nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800331 if testcase.unbundled {
332 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
333 }
Colin Cross98fd5742019-01-09 23:04:25 -0800334 if testcase.pdk {
335 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
336 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700337 ctx := testContext(bp, nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800338 run(t, ctx, config)
339
Pete Gillinbdf5d712019-10-21 14:29:58 +0100340 checkClasspath(t, ctx, false /* isJava8 */)
341
342 if testcase.host != android.Host {
343 aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
344
345 if g, w := aidl.RuleParams.Command, testcase.aidl+" -I."; !strings.Contains(g, w) {
346 t.Errorf("want aidl command to contain %q, got %q", w, g)
347 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800348 }
349 })
Colin Crossff0daf42019-04-02 16:10:56 -0700350
Pete Gillinbdf5d712019-10-21 14:29:58 +0100351 // Test again with PLATFORM_VERSION_CODENAME=REL, javac -source 8 -target 8
352 t.Run("REL + Java language level 8", func(t *testing.T) {
353 config := testConfig(nil)
Colin Crossff0daf42019-04-02 16:10:56 -0700354 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
355 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
356
357 if testcase.unbundled {
358 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
359 }
360 if testcase.pdk {
361 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
362 }
Pete Gillinbdf5d712019-10-21 14:29:58 +0100363 ctx := testContext(bpJava8, nil)
Colin Crossff0daf42019-04-02 16:10:56 -0700364 run(t, ctx, config)
365
Pete Gillinbdf5d712019-10-21 14:29:58 +0100366 checkClasspath(t, ctx, true /* isJava8 */)
Colin Crossff0daf42019-04-02 16:10:56 -0700367 })
Pete Gillinbdf5d712019-10-21 14:29:58 +0100368
369 // 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 -0800370 })
371 }
372
373}