blob: a20e15f03a25e68181e8f66fd08c785490b455f3 [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
29var classpathTestcases = []struct {
30 name string
31 unbundled bool
Colin Cross98fd5742019-01-09 23:04:25 -080032 pdk bool
Colin Crossfb6d7812019-01-09 22:17:55 -080033 moduleType string
34 host android.OsClass
35 properties string
36 bootclasspath []string
37 system string
38 classpath []string
39}{
40 {
41 name: "default",
Colin Cross60405e52019-04-18 12:31:22 -070042 bootclasspath: config.DefaultBootclasspathLibraries,
43 system: config.DefaultSystemModules,
44 classpath: config.DefaultLibraries,
Colin Crossfb6d7812019-01-09 22:17:55 -080045 },
46 {
47 name: "blank sdk version",
48 properties: `sdk_version: "",`,
Colin Cross60405e52019-04-18 12:31:22 -070049 bootclasspath: config.DefaultBootclasspathLibraries,
50 system: config.DefaultSystemModules,
51 classpath: config.DefaultLibraries,
Colin Crossfb6d7812019-01-09 22:17:55 -080052 },
53 {
54
Colin Crossff0daf42019-04-02 16:10:56 -070055 name: "sdk v25",
56 properties: `sdk_version: "25",`,
Colin Crossfb6d7812019-01-09 22:17:55 -080057 bootclasspath: []string{`""`},
58 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -070059 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Crossfb6d7812019-01-09 22:17:55 -080060 },
61 {
62
63 name: "current",
64 properties: `sdk_version: "current",`,
65 bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
66 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
67 },
68 {
69
70 name: "system_current",
71 properties: `sdk_version: "system_current",`,
72 bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
73 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
74 },
75 {
76
Colin Crossff0daf42019-04-02 16:10:56 -070077 name: "system_25",
78 properties: `sdk_version: "system_25",`,
Colin Crossfb6d7812019-01-09 22:17:55 -080079 bootclasspath: []string{`""`},
80 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -070081 classpath: []string{"prebuilts/sdk/25/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Crossfb6d7812019-01-09 22:17:55 -080082 },
83 {
84
85 name: "test_current",
86 properties: `sdk_version: "test_current",`,
87 bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
88 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
89 },
90 {
91
92 name: "core_current",
93 properties: `sdk_version: "core_current",`,
94 bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
95 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
96 },
97 {
98
99 name: "nostdlib",
100 properties: `no_standard_libs: true, system_modules: "none"`,
101 system: "none",
102 bootclasspath: []string{`""`},
103 classpath: []string{},
104 },
105 {
106
107 name: "nostdlib system_modules",
108 properties: `no_standard_libs: true, system_modules: "core-platform-api-stubs-system-modules"`,
109 system: "core-platform-api-stubs-system-modules",
110 bootclasspath: []string{`""`},
111 classpath: []string{},
112 },
113 {
114
115 name: "host default",
116 moduleType: "java_library_host",
117 properties: ``,
118 host: android.Host,
119 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
120 classpath: []string{},
121 },
122 {
123 name: "host nostdlib",
124 moduleType: "java_library_host",
125 host: android.Host,
126 properties: `no_standard_libs: true`,
127 classpath: []string{},
128 },
129 {
130
131 name: "host supported default",
132 host: android.Host,
133 properties: `host_supported: true,`,
134 classpath: []string{},
135 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
136 },
137 {
138 name: "host supported nostdlib",
139 host: android.Host,
140 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
141 classpath: []string{},
142 },
143 {
144
Colin Crossff0daf42019-04-02 16:10:56 -0700145 name: "unbundled sdk v25",
Colin Crossfb6d7812019-01-09 22:17:55 -0800146 unbundled: true,
Colin Crossff0daf42019-04-02 16:10:56 -0700147 properties: `sdk_version: "25",`,
Colin Crossfb6d7812019-01-09 22:17:55 -0800148 bootclasspath: []string{`""`},
149 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -0700150 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Crossfb6d7812019-01-09 22:17:55 -0800151 },
152 {
153
154 name: "unbundled current",
155 unbundled: true,
156 properties: `sdk_version: "current",`,
157 bootclasspath: []string{`""`},
158 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
159 classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
160 },
Colin Cross98fd5742019-01-09 23:04:25 -0800161
162 {
163 name: "pdk default",
164 pdk: true,
165 bootclasspath: []string{`""`},
166 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -0700167 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Cross98fd5742019-01-09 23:04:25 -0800168 },
169 {
170 name: "pdk current",
171 pdk: true,
172 properties: `sdk_version: "current",`,
173 bootclasspath: []string{`""`},
174 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -0700175 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Cross98fd5742019-01-09 23:04:25 -0800176 },
177 {
Colin Crossff0daf42019-04-02 16:10:56 -0700178 name: "pdk 25",
Colin Cross98fd5742019-01-09 23:04:25 -0800179 pdk: true,
Colin Crossff0daf42019-04-02 16:10:56 -0700180 properties: `sdk_version: "25",`,
Colin Cross98fd5742019-01-09 23:04:25 -0800181 bootclasspath: []string{`""`},
182 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -0700183 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Cross98fd5742019-01-09 23:04:25 -0800184 },
Colin Crossfb6d7812019-01-09 22:17:55 -0800185}
186
187func TestClasspath(t *testing.T) {
188 for _, testcase := range classpathTestcases {
189 t.Run(testcase.name, func(t *testing.T) {
190 moduleType := "java_library"
191 if testcase.moduleType != "" {
192 moduleType = testcase.moduleType
193 }
194
195 bp := moduleType + ` {
196 name: "foo",
197 srcs: ["a.java"],
198 ` + testcase.properties + `
199 }`
200
201 variant := "android_common"
202 if testcase.host == android.Host {
203 variant = android.BuildOs.String() + "_common"
204 }
205
206 convertModulesToPaths := func(cp []string) []string {
207 ret := make([]string, len(cp))
208 for i, e := range cp {
209 ret[i] = moduleToPath(e)
210 }
211 return ret
212 }
213
214 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
215 classpath := convertModulesToPaths(testcase.classpath)
216
217 bc := strings.Join(bootclasspath, ":")
218 if bc != "" {
219 bc = "-bootclasspath " + bc
220 }
221
222 c := strings.Join(classpath, ":")
223 if c != "" {
224 c = "-classpath " + c
225 }
226 system := ""
227 if testcase.system == "none" {
228 system = "--system=none"
229 } else if testcase.system != "" {
230 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
231 }
232
233 t.Run("1.8", func(t *testing.T) {
234 // Test default javac 1.8
235 config := testConfig(nil)
236 if testcase.unbundled {
237 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
238 }
Colin Cross98fd5742019-01-09 23:04:25 -0800239 if testcase.pdk {
240 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
241 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800242 ctx := testContext(config, bp, nil)
243 run(t, ctx, config)
244
245 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
246
247 got := javac.Args["bootClasspath"]
248 if got != bc {
249 t.Errorf("bootclasspath expected %q != got %q", bc, got)
250 }
251
252 got = javac.Args["classpath"]
253 if got != c {
254 t.Errorf("classpath expected %q != got %q", c, got)
255 }
256
257 var deps []string
258 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
259 deps = append(deps, bootclasspath...)
260 }
261 deps = append(deps, classpath...)
262
263 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
264 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
265 }
266 })
267
268 // Test again with javac 1.9
269 t.Run("1.9", func(t *testing.T) {
270 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
271 if testcase.unbundled {
272 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
273 }
Colin Cross98fd5742019-01-09 23:04:25 -0800274 if testcase.pdk {
275 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
276 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800277 ctx := testContext(config, bp, nil)
278 run(t, ctx, config)
279
280 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
281 got := javac.Args["bootClasspath"]
282 expected := system
283 if testcase.system == "bootclasspath" {
284 expected = bc
285 }
286 if got != expected {
287 t.Errorf("bootclasspath expected %q != got %q", expected, got)
288 }
289 })
Colin Crossff0daf42019-04-02 16:10:56 -0700290
291 // Test again with PLATFORM_VERSION_CODENAME=REL
292 t.Run("REL", func(t *testing.T) {
293 config := testConfig(nil)
294 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
295 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
296
297 if testcase.unbundled {
298 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
299 }
300 if testcase.pdk {
301 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
302 }
303 ctx := testContext(config, bp, nil)
304 run(t, ctx, config)
305
306 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
307
308 got := javac.Args["bootClasspath"]
309 if got != bc {
310 t.Errorf("bootclasspath expected %q != got %q", bc, got)
311 }
312
313 got = javac.Args["classpath"]
314 if got != c {
315 t.Errorf("classpath expected %q != got %q", c, got)
316 }
317
318 var deps []string
319 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
320 deps = append(deps, bootclasspath...)
321 }
322 deps = append(deps, classpath...)
323
324 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
325 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
326 }
327 })
Colin Crossfb6d7812019-01-09 22:17:55 -0800328 })
329 }
330
331}