blob: 5001b477b08dab4bf42bea3dd6cad11aa1ec3167 [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
209 bp := moduleType + ` {
210 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 },
Colin Crossfb6d7812019-01-09 22:17:55 -0800217 ` + testcase.properties + `
218 }`
219
220 variant := "android_common"
221 if testcase.host == android.Host {
222 variant = android.BuildOs.String() + "_common"
223 }
224
225 convertModulesToPaths := func(cp []string) []string {
226 ret := make([]string, len(cp))
227 for i, e := range cp {
228 ret[i] = moduleToPath(e)
229 }
230 return ret
231 }
232
233 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
234 classpath := convertModulesToPaths(testcase.classpath)
235
236 bc := strings.Join(bootclasspath, ":")
237 if bc != "" {
238 bc = "-bootclasspath " + bc
239 }
240
241 c := strings.Join(classpath, ":")
242 if c != "" {
243 c = "-classpath " + c
244 }
245 system := ""
246 if testcase.system == "none" {
247 system = "--system=none"
248 } else if testcase.system != "" {
Dan Willemsenff60a732019-06-13 16:52:01 +0000249 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system")
Colin Crossfb6d7812019-01-09 22:17:55 -0800250 }
251
Colin Cross3047fa22019-04-18 10:56:44 -0700252 checkClasspath := func(t *testing.T, ctx *android.TestContext) {
Colin Crossc0806172019-06-14 18:51:47 -0700253 foo := ctx.ModuleForTests("foo", variant)
254 javac := foo.Rule("javac")
255
256 aidl := foo.MaybeRule("aidl")
Colin Crossfb6d7812019-01-09 22:17:55 -0800257
258 got := javac.Args["bootClasspath"]
259 if got != bc {
260 t.Errorf("bootclasspath expected %q != got %q", bc, got)
261 }
262
263 got = javac.Args["classpath"]
264 if got != c {
265 t.Errorf("classpath expected %q != got %q", c, got)
266 }
267
268 var deps []string
Colin Crossc0806172019-06-14 18:51:47 -0700269 if aidl.Rule != nil {
270 deps = append(deps, aidl.Output.String())
271 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800272 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
273 deps = append(deps, bootclasspath...)
274 }
275 deps = append(deps, classpath...)
276
277 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
278 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
279 }
Colin Cross3047fa22019-04-18 10:56:44 -0700280 }
281
Pete Gillin0c2143e2019-05-02 15:32:11 +0100282 t.Run("Java language level 8", func(t *testing.T) {
283 // Test default javac -source 1.8 -target 1.8
Colin Cross3047fa22019-04-18 10:56:44 -0700284 config := testConfig(nil)
285 if testcase.unbundled {
286 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
287 }
288 if testcase.pdk {
289 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
290 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700291 ctx := testContext(bp, nil)
Colin Cross3047fa22019-04-18 10:56:44 -0700292 run(t, ctx, config)
293
294 checkClasspath(t, ctx)
295
296 if testcase.host != android.Host {
297 aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")
298
Colin Crossc0806172019-06-14 18:51:47 -0700299 if g, w := aidl.RuleParams.Command, testcase.aidl+" -I."; !strings.Contains(g, w) {
300 t.Errorf("want aidl command to contain %q, got %q", w, g)
Colin Cross3047fa22019-04-18 10:56:44 -0700301 }
302 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800303 })
304
Pete Gillin0c2143e2019-05-02 15:32:11 +0100305 // Test again with javac -source 9 -target 9
306 t.Run("Java language level 9", func(t *testing.T) {
307 config := testConfig(map[string]string{"EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9": "true"})
Colin Crossfb6d7812019-01-09 22:17:55 -0800308 if testcase.unbundled {
309 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
310 }
Colin Cross98fd5742019-01-09 23:04:25 -0800311 if testcase.pdk {
312 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
313 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700314 ctx := testContext(bp, nil)
Colin Crossfb6d7812019-01-09 22:17:55 -0800315 run(t, ctx, config)
316
317 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
318 got := javac.Args["bootClasspath"]
319 expected := system
320 if testcase.system == "bootclasspath" {
321 expected = bc
322 }
323 if got != expected {
324 t.Errorf("bootclasspath expected %q != got %q", expected, got)
325 }
326 })
Colin Crossff0daf42019-04-02 16:10:56 -0700327
328 // Test again with PLATFORM_VERSION_CODENAME=REL
329 t.Run("REL", func(t *testing.T) {
330 config := testConfig(nil)
331 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
332 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
333
334 if testcase.unbundled {
335 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
336 }
337 if testcase.pdk {
338 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
339 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700340 ctx := testContext(bp, nil)
Colin Crossff0daf42019-04-02 16:10:56 -0700341 run(t, ctx, config)
342
Colin Cross3047fa22019-04-18 10:56:44 -0700343 checkClasspath(t, ctx)
Colin Crossff0daf42019-04-02 16:10:56 -0700344 })
Colin Crossfb6d7812019-01-09 22:17:55 -0800345 })
346 }
347
348}