blob: 23dcd53aba96d9932327fd091a0e87f01310c8cb [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`,
721 // This should be sdklib.stubs but is switched to the prebuilt because it is preferred.
722 `prebuilt_sdklib.stubs`,
723 `sdklib.impl`,
724 `sdklib.stubs.source`,
725 `sdklib.xml`,
726 })
727
728 checkModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
729 `prebuilt_sdklib.stubs`,
730 `sdklib.impl`,
731 `sdklib.xml`,
732 })
Paul Duffin56d44902020-01-31 13:36:25 +0000733}
734
Colin Cross89536d42017-07-07 14:35:50 -0700735func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700736 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700737 java_defaults {
738 name: "defaults",
739 srcs: ["a.java"],
740 libs: ["bar"],
741 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700742 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700743 }
744
745 java_library {
746 name: "foo",
747 defaults: ["defaults"],
748 }
749
750 java_library {
751 name: "bar",
752 srcs: ["b.java"],
753 }
754
755 java_library {
756 name: "baz",
757 srcs: ["c.java"],
758 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700759
760 android_test {
761 name: "atestOptimize",
762 defaults: ["defaults"],
763 optimize: {enabled: true},
764 }
765
766 android_test {
767 name: "atestNoOptimize",
768 defaults: ["defaults"],
769 }
770
771 android_test {
772 name: "atestDefault",
773 srcs: ["a.java"],
774 }
Colin Cross89536d42017-07-07 14:35:50 -0700775 `)
776
Colin Cross4c428df2017-09-15 17:36:05 -0700777 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700778 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700779
780 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
781 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
782 }
783
Nan Zhanged19fc32017-10-19 13:06:22 -0700784 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
785 if !strings.Contains(javac.Args["classpath"], barTurbine) {
786 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700787 }
788
Colin Cross1ee23172017-10-18 14:44:18 -0700789 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700790 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
791 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700792 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700793
794 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
795 if atestOptimize.Output == nil {
796 t.Errorf("atestOptimize should optimize APK")
797 }
798
799 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
800 if atestNoOptimize.Output == nil {
801 t.Errorf("atestNoOptimize should not optimize APK")
802 }
803
804 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
805 if atestDefault.Output == nil {
806 t.Errorf("atestDefault should optimize APK")
807 }
Colin Cross89536d42017-07-07 14:35:50 -0700808}
809
Colin Cross0f37af02017-09-27 17:42:05 -0700810func TestResources(t *testing.T) {
811 var table = []struct {
812 name string
813 prop string
814 extra string
815 args string
816 }{
817 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700818 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700819 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800820 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700821 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700822 },
823 {
824 // Test that a module with java_resources includes the files
825 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700826 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
827 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700828 },
829 {
830 // Test that a module with a filegroup in java_resources includes the files with the
831 // path prefix
832 name: "resource filegroup",
833 prop: `java_resources: [":foo-res"]`,
834 extra: `
835 filegroup {
836 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800837 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700838 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700839 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700840 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700841 },
842 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700843 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
844 name: "wildcard dirs",
845 prop: `java_resource_dirs: ["java-res/*"]`,
846 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
847 },
848 {
849 // Test that a module exclude_java_resource_dirs excludes the files
850 name: "wildcard dirs",
851 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
852 args: "-C java-res/a -f java-res/a/a",
853 },
Colin Crosscedd4762018-09-13 11:26:19 -0700854 {
855 // Test wildcards in java_resources
856 name: "wildcard files",
857 prop: `java_resources: ["java-res/**/*"]`,
858 args: "-C . -f java-res/a/a -f java-res/b/b",
859 },
860 {
861 // Test exclude_java_resources with java_resources
862 name: "wildcard files with exclude",
863 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
864 args: "-C . -f java-res/a/a",
865 },
866 {
867 // Test exclude_java_resources with java_resource_dirs
868 name: "resource dirs with exclude files",
869 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
870 args: "-C java-res -f java-res/a/a",
871 },
872 {
873 // Test exclude_java_resource_dirs with java_resource_dirs
874 name: "resource dirs with exclude files",
875 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
876 args: "-C java-res -f java-res/a/a -f java-res/b/b",
877 },
Colin Cross0f37af02017-09-27 17:42:05 -0700878 }
879
880 for _, test := range table {
881 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700882 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700883 java_library {
884 name: "foo",
885 srcs: [
886 "a.java",
887 "b.java",
888 "c.java",
889 ],
890 `+test.prop+`,
891 }
Colin Cross238c1f32020-06-07 16:58:18 -0700892 `+test.extra,
893 map[string][]byte{
894 "java-res/a/a": nil,
895 "java-res/b/b": nil,
896 "java-res2/a": nil,
897 },
898 )
Colin Cross0f37af02017-09-27 17:42:05 -0700899
Colin Cross331a1212018-08-15 20:40:52 -0700900 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700901 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700902
903 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
904 t.Errorf("foo combined jars %v does not contain %q",
905 foo.Inputs.Strings(), fooRes.Output.String())
906 }
907
Colin Crossaf9c55b2017-10-03 14:50:08 -0700908 if fooRes.Args["jarArgs"] != test.args {
909 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700910 fooRes.Args["jarArgs"], test.args)
911 }
912 })
913 }
914}
915
Colin Cross0c4ce212019-05-03 15:28:19 -0700916func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700917 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700918 java_library {
919 name: "foo",
920 srcs: [
921 "a.java",
922 "b.java",
923 "c.java",
924 ],
925 include_srcs: true,
926 }
927
928 java_library {
929 name: "bar",
930 srcs: [
931 "a.java",
932 "b.java",
933 "c.java",
934 ],
935 java_resource_dirs: ["java-res"],
936 include_srcs: true,
937 }
Colin Cross238c1f32020-06-07 16:58:18 -0700938 `, map[string][]byte{
939 "java-res/a/a": nil,
940 "java-res/b/b": nil,
941 "java-res2/a": nil,
942 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700943
944 // Test a library with include_srcs: true
945 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
946 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
947
948 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
949 t.Errorf("foo combined jars %v does not contain %q", w, g)
950 }
951
952 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
953 t.Errorf("foo source jar args %q is not %q", w, g)
954 }
955
956 // Test a library with include_srcs: true and resources
957 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
958 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
959 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
960 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
961
962 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
963 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
964 }
965
966 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
967 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
968 }
969
970 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
971 t.Errorf("bar combined jars %v does not contain %q", w, g)
972 }
973
974 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
975 t.Errorf("bar source jar args %q is not %q", w, g)
976 }
977
978 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
979 t.Errorf("bar resource jar args %q is not %q", w, g)
980 }
981}
982
Colin Cross54190b32017-10-09 15:34:10 -0700983func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700984 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700985 java_library {
986 name: "foo",
987 srcs: [
988 "a*.java",
989 ":gen",
990 "b*.java",
991 ],
992 }
993
994 genrule {
995 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800996 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700997 out: ["gen.java"],
998 }
Colin Cross238c1f32020-06-07 16:58:18 -0700999 `, map[string][]byte{
1000 "a.java": nil,
1001 "b.java": nil,
1002 })
Colin Cross54190b32017-10-09 15:34:10 -07001003
1004 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1005 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1006
Colin Cross15e86d92017-10-20 15:07:08 -07001007 if filepath.Base(genrule.Output.String()) != "gen.java" {
1008 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001009 }
1010
1011 if len(javac.Inputs) != 3 ||
1012 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001013 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001014 javac.Inputs[2].String() != "b.java" {
1015 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1016 }
1017}
1018
Nan Zhang61eaedb2017-11-02 13:28:15 -07001019func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001020 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001021 java_library {
1022 name: "foo",
1023 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001024 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001025 }
1026
1027 java_library {
1028 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001029 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001030 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001031 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001032 }
1033
1034 java_library {
1035 name: "baz",
1036 srcs: ["c.java"],
1037 libs: ["bar"],
1038 sdk_version: "14",
1039 }
1040 `)
1041
1042 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1043 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1044 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1045 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1046 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1047
1048 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1049 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1050 }
1051
1052 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1053 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1054 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1055 }
1056 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1057 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1058 }
1059 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1060 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1061 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001062 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001063 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001064 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001065 }
1066}
1067
1068func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001069 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001070 java_library {
1071 name: "bar",
1072 srcs: ["a.java","b.java","c.java"],
1073 javac_shard_size: 1
1074 }
1075 `)
1076
1077 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1078 for i := 0; i < 3; i++ {
1079 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1080 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1081 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1082 }
1083 }
1084}
1085
Nan Zhang581fd212018-01-10 16:06:12 -08001086func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001087 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001088 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001089 name: "droiddoc-templates-sdk",
1090 path: ".",
1091 }
Jiyong Park29074592019-07-07 16:27:47 +09001092 filegroup {
1093 name: "bar-doc-aidl-srcs",
1094 srcs: ["bar-doc/IBar.aidl"],
1095 path: "bar-doc",
1096 }
Nan Zhang581fd212018-01-10 16:06:12 -08001097 droiddoc {
1098 name: "bar-doc",
1099 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001100 "bar-doc/a.java",
Jiyong Park1e440682018-05-23 18:42:04 +09001101 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +09001102 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -08001103 ],
1104 exclude_srcs: [
1105 "bar-doc/b.java"
1106 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001107 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001108 hdf: [
1109 "android.whichdoc offline",
1110 ],
1111 knowntags: [
1112 "bar-doc/known_oj_tags.txt",
1113 ],
1114 proofread_file: "libcore-proofread.txt",
1115 todo_file: "libcore-docs-todo.html",
1116 args: "-offlinemode -title \"libcore\"",
1117 }
Colin Cross238c1f32020-06-07 16:58:18 -07001118 `,
1119 map[string][]byte{
1120 "bar-doc/a.java": nil,
1121 "bar-doc/b.java": nil,
1122 })
Nan Zhang581fd212018-01-10 16:06:12 -08001123
Colin Crossc0806172019-06-14 18:51:47 -07001124 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +09001125 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -07001126 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +09001127 javaSrcs = append(javaSrcs, i.Base())
1128 }
Colin Crossc0806172019-06-14 18:51:47 -07001129 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
1130 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +09001131 }
1132
Colin Crossc0806172019-06-14 18:51:47 -07001133 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
1134 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
1135 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1136 }
1137
1138 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1139 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001140 }
Nan Zhang581fd212018-01-10 16:06:12 -08001141}
1142
Paul Duffin83a2d962019-11-19 19:44:10 +00001143func TestDroidstubsWithSystemModules(t *testing.T) {
1144 ctx, _ := testJava(t, `
1145 droidstubs {
1146 name: "stubs-source-system-modules",
1147 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001148 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001149 ],
1150 sdk_version: "none",
1151 system_modules: "source-system-modules",
1152 }
1153
1154 java_library {
1155 name: "source-jar",
1156 srcs: [
1157 "a.java",
1158 ],
1159 }
1160
1161 java_system_modules {
1162 name: "source-system-modules",
1163 libs: ["source-jar"],
1164 }
1165
1166 droidstubs {
1167 name: "stubs-prebuilt-system-modules",
1168 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001169 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001170 ],
1171 sdk_version: "none",
1172 system_modules: "prebuilt-system-modules",
1173 }
1174
1175 java_import {
1176 name: "prebuilt-jar",
1177 jars: ["a.jar"],
1178 }
1179
1180 java_system_modules_import {
1181 name: "prebuilt-system-modules",
1182 libs: ["prebuilt-jar"],
1183 }
1184 `)
1185
1186 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1187
1188 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1189}
1190
1191func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1192 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1193 var systemJars []string
1194 for _, i := range metalavaRule.Implicits {
1195 systemJars = append(systemJars, i.Base())
1196 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001197 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001198 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1199 }
1200}
1201
Colin Cross54250902017-12-05 09:28:08 -08001202func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001203 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001204 java_library {
1205 name: "foo",
1206 srcs: ["a.java"],
1207 }
1208
1209 java_genrule {
1210 name: "jargen",
1211 tool_files: ["b.java"],
1212 cmd: "$(location b.java) $(in) $(out)",
1213 out: ["jargen.jar"],
1214 srcs: [":foo"],
1215 }
1216
1217 java_library {
1218 name: "bar",
1219 static_libs: ["jargen"],
1220 srcs: ["c.java"],
1221 }
1222
1223 java_library {
1224 name: "baz",
1225 libs: ["jargen"],
1226 srcs: ["c.java"],
1227 }
1228 `)
1229
1230 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1231 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1232 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1233 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1234 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1235
1236 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1237 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1238 }
1239
1240 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1241 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1242 }
1243
1244 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1245 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1246 }
1247
1248 if len(barCombined.Inputs) != 2 ||
1249 barCombined.Inputs[0].String() != bar.Output.String() ||
1250 barCombined.Inputs[1].String() != jargen.Output.String() {
1251 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1252 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1253 }
1254}
1255
Nan Zhang27e284d2018-02-09 21:03:53 +00001256func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001257 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001258 java_library {
1259 name: "foo",
1260 srcs: ["a.java", ":foo-srcs"],
1261 exclude_srcs: ["a.java", ":foo-excludes"],
1262 }
1263
1264 filegroup {
1265 name: "foo-srcs",
1266 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1267 }
1268
1269 filegroup {
1270 name: "foo-excludes",
1271 srcs: ["java-fg/a.java", "java-fg/b.java"],
1272 }
1273 `)
1274
1275 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1276
1277 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1278 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1279 }
1280}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001281
Paul Duffin52d398a2019-06-11 12:31:14 +01001282func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001283 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001284 "libcore/Android.bp": []byte(`
1285 java_library {
1286 name: "core",
1287 sdk_version: "none",
1288 system_modules: "none",
1289 }`),
1290 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001291 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001292 run(t, ctx, config)
1293}
1294
Jiyong Parkc678ad32018-04-10 13:07:10 +09001295func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001296 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001297 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001298 name: "droiddoc-templates-sdk",
1299 path: ".",
1300 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001301 java_sdk_library {
1302 name: "foo",
1303 srcs: ["a.java", "b.java"],
1304 api_packages: ["foo"],
1305 }
1306 java_sdk_library {
1307 name: "bar",
1308 srcs: ["a.java", "b.java"],
1309 api_packages: ["bar"],
1310 }
1311 java_library {
1312 name: "baz",
1313 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001314 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001315 sdk_version: "system_current",
1316 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001317 java_sdk_library {
1318 name: "barney",
1319 srcs: ["c.java"],
1320 api_only: true,
1321 }
1322 java_sdk_library {
1323 name: "betty",
1324 srcs: ["c.java"],
1325 shared_library: false,
1326 }
Paul Duffin859fe962020-05-15 10:20:31 +01001327 java_sdk_library_import {
1328 name: "quuz",
1329 public: {
1330 jars: ["c.jar"],
1331 },
1332 }
1333 java_sdk_library_import {
1334 name: "fred",
1335 public: {
1336 jars: ["b.jar"],
1337 },
1338 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001339 java_sdk_library_import {
1340 name: "wilma",
1341 public: {
1342 jars: ["b.jar"],
1343 },
1344 shared_library: false,
1345 }
Jiyong Park1be96912018-05-28 18:02:19 +09001346 java_library {
1347 name: "qux",
1348 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001349 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001350 sdk_version: "system_current",
1351 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001352 java_library {
1353 name: "baz-test",
1354 srcs: ["c.java"],
1355 libs: ["foo"],
1356 sdk_version: "test_current",
1357 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001358 java_library {
1359 name: "baz-29",
1360 srcs: ["c.java"],
1361 libs: ["foo"],
1362 sdk_version: "system_29",
1363 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001364 `)
1365
1366 // check the existence of the internal modules
1367 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001368 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1369 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1370 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1371 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1372 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1373 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001374 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001375 ctx.ModuleForTests("foo.api.public.28", "")
1376 ctx.ModuleForTests("foo.api.system.28", "")
1377 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001378
1379 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1380 // tests if baz is actually linked to the stubs lib
1381 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1382 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1383 "foo.stubs.system.jar")
1384 }
1385 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001386 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001387 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001388 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001389 }
1390 // test if baz is not linked to the system variant of foo
1391 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1392 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1393 "foo.stubs.jar")
1394 }
Jiyong Park1be96912018-05-28 18:02:19 +09001395
Paul Duffin726d23c2020-01-22 16:30:37 +00001396 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1397 // tests if baz-test is actually linked to the test stubs lib
1398 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1399 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1400 "foo.stubs.test.jar")
1401 }
1402
Paul Duffina2db18f2020-01-22 17:11:15 +00001403 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1404 // tests if baz-29 is actually linked to the system 29 stubs lib
1405 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1406 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1407 "prebuilts/sdk/29/system/foo.jar")
1408 }
1409
Jiyong Park1be96912018-05-28 18:02:19 +09001410 // test if baz has exported SDK lib names foo and bar to qux
1411 qux := ctx.ModuleForTests("qux", "android_common")
1412 if quxLib, ok := qux.Module().(*Library); ok {
1413 sdkLibs := quxLib.ExportedSdkLibs()
Paul Duffin859fe962020-05-15 10:20:31 +01001414 sort.Strings(sdkLibs)
1415 if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
1416 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001417 }
1418 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001419}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001420
Paul Duffindaaa3322020-05-26 18:13:57 +01001421func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1422 ctx, _ := testJava(t, `
1423 java_sdk_library {
1424 name: "foo",
1425 srcs: ["a.java"],
1426 api_only: true,
1427 public: {
1428 enabled: true,
1429 },
1430 }
1431
1432 java_library {
1433 name: "bar",
1434 srcs: ["b.java"],
1435 libs: ["foo"],
1436 }
1437 `)
1438
1439 // The bar library should depend on the stubs jar.
1440 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1441 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1442 t.Errorf("expected %q, found %#q", expected, actual)
1443 }
1444}
1445
Paul Duffin46dc45a2020-05-14 15:39:10 +01001446func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1447 testJava(t, `
1448 java_sdk_library {
1449 name: "foo",
1450 srcs: ["a.java"],
1451 api_packages: ["foo"],
1452 public: {
1453 enabled: true,
1454 },
1455 }
1456
1457 java_library {
1458 name: "bar",
1459 srcs: ["b.java", ":foo{.public.stubs.source}"],
1460 }
1461 `)
1462}
1463
1464func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1465 testJavaError(t, `"foo" does not provide api scope system`, `
1466 java_sdk_library {
1467 name: "foo",
1468 srcs: ["a.java"],
1469 api_packages: ["foo"],
1470 public: {
1471 enabled: true,
1472 },
1473 }
1474
1475 java_library {
1476 name: "bar",
1477 srcs: ["b.java", ":foo{.system.stubs.source}"],
1478 }
1479 `)
1480}
1481
Paul Duffinca8d9a52020-06-26 22:20:25 +01001482func TestJavaSdkLibrary_Deps(t *testing.T) {
1483 ctx, _ := testJava(t, `
1484 java_sdk_library {
1485 name: "sdklib",
1486 srcs: ["a.java"],
1487 sdk_version: "none",
1488 system_modules: "none",
1489 public: {
1490 enabled: true,
1491 },
1492 }
1493 `)
1494
1495 checkModuleDependencies(t, ctx, "sdklib", "android_common", []string{
1496 `dex2oatd`,
1497 `sdklib.impl`,
1498 `sdklib.stubs`,
1499 `sdklib.stubs.source`,
1500 `sdklib.xml`,
1501 })
1502}
1503
Paul Duffin46dc45a2020-05-14 15:39:10 +01001504func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1505 testJava(t, `
1506 java_sdk_library_import {
1507 name: "foo",
1508 public: {
1509 jars: ["a.jar"],
1510 stub_srcs: ["a.java"],
1511 current_api: "api/current.txt",
1512 removed_api: "api/removed.txt",
1513 },
1514 }
1515
1516 java_library {
1517 name: "bar",
1518 srcs: [":foo{.public.stubs.source}"],
1519 java_resources: [
1520 ":foo{.public.api.txt}",
1521 ":foo{.public.removed-api.txt}",
1522 ],
1523 }
1524 `)
1525}
1526
1527func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1528 bp := `
1529 java_sdk_library_import {
1530 name: "foo",
1531 public: {
1532 jars: ["a.jar"],
1533 },
1534 }
1535 `
1536
1537 t.Run("stubs.source", func(t *testing.T) {
1538 testJavaError(t, `stubs.source not available for api scope public`, bp+`
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
1550 t.Run("api.txt", func(t *testing.T) {
1551 testJavaError(t, `api.txt not available for api scope public`, bp+`
1552 java_library {
1553 name: "bar",
1554 srcs: ["a.java"],
1555 java_resources: [
1556 ":foo{.public.api.txt}",
1557 ],
1558 }
1559 `)
1560 })
1561
1562 t.Run("removed-api.txt", func(t *testing.T) {
1563 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1564 java_library {
1565 name: "bar",
1566 srcs: ["a.java"],
1567 java_resources: [
1568 ":foo{.public.removed-api.txt}",
1569 ],
1570 }
1571 `)
1572 })
1573}
1574
Paul Duffin3375e352020-04-28 10:44:03 +01001575func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1576 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1577 java_sdk_library {
1578 name: "foo",
1579 srcs: ["a.java", "b.java"],
1580 api_packages: ["foo"],
1581 // Explicitly disable public to test the check that ensures the set of enabled
1582 // scopes is consistent.
1583 public: {
1584 enabled: false,
1585 },
1586 system: {
1587 enabled: true,
1588 },
1589 }
1590 `)
1591}
1592
Paul Duffin87a05a32020-05-12 11:50:28 +01001593func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1594 testJava(t, `
1595 java_sdk_library {
1596 name: "foo",
1597 srcs: ["a.java", "b.java"],
1598 api_packages: ["foo"],
1599 system: {
1600 enabled: true,
1601 sdk_version: "module_current",
1602 },
1603 }
1604 `)
1605}
1606
Paul Duffin0c5bae52020-06-02 13:00:08 +01001607func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
1608 testJava(t, `
1609 java_sdk_library {
1610 name: "foo",
1611 srcs: ["a.java", "b.java"],
1612 api_packages: ["foo"],
1613 system: {
1614 enabled: true,
1615 },
1616 module_lib: {
1617 enabled: true,
1618 },
1619 }
1620 `)
1621}
1622
1623func TestJavaSdkLibrary_SystemServer(t *testing.T) {
1624 testJava(t, `
1625 java_sdk_library {
1626 name: "foo",
1627 srcs: ["a.java", "b.java"],
1628 api_packages: ["foo"],
1629 system: {
1630 enabled: true,
1631 },
1632 system_server: {
1633 enabled: true,
1634 },
1635 }
1636 `)
1637}
1638
Paul Duffin803a9562020-05-20 11:52:25 +01001639func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1640 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1641 java_sdk_library {
1642 name: "foo",
1643 srcs: ["a.java"],
1644 public: {
1645 enabled: false,
1646 },
1647 }
1648
1649 java_library {
1650 name: "baz",
1651 srcs: ["a.java"],
1652 libs: ["foo"],
1653 sdk_version: "module_current",
1654 }
1655 `)
1656}
1657
1658func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1659 testJava(t, `
1660 java_sdk_library {
1661 name: "foo",
1662 srcs: ["a.java"],
1663 system: {
1664 enabled: true,
1665 },
1666 }
1667
1668 java_library {
1669 name: "baz",
1670 srcs: ["a.java"],
1671 libs: ["foo"],
1672 // foo does not have module-lib scope so it should fallback to system
1673 sdk_version: "module_current",
1674 }
1675 `)
1676}
1677
Jiyong Park932cdfe2020-05-28 00:19:53 +09001678func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1679 ctx, _ := testJava(t, `
1680 java_sdk_library {
1681 name: "foo",
1682 srcs: ["a.java"],
1683 system: {
1684 enabled: true,
1685 },
1686 default_to_stubs: true,
1687 }
1688
1689 java_library {
1690 name: "baz",
1691 srcs: ["a.java"],
1692 libs: ["foo"],
1693 // does not have sdk_version set, should fallback to module,
1694 // which will then fallback to system because the module scope
1695 // is not enabled.
1696 }
1697 `)
1698 // The baz library should depend on the system stubs jar.
1699 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1700 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1701 t.Errorf("expected %q, found %#q", expected, actual)
1702 }
1703}
1704
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001705var compilerFlagsTestCases = []struct {
1706 in string
1707 out bool
1708}{
1709 {
1710 in: "a",
1711 out: false,
1712 },
1713 {
1714 in: "-a",
1715 out: true,
1716 },
1717 {
1718 in: "-no-jdk",
1719 out: false,
1720 },
1721 {
1722 in: "-no-stdlib",
1723 out: false,
1724 },
1725 {
1726 in: "-kotlin-home",
1727 out: false,
1728 },
1729 {
1730 in: "-kotlin-home /some/path",
1731 out: false,
1732 },
1733 {
1734 in: "-include-runtime",
1735 out: false,
1736 },
1737 {
1738 in: "-Xintellij-plugin-root",
1739 out: false,
1740 },
1741}
1742
1743type mockContext struct {
1744 android.ModuleContext
1745 result bool
1746}
1747
1748func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1749 // CheckBadCompilerFlags calls this function when the flag should be rejected
1750 ctx.result = false
1751}
1752
1753func TestCompilerFlags(t *testing.T) {
1754 for _, testCase := range compilerFlagsTestCases {
1755 ctx := &mockContext{result: true}
1756 CheckKotlincFlags(ctx, []string{testCase.in})
1757 if ctx.result != testCase.out {
1758 t.Errorf("incorrect output:")
1759 t.Errorf(" input: %#v", testCase.in)
1760 t.Errorf(" expected: %#v", testCase.out)
1761 t.Errorf(" got: %#v", ctx.result)
1762 }
1763 }
1764}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001765
1766// TODO(jungjw): Consider making this more robust by ignoring path order.
1767func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1768 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1769 flags := strings.Split(variables["javacFlags"], " ")
1770 got := ""
1771 for _, flag := range flags {
1772 keyEnd := strings.Index(flag, "=")
1773 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1774 got = flag[keyEnd+1:]
1775 break
1776 }
1777 }
1778 if expected != got {
1779 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1780 }
1781}
1782
1783func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001784 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001785 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001786 bp := `
1787 java_library {
1788 name: "foo",
1789 srcs: ["a.java"],
1790 java_version: "1.8",
1791 }
1792
1793 java_library {
1794 name: "bar",
1795 srcs: ["b.java"],
1796 sdk_version: "none",
1797 system_modules: "none",
1798 patch_module: "java.base",
1799 java_version: "1.8",
1800 }
1801
1802 java_library {
1803 name: "baz",
1804 srcs: ["c.java"],
1805 patch_module: "java.base",
1806 java_version: "1.8",
1807 }
1808 `
1809 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001810
1811 checkPatchModuleFlag(t, ctx, "foo", "")
1812 checkPatchModuleFlag(t, ctx, "bar", "")
1813 checkPatchModuleFlag(t, ctx, "baz", "")
1814 })
1815
Pete Gillin0c2143e2019-05-02 15:32:11 +01001816 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001817 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001818 bp := `
1819 java_library {
1820 name: "foo",
1821 srcs: ["a.java"],
1822 }
1823
1824 java_library {
1825 name: "bar",
1826 srcs: ["b.java"],
1827 sdk_version: "none",
1828 system_modules: "none",
1829 patch_module: "java.base",
1830 }
1831
1832 java_library {
1833 name: "baz",
1834 srcs: ["c.java"],
1835 patch_module: "java.base",
1836 }
1837 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001838 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001839
1840 checkPatchModuleFlag(t, ctx, "foo", "")
1841 expected := "java.base=.:" + buildDir
1842 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09001843 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001844 checkPatchModuleFlag(t, ctx, "baz", expected)
1845 })
1846}
Paul Duffina7b9f422020-01-10 17:12:18 +00001847
1848func TestJavaSystemModules(t *testing.T) {
1849 ctx, _ := testJava(t, `
1850 java_system_modules {
1851 name: "system-modules",
1852 libs: ["system-module1", "system-module2"],
1853 }
1854 java_library {
1855 name: "system-module1",
1856 srcs: ["a.java"],
1857 sdk_version: "none",
1858 system_modules: "none",
1859 }
1860 java_library {
1861 name: "system-module2",
1862 srcs: ["b.java"],
1863 sdk_version: "none",
1864 system_modules: "none",
1865 }
1866 `)
1867
1868 // check the existence of the module
1869 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1870
1871 cmd := systemModules.Rule("jarsTosystemModules")
1872
1873 // make sure the command compiles against the supplied modules.
1874 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1875 if !strings.Contains(cmd.Args["classpath"], module) {
1876 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1877 module)
1878 }
1879 }
1880}
Paul Duffin90169ba2020-01-10 17:16:44 +00001881
1882func TestJavaSystemModulesImport(t *testing.T) {
1883 ctx, _ := testJava(t, `
1884 java_system_modules_import {
1885 name: "system-modules",
1886 libs: ["system-module1", "system-module2"],
1887 }
1888 java_import {
1889 name: "system-module1",
1890 jars: ["a.jar"],
1891 }
1892 java_import {
1893 name: "system-module2",
1894 jars: ["b.jar"],
1895 }
1896 `)
1897
1898 // check the existence of the module
1899 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1900
1901 cmd := systemModules.Rule("jarsTosystemModules")
1902
1903 // make sure the command compiles against the supplied modules.
1904 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1905 if !strings.Contains(cmd.Args["classpath"], module) {
1906 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1907 module)
1908 }
1909 }
1910}
Paul Duffin83a2d962019-11-19 19:44:10 +00001911
1912func TestJavaLibraryWithSystemModules(t *testing.T) {
1913 ctx, _ := testJava(t, `
1914 java_library {
1915 name: "lib-with-source-system-modules",
1916 srcs: [
1917 "a.java",
1918 ],
1919 sdk_version: "none",
1920 system_modules: "source-system-modules",
1921 }
1922
1923 java_library {
1924 name: "source-jar",
1925 srcs: [
1926 "a.java",
1927 ],
1928 }
1929
1930 java_system_modules {
1931 name: "source-system-modules",
1932 libs: ["source-jar"],
1933 }
1934
1935 java_library {
1936 name: "lib-with-prebuilt-system-modules",
1937 srcs: [
1938 "a.java",
1939 ],
1940 sdk_version: "none",
1941 system_modules: "prebuilt-system-modules",
1942 }
1943
1944 java_import {
1945 name: "prebuilt-jar",
1946 jars: ["a.jar"],
1947 }
1948
1949 java_system_modules_import {
1950 name: "prebuilt-system-modules",
1951 libs: ["prebuilt-jar"],
1952 }
1953 `)
1954
1955 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
1956
1957 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
1958}
1959
1960func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
1961 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
1962 bootClasspath := javacRule.Args["bootClasspath"]
1963 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
1964 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
1965 }
1966}
Jiyong Park19604de2020-03-24 16:44:11 +09001967
1968func TestAidlExportIncludeDirsFromImports(t *testing.T) {
1969 ctx, _ := testJava(t, `
1970 java_library {
1971 name: "foo",
1972 srcs: ["aidl/foo/IFoo.aidl"],
1973 libs: ["bar"],
1974 }
1975
1976 java_import {
1977 name: "bar",
1978 jars: ["a.jar"],
1979 aidl: {
1980 export_include_dirs: ["aidl/bar"],
1981 },
1982 }
1983 `)
1984
1985 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
1986 expectedAidlFlag := "-Iaidl/bar"
1987 if !strings.Contains(aidlCommand, expectedAidlFlag) {
1988 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
1989 }
1990}