blob: 915333ec98e63d1ec6e682e08d5a12be835474ee [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 Duffinfad79802019-06-12 13:00:53 +010050 name: "no_framework_libs:true",
51 properties: `no_framework_libs:true`,
52 bootclasspath: config.DefaultBootclasspathLibraries,
53 system: config.DefaultSystemModules,
54 classpath: []string{},
55 aidl: "",
56 },
57 {
Paul Duffin50c217c2019-06-12 13:25:22 +010058 name: `sdk_version:"core_platform"`,
59 properties: `sdk_version:"core_platform"`,
60 bootclasspath: config.DefaultBootclasspathLibraries,
61 system: config.DefaultSystemModules,
62 classpath: []string{},
63 aidl: "",
64 },
65 {
Colin Cross3047fa22019-04-18 10:56:44 -070066 name: "blank sdk version",
67 properties: `sdk_version: "",`,
68 bootclasspath: config.DefaultBootclasspathLibraries,
69 system: config.DefaultSystemModules,
70 classpath: config.DefaultLibraries,
71 aidl: "-Iframework/aidl",
72 },
73 {
74
75 name: "sdk v25",
76 properties: `sdk_version: "25",`,
77 bootclasspath: []string{`""`},
78 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
79 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
80 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
81 },
82 {
83
84 name: "current",
85 properties: `sdk_version: "current",`,
86 bootclasspath: []string{"android_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_current",
93 properties: `sdk_version: "system_current",`,
94 bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
95 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
96 aidl: "-p" + buildDir + "/framework.aidl",
97 },
98 {
99
100 name: "system_25",
101 properties: `sdk_version: "system_25",`,
102 bootclasspath: []string{`""`},
103 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
104 classpath: []string{"prebuilts/sdk/25/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
105 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
106 },
107 {
108
109 name: "test_current",
110 properties: `sdk_version: "test_current",`,
111 bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
112 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
113 aidl: "-p" + buildDir + "/framework.aidl",
114 },
115 {
116
117 name: "core_current",
118 properties: `sdk_version: "core_current",`,
119 bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
120 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
121 },
122 {
123
Paul Duffin52d398a2019-06-11 12:31:14 +0100124 name: "nostdlib",
125 properties: `sdk_version: "none", system_modules: "none"`,
126 system: "none",
127 bootclasspath: []string{`""`},
128 classpath: []string{},
129 },
130 {
131
Paul Duffin52d398a2019-06-11 12:31:14 +0100132 name: "nostdlib system_modules",
133 properties: `sdk_version: "none", system_modules: "core-platform-api-stubs-system-modules"`,
134 system: "core-platform-api-stubs-system-modules",
135 bootclasspath: []string{`""`},
136 classpath: []string{},
137 },
138 {
139
Colin Cross3047fa22019-04-18 10:56:44 -0700140 name: "host default",
141 moduleType: "java_library_host",
142 properties: ``,
143 host: android.Host,
144 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
145 classpath: []string{},
146 },
147 {
Colin Cross3047fa22019-04-18 10:56:44 -0700148
149 name: "host supported default",
150 host: android.Host,
151 properties: `host_supported: true,`,
152 classpath: []string{},
153 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
154 },
155 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100156 name: "host supported nostdlib",
157 host: android.Host,
158 properties: `host_supported: true, sdk_version: "none", system_modules: "none"`,
159 classpath: []string{},
160 },
161 {
Colin Cross3047fa22019-04-18 10:56:44 -0700162
163 name: "unbundled sdk v25",
164 unbundled: true,
165 properties: `sdk_version: "25",`,
166 bootclasspath: []string{`""`},
167 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
168 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
169 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
170 },
171 {
172
173 name: "unbundled current",
174 unbundled: true,
175 properties: `sdk_version: "current",`,
176 bootclasspath: []string{`""`},
177 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
178 classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
179 aidl: "-pprebuilts/sdk/current/public/framework.aidl",
180 },
181
182 {
183 name: "pdk default",
184 pdk: true,
185 bootclasspath: []string{`""`},
186 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
187 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
188 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
189 },
190 {
191 name: "pdk current",
192 pdk: true,
193 properties: `sdk_version: "current",`,
194 bootclasspath: []string{`""`},
195 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
196 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
197 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
198 },
199 {
200 name: "pdk 25",
201 pdk: true,
202 properties: `sdk_version: "25",`,
203 bootclasspath: []string{`""`},
204 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
205 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
206 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
207 },
208 }
209
Colin Crossfb6d7812019-01-09 22:17:55 -0800210 for _, testcase := range classpathTestcases {
211 t.Run(testcase.name, func(t *testing.T) {
212 moduleType := "java_library"
213 if testcase.moduleType != "" {
214 moduleType = testcase.moduleType
215 }
216
217 bp := moduleType + ` {
218 name: "foo",
219 srcs: ["a.java"],
Colin Cross3047fa22019-04-18 10:56:44 -0700220 target: {
221 android: {
222 srcs: ["bar-doc/IFoo.aidl"],
223 },
224 },
Colin Crossfb6d7812019-01-09 22:17:55 -0800225 ` + testcase.properties + `
226 }`
227
228 variant := "android_common"
229 if testcase.host == android.Host {
230 variant = android.BuildOs.String() + "_common"
231 }
232
233 convertModulesToPaths := func(cp []string) []string {
234 ret := make([]string, len(cp))
235 for i, e := range cp {
236 ret[i] = moduleToPath(e)
237 }
238 return ret
239 }
240
241 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
242 classpath := convertModulesToPaths(testcase.classpath)
243
244 bc := strings.Join(bootclasspath, ":")
245 if bc != "" {
246 bc = "-bootclasspath " + bc
247 }
248
249 c := strings.Join(classpath, ":")
250 if c != "" {
251 c = "-classpath " + c
252 }
253 system := ""
254 if testcase.system == "none" {
255 system = "--system=none"
256 } else if testcase.system != "" {
257 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
258 }
259
Colin Cross3047fa22019-04-18 10:56:44 -0700260 checkClasspath := func(t *testing.T, ctx *android.TestContext) {
Colin Crossfb6d7812019-01-09 22:17:55 -0800261 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
262
263 got := javac.Args["bootClasspath"]
264 if got != bc {
265 t.Errorf("bootclasspath expected %q != got %q", bc, got)
266 }
267
268 got = javac.Args["classpath"]
269 if got != c {
270 t.Errorf("classpath expected %q != got %q", c, got)
271 }
272
273 var deps []string
274 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
275 deps = append(deps, bootclasspath...)
276 }
277 deps = append(deps, classpath...)
278
279 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
280 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
281 }
Colin Cross3047fa22019-04-18 10:56:44 -0700282 }
283
Pete Gillin0c2143e2019-05-02 15:32:11 +0100284 t.Run("Java language level 8", func(t *testing.T) {
285 // Test default javac -source 1.8 -target 1.8
Colin Cross3047fa22019-04-18 10:56:44 -0700286 config := testConfig(nil)
287 if testcase.unbundled {
288 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
289 }
290 if testcase.pdk {
291 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
292 }
293 ctx := testContext(config, bp, nil)
294 run(t, ctx, config)
295
296 checkClasspath(t, ctx)
297
298 if testcase.host != android.Host {
299 aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
300
301 aidlFlags := aidl.Args["aidlFlags"]
302 // Trim trailing "-I." to avoid having to specify it in every test
303 aidlFlags = strings.TrimSpace(strings.TrimSuffix(aidlFlags, "-I."))
304
305 if g, w := aidlFlags, testcase.aidl; g != w {
306 t.Errorf("want aidl flags %q, got %q", w, g)
307 }
308 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800309 })
310
Pete Gillin0c2143e2019-05-02 15:32:11 +0100311 // Test again with javac -source 9 -target 9
312 t.Run("Java language level 9", func(t *testing.T) {
313 config := testConfig(map[string]string{"EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9": "true"})
Colin Crossfb6d7812019-01-09 22:17:55 -0800314 if testcase.unbundled {
315 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
316 }
Colin Cross98fd5742019-01-09 23:04:25 -0800317 if testcase.pdk {
318 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
319 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800320 ctx := testContext(config, bp, nil)
321 run(t, ctx, config)
322
323 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
324 got := javac.Args["bootClasspath"]
325 expected := system
326 if testcase.system == "bootclasspath" {
327 expected = bc
328 }
329 if got != expected {
330 t.Errorf("bootclasspath expected %q != got %q", expected, got)
331 }
332 })
Colin Crossff0daf42019-04-02 16:10:56 -0700333
334 // Test again with PLATFORM_VERSION_CODENAME=REL
335 t.Run("REL", func(t *testing.T) {
336 config := testConfig(nil)
337 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
338 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
339
340 if testcase.unbundled {
341 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
342 }
343 if testcase.pdk {
344 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
345 }
346 ctx := testContext(config, bp, nil)
347 run(t, ctx, config)
348
Colin Cross3047fa22019-04-18 10:56:44 -0700349 checkClasspath(t, ctx)
Colin Crossff0daf42019-04-02 16:10:56 -0700350 })
Colin Crossfb6d7812019-01-09 22:17:55 -0800351 })
352 }
353
354}