blob: 1efe83b749da32d02a63585cbcacf179548871a1 [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 {
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 - no_standard_libs: true",
Colin Cross3047fa22019-04-18 10:56:44 -0700117 properties: `no_standard_libs: true, 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",
125 properties: `sdk_version: "none", system_modules: "none"`,
126 system: "none",
127 bootclasspath: []string{`""`},
128 classpath: []string{},
129 },
130 {
131
132 name: "nostdlib system_modules - no_standard_libs: true",
Colin Cross3047fa22019-04-18 10:56:44 -0700133 properties: `no_standard_libs: true, 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
Paul Duffin52d398a2019-06-11 12:31:14 +0100140 name: "nostdlib system_modules",
141 properties: `sdk_version: "none", system_modules: "core-platform-api-stubs-system-modules"`,
142 system: "core-platform-api-stubs-system-modules",
143 bootclasspath: []string{`""`},
144 classpath: []string{},
145 },
146 {
147
Colin Cross3047fa22019-04-18 10:56:44 -0700148 name: "host default",
149 moduleType: "java_library_host",
150 properties: ``,
151 host: android.Host,
152 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
153 classpath: []string{},
154 },
155 {
Colin Cross3047fa22019-04-18 10:56:44 -0700156
157 name: "host supported default",
158 host: android.Host,
159 properties: `host_supported: true,`,
160 classpath: []string{},
161 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
162 },
163 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100164 name: "host supported nostdlib - no_standard_libs: true",
Colin Cross3047fa22019-04-18 10:56:44 -0700165 host: android.Host,
166 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
167 classpath: []string{},
168 },
169 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100170 name: "host supported nostdlib",
171 host: android.Host,
172 properties: `host_supported: true, sdk_version: "none", system_modules: "none"`,
173 classpath: []string{},
174 },
175 {
Colin Cross3047fa22019-04-18 10:56:44 -0700176
177 name: "unbundled sdk v25",
178 unbundled: true,
179 properties: `sdk_version: "25",`,
180 bootclasspath: []string{`""`},
181 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
182 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
183 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
184 },
185 {
186
187 name: "unbundled current",
188 unbundled: true,
189 properties: `sdk_version: "current",`,
190 bootclasspath: []string{`""`},
191 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
192 classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
193 aidl: "-pprebuilts/sdk/current/public/framework.aidl",
194 },
195
196 {
197 name: "pdk default",
198 pdk: true,
199 bootclasspath: []string{`""`},
200 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
201 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
202 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
203 },
204 {
205 name: "pdk current",
206 pdk: true,
207 properties: `sdk_version: "current",`,
208 bootclasspath: []string{`""`},
209 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
210 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
211 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
212 },
213 {
214 name: "pdk 25",
215 pdk: true,
216 properties: `sdk_version: "25",`,
217 bootclasspath: []string{`""`},
218 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
219 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
220 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
221 },
222 }
223
Colin Crossfb6d7812019-01-09 22:17:55 -0800224 for _, testcase := range classpathTestcases {
225 t.Run(testcase.name, func(t *testing.T) {
226 moduleType := "java_library"
227 if testcase.moduleType != "" {
228 moduleType = testcase.moduleType
229 }
230
231 bp := moduleType + ` {
232 name: "foo",
233 srcs: ["a.java"],
Colin Cross3047fa22019-04-18 10:56:44 -0700234 target: {
235 android: {
236 srcs: ["bar-doc/IFoo.aidl"],
237 },
238 },
Colin Crossfb6d7812019-01-09 22:17:55 -0800239 ` + testcase.properties + `
240 }`
241
242 variant := "android_common"
243 if testcase.host == android.Host {
244 variant = android.BuildOs.String() + "_common"
245 }
246
247 convertModulesToPaths := func(cp []string) []string {
248 ret := make([]string, len(cp))
249 for i, e := range cp {
250 ret[i] = moduleToPath(e)
251 }
252 return ret
253 }
254
255 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
256 classpath := convertModulesToPaths(testcase.classpath)
257
258 bc := strings.Join(bootclasspath, ":")
259 if bc != "" {
260 bc = "-bootclasspath " + bc
261 }
262
263 c := strings.Join(classpath, ":")
264 if c != "" {
265 c = "-classpath " + c
266 }
267 system := ""
268 if testcase.system == "none" {
269 system = "--system=none"
270 } else if testcase.system != "" {
271 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
272 }
273
Colin Cross3047fa22019-04-18 10:56:44 -0700274 checkClasspath := func(t *testing.T, ctx *android.TestContext) {
Colin Crossfb6d7812019-01-09 22:17:55 -0800275 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
276
277 got := javac.Args["bootClasspath"]
278 if got != bc {
279 t.Errorf("bootclasspath expected %q != got %q", bc, got)
280 }
281
282 got = javac.Args["classpath"]
283 if got != c {
284 t.Errorf("classpath expected %q != got %q", c, got)
285 }
286
287 var deps []string
288 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
289 deps = append(deps, bootclasspath...)
290 }
291 deps = append(deps, classpath...)
292
293 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
294 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
295 }
Colin Cross3047fa22019-04-18 10:56:44 -0700296 }
297
Pete Gillin0c2143e2019-05-02 15:32:11 +0100298 t.Run("Java language level 8", func(t *testing.T) {
299 // Test default javac -source 1.8 -target 1.8
Colin Cross3047fa22019-04-18 10:56:44 -0700300 config := testConfig(nil)
301 if testcase.unbundled {
302 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
303 }
304 if testcase.pdk {
305 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
306 }
307 ctx := testContext(config, bp, nil)
308 run(t, ctx, config)
309
310 checkClasspath(t, ctx)
311
312 if testcase.host != android.Host {
313 aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
314
315 aidlFlags := aidl.Args["aidlFlags"]
316 // Trim trailing "-I." to avoid having to specify it in every test
317 aidlFlags = strings.TrimSpace(strings.TrimSuffix(aidlFlags, "-I."))
318
319 if g, w := aidlFlags, testcase.aidl; g != w {
320 t.Errorf("want aidl flags %q, got %q", w, g)
321 }
322 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800323 })
324
Pete Gillin0c2143e2019-05-02 15:32:11 +0100325 // Test again with javac -source 9 -target 9
326 t.Run("Java language level 9", func(t *testing.T) {
327 config := testConfig(map[string]string{"EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9": "true"})
Colin Crossfb6d7812019-01-09 22:17:55 -0800328 if testcase.unbundled {
329 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
330 }
Colin Cross98fd5742019-01-09 23:04:25 -0800331 if testcase.pdk {
332 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
333 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800334 ctx := testContext(config, bp, nil)
335 run(t, ctx, config)
336
337 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
338 got := javac.Args["bootClasspath"]
339 expected := system
340 if testcase.system == "bootclasspath" {
341 expected = bc
342 }
343 if got != expected {
344 t.Errorf("bootclasspath expected %q != got %q", expected, got)
345 }
346 })
Colin Crossff0daf42019-04-02 16:10:56 -0700347
348 // Test again with PLATFORM_VERSION_CODENAME=REL
349 t.Run("REL", func(t *testing.T) {
350 config := testConfig(nil)
351 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
352 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
353
354 if testcase.unbundled {
355 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
356 }
357 if testcase.pdk {
358 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
359 }
360 ctx := testContext(config, bp, nil)
361 run(t, ctx, config)
362
Colin Cross3047fa22019-04-18 10:56:44 -0700363 checkClasspath(t, ctx)
Colin Crossff0daf42019-04-02 16:10:56 -0700364 })
Colin Crossfb6d7812019-01-09 22:17:55 -0800365 })
366 }
367
368}