blob: f61f4bb20d4f192b8a5719a18cead15f4d3eca16 [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 (
Colin Cross72bb3632017-07-13 16:23:21 -070018 "io/ioutil"
19 "os"
20 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070021 "reflect"
Nan Zhang61eaedb2017-11-02 13:28:15 -070022 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070023 "strings"
24 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070025
Jeongik Cha28df2572019-11-11 10:46:36 +090026 "github.com/google/blueprint/proptools"
27
Colin Crossa4f08812018-10-02 22:03:40 -070028 "android/soong/android"
29 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080030 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070031 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070032)
33
34var buildDir string
35
36func setUp() {
37 var err error
38 buildDir, err = ioutil.TempDir("", "soong_java_test")
39 if err != nil {
40 panic(err)
41 }
42}
43
44func tearDown() {
45 os.RemoveAll(buildDir)
46}
47
48func TestMain(m *testing.M) {
49 run := func() int {
50 setUp()
51 defer tearDown()
52
53 return m.Run()
54 }
55
56 os.Exit(run())
57}
Colin Cross527012a2017-11-30 22:56:16 -080058
Colin Cross98be1bb2019-12-13 20:41:13 -080059func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000060 bp += dexpreopt.BpToolModulesForTest()
61
Martin Stjernholm75a48d82020-01-10 20:32:59 +000062 config := TestConfig(buildDir, env, bp, fs)
63
64 // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that
65 // it doesn't create a real one, which would fail.
66 _ = dexpreopt.GlobalSoongConfigForTests(config)
67
68 return config
Colin Cross1369cdb2017-09-29 17:58:17 -070069}
70
Colin Cross98be1bb2019-12-13 20:41:13 -080071func testContext() *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070072
Colin Cross4c428df2017-09-15 17:36:05 -070073 ctx := android.NewTestArchContext()
Paul Duffinf9b1da02019-12-18 19:51:55 +000074 RegisterJavaBuildComponents(ctx)
75 RegisterAppBuildComponents(ctx)
76 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000077 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000078 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080079 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080080 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
81 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000082 RegisterDocsBuildComponents(ctx)
83 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000084 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070085 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffina48f7582019-12-19 11:25:19 +000086
87 RegisterPrebuiltApisBuildComponents(ctx)
88
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070089 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080090 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070091 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070092
93 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +000094 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -070095
Martin Stjernholmd90676f2020-01-11 00:37:30 +000096 dexpreopt.RegisterToolModulesForTest(ctx)
97
Colin Cross527012a2017-11-30 22:56:16 -080098 return ctx
99}
100
101func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800102 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800103
Colin Cross98be1bb2019-12-13 20:41:13 -0800104 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000105 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800106
Colin Cross98be1bb2019-12-13 20:41:13 -0800107 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100108 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800109 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700110 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800111 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800112}
113
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900114func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900115 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800116 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900117}
118
Colin Cross98be1bb2019-12-13 20:41:13 -0800119func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900120 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800121 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900122
Colin Cross98be1bb2019-12-13 20:41:13 -0800123 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000124 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900125
Colin Cross98be1bb2019-12-13 20:41:13 -0800126 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900127 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
128 if len(errs) > 0 {
129 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900130 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900131 }
132 _, errs = ctx.PrepareBuildActions(config)
133 if len(errs) > 0 {
134 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900135 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900136 }
137
138 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900139
140 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900141}
142
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700143func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800144 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800145 return testJavaWithConfig(t, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900146}
147
Colin Cross98be1bb2019-12-13 20:41:13 -0800148func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900149 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800150 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800151 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700152
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700153 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700154}
155
Colin Cross2acdae82017-09-15 19:44:24 -0700156func moduleToPath(name string) string {
157 switch {
158 case name == `""`:
159 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700160 case strings.HasSuffix(name, ".jar"):
161 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700162 default:
163 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700164 }
165}
166
Jeongik Chae403e9e2019-12-07 00:16:24 +0900167func TestJavaLinkType(t *testing.T) {
168 testJava(t, `
169 java_library {
170 name: "foo",
171 srcs: ["a.java"],
172 libs: ["bar"],
173 static_libs: ["baz"],
174 }
175
176 java_library {
177 name: "bar",
178 sdk_version: "current",
179 srcs: ["b.java"],
180 }
181
182 java_library {
183 name: "baz",
184 sdk_version: "system_current",
185 srcs: ["c.java"],
186 }
187 `)
188
189 testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", `
190 java_library {
191 name: "foo",
192 srcs: ["a.java"],
193 libs: ["bar"],
194 sdk_version: "current",
195 static_libs: ["baz"],
196 }
197
198 java_library {
199 name: "bar",
200 sdk_version: "current",
201 srcs: ["b.java"],
202 }
203
204 java_library {
205 name: "baz",
206 sdk_version: "system_current",
207 srcs: ["c.java"],
208 }
209 `)
210
211 testJava(t, `
212 java_library {
213 name: "foo",
214 srcs: ["a.java"],
215 libs: ["bar"],
216 sdk_version: "system_current",
217 static_libs: ["baz"],
218 }
219
220 java_library {
221 name: "bar",
222 sdk_version: "current",
223 srcs: ["b.java"],
224 }
225
226 java_library {
227 name: "baz",
228 sdk_version: "system_current",
229 srcs: ["c.java"],
230 }
231 `)
232
233 testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", `
234 java_library {
235 name: "foo",
236 srcs: ["a.java"],
237 libs: ["bar"],
238 sdk_version: "system_current",
239 static_libs: ["baz"],
240 }
241
242 java_library {
243 name: "bar",
244 sdk_version: "current",
245 srcs: ["b.java"],
246 }
247
248 java_library {
249 name: "baz",
250 srcs: ["c.java"],
251 }
252 `)
253}
254
Colin Cross72bb3632017-07-13 16:23:21 -0700255func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700256 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700257 java_library {
258 name: "foo",
259 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700260 libs: ["bar"],
261 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700262 }
263
264 java_library {
265 name: "bar",
266 srcs: ["b.java"],
267 }
268
269 java_library {
270 name: "baz",
271 srcs: ["c.java"],
272 }
Colin Crossd5934c82017-10-02 13:55:26 -0700273 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700274
Colin Cross4c428df2017-09-15 17:36:05 -0700275 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700276 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700277
278 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
279 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
280 }
281
Colin Cross1ee23172017-10-18 14:44:18 -0700282 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700283 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
284 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700285
Nan Zhanged19fc32017-10-19 13:06:22 -0700286 if !strings.Contains(javac.Args["classpath"], barTurbine) {
287 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700288 }
289
Nan Zhanged19fc32017-10-19 13:06:22 -0700290 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
291 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700292 }
293
294 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
295 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700296 }
297}
298
Artur Satayev9cf46692019-11-26 18:08:34 +0000299func TestExportedPlugins(t *testing.T) {
300 type Result struct {
301 library string
302 processors string
303 }
304 var tests = []struct {
305 name string
306 extra string
307 results []Result
308 }{
309 {
310 name: "Exported plugin is not a direct plugin",
311 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
312 results: []Result{{library: "exports", processors: "-proc:none"}},
313 },
314 {
315 name: "Exports plugin to dependee",
316 extra: `
317 java_library{name: "exports", exported_plugins: ["plugin"]}
318 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
319 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
320 `,
321 results: []Result{
322 {library: "foo", processors: "-processor com.android.TestPlugin"},
323 {library: "bar", processors: "-processor com.android.TestPlugin"},
324 },
325 },
326 {
327 name: "Exports plugin to android_library",
328 extra: `
329 java_library{name: "exports", exported_plugins: ["plugin"]}
330 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
331 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
332 `,
333 results: []Result{
334 {library: "foo", processors: "-processor com.android.TestPlugin"},
335 {library: "bar", processors: "-processor com.android.TestPlugin"},
336 },
337 },
338 {
339 name: "Exports plugin is not propagated via transitive deps",
340 extra: `
341 java_library{name: "exports", exported_plugins: ["plugin"]}
342 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
343 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
344 `,
345 results: []Result{
346 {library: "foo", processors: "-processor com.android.TestPlugin"},
347 {library: "bar", processors: "-proc:none"},
348 },
349 },
350 {
351 name: "Exports plugin appends to plugins",
352 extra: `
353 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
354 java_library{name: "exports", exported_plugins: ["plugin"]}
355 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
356 `,
357 results: []Result{
358 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
359 },
360 },
361 }
362
363 for _, test := range tests {
364 t.Run(test.name, func(t *testing.T) {
365 ctx, _ := testJava(t, `
366 java_plugin {
367 name: "plugin",
368 processor_class: "com.android.TestPlugin",
369 }
370 `+test.extra)
371
372 for _, want := range test.results {
373 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
374 if javac.Args["processor"] != want.processors {
375 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
376 }
377 }
378 })
379 }
380}
381
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900382func TestSdkVersionByPartition(t *testing.T) {
383 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900384 java_library {
385 name: "foo",
386 srcs: ["a.java"],
387 vendor: true,
388 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900389 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900390
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900391 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900392 java_library {
393 name: "bar",
394 srcs: ["b.java"],
395 }
396 `)
397
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900398 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900399 bp := `
400 java_library {
401 name: "foo",
402 srcs: ["a.java"],
403 product_specific: true,
404 }
405 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800406
407 config := testConfig(nil, bp, nil)
408 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900409 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800410 testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900411 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800412 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900413 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900414 }
415}
416
Colin Crossd5934c82017-10-02 13:55:26 -0700417func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700418 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700419 java_library {
420 name: "foo",
421 srcs: ["a.java"],
422 target: {
423 android: {
424 srcs: ["b.java"],
425 },
426 },
427 }
428 `)
429
430 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
431 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
432 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
433 }
434}
435
Colin Cross6b4a32d2017-12-05 13:42:45 -0800436func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700437 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800438 java_library_host {
439 name: "foo",
440 srcs: ["a.java"],
441 }
442
443 java_binary_host {
444 name: "bar",
445 srcs: ["b.java"],
446 static_libs: ["foo"],
447 }
448 `)
449
450 buildOS := android.BuildOs.String()
451
452 bar := ctx.ModuleForTests("bar", buildOS+"_common")
453 barJar := bar.Output("bar.jar").Output.String()
454 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
455 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
456
457 // Test that the install binary wrapper depends on the installed jar file
458 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
459 t.Errorf("expected binary wrapper implicits [%q], got %v",
460 barJar, barWrapperDeps)
461 }
462
463}
464
Colin Cross72bb3632017-07-13 16:23:21 -0700465func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700466 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700467 java_library {
468 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000469 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000470 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700471 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700472 }
473
Colin Cross74d73e22017-08-02 11:05:49 -0700474 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700475 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700476 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700477 }
478
Colin Cross74d73e22017-08-02 11:05:49 -0700479 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700480 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700481 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700482 }
Colin Cross42be7612019-02-21 18:12:14 -0800483
484 dex_import {
485 name: "qux",
486 jars: ["b.jar"],
487 }
Colin Cross79c7c262019-04-17 11:11:46 -0700488
489 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000490 name: "sdklib",
491 public: {
492 jars: ["c.jar"],
493 },
494 }
495
Paul Duffin91547182019-11-12 19:39:36 +0000496 prebuilt_stubs_sources {
497 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000498 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000499 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000500
501 java_test_import {
502 name: "test",
503 jars: ["a.jar"],
504 test_suites: ["cts"],
505 test_config: "AndroidTest.xml",
506 }
Colin Cross72bb3632017-07-13 16:23:21 -0700507 `)
508
Paul Duffin9b478b02019-12-10 13:41:51 +0000509 fooModule := ctx.ModuleForTests("foo", "android_common")
510 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700511 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700512 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
513 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700514 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700515
Paul Duffin9b478b02019-12-10 13:41:51 +0000516 fooLibrary := fooModule.Module().(*Library)
517 assertDeepEquals(t, "foo java sources incorrect",
518 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000519
Paul Duffin9b478b02019-12-10 13:41:51 +0000520 assertDeepEquals(t, "foo java source jars incorrect",
521 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
522 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000523
Colin Cross37f6d792018-07-12 12:28:41 -0700524 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
525 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700526 }
527
Colin Cross79c7c262019-04-17 11:11:46 -0700528 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
529 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
530 }
531
Colin Cross37f6d792018-07-12 12:28:41 -0700532 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
533 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700534 }
Colin Cross42be7612019-02-21 18:12:14 -0800535
536 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700537}
538
Paul Duffin9b478b02019-12-10 13:41:51 +0000539func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
540 if !reflect.DeepEqual(expected, actual) {
541 t.Errorf("%s: expected %q, found %q", message, expected, actual)
542 }
543}
544
Paul Duffin56d44902020-01-31 13:36:25 +0000545func TestJavaSdkLibraryImport(t *testing.T) {
546 ctx, _ := testJava(t, `
547 java_library {
548 name: "foo",
549 srcs: ["a.java"],
550 libs: ["sdklib"],
551 sdk_version: "current",
552 }
553
554 java_library {
555 name: "foo.system",
556 srcs: ["a.java"],
557 libs: ["sdklib"],
558 sdk_version: "system_current",
559 }
560
561 java_library {
562 name: "foo.test",
563 srcs: ["a.java"],
564 libs: ["sdklib"],
565 sdk_version: "test_current",
566 }
567
568 java_sdk_library_import {
569 name: "sdklib",
570 public: {
571 jars: ["a.jar"],
572 },
573 system: {
574 jars: ["b.jar"],
575 },
576 test: {
577 jars: ["c.jar"],
578 },
579 }
580 `)
581
582 for _, scope := range []string{"", ".system", ".test"} {
583 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
584 javac := fooModule.Rule("javac")
585
586 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
587 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
588 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
589 }
590 }
591}
592
Colin Cross89536d42017-07-07 14:35:50 -0700593func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700594 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700595 java_defaults {
596 name: "defaults",
597 srcs: ["a.java"],
598 libs: ["bar"],
599 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700600 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700601 }
602
603 java_library {
604 name: "foo",
605 defaults: ["defaults"],
606 }
607
608 java_library {
609 name: "bar",
610 srcs: ["b.java"],
611 }
612
613 java_library {
614 name: "baz",
615 srcs: ["c.java"],
616 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700617
618 android_test {
619 name: "atestOptimize",
620 defaults: ["defaults"],
621 optimize: {enabled: true},
622 }
623
624 android_test {
625 name: "atestNoOptimize",
626 defaults: ["defaults"],
627 }
628
629 android_test {
630 name: "atestDefault",
631 srcs: ["a.java"],
632 }
Colin Cross89536d42017-07-07 14:35:50 -0700633 `)
634
Colin Cross4c428df2017-09-15 17:36:05 -0700635 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700636 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700637
638 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
639 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
640 }
641
Nan Zhanged19fc32017-10-19 13:06:22 -0700642 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
643 if !strings.Contains(javac.Args["classpath"], barTurbine) {
644 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700645 }
646
Colin Cross1ee23172017-10-18 14:44:18 -0700647 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700648 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
649 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700650 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700651
652 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
653 if atestOptimize.Output == nil {
654 t.Errorf("atestOptimize should optimize APK")
655 }
656
657 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
658 if atestNoOptimize.Output == nil {
659 t.Errorf("atestNoOptimize should not optimize APK")
660 }
661
662 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
663 if atestDefault.Output == nil {
664 t.Errorf("atestDefault should optimize APK")
665 }
Colin Cross89536d42017-07-07 14:35:50 -0700666}
667
Colin Cross0f37af02017-09-27 17:42:05 -0700668func TestResources(t *testing.T) {
669 var table = []struct {
670 name string
671 prop string
672 extra string
673 args string
674 }{
675 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700676 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700677 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800678 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700679 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700680 },
681 {
682 // Test that a module with java_resources includes the files
683 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700684 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
685 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700686 },
687 {
688 // Test that a module with a filegroup in java_resources includes the files with the
689 // path prefix
690 name: "resource filegroup",
691 prop: `java_resources: [":foo-res"]`,
692 extra: `
693 filegroup {
694 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800695 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700696 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700697 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700698 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700699 },
700 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700701 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
702 name: "wildcard dirs",
703 prop: `java_resource_dirs: ["java-res/*"]`,
704 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
705 },
706 {
707 // Test that a module exclude_java_resource_dirs excludes the files
708 name: "wildcard dirs",
709 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
710 args: "-C java-res/a -f java-res/a/a",
711 },
Colin Crosscedd4762018-09-13 11:26:19 -0700712 {
713 // Test wildcards in java_resources
714 name: "wildcard files",
715 prop: `java_resources: ["java-res/**/*"]`,
716 args: "-C . -f java-res/a/a -f java-res/b/b",
717 },
718 {
719 // Test exclude_java_resources with java_resources
720 name: "wildcard files with exclude",
721 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
722 args: "-C . -f java-res/a/a",
723 },
724 {
725 // Test exclude_java_resources with java_resource_dirs
726 name: "resource dirs with exclude files",
727 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
728 args: "-C java-res -f java-res/a/a",
729 },
730 {
731 // Test exclude_java_resource_dirs with java_resource_dirs
732 name: "resource dirs with exclude files",
733 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
734 args: "-C java-res -f java-res/a/a -f java-res/b/b",
735 },
Colin Cross0f37af02017-09-27 17:42:05 -0700736 }
737
738 for _, test := range table {
739 t.Run(test.name, func(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700740 ctx, _ := testJava(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700741 java_library {
742 name: "foo",
743 srcs: [
744 "a.java",
745 "b.java",
746 "c.java",
747 ],
748 `+test.prop+`,
749 }
750 `+test.extra)
751
Colin Cross331a1212018-08-15 20:40:52 -0700752 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700753 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700754
755 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
756 t.Errorf("foo combined jars %v does not contain %q",
757 foo.Inputs.Strings(), fooRes.Output.String())
758 }
759
Colin Crossaf9c55b2017-10-03 14:50:08 -0700760 if fooRes.Args["jarArgs"] != test.args {
761 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700762 fooRes.Args["jarArgs"], test.args)
763 }
764 })
765 }
766}
767
Colin Cross0c4ce212019-05-03 15:28:19 -0700768func TestIncludeSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700769 ctx, _ := testJava(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700770 java_library {
771 name: "foo",
772 srcs: [
773 "a.java",
774 "b.java",
775 "c.java",
776 ],
777 include_srcs: true,
778 }
779
780 java_library {
781 name: "bar",
782 srcs: [
783 "a.java",
784 "b.java",
785 "c.java",
786 ],
787 java_resource_dirs: ["java-res"],
788 include_srcs: true,
789 }
790 `)
791
792 // Test a library with include_srcs: true
793 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
794 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
795
796 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
797 t.Errorf("foo combined jars %v does not contain %q", w, g)
798 }
799
800 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
801 t.Errorf("foo source jar args %q is not %q", w, g)
802 }
803
804 // Test a library with include_srcs: true and resources
805 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
806 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
807 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
808 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
809
810 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
811 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
812 }
813
814 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
815 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
816 }
817
818 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
819 t.Errorf("bar combined jars %v does not contain %q", w, g)
820 }
821
822 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
823 t.Errorf("bar source jar args %q is not %q", w, g)
824 }
825
826 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
827 t.Errorf("bar resource jar args %q is not %q", w, g)
828 }
829}
830
Colin Cross54190b32017-10-09 15:34:10 -0700831func TestGeneratedSources(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700832 ctx, _ := testJava(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700833 java_library {
834 name: "foo",
835 srcs: [
836 "a*.java",
837 ":gen",
838 "b*.java",
839 ],
840 }
841
842 genrule {
843 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800844 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700845 out: ["gen.java"],
846 }
847 `)
848
849 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
850 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
851
Colin Cross15e86d92017-10-20 15:07:08 -0700852 if filepath.Base(genrule.Output.String()) != "gen.java" {
853 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700854 }
855
856 if len(javac.Inputs) != 3 ||
857 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700858 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700859 javac.Inputs[2].String() != "b.java" {
860 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
861 }
862}
863
Nan Zhang61eaedb2017-11-02 13:28:15 -0700864func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700865 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700866 java_library {
867 name: "foo",
868 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900869 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700870 }
871
872 java_library {
873 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800874 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700875 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900876 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700877 }
878
879 java_library {
880 name: "baz",
881 srcs: ["c.java"],
882 libs: ["bar"],
883 sdk_version: "14",
884 }
885 `)
886
887 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
888 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
889 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
890 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
891 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
892
893 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
894 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
895 }
896
897 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
898 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
899 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
900 }
901 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
902 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
903 }
904 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
905 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
906 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100907 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700908 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100909 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700910 }
911}
912
913func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700914 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700915 java_library {
916 name: "bar",
917 srcs: ["a.java","b.java","c.java"],
918 javac_shard_size: 1
919 }
920 `)
921
922 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
923 for i := 0; i < 3; i++ {
924 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
925 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
926 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
927 }
928 }
929}
930
Nan Zhang581fd212018-01-10 16:06:12 -0800931func TestDroiddoc(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700932 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +0000933 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -0800934 name: "droiddoc-templates-sdk",
935 path: ".",
936 }
Jiyong Park29074592019-07-07 16:27:47 +0900937 filegroup {
938 name: "bar-doc-aidl-srcs",
939 srcs: ["bar-doc/IBar.aidl"],
940 path: "bar-doc",
941 }
Nan Zhang581fd212018-01-10 16:06:12 -0800942 droiddoc {
943 name: "bar-doc",
944 srcs: [
945 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900946 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +0900947 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -0800948 ],
949 exclude_srcs: [
950 "bar-doc/b.java"
951 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800952 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800953 hdf: [
954 "android.whichdoc offline",
955 ],
956 knowntags: [
957 "bar-doc/known_oj_tags.txt",
958 ],
959 proofread_file: "libcore-proofread.txt",
960 todo_file: "libcore-docs-todo.html",
961 args: "-offlinemode -title \"libcore\"",
962 }
963 `)
964
Colin Crossc0806172019-06-14 18:51:47 -0700965 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +0900966 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -0700967 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +0900968 javaSrcs = append(javaSrcs, i.Base())
969 }
Colin Crossc0806172019-06-14 18:51:47 -0700970 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
971 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +0900972 }
973
Colin Crossc0806172019-06-14 18:51:47 -0700974 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
975 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
976 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
977 }
978
979 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
980 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +0900981 }
Nan Zhang581fd212018-01-10 16:06:12 -0800982}
983
Paul Duffin83a2d962019-11-19 19:44:10 +0000984func TestDroidstubsWithSystemModules(t *testing.T) {
985 ctx, _ := testJava(t, `
986 droidstubs {
987 name: "stubs-source-system-modules",
988 srcs: [
989 "bar-doc/*.java",
990 ],
991 sdk_version: "none",
992 system_modules: "source-system-modules",
993 }
994
995 java_library {
996 name: "source-jar",
997 srcs: [
998 "a.java",
999 ],
1000 }
1001
1002 java_system_modules {
1003 name: "source-system-modules",
1004 libs: ["source-jar"],
1005 }
1006
1007 droidstubs {
1008 name: "stubs-prebuilt-system-modules",
1009 srcs: [
1010 "bar-doc/*.java",
1011 ],
1012 sdk_version: "none",
1013 system_modules: "prebuilt-system-modules",
1014 }
1015
1016 java_import {
1017 name: "prebuilt-jar",
1018 jars: ["a.jar"],
1019 }
1020
1021 java_system_modules_import {
1022 name: "prebuilt-system-modules",
1023 libs: ["prebuilt-jar"],
1024 }
1025 `)
1026
1027 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1028
1029 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1030}
1031
1032func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1033 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1034 var systemJars []string
1035 for _, i := range metalavaRule.Implicits {
1036 systemJars = append(systemJars, i.Base())
1037 }
1038 if len(systemJars) != 1 || systemJars[0] != systemJar {
1039 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1040 }
1041}
1042
Colin Cross54250902017-12-05 09:28:08 -08001043func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001044 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001045 java_library {
1046 name: "foo",
1047 srcs: ["a.java"],
1048 }
1049
1050 java_genrule {
1051 name: "jargen",
1052 tool_files: ["b.java"],
1053 cmd: "$(location b.java) $(in) $(out)",
1054 out: ["jargen.jar"],
1055 srcs: [":foo"],
1056 }
1057
1058 java_library {
1059 name: "bar",
1060 static_libs: ["jargen"],
1061 srcs: ["c.java"],
1062 }
1063
1064 java_library {
1065 name: "baz",
1066 libs: ["jargen"],
1067 srcs: ["c.java"],
1068 }
1069 `)
1070
1071 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1072 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1073 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1074 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1075 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1076
1077 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1078 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1079 }
1080
1081 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1082 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1083 }
1084
1085 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1086 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1087 }
1088
1089 if len(barCombined.Inputs) != 2 ||
1090 barCombined.Inputs[0].String() != bar.Output.String() ||
1091 barCombined.Inputs[1].String() != jargen.Output.String() {
1092 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1093 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1094 }
1095}
1096
Nan Zhang27e284d2018-02-09 21:03:53 +00001097func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001098 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001099 java_library {
1100 name: "foo",
1101 srcs: ["a.java", ":foo-srcs"],
1102 exclude_srcs: ["a.java", ":foo-excludes"],
1103 }
1104
1105 filegroup {
1106 name: "foo-srcs",
1107 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1108 }
1109
1110 filegroup {
1111 name: "foo-excludes",
1112 srcs: ["java-fg/a.java", "java-fg/b.java"],
1113 }
1114 `)
1115
1116 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1117
1118 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1119 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1120 }
1121}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001122
Paul Duffin52d398a2019-06-11 12:31:14 +01001123func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001124 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001125 "libcore/Android.bp": []byte(`
1126 java_library {
1127 name: "core",
1128 sdk_version: "none",
1129 system_modules: "none",
1130 }`),
1131 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001132 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001133 run(t, ctx, config)
1134}
1135
Jiyong Parkc678ad32018-04-10 13:07:10 +09001136func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001137 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001138 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001139 name: "droiddoc-templates-sdk",
1140 path: ".",
1141 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001142 java_sdk_library {
1143 name: "foo",
1144 srcs: ["a.java", "b.java"],
1145 api_packages: ["foo"],
1146 }
1147 java_sdk_library {
1148 name: "bar",
1149 srcs: ["a.java", "b.java"],
1150 api_packages: ["bar"],
1151 }
1152 java_library {
1153 name: "baz",
1154 srcs: ["c.java"],
1155 libs: ["foo", "bar"],
1156 sdk_version: "system_current",
1157 }
Jiyong Park1be96912018-05-28 18:02:19 +09001158 java_library {
1159 name: "qux",
1160 srcs: ["c.java"],
1161 libs: ["baz"],
1162 sdk_version: "system_current",
1163 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001164 java_library {
1165 name: "baz-test",
1166 srcs: ["c.java"],
1167 libs: ["foo"],
1168 sdk_version: "test_current",
1169 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001170 java_library {
1171 name: "baz-29",
1172 srcs: ["c.java"],
1173 libs: ["foo"],
1174 sdk_version: "system_29",
1175 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001176 `)
1177
1178 // check the existence of the internal modules
1179 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001180 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1181 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1182 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1183 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1184 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1185 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001186 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001187 ctx.ModuleForTests("foo.api.public.28", "")
1188 ctx.ModuleForTests("foo.api.system.28", "")
1189 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001190
1191 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1192 // tests if baz is actually linked to the stubs lib
1193 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1194 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1195 "foo.stubs.system.jar")
1196 }
1197 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001198 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001199 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001200 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001201 }
1202 // test if baz is not linked to the system variant of foo
1203 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1204 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1205 "foo.stubs.jar")
1206 }
Jiyong Park1be96912018-05-28 18:02:19 +09001207
Paul Duffin726d23c2020-01-22 16:30:37 +00001208 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1209 // tests if baz-test is actually linked to the test stubs lib
1210 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1211 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1212 "foo.stubs.test.jar")
1213 }
1214
Paul Duffina2db18f2020-01-22 17:11:15 +00001215 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1216 // tests if baz-29 is actually linked to the system 29 stubs lib
1217 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1218 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1219 "prebuilts/sdk/29/system/foo.jar")
1220 }
1221
Jiyong Park1be96912018-05-28 18:02:19 +09001222 // test if baz has exported SDK lib names foo and bar to qux
1223 qux := ctx.ModuleForTests("qux", "android_common")
1224 if quxLib, ok := qux.Module().(*Library); ok {
1225 sdkLibs := quxLib.ExportedSdkLibs()
1226 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
1227 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
1228 }
1229 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001230}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001231
Paul Duffin3375e352020-04-28 10:44:03 +01001232func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1233 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1234 java_sdk_library {
1235 name: "foo",
1236 srcs: ["a.java", "b.java"],
1237 api_packages: ["foo"],
1238 // Explicitly disable public to test the check that ensures the set of enabled
1239 // scopes is consistent.
1240 public: {
1241 enabled: false,
1242 },
1243 system: {
1244 enabled: true,
1245 },
1246 }
1247 `)
1248}
1249
Paul Duffin87a05a32020-05-12 11:50:28 +01001250func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1251 testJava(t, `
1252 java_sdk_library {
1253 name: "foo",
1254 srcs: ["a.java", "b.java"],
1255 api_packages: ["foo"],
1256 system: {
1257 enabled: true,
1258 sdk_version: "module_current",
1259 },
1260 }
1261 `)
1262}
1263
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001264var compilerFlagsTestCases = []struct {
1265 in string
1266 out bool
1267}{
1268 {
1269 in: "a",
1270 out: false,
1271 },
1272 {
1273 in: "-a",
1274 out: true,
1275 },
1276 {
1277 in: "-no-jdk",
1278 out: false,
1279 },
1280 {
1281 in: "-no-stdlib",
1282 out: false,
1283 },
1284 {
1285 in: "-kotlin-home",
1286 out: false,
1287 },
1288 {
1289 in: "-kotlin-home /some/path",
1290 out: false,
1291 },
1292 {
1293 in: "-include-runtime",
1294 out: false,
1295 },
1296 {
1297 in: "-Xintellij-plugin-root",
1298 out: false,
1299 },
1300}
1301
1302type mockContext struct {
1303 android.ModuleContext
1304 result bool
1305}
1306
1307func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1308 // CheckBadCompilerFlags calls this function when the flag should be rejected
1309 ctx.result = false
1310}
1311
1312func TestCompilerFlags(t *testing.T) {
1313 for _, testCase := range compilerFlagsTestCases {
1314 ctx := &mockContext{result: true}
1315 CheckKotlincFlags(ctx, []string{testCase.in})
1316 if ctx.result != testCase.out {
1317 t.Errorf("incorrect output:")
1318 t.Errorf(" input: %#v", testCase.in)
1319 t.Errorf(" expected: %#v", testCase.out)
1320 t.Errorf(" got: %#v", ctx.result)
1321 }
1322 }
1323}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001324
1325// TODO(jungjw): Consider making this more robust by ignoring path order.
1326func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1327 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1328 flags := strings.Split(variables["javacFlags"], " ")
1329 got := ""
1330 for _, flag := range flags {
1331 keyEnd := strings.Index(flag, "=")
1332 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1333 got = flag[keyEnd+1:]
1334 break
1335 }
1336 }
1337 if expected != got {
1338 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1339 }
1340}
1341
1342func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001343 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001344 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001345 bp := `
1346 java_library {
1347 name: "foo",
1348 srcs: ["a.java"],
1349 java_version: "1.8",
1350 }
1351
1352 java_library {
1353 name: "bar",
1354 srcs: ["b.java"],
1355 sdk_version: "none",
1356 system_modules: "none",
1357 patch_module: "java.base",
1358 java_version: "1.8",
1359 }
1360
1361 java_library {
1362 name: "baz",
1363 srcs: ["c.java"],
1364 patch_module: "java.base",
1365 java_version: "1.8",
1366 }
1367 `
1368 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001369
1370 checkPatchModuleFlag(t, ctx, "foo", "")
1371 checkPatchModuleFlag(t, ctx, "bar", "")
1372 checkPatchModuleFlag(t, ctx, "baz", "")
1373 })
1374
Pete Gillin0c2143e2019-05-02 15:32:11 +01001375 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001376 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001377 bp := `
1378 java_library {
1379 name: "foo",
1380 srcs: ["a.java"],
1381 }
1382
1383 java_library {
1384 name: "bar",
1385 srcs: ["b.java"],
1386 sdk_version: "none",
1387 system_modules: "none",
1388 patch_module: "java.base",
1389 }
1390
1391 java_library {
1392 name: "baz",
1393 srcs: ["c.java"],
1394 patch_module: "java.base",
1395 }
1396 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001397 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001398
1399 checkPatchModuleFlag(t, ctx, "foo", "")
1400 expected := "java.base=.:" + buildDir
1401 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09001402 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001403 checkPatchModuleFlag(t, ctx, "baz", expected)
1404 })
1405}
Paul Duffina7b9f422020-01-10 17:12:18 +00001406
1407func TestJavaSystemModules(t *testing.T) {
1408 ctx, _ := testJava(t, `
1409 java_system_modules {
1410 name: "system-modules",
1411 libs: ["system-module1", "system-module2"],
1412 }
1413 java_library {
1414 name: "system-module1",
1415 srcs: ["a.java"],
1416 sdk_version: "none",
1417 system_modules: "none",
1418 }
1419 java_library {
1420 name: "system-module2",
1421 srcs: ["b.java"],
1422 sdk_version: "none",
1423 system_modules: "none",
1424 }
1425 `)
1426
1427 // check the existence of the module
1428 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1429
1430 cmd := systemModules.Rule("jarsTosystemModules")
1431
1432 // make sure the command compiles against the supplied modules.
1433 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1434 if !strings.Contains(cmd.Args["classpath"], module) {
1435 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1436 module)
1437 }
1438 }
1439}
Paul Duffin90169ba2020-01-10 17:16:44 +00001440
1441func TestJavaSystemModulesImport(t *testing.T) {
1442 ctx, _ := testJava(t, `
1443 java_system_modules_import {
1444 name: "system-modules",
1445 libs: ["system-module1", "system-module2"],
1446 }
1447 java_import {
1448 name: "system-module1",
1449 jars: ["a.jar"],
1450 }
1451 java_import {
1452 name: "system-module2",
1453 jars: ["b.jar"],
1454 }
1455 `)
1456
1457 // check the existence of the module
1458 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1459
1460 cmd := systemModules.Rule("jarsTosystemModules")
1461
1462 // make sure the command compiles against the supplied modules.
1463 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1464 if !strings.Contains(cmd.Args["classpath"], module) {
1465 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1466 module)
1467 }
1468 }
1469}
Paul Duffin83a2d962019-11-19 19:44:10 +00001470
1471func TestJavaLibraryWithSystemModules(t *testing.T) {
1472 ctx, _ := testJava(t, `
1473 java_library {
1474 name: "lib-with-source-system-modules",
1475 srcs: [
1476 "a.java",
1477 ],
1478 sdk_version: "none",
1479 system_modules: "source-system-modules",
1480 }
1481
1482 java_library {
1483 name: "source-jar",
1484 srcs: [
1485 "a.java",
1486 ],
1487 }
1488
1489 java_system_modules {
1490 name: "source-system-modules",
1491 libs: ["source-jar"],
1492 }
1493
1494 java_library {
1495 name: "lib-with-prebuilt-system-modules",
1496 srcs: [
1497 "a.java",
1498 ],
1499 sdk_version: "none",
1500 system_modules: "prebuilt-system-modules",
1501 }
1502
1503 java_import {
1504 name: "prebuilt-jar",
1505 jars: ["a.jar"],
1506 }
1507
1508 java_system_modules_import {
1509 name: "prebuilt-system-modules",
1510 libs: ["prebuilt-jar"],
1511 }
1512 `)
1513
1514 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
1515
1516 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
1517}
1518
1519func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
1520 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
1521 bootClasspath := javacRule.Args["bootClasspath"]
1522 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
1523 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
1524 }
1525}
Jiyong Park19604de2020-03-24 16:44:11 +09001526
1527func TestAidlExportIncludeDirsFromImports(t *testing.T) {
1528 ctx, _ := testJava(t, `
1529 java_library {
1530 name: "foo",
1531 srcs: ["aidl/foo/IFoo.aidl"],
1532 libs: ["bar"],
1533 }
1534
1535 java_import {
1536 name: "bar",
1537 jars: ["a.jar"],
1538 aidl: {
1539 export_include_dirs: ["aidl/bar"],
1540 },
1541 }
1542 `)
1543
1544 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
1545 expectedAidlFlag := "-Iaidl/bar"
1546 if !strings.Contains(aidlCommand, expectedAidlFlag) {
1547 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
1548 }
1549}