blob: 1d07e70011e07dfbb7619133648e9b24277539ea [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"
Paul Duffindaaa3322020-05-26 18:13:57 +010022 "regexp"
Paul Duffin859fe962020-05-15 10:20:31 +010023 "sort"
Nan Zhang61eaedb2017-11-02 13:28:15 -070024 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070027
Jeongik Cha28df2572019-11-11 10:46:36 +090028 "github.com/google/blueprint/proptools"
29
Colin Crossa4f08812018-10-02 22:03:40 -070030 "android/soong/android"
31 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080032 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070033 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070034)
35
36var buildDir string
37
38func setUp() {
39 var err error
40 buildDir, err = ioutil.TempDir("", "soong_java_test")
41 if err != nil {
42 panic(err)
43 }
44}
45
46func tearDown() {
47 os.RemoveAll(buildDir)
48}
49
50func TestMain(m *testing.M) {
51 run := func() int {
52 setUp()
53 defer tearDown()
54
55 return m.Run()
56 }
57
58 os.Exit(run())
59}
Colin Cross527012a2017-11-30 22:56:16 -080060
Colin Cross98be1bb2019-12-13 20:41:13 -080061func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000062 bp += dexpreopt.BpToolModulesForTest()
63
Martin Stjernholm75a48d82020-01-10 20:32:59 +000064 config := TestConfig(buildDir, env, bp, fs)
65
66 // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that
67 // it doesn't create a real one, which would fail.
68 _ = dexpreopt.GlobalSoongConfigForTests(config)
69
70 return config
Colin Cross1369cdb2017-09-29 17:58:17 -070071}
72
Colin Cross98be1bb2019-12-13 20:41:13 -080073func testContext() *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070074
Colin Cross4c428df2017-09-15 17:36:05 -070075 ctx := android.NewTestArchContext()
Paul Duffinf9b1da02019-12-18 19:51:55 +000076 RegisterJavaBuildComponents(ctx)
77 RegisterAppBuildComponents(ctx)
78 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000079 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000080 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080081 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080082 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
83 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000084 RegisterDocsBuildComponents(ctx)
85 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000086 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070087 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffina48f7582019-12-19 11:25:19 +000088
89 RegisterPrebuiltApisBuildComponents(ctx)
90
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070091 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080092 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070093 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070094
95 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +000096 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -070097
Martin Stjernholmd90676f2020-01-11 00:37:30 +000098 dexpreopt.RegisterToolModulesForTest(ctx)
99
Colin Cross527012a2017-11-30 22:56:16 -0800100 return ctx
101}
102
103func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800104 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800105
Colin Cross98be1bb2019-12-13 20:41:13 -0800106 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000107 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800108
Colin Cross98be1bb2019-12-13 20:41:13 -0800109 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100110 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800111 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700112 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800113 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800114}
115
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900116func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900117 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800118 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900119}
120
Colin Cross98be1bb2019-12-13 20:41:13 -0800121func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900122 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800123 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900124
Colin Cross98be1bb2019-12-13 20:41:13 -0800125 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000126 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900127
Colin Cross98be1bb2019-12-13 20:41:13 -0800128 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900129 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
130 if len(errs) > 0 {
131 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900132 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900133 }
134 _, errs = ctx.PrepareBuildActions(config)
135 if len(errs) > 0 {
136 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900137 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900138 }
139
140 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900141
142 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900143}
144
Colin Cross238c1f32020-06-07 16:58:18 -0700145func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
146 t.Helper()
147 return testJavaWithConfig(t, testConfig(nil, bp, fs))
148}
149
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700150func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800151 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700152 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900153}
154
Colin Cross98be1bb2019-12-13 20:41:13 -0800155func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900156 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800157 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800158 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700159
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700160 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700161}
162
Colin Cross2acdae82017-09-15 19:44:24 -0700163func moduleToPath(name string) string {
164 switch {
165 case name == `""`:
166 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700167 case strings.HasSuffix(name, ".jar"):
168 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700169 default:
170 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700171 }
172}
173
Jeongik Chae403e9e2019-12-07 00:16:24 +0900174func TestJavaLinkType(t *testing.T) {
175 testJava(t, `
176 java_library {
177 name: "foo",
178 srcs: ["a.java"],
179 libs: ["bar"],
180 static_libs: ["baz"],
181 }
182
183 java_library {
184 name: "bar",
185 sdk_version: "current",
186 srcs: ["b.java"],
187 }
188
189 java_library {
190 name: "baz",
191 sdk_version: "system_current",
192 srcs: ["c.java"],
193 }
194 `)
195
196 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.", `
197 java_library {
198 name: "foo",
199 srcs: ["a.java"],
200 libs: ["bar"],
201 sdk_version: "current",
202 static_libs: ["baz"],
203 }
204
205 java_library {
206 name: "bar",
207 sdk_version: "current",
208 srcs: ["b.java"],
209 }
210
211 java_library {
212 name: "baz",
213 sdk_version: "system_current",
214 srcs: ["c.java"],
215 }
216 `)
217
218 testJava(t, `
219 java_library {
220 name: "foo",
221 srcs: ["a.java"],
222 libs: ["bar"],
223 sdk_version: "system_current",
224 static_libs: ["baz"],
225 }
226
227 java_library {
228 name: "bar",
229 sdk_version: "current",
230 srcs: ["b.java"],
231 }
232
233 java_library {
234 name: "baz",
235 sdk_version: "system_current",
236 srcs: ["c.java"],
237 }
238 `)
239
240 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.", `
241 java_library {
242 name: "foo",
243 srcs: ["a.java"],
244 libs: ["bar"],
245 sdk_version: "system_current",
246 static_libs: ["baz"],
247 }
248
249 java_library {
250 name: "bar",
251 sdk_version: "current",
252 srcs: ["b.java"],
253 }
254
255 java_library {
256 name: "baz",
257 srcs: ["c.java"],
258 }
259 `)
260}
261
Colin Cross72bb3632017-07-13 16:23:21 -0700262func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700263 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700264 java_library {
265 name: "foo",
266 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700267 libs: ["bar"],
268 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700269 }
270
271 java_library {
272 name: "bar",
273 srcs: ["b.java"],
274 }
275
276 java_library {
277 name: "baz",
278 srcs: ["c.java"],
279 }
Colin Crossd5934c82017-10-02 13:55:26 -0700280 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700281
Colin Cross4c428df2017-09-15 17:36:05 -0700282 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700283 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700284
285 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
286 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
287 }
288
Colin Cross1ee23172017-10-18 14:44:18 -0700289 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700290 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
291 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700292
Nan Zhanged19fc32017-10-19 13:06:22 -0700293 if !strings.Contains(javac.Args["classpath"], barTurbine) {
294 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700295 }
296
Nan Zhanged19fc32017-10-19 13:06:22 -0700297 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
298 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700299 }
300
301 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
302 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700303 }
304}
305
Artur Satayev9cf46692019-11-26 18:08:34 +0000306func TestExportedPlugins(t *testing.T) {
307 type Result struct {
308 library string
309 processors string
310 }
311 var tests = []struct {
312 name string
313 extra string
314 results []Result
315 }{
316 {
317 name: "Exported plugin is not a direct plugin",
318 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
319 results: []Result{{library: "exports", processors: "-proc:none"}},
320 },
321 {
322 name: "Exports plugin to dependee",
323 extra: `
324 java_library{name: "exports", exported_plugins: ["plugin"]}
325 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
326 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
327 `,
328 results: []Result{
329 {library: "foo", processors: "-processor com.android.TestPlugin"},
330 {library: "bar", processors: "-processor com.android.TestPlugin"},
331 },
332 },
333 {
334 name: "Exports plugin to android_library",
335 extra: `
336 java_library{name: "exports", exported_plugins: ["plugin"]}
337 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
338 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
339 `,
340 results: []Result{
341 {library: "foo", processors: "-processor com.android.TestPlugin"},
342 {library: "bar", processors: "-processor com.android.TestPlugin"},
343 },
344 },
345 {
346 name: "Exports plugin is not propagated via transitive deps",
347 extra: `
348 java_library{name: "exports", exported_plugins: ["plugin"]}
349 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
350 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
351 `,
352 results: []Result{
353 {library: "foo", processors: "-processor com.android.TestPlugin"},
354 {library: "bar", processors: "-proc:none"},
355 },
356 },
357 {
358 name: "Exports plugin appends to plugins",
359 extra: `
360 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
361 java_library{name: "exports", exported_plugins: ["plugin"]}
362 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
363 `,
364 results: []Result{
365 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
366 },
367 },
368 }
369
370 for _, test := range tests {
371 t.Run(test.name, func(t *testing.T) {
372 ctx, _ := testJava(t, `
373 java_plugin {
374 name: "plugin",
375 processor_class: "com.android.TestPlugin",
376 }
377 `+test.extra)
378
379 for _, want := range test.results {
380 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
381 if javac.Args["processor"] != want.processors {
382 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
383 }
384 }
385 })
386 }
387}
388
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900389func TestSdkVersionByPartition(t *testing.T) {
390 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900391 java_library {
392 name: "foo",
393 srcs: ["a.java"],
394 vendor: true,
395 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900396 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900397
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900398 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900399 java_library {
400 name: "bar",
401 srcs: ["b.java"],
402 }
403 `)
404
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900405 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900406 bp := `
407 java_library {
408 name: "foo",
409 srcs: ["a.java"],
410 product_specific: true,
411 }
412 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800413
414 config := testConfig(nil, bp, nil)
415 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900416 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800417 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 +0900418 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800419 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900420 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900421 }
422}
423
Colin Crossd5934c82017-10-02 13:55:26 -0700424func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700425 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700426 java_library {
427 name: "foo",
428 srcs: ["a.java"],
429 target: {
430 android: {
431 srcs: ["b.java"],
432 },
433 },
434 }
435 `)
436
437 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
438 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
439 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
440 }
441}
442
Colin Cross6b4a32d2017-12-05 13:42:45 -0800443func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700444 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800445 java_library_host {
446 name: "foo",
447 srcs: ["a.java"],
448 }
449
450 java_binary_host {
451 name: "bar",
452 srcs: ["b.java"],
453 static_libs: ["foo"],
454 }
455 `)
456
457 buildOS := android.BuildOs.String()
458
459 bar := ctx.ModuleForTests("bar", buildOS+"_common")
460 barJar := bar.Output("bar.jar").Output.String()
461 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
462 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
463
464 // Test that the install binary wrapper depends on the installed jar file
465 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
466 t.Errorf("expected binary wrapper implicits [%q], got %v",
467 barJar, barWrapperDeps)
468 }
Alex Humesky2070e322020-06-09 20:23:08 -0400469}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800470
Alex Humesky2070e322020-06-09 20:23:08 -0400471func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
472 bp := `
473 java_library {
474 name: "target_library",
475 srcs: ["a.java"],
476 }
477
478 java_binary_host {
479 name: "host_binary",
480 srcs: ["b.java"],
481 }
482 `
483 config := testConfig(nil, bp, nil)
484 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
485
486 ctx, _ := testJavaWithConfig(t, config)
487
488 // first, sanity check that the -g flag is added to target modules
489 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
490 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
491 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
492 t.Errorf("target library javac flags %v should contain "+
493 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
494 }
495
496 // check that -g is not overridden for host modules
497 buildOS := android.BuildOs.String()
498 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
499 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
500 if strings.Contains(hostJavaFlags, "-g:source,lines") {
501 t.Errorf("java_binary_host javac flags %v should not have "+
502 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
503 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800504}
505
Colin Cross72bb3632017-07-13 16:23:21 -0700506func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700507 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700508 java_library {
509 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000510 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000511 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700512 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700513 }
514
Colin Cross74d73e22017-08-02 11:05:49 -0700515 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700516 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700517 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700518 }
519
Colin Cross74d73e22017-08-02 11:05:49 -0700520 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700521 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700522 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700523 }
Colin Cross42be7612019-02-21 18:12:14 -0800524
525 dex_import {
526 name: "qux",
527 jars: ["b.jar"],
528 }
Colin Cross79c7c262019-04-17 11:11:46 -0700529
530 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000531 name: "sdklib",
532 public: {
533 jars: ["c.jar"],
534 },
535 }
536
Paul Duffin91547182019-11-12 19:39:36 +0000537 prebuilt_stubs_sources {
538 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000539 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000540 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000541
542 java_test_import {
543 name: "test",
544 jars: ["a.jar"],
545 test_suites: ["cts"],
546 test_config: "AndroidTest.xml",
547 }
Colin Cross72bb3632017-07-13 16:23:21 -0700548 `)
549
Paul Duffin9b478b02019-12-10 13:41:51 +0000550 fooModule := ctx.ModuleForTests("foo", "android_common")
551 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700552 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700553 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
554 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700555 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700556
Paul Duffin9b478b02019-12-10 13:41:51 +0000557 fooLibrary := fooModule.Module().(*Library)
558 assertDeepEquals(t, "foo java sources incorrect",
559 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000560
Paul Duffin9b478b02019-12-10 13:41:51 +0000561 assertDeepEquals(t, "foo java source jars incorrect",
562 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
563 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000564
Colin Cross37f6d792018-07-12 12:28:41 -0700565 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
566 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700567 }
568
Colin Cross79c7c262019-04-17 11:11:46 -0700569 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
570 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
571 }
572
Colin Cross37f6d792018-07-12 12:28:41 -0700573 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
574 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700575 }
Colin Cross42be7612019-02-21 18:12:14 -0800576
577 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700578}
579
Paul Duffin9b478b02019-12-10 13:41:51 +0000580func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
581 if !reflect.DeepEqual(expected, actual) {
582 t.Errorf("%s: expected %q, found %q", message, expected, actual)
583 }
584}
585
Paul Duffin56d44902020-01-31 13:36:25 +0000586func TestJavaSdkLibraryImport(t *testing.T) {
587 ctx, _ := testJava(t, `
588 java_library {
589 name: "foo",
590 srcs: ["a.java"],
591 libs: ["sdklib"],
592 sdk_version: "current",
593 }
594
595 java_library {
596 name: "foo.system",
597 srcs: ["a.java"],
598 libs: ["sdklib"],
599 sdk_version: "system_current",
600 }
601
602 java_library {
603 name: "foo.test",
604 srcs: ["a.java"],
605 libs: ["sdklib"],
606 sdk_version: "test_current",
607 }
608
609 java_sdk_library_import {
610 name: "sdklib",
611 public: {
612 jars: ["a.jar"],
613 },
614 system: {
615 jars: ["b.jar"],
616 },
617 test: {
618 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100619 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000620 },
621 }
622 `)
623
624 for _, scope := range []string{"", ".system", ".test"} {
625 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
626 javac := fooModule.Rule("javac")
627
628 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
629 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
630 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
631 }
632 }
633}
634
Colin Cross89536d42017-07-07 14:35:50 -0700635func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700636 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700637 java_defaults {
638 name: "defaults",
639 srcs: ["a.java"],
640 libs: ["bar"],
641 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700642 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700643 }
644
645 java_library {
646 name: "foo",
647 defaults: ["defaults"],
648 }
649
650 java_library {
651 name: "bar",
652 srcs: ["b.java"],
653 }
654
655 java_library {
656 name: "baz",
657 srcs: ["c.java"],
658 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700659
660 android_test {
661 name: "atestOptimize",
662 defaults: ["defaults"],
663 optimize: {enabled: true},
664 }
665
666 android_test {
667 name: "atestNoOptimize",
668 defaults: ["defaults"],
669 }
670
671 android_test {
672 name: "atestDefault",
673 srcs: ["a.java"],
674 }
Colin Cross89536d42017-07-07 14:35:50 -0700675 `)
676
Colin Cross4c428df2017-09-15 17:36:05 -0700677 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700678 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700679
680 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
681 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
682 }
683
Nan Zhanged19fc32017-10-19 13:06:22 -0700684 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
685 if !strings.Contains(javac.Args["classpath"], barTurbine) {
686 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700687 }
688
Colin Cross1ee23172017-10-18 14:44:18 -0700689 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700690 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
691 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700692 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700693
694 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
695 if atestOptimize.Output == nil {
696 t.Errorf("atestOptimize should optimize APK")
697 }
698
699 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
700 if atestNoOptimize.Output == nil {
701 t.Errorf("atestNoOptimize should not optimize APK")
702 }
703
704 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
705 if atestDefault.Output == nil {
706 t.Errorf("atestDefault should optimize APK")
707 }
Colin Cross89536d42017-07-07 14:35:50 -0700708}
709
Colin Cross0f37af02017-09-27 17:42:05 -0700710func TestResources(t *testing.T) {
711 var table = []struct {
712 name string
713 prop string
714 extra string
715 args string
716 }{
717 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700718 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700719 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800720 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700721 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700722 },
723 {
724 // Test that a module with java_resources includes the files
725 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700726 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
727 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700728 },
729 {
730 // Test that a module with a filegroup in java_resources includes the files with the
731 // path prefix
732 name: "resource filegroup",
733 prop: `java_resources: [":foo-res"]`,
734 extra: `
735 filegroup {
736 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800737 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700738 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700739 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700740 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700741 },
742 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700743 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
744 name: "wildcard dirs",
745 prop: `java_resource_dirs: ["java-res/*"]`,
746 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
747 },
748 {
749 // Test that a module exclude_java_resource_dirs excludes the files
750 name: "wildcard dirs",
751 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
752 args: "-C java-res/a -f java-res/a/a",
753 },
Colin Crosscedd4762018-09-13 11:26:19 -0700754 {
755 // Test wildcards in java_resources
756 name: "wildcard files",
757 prop: `java_resources: ["java-res/**/*"]`,
758 args: "-C . -f java-res/a/a -f java-res/b/b",
759 },
760 {
761 // Test exclude_java_resources with java_resources
762 name: "wildcard files with exclude",
763 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
764 args: "-C . -f java-res/a/a",
765 },
766 {
767 // Test exclude_java_resources with java_resource_dirs
768 name: "resource dirs with exclude files",
769 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
770 args: "-C java-res -f java-res/a/a",
771 },
772 {
773 // Test exclude_java_resource_dirs with java_resource_dirs
774 name: "resource dirs with exclude files",
775 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
776 args: "-C java-res -f java-res/a/a -f java-res/b/b",
777 },
Colin Cross0f37af02017-09-27 17:42:05 -0700778 }
779
780 for _, test := range table {
781 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700782 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700783 java_library {
784 name: "foo",
785 srcs: [
786 "a.java",
787 "b.java",
788 "c.java",
789 ],
790 `+test.prop+`,
791 }
Colin Cross238c1f32020-06-07 16:58:18 -0700792 `+test.extra,
793 map[string][]byte{
794 "java-res/a/a": nil,
795 "java-res/b/b": nil,
796 "java-res2/a": nil,
797 },
798 )
Colin Cross0f37af02017-09-27 17:42:05 -0700799
Colin Cross331a1212018-08-15 20:40:52 -0700800 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700801 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700802
803 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
804 t.Errorf("foo combined jars %v does not contain %q",
805 foo.Inputs.Strings(), fooRes.Output.String())
806 }
807
Colin Crossaf9c55b2017-10-03 14:50:08 -0700808 if fooRes.Args["jarArgs"] != test.args {
809 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700810 fooRes.Args["jarArgs"], test.args)
811 }
812 })
813 }
814}
815
Colin Cross0c4ce212019-05-03 15:28:19 -0700816func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700817 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700818 java_library {
819 name: "foo",
820 srcs: [
821 "a.java",
822 "b.java",
823 "c.java",
824 ],
825 include_srcs: true,
826 }
827
828 java_library {
829 name: "bar",
830 srcs: [
831 "a.java",
832 "b.java",
833 "c.java",
834 ],
835 java_resource_dirs: ["java-res"],
836 include_srcs: true,
837 }
Colin Cross238c1f32020-06-07 16:58:18 -0700838 `, map[string][]byte{
839 "java-res/a/a": nil,
840 "java-res/b/b": nil,
841 "java-res2/a": nil,
842 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700843
844 // Test a library with include_srcs: true
845 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
846 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
847
848 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
849 t.Errorf("foo combined jars %v does not contain %q", w, g)
850 }
851
852 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
853 t.Errorf("foo source jar args %q is not %q", w, g)
854 }
855
856 // Test a library with include_srcs: true and resources
857 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
858 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
859 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
860 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
861
862 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
863 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
864 }
865
866 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
867 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
868 }
869
870 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
871 t.Errorf("bar combined jars %v does not contain %q", w, g)
872 }
873
874 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
875 t.Errorf("bar source jar args %q is not %q", w, g)
876 }
877
878 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
879 t.Errorf("bar resource jar args %q is not %q", w, g)
880 }
881}
882
Colin Cross54190b32017-10-09 15:34:10 -0700883func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700884 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700885 java_library {
886 name: "foo",
887 srcs: [
888 "a*.java",
889 ":gen",
890 "b*.java",
891 ],
892 }
893
894 genrule {
895 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800896 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700897 out: ["gen.java"],
898 }
Colin Cross238c1f32020-06-07 16:58:18 -0700899 `, map[string][]byte{
900 "a.java": nil,
901 "b.java": nil,
902 })
Colin Cross54190b32017-10-09 15:34:10 -0700903
904 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
905 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
906
Colin Cross15e86d92017-10-20 15:07:08 -0700907 if filepath.Base(genrule.Output.String()) != "gen.java" {
908 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700909 }
910
911 if len(javac.Inputs) != 3 ||
912 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700913 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700914 javac.Inputs[2].String() != "b.java" {
915 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
916 }
917}
918
Nan Zhang61eaedb2017-11-02 13:28:15 -0700919func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700920 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700921 java_library {
922 name: "foo",
923 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900924 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700925 }
926
927 java_library {
928 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800929 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700930 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900931 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700932 }
933
934 java_library {
935 name: "baz",
936 srcs: ["c.java"],
937 libs: ["bar"],
938 sdk_version: "14",
939 }
940 `)
941
942 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
943 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
944 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
945 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
946 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
947
948 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
949 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
950 }
951
952 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
953 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
954 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
955 }
956 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
957 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
958 }
959 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
960 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
961 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100962 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700963 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100964 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700965 }
966}
967
968func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700969 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700970 java_library {
971 name: "bar",
972 srcs: ["a.java","b.java","c.java"],
973 javac_shard_size: 1
974 }
975 `)
976
977 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
978 for i := 0; i < 3; i++ {
979 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
980 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
981 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
982 }
983 }
984}
985
Nan Zhang581fd212018-01-10 16:06:12 -0800986func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700987 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +0000988 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -0800989 name: "droiddoc-templates-sdk",
990 path: ".",
991 }
Jiyong Park29074592019-07-07 16:27:47 +0900992 filegroup {
993 name: "bar-doc-aidl-srcs",
994 srcs: ["bar-doc/IBar.aidl"],
995 path: "bar-doc",
996 }
Nan Zhang581fd212018-01-10 16:06:12 -0800997 droiddoc {
998 name: "bar-doc",
999 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001000 "bar-doc/a.java",
Jiyong Park1e440682018-05-23 18:42:04 +09001001 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +09001002 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -08001003 ],
1004 exclude_srcs: [
1005 "bar-doc/b.java"
1006 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001007 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001008 hdf: [
1009 "android.whichdoc offline",
1010 ],
1011 knowntags: [
1012 "bar-doc/known_oj_tags.txt",
1013 ],
1014 proofread_file: "libcore-proofread.txt",
1015 todo_file: "libcore-docs-todo.html",
1016 args: "-offlinemode -title \"libcore\"",
1017 }
Colin Cross238c1f32020-06-07 16:58:18 -07001018 `,
1019 map[string][]byte{
1020 "bar-doc/a.java": nil,
1021 "bar-doc/b.java": nil,
1022 })
Nan Zhang581fd212018-01-10 16:06:12 -08001023
Colin Crossc0806172019-06-14 18:51:47 -07001024 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +09001025 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -07001026 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +09001027 javaSrcs = append(javaSrcs, i.Base())
1028 }
Colin Crossc0806172019-06-14 18:51:47 -07001029 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
1030 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +09001031 }
1032
Colin Crossc0806172019-06-14 18:51:47 -07001033 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
1034 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
1035 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1036 }
1037
1038 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1039 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001040 }
Nan Zhang581fd212018-01-10 16:06:12 -08001041}
1042
Paul Duffin83a2d962019-11-19 19:44:10 +00001043func TestDroidstubsWithSystemModules(t *testing.T) {
1044 ctx, _ := testJava(t, `
1045 droidstubs {
1046 name: "stubs-source-system-modules",
1047 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001048 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001049 ],
1050 sdk_version: "none",
1051 system_modules: "source-system-modules",
1052 }
1053
1054 java_library {
1055 name: "source-jar",
1056 srcs: [
1057 "a.java",
1058 ],
1059 }
1060
1061 java_system_modules {
1062 name: "source-system-modules",
1063 libs: ["source-jar"],
1064 }
1065
1066 droidstubs {
1067 name: "stubs-prebuilt-system-modules",
1068 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001069 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001070 ],
1071 sdk_version: "none",
1072 system_modules: "prebuilt-system-modules",
1073 }
1074
1075 java_import {
1076 name: "prebuilt-jar",
1077 jars: ["a.jar"],
1078 }
1079
1080 java_system_modules_import {
1081 name: "prebuilt-system-modules",
1082 libs: ["prebuilt-jar"],
1083 }
1084 `)
1085
1086 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1087
1088 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1089}
1090
1091func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1092 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1093 var systemJars []string
1094 for _, i := range metalavaRule.Implicits {
1095 systemJars = append(systemJars, i.Base())
1096 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001097 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001098 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1099 }
1100}
1101
Colin Cross54250902017-12-05 09:28:08 -08001102func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001103 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001104 java_library {
1105 name: "foo",
1106 srcs: ["a.java"],
1107 }
1108
1109 java_genrule {
1110 name: "jargen",
1111 tool_files: ["b.java"],
1112 cmd: "$(location b.java) $(in) $(out)",
1113 out: ["jargen.jar"],
1114 srcs: [":foo"],
1115 }
1116
1117 java_library {
1118 name: "bar",
1119 static_libs: ["jargen"],
1120 srcs: ["c.java"],
1121 }
1122
1123 java_library {
1124 name: "baz",
1125 libs: ["jargen"],
1126 srcs: ["c.java"],
1127 }
1128 `)
1129
1130 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1131 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1132 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1133 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1134 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1135
1136 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1137 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1138 }
1139
1140 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1141 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1142 }
1143
1144 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1145 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1146 }
1147
1148 if len(barCombined.Inputs) != 2 ||
1149 barCombined.Inputs[0].String() != bar.Output.String() ||
1150 barCombined.Inputs[1].String() != jargen.Output.String() {
1151 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1152 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1153 }
1154}
1155
Nan Zhang27e284d2018-02-09 21:03:53 +00001156func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001157 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001158 java_library {
1159 name: "foo",
1160 srcs: ["a.java", ":foo-srcs"],
1161 exclude_srcs: ["a.java", ":foo-excludes"],
1162 }
1163
1164 filegroup {
1165 name: "foo-srcs",
1166 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1167 }
1168
1169 filegroup {
1170 name: "foo-excludes",
1171 srcs: ["java-fg/a.java", "java-fg/b.java"],
1172 }
1173 `)
1174
1175 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1176
1177 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1178 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1179 }
1180}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001181
Paul Duffin52d398a2019-06-11 12:31:14 +01001182func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001183 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001184 "libcore/Android.bp": []byte(`
1185 java_library {
1186 name: "core",
1187 sdk_version: "none",
1188 system_modules: "none",
1189 }`),
1190 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001191 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001192 run(t, ctx, config)
1193}
1194
Jiyong Parkc678ad32018-04-10 13:07:10 +09001195func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001196 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001197 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001198 name: "droiddoc-templates-sdk",
1199 path: ".",
1200 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001201 java_sdk_library {
1202 name: "foo",
1203 srcs: ["a.java", "b.java"],
1204 api_packages: ["foo"],
1205 }
1206 java_sdk_library {
1207 name: "bar",
1208 srcs: ["a.java", "b.java"],
1209 api_packages: ["bar"],
1210 }
1211 java_library {
1212 name: "baz",
1213 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001214 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001215 sdk_version: "system_current",
1216 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001217 java_sdk_library {
1218 name: "barney",
1219 srcs: ["c.java"],
1220 api_only: true,
1221 }
1222 java_sdk_library {
1223 name: "betty",
1224 srcs: ["c.java"],
1225 shared_library: false,
1226 }
Paul Duffin859fe962020-05-15 10:20:31 +01001227 java_sdk_library_import {
1228 name: "quuz",
1229 public: {
1230 jars: ["c.jar"],
1231 },
1232 }
1233 java_sdk_library_import {
1234 name: "fred",
1235 public: {
1236 jars: ["b.jar"],
1237 },
1238 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001239 java_sdk_library_import {
1240 name: "wilma",
1241 public: {
1242 jars: ["b.jar"],
1243 },
1244 shared_library: false,
1245 }
Jiyong Park1be96912018-05-28 18:02:19 +09001246 java_library {
1247 name: "qux",
1248 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001249 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001250 sdk_version: "system_current",
1251 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001252 java_library {
1253 name: "baz-test",
1254 srcs: ["c.java"],
1255 libs: ["foo"],
1256 sdk_version: "test_current",
1257 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001258 java_library {
1259 name: "baz-29",
1260 srcs: ["c.java"],
1261 libs: ["foo"],
1262 sdk_version: "system_29",
1263 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001264 `)
1265
1266 // check the existence of the internal modules
1267 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001268 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1269 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1270 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1271 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1272 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1273 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001274 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001275 ctx.ModuleForTests("foo.api.public.28", "")
1276 ctx.ModuleForTests("foo.api.system.28", "")
1277 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001278
1279 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1280 // tests if baz is actually linked to the stubs lib
1281 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1282 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1283 "foo.stubs.system.jar")
1284 }
1285 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001286 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001287 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001288 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001289 }
1290 // test if baz is not linked to the system variant of foo
1291 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1292 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1293 "foo.stubs.jar")
1294 }
Jiyong Park1be96912018-05-28 18:02:19 +09001295
Paul Duffin726d23c2020-01-22 16:30:37 +00001296 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1297 // tests if baz-test is actually linked to the test stubs lib
1298 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1299 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1300 "foo.stubs.test.jar")
1301 }
1302
Paul Duffina2db18f2020-01-22 17:11:15 +00001303 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1304 // tests if baz-29 is actually linked to the system 29 stubs lib
1305 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1306 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1307 "prebuilts/sdk/29/system/foo.jar")
1308 }
1309
Jiyong Park1be96912018-05-28 18:02:19 +09001310 // test if baz has exported SDK lib names foo and bar to qux
1311 qux := ctx.ModuleForTests("qux", "android_common")
1312 if quxLib, ok := qux.Module().(*Library); ok {
1313 sdkLibs := quxLib.ExportedSdkLibs()
Paul Duffin859fe962020-05-15 10:20:31 +01001314 sort.Strings(sdkLibs)
1315 if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
1316 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001317 }
1318 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001319}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001320
Paul Duffindaaa3322020-05-26 18:13:57 +01001321func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1322 ctx, _ := testJava(t, `
1323 java_sdk_library {
1324 name: "foo",
1325 srcs: ["a.java"],
1326 api_only: true,
1327 public: {
1328 enabled: true,
1329 },
1330 }
1331
1332 java_library {
1333 name: "bar",
1334 srcs: ["b.java"],
1335 libs: ["foo"],
1336 }
1337 `)
1338
1339 // The bar library should depend on the stubs jar.
1340 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1341 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1342 t.Errorf("expected %q, found %#q", expected, actual)
1343 }
1344}
1345
Paul Duffin46dc45a2020-05-14 15:39:10 +01001346func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1347 testJava(t, `
1348 java_sdk_library {
1349 name: "foo",
1350 srcs: ["a.java"],
1351 api_packages: ["foo"],
1352 public: {
1353 enabled: true,
1354 },
1355 }
1356
1357 java_library {
1358 name: "bar",
1359 srcs: ["b.java", ":foo{.public.stubs.source}"],
1360 }
1361 `)
1362}
1363
1364func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1365 testJavaError(t, `"foo" does not provide api scope system`, `
1366 java_sdk_library {
1367 name: "foo",
1368 srcs: ["a.java"],
1369 api_packages: ["foo"],
1370 public: {
1371 enabled: true,
1372 },
1373 }
1374
1375 java_library {
1376 name: "bar",
1377 srcs: ["b.java", ":foo{.system.stubs.source}"],
1378 }
1379 `)
1380}
1381
1382func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1383 testJava(t, `
1384 java_sdk_library_import {
1385 name: "foo",
1386 public: {
1387 jars: ["a.jar"],
1388 stub_srcs: ["a.java"],
1389 current_api: "api/current.txt",
1390 removed_api: "api/removed.txt",
1391 },
1392 }
1393
1394 java_library {
1395 name: "bar",
1396 srcs: [":foo{.public.stubs.source}"],
1397 java_resources: [
1398 ":foo{.public.api.txt}",
1399 ":foo{.public.removed-api.txt}",
1400 ],
1401 }
1402 `)
1403}
1404
1405func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1406 bp := `
1407 java_sdk_library_import {
1408 name: "foo",
1409 public: {
1410 jars: ["a.jar"],
1411 },
1412 }
1413 `
1414
1415 t.Run("stubs.source", func(t *testing.T) {
1416 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1417 java_library {
1418 name: "bar",
1419 srcs: [":foo{.public.stubs.source}"],
1420 java_resources: [
1421 ":foo{.public.api.txt}",
1422 ":foo{.public.removed-api.txt}",
1423 ],
1424 }
1425 `)
1426 })
1427
1428 t.Run("api.txt", func(t *testing.T) {
1429 testJavaError(t, `api.txt not available for api scope public`, bp+`
1430 java_library {
1431 name: "bar",
1432 srcs: ["a.java"],
1433 java_resources: [
1434 ":foo{.public.api.txt}",
1435 ],
1436 }
1437 `)
1438 })
1439
1440 t.Run("removed-api.txt", func(t *testing.T) {
1441 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1442 java_library {
1443 name: "bar",
1444 srcs: ["a.java"],
1445 java_resources: [
1446 ":foo{.public.removed-api.txt}",
1447 ],
1448 }
1449 `)
1450 })
1451}
1452
Paul Duffin3375e352020-04-28 10:44:03 +01001453func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1454 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1455 java_sdk_library {
1456 name: "foo",
1457 srcs: ["a.java", "b.java"],
1458 api_packages: ["foo"],
1459 // Explicitly disable public to test the check that ensures the set of enabled
1460 // scopes is consistent.
1461 public: {
1462 enabled: false,
1463 },
1464 system: {
1465 enabled: true,
1466 },
1467 }
1468 `)
1469}
1470
Paul Duffin87a05a32020-05-12 11:50:28 +01001471func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1472 testJava(t, `
1473 java_sdk_library {
1474 name: "foo",
1475 srcs: ["a.java", "b.java"],
1476 api_packages: ["foo"],
1477 system: {
1478 enabled: true,
1479 sdk_version: "module_current",
1480 },
1481 }
1482 `)
1483}
1484
Paul Duffin0c5bae52020-06-02 13:00:08 +01001485func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
1486 testJava(t, `
1487 java_sdk_library {
1488 name: "foo",
1489 srcs: ["a.java", "b.java"],
1490 api_packages: ["foo"],
1491 system: {
1492 enabled: true,
1493 },
1494 module_lib: {
1495 enabled: true,
1496 },
1497 }
1498 `)
1499}
1500
1501func TestJavaSdkLibrary_SystemServer(t *testing.T) {
1502 testJava(t, `
1503 java_sdk_library {
1504 name: "foo",
1505 srcs: ["a.java", "b.java"],
1506 api_packages: ["foo"],
1507 system: {
1508 enabled: true,
1509 },
1510 system_server: {
1511 enabled: true,
1512 },
1513 }
1514 `)
1515}
1516
Paul Duffin803a9562020-05-20 11:52:25 +01001517func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1518 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1519 java_sdk_library {
1520 name: "foo",
1521 srcs: ["a.java"],
1522 public: {
1523 enabled: false,
1524 },
1525 }
1526
1527 java_library {
1528 name: "baz",
1529 srcs: ["a.java"],
1530 libs: ["foo"],
1531 sdk_version: "module_current",
1532 }
1533 `)
1534}
1535
1536func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1537 testJava(t, `
1538 java_sdk_library {
1539 name: "foo",
1540 srcs: ["a.java"],
1541 system: {
1542 enabled: true,
1543 },
1544 }
1545
1546 java_library {
1547 name: "baz",
1548 srcs: ["a.java"],
1549 libs: ["foo"],
1550 // foo does not have module-lib scope so it should fallback to system
1551 sdk_version: "module_current",
1552 }
1553 `)
1554}
1555
Jiyong Park932cdfe2020-05-28 00:19:53 +09001556func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1557 ctx, _ := testJava(t, `
1558 java_sdk_library {
1559 name: "foo",
1560 srcs: ["a.java"],
1561 system: {
1562 enabled: true,
1563 },
1564 default_to_stubs: true,
1565 }
1566
1567 java_library {
1568 name: "baz",
1569 srcs: ["a.java"],
1570 libs: ["foo"],
1571 // does not have sdk_version set, should fallback to module,
1572 // which will then fallback to system because the module scope
1573 // is not enabled.
1574 }
1575 `)
1576 // The baz library should depend on the system stubs jar.
1577 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1578 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1579 t.Errorf("expected %q, found %#q", expected, actual)
1580 }
1581}
1582
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001583var compilerFlagsTestCases = []struct {
1584 in string
1585 out bool
1586}{
1587 {
1588 in: "a",
1589 out: false,
1590 },
1591 {
1592 in: "-a",
1593 out: true,
1594 },
1595 {
1596 in: "-no-jdk",
1597 out: false,
1598 },
1599 {
1600 in: "-no-stdlib",
1601 out: false,
1602 },
1603 {
1604 in: "-kotlin-home",
1605 out: false,
1606 },
1607 {
1608 in: "-kotlin-home /some/path",
1609 out: false,
1610 },
1611 {
1612 in: "-include-runtime",
1613 out: false,
1614 },
1615 {
1616 in: "-Xintellij-plugin-root",
1617 out: false,
1618 },
1619}
1620
1621type mockContext struct {
1622 android.ModuleContext
1623 result bool
1624}
1625
1626func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1627 // CheckBadCompilerFlags calls this function when the flag should be rejected
1628 ctx.result = false
1629}
1630
1631func TestCompilerFlags(t *testing.T) {
1632 for _, testCase := range compilerFlagsTestCases {
1633 ctx := &mockContext{result: true}
1634 CheckKotlincFlags(ctx, []string{testCase.in})
1635 if ctx.result != testCase.out {
1636 t.Errorf("incorrect output:")
1637 t.Errorf(" input: %#v", testCase.in)
1638 t.Errorf(" expected: %#v", testCase.out)
1639 t.Errorf(" got: %#v", ctx.result)
1640 }
1641 }
1642}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001643
1644// TODO(jungjw): Consider making this more robust by ignoring path order.
1645func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1646 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1647 flags := strings.Split(variables["javacFlags"], " ")
1648 got := ""
1649 for _, flag := range flags {
1650 keyEnd := strings.Index(flag, "=")
1651 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1652 got = flag[keyEnd+1:]
1653 break
1654 }
1655 }
1656 if expected != got {
1657 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1658 }
1659}
1660
1661func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001662 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001663 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001664 bp := `
1665 java_library {
1666 name: "foo",
1667 srcs: ["a.java"],
1668 java_version: "1.8",
1669 }
1670
1671 java_library {
1672 name: "bar",
1673 srcs: ["b.java"],
1674 sdk_version: "none",
1675 system_modules: "none",
1676 patch_module: "java.base",
1677 java_version: "1.8",
1678 }
1679
1680 java_library {
1681 name: "baz",
1682 srcs: ["c.java"],
1683 patch_module: "java.base",
1684 java_version: "1.8",
1685 }
1686 `
1687 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001688
1689 checkPatchModuleFlag(t, ctx, "foo", "")
1690 checkPatchModuleFlag(t, ctx, "bar", "")
1691 checkPatchModuleFlag(t, ctx, "baz", "")
1692 })
1693
Pete Gillin0c2143e2019-05-02 15:32:11 +01001694 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001695 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001696 bp := `
1697 java_library {
1698 name: "foo",
1699 srcs: ["a.java"],
1700 }
1701
1702 java_library {
1703 name: "bar",
1704 srcs: ["b.java"],
1705 sdk_version: "none",
1706 system_modules: "none",
1707 patch_module: "java.base",
1708 }
1709
1710 java_library {
1711 name: "baz",
1712 srcs: ["c.java"],
1713 patch_module: "java.base",
1714 }
1715 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001716 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001717
1718 checkPatchModuleFlag(t, ctx, "foo", "")
1719 expected := "java.base=.:" + buildDir
1720 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09001721 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001722 checkPatchModuleFlag(t, ctx, "baz", expected)
1723 })
1724}
Paul Duffina7b9f422020-01-10 17:12:18 +00001725
1726func TestJavaSystemModules(t *testing.T) {
1727 ctx, _ := testJava(t, `
1728 java_system_modules {
1729 name: "system-modules",
1730 libs: ["system-module1", "system-module2"],
1731 }
1732 java_library {
1733 name: "system-module1",
1734 srcs: ["a.java"],
1735 sdk_version: "none",
1736 system_modules: "none",
1737 }
1738 java_library {
1739 name: "system-module2",
1740 srcs: ["b.java"],
1741 sdk_version: "none",
1742 system_modules: "none",
1743 }
1744 `)
1745
1746 // check the existence of the module
1747 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1748
1749 cmd := systemModules.Rule("jarsTosystemModules")
1750
1751 // make sure the command compiles against the supplied modules.
1752 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1753 if !strings.Contains(cmd.Args["classpath"], module) {
1754 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1755 module)
1756 }
1757 }
1758}
Paul Duffin90169ba2020-01-10 17:16:44 +00001759
1760func TestJavaSystemModulesImport(t *testing.T) {
1761 ctx, _ := testJava(t, `
1762 java_system_modules_import {
1763 name: "system-modules",
1764 libs: ["system-module1", "system-module2"],
1765 }
1766 java_import {
1767 name: "system-module1",
1768 jars: ["a.jar"],
1769 }
1770 java_import {
1771 name: "system-module2",
1772 jars: ["b.jar"],
1773 }
1774 `)
1775
1776 // check the existence of the module
1777 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1778
1779 cmd := systemModules.Rule("jarsTosystemModules")
1780
1781 // make sure the command compiles against the supplied modules.
1782 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1783 if !strings.Contains(cmd.Args["classpath"], module) {
1784 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1785 module)
1786 }
1787 }
1788}
Paul Duffin83a2d962019-11-19 19:44:10 +00001789
1790func TestJavaLibraryWithSystemModules(t *testing.T) {
1791 ctx, _ := testJava(t, `
1792 java_library {
1793 name: "lib-with-source-system-modules",
1794 srcs: [
1795 "a.java",
1796 ],
1797 sdk_version: "none",
1798 system_modules: "source-system-modules",
1799 }
1800
1801 java_library {
1802 name: "source-jar",
1803 srcs: [
1804 "a.java",
1805 ],
1806 }
1807
1808 java_system_modules {
1809 name: "source-system-modules",
1810 libs: ["source-jar"],
1811 }
1812
1813 java_library {
1814 name: "lib-with-prebuilt-system-modules",
1815 srcs: [
1816 "a.java",
1817 ],
1818 sdk_version: "none",
1819 system_modules: "prebuilt-system-modules",
1820 }
1821
1822 java_import {
1823 name: "prebuilt-jar",
1824 jars: ["a.jar"],
1825 }
1826
1827 java_system_modules_import {
1828 name: "prebuilt-system-modules",
1829 libs: ["prebuilt-jar"],
1830 }
1831 `)
1832
1833 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
1834
1835 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
1836}
1837
1838func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
1839 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
1840 bootClasspath := javacRule.Args["bootClasspath"]
1841 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
1842 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
1843 }
1844}
Jiyong Park19604de2020-03-24 16:44:11 +09001845
1846func TestAidlExportIncludeDirsFromImports(t *testing.T) {
1847 ctx, _ := testJava(t, `
1848 java_library {
1849 name: "foo",
1850 srcs: ["aidl/foo/IFoo.aidl"],
1851 libs: ["bar"],
1852 }
1853
1854 java_import {
1855 name: "bar",
1856 jars: ["a.jar"],
1857 aidl: {
1858 export_include_dirs: ["aidl/bar"],
1859 },
1860 }
1861 `)
1862
1863 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
1864 expectedAidlFlag := "-Iaidl/bar"
1865 if !strings.Contains(aidlCommand, expectedAidlFlag) {
1866 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
1867 }
1868}