blob: 84fe9030f2ba1f2ede75f3007be30434c2c7a447 [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 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 (
18 "android/soong/android"
Colin Cross0f37af02017-09-27 17:42:05 -070019 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070020 "fmt"
21 "io/ioutil"
22 "os"
23 "path/filepath"
Colin Cross227d4362017-08-30 14:14:52 -070024 "reflect"
Nan Zhang61eaedb2017-11-02 13:28:15 -070025 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070026 "strings"
27 "testing"
28)
29
30var buildDir string
31
32func setUp() {
33 var err error
34 buildDir, err = ioutil.TempDir("", "soong_java_test")
35 if err != nil {
36 panic(err)
37 }
38}
39
40func tearDown() {
41 os.RemoveAll(buildDir)
42}
43
44func TestMain(m *testing.M) {
45 run := func() int {
46 setUp()
47 defer tearDown()
48
49 return m.Run()
50 }
51
52 os.Exit(run())
53}
Colin Cross527012a2017-11-30 22:56:16 -080054
55func testConfig(env map[string]string) android.Config {
56 return android.TestArchConfig(buildDir, env)
57
Colin Cross1369cdb2017-09-29 17:58:17 -070058}
59
Colin Cross527012a2017-11-30 22:56:16 -080060func testContext(config android.Config, bp string,
61 fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070062
Colin Cross4c428df2017-09-15 17:36:05 -070063 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070064 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080065 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Crossa60ead82017-10-02 18:10:21 -070066 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true)))
Colin Cross2acdae82017-09-15 19:44:24 -070067 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070068 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070069 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070070 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080071 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Colin Cross0f37af02017-09-27 17:42:05 -070072 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070073 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070074 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
75 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070076 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080077 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross72bb3632017-07-13 16:23:21 -070078 ctx.Register()
79
Colin Cross4c428df2017-09-15 17:36:05 -070080 extraModules := []string{
81 "core-oj",
82 "core-libart",
83 "framework",
84 "ext",
85 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -070086 "android_stubs_current",
87 "android_system_stubs_current",
88 "android_test_stubs_current",
Colin Cross93e85952017-08-15 13:34:18 -070089 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -070090 }
Colin Cross72bb3632017-07-13 16:23:21 -070091
92 for _, extra := range extraModules {
93 bp += fmt.Sprintf(`
94 java_library {
95 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -070096 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -070097 no_standard_libs: true,
Colin Cross1369cdb2017-09-29 17:58:17 -070098 system_modules: "core-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -070099 }
100 `, extra)
101 }
102
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800103 bp += `
104 android_app {
105 name: "framework-res",
106 no_framework_libs: true,
107 }
108 `
109
Colin Cross527012a2017-11-30 22:56:16 -0800110 systemModules := []string{
111 "core-system-modules",
112 "android_stubs_current_system_modules",
113 "android_system_stubs_current_system_modules",
114 "android_test_stubs_current_system_modules",
115 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700116
Colin Cross527012a2017-11-30 22:56:16 -0800117 for _, extra := range systemModules {
118 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700119 java_system_modules {
120 name: "%s",
121 }
122 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700123 }
124
Colin Cross824bee32017-11-22 17:27:51 -0800125 mockFS := map[string][]byte{
126 "Android.bp": []byte(bp),
127 "a.java": nil,
128 "b.java": nil,
129 "c.java": nil,
130 "b.kt": nil,
131 "a.jar": nil,
132 "b.jar": nil,
133 "java-res/a": nil,
134 "java-res/b": nil,
135 "java-res2/a": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700136
Colin Cross2ebc4762017-10-20 14:00:31 -0700137 "prebuilts/sdk/14/android.jar": nil,
138 "prebuilts/sdk/14/framework.aidl": nil,
139 "prebuilts/sdk/current/android.jar": nil,
140 "prebuilts/sdk/current/framework.aidl": nil,
141 "prebuilts/sdk/system_current/android.jar": nil,
142 "prebuilts/sdk/system_current/framework.aidl": nil,
Sundong Ahn0926fae2017-10-17 16:34:51 +0900143 "prebuilts/sdk/system_14/android.jar": nil,
144 "prebuilts/sdk/system_14/framework.aidl": nil,
Colin Cross2ebc4762017-10-20 14:00:31 -0700145 "prebuilts/sdk/test_current/android.jar": nil,
146 "prebuilts/sdk/test_current/framework.aidl": nil,
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800147
148 // For framework-res, which is an implicit dependency for framework
149 "AndroidManifest.xml": nil,
150 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800151
152 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800153 }
154
155 for k, v := range fs {
156 mockFS[k] = v
157 }
158
159 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700160
Colin Cross527012a2017-11-30 22:56:16 -0800161 return ctx
162}
163
164func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800165 t.Helper()
Jeff Gastond3e141d2017-08-08 17:46:01 -0700166 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Colin Cross72bb3632017-07-13 16:23:21 -0700167 fail(t, errs)
168 _, errs = ctx.PrepareBuildActions(config)
169 fail(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800170}
171
172func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800173 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800174 config := testConfig(nil)
175 ctx := testContext(config, bp, nil)
176 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700177
178 return ctx
179}
180
Colin Cross2acdae82017-09-15 19:44:24 -0700181func moduleToPath(name string) string {
182 switch {
183 case name == `""`:
184 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700185 case strings.HasSuffix(name, ".jar"):
186 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700187 case name == "android_stubs_current" || name == "android_system_stubs_current" ||
188 name == "android_test_stubs_current":
Colin Cross1ee23172017-10-18 14:44:18 -0700189 return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700190 default:
191 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700192 }
193}
194
Colin Cross72bb3632017-07-13 16:23:21 -0700195func TestSimple(t *testing.T) {
196 ctx := testJava(t, `
197 java_library {
198 name: "foo",
199 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700200 libs: ["bar"],
201 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700202 }
203
204 java_library {
205 name: "bar",
206 srcs: ["b.java"],
207 }
208
209 java_library {
210 name: "baz",
211 srcs: ["c.java"],
212 }
Colin Crossd5934c82017-10-02 13:55:26 -0700213 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700214
Colin Cross4c428df2017-09-15 17:36:05 -0700215 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700216 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700217
218 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
219 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
220 }
221
Colin Cross1ee23172017-10-18 14:44:18 -0700222 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700223 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
224 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700225
Nan Zhanged19fc32017-10-19 13:06:22 -0700226 if !strings.Contains(javac.Args["classpath"], barTurbine) {
227 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700228 }
229
Nan Zhanged19fc32017-10-19 13:06:22 -0700230 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
231 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700232 }
233
234 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
235 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700236 }
237}
238
Colin Crossd5934c82017-10-02 13:55:26 -0700239func TestArchSpecific(t *testing.T) {
240 ctx := testJava(t, `
241 java_library {
242 name: "foo",
243 srcs: ["a.java"],
244 target: {
245 android: {
246 srcs: ["b.java"],
247 },
248 },
249 }
250 `)
251
252 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
253 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
254 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
255 }
256}
257
Colin Cross6b4a32d2017-12-05 13:42:45 -0800258func TestBinary(t *testing.T) {
259 ctx := testJava(t, `
260 java_library_host {
261 name: "foo",
262 srcs: ["a.java"],
263 }
264
265 java_binary_host {
266 name: "bar",
267 srcs: ["b.java"],
268 static_libs: ["foo"],
269 }
270 `)
271
272 buildOS := android.BuildOs.String()
273
274 bar := ctx.ModuleForTests("bar", buildOS+"_common")
275 barJar := bar.Output("bar.jar").Output.String()
276 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
277 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
278
279 // Test that the install binary wrapper depends on the installed jar file
280 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
281 t.Errorf("expected binary wrapper implicits [%q], got %v",
282 barJar, barWrapperDeps)
283 }
284
285}
286
Colin Cross2acdae82017-09-15 19:44:24 -0700287var classpathTestcases = []struct {
288 name string
Colin Cross965714f2017-10-03 20:57:01 -0700289 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700290 host android.OsClass
291 properties string
292 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700293 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700294 classpath []string
295}{
296 {
297 name: "default",
298 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700299 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700300 classpath: []string{"ext", "framework", "okhttp"},
301 },
302 {
303 name: "blank sdk version",
304 properties: `sdk_version: "",`,
305 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700306 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700307 classpath: []string{"ext", "framework", "okhttp"},
308 },
309 {
Colin Cross4c428df2017-09-15 17:36:05 -0700310
Colin Cross2acdae82017-09-15 19:44:24 -0700311 name: "sdk v14",
312 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700313 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700314 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossfc3674a2017-09-18 17:41:52 -0700315 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700316 },
317 {
Colin Cross72bb3632017-07-13 16:23:21 -0700318
Colin Cross2acdae82017-09-15 19:44:24 -0700319 name: "current",
320 properties: `sdk_version: "current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700321 bootclasspath: []string{`""`},
322 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
323 classpath: []string{"prebuilts/sdk/current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700324 },
325 {
Colin Cross72bb3632017-07-13 16:23:21 -0700326
Colin Cross2acdae82017-09-15 19:44:24 -0700327 name: "system_current",
328 properties: `sdk_version: "system_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700329 bootclasspath: []string{`""`},
330 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
331 classpath: []string{"prebuilts/sdk/system_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700332 },
333 {
Colin Cross72bb3632017-07-13 16:23:21 -0700334
Sundong Ahn0926fae2017-10-17 16:34:51 +0900335 name: "system_14",
336 properties: `sdk_version: "system_14",`,
337 bootclasspath: []string{`""`},
338 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
339 classpath: []string{"prebuilts/sdk/system_14/android.jar"},
340 },
341 {
342
Colin Cross2acdae82017-09-15 19:44:24 -0700343 name: "test_current",
344 properties: `sdk_version: "test_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700345 bootclasspath: []string{`""`},
346 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
347 classpath: []string{"prebuilts/sdk/test_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700348 },
349 {
Colin Cross72bb3632017-07-13 16:23:21 -0700350
Colin Cross2acdae82017-09-15 19:44:24 -0700351 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700352 properties: `no_standard_libs: true, system_modules: "none"`,
353 system: "none",
354 bootclasspath: []string{`""`},
355 classpath: []string{},
356 },
357 {
358
359 name: "nostdlib system_modules",
360 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
361 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700362 bootclasspath: []string{`""`},
363 classpath: []string{},
364 },
365 {
Colin Cross72bb3632017-07-13 16:23:21 -0700366
Colin Cross2acdae82017-09-15 19:44:24 -0700367 name: "host default",
Colin Cross965714f2017-10-03 20:57:01 -0700368 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700369 properties: ``,
Colin Cross965714f2017-10-03 20:57:01 -0700370 host: android.Host,
Colin Cross2acdae82017-09-15 19:44:24 -0700371 classpath: []string{},
372 },
373 {
374 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700375 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700376 host: android.Host,
377 properties: `no_standard_libs: true`,
378 classpath: []string{},
379 },
Colin Cross965714f2017-10-03 20:57:01 -0700380 {
381
382 name: "host supported default",
383 host: android.Host,
384 properties: `host_supported: true,`,
385 classpath: []string{},
386 },
387 {
388 name: "host supported nostdlib",
389 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700390 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700391 classpath: []string{},
392 },
Colin Cross2acdae82017-09-15 19:44:24 -0700393}
Colin Cross72bb3632017-07-13 16:23:21 -0700394
Colin Cross2acdae82017-09-15 19:44:24 -0700395func TestClasspath(t *testing.T) {
396 for _, testcase := range classpathTestcases {
397 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700398 moduleType := "java_library"
399 if testcase.moduleType != "" {
400 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700401 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700402
403 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700404 name: "foo",
405 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700406 ` + testcase.properties + `
407 }`
408
409 variant := "android_common"
410 if testcase.host == android.Host {
411 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700412 }
Colin Cross72bb3632017-07-13 16:23:21 -0700413
Colin Cross2acdae82017-09-15 19:44:24 -0700414 convertModulesToPaths := func(cp []string) []string {
415 ret := make([]string, len(cp))
416 for i, e := range cp {
417 ret[i] = moduleToPath(e)
418 }
419 return ret
420 }
Colin Cross72bb3632017-07-13 16:23:21 -0700421
Colin Cross2acdae82017-09-15 19:44:24 -0700422 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
423 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700424
Colin Cross2acdae82017-09-15 19:44:24 -0700425 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700426 if bc != "" {
427 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700428 }
Colin Cross72bb3632017-07-13 16:23:21 -0700429
Colin Cross2acdae82017-09-15 19:44:24 -0700430 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700431 if c != "" {
432 c = "-classpath " + c
433 }
434 system := ""
435 if testcase.system == "none" {
436 system = "--system=none"
437 } else if testcase.system != "" {
438 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700439 }
440
Colin Cross1369cdb2017-09-29 17:58:17 -0700441 t.Run("1.8", func(t *testing.T) {
442 // Test default javac 1.8
443 ctx := testJava(t, bp)
Colin Cross2acdae82017-09-15 19:44:24 -0700444
Colin Cross1369cdb2017-09-29 17:58:17 -0700445 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
446
447 got := javac.Args["bootClasspath"]
448 if got != bc {
449 t.Errorf("bootclasspath expected %q != got %q", bc, got)
450 }
451
452 got = javac.Args["classpath"]
453 if got != c {
454 t.Errorf("classpath expected %q != got %q", c, got)
455 }
456
457 var deps []string
458 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
459 deps = append(deps, bootclasspath...)
460 }
461 deps = append(deps, classpath...)
462
463 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
464 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
465 }
466 })
467
468 // Test again with javac 1.9
469 t.Run("1.9", func(t *testing.T) {
Colin Cross527012a2017-11-30 22:56:16 -0800470 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
471 ctx := testContext(config, bp, nil)
472 run(t, ctx, config)
Colin Cross1369cdb2017-09-29 17:58:17 -0700473
474 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
475 got := javac.Args["bootClasspath"]
476 expected := system
477 if testcase.system == "bootclasspath" {
478 expected = bc
479 }
480 if got != expected {
481 t.Errorf("bootclasspath expected %q != got %q", expected, got)
482 }
483 })
Colin Cross2acdae82017-09-15 19:44:24 -0700484 })
Colin Cross72bb3632017-07-13 16:23:21 -0700485 }
486
Colin Cross72bb3632017-07-13 16:23:21 -0700487}
488
489func TestPrebuilts(t *testing.T) {
490 ctx := testJava(t, `
491 java_library {
492 name: "foo",
493 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700494 libs: ["bar"],
495 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700496 }
497
Colin Cross74d73e22017-08-02 11:05:49 -0700498 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700499 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700500 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700501 }
502
Colin Cross74d73e22017-08-02 11:05:49 -0700503 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700504 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700505 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700506 }
507 `)
508
Colin Cross4c428df2017-09-15 17:36:05 -0700509 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700510 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700511
512 bar := "a.jar"
513 if !strings.Contains(javac.Args["classpath"], bar) {
514 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
515 }
516
Colin Cross0a6e0072017-08-30 14:24:55 -0700517 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
518 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700519 }
520}
521
Colin Cross89536d42017-07-07 14:35:50 -0700522func TestDefaults(t *testing.T) {
523 ctx := testJava(t, `
524 java_defaults {
525 name: "defaults",
526 srcs: ["a.java"],
527 libs: ["bar"],
528 static_libs: ["baz"],
529 }
530
531 java_library {
532 name: "foo",
533 defaults: ["defaults"],
534 }
535
536 java_library {
537 name: "bar",
538 srcs: ["b.java"],
539 }
540
541 java_library {
542 name: "baz",
543 srcs: ["c.java"],
544 }
545 `)
546
Colin Cross4c428df2017-09-15 17:36:05 -0700547 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700548 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700549
550 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
551 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
552 }
553
Nan Zhanged19fc32017-10-19 13:06:22 -0700554 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
555 if !strings.Contains(javac.Args["classpath"], barTurbine) {
556 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700557 }
558
Colin Cross1ee23172017-10-18 14:44:18 -0700559 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700560 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
561 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700562 }
563}
564
Colin Cross0f37af02017-09-27 17:42:05 -0700565func TestResources(t *testing.T) {
566 var table = []struct {
567 name string
568 prop string
569 extra string
570 args string
571 }{
572 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700573 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700574 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800575 prop: `java_resource_dirs: ["java-res"]`,
576 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700577 },
578 {
579 // Test that a module with java_resources includes the files
580 name: "resource files",
Colin Cross824bee32017-11-22 17:27:51 -0800581 prop: `java_resources: ["java-res/a", "java-res/b"]`,
582 args: "-C . -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700583 },
584 {
585 // Test that a module with a filegroup in java_resources includes the files with the
586 // path prefix
587 name: "resource filegroup",
588 prop: `java_resources: [":foo-res"]`,
589 extra: `
590 filegroup {
591 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800592 path: "java-res",
593 srcs: ["java-res/a", "java-res/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700594 }`,
Colin Cross824bee32017-11-22 17:27:51 -0800595 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700596 },
597 {
598 // Test that a module with "include_srcs: true" includes its source files in the resources jar
599 name: "include sources",
600 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700601 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700602 },
603 }
604
605 for _, test := range table {
606 t.Run(test.name, func(t *testing.T) {
607 ctx := testJava(t, `
608 java_library {
609 name: "foo",
610 srcs: [
611 "a.java",
612 "b.java",
613 "c.java",
614 ],
615 `+test.prop+`,
616 }
617 `+test.extra)
618
Colin Cross1ee23172017-10-18 14:44:18 -0700619 foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
620 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700621
622 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
623 t.Errorf("foo combined jars %v does not contain %q",
624 foo.Inputs.Strings(), fooRes.Output.String())
625 }
626
Colin Crossaf9c55b2017-10-03 14:50:08 -0700627 if fooRes.Args["jarArgs"] != test.args {
628 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700629 fooRes.Args["jarArgs"], test.args)
630 }
631 })
632 }
633}
634
Colin Cross0532fb02017-10-02 16:57:40 -0700635func TestExcludeResources(t *testing.T) {
636 ctx := testJava(t, `
637 java_library {
638 name: "foo",
639 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800640 java_resource_dirs: ["java-res", "java-res2"],
641 exclude_java_resource_dirs: ["java-res2"],
Colin Cross0532fb02017-10-02 16:57:40 -0700642 }
643
644 java_library {
645 name: "bar",
646 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800647 java_resources: ["java-res/*"],
648 exclude_java_resources: ["java-res/b"],
Colin Cross0532fb02017-10-02 16:57:40 -0700649 }
650 `)
651
Colin Cross1ee23172017-10-18 14:44:18 -0700652 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700653
Colin Cross824bee32017-11-22 17:27:51 -0800654 expected := "-C java-res -f java-res/a -f java-res/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700655 if fooRes.Args["jarArgs"] != expected {
656 t.Errorf("foo resource jar args %q is not %q",
657 fooRes.Args["jarArgs"], expected)
658
659 }
660
Colin Cross1ee23172017-10-18 14:44:18 -0700661 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700662
Colin Cross824bee32017-11-22 17:27:51 -0800663 expected = "-C . -f java-res/a"
Colin Cross0532fb02017-10-02 16:57:40 -0700664 if barRes.Args["jarArgs"] != expected {
665 t.Errorf("bar resource jar args %q is not %q",
666 barRes.Args["jarArgs"], expected)
667
668 }
669}
670
Colin Cross54190b32017-10-09 15:34:10 -0700671func TestGeneratedSources(t *testing.T) {
672 ctx := testJava(t, `
673 java_library {
674 name: "foo",
675 srcs: [
676 "a*.java",
677 ":gen",
678 "b*.java",
679 ],
680 }
681
682 genrule {
683 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800684 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700685 out: ["gen.java"],
686 }
687 `)
688
689 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
690 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
691
Colin Cross15e86d92017-10-20 15:07:08 -0700692 if filepath.Base(genrule.Output.String()) != "gen.java" {
693 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700694 }
695
696 if len(javac.Inputs) != 3 ||
697 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700698 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700699 javac.Inputs[2].String() != "b.java" {
700 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
701 }
702}
703
Colin Cross93e85952017-08-15 13:34:18 -0700704func TestKotlin(t *testing.T) {
705 ctx := testJava(t, `
706 java_library {
707 name: "foo",
Colin Cross9bc43432017-12-15 20:20:39 -0800708 srcs: ["a.java", "b.kt"],
Colin Cross93e85952017-08-15 13:34:18 -0700709 }
Colin Cross715d7112017-10-25 20:32:18 -0700710
711 java_library {
712 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800713 srcs: ["b.kt"],
714 libs: ["foo"],
715 static_libs: ["baz"],
716 }
717
718 java_library {
719 name: "baz",
720 srcs: ["c.java"],
Colin Cross715d7112017-10-25 20:32:18 -0700721 }
Colin Cross93e85952017-08-15 13:34:18 -0700722 `)
723
Colin Cross9bc43432017-12-15 20:20:39 -0800724 fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
725 fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
726 fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700727
Colin Cross9bc43432017-12-15 20:20:39 -0800728 if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
729 fooKotlinc.Inputs[1].String() != "b.kt" {
730 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700731 }
732
Colin Cross9bc43432017-12-15 20:20:39 -0800733 if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
734 t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700735 }
736
Colin Cross9bc43432017-12-15 20:20:39 -0800737 if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
Colin Cross93e85952017-08-15 13:34:18 -0700738 t.Errorf("foo classpath %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800739 fooJavac.Args["classpath"], fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700740 }
741
Colin Cross9bc43432017-12-15 20:20:39 -0800742 if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
Colin Cross93e85952017-08-15 13:34:18 -0700743 t.Errorf("foo jar inputs %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800744 fooJar.Inputs.Strings(), fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700745 }
Colin Cross715d7112017-10-25 20:32:18 -0700746
Colin Cross9bc43432017-12-15 20:20:39 -0800747 fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
748 bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
749 barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
Colin Cross715d7112017-10-25 20:32:18 -0700750
Colin Cross9bc43432017-12-15 20:20:39 -0800751 if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
752 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
753 }
754
755 if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
756 t.Errorf(`expected %q in bar implicits %v`,
757 fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
758 }
759
760 if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
761 t.Errorf(`expected %q in bar implicits %v`,
762 bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
Colin Cross715d7112017-10-25 20:32:18 -0700763 }
Colin Cross93e85952017-08-15 13:34:18 -0700764}
765
Nan Zhang61eaedb2017-11-02 13:28:15 -0700766func TestTurbine(t *testing.T) {
767 ctx := testJava(t, `
768 java_library {
769 name: "foo",
770 srcs: ["a.java"],
771 }
772
773 java_library {
774 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800775 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700776 static_libs: ["foo"],
777 }
778
779 java_library {
780 name: "baz",
781 srcs: ["c.java"],
782 libs: ["bar"],
783 sdk_version: "14",
784 }
785 `)
786
787 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
788 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
789 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
790 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
791 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
792
793 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
794 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
795 }
796
797 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
798 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
799 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
800 }
801 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
802 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
803 }
804 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
805 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
806 }
807 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/android.jar") {
808 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
809 "prebuilts/sdk/14/android.jar")
810 }
811}
812
813func TestSharding(t *testing.T) {
814 ctx := testJava(t, `
815 java_library {
816 name: "bar",
817 srcs: ["a.java","b.java","c.java"],
818 javac_shard_size: 1
819 }
820 `)
821
822 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
823 for i := 0; i < 3; i++ {
824 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
825 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
826 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
827 }
828 }
829}
830
Colin Cross54250902017-12-05 09:28:08 -0800831func TestJarGenrules(t *testing.T) {
832 ctx := testJava(t, `
833 java_library {
834 name: "foo",
835 srcs: ["a.java"],
836 }
837
838 java_genrule {
839 name: "jargen",
840 tool_files: ["b.java"],
841 cmd: "$(location b.java) $(in) $(out)",
842 out: ["jargen.jar"],
843 srcs: [":foo"],
844 }
845
846 java_library {
847 name: "bar",
848 static_libs: ["jargen"],
849 srcs: ["c.java"],
850 }
851
852 java_library {
853 name: "baz",
854 libs: ["jargen"],
855 srcs: ["c.java"],
856 }
857 `)
858
859 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
860 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
861 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
862 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
863 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
864
865 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
866 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
867 }
868
869 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
870 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
871 }
872
873 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
874 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
875 }
876
877 if len(barCombined.Inputs) != 2 ||
878 barCombined.Inputs[0].String() != bar.Output.String() ||
879 barCombined.Inputs[1].String() != jargen.Output.String() {
880 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
881 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
882 }
883}
884
Colin Cross72bb3632017-07-13 16:23:21 -0700885func fail(t *testing.T, errs []error) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800886 t.Helper()
Colin Cross72bb3632017-07-13 16:23:21 -0700887 if len(errs) > 0 {
888 for _, err := range errs {
889 t.Error(err)
890 }
891 t.FailNow()
892 }
893}