blob: 6e3ada424a55a8d1c44b717e15438785729af066 [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 {
Colin Cross7fdd2b72018-01-02 18:14:25 -080056 if env == nil {
57 env = make(map[string]string)
58 }
59 if env["ANDROID_JAVA8_HOME"] == "" {
60 env["ANDROID_JAVA8_HOME"] = "jdk8"
61 }
Jiyong Park1a5d7b12018-01-15 15:05:10 +090062 config := android.TestArchConfig(buildDir, env)
63 config.ProductVariables.DeviceSystemSdkVersions = &[]string{"14", "15"}
64 return config
Colin Cross527012a2017-11-30 22:56:16 -080065
Colin Cross1369cdb2017-09-29 17:58:17 -070066}
67
Colin Cross527012a2017-11-30 22:56:16 -080068func testContext(config android.Config, bp string,
69 fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070070
Colin Cross4c428df2017-09-15 17:36:05 -070071 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070072 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080073 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Crossa60ead82017-10-02 18:10:21 -070074 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true)))
Colin Cross2acdae82017-09-15 19:44:24 -070075 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070076 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070077 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070078 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080079 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Colin Cross0f37af02017-09-27 17:42:05 -070080 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070081 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080082 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
83 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Dan Willemsencc090972018-02-26 14:33:31 -080084 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(DroiddocTemplateFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070085 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
86 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070087 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080088 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross72bb3632017-07-13 16:23:21 -070089 ctx.Register()
90
Colin Cross4c428df2017-09-15 17:36:05 -070091 extraModules := []string{
92 "core-oj",
93 "core-libart",
94 "framework",
95 "ext",
96 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -070097 "android_stubs_current",
98 "android_system_stubs_current",
99 "android_test_stubs_current",
Colin Cross93e85952017-08-15 13:34:18 -0700100 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -0700101 }
Colin Cross72bb3632017-07-13 16:23:21 -0700102
103 for _, extra := range extraModules {
104 bp += fmt.Sprintf(`
105 java_library {
106 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -0700107 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -0700108 no_standard_libs: true,
Colin Cross1369cdb2017-09-29 17:58:17 -0700109 system_modules: "core-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -0700110 }
111 `, extra)
112 }
113
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800114 bp += `
115 android_app {
116 name: "framework-res",
117 no_framework_libs: true,
118 }
119 `
120
Colin Cross527012a2017-11-30 22:56:16 -0800121 systemModules := []string{
122 "core-system-modules",
123 "android_stubs_current_system_modules",
124 "android_system_stubs_current_system_modules",
125 "android_test_stubs_current_system_modules",
126 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700127
Colin Cross527012a2017-11-30 22:56:16 -0800128 for _, extra := range systemModules {
129 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700130 java_system_modules {
131 name: "%s",
132 }
133 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700134 }
135
Colin Cross824bee32017-11-22 17:27:51 -0800136 mockFS := map[string][]byte{
Nan Zhang27e284d2018-02-09 21:03:53 +0000137 "Android.bp": []byte(bp),
138 "a.java": nil,
139 "b.java": nil,
140 "c.java": nil,
141 "b.kt": nil,
142 "a.jar": nil,
143 "b.jar": nil,
144 "java-res/a": nil,
145 "java-res/b": nil,
146 "java-res2/a": nil,
147 "java-fg/a.java": nil,
148 "java-fg/b.java": nil,
149 "java-fg/c.java": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700150
Colin Cross2ebc4762017-10-20 14:00:31 -0700151 "prebuilts/sdk/14/android.jar": nil,
152 "prebuilts/sdk/14/framework.aidl": nil,
153 "prebuilts/sdk/current/android.jar": nil,
154 "prebuilts/sdk/current/framework.aidl": nil,
Jiyong Park750e5572018-01-31 00:20:13 +0900155 "prebuilts/sdk/current/core.jar": nil,
Colin Cross2ebc4762017-10-20 14:00:31 -0700156 "prebuilts/sdk/system_current/android.jar": nil,
157 "prebuilts/sdk/system_current/framework.aidl": nil,
Sundong Ahn0926fae2017-10-17 16:34:51 +0900158 "prebuilts/sdk/system_14/android.jar": nil,
159 "prebuilts/sdk/system_14/framework.aidl": nil,
Colin Cross2ebc4762017-10-20 14:00:31 -0700160 "prebuilts/sdk/test_current/android.jar": nil,
161 "prebuilts/sdk/test_current/framework.aidl": nil,
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800162
163 // For framework-res, which is an implicit dependency for framework
164 "AndroidManifest.xml": nil,
165 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800166
167 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800168
Colin Cross66dbc0b2017-12-28 12:23:20 -0800169 "build/make/core/proguard.flags": nil,
170 "build/make/core/proguard_basic_keeps.flags": nil,
171
Colin Cross7fdd2b72018-01-02 18:14:25 -0800172 "jdk8/jre/lib/jce.jar": nil,
173 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800174
175 "bar-doc/a.java": nil,
176 "bar-doc/b.java": nil,
177 "bar-doc/known_oj_tags.txt": nil,
178 "external/doclava/templates-sdk": nil,
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000179
180 "external/kotlinc/jarjar-rules.txt": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800181 }
182
183 for k, v := range fs {
184 mockFS[k] = v
185 }
186
187 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700188
Colin Cross527012a2017-11-30 22:56:16 -0800189 return ctx
190}
191
192func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800193 t.Helper()
Jeff Gastond3e141d2017-08-08 17:46:01 -0700194 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800195 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700196 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800197 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800198}
199
200func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800201 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800202 config := testConfig(nil)
203 ctx := testContext(config, bp, nil)
204 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700205
206 return ctx
207}
208
Colin Cross2acdae82017-09-15 19:44:24 -0700209func moduleToPath(name string) string {
210 switch {
211 case name == `""`:
212 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700213 case strings.HasSuffix(name, ".jar"):
214 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700215 case name == "android_stubs_current" || name == "android_system_stubs_current" ||
216 name == "android_test_stubs_current":
Colin Cross1ee23172017-10-18 14:44:18 -0700217 return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700218 default:
219 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700220 }
221}
222
Colin Cross72bb3632017-07-13 16:23:21 -0700223func TestSimple(t *testing.T) {
224 ctx := testJava(t, `
225 java_library {
226 name: "foo",
227 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700228 libs: ["bar"],
229 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700230 }
231
232 java_library {
233 name: "bar",
234 srcs: ["b.java"],
235 }
236
237 java_library {
238 name: "baz",
239 srcs: ["c.java"],
240 }
Colin Crossd5934c82017-10-02 13:55:26 -0700241 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700242
Colin Cross4c428df2017-09-15 17:36:05 -0700243 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700244 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700245
246 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
247 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
248 }
249
Colin Cross1ee23172017-10-18 14:44:18 -0700250 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700251 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
252 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700253
Nan Zhanged19fc32017-10-19 13:06:22 -0700254 if !strings.Contains(javac.Args["classpath"], barTurbine) {
255 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700256 }
257
Nan Zhanged19fc32017-10-19 13:06:22 -0700258 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
259 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700260 }
261
262 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
263 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700264 }
265}
266
Colin Crossd5934c82017-10-02 13:55:26 -0700267func TestArchSpecific(t *testing.T) {
268 ctx := testJava(t, `
269 java_library {
270 name: "foo",
271 srcs: ["a.java"],
272 target: {
273 android: {
274 srcs: ["b.java"],
275 },
276 },
277 }
278 `)
279
280 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
281 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
282 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
283 }
284}
285
Colin Cross6b4a32d2017-12-05 13:42:45 -0800286func TestBinary(t *testing.T) {
287 ctx := testJava(t, `
288 java_library_host {
289 name: "foo",
290 srcs: ["a.java"],
291 }
292
293 java_binary_host {
294 name: "bar",
295 srcs: ["b.java"],
296 static_libs: ["foo"],
297 }
298 `)
299
300 buildOS := android.BuildOs.String()
301
302 bar := ctx.ModuleForTests("bar", buildOS+"_common")
303 barJar := bar.Output("bar.jar").Output.String()
304 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
305 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
306
307 // Test that the install binary wrapper depends on the installed jar file
308 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
309 t.Errorf("expected binary wrapper implicits [%q], got %v",
310 barJar, barWrapperDeps)
311 }
312
313}
314
Colin Cross2acdae82017-09-15 19:44:24 -0700315var classpathTestcases = []struct {
316 name string
Colin Cross965714f2017-10-03 20:57:01 -0700317 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700318 host android.OsClass
319 properties string
320 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700321 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700322 classpath []string
323}{
324 {
325 name: "default",
326 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700327 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700328 classpath: []string{"ext", "framework", "okhttp"},
329 },
330 {
331 name: "blank sdk version",
332 properties: `sdk_version: "",`,
333 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700334 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700335 classpath: []string{"ext", "framework", "okhttp"},
336 },
337 {
Colin Cross4c428df2017-09-15 17:36:05 -0700338
Colin Cross2acdae82017-09-15 19:44:24 -0700339 name: "sdk v14",
340 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700341 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700342 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossfc3674a2017-09-18 17:41:52 -0700343 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700344 },
345 {
Colin Cross72bb3632017-07-13 16:23:21 -0700346
Colin Cross2acdae82017-09-15 19:44:24 -0700347 name: "current",
348 properties: `sdk_version: "current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700349 bootclasspath: []string{`""`},
350 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
351 classpath: []string{"prebuilts/sdk/current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700352 },
353 {
Colin Cross72bb3632017-07-13 16:23:21 -0700354
Colin Cross2acdae82017-09-15 19:44:24 -0700355 name: "system_current",
356 properties: `sdk_version: "system_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700357 bootclasspath: []string{`""`},
358 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
359 classpath: []string{"prebuilts/sdk/system_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700360 },
361 {
Colin Cross72bb3632017-07-13 16:23:21 -0700362
Sundong Ahn0926fae2017-10-17 16:34:51 +0900363 name: "system_14",
364 properties: `sdk_version: "system_14",`,
365 bootclasspath: []string{`""`},
366 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
367 classpath: []string{"prebuilts/sdk/system_14/android.jar"},
368 },
369 {
370
Colin Cross2acdae82017-09-15 19:44:24 -0700371 name: "test_current",
372 properties: `sdk_version: "test_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700373 bootclasspath: []string{`""`},
374 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
375 classpath: []string{"prebuilts/sdk/test_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700376 },
377 {
Colin Cross72bb3632017-07-13 16:23:21 -0700378
Jiyong Park750e5572018-01-31 00:20:13 +0900379 name: "core_current",
380 properties: `sdk_version: "core_current",`,
381 bootclasspath: []string{`""`},
382 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
383 classpath: []string{"prebuilts/sdk/current/core.jar"},
384 },
385 {
386
Colin Cross2acdae82017-09-15 19:44:24 -0700387 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700388 properties: `no_standard_libs: true, system_modules: "none"`,
389 system: "none",
390 bootclasspath: []string{`""`},
391 classpath: []string{},
392 },
393 {
394
395 name: "nostdlib system_modules",
396 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
397 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700398 bootclasspath: []string{`""`},
399 classpath: []string{},
400 },
401 {
Colin Cross72bb3632017-07-13 16:23:21 -0700402
Colin Cross7fdd2b72018-01-02 18:14:25 -0800403 name: "host default",
404 moduleType: "java_library_host",
405 properties: ``,
406 host: android.Host,
407 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
408 classpath: []string{},
Colin Cross2acdae82017-09-15 19:44:24 -0700409 },
410 {
411 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700412 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700413 host: android.Host,
414 properties: `no_standard_libs: true`,
415 classpath: []string{},
416 },
Colin Cross965714f2017-10-03 20:57:01 -0700417 {
418
Colin Cross7fdd2b72018-01-02 18:14:25 -0800419 name: "host supported default",
420 host: android.Host,
421 properties: `host_supported: true,`,
422 classpath: []string{},
423 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
Colin Cross965714f2017-10-03 20:57:01 -0700424 },
425 {
426 name: "host supported nostdlib",
427 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700428 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700429 classpath: []string{},
430 },
Colin Cross2acdae82017-09-15 19:44:24 -0700431}
Colin Cross72bb3632017-07-13 16:23:21 -0700432
Colin Cross2acdae82017-09-15 19:44:24 -0700433func TestClasspath(t *testing.T) {
434 for _, testcase := range classpathTestcases {
435 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700436 moduleType := "java_library"
437 if testcase.moduleType != "" {
438 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700439 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700440
441 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700442 name: "foo",
443 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700444 ` + testcase.properties + `
445 }`
446
447 variant := "android_common"
448 if testcase.host == android.Host {
449 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700450 }
Colin Cross72bb3632017-07-13 16:23:21 -0700451
Colin Cross2acdae82017-09-15 19:44:24 -0700452 convertModulesToPaths := func(cp []string) []string {
453 ret := make([]string, len(cp))
454 for i, e := range cp {
455 ret[i] = moduleToPath(e)
456 }
457 return ret
458 }
Colin Cross72bb3632017-07-13 16:23:21 -0700459
Colin Cross2acdae82017-09-15 19:44:24 -0700460 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
461 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700462
Colin Cross2acdae82017-09-15 19:44:24 -0700463 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700464 if bc != "" {
465 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700466 }
Colin Cross72bb3632017-07-13 16:23:21 -0700467
Colin Cross2acdae82017-09-15 19:44:24 -0700468 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700469 if c != "" {
470 c = "-classpath " + c
471 }
472 system := ""
473 if testcase.system == "none" {
474 system = "--system=none"
475 } else if testcase.system != "" {
476 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700477 }
478
Colin Cross1369cdb2017-09-29 17:58:17 -0700479 t.Run("1.8", func(t *testing.T) {
480 // Test default javac 1.8
481 ctx := testJava(t, bp)
Colin Cross2acdae82017-09-15 19:44:24 -0700482
Colin Cross1369cdb2017-09-29 17:58:17 -0700483 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
484
485 got := javac.Args["bootClasspath"]
486 if got != bc {
487 t.Errorf("bootclasspath expected %q != got %q", bc, got)
488 }
489
490 got = javac.Args["classpath"]
491 if got != c {
492 t.Errorf("classpath expected %q != got %q", c, got)
493 }
494
495 var deps []string
496 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
497 deps = append(deps, bootclasspath...)
498 }
499 deps = append(deps, classpath...)
500
501 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
502 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
503 }
504 })
505
506 // Test again with javac 1.9
507 t.Run("1.9", func(t *testing.T) {
Colin Cross527012a2017-11-30 22:56:16 -0800508 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
509 ctx := testContext(config, bp, nil)
510 run(t, ctx, config)
Colin Cross1369cdb2017-09-29 17:58:17 -0700511
512 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
513 got := javac.Args["bootClasspath"]
514 expected := system
515 if testcase.system == "bootclasspath" {
516 expected = bc
517 }
518 if got != expected {
519 t.Errorf("bootclasspath expected %q != got %q", expected, got)
520 }
521 })
Colin Cross2acdae82017-09-15 19:44:24 -0700522 })
Colin Cross72bb3632017-07-13 16:23:21 -0700523 }
524
Colin Cross72bb3632017-07-13 16:23:21 -0700525}
526
527func TestPrebuilts(t *testing.T) {
528 ctx := testJava(t, `
529 java_library {
530 name: "foo",
531 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700532 libs: ["bar"],
533 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700534 }
535
Colin Cross74d73e22017-08-02 11:05:49 -0700536 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700537 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700538 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700539 }
540
Colin Cross74d73e22017-08-02 11:05:49 -0700541 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700542 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700543 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700544 }
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 Cross72bb3632017-07-13 16:23:21 -0700549
550 bar := "a.jar"
551 if !strings.Contains(javac.Args["classpath"], bar) {
552 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
553 }
554
Colin Cross0a6e0072017-08-30 14:24:55 -0700555 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
556 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700557 }
558}
559
Colin Cross89536d42017-07-07 14:35:50 -0700560func TestDefaults(t *testing.T) {
561 ctx := testJava(t, `
562 java_defaults {
563 name: "defaults",
564 srcs: ["a.java"],
565 libs: ["bar"],
566 static_libs: ["baz"],
567 }
568
569 java_library {
570 name: "foo",
571 defaults: ["defaults"],
572 }
573
574 java_library {
575 name: "bar",
576 srcs: ["b.java"],
577 }
578
579 java_library {
580 name: "baz",
581 srcs: ["c.java"],
582 }
583 `)
584
Colin Cross4c428df2017-09-15 17:36:05 -0700585 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700586 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700587
588 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
589 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
590 }
591
Nan Zhanged19fc32017-10-19 13:06:22 -0700592 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
593 if !strings.Contains(javac.Args["classpath"], barTurbine) {
594 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700595 }
596
Colin Cross1ee23172017-10-18 14:44:18 -0700597 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700598 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
599 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700600 }
601}
602
Colin Cross0f37af02017-09-27 17:42:05 -0700603func TestResources(t *testing.T) {
604 var table = []struct {
605 name string
606 prop string
607 extra string
608 args string
609 }{
610 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700611 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700612 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800613 prop: `java_resource_dirs: ["java-res"]`,
614 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700615 },
616 {
617 // Test that a module with java_resources includes the files
618 name: "resource files",
Colin Cross824bee32017-11-22 17:27:51 -0800619 prop: `java_resources: ["java-res/a", "java-res/b"]`,
620 args: "-C . -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700621 },
622 {
623 // Test that a module with a filegroup in java_resources includes the files with the
624 // path prefix
625 name: "resource filegroup",
626 prop: `java_resources: [":foo-res"]`,
627 extra: `
628 filegroup {
629 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800630 path: "java-res",
631 srcs: ["java-res/a", "java-res/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700632 }`,
Colin Cross824bee32017-11-22 17:27:51 -0800633 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700634 },
635 {
636 // Test that a module with "include_srcs: true" includes its source files in the resources jar
637 name: "include sources",
638 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700639 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700640 },
641 }
642
643 for _, test := range table {
644 t.Run(test.name, func(t *testing.T) {
645 ctx := testJava(t, `
646 java_library {
647 name: "foo",
648 srcs: [
649 "a.java",
650 "b.java",
651 "c.java",
652 ],
653 `+test.prop+`,
654 }
655 `+test.extra)
656
Colin Cross1ee23172017-10-18 14:44:18 -0700657 foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
658 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700659
660 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
661 t.Errorf("foo combined jars %v does not contain %q",
662 foo.Inputs.Strings(), fooRes.Output.String())
663 }
664
Colin Crossaf9c55b2017-10-03 14:50:08 -0700665 if fooRes.Args["jarArgs"] != test.args {
666 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700667 fooRes.Args["jarArgs"], test.args)
668 }
669 })
670 }
671}
672
Colin Cross0532fb02017-10-02 16:57:40 -0700673func TestExcludeResources(t *testing.T) {
674 ctx := testJava(t, `
675 java_library {
676 name: "foo",
677 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800678 java_resource_dirs: ["java-res", "java-res2"],
679 exclude_java_resource_dirs: ["java-res2"],
Colin Cross0532fb02017-10-02 16:57:40 -0700680 }
681
682 java_library {
683 name: "bar",
684 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800685 java_resources: ["java-res/*"],
686 exclude_java_resources: ["java-res/b"],
Colin Cross0532fb02017-10-02 16:57:40 -0700687 }
688 `)
689
Colin Cross1ee23172017-10-18 14:44:18 -0700690 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700691
Colin Cross824bee32017-11-22 17:27:51 -0800692 expected := "-C java-res -f java-res/a -f java-res/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700693 if fooRes.Args["jarArgs"] != expected {
694 t.Errorf("foo resource jar args %q is not %q",
695 fooRes.Args["jarArgs"], expected)
696
697 }
698
Colin Cross1ee23172017-10-18 14:44:18 -0700699 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700700
Colin Cross824bee32017-11-22 17:27:51 -0800701 expected = "-C . -f java-res/a"
Colin Cross0532fb02017-10-02 16:57:40 -0700702 if barRes.Args["jarArgs"] != expected {
703 t.Errorf("bar resource jar args %q is not %q",
704 barRes.Args["jarArgs"], expected)
705
706 }
707}
708
Colin Cross54190b32017-10-09 15:34:10 -0700709func TestGeneratedSources(t *testing.T) {
710 ctx := testJava(t, `
711 java_library {
712 name: "foo",
713 srcs: [
714 "a*.java",
715 ":gen",
716 "b*.java",
717 ],
718 }
719
720 genrule {
721 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800722 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700723 out: ["gen.java"],
724 }
725 `)
726
727 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
728 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
729
Colin Cross15e86d92017-10-20 15:07:08 -0700730 if filepath.Base(genrule.Output.String()) != "gen.java" {
731 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700732 }
733
734 if len(javac.Inputs) != 3 ||
735 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700736 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700737 javac.Inputs[2].String() != "b.java" {
738 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
739 }
740}
741
Colin Cross93e85952017-08-15 13:34:18 -0700742func TestKotlin(t *testing.T) {
743 ctx := testJava(t, `
744 java_library {
745 name: "foo",
Colin Cross9bc43432017-12-15 20:20:39 -0800746 srcs: ["a.java", "b.kt"],
Colin Cross93e85952017-08-15 13:34:18 -0700747 }
Colin Cross715d7112017-10-25 20:32:18 -0700748
749 java_library {
750 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800751 srcs: ["b.kt"],
752 libs: ["foo"],
753 static_libs: ["baz"],
754 }
755
756 java_library {
757 name: "baz",
758 srcs: ["c.java"],
Colin Cross715d7112017-10-25 20:32:18 -0700759 }
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000760
761 java_library {
762 name: "blorg",
763 renamed_kotlin_stdlib: true,
764 srcs: ["b.kt"],
765 }
Colin Cross93e85952017-08-15 13:34:18 -0700766 `)
767
Colin Cross9bc43432017-12-15 20:20:39 -0800768 fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
769 fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
770 fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700771
Colin Cross9bc43432017-12-15 20:20:39 -0800772 if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
773 fooKotlinc.Inputs[1].String() != "b.kt" {
774 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700775 }
776
Colin Cross9bc43432017-12-15 20:20:39 -0800777 if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
778 t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700779 }
780
Colin Cross9bc43432017-12-15 20:20:39 -0800781 if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
Colin Cross93e85952017-08-15 13:34:18 -0700782 t.Errorf("foo classpath %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800783 fooJavac.Args["classpath"], fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700784 }
785
Colin Cross9bc43432017-12-15 20:20:39 -0800786 if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
Colin Cross93e85952017-08-15 13:34:18 -0700787 t.Errorf("foo jar inputs %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800788 fooJar.Inputs.Strings(), fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700789 }
Colin Cross715d7112017-10-25 20:32:18 -0700790
Colin Cross9bc43432017-12-15 20:20:39 -0800791 fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
792 bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
793 barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
Colin Cross715d7112017-10-25 20:32:18 -0700794
Colin Cross9bc43432017-12-15 20:20:39 -0800795 if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
796 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
797 }
798
799 if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
800 t.Errorf(`expected %q in bar implicits %v`,
801 fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
802 }
803
804 if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
805 t.Errorf(`expected %q in bar implicits %v`,
806 bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
Colin Cross715d7112017-10-25 20:32:18 -0700807 }
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000808
809 blorgRenamedJar := ctx.ModuleForTests("blorg", "android_common").Output("kotlin-renamed/blorg.jar")
810 if blorgRenamedJar.Implicit.String() != "external/kotlinc/jarjar-rules.txt" {
811 t.Errorf(`expected external/kotlinc/jarjar-rules.txt in blorg implicit %q`,
812 blorgRenamedJar.Implicit.String())
813 }
Colin Cross93e85952017-08-15 13:34:18 -0700814}
815
Nan Zhang61eaedb2017-11-02 13:28:15 -0700816func TestTurbine(t *testing.T) {
817 ctx := testJava(t, `
818 java_library {
819 name: "foo",
820 srcs: ["a.java"],
821 }
822
823 java_library {
824 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800825 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700826 static_libs: ["foo"],
827 }
828
829 java_library {
830 name: "baz",
831 srcs: ["c.java"],
832 libs: ["bar"],
833 sdk_version: "14",
834 }
835 `)
836
837 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
838 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
839 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
840 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
841 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
842
843 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
844 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
845 }
846
847 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
848 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
849 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
850 }
851 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
852 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
853 }
854 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
855 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
856 }
857 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/android.jar") {
858 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
859 "prebuilts/sdk/14/android.jar")
860 }
861}
862
863func TestSharding(t *testing.T) {
864 ctx := testJava(t, `
865 java_library {
866 name: "bar",
867 srcs: ["a.java","b.java","c.java"],
868 javac_shard_size: 1
869 }
870 `)
871
872 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
873 for i := 0; i < 3; i++ {
874 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
875 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
876 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
877 }
878 }
879}
880
Nan Zhang581fd212018-01-10 16:06:12 -0800881func TestDroiddoc(t *testing.T) {
882 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800883 droiddoc_template {
884 name: "droiddoc-templates-sdk",
885 path: ".",
886 }
Nan Zhang581fd212018-01-10 16:06:12 -0800887 droiddoc {
888 name: "bar-doc",
889 srcs: [
890 "bar-doc/*.java",
891 ],
892 exclude_srcs: [
893 "bar-doc/b.java"
894 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800895 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800896 hdf: [
897 "android.whichdoc offline",
898 ],
899 knowntags: [
900 "bar-doc/known_oj_tags.txt",
901 ],
902 proofread_file: "libcore-proofread.txt",
903 todo_file: "libcore-docs-todo.html",
904 args: "-offlinemode -title \"libcore\"",
905 }
906 `)
907
Nan Zhang3bb14202018-02-28 10:14:25 -0800908 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
909 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800910 if stubsJar != barDoc.Output.String() {
911 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
912 }
913}
914
Colin Cross54250902017-12-05 09:28:08 -0800915func TestJarGenrules(t *testing.T) {
916 ctx := testJava(t, `
917 java_library {
918 name: "foo",
919 srcs: ["a.java"],
920 }
921
922 java_genrule {
923 name: "jargen",
924 tool_files: ["b.java"],
925 cmd: "$(location b.java) $(in) $(out)",
926 out: ["jargen.jar"],
927 srcs: [":foo"],
928 }
929
930 java_library {
931 name: "bar",
932 static_libs: ["jargen"],
933 srcs: ["c.java"],
934 }
935
936 java_library {
937 name: "baz",
938 libs: ["jargen"],
939 srcs: ["c.java"],
940 }
941 `)
942
943 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
944 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
945 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
946 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
947 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
948
949 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
950 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
951 }
952
953 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
954 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
955 }
956
957 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
958 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
959 }
960
961 if len(barCombined.Inputs) != 2 ||
962 barCombined.Inputs[0].String() != bar.Output.String() ||
963 barCombined.Inputs[1].String() != jargen.Output.String() {
964 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
965 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
966 }
967}
968
Nan Zhang27e284d2018-02-09 21:03:53 +0000969func TestExcludeFileGroupInSrcs(t *testing.T) {
970 ctx := testJava(t, `
971 java_library {
972 name: "foo",
973 srcs: ["a.java", ":foo-srcs"],
974 exclude_srcs: ["a.java", ":foo-excludes"],
975 }
976
977 filegroup {
978 name: "foo-srcs",
979 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
980 }
981
982 filegroup {
983 name: "foo-excludes",
984 srcs: ["java-fg/a.java", "java-fg/b.java"],
985 }
986 `)
987
988 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
989
990 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
991 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
992 }
993}