blob: 59a2ce7aaef5608641aa4cdd0b8e4c33da3e93b6 [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
Paul Duffinca8d9a52020-06-26 22:20:25 +010028 "github.com/google/blueprint"
Jeongik Cha28df2572019-11-11 10:46:36 +090029 "github.com/google/blueprint/proptools"
30
Colin Crossa4f08812018-10-02 22:03:40 -070031 "android/soong/android"
32 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080033 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070034 "android/soong/genrule"
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)
Paul Duffin884363e2019-12-19 10:21:09 +000085 RegisterDocsBuildComponents(ctx)
86 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000087 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070088 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010089 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000090
91 RegisterPrebuiltApisBuildComponents(ctx)
92
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070093 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080094 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070095 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070096
97 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +000098 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -070099
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000100 dexpreopt.RegisterToolModulesForTest(ctx)
101
Colin Cross527012a2017-11-30 22:56:16 -0800102 return ctx
103}
104
105func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800106 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800107
Colin Cross98be1bb2019-12-13 20:41:13 -0800108 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000109 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800110
Colin Cross98be1bb2019-12-13 20:41:13 -0800111 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100112 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800113 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700114 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800115 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800116}
117
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900118func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900119 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800120 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900121}
122
Colin Cross98be1bb2019-12-13 20:41:13 -0800123func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900124 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800125 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900126
Colin Cross98be1bb2019-12-13 20:41:13 -0800127 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000128 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900129
Colin Cross98be1bb2019-12-13 20:41:13 -0800130 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900131 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
132 if len(errs) > 0 {
133 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900134 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900135 }
136 _, errs = ctx.PrepareBuildActions(config)
137 if len(errs) > 0 {
138 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900139 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900140 }
141
142 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900143
144 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900145}
146
Colin Cross238c1f32020-06-07 16:58:18 -0700147func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
148 t.Helper()
149 return testJavaWithConfig(t, testConfig(nil, bp, fs))
150}
151
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700152func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800153 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700154 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900155}
156
Colin Cross98be1bb2019-12-13 20:41:13 -0800157func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900158 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800159 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800160 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700161
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700162 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700163}
164
Colin Cross2acdae82017-09-15 19:44:24 -0700165func moduleToPath(name string) string {
166 switch {
167 case name == `""`:
168 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700169 case strings.HasSuffix(name, ".jar"):
170 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700171 default:
172 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700173 }
174}
175
Paul Duffinca8d9a52020-06-26 22:20:25 +0100176func checkModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
177 t.Helper()
178 module := ctx.ModuleForTests(name, variant).Module()
179 deps := []string{}
180 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
181 deps = append(deps, m.Name())
182 })
183 sort.Strings(deps)
184
185 if actual := deps; !reflect.DeepEqual(expected, actual) {
186 t.Errorf("expected %#q, found %#q", expected, actual)
187 }
188}
189
Jeongik Chae403e9e2019-12-07 00:16:24 +0900190func TestJavaLinkType(t *testing.T) {
191 testJava(t, `
192 java_library {
193 name: "foo",
194 srcs: ["a.java"],
195 libs: ["bar"],
196 static_libs: ["baz"],
197 }
198
199 java_library {
200 name: "bar",
201 sdk_version: "current",
202 srcs: ["b.java"],
203 }
204
205 java_library {
206 name: "baz",
207 sdk_version: "system_current",
208 srcs: ["c.java"],
209 }
210 `)
211
212 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.", `
213 java_library {
214 name: "foo",
215 srcs: ["a.java"],
216 libs: ["bar"],
217 sdk_version: "current",
218 static_libs: ["baz"],
219 }
220
221 java_library {
222 name: "bar",
223 sdk_version: "current",
224 srcs: ["b.java"],
225 }
226
227 java_library {
228 name: "baz",
229 sdk_version: "system_current",
230 srcs: ["c.java"],
231 }
232 `)
233
234 testJava(t, `
235 java_library {
236 name: "foo",
237 srcs: ["a.java"],
238 libs: ["bar"],
239 sdk_version: "system_current",
240 static_libs: ["baz"],
241 }
242
243 java_library {
244 name: "bar",
245 sdk_version: "current",
246 srcs: ["b.java"],
247 }
248
249 java_library {
250 name: "baz",
251 sdk_version: "system_current",
252 srcs: ["c.java"],
253 }
254 `)
255
256 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.", `
257 java_library {
258 name: "foo",
259 srcs: ["a.java"],
260 libs: ["bar"],
261 sdk_version: "system_current",
262 static_libs: ["baz"],
263 }
264
265 java_library {
266 name: "bar",
267 sdk_version: "current",
268 srcs: ["b.java"],
269 }
270
271 java_library {
272 name: "baz",
273 srcs: ["c.java"],
274 }
275 `)
276}
277
Colin Cross72bb3632017-07-13 16:23:21 -0700278func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700279 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700280 java_library {
281 name: "foo",
282 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700283 libs: ["bar"],
284 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700285 }
286
287 java_library {
288 name: "bar",
289 srcs: ["b.java"],
290 }
291
292 java_library {
293 name: "baz",
294 srcs: ["c.java"],
295 }
Colin Crossd5934c82017-10-02 13:55:26 -0700296 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700297
Colin Cross4c428df2017-09-15 17:36:05 -0700298 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700299 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700300
301 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
302 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
303 }
304
Colin Cross1ee23172017-10-18 14:44:18 -0700305 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700306 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
307 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700308
Nan Zhanged19fc32017-10-19 13:06:22 -0700309 if !strings.Contains(javac.Args["classpath"], barTurbine) {
310 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700311 }
312
Nan Zhanged19fc32017-10-19 13:06:22 -0700313 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
314 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700315 }
316
317 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
318 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700319 }
320}
321
Artur Satayev9cf46692019-11-26 18:08:34 +0000322func TestExportedPlugins(t *testing.T) {
323 type Result struct {
324 library string
325 processors string
326 }
327 var tests = []struct {
328 name string
329 extra string
330 results []Result
331 }{
332 {
333 name: "Exported plugin is not a direct plugin",
334 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
335 results: []Result{{library: "exports", processors: "-proc:none"}},
336 },
337 {
338 name: "Exports plugin to dependee",
339 extra: `
340 java_library{name: "exports", exported_plugins: ["plugin"]}
341 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
342 java_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 to android_library",
351 extra: `
352 java_library{name: "exports", exported_plugins: ["plugin"]}
353 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
354 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
355 `,
356 results: []Result{
357 {library: "foo", processors: "-processor com.android.TestPlugin"},
358 {library: "bar", processors: "-processor com.android.TestPlugin"},
359 },
360 },
361 {
362 name: "Exports plugin is not propagated via transitive deps",
363 extra: `
364 java_library{name: "exports", exported_plugins: ["plugin"]}
365 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
366 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
367 `,
368 results: []Result{
369 {library: "foo", processors: "-processor com.android.TestPlugin"},
370 {library: "bar", processors: "-proc:none"},
371 },
372 },
373 {
374 name: "Exports plugin appends to plugins",
375 extra: `
376 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
377 java_library{name: "exports", exported_plugins: ["plugin"]}
378 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
379 `,
380 results: []Result{
381 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
382 },
383 },
384 }
385
386 for _, test := range tests {
387 t.Run(test.name, func(t *testing.T) {
388 ctx, _ := testJava(t, `
389 java_plugin {
390 name: "plugin",
391 processor_class: "com.android.TestPlugin",
392 }
393 `+test.extra)
394
395 for _, want := range test.results {
396 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
397 if javac.Args["processor"] != want.processors {
398 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
399 }
400 }
401 })
402 }
403}
404
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900405func TestSdkVersionByPartition(t *testing.T) {
406 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900407 java_library {
408 name: "foo",
409 srcs: ["a.java"],
410 vendor: true,
411 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900412 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900413
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900414 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900415 java_library {
416 name: "bar",
417 srcs: ["b.java"],
418 }
419 `)
420
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900421 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900422 bp := `
423 java_library {
424 name: "foo",
425 srcs: ["a.java"],
426 product_specific: true,
427 }
428 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800429
430 config := testConfig(nil, bp, nil)
431 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900432 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800433 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 +0900434 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800435 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900436 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900437 }
438}
439
Colin Crossd5934c82017-10-02 13:55:26 -0700440func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700441 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700442 java_library {
443 name: "foo",
444 srcs: ["a.java"],
445 target: {
446 android: {
447 srcs: ["b.java"],
448 },
449 },
450 }
451 `)
452
453 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
454 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
455 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
456 }
457}
458
Colin Cross6b4a32d2017-12-05 13:42:45 -0800459func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700460 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800461 java_library_host {
462 name: "foo",
463 srcs: ["a.java"],
464 }
465
466 java_binary_host {
467 name: "bar",
468 srcs: ["b.java"],
469 static_libs: ["foo"],
470 }
471 `)
472
473 buildOS := android.BuildOs.String()
474
475 bar := ctx.ModuleForTests("bar", buildOS+"_common")
476 barJar := bar.Output("bar.jar").Output.String()
477 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
478 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
479
480 // Test that the install binary wrapper depends on the installed jar file
481 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
482 t.Errorf("expected binary wrapper implicits [%q], got %v",
483 barJar, barWrapperDeps)
484 }
Alex Humesky2070e322020-06-09 20:23:08 -0400485}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800486
Alex Humesky2070e322020-06-09 20:23:08 -0400487func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
488 bp := `
489 java_library {
490 name: "target_library",
491 srcs: ["a.java"],
492 }
493
494 java_binary_host {
495 name: "host_binary",
496 srcs: ["b.java"],
497 }
498 `
499 config := testConfig(nil, bp, nil)
500 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
501
502 ctx, _ := testJavaWithConfig(t, config)
503
504 // first, sanity check that the -g flag is added to target modules
505 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
506 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
507 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
508 t.Errorf("target library javac flags %v should contain "+
509 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
510 }
511
512 // check that -g is not overridden for host modules
513 buildOS := android.BuildOs.String()
514 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
515 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
516 if strings.Contains(hostJavaFlags, "-g:source,lines") {
517 t.Errorf("java_binary_host javac flags %v should not have "+
518 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
519 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800520}
521
Colin Cross72bb3632017-07-13 16:23:21 -0700522func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700523 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700524 java_library {
525 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000526 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000527 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700528 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700529 }
530
Colin Cross74d73e22017-08-02 11:05:49 -0700531 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700532 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700533 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700534 }
535
Colin Cross74d73e22017-08-02 11:05:49 -0700536 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700537 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700538 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700539 }
Colin Cross42be7612019-02-21 18:12:14 -0800540
541 dex_import {
542 name: "qux",
543 jars: ["b.jar"],
544 }
Colin Cross79c7c262019-04-17 11:11:46 -0700545
546 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000547 name: "sdklib",
548 public: {
549 jars: ["c.jar"],
550 },
551 }
552
Paul Duffin91547182019-11-12 19:39:36 +0000553 prebuilt_stubs_sources {
554 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000555 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000556 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000557
558 java_test_import {
559 name: "test",
560 jars: ["a.jar"],
561 test_suites: ["cts"],
562 test_config: "AndroidTest.xml",
563 }
Colin Cross72bb3632017-07-13 16:23:21 -0700564 `)
565
Paul Duffin9b478b02019-12-10 13:41:51 +0000566 fooModule := ctx.ModuleForTests("foo", "android_common")
567 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700568 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700569 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
570 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700571 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700572
Paul Duffin9b478b02019-12-10 13:41:51 +0000573 fooLibrary := fooModule.Module().(*Library)
574 assertDeepEquals(t, "foo java sources incorrect",
575 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000576
Paul Duffin9b478b02019-12-10 13:41:51 +0000577 assertDeepEquals(t, "foo java source jars incorrect",
578 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
579 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000580
Colin Cross37f6d792018-07-12 12:28:41 -0700581 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
582 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700583 }
584
Colin Cross79c7c262019-04-17 11:11:46 -0700585 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
586 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
587 }
588
Colin Cross37f6d792018-07-12 12:28:41 -0700589 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
590 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700591 }
Colin Cross42be7612019-02-21 18:12:14 -0800592
593 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700594}
595
Paul Duffin9b478b02019-12-10 13:41:51 +0000596func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
597 if !reflect.DeepEqual(expected, actual) {
598 t.Errorf("%s: expected %q, found %q", message, expected, actual)
599 }
600}
601
Paul Duffin56d44902020-01-31 13:36:25 +0000602func TestJavaSdkLibraryImport(t *testing.T) {
603 ctx, _ := testJava(t, `
604 java_library {
605 name: "foo",
606 srcs: ["a.java"],
607 libs: ["sdklib"],
608 sdk_version: "current",
609 }
610
611 java_library {
612 name: "foo.system",
613 srcs: ["a.java"],
614 libs: ["sdklib"],
615 sdk_version: "system_current",
616 }
617
618 java_library {
619 name: "foo.test",
620 srcs: ["a.java"],
621 libs: ["sdklib"],
622 sdk_version: "test_current",
623 }
624
625 java_sdk_library_import {
626 name: "sdklib",
627 public: {
628 jars: ["a.jar"],
629 },
630 system: {
631 jars: ["b.jar"],
632 },
633 test: {
634 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100635 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000636 },
637 }
638 `)
639
640 for _, scope := range []string{"", ".system", ".test"} {
641 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
642 javac := fooModule.Rule("javac")
643
644 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
645 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
646 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
647 }
648 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100649
650 checkModuleDependencies(t, ctx, "sdklib", "android_common", []string{
651 `prebuilt_sdklib.stubs`,
652 `prebuilt_sdklib.stubs.source.test`,
653 `prebuilt_sdklib.stubs.system`,
654 `prebuilt_sdklib.stubs.test`,
655 })
656}
657
658func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
659 ctx, _ := testJava(t, `
660 java_sdk_library {
661 name: "sdklib",
662 srcs: ["a.java"],
663 sdk_version: "none",
664 system_modules: "none",
665 public: {
666 enabled: true,
667 },
668 }
669
670 java_sdk_library_import {
671 name: "sdklib",
672 public: {
673 jars: ["a.jar"],
674 },
675 }
676 `)
677
678 checkModuleDependencies(t, ctx, "sdklib", "android_common", []string{
679 `dex2oatd`,
680 `prebuilt_sdklib`,
681 `sdklib.impl`,
682 `sdklib.stubs`,
683 `sdklib.stubs.source`,
684 `sdklib.xml`,
685 })
686
687 checkModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100688 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100689 `sdklib.impl`,
690 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
691 // dependency is added after prebuilts may have been renamed and so has to use
692 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100693 `sdklib.xml`,
694 })
695}
696
697func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
698 ctx, _ := testJava(t, `
699 java_sdk_library {
700 name: "sdklib",
701 srcs: ["a.java"],
702 sdk_version: "none",
703 system_modules: "none",
704 public: {
705 enabled: true,
706 },
707 }
708
709 java_sdk_library_import {
710 name: "sdklib",
711 prefer: true,
712 public: {
713 jars: ["a.jar"],
714 },
715 }
716 `)
717
718 checkModuleDependencies(t, ctx, "sdklib", "android_common", []string{
719 `dex2oatd`,
720 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100721 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100722 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100723 `sdklib.stubs.source`,
724 `sdklib.xml`,
725 })
726
727 checkModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
728 `prebuilt_sdklib.stubs`,
729 `sdklib.impl`,
730 `sdklib.xml`,
731 })
Paul Duffin56d44902020-01-31 13:36:25 +0000732}
733
Colin Cross89536d42017-07-07 14:35:50 -0700734func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700735 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700736 java_defaults {
737 name: "defaults",
738 srcs: ["a.java"],
739 libs: ["bar"],
740 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700741 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700742 }
743
744 java_library {
745 name: "foo",
746 defaults: ["defaults"],
747 }
748
749 java_library {
750 name: "bar",
751 srcs: ["b.java"],
752 }
753
754 java_library {
755 name: "baz",
756 srcs: ["c.java"],
757 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700758
759 android_test {
760 name: "atestOptimize",
761 defaults: ["defaults"],
762 optimize: {enabled: true},
763 }
764
765 android_test {
766 name: "atestNoOptimize",
767 defaults: ["defaults"],
768 }
769
770 android_test {
771 name: "atestDefault",
772 srcs: ["a.java"],
773 }
Colin Cross89536d42017-07-07 14:35:50 -0700774 `)
775
Colin Cross4c428df2017-09-15 17:36:05 -0700776 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700777 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700778
779 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
780 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
781 }
782
Nan Zhanged19fc32017-10-19 13:06:22 -0700783 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
784 if !strings.Contains(javac.Args["classpath"], barTurbine) {
785 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700786 }
787
Colin Cross1ee23172017-10-18 14:44:18 -0700788 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700789 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
790 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700791 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700792
793 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
794 if atestOptimize.Output == nil {
795 t.Errorf("atestOptimize should optimize APK")
796 }
797
798 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
799 if atestNoOptimize.Output == nil {
800 t.Errorf("atestNoOptimize should not optimize APK")
801 }
802
803 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
804 if atestDefault.Output == nil {
805 t.Errorf("atestDefault should optimize APK")
806 }
Colin Cross89536d42017-07-07 14:35:50 -0700807}
808
Colin Cross0f37af02017-09-27 17:42:05 -0700809func TestResources(t *testing.T) {
810 var table = []struct {
811 name string
812 prop string
813 extra string
814 args string
815 }{
816 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700817 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700818 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800819 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700820 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700821 },
822 {
823 // Test that a module with java_resources includes the files
824 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700825 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
826 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700827 },
828 {
829 // Test that a module with a filegroup in java_resources includes the files with the
830 // path prefix
831 name: "resource filegroup",
832 prop: `java_resources: [":foo-res"]`,
833 extra: `
834 filegroup {
835 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800836 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700837 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700838 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700839 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700840 },
841 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700842 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
843 name: "wildcard dirs",
844 prop: `java_resource_dirs: ["java-res/*"]`,
845 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
846 },
847 {
848 // Test that a module exclude_java_resource_dirs excludes the files
849 name: "wildcard dirs",
850 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
851 args: "-C java-res/a -f java-res/a/a",
852 },
Colin Crosscedd4762018-09-13 11:26:19 -0700853 {
854 // Test wildcards in java_resources
855 name: "wildcard files",
856 prop: `java_resources: ["java-res/**/*"]`,
857 args: "-C . -f java-res/a/a -f java-res/b/b",
858 },
859 {
860 // Test exclude_java_resources with java_resources
861 name: "wildcard files with exclude",
862 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
863 args: "-C . -f java-res/a/a",
864 },
865 {
866 // Test exclude_java_resources with java_resource_dirs
867 name: "resource dirs with exclude files",
868 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
869 args: "-C java-res -f java-res/a/a",
870 },
871 {
872 // Test exclude_java_resource_dirs with java_resource_dirs
873 name: "resource dirs with exclude files",
874 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
875 args: "-C java-res -f java-res/a/a -f java-res/b/b",
876 },
Colin Cross0f37af02017-09-27 17:42:05 -0700877 }
878
879 for _, test := range table {
880 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700881 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700882 java_library {
883 name: "foo",
884 srcs: [
885 "a.java",
886 "b.java",
887 "c.java",
888 ],
889 `+test.prop+`,
890 }
Colin Cross238c1f32020-06-07 16:58:18 -0700891 `+test.extra,
892 map[string][]byte{
893 "java-res/a/a": nil,
894 "java-res/b/b": nil,
895 "java-res2/a": nil,
896 },
897 )
Colin Cross0f37af02017-09-27 17:42:05 -0700898
Colin Cross331a1212018-08-15 20:40:52 -0700899 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700900 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700901
902 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
903 t.Errorf("foo combined jars %v does not contain %q",
904 foo.Inputs.Strings(), fooRes.Output.String())
905 }
906
Colin Crossaf9c55b2017-10-03 14:50:08 -0700907 if fooRes.Args["jarArgs"] != test.args {
908 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700909 fooRes.Args["jarArgs"], test.args)
910 }
911 })
912 }
913}
914
Colin Cross0c4ce212019-05-03 15:28:19 -0700915func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700916 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700917 java_library {
918 name: "foo",
919 srcs: [
920 "a.java",
921 "b.java",
922 "c.java",
923 ],
924 include_srcs: true,
925 }
926
927 java_library {
928 name: "bar",
929 srcs: [
930 "a.java",
931 "b.java",
932 "c.java",
933 ],
934 java_resource_dirs: ["java-res"],
935 include_srcs: true,
936 }
Colin Cross238c1f32020-06-07 16:58:18 -0700937 `, map[string][]byte{
938 "java-res/a/a": nil,
939 "java-res/b/b": nil,
940 "java-res2/a": nil,
941 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700942
943 // Test a library with include_srcs: true
944 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
945 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
946
947 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
948 t.Errorf("foo combined jars %v does not contain %q", w, g)
949 }
950
951 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
952 t.Errorf("foo source jar args %q is not %q", w, g)
953 }
954
955 // Test a library with include_srcs: true and resources
956 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
957 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
958 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
959 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
960
961 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
962 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
963 }
964
965 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
966 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
967 }
968
969 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
970 t.Errorf("bar combined jars %v does not contain %q", w, g)
971 }
972
973 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
974 t.Errorf("bar source jar args %q is not %q", w, g)
975 }
976
977 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
978 t.Errorf("bar resource jar args %q is not %q", w, g)
979 }
980}
981
Colin Cross54190b32017-10-09 15:34:10 -0700982func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700983 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700984 java_library {
985 name: "foo",
986 srcs: [
987 "a*.java",
988 ":gen",
989 "b*.java",
990 ],
991 }
992
993 genrule {
994 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800995 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700996 out: ["gen.java"],
997 }
Colin Cross238c1f32020-06-07 16:58:18 -0700998 `, map[string][]byte{
999 "a.java": nil,
1000 "b.java": nil,
1001 })
Colin Cross54190b32017-10-09 15:34:10 -07001002
1003 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1004 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1005
Colin Cross15e86d92017-10-20 15:07:08 -07001006 if filepath.Base(genrule.Output.String()) != "gen.java" {
1007 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001008 }
1009
1010 if len(javac.Inputs) != 3 ||
1011 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001012 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001013 javac.Inputs[2].String() != "b.java" {
1014 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1015 }
1016}
1017
Nan Zhang61eaedb2017-11-02 13:28:15 -07001018func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001019 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001020 java_library {
1021 name: "foo",
1022 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001023 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001024 }
1025
1026 java_library {
1027 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001028 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001029 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001030 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001031 }
1032
1033 java_library {
1034 name: "baz",
1035 srcs: ["c.java"],
1036 libs: ["bar"],
1037 sdk_version: "14",
1038 }
1039 `)
1040
1041 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1042 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1043 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1044 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1045 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1046
1047 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1048 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1049 }
1050
1051 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1052 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1053 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1054 }
1055 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1056 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1057 }
1058 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1059 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1060 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001061 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001062 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001063 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001064 }
1065}
1066
1067func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001068 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001069 java_library {
1070 name: "bar",
1071 srcs: ["a.java","b.java","c.java"],
1072 javac_shard_size: 1
1073 }
1074 `)
1075
1076 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1077 for i := 0; i < 3; i++ {
1078 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1079 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1080 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1081 }
1082 }
1083}
1084
Nan Zhang581fd212018-01-10 16:06:12 -08001085func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001086 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001087 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001088 name: "droiddoc-templates-sdk",
1089 path: ".",
1090 }
Jiyong Park29074592019-07-07 16:27:47 +09001091 filegroup {
1092 name: "bar-doc-aidl-srcs",
1093 srcs: ["bar-doc/IBar.aidl"],
1094 path: "bar-doc",
1095 }
Nan Zhang581fd212018-01-10 16:06:12 -08001096 droiddoc {
1097 name: "bar-doc",
1098 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001099 "bar-doc/a.java",
Jiyong Park1e440682018-05-23 18:42:04 +09001100 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +09001101 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -08001102 ],
1103 exclude_srcs: [
1104 "bar-doc/b.java"
1105 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001106 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001107 hdf: [
1108 "android.whichdoc offline",
1109 ],
1110 knowntags: [
1111 "bar-doc/known_oj_tags.txt",
1112 ],
1113 proofread_file: "libcore-proofread.txt",
1114 todo_file: "libcore-docs-todo.html",
1115 args: "-offlinemode -title \"libcore\"",
1116 }
Colin Cross238c1f32020-06-07 16:58:18 -07001117 `,
1118 map[string][]byte{
1119 "bar-doc/a.java": nil,
1120 "bar-doc/b.java": nil,
1121 })
Nan Zhang581fd212018-01-10 16:06:12 -08001122
Colin Crossc0806172019-06-14 18:51:47 -07001123 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +09001124 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -07001125 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +09001126 javaSrcs = append(javaSrcs, i.Base())
1127 }
Colin Crossc0806172019-06-14 18:51:47 -07001128 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
1129 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +09001130 }
1131
Colin Crossc0806172019-06-14 18:51:47 -07001132 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
1133 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
1134 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1135 }
1136
1137 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1138 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001139 }
Nan Zhang581fd212018-01-10 16:06:12 -08001140}
1141
Paul Duffin83a2d962019-11-19 19:44:10 +00001142func TestDroidstubsWithSystemModules(t *testing.T) {
1143 ctx, _ := testJava(t, `
1144 droidstubs {
1145 name: "stubs-source-system-modules",
1146 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001147 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001148 ],
1149 sdk_version: "none",
1150 system_modules: "source-system-modules",
1151 }
1152
1153 java_library {
1154 name: "source-jar",
1155 srcs: [
1156 "a.java",
1157 ],
1158 }
1159
1160 java_system_modules {
1161 name: "source-system-modules",
1162 libs: ["source-jar"],
1163 }
1164
1165 droidstubs {
1166 name: "stubs-prebuilt-system-modules",
1167 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001168 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001169 ],
1170 sdk_version: "none",
1171 system_modules: "prebuilt-system-modules",
1172 }
1173
1174 java_import {
1175 name: "prebuilt-jar",
1176 jars: ["a.jar"],
1177 }
1178
1179 java_system_modules_import {
1180 name: "prebuilt-system-modules",
1181 libs: ["prebuilt-jar"],
1182 }
1183 `)
1184
1185 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1186
1187 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1188}
1189
1190func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1191 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1192 var systemJars []string
1193 for _, i := range metalavaRule.Implicits {
1194 systemJars = append(systemJars, i.Base())
1195 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001196 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001197 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1198 }
1199}
1200
Colin Cross54250902017-12-05 09:28:08 -08001201func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001202 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001203 java_library {
1204 name: "foo",
1205 srcs: ["a.java"],
1206 }
1207
1208 java_genrule {
1209 name: "jargen",
1210 tool_files: ["b.java"],
1211 cmd: "$(location b.java) $(in) $(out)",
1212 out: ["jargen.jar"],
1213 srcs: [":foo"],
1214 }
1215
1216 java_library {
1217 name: "bar",
1218 static_libs: ["jargen"],
1219 srcs: ["c.java"],
1220 }
1221
1222 java_library {
1223 name: "baz",
1224 libs: ["jargen"],
1225 srcs: ["c.java"],
1226 }
1227 `)
1228
1229 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1230 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1231 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1232 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1233 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1234
1235 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1236 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1237 }
1238
1239 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1240 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1241 }
1242
1243 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1244 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1245 }
1246
1247 if len(barCombined.Inputs) != 2 ||
1248 barCombined.Inputs[0].String() != bar.Output.String() ||
1249 barCombined.Inputs[1].String() != jargen.Output.String() {
1250 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1251 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1252 }
1253}
1254
Nan Zhang27e284d2018-02-09 21:03:53 +00001255func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001256 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001257 java_library {
1258 name: "foo",
1259 srcs: ["a.java", ":foo-srcs"],
1260 exclude_srcs: ["a.java", ":foo-excludes"],
1261 }
1262
1263 filegroup {
1264 name: "foo-srcs",
1265 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1266 }
1267
1268 filegroup {
1269 name: "foo-excludes",
1270 srcs: ["java-fg/a.java", "java-fg/b.java"],
1271 }
1272 `)
1273
1274 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1275
1276 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1277 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1278 }
1279}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001280
Paul Duffin52d398a2019-06-11 12:31:14 +01001281func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001282 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001283 "libcore/Android.bp": []byte(`
1284 java_library {
1285 name: "core",
1286 sdk_version: "none",
1287 system_modules: "none",
1288 }`),
1289 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001290 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001291 run(t, ctx, config)
1292}
1293
Jiyong Parkc678ad32018-04-10 13:07:10 +09001294func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001295 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001296 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001297 name: "droiddoc-templates-sdk",
1298 path: ".",
1299 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001300 java_sdk_library {
1301 name: "foo",
1302 srcs: ["a.java", "b.java"],
1303 api_packages: ["foo"],
1304 }
1305 java_sdk_library {
1306 name: "bar",
1307 srcs: ["a.java", "b.java"],
1308 api_packages: ["bar"],
1309 }
1310 java_library {
1311 name: "baz",
1312 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001313 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001314 sdk_version: "system_current",
1315 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001316 java_sdk_library {
1317 name: "barney",
1318 srcs: ["c.java"],
1319 api_only: true,
1320 }
1321 java_sdk_library {
1322 name: "betty",
1323 srcs: ["c.java"],
1324 shared_library: false,
1325 }
Paul Duffin859fe962020-05-15 10:20:31 +01001326 java_sdk_library_import {
1327 name: "quuz",
1328 public: {
1329 jars: ["c.jar"],
1330 },
1331 }
1332 java_sdk_library_import {
1333 name: "fred",
1334 public: {
1335 jars: ["b.jar"],
1336 },
1337 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001338 java_sdk_library_import {
1339 name: "wilma",
1340 public: {
1341 jars: ["b.jar"],
1342 },
1343 shared_library: false,
1344 }
Jiyong Park1be96912018-05-28 18:02:19 +09001345 java_library {
1346 name: "qux",
1347 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001348 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001349 sdk_version: "system_current",
1350 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001351 java_library {
1352 name: "baz-test",
1353 srcs: ["c.java"],
1354 libs: ["foo"],
1355 sdk_version: "test_current",
1356 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001357 java_library {
1358 name: "baz-29",
1359 srcs: ["c.java"],
1360 libs: ["foo"],
1361 sdk_version: "system_29",
1362 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001363 `)
1364
1365 // check the existence of the internal modules
1366 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001367 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1368 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1369 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1370 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1371 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1372 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001373 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001374 ctx.ModuleForTests("foo.api.public.28", "")
1375 ctx.ModuleForTests("foo.api.system.28", "")
1376 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001377
1378 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1379 // tests if baz is actually linked to the stubs lib
1380 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1381 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1382 "foo.stubs.system.jar")
1383 }
1384 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001385 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001386 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001387 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001388 }
1389 // test if baz is not linked to the system variant of foo
1390 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1391 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1392 "foo.stubs.jar")
1393 }
Jiyong Park1be96912018-05-28 18:02:19 +09001394
Paul Duffin726d23c2020-01-22 16:30:37 +00001395 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1396 // tests if baz-test is actually linked to the test stubs lib
1397 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1398 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1399 "foo.stubs.test.jar")
1400 }
1401
Paul Duffina2db18f2020-01-22 17:11:15 +00001402 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1403 // tests if baz-29 is actually linked to the system 29 stubs lib
1404 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1405 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1406 "prebuilts/sdk/29/system/foo.jar")
1407 }
1408
Jiyong Park1be96912018-05-28 18:02:19 +09001409 // test if baz has exported SDK lib names foo and bar to qux
1410 qux := ctx.ModuleForTests("qux", "android_common")
1411 if quxLib, ok := qux.Module().(*Library); ok {
1412 sdkLibs := quxLib.ExportedSdkLibs()
Paul Duffin859fe962020-05-15 10:20:31 +01001413 sort.Strings(sdkLibs)
1414 if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
1415 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001416 }
1417 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001418}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001419
Paul Duffindaaa3322020-05-26 18:13:57 +01001420func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1421 ctx, _ := testJava(t, `
1422 java_sdk_library {
1423 name: "foo",
1424 srcs: ["a.java"],
1425 api_only: true,
1426 public: {
1427 enabled: true,
1428 },
1429 }
1430
1431 java_library {
1432 name: "bar",
1433 srcs: ["b.java"],
1434 libs: ["foo"],
1435 }
1436 `)
1437
1438 // The bar library should depend on the stubs jar.
1439 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1440 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1441 t.Errorf("expected %q, found %#q", expected, actual)
1442 }
1443}
1444
Paul Duffin46dc45a2020-05-14 15:39:10 +01001445func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1446 testJava(t, `
1447 java_sdk_library {
1448 name: "foo",
1449 srcs: ["a.java"],
1450 api_packages: ["foo"],
1451 public: {
1452 enabled: true,
1453 },
1454 }
1455
1456 java_library {
1457 name: "bar",
1458 srcs: ["b.java", ":foo{.public.stubs.source}"],
1459 }
1460 `)
1461}
1462
1463func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1464 testJavaError(t, `"foo" does not provide api scope system`, `
1465 java_sdk_library {
1466 name: "foo",
1467 srcs: ["a.java"],
1468 api_packages: ["foo"],
1469 public: {
1470 enabled: true,
1471 },
1472 }
1473
1474 java_library {
1475 name: "bar",
1476 srcs: ["b.java", ":foo{.system.stubs.source}"],
1477 }
1478 `)
1479}
1480
Paul Duffinca8d9a52020-06-26 22:20:25 +01001481func TestJavaSdkLibrary_Deps(t *testing.T) {
1482 ctx, _ := testJava(t, `
1483 java_sdk_library {
1484 name: "sdklib",
1485 srcs: ["a.java"],
1486 sdk_version: "none",
1487 system_modules: "none",
1488 public: {
1489 enabled: true,
1490 },
1491 }
1492 `)
1493
1494 checkModuleDependencies(t, ctx, "sdklib", "android_common", []string{
1495 `dex2oatd`,
1496 `sdklib.impl`,
1497 `sdklib.stubs`,
1498 `sdklib.stubs.source`,
1499 `sdklib.xml`,
1500 })
1501}
1502
Paul Duffin46dc45a2020-05-14 15:39:10 +01001503func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1504 testJava(t, `
1505 java_sdk_library_import {
1506 name: "foo",
1507 public: {
1508 jars: ["a.jar"],
1509 stub_srcs: ["a.java"],
1510 current_api: "api/current.txt",
1511 removed_api: "api/removed.txt",
1512 },
1513 }
1514
1515 java_library {
1516 name: "bar",
1517 srcs: [":foo{.public.stubs.source}"],
1518 java_resources: [
1519 ":foo{.public.api.txt}",
1520 ":foo{.public.removed-api.txt}",
1521 ],
1522 }
1523 `)
1524}
1525
1526func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1527 bp := `
1528 java_sdk_library_import {
1529 name: "foo",
1530 public: {
1531 jars: ["a.jar"],
1532 },
1533 }
1534 `
1535
1536 t.Run("stubs.source", func(t *testing.T) {
1537 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1538 java_library {
1539 name: "bar",
1540 srcs: [":foo{.public.stubs.source}"],
1541 java_resources: [
1542 ":foo{.public.api.txt}",
1543 ":foo{.public.removed-api.txt}",
1544 ],
1545 }
1546 `)
1547 })
1548
1549 t.Run("api.txt", func(t *testing.T) {
1550 testJavaError(t, `api.txt not available for api scope public`, bp+`
1551 java_library {
1552 name: "bar",
1553 srcs: ["a.java"],
1554 java_resources: [
1555 ":foo{.public.api.txt}",
1556 ],
1557 }
1558 `)
1559 })
1560
1561 t.Run("removed-api.txt", func(t *testing.T) {
1562 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1563 java_library {
1564 name: "bar",
1565 srcs: ["a.java"],
1566 java_resources: [
1567 ":foo{.public.removed-api.txt}",
1568 ],
1569 }
1570 `)
1571 })
1572}
1573
Paul Duffin3375e352020-04-28 10:44:03 +01001574func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1575 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1576 java_sdk_library {
1577 name: "foo",
1578 srcs: ["a.java", "b.java"],
1579 api_packages: ["foo"],
1580 // Explicitly disable public to test the check that ensures the set of enabled
1581 // scopes is consistent.
1582 public: {
1583 enabled: false,
1584 },
1585 system: {
1586 enabled: true,
1587 },
1588 }
1589 `)
1590}
1591
Paul Duffin87a05a32020-05-12 11:50:28 +01001592func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1593 testJava(t, `
1594 java_sdk_library {
1595 name: "foo",
1596 srcs: ["a.java", "b.java"],
1597 api_packages: ["foo"],
1598 system: {
1599 enabled: true,
1600 sdk_version: "module_current",
1601 },
1602 }
1603 `)
1604}
1605
Paul Duffin0c5bae52020-06-02 13:00:08 +01001606func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
1607 testJava(t, `
1608 java_sdk_library {
1609 name: "foo",
1610 srcs: ["a.java", "b.java"],
1611 api_packages: ["foo"],
1612 system: {
1613 enabled: true,
1614 },
1615 module_lib: {
1616 enabled: true,
1617 },
1618 }
1619 `)
1620}
1621
1622func TestJavaSdkLibrary_SystemServer(t *testing.T) {
1623 testJava(t, `
1624 java_sdk_library {
1625 name: "foo",
1626 srcs: ["a.java", "b.java"],
1627 api_packages: ["foo"],
1628 system: {
1629 enabled: true,
1630 },
1631 system_server: {
1632 enabled: true,
1633 },
1634 }
1635 `)
1636}
1637
Paul Duffin803a9562020-05-20 11:52:25 +01001638func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1639 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1640 java_sdk_library {
1641 name: "foo",
1642 srcs: ["a.java"],
1643 public: {
1644 enabled: false,
1645 },
1646 }
1647
1648 java_library {
1649 name: "baz",
1650 srcs: ["a.java"],
1651 libs: ["foo"],
1652 sdk_version: "module_current",
1653 }
1654 `)
1655}
1656
1657func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1658 testJava(t, `
1659 java_sdk_library {
1660 name: "foo",
1661 srcs: ["a.java"],
1662 system: {
1663 enabled: true,
1664 },
1665 }
1666
1667 java_library {
1668 name: "baz",
1669 srcs: ["a.java"],
1670 libs: ["foo"],
1671 // foo does not have module-lib scope so it should fallback to system
1672 sdk_version: "module_current",
1673 }
1674 `)
1675}
1676
Jiyong Park932cdfe2020-05-28 00:19:53 +09001677func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1678 ctx, _ := testJava(t, `
1679 java_sdk_library {
1680 name: "foo",
1681 srcs: ["a.java"],
1682 system: {
1683 enabled: true,
1684 },
1685 default_to_stubs: true,
1686 }
1687
1688 java_library {
1689 name: "baz",
1690 srcs: ["a.java"],
1691 libs: ["foo"],
1692 // does not have sdk_version set, should fallback to module,
1693 // which will then fallback to system because the module scope
1694 // is not enabled.
1695 }
1696 `)
1697 // The baz library should depend on the system stubs jar.
1698 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1699 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1700 t.Errorf("expected %q, found %#q", expected, actual)
1701 }
1702}
1703
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001704var compilerFlagsTestCases = []struct {
1705 in string
1706 out bool
1707}{
1708 {
1709 in: "a",
1710 out: false,
1711 },
1712 {
1713 in: "-a",
1714 out: true,
1715 },
1716 {
1717 in: "-no-jdk",
1718 out: false,
1719 },
1720 {
1721 in: "-no-stdlib",
1722 out: false,
1723 },
1724 {
1725 in: "-kotlin-home",
1726 out: false,
1727 },
1728 {
1729 in: "-kotlin-home /some/path",
1730 out: false,
1731 },
1732 {
1733 in: "-include-runtime",
1734 out: false,
1735 },
1736 {
1737 in: "-Xintellij-plugin-root",
1738 out: false,
1739 },
1740}
1741
1742type mockContext struct {
1743 android.ModuleContext
1744 result bool
1745}
1746
1747func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1748 // CheckBadCompilerFlags calls this function when the flag should be rejected
1749 ctx.result = false
1750}
1751
1752func TestCompilerFlags(t *testing.T) {
1753 for _, testCase := range compilerFlagsTestCases {
1754 ctx := &mockContext{result: true}
1755 CheckKotlincFlags(ctx, []string{testCase.in})
1756 if ctx.result != testCase.out {
1757 t.Errorf("incorrect output:")
1758 t.Errorf(" input: %#v", testCase.in)
1759 t.Errorf(" expected: %#v", testCase.out)
1760 t.Errorf(" got: %#v", ctx.result)
1761 }
1762 }
1763}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001764
1765// TODO(jungjw): Consider making this more robust by ignoring path order.
1766func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1767 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1768 flags := strings.Split(variables["javacFlags"], " ")
1769 got := ""
1770 for _, flag := range flags {
1771 keyEnd := strings.Index(flag, "=")
1772 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1773 got = flag[keyEnd+1:]
1774 break
1775 }
1776 }
1777 if expected != got {
1778 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1779 }
1780}
1781
1782func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001783 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001784 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001785 bp := `
1786 java_library {
1787 name: "foo",
1788 srcs: ["a.java"],
1789 java_version: "1.8",
1790 }
1791
1792 java_library {
1793 name: "bar",
1794 srcs: ["b.java"],
1795 sdk_version: "none",
1796 system_modules: "none",
1797 patch_module: "java.base",
1798 java_version: "1.8",
1799 }
1800
1801 java_library {
1802 name: "baz",
1803 srcs: ["c.java"],
1804 patch_module: "java.base",
1805 java_version: "1.8",
1806 }
1807 `
1808 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001809
1810 checkPatchModuleFlag(t, ctx, "foo", "")
1811 checkPatchModuleFlag(t, ctx, "bar", "")
1812 checkPatchModuleFlag(t, ctx, "baz", "")
1813 })
1814
Pete Gillin0c2143e2019-05-02 15:32:11 +01001815 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001816 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001817 bp := `
1818 java_library {
1819 name: "foo",
1820 srcs: ["a.java"],
1821 }
1822
1823 java_library {
1824 name: "bar",
1825 srcs: ["b.java"],
1826 sdk_version: "none",
1827 system_modules: "none",
1828 patch_module: "java.base",
1829 }
1830
1831 java_library {
1832 name: "baz",
1833 srcs: ["c.java"],
1834 patch_module: "java.base",
1835 }
1836 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001837 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001838
1839 checkPatchModuleFlag(t, ctx, "foo", "")
1840 expected := "java.base=.:" + buildDir
1841 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09001842 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001843 checkPatchModuleFlag(t, ctx, "baz", expected)
1844 })
1845}
Paul Duffina7b9f422020-01-10 17:12:18 +00001846
1847func TestJavaSystemModules(t *testing.T) {
1848 ctx, _ := testJava(t, `
1849 java_system_modules {
1850 name: "system-modules",
1851 libs: ["system-module1", "system-module2"],
1852 }
1853 java_library {
1854 name: "system-module1",
1855 srcs: ["a.java"],
1856 sdk_version: "none",
1857 system_modules: "none",
1858 }
1859 java_library {
1860 name: "system-module2",
1861 srcs: ["b.java"],
1862 sdk_version: "none",
1863 system_modules: "none",
1864 }
1865 `)
1866
1867 // check the existence of the module
1868 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1869
1870 cmd := systemModules.Rule("jarsTosystemModules")
1871
1872 // make sure the command compiles against the supplied modules.
1873 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1874 if !strings.Contains(cmd.Args["classpath"], module) {
1875 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1876 module)
1877 }
1878 }
1879}
Paul Duffin90169ba2020-01-10 17:16:44 +00001880
1881func TestJavaSystemModulesImport(t *testing.T) {
1882 ctx, _ := testJava(t, `
1883 java_system_modules_import {
1884 name: "system-modules",
1885 libs: ["system-module1", "system-module2"],
1886 }
1887 java_import {
1888 name: "system-module1",
1889 jars: ["a.jar"],
1890 }
1891 java_import {
1892 name: "system-module2",
1893 jars: ["b.jar"],
1894 }
1895 `)
1896
1897 // check the existence of the module
1898 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1899
1900 cmd := systemModules.Rule("jarsTosystemModules")
1901
1902 // make sure the command compiles against the supplied modules.
1903 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1904 if !strings.Contains(cmd.Args["classpath"], module) {
1905 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1906 module)
1907 }
1908 }
1909}
Paul Duffin83a2d962019-11-19 19:44:10 +00001910
1911func TestJavaLibraryWithSystemModules(t *testing.T) {
1912 ctx, _ := testJava(t, `
1913 java_library {
1914 name: "lib-with-source-system-modules",
1915 srcs: [
1916 "a.java",
1917 ],
1918 sdk_version: "none",
1919 system_modules: "source-system-modules",
1920 }
1921
1922 java_library {
1923 name: "source-jar",
1924 srcs: [
1925 "a.java",
1926 ],
1927 }
1928
1929 java_system_modules {
1930 name: "source-system-modules",
1931 libs: ["source-jar"],
1932 }
1933
1934 java_library {
1935 name: "lib-with-prebuilt-system-modules",
1936 srcs: [
1937 "a.java",
1938 ],
1939 sdk_version: "none",
1940 system_modules: "prebuilt-system-modules",
1941 }
1942
1943 java_import {
1944 name: "prebuilt-jar",
1945 jars: ["a.jar"],
1946 }
1947
1948 java_system_modules_import {
1949 name: "prebuilt-system-modules",
1950 libs: ["prebuilt-jar"],
1951 }
1952 `)
1953
1954 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
1955
1956 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
1957}
1958
1959func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
1960 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
1961 bootClasspath := javacRule.Args["bootClasspath"]
1962 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
1963 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
1964 }
1965}
Jiyong Park19604de2020-03-24 16:44:11 +09001966
1967func TestAidlExportIncludeDirsFromImports(t *testing.T) {
1968 ctx, _ := testJava(t, `
1969 java_library {
1970 name: "foo",
1971 srcs: ["aidl/foo/IFoo.aidl"],
1972 libs: ["bar"],
1973 }
1974
1975 java_import {
1976 name: "bar",
1977 jars: ["a.jar"],
1978 aidl: {
1979 export_include_dirs: ["aidl/bar"],
1980 },
1981 }
1982 `)
1983
1984 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
1985 expectedAidlFlag := "-Iaidl/bar"
1986 if !strings.Contains(aidlCommand, expectedAidlFlag) {
1987 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
1988 }
1989}