blob: 73e6792b4d7bd72dbef9fd27aeb6bb5af3685b5a [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"
Liz Kammerdd849a82020-06-12 16:38:45 -070034 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070035)
36
37var buildDir string
38
39func setUp() {
40 var err error
41 buildDir, err = ioutil.TempDir("", "soong_java_test")
42 if err != nil {
43 panic(err)
44 }
45}
46
47func tearDown() {
48 os.RemoveAll(buildDir)
49}
50
51func TestMain(m *testing.M) {
52 run := func() int {
53 setUp()
54 defer tearDown()
55
56 return m.Run()
57 }
58
59 os.Exit(run())
60}
Colin Cross527012a2017-11-30 22:56:16 -080061
Colin Cross98be1bb2019-12-13 20:41:13 -080062func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000063 bp += dexpreopt.BpToolModulesForTest()
64
Martin Stjernholm75a48d82020-01-10 20:32:59 +000065 config := TestConfig(buildDir, env, bp, fs)
66
67 // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that
68 // it doesn't create a real one, which would fail.
69 _ = dexpreopt.GlobalSoongConfigForTests(config)
70
71 return config
Colin Cross1369cdb2017-09-29 17:58:17 -070072}
73
Colin Cross98be1bb2019-12-13 20:41:13 -080074func testContext() *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070075
Colin Cross4c428df2017-09-15 17:36:05 -070076 ctx := android.NewTestArchContext()
Paul Duffinf9b1da02019-12-18 19:51:55 +000077 RegisterJavaBuildComponents(ctx)
78 RegisterAppBuildComponents(ctx)
79 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000080 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000081 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080082 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080083 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
84 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070085 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000086 RegisterDocsBuildComponents(ctx)
87 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000088 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070089 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010090 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000091
92 RegisterPrebuiltApisBuildComponents(ctx)
93
Liz Kammerdd849a82020-06-12 16:38:45 -070094 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070095 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080096 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070097 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070098
99 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000100 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700101
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000102 dexpreopt.RegisterToolModulesForTest(ctx)
103
Colin Cross527012a2017-11-30 22:56:16 -0800104 return ctx
105}
106
107func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800108 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800109
Colin Cross98be1bb2019-12-13 20:41:13 -0800110 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000111 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800112
Colin Cross98be1bb2019-12-13 20:41:13 -0800113 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100114 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800115 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700116 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800117 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800118}
119
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900120func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900121 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800122 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900123}
124
Colin Cross98be1bb2019-12-13 20:41:13 -0800125func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900126 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800127 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900128
Colin Cross98be1bb2019-12-13 20:41:13 -0800129 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000130 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900131
Colin Cross98be1bb2019-12-13 20:41:13 -0800132 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900133 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
134 if len(errs) > 0 {
135 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900136 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900137 }
138 _, errs = ctx.PrepareBuildActions(config)
139 if len(errs) > 0 {
140 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900141 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900142 }
143
144 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900145
146 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900147}
148
Colin Cross238c1f32020-06-07 16:58:18 -0700149func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
150 t.Helper()
151 return testJavaWithConfig(t, testConfig(nil, bp, fs))
152}
153
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700154func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800155 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700156 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900157}
158
Colin Cross98be1bb2019-12-13 20:41:13 -0800159func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900160 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800161 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800162 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700163
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700164 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700165}
166
Colin Cross2acdae82017-09-15 19:44:24 -0700167func moduleToPath(name string) string {
168 switch {
169 case name == `""`:
170 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700171 case strings.HasSuffix(name, ".jar"):
172 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700173 default:
174 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700175 }
176}
177
Jeongik Chae403e9e2019-12-07 00:16:24 +0900178func TestJavaLinkType(t *testing.T) {
179 testJava(t, `
180 java_library {
181 name: "foo",
182 srcs: ["a.java"],
183 libs: ["bar"],
184 static_libs: ["baz"],
185 }
186
187 java_library {
188 name: "bar",
189 sdk_version: "current",
190 srcs: ["b.java"],
191 }
192
193 java_library {
194 name: "baz",
195 sdk_version: "system_current",
196 srcs: ["c.java"],
197 }
198 `)
199
200 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.", `
201 java_library {
202 name: "foo",
203 srcs: ["a.java"],
204 libs: ["bar"],
205 sdk_version: "current",
206 static_libs: ["baz"],
207 }
208
209 java_library {
210 name: "bar",
211 sdk_version: "current",
212 srcs: ["b.java"],
213 }
214
215 java_library {
216 name: "baz",
217 sdk_version: "system_current",
218 srcs: ["c.java"],
219 }
220 `)
221
222 testJava(t, `
223 java_library {
224 name: "foo",
225 srcs: ["a.java"],
226 libs: ["bar"],
227 sdk_version: "system_current",
228 static_libs: ["baz"],
229 }
230
231 java_library {
232 name: "bar",
233 sdk_version: "current",
234 srcs: ["b.java"],
235 }
236
237 java_library {
238 name: "baz",
239 sdk_version: "system_current",
240 srcs: ["c.java"],
241 }
242 `)
243
244 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.", `
245 java_library {
246 name: "foo",
247 srcs: ["a.java"],
248 libs: ["bar"],
249 sdk_version: "system_current",
250 static_libs: ["baz"],
251 }
252
253 java_library {
254 name: "bar",
255 sdk_version: "current",
256 srcs: ["b.java"],
257 }
258
259 java_library {
260 name: "baz",
261 srcs: ["c.java"],
262 }
263 `)
264}
265
Colin Cross72bb3632017-07-13 16:23:21 -0700266func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700267 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700268 java_library {
269 name: "foo",
270 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700271 libs: ["bar"],
272 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700273 }
274
275 java_library {
276 name: "bar",
277 srcs: ["b.java"],
278 }
279
280 java_library {
281 name: "baz",
282 srcs: ["c.java"],
283 }
Colin Crossd5934c82017-10-02 13:55:26 -0700284 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700285
Colin Cross4c428df2017-09-15 17:36:05 -0700286 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700287 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700288
289 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
290 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
291 }
292
Colin Cross1ee23172017-10-18 14:44:18 -0700293 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700294 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
295 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700296
Nan Zhanged19fc32017-10-19 13:06:22 -0700297 if !strings.Contains(javac.Args["classpath"], barTurbine) {
298 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700299 }
300
Nan Zhanged19fc32017-10-19 13:06:22 -0700301 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
302 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700303 }
304
305 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
306 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700307 }
308}
309
Artur Satayev9cf46692019-11-26 18:08:34 +0000310func TestExportedPlugins(t *testing.T) {
311 type Result struct {
312 library string
313 processors string
314 }
315 var tests = []struct {
316 name string
317 extra string
318 results []Result
319 }{
320 {
321 name: "Exported plugin is not a direct plugin",
322 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
323 results: []Result{{library: "exports", processors: "-proc:none"}},
324 },
325 {
326 name: "Exports plugin to dependee",
327 extra: `
328 java_library{name: "exports", exported_plugins: ["plugin"]}
329 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
330 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
331 `,
332 results: []Result{
333 {library: "foo", processors: "-processor com.android.TestPlugin"},
334 {library: "bar", processors: "-processor com.android.TestPlugin"},
335 },
336 },
337 {
338 name: "Exports plugin to android_library",
339 extra: `
340 java_library{name: "exports", exported_plugins: ["plugin"]}
341 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
342 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
343 `,
344 results: []Result{
345 {library: "foo", processors: "-processor com.android.TestPlugin"},
346 {library: "bar", processors: "-processor com.android.TestPlugin"},
347 },
348 },
349 {
350 name: "Exports plugin is not propagated via transitive deps",
351 extra: `
352 java_library{name: "exports", exported_plugins: ["plugin"]}
353 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
354 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
355 `,
356 results: []Result{
357 {library: "foo", processors: "-processor com.android.TestPlugin"},
358 {library: "bar", processors: "-proc:none"},
359 },
360 },
361 {
362 name: "Exports plugin appends to plugins",
363 extra: `
364 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
365 java_library{name: "exports", exported_plugins: ["plugin"]}
366 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
367 `,
368 results: []Result{
369 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
370 },
371 },
372 }
373
374 for _, test := range tests {
375 t.Run(test.name, func(t *testing.T) {
376 ctx, _ := testJava(t, `
377 java_plugin {
378 name: "plugin",
379 processor_class: "com.android.TestPlugin",
380 }
381 `+test.extra)
382
383 for _, want := range test.results {
384 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
385 if javac.Args["processor"] != want.processors {
386 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
387 }
388 }
389 })
390 }
391}
392
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900393func TestSdkVersionByPartition(t *testing.T) {
394 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900395 java_library {
396 name: "foo",
397 srcs: ["a.java"],
398 vendor: true,
399 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900400 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900401
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900402 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900403 java_library {
404 name: "bar",
405 srcs: ["b.java"],
406 }
407 `)
408
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900409 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900410 bp := `
411 java_library {
412 name: "foo",
413 srcs: ["a.java"],
414 product_specific: true,
415 }
416 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800417
418 config := testConfig(nil, bp, nil)
419 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900420 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800421 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 +0900422 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800423 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900424 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900425 }
426}
427
Colin Crossd5934c82017-10-02 13:55:26 -0700428func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700429 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700430 java_library {
431 name: "foo",
432 srcs: ["a.java"],
433 target: {
434 android: {
435 srcs: ["b.java"],
436 },
437 },
438 }
439 `)
440
441 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
442 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
443 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
444 }
445}
446
Colin Cross6b4a32d2017-12-05 13:42:45 -0800447func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700448 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800449 java_library_host {
450 name: "foo",
451 srcs: ["a.java"],
452 }
453
454 java_binary_host {
455 name: "bar",
456 srcs: ["b.java"],
457 static_libs: ["foo"],
458 }
459 `)
460
461 buildOS := android.BuildOs.String()
462
463 bar := ctx.ModuleForTests("bar", buildOS+"_common")
464 barJar := bar.Output("bar.jar").Output.String()
465 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
466 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
467
468 // Test that the install binary wrapper depends on the installed jar file
469 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
470 t.Errorf("expected binary wrapper implicits [%q], got %v",
471 barJar, barWrapperDeps)
472 }
Alex Humesky2070e322020-06-09 20:23:08 -0400473}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800474
Alex Humesky2070e322020-06-09 20:23:08 -0400475func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
476 bp := `
477 java_library {
478 name: "target_library",
479 srcs: ["a.java"],
480 }
481
482 java_binary_host {
483 name: "host_binary",
484 srcs: ["b.java"],
485 }
486 `
487 config := testConfig(nil, bp, nil)
488 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
489
490 ctx, _ := testJavaWithConfig(t, config)
491
492 // first, sanity check that the -g flag is added to target modules
493 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
494 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
495 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
496 t.Errorf("target library javac flags %v should contain "+
497 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
498 }
499
500 // check that -g is not overridden for host modules
501 buildOS := android.BuildOs.String()
502 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
503 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
504 if strings.Contains(hostJavaFlags, "-g:source,lines") {
505 t.Errorf("java_binary_host javac flags %v should not have "+
506 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
507 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800508}
509
Colin Cross72bb3632017-07-13 16:23:21 -0700510func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700511 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700512 java_library {
513 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000514 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000515 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700516 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700517 }
518
Colin Cross74d73e22017-08-02 11:05:49 -0700519 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700520 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700521 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700522 }
523
Colin Cross74d73e22017-08-02 11:05:49 -0700524 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700525 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700526 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700527 }
Colin Cross42be7612019-02-21 18:12:14 -0800528
529 dex_import {
530 name: "qux",
531 jars: ["b.jar"],
532 }
Colin Cross79c7c262019-04-17 11:11:46 -0700533
534 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000535 name: "sdklib",
536 public: {
537 jars: ["c.jar"],
538 },
539 }
540
Paul Duffin91547182019-11-12 19:39:36 +0000541 prebuilt_stubs_sources {
542 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000543 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000544 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000545
546 java_test_import {
547 name: "test",
548 jars: ["a.jar"],
549 test_suites: ["cts"],
550 test_config: "AndroidTest.xml",
551 }
Colin Cross72bb3632017-07-13 16:23:21 -0700552 `)
553
Paul Duffin9b478b02019-12-10 13:41:51 +0000554 fooModule := ctx.ModuleForTests("foo", "android_common")
555 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700556 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700557 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
558 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700559 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700560
Paul Duffin9b478b02019-12-10 13:41:51 +0000561 fooLibrary := fooModule.Module().(*Library)
562 assertDeepEquals(t, "foo java sources incorrect",
563 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000564
Paul Duffin9b478b02019-12-10 13:41:51 +0000565 assertDeepEquals(t, "foo java source jars incorrect",
566 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
567 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000568
Colin Cross37f6d792018-07-12 12:28:41 -0700569 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
570 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700571 }
572
Colin Cross79c7c262019-04-17 11:11:46 -0700573 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
574 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
575 }
576
Colin Cross37f6d792018-07-12 12:28:41 -0700577 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
578 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700579 }
Colin Cross42be7612019-02-21 18:12:14 -0800580
581 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700582}
583
Paul Duffin9b478b02019-12-10 13:41:51 +0000584func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
585 if !reflect.DeepEqual(expected, actual) {
586 t.Errorf("%s: expected %q, found %q", message, expected, actual)
587 }
588}
589
Paul Duffin56d44902020-01-31 13:36:25 +0000590func TestJavaSdkLibraryImport(t *testing.T) {
591 ctx, _ := testJava(t, `
592 java_library {
593 name: "foo",
594 srcs: ["a.java"],
595 libs: ["sdklib"],
596 sdk_version: "current",
597 }
598
599 java_library {
600 name: "foo.system",
601 srcs: ["a.java"],
602 libs: ["sdklib"],
603 sdk_version: "system_current",
604 }
605
606 java_library {
607 name: "foo.test",
608 srcs: ["a.java"],
609 libs: ["sdklib"],
610 sdk_version: "test_current",
611 }
612
613 java_sdk_library_import {
614 name: "sdklib",
615 public: {
616 jars: ["a.jar"],
617 },
618 system: {
619 jars: ["b.jar"],
620 },
621 test: {
622 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100623 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000624 },
625 }
626 `)
627
628 for _, scope := range []string{"", ".system", ".test"} {
629 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
630 javac := fooModule.Rule("javac")
631
632 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
633 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
634 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
635 }
636 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100637
Paul Duffincee7e662020-07-09 17:32:57 +0100638 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100639 `prebuilt_sdklib.stubs`,
640 `prebuilt_sdklib.stubs.source.test`,
641 `prebuilt_sdklib.stubs.system`,
642 `prebuilt_sdklib.stubs.test`,
643 })
644}
645
646func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
647 ctx, _ := testJava(t, `
648 java_sdk_library {
649 name: "sdklib",
650 srcs: ["a.java"],
651 sdk_version: "none",
652 system_modules: "none",
653 public: {
654 enabled: true,
655 },
656 }
657
658 java_sdk_library_import {
659 name: "sdklib",
660 public: {
661 jars: ["a.jar"],
662 },
663 }
664 `)
665
Paul Duffincee7e662020-07-09 17:32:57 +0100666 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100667 `dex2oatd`,
668 `prebuilt_sdklib`,
669 `sdklib.impl`,
670 `sdklib.stubs`,
671 `sdklib.stubs.source`,
672 `sdklib.xml`,
673 })
674
Paul Duffincee7e662020-07-09 17:32:57 +0100675 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100676 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100677 `sdklib.impl`,
678 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
679 // dependency is added after prebuilts may have been renamed and so has to use
680 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100681 `sdklib.xml`,
682 })
683}
684
685func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
686 ctx, _ := testJava(t, `
687 java_sdk_library {
688 name: "sdklib",
689 srcs: ["a.java"],
690 sdk_version: "none",
691 system_modules: "none",
692 public: {
693 enabled: true,
694 },
695 }
696
697 java_sdk_library_import {
698 name: "sdklib",
699 prefer: true,
700 public: {
701 jars: ["a.jar"],
702 },
703 }
704 `)
705
Paul Duffincee7e662020-07-09 17:32:57 +0100706 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100707 `dex2oatd`,
708 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100709 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100710 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100711 `sdklib.stubs.source`,
712 `sdklib.xml`,
713 })
714
Paul Duffincee7e662020-07-09 17:32:57 +0100715 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100716 `prebuilt_sdklib.stubs`,
717 `sdklib.impl`,
718 `sdklib.xml`,
719 })
Paul Duffin56d44902020-01-31 13:36:25 +0000720}
721
Colin Cross89536d42017-07-07 14:35:50 -0700722func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700723 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700724 java_defaults {
725 name: "defaults",
726 srcs: ["a.java"],
727 libs: ["bar"],
728 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700729 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700730 }
731
732 java_library {
733 name: "foo",
734 defaults: ["defaults"],
735 }
736
737 java_library {
738 name: "bar",
739 srcs: ["b.java"],
740 }
741
742 java_library {
743 name: "baz",
744 srcs: ["c.java"],
745 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700746
747 android_test {
748 name: "atestOptimize",
749 defaults: ["defaults"],
750 optimize: {enabled: true},
751 }
752
753 android_test {
754 name: "atestNoOptimize",
755 defaults: ["defaults"],
756 }
757
758 android_test {
759 name: "atestDefault",
760 srcs: ["a.java"],
761 }
Colin Cross89536d42017-07-07 14:35:50 -0700762 `)
763
Colin Cross4c428df2017-09-15 17:36:05 -0700764 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700765 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700766
767 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
768 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
769 }
770
Nan Zhanged19fc32017-10-19 13:06:22 -0700771 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
772 if !strings.Contains(javac.Args["classpath"], barTurbine) {
773 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700774 }
775
Colin Cross1ee23172017-10-18 14:44:18 -0700776 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700777 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
778 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700779 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700780
781 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
782 if atestOptimize.Output == nil {
783 t.Errorf("atestOptimize should optimize APK")
784 }
785
786 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
787 if atestNoOptimize.Output == nil {
788 t.Errorf("atestNoOptimize should not optimize APK")
789 }
790
791 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
792 if atestDefault.Output == nil {
793 t.Errorf("atestDefault should optimize APK")
794 }
Colin Cross89536d42017-07-07 14:35:50 -0700795}
796
Colin Cross0f37af02017-09-27 17:42:05 -0700797func TestResources(t *testing.T) {
798 var table = []struct {
799 name string
800 prop string
801 extra string
802 args string
803 }{
804 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700805 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700806 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800807 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700808 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700809 },
810 {
811 // Test that a module with java_resources includes the files
812 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700813 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
814 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700815 },
816 {
817 // Test that a module with a filegroup in java_resources includes the files with the
818 // path prefix
819 name: "resource filegroup",
820 prop: `java_resources: [":foo-res"]`,
821 extra: `
822 filegroup {
823 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800824 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700825 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700826 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700827 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700828 },
829 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700830 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
831 name: "wildcard dirs",
832 prop: `java_resource_dirs: ["java-res/*"]`,
833 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
834 },
835 {
836 // Test that a module exclude_java_resource_dirs excludes the files
837 name: "wildcard dirs",
838 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
839 args: "-C java-res/a -f java-res/a/a",
840 },
Colin Crosscedd4762018-09-13 11:26:19 -0700841 {
842 // Test wildcards in java_resources
843 name: "wildcard files",
844 prop: `java_resources: ["java-res/**/*"]`,
845 args: "-C . -f java-res/a/a -f java-res/b/b",
846 },
847 {
848 // Test exclude_java_resources with java_resources
849 name: "wildcard files with exclude",
850 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
851 args: "-C . -f java-res/a/a",
852 },
853 {
854 // Test exclude_java_resources with java_resource_dirs
855 name: "resource dirs with exclude files",
856 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
857 args: "-C java-res -f java-res/a/a",
858 },
859 {
860 // Test exclude_java_resource_dirs with java_resource_dirs
861 name: "resource dirs with exclude files",
862 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
863 args: "-C java-res -f java-res/a/a -f java-res/b/b",
864 },
Colin Cross0f37af02017-09-27 17:42:05 -0700865 }
866
867 for _, test := range table {
868 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700869 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700870 java_library {
871 name: "foo",
872 srcs: [
873 "a.java",
874 "b.java",
875 "c.java",
876 ],
877 `+test.prop+`,
878 }
Colin Cross238c1f32020-06-07 16:58:18 -0700879 `+test.extra,
880 map[string][]byte{
881 "java-res/a/a": nil,
882 "java-res/b/b": nil,
883 "java-res2/a": nil,
884 },
885 )
Colin Cross0f37af02017-09-27 17:42:05 -0700886
Colin Cross331a1212018-08-15 20:40:52 -0700887 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700888 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700889
890 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
891 t.Errorf("foo combined jars %v does not contain %q",
892 foo.Inputs.Strings(), fooRes.Output.String())
893 }
894
Colin Crossaf9c55b2017-10-03 14:50:08 -0700895 if fooRes.Args["jarArgs"] != test.args {
896 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700897 fooRes.Args["jarArgs"], test.args)
898 }
899 })
900 }
901}
902
Colin Cross0c4ce212019-05-03 15:28:19 -0700903func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700904 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700905 java_library {
906 name: "foo",
907 srcs: [
908 "a.java",
909 "b.java",
910 "c.java",
911 ],
912 include_srcs: true,
913 }
914
915 java_library {
916 name: "bar",
917 srcs: [
918 "a.java",
919 "b.java",
920 "c.java",
921 ],
922 java_resource_dirs: ["java-res"],
923 include_srcs: true,
924 }
Colin Cross238c1f32020-06-07 16:58:18 -0700925 `, map[string][]byte{
926 "java-res/a/a": nil,
927 "java-res/b/b": nil,
928 "java-res2/a": nil,
929 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700930
931 // Test a library with include_srcs: true
932 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
933 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
934
935 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
936 t.Errorf("foo combined jars %v does not contain %q", w, g)
937 }
938
939 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
940 t.Errorf("foo source jar args %q is not %q", w, g)
941 }
942
943 // Test a library with include_srcs: true and resources
944 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
945 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
946 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
947 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
948
949 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
950 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
951 }
952
953 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
954 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
955 }
956
957 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
958 t.Errorf("bar combined jars %v does not contain %q", w, g)
959 }
960
961 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
962 t.Errorf("bar source jar args %q is not %q", w, g)
963 }
964
965 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
966 t.Errorf("bar resource jar args %q is not %q", w, g)
967 }
968}
969
Colin Cross54190b32017-10-09 15:34:10 -0700970func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700971 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700972 java_library {
973 name: "foo",
974 srcs: [
975 "a*.java",
976 ":gen",
977 "b*.java",
978 ],
979 }
980
981 genrule {
982 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800983 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700984 out: ["gen.java"],
985 }
Colin Cross238c1f32020-06-07 16:58:18 -0700986 `, map[string][]byte{
987 "a.java": nil,
988 "b.java": nil,
989 })
Colin Cross54190b32017-10-09 15:34:10 -0700990
991 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
992 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
993
Colin Cross15e86d92017-10-20 15:07:08 -0700994 if filepath.Base(genrule.Output.String()) != "gen.java" {
995 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700996 }
997
998 if len(javac.Inputs) != 3 ||
999 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001000 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001001 javac.Inputs[2].String() != "b.java" {
1002 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1003 }
1004}
1005
Nan Zhang61eaedb2017-11-02 13:28:15 -07001006func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001007 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001008 java_library {
1009 name: "foo",
1010 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001011 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001012 }
1013
1014 java_library {
1015 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001016 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001017 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001018 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001019 }
1020
1021 java_library {
1022 name: "baz",
1023 srcs: ["c.java"],
1024 libs: ["bar"],
1025 sdk_version: "14",
1026 }
1027 `)
1028
1029 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1030 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1031 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1032 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1033 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1034
1035 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1036 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1037 }
1038
1039 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1040 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1041 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1042 }
1043 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1044 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1045 }
1046 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1047 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1048 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001049 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001050 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001051 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001052 }
1053}
1054
1055func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001056 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001057 java_library {
1058 name: "bar",
1059 srcs: ["a.java","b.java","c.java"],
1060 javac_shard_size: 1
1061 }
1062 `)
1063
1064 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1065 for i := 0; i < 3; i++ {
1066 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1067 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1068 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1069 }
1070 }
1071}
1072
Nan Zhang581fd212018-01-10 16:06:12 -08001073func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001074 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001075 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001076 name: "droiddoc-templates-sdk",
1077 path: ".",
1078 }
Jiyong Park29074592019-07-07 16:27:47 +09001079 filegroup {
1080 name: "bar-doc-aidl-srcs",
1081 srcs: ["bar-doc/IBar.aidl"],
1082 path: "bar-doc",
1083 }
Nan Zhang581fd212018-01-10 16:06:12 -08001084 droiddoc {
1085 name: "bar-doc",
1086 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001087 "bar-doc/a.java",
Jiyong Park1e440682018-05-23 18:42:04 +09001088 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +09001089 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -08001090 ],
1091 exclude_srcs: [
1092 "bar-doc/b.java"
1093 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001094 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001095 hdf: [
1096 "android.whichdoc offline",
1097 ],
1098 knowntags: [
1099 "bar-doc/known_oj_tags.txt",
1100 ],
1101 proofread_file: "libcore-proofread.txt",
1102 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001103 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001104 }
Colin Cross238c1f32020-06-07 16:58:18 -07001105 `,
1106 map[string][]byte{
1107 "bar-doc/a.java": nil,
1108 "bar-doc/b.java": nil,
1109 })
Nan Zhang581fd212018-01-10 16:06:12 -08001110
Colin Crossc0806172019-06-14 18:51:47 -07001111 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +09001112 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -07001113 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +09001114 javaSrcs = append(javaSrcs, i.Base())
1115 }
Colin Crossc0806172019-06-14 18:51:47 -07001116 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
1117 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +09001118 }
1119
Colin Crossc0806172019-06-14 18:51:47 -07001120 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
1121 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
1122 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1123 }
1124
1125 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1126 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001127 }
Nan Zhang581fd212018-01-10 16:06:12 -08001128}
1129
Liz Kammer585cac22020-07-06 09:12:57 -07001130func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1131 testJavaError(t, "flags is set. Cannot set args", `
1132 droiddoc_exported_dir {
1133 name: "droiddoc-templates-sdk",
1134 path: ".",
1135 }
1136 filegroup {
1137 name: "bar-doc-aidl-srcs",
1138 srcs: ["bar-doc/IBar.aidl"],
1139 path: "bar-doc",
1140 }
1141 droiddoc {
1142 name: "bar-doc",
1143 srcs: [
1144 "bar-doc/a.java",
1145 "bar-doc/IFoo.aidl",
1146 ":bar-doc-aidl-srcs",
1147 ],
1148 exclude_srcs: [
1149 "bar-doc/b.java"
1150 ],
1151 custom_template: "droiddoc-templates-sdk",
1152 hdf: [
1153 "android.whichdoc offline",
1154 ],
1155 knowntags: [
1156 "bar-doc/known_oj_tags.txt",
1157 ],
1158 proofread_file: "libcore-proofread.txt",
1159 todo_file: "libcore-docs-todo.html",
1160 flags: ["-offlinemode -title \"libcore\""],
1161 args: "-offlinemode -title \"libcore\"",
1162 }
1163 `)
1164}
1165
Paul Duffin83a2d962019-11-19 19:44:10 +00001166func TestDroidstubsWithSystemModules(t *testing.T) {
1167 ctx, _ := testJava(t, `
1168 droidstubs {
1169 name: "stubs-source-system-modules",
1170 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001171 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001172 ],
1173 sdk_version: "none",
1174 system_modules: "source-system-modules",
1175 }
1176
1177 java_library {
1178 name: "source-jar",
1179 srcs: [
1180 "a.java",
1181 ],
1182 }
1183
1184 java_system_modules {
1185 name: "source-system-modules",
1186 libs: ["source-jar"],
1187 }
1188
1189 droidstubs {
1190 name: "stubs-prebuilt-system-modules",
1191 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001192 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001193 ],
1194 sdk_version: "none",
1195 system_modules: "prebuilt-system-modules",
1196 }
1197
1198 java_import {
1199 name: "prebuilt-jar",
1200 jars: ["a.jar"],
1201 }
1202
1203 java_system_modules_import {
1204 name: "prebuilt-system-modules",
1205 libs: ["prebuilt-jar"],
1206 }
1207 `)
1208
1209 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1210
1211 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1212}
1213
1214func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1215 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1216 var systemJars []string
1217 for _, i := range metalavaRule.Implicits {
1218 systemJars = append(systemJars, i.Base())
1219 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001220 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001221 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1222 }
1223}
1224
Colin Cross54250902017-12-05 09:28:08 -08001225func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001226 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001227 java_library {
1228 name: "foo",
1229 srcs: ["a.java"],
1230 }
1231
1232 java_genrule {
1233 name: "jargen",
1234 tool_files: ["b.java"],
1235 cmd: "$(location b.java) $(in) $(out)",
1236 out: ["jargen.jar"],
1237 srcs: [":foo"],
1238 }
1239
1240 java_library {
1241 name: "bar",
1242 static_libs: ["jargen"],
1243 srcs: ["c.java"],
1244 }
1245
1246 java_library {
1247 name: "baz",
1248 libs: ["jargen"],
1249 srcs: ["c.java"],
1250 }
1251 `)
1252
1253 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1254 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1255 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1256 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1257 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1258
1259 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1260 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1261 }
1262
1263 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1264 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1265 }
1266
1267 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1268 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1269 }
1270
1271 if len(barCombined.Inputs) != 2 ||
1272 barCombined.Inputs[0].String() != bar.Output.String() ||
1273 barCombined.Inputs[1].String() != jargen.Output.String() {
1274 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1275 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1276 }
1277}
1278
Nan Zhang27e284d2018-02-09 21:03:53 +00001279func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001280 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001281 java_library {
1282 name: "foo",
1283 srcs: ["a.java", ":foo-srcs"],
1284 exclude_srcs: ["a.java", ":foo-excludes"],
1285 }
1286
1287 filegroup {
1288 name: "foo-srcs",
1289 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1290 }
1291
1292 filegroup {
1293 name: "foo-excludes",
1294 srcs: ["java-fg/a.java", "java-fg/b.java"],
1295 }
1296 `)
1297
1298 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1299
1300 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1301 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1302 }
1303}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001304
Paul Duffin52d398a2019-06-11 12:31:14 +01001305func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001306 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001307 "libcore/Android.bp": []byte(`
1308 java_library {
1309 name: "core",
1310 sdk_version: "none",
1311 system_modules: "none",
1312 }`),
1313 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001314 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001315 run(t, ctx, config)
1316}
1317
Jiyong Parkc678ad32018-04-10 13:07:10 +09001318func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001319 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001320 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001321 name: "droiddoc-templates-sdk",
1322 path: ".",
1323 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001324 java_sdk_library {
1325 name: "foo",
1326 srcs: ["a.java", "b.java"],
1327 api_packages: ["foo"],
1328 }
1329 java_sdk_library {
1330 name: "bar",
1331 srcs: ["a.java", "b.java"],
1332 api_packages: ["bar"],
1333 }
1334 java_library {
1335 name: "baz",
1336 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001337 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001338 sdk_version: "system_current",
1339 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001340 java_sdk_library {
1341 name: "barney",
1342 srcs: ["c.java"],
1343 api_only: true,
1344 }
1345 java_sdk_library {
1346 name: "betty",
1347 srcs: ["c.java"],
1348 shared_library: false,
1349 }
Paul Duffin859fe962020-05-15 10:20:31 +01001350 java_sdk_library_import {
1351 name: "quuz",
1352 public: {
1353 jars: ["c.jar"],
1354 },
1355 }
1356 java_sdk_library_import {
1357 name: "fred",
1358 public: {
1359 jars: ["b.jar"],
1360 },
1361 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001362 java_sdk_library_import {
1363 name: "wilma",
1364 public: {
1365 jars: ["b.jar"],
1366 },
1367 shared_library: false,
1368 }
Jiyong Park1be96912018-05-28 18:02:19 +09001369 java_library {
1370 name: "qux",
1371 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001372 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001373 sdk_version: "system_current",
1374 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001375 java_library {
1376 name: "baz-test",
1377 srcs: ["c.java"],
1378 libs: ["foo"],
1379 sdk_version: "test_current",
1380 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001381 java_library {
1382 name: "baz-29",
1383 srcs: ["c.java"],
1384 libs: ["foo"],
1385 sdk_version: "system_29",
1386 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001387 `)
1388
1389 // check the existence of the internal modules
1390 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001391 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1392 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1393 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1394 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1395 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1396 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001397 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001398 ctx.ModuleForTests("foo.api.public.28", "")
1399 ctx.ModuleForTests("foo.api.system.28", "")
1400 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001401
1402 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1403 // tests if baz is actually linked to the stubs lib
1404 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1405 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1406 "foo.stubs.system.jar")
1407 }
1408 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001409 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001410 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001411 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001412 }
1413 // test if baz is not linked to the system variant of foo
1414 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1415 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1416 "foo.stubs.jar")
1417 }
Jiyong Park1be96912018-05-28 18:02:19 +09001418
Paul Duffin726d23c2020-01-22 16:30:37 +00001419 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1420 // tests if baz-test is actually linked to the test stubs lib
1421 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1422 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1423 "foo.stubs.test.jar")
1424 }
1425
Paul Duffina2db18f2020-01-22 17:11:15 +00001426 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1427 // tests if baz-29 is actually linked to the system 29 stubs lib
1428 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1429 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1430 "prebuilts/sdk/29/system/foo.jar")
1431 }
1432
Jiyong Park1be96912018-05-28 18:02:19 +09001433 // test if baz has exported SDK lib names foo and bar to qux
1434 qux := ctx.ModuleForTests("qux", "android_common")
1435 if quxLib, ok := qux.Module().(*Library); ok {
1436 sdkLibs := quxLib.ExportedSdkLibs()
Paul Duffin859fe962020-05-15 10:20:31 +01001437 sort.Strings(sdkLibs)
1438 if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
1439 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001440 }
1441 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001442}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001443
Paul Duffindaaa3322020-05-26 18:13:57 +01001444func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1445 ctx, _ := testJava(t, `
1446 java_sdk_library {
1447 name: "foo",
1448 srcs: ["a.java"],
1449 api_only: true,
1450 public: {
1451 enabled: true,
1452 },
1453 }
1454
1455 java_library {
1456 name: "bar",
1457 srcs: ["b.java"],
1458 libs: ["foo"],
1459 }
1460 `)
1461
1462 // The bar library should depend on the stubs jar.
1463 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1464 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1465 t.Errorf("expected %q, found %#q", expected, actual)
1466 }
1467}
1468
Paul Duffin46dc45a2020-05-14 15:39:10 +01001469func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1470 testJava(t, `
1471 java_sdk_library {
1472 name: "foo",
1473 srcs: ["a.java"],
1474 api_packages: ["foo"],
1475 public: {
1476 enabled: true,
1477 },
1478 }
1479
1480 java_library {
1481 name: "bar",
1482 srcs: ["b.java", ":foo{.public.stubs.source}"],
1483 }
1484 `)
1485}
1486
1487func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1488 testJavaError(t, `"foo" does not provide api scope system`, `
1489 java_sdk_library {
1490 name: "foo",
1491 srcs: ["a.java"],
1492 api_packages: ["foo"],
1493 public: {
1494 enabled: true,
1495 },
1496 }
1497
1498 java_library {
1499 name: "bar",
1500 srcs: ["b.java", ":foo{.system.stubs.source}"],
1501 }
1502 `)
1503}
1504
Paul Duffinca8d9a52020-06-26 22:20:25 +01001505func TestJavaSdkLibrary_Deps(t *testing.T) {
1506 ctx, _ := testJava(t, `
1507 java_sdk_library {
1508 name: "sdklib",
1509 srcs: ["a.java"],
1510 sdk_version: "none",
1511 system_modules: "none",
1512 public: {
1513 enabled: true,
1514 },
1515 }
1516 `)
1517
Paul Duffincee7e662020-07-09 17:32:57 +01001518 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001519 `dex2oatd`,
1520 `sdklib.impl`,
1521 `sdklib.stubs`,
1522 `sdklib.stubs.source`,
1523 `sdklib.xml`,
1524 })
1525}
1526
Paul Duffin46dc45a2020-05-14 15:39:10 +01001527func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1528 testJava(t, `
1529 java_sdk_library_import {
1530 name: "foo",
1531 public: {
1532 jars: ["a.jar"],
1533 stub_srcs: ["a.java"],
1534 current_api: "api/current.txt",
1535 removed_api: "api/removed.txt",
1536 },
1537 }
1538
1539 java_library {
1540 name: "bar",
1541 srcs: [":foo{.public.stubs.source}"],
1542 java_resources: [
1543 ":foo{.public.api.txt}",
1544 ":foo{.public.removed-api.txt}",
1545 ],
1546 }
1547 `)
1548}
1549
1550func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1551 bp := `
1552 java_sdk_library_import {
1553 name: "foo",
1554 public: {
1555 jars: ["a.jar"],
1556 },
1557 }
1558 `
1559
1560 t.Run("stubs.source", func(t *testing.T) {
1561 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1562 java_library {
1563 name: "bar",
1564 srcs: [":foo{.public.stubs.source}"],
1565 java_resources: [
1566 ":foo{.public.api.txt}",
1567 ":foo{.public.removed-api.txt}",
1568 ],
1569 }
1570 `)
1571 })
1572
1573 t.Run("api.txt", func(t *testing.T) {
1574 testJavaError(t, `api.txt not available for api scope public`, bp+`
1575 java_library {
1576 name: "bar",
1577 srcs: ["a.java"],
1578 java_resources: [
1579 ":foo{.public.api.txt}",
1580 ],
1581 }
1582 `)
1583 })
1584
1585 t.Run("removed-api.txt", func(t *testing.T) {
1586 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1587 java_library {
1588 name: "bar",
1589 srcs: ["a.java"],
1590 java_resources: [
1591 ":foo{.public.removed-api.txt}",
1592 ],
1593 }
1594 `)
1595 })
1596}
1597
Paul Duffin3375e352020-04-28 10:44:03 +01001598func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1599 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1600 java_sdk_library {
1601 name: "foo",
1602 srcs: ["a.java", "b.java"],
1603 api_packages: ["foo"],
1604 // Explicitly disable public to test the check that ensures the set of enabled
1605 // scopes is consistent.
1606 public: {
1607 enabled: false,
1608 },
1609 system: {
1610 enabled: true,
1611 },
1612 }
1613 `)
1614}
1615
Paul Duffin87a05a32020-05-12 11:50:28 +01001616func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1617 testJava(t, `
1618 java_sdk_library {
1619 name: "foo",
1620 srcs: ["a.java", "b.java"],
1621 api_packages: ["foo"],
1622 system: {
1623 enabled: true,
1624 sdk_version: "module_current",
1625 },
1626 }
1627 `)
1628}
1629
Paul Duffin0c5bae52020-06-02 13:00:08 +01001630func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
1631 testJava(t, `
1632 java_sdk_library {
1633 name: "foo",
1634 srcs: ["a.java", "b.java"],
1635 api_packages: ["foo"],
1636 system: {
1637 enabled: true,
1638 },
1639 module_lib: {
1640 enabled: true,
1641 },
1642 }
1643 `)
1644}
1645
1646func TestJavaSdkLibrary_SystemServer(t *testing.T) {
1647 testJava(t, `
1648 java_sdk_library {
1649 name: "foo",
1650 srcs: ["a.java", "b.java"],
1651 api_packages: ["foo"],
1652 system: {
1653 enabled: true,
1654 },
1655 system_server: {
1656 enabled: true,
1657 },
1658 }
1659 `)
1660}
1661
Paul Duffin803a9562020-05-20 11:52:25 +01001662func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1663 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1664 java_sdk_library {
1665 name: "foo",
1666 srcs: ["a.java"],
1667 public: {
1668 enabled: false,
1669 },
1670 }
1671
1672 java_library {
1673 name: "baz",
1674 srcs: ["a.java"],
1675 libs: ["foo"],
1676 sdk_version: "module_current",
1677 }
1678 `)
1679}
1680
1681func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1682 testJava(t, `
1683 java_sdk_library {
1684 name: "foo",
1685 srcs: ["a.java"],
1686 system: {
1687 enabled: true,
1688 },
1689 }
1690
1691 java_library {
1692 name: "baz",
1693 srcs: ["a.java"],
1694 libs: ["foo"],
1695 // foo does not have module-lib scope so it should fallback to system
1696 sdk_version: "module_current",
1697 }
1698 `)
1699}
1700
Jiyong Park932cdfe2020-05-28 00:19:53 +09001701func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1702 ctx, _ := testJava(t, `
1703 java_sdk_library {
1704 name: "foo",
1705 srcs: ["a.java"],
1706 system: {
1707 enabled: true,
1708 },
1709 default_to_stubs: true,
1710 }
1711
1712 java_library {
1713 name: "baz",
1714 srcs: ["a.java"],
1715 libs: ["foo"],
1716 // does not have sdk_version set, should fallback to module,
1717 // which will then fallback to system because the module scope
1718 // is not enabled.
1719 }
1720 `)
1721 // The baz library should depend on the system stubs jar.
1722 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1723 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1724 t.Errorf("expected %q, found %#q", expected, actual)
1725 }
1726}
1727
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001728var compilerFlagsTestCases = []struct {
1729 in string
1730 out bool
1731}{
1732 {
1733 in: "a",
1734 out: false,
1735 },
1736 {
1737 in: "-a",
1738 out: true,
1739 },
1740 {
1741 in: "-no-jdk",
1742 out: false,
1743 },
1744 {
1745 in: "-no-stdlib",
1746 out: false,
1747 },
1748 {
1749 in: "-kotlin-home",
1750 out: false,
1751 },
1752 {
1753 in: "-kotlin-home /some/path",
1754 out: false,
1755 },
1756 {
1757 in: "-include-runtime",
1758 out: false,
1759 },
1760 {
1761 in: "-Xintellij-plugin-root",
1762 out: false,
1763 },
1764}
1765
1766type mockContext struct {
1767 android.ModuleContext
1768 result bool
1769}
1770
1771func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1772 // CheckBadCompilerFlags calls this function when the flag should be rejected
1773 ctx.result = false
1774}
1775
1776func TestCompilerFlags(t *testing.T) {
1777 for _, testCase := range compilerFlagsTestCases {
1778 ctx := &mockContext{result: true}
1779 CheckKotlincFlags(ctx, []string{testCase.in})
1780 if ctx.result != testCase.out {
1781 t.Errorf("incorrect output:")
1782 t.Errorf(" input: %#v", testCase.in)
1783 t.Errorf(" expected: %#v", testCase.out)
1784 t.Errorf(" got: %#v", ctx.result)
1785 }
1786 }
1787}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001788
1789// TODO(jungjw): Consider making this more robust by ignoring path order.
1790func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1791 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1792 flags := strings.Split(variables["javacFlags"], " ")
1793 got := ""
1794 for _, flag := range flags {
1795 keyEnd := strings.Index(flag, "=")
1796 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1797 got = flag[keyEnd+1:]
1798 break
1799 }
1800 }
1801 if expected != got {
1802 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1803 }
1804}
1805
1806func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001807 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001808 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001809 bp := `
1810 java_library {
1811 name: "foo",
1812 srcs: ["a.java"],
1813 java_version: "1.8",
1814 }
1815
1816 java_library {
1817 name: "bar",
1818 srcs: ["b.java"],
1819 sdk_version: "none",
1820 system_modules: "none",
1821 patch_module: "java.base",
1822 java_version: "1.8",
1823 }
1824
1825 java_library {
1826 name: "baz",
1827 srcs: ["c.java"],
1828 patch_module: "java.base",
1829 java_version: "1.8",
1830 }
1831 `
1832 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001833
1834 checkPatchModuleFlag(t, ctx, "foo", "")
1835 checkPatchModuleFlag(t, ctx, "bar", "")
1836 checkPatchModuleFlag(t, ctx, "baz", "")
1837 })
1838
Pete Gillin0c2143e2019-05-02 15:32:11 +01001839 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001840 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001841 bp := `
1842 java_library {
1843 name: "foo",
1844 srcs: ["a.java"],
1845 }
1846
1847 java_library {
1848 name: "bar",
1849 srcs: ["b.java"],
1850 sdk_version: "none",
1851 system_modules: "none",
1852 patch_module: "java.base",
1853 }
1854
1855 java_library {
1856 name: "baz",
1857 srcs: ["c.java"],
1858 patch_module: "java.base",
1859 }
1860 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001861 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001862
1863 checkPatchModuleFlag(t, ctx, "foo", "")
1864 expected := "java.base=.:" + buildDir
1865 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09001866 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001867 checkPatchModuleFlag(t, ctx, "baz", expected)
1868 })
1869}
Paul Duffina7b9f422020-01-10 17:12:18 +00001870
1871func TestJavaSystemModules(t *testing.T) {
1872 ctx, _ := testJava(t, `
1873 java_system_modules {
1874 name: "system-modules",
1875 libs: ["system-module1", "system-module2"],
1876 }
1877 java_library {
1878 name: "system-module1",
1879 srcs: ["a.java"],
1880 sdk_version: "none",
1881 system_modules: "none",
1882 }
1883 java_library {
1884 name: "system-module2",
1885 srcs: ["b.java"],
1886 sdk_version: "none",
1887 system_modules: "none",
1888 }
1889 `)
1890
1891 // check the existence of the module
1892 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1893
1894 cmd := systemModules.Rule("jarsTosystemModules")
1895
1896 // make sure the command compiles against the supplied modules.
1897 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1898 if !strings.Contains(cmd.Args["classpath"], module) {
1899 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1900 module)
1901 }
1902 }
1903}
Paul Duffin90169ba2020-01-10 17:16:44 +00001904
1905func TestJavaSystemModulesImport(t *testing.T) {
1906 ctx, _ := testJava(t, `
1907 java_system_modules_import {
1908 name: "system-modules",
1909 libs: ["system-module1", "system-module2"],
1910 }
1911 java_import {
1912 name: "system-module1",
1913 jars: ["a.jar"],
1914 }
1915 java_import {
1916 name: "system-module2",
1917 jars: ["b.jar"],
1918 }
1919 `)
1920
1921 // check the existence of the module
1922 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1923
1924 cmd := systemModules.Rule("jarsTosystemModules")
1925
1926 // make sure the command compiles against the supplied modules.
1927 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1928 if !strings.Contains(cmd.Args["classpath"], module) {
1929 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1930 module)
1931 }
1932 }
1933}
Paul Duffin83a2d962019-11-19 19:44:10 +00001934
1935func TestJavaLibraryWithSystemModules(t *testing.T) {
1936 ctx, _ := testJava(t, `
1937 java_library {
1938 name: "lib-with-source-system-modules",
1939 srcs: [
1940 "a.java",
1941 ],
1942 sdk_version: "none",
1943 system_modules: "source-system-modules",
1944 }
1945
1946 java_library {
1947 name: "source-jar",
1948 srcs: [
1949 "a.java",
1950 ],
1951 }
1952
1953 java_system_modules {
1954 name: "source-system-modules",
1955 libs: ["source-jar"],
1956 }
1957
1958 java_library {
1959 name: "lib-with-prebuilt-system-modules",
1960 srcs: [
1961 "a.java",
1962 ],
1963 sdk_version: "none",
1964 system_modules: "prebuilt-system-modules",
1965 }
1966
1967 java_import {
1968 name: "prebuilt-jar",
1969 jars: ["a.jar"],
1970 }
1971
1972 java_system_modules_import {
1973 name: "prebuilt-system-modules",
1974 libs: ["prebuilt-jar"],
1975 }
1976 `)
1977
1978 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
1979
1980 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
1981}
1982
1983func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
1984 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
1985 bootClasspath := javacRule.Args["bootClasspath"]
1986 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
1987 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
1988 }
1989}
Jiyong Park19604de2020-03-24 16:44:11 +09001990
1991func TestAidlExportIncludeDirsFromImports(t *testing.T) {
1992 ctx, _ := testJava(t, `
1993 java_library {
1994 name: "foo",
1995 srcs: ["aidl/foo/IFoo.aidl"],
1996 libs: ["bar"],
1997 }
1998
1999 java_import {
2000 name: "bar",
2001 jars: ["a.jar"],
2002 aidl: {
2003 export_include_dirs: ["aidl/bar"],
2004 },
2005 }
2006 `)
2007
2008 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2009 expectedAidlFlag := "-Iaidl/bar"
2010 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2011 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2012 }
2013}
Liz Kammerdd849a82020-06-12 16:38:45 -07002014
2015func TestDataNativeBinaries(t *testing.T) {
2016 ctx, config := testJava(t, `
2017 java_test_host {
2018 name: "foo",
2019 srcs: ["a.java"],
2020 data_native_bins: ["bin"]
2021 }
2022
2023 python_binary_host {
2024 name: "bin",
2025 srcs: ["bin.py"],
2026 }
2027 `)
2028
2029 buildOS := android.BuildOs.String()
2030
2031 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2032 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
2033 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2034 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2035 if !reflect.DeepEqual(expected, actual) {
2036 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2037 }
2038}