blob: 142d6f55e7d5930f0ed67d342fd75de1fce5b63f [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
116 name: "nostdlib",
117 properties: `no_standard_libs: true, system_modules: "none"`,
118 system: "none",
119 bootclasspath: []string{`""`},
120 classpath: []string{},
121 },
122 {
123
124 name: "nostdlib system_modules",
125 properties: `no_standard_libs: true, system_modules: "core-platform-api-stubs-system-modules"`,
126 system: "core-platform-api-stubs-system-modules",
127 bootclasspath: []string{`""`},
128 classpath: []string{},
129 },
130 {
131
132 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 {
140 name: "host nostdlib",
141 moduleType: "java_library_host",
142 host: android.Host,
143 properties: `no_standard_libs: true`,
144 classpath: []string{},
145 },
146 {
147
148 name: "host supported default",
149 host: android.Host,
150 properties: `host_supported: true,`,
151 classpath: []string{},
152 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
153 },
154 {
155 name: "host supported nostdlib",
156 host: android.Host,
157 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
158 classpath: []string{},
159 },
160 {
161
162 name: "unbundled sdk v25",
163 unbundled: true,
164 properties: `sdk_version: "25",`,
165 bootclasspath: []string{`""`},
166 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
167 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
168 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
169 },
170 {
171
172 name: "unbundled current",
173 unbundled: true,
174 properties: `sdk_version: "current",`,
175 bootclasspath: []string{`""`},
176 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
177 classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
178 aidl: "-pprebuilts/sdk/current/public/framework.aidl",
179 },
180
181 {
182 name: "pdk default",
183 pdk: true,
184 bootclasspath: []string{`""`},
185 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
186 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
187 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
188 },
189 {
190 name: "pdk current",
191 pdk: true,
192 properties: `sdk_version: "current",`,
193 bootclasspath: []string{`""`},
194 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
195 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
196 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
197 },
198 {
199 name: "pdk 25",
200 pdk: true,
201 properties: `sdk_version: "25",`,
202 bootclasspath: []string{`""`},
203 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
204 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
205 aidl: "-pprebuilts/sdk/25/public/framework.aidl",
206 },
207 }
208
Colin Crossfb6d7812019-01-09 22:17:55 -0800209 for _, testcase := range classpathTestcases {
210 t.Run(testcase.name, func(t *testing.T) {
211 moduleType := "java_library"
212 if testcase.moduleType != "" {
213 moduleType = testcase.moduleType
214 }
215
216 bp := moduleType + ` {
217 name: "foo",
218 srcs: ["a.java"],
Colin Cross3047fa22019-04-18 10:56:44 -0700219 target: {
220 android: {
221 srcs: ["bar-doc/IFoo.aidl"],
222 },
223 },
Colin Crossfb6d7812019-01-09 22:17:55 -0800224 ` + testcase.properties + `
225 }`
226
227 variant := "android_common"
228 if testcase.host == android.Host {
229 variant = android.BuildOs.String() + "_common"
230 }
231
232 convertModulesToPaths := func(cp []string) []string {
233 ret := make([]string, len(cp))
234 for i, e := range cp {
235 ret[i] = moduleToPath(e)
236 }
237 return ret
238 }
239
240 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
241 classpath := convertModulesToPaths(testcase.classpath)
242
243 bc := strings.Join(bootclasspath, ":")
244 if bc != "" {
245 bc = "-bootclasspath " + bc
246 }
247
248 c := strings.Join(classpath, ":")
249 if c != "" {
250 c = "-classpath " + c
251 }
252 system := ""
253 if testcase.system == "none" {
254 system = "--system=none"
255 } else if testcase.system != "" {
256 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
257 }
258
Colin Cross3047fa22019-04-18 10:56:44 -0700259 checkClasspath := func(t *testing.T, ctx *android.TestContext) {
Colin Crossfb6d7812019-01-09 22:17:55 -0800260 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
261
262 got := javac.Args["bootClasspath"]
263 if got != bc {
264 t.Errorf("bootclasspath expected %q != got %q", bc, got)
265 }
266
267 got = javac.Args["classpath"]
268 if got != c {
269 t.Errorf("classpath expected %q != got %q", c, got)
270 }
271
272 var deps []string
273 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
274 deps = append(deps, bootclasspath...)
275 }
276 deps = append(deps, classpath...)
277
278 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
279 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
280 }
Colin Cross3047fa22019-04-18 10:56:44 -0700281 }
282
Pete Gillin0c2143e2019-05-02 15:32:11 +0100283 t.Run("Java language level 8", func(t *testing.T) {
284 // Test default javac -source 1.8 -target 1.8
Colin Cross3047fa22019-04-18 10:56:44 -0700285 config := testConfig(nil)
286 if testcase.unbundled {
287 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
288 }
289 if testcase.pdk {
290 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
291 }
292 ctx := testContext(config, bp, nil)
293 run(t, ctx, config)
294
295 checkClasspath(t, ctx)
296
297 if testcase.host != android.Host {
298 aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
299
300 aidlFlags := aidl.Args["aidlFlags"]
301 // Trim trailing "-I." to avoid having to specify it in every test
302 aidlFlags = strings.TrimSpace(strings.TrimSuffix(aidlFlags, "-I."))
303
304 if g, w := aidlFlags, testcase.aidl; g != w {
305 t.Errorf("want aidl flags %q, got %q", w, g)
306 }
307 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800308 })
309
Pete Gillin0c2143e2019-05-02 15:32:11 +0100310 // Test again with javac -source 9 -target 9
311 t.Run("Java language level 9", func(t *testing.T) {
312 config := testConfig(map[string]string{"EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9": "true"})
Colin Crossfb6d7812019-01-09 22:17:55 -0800313 if testcase.unbundled {
314 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
315 }
Colin Cross98fd5742019-01-09 23:04:25 -0800316 if testcase.pdk {
317 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
318 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800319 ctx := testContext(config, bp, nil)
320 run(t, ctx, config)
321
322 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
323 got := javac.Args["bootClasspath"]
324 expected := system
325 if testcase.system == "bootclasspath" {
326 expected = bc
327 }
328 if got != expected {
329 t.Errorf("bootclasspath expected %q != got %q", expected, got)
330 }
331 })
Colin Crossff0daf42019-04-02 16:10:56 -0700332
333 // Test again with PLATFORM_VERSION_CODENAME=REL
334 t.Run("REL", func(t *testing.T) {
335 config := testConfig(nil)
336 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
337 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
338
339 if testcase.unbundled {
340 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
341 }
342 if testcase.pdk {
343 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
344 }
345 ctx := testContext(config, bp, nil)
346 run(t, ctx, config)
347
Colin Cross3047fa22019-04-18 10:56:44 -0700348 checkClasspath(t, ctx)
Colin Crossff0daf42019-04-02 16:10:56 -0700349 })
Colin Crossfb6d7812019-01-09 22:17:55 -0800350 })
351 }
352
353}