blob: c751ea4e7cb4786db1f9f0ebcd04c870e131a363 [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"
Nan Zhang61eaedb2017-11-02 13:28:15 -070023 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070024 "strings"
25 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070026
Jeongik Cha28df2572019-11-11 10:46:36 +090027 "github.com/google/blueprint/proptools"
28
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/android"
30 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080031 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070032 "android/soong/genrule"
Liz Kammerdd849a82020-06-12 16:38:45 -070033 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070034)
35
36var buildDir string
37
38func setUp() {
39 var err error
40 buildDir, err = ioutil.TempDir("", "soong_java_test")
41 if err != nil {
42 panic(err)
43 }
44}
45
46func tearDown() {
47 os.RemoveAll(buildDir)
48}
49
50func TestMain(m *testing.M) {
51 run := func() int {
52 setUp()
53 defer tearDown()
54
55 return m.Run()
56 }
57
58 os.Exit(run())
59}
Colin Cross527012a2017-11-30 22:56:16 -080060
Colin Cross98be1bb2019-12-13 20:41:13 -080061func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000062 bp += dexpreopt.BpToolModulesForTest()
63
Martin Stjernholm75a48d82020-01-10 20:32:59 +000064 config := TestConfig(buildDir, env, bp, fs)
65
66 // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that
67 // it doesn't create a real one, which would fail.
68 _ = dexpreopt.GlobalSoongConfigForTests(config)
69
70 return config
Colin Cross1369cdb2017-09-29 17:58:17 -070071}
72
Colin Cross98be1bb2019-12-13 20:41:13 -080073func testContext() *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070074
Colin Cross4c428df2017-09-15 17:36:05 -070075 ctx := android.NewTestArchContext()
Paul Duffinf9b1da02019-12-18 19:51:55 +000076 RegisterJavaBuildComponents(ctx)
77 RegisterAppBuildComponents(ctx)
78 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000079 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000080 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080081 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080082 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
83 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070084 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
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
Liz Kammerdd849a82020-06-12 16:38:45 -070093 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070094 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080095 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070096 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070097
Paul Duffin021f4e52020-07-30 16:04:17 +010098 android.RegisterPrebuiltMutators(ctx)
99
Colin Crossa4f08812018-10-02 22:03:40 -0700100 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000101 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700102
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000103 dexpreopt.RegisterToolModulesForTest(ctx)
104
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700105 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
106 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
107 })
108
Colin Cross527012a2017-11-30 22:56:16 -0800109 return ctx
110}
111
112func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800113 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800114
Colin Cross98be1bb2019-12-13 20:41:13 -0800115 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000116 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800117
Colin Cross98be1bb2019-12-13 20:41:13 -0800118 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100119 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800120 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700121 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800122 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800123}
124
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900125func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900126 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800127 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900128}
129
Colin Cross98be1bb2019-12-13 20:41:13 -0800130func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900131 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800132 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900133
Colin Cross98be1bb2019-12-13 20:41:13 -0800134 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000135 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900136
Colin Cross98be1bb2019-12-13 20:41:13 -0800137 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900138 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
139 if len(errs) > 0 {
140 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900141 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900142 }
143 _, errs = ctx.PrepareBuildActions(config)
144 if len(errs) > 0 {
145 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900146 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900147 }
148
149 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900150
151 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900152}
153
Colin Cross238c1f32020-06-07 16:58:18 -0700154func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
155 t.Helper()
156 return testJavaWithConfig(t, testConfig(nil, bp, fs))
157}
158
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700159func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800160 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700161 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900162}
163
Colin Cross98be1bb2019-12-13 20:41:13 -0800164func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900165 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800166 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800167 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700168
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700169 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700170}
171
Colin Cross2acdae82017-09-15 19:44:24 -0700172func moduleToPath(name string) string {
173 switch {
174 case name == `""`:
175 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700176 case strings.HasSuffix(name, ".jar"):
177 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700178 default:
179 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700180 }
181}
182
Jeongik Chae403e9e2019-12-07 00:16:24 +0900183func TestJavaLinkType(t *testing.T) {
184 testJava(t, `
185 java_library {
186 name: "foo",
187 srcs: ["a.java"],
188 libs: ["bar"],
189 static_libs: ["baz"],
190 }
191
192 java_library {
193 name: "bar",
194 sdk_version: "current",
195 srcs: ["b.java"],
196 }
197
198 java_library {
199 name: "baz",
200 sdk_version: "system_current",
201 srcs: ["c.java"],
202 }
203 `)
204
205 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.", `
206 java_library {
207 name: "foo",
208 srcs: ["a.java"],
209 libs: ["bar"],
210 sdk_version: "current",
211 static_libs: ["baz"],
212 }
213
214 java_library {
215 name: "bar",
216 sdk_version: "current",
217 srcs: ["b.java"],
218 }
219
220 java_library {
221 name: "baz",
222 sdk_version: "system_current",
223 srcs: ["c.java"],
224 }
225 `)
226
227 testJava(t, `
228 java_library {
229 name: "foo",
230 srcs: ["a.java"],
231 libs: ["bar"],
232 sdk_version: "system_current",
233 static_libs: ["baz"],
234 }
235
236 java_library {
237 name: "bar",
238 sdk_version: "current",
239 srcs: ["b.java"],
240 }
241
242 java_library {
243 name: "baz",
244 sdk_version: "system_current",
245 srcs: ["c.java"],
246 }
247 `)
248
249 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.", `
250 java_library {
251 name: "foo",
252 srcs: ["a.java"],
253 libs: ["bar"],
254 sdk_version: "system_current",
255 static_libs: ["baz"],
256 }
257
258 java_library {
259 name: "bar",
260 sdk_version: "current",
261 srcs: ["b.java"],
262 }
263
264 java_library {
265 name: "baz",
266 srcs: ["c.java"],
267 }
268 `)
269}
270
Colin Cross72bb3632017-07-13 16:23:21 -0700271func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700272 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700273 java_library {
274 name: "foo",
275 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700276 libs: ["bar"],
277 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700278 }
279
280 java_library {
281 name: "bar",
282 srcs: ["b.java"],
283 }
284
285 java_library {
286 name: "baz",
287 srcs: ["c.java"],
288 }
Colin Crossd5934c82017-10-02 13:55:26 -0700289 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700290
Colin Cross4c428df2017-09-15 17:36:05 -0700291 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700292 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700293
294 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
295 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
296 }
297
Colin Cross1ee23172017-10-18 14:44:18 -0700298 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700299 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
300 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700301
Nan Zhanged19fc32017-10-19 13:06:22 -0700302 if !strings.Contains(javac.Args["classpath"], barTurbine) {
303 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700304 }
305
Nan Zhanged19fc32017-10-19 13:06:22 -0700306 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
307 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700308 }
309
310 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
311 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700312 }
313}
314
Artur Satayev9cf46692019-11-26 18:08:34 +0000315func TestExportedPlugins(t *testing.T) {
316 type Result struct {
317 library string
318 processors string
319 }
320 var tests = []struct {
321 name string
322 extra string
323 results []Result
324 }{
325 {
326 name: "Exported plugin is not a direct plugin",
327 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
328 results: []Result{{library: "exports", processors: "-proc:none"}},
329 },
330 {
331 name: "Exports plugin to dependee",
332 extra: `
333 java_library{name: "exports", exported_plugins: ["plugin"]}
334 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
335 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
336 `,
337 results: []Result{
338 {library: "foo", processors: "-processor com.android.TestPlugin"},
339 {library: "bar", processors: "-processor com.android.TestPlugin"},
340 },
341 },
342 {
343 name: "Exports plugin to android_library",
344 extra: `
345 java_library{name: "exports", exported_plugins: ["plugin"]}
346 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
347 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
348 `,
349 results: []Result{
350 {library: "foo", processors: "-processor com.android.TestPlugin"},
351 {library: "bar", processors: "-processor com.android.TestPlugin"},
352 },
353 },
354 {
355 name: "Exports plugin is not propagated via transitive deps",
356 extra: `
357 java_library{name: "exports", exported_plugins: ["plugin"]}
358 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
359 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
360 `,
361 results: []Result{
362 {library: "foo", processors: "-processor com.android.TestPlugin"},
363 {library: "bar", processors: "-proc:none"},
364 },
365 },
366 {
367 name: "Exports plugin appends to plugins",
368 extra: `
369 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
370 java_library{name: "exports", exported_plugins: ["plugin"]}
371 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
372 `,
373 results: []Result{
374 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
375 },
376 },
377 }
378
379 for _, test := range tests {
380 t.Run(test.name, func(t *testing.T) {
381 ctx, _ := testJava(t, `
382 java_plugin {
383 name: "plugin",
384 processor_class: "com.android.TestPlugin",
385 }
386 `+test.extra)
387
388 for _, want := range test.results {
389 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
390 if javac.Args["processor"] != want.processors {
391 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
392 }
393 }
394 })
395 }
396}
397
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900398func TestSdkVersionByPartition(t *testing.T) {
399 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900400 java_library {
401 name: "foo",
402 srcs: ["a.java"],
403 vendor: true,
404 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900405 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900406
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900407 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900408 java_library {
409 name: "bar",
410 srcs: ["b.java"],
411 }
412 `)
413
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900414 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900415 bp := `
416 java_library {
417 name: "foo",
418 srcs: ["a.java"],
419 product_specific: true,
420 }
421 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800422
423 config := testConfig(nil, bp, nil)
424 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900425 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800426 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 +0900427 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800428 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900429 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900430 }
431}
432
Colin Crossd5934c82017-10-02 13:55:26 -0700433func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700434 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700435 java_library {
436 name: "foo",
437 srcs: ["a.java"],
438 target: {
439 android: {
440 srcs: ["b.java"],
441 },
442 },
443 }
444 `)
445
446 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
447 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
448 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
449 }
450}
451
Colin Cross6b4a32d2017-12-05 13:42:45 -0800452func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700453 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800454 java_library_host {
455 name: "foo",
456 srcs: ["a.java"],
457 }
458
459 java_binary_host {
460 name: "bar",
461 srcs: ["b.java"],
462 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700463 jni_libs: ["libjni"],
464 }
465
466 cc_library_shared {
467 name: "libjni",
468 host_supported: true,
469 device_supported: false,
470 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800471 }
472 `)
473
474 buildOS := android.BuildOs.String()
475
476 bar := ctx.ModuleForTests("bar", buildOS+"_common")
477 barJar := bar.Output("bar.jar").Output.String()
478 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
479 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
480
Colin Cross89226d92020-10-09 19:00:54 -0700481 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
482 libjniSO := libjni.Rule("Cp").Output.String()
483
Colin Cross6b4a32d2017-12-05 13:42:45 -0800484 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700485 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
486 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800487 }
Colin Cross89226d92020-10-09 19:00:54 -0700488
489 // Test that the install binary wrapper depends on the installed JNI libraries
490 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
491 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800492 }
Alex Humesky2070e322020-06-09 20:23:08 -0400493}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800494
Alex Humesky2070e322020-06-09 20:23:08 -0400495func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
496 bp := `
497 java_library {
498 name: "target_library",
499 srcs: ["a.java"],
500 }
501
502 java_binary_host {
503 name: "host_binary",
504 srcs: ["b.java"],
505 }
506 `
507 config := testConfig(nil, bp, nil)
508 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
509
510 ctx, _ := testJavaWithConfig(t, config)
511
Liz Kammer7941b302020-07-28 13:27:34 -0700512 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400513 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
514 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
515 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
516 t.Errorf("target library javac flags %v should contain "+
517 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
518 }
519
520 // check that -g is not overridden for host modules
521 buildOS := android.BuildOs.String()
522 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
523 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
524 if strings.Contains(hostJavaFlags, "-g:source,lines") {
525 t.Errorf("java_binary_host javac flags %v should not have "+
526 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
527 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800528}
529
Colin Cross72bb3632017-07-13 16:23:21 -0700530func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700531 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700532 java_library {
533 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000534 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000535 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700536 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700537 }
538
Colin Cross74d73e22017-08-02 11:05:49 -0700539 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700540 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700541 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700542 }
543
Colin Cross74d73e22017-08-02 11:05:49 -0700544 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700545 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700546 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700547 sdk_version: "current",
548 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700549 }
Colin Cross42be7612019-02-21 18:12:14 -0800550
551 dex_import {
552 name: "qux",
553 jars: ["b.jar"],
554 }
Colin Cross79c7c262019-04-17 11:11:46 -0700555
556 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000557 name: "sdklib",
558 public: {
559 jars: ["c.jar"],
560 },
561 }
562
Paul Duffin91547182019-11-12 19:39:36 +0000563 prebuilt_stubs_sources {
564 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000565 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000566 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000567
568 java_test_import {
569 name: "test",
570 jars: ["a.jar"],
571 test_suites: ["cts"],
572 test_config: "AndroidTest.xml",
573 }
Colin Cross72bb3632017-07-13 16:23:21 -0700574 `)
575
Paul Duffin9b478b02019-12-10 13:41:51 +0000576 fooModule := ctx.ModuleForTests("foo", "android_common")
577 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700578 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700579 barModule := ctx.ModuleForTests("bar", "android_common")
580 barJar := barModule.Rule("combineJar").Output
581 bazModule := ctx.ModuleForTests("baz", "android_common")
582 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700583 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700584
Paul Duffin9b478b02019-12-10 13:41:51 +0000585 fooLibrary := fooModule.Module().(*Library)
586 assertDeepEquals(t, "foo java sources incorrect",
587 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000588
Paul Duffin9b478b02019-12-10 13:41:51 +0000589 assertDeepEquals(t, "foo java source jars incorrect",
590 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
591 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000592
Colin Cross37f6d792018-07-12 12:28:41 -0700593 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
594 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700595 }
596
Liz Kammerd6c31d22020-08-05 15:40:41 -0700597 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
598 if barDexJar != nil {
599 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
600 }
601
Colin Cross79c7c262019-04-17 11:11:46 -0700602 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
603 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
604 }
605
Colin Cross37f6d792018-07-12 12:28:41 -0700606 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
607 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700608 }
Colin Cross42be7612019-02-21 18:12:14 -0800609
Liz Kammerd6c31d22020-08-05 15:40:41 -0700610 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
611 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
612 if bazDexJar != expectedDexJar {
613 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
614 }
615
Colin Cross42be7612019-02-21 18:12:14 -0800616 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700617}
618
Paul Duffin9b478b02019-12-10 13:41:51 +0000619func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
620 if !reflect.DeepEqual(expected, actual) {
621 t.Errorf("%s: expected %q, found %q", message, expected, actual)
622 }
623}
624
Paul Duffin56d44902020-01-31 13:36:25 +0000625func TestJavaSdkLibraryImport(t *testing.T) {
626 ctx, _ := testJava(t, `
627 java_library {
628 name: "foo",
629 srcs: ["a.java"],
630 libs: ["sdklib"],
631 sdk_version: "current",
632 }
633
634 java_library {
635 name: "foo.system",
636 srcs: ["a.java"],
637 libs: ["sdklib"],
638 sdk_version: "system_current",
639 }
640
641 java_library {
642 name: "foo.test",
643 srcs: ["a.java"],
644 libs: ["sdklib"],
645 sdk_version: "test_current",
646 }
647
648 java_sdk_library_import {
649 name: "sdklib",
650 public: {
651 jars: ["a.jar"],
652 },
653 system: {
654 jars: ["b.jar"],
655 },
656 test: {
657 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100658 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000659 },
660 }
661 `)
662
663 for _, scope := range []string{"", ".system", ".test"} {
664 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
665 javac := fooModule.Rule("javac")
666
667 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
668 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
669 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
670 }
671 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100672
Paul Duffincee7e662020-07-09 17:32:57 +0100673 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100674 `prebuilt_sdklib.stubs`,
675 `prebuilt_sdklib.stubs.source.test`,
676 `prebuilt_sdklib.stubs.system`,
677 `prebuilt_sdklib.stubs.test`,
678 })
679}
680
681func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
682 ctx, _ := testJava(t, `
683 java_sdk_library {
684 name: "sdklib",
685 srcs: ["a.java"],
686 sdk_version: "none",
687 system_modules: "none",
688 public: {
689 enabled: true,
690 },
691 }
692
693 java_sdk_library_import {
694 name: "sdklib",
695 public: {
696 jars: ["a.jar"],
697 },
698 }
699 `)
700
Paul Duffincee7e662020-07-09 17:32:57 +0100701 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100702 `dex2oatd`,
703 `prebuilt_sdklib`,
704 `sdklib.impl`,
705 `sdklib.stubs`,
706 `sdklib.stubs.source`,
707 `sdklib.xml`,
708 })
709
Paul Duffincee7e662020-07-09 17:32:57 +0100710 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100711 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100712 `sdklib.impl`,
713 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
714 // dependency is added after prebuilts may have been renamed and so has to use
715 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100716 `sdklib.xml`,
717 })
718}
719
720func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
721 ctx, _ := testJava(t, `
722 java_sdk_library {
723 name: "sdklib",
724 srcs: ["a.java"],
725 sdk_version: "none",
726 system_modules: "none",
727 public: {
728 enabled: true,
729 },
730 }
731
732 java_sdk_library_import {
733 name: "sdklib",
734 prefer: true,
735 public: {
736 jars: ["a.jar"],
737 },
738 }
739 `)
740
Paul Duffincee7e662020-07-09 17:32:57 +0100741 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100742 `dex2oatd`,
743 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100744 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100745 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100746 `sdklib.stubs.source`,
747 `sdklib.xml`,
748 })
749
Paul Duffincee7e662020-07-09 17:32:57 +0100750 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100751 `prebuilt_sdklib.stubs`,
752 `sdklib.impl`,
753 `sdklib.xml`,
754 })
Paul Duffin56d44902020-01-31 13:36:25 +0000755}
756
Colin Cross89536d42017-07-07 14:35:50 -0700757func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700758 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700759 java_defaults {
760 name: "defaults",
761 srcs: ["a.java"],
762 libs: ["bar"],
763 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700764 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700765 }
766
767 java_library {
768 name: "foo",
769 defaults: ["defaults"],
770 }
771
772 java_library {
773 name: "bar",
774 srcs: ["b.java"],
775 }
776
777 java_library {
778 name: "baz",
779 srcs: ["c.java"],
780 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700781
782 android_test {
783 name: "atestOptimize",
784 defaults: ["defaults"],
785 optimize: {enabled: true},
786 }
787
788 android_test {
789 name: "atestNoOptimize",
790 defaults: ["defaults"],
791 }
792
793 android_test {
794 name: "atestDefault",
795 srcs: ["a.java"],
796 }
Colin Cross89536d42017-07-07 14:35:50 -0700797 `)
798
Colin Cross4c428df2017-09-15 17:36:05 -0700799 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700800 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700801
802 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
803 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
804 }
805
Nan Zhanged19fc32017-10-19 13:06:22 -0700806 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
807 if !strings.Contains(javac.Args["classpath"], barTurbine) {
808 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700809 }
810
Colin Cross1ee23172017-10-18 14:44:18 -0700811 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700812 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
813 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700814 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700815
816 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
817 if atestOptimize.Output == nil {
818 t.Errorf("atestOptimize should optimize APK")
819 }
820
821 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
822 if atestNoOptimize.Output == nil {
823 t.Errorf("atestNoOptimize should not optimize APK")
824 }
825
826 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
827 if atestDefault.Output == nil {
828 t.Errorf("atestDefault should optimize APK")
829 }
Colin Cross89536d42017-07-07 14:35:50 -0700830}
831
Colin Cross0f37af02017-09-27 17:42:05 -0700832func TestResources(t *testing.T) {
833 var table = []struct {
834 name string
835 prop string
836 extra string
837 args string
838 }{
839 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700840 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700841 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800842 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700843 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700844 },
845 {
846 // Test that a module with java_resources includes the files
847 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700848 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
849 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700850 },
851 {
852 // Test that a module with a filegroup in java_resources includes the files with the
853 // path prefix
854 name: "resource filegroup",
855 prop: `java_resources: [":foo-res"]`,
856 extra: `
857 filegroup {
858 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800859 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700860 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700861 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700862 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700863 },
864 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700865 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
866 name: "wildcard dirs",
867 prop: `java_resource_dirs: ["java-res/*"]`,
868 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
869 },
870 {
871 // Test that a module exclude_java_resource_dirs excludes the files
872 name: "wildcard dirs",
873 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
874 args: "-C java-res/a -f java-res/a/a",
875 },
Colin Crosscedd4762018-09-13 11:26:19 -0700876 {
877 // Test wildcards in java_resources
878 name: "wildcard files",
879 prop: `java_resources: ["java-res/**/*"]`,
880 args: "-C . -f java-res/a/a -f java-res/b/b",
881 },
882 {
883 // Test exclude_java_resources with java_resources
884 name: "wildcard files with exclude",
885 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
886 args: "-C . -f java-res/a/a",
887 },
888 {
889 // Test exclude_java_resources with java_resource_dirs
890 name: "resource dirs with exclude files",
891 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
892 args: "-C java-res -f java-res/a/a",
893 },
894 {
895 // Test exclude_java_resource_dirs with java_resource_dirs
896 name: "resource dirs with exclude files",
897 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
898 args: "-C java-res -f java-res/a/a -f java-res/b/b",
899 },
Colin Cross0f37af02017-09-27 17:42:05 -0700900 }
901
902 for _, test := range table {
903 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700904 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700905 java_library {
906 name: "foo",
907 srcs: [
908 "a.java",
909 "b.java",
910 "c.java",
911 ],
912 `+test.prop+`,
913 }
Colin Cross238c1f32020-06-07 16:58:18 -0700914 `+test.extra,
915 map[string][]byte{
916 "java-res/a/a": nil,
917 "java-res/b/b": nil,
918 "java-res2/a": nil,
919 },
920 )
Colin Cross0f37af02017-09-27 17:42:05 -0700921
Colin Cross331a1212018-08-15 20:40:52 -0700922 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700923 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700924
925 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
926 t.Errorf("foo combined jars %v does not contain %q",
927 foo.Inputs.Strings(), fooRes.Output.String())
928 }
929
Colin Crossaf9c55b2017-10-03 14:50:08 -0700930 if fooRes.Args["jarArgs"] != test.args {
931 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700932 fooRes.Args["jarArgs"], test.args)
933 }
934 })
935 }
936}
937
Colin Cross0c4ce212019-05-03 15:28:19 -0700938func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700939 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700940 java_library {
941 name: "foo",
942 srcs: [
943 "a.java",
944 "b.java",
945 "c.java",
946 ],
947 include_srcs: true,
948 }
949
950 java_library {
951 name: "bar",
952 srcs: [
953 "a.java",
954 "b.java",
955 "c.java",
956 ],
957 java_resource_dirs: ["java-res"],
958 include_srcs: true,
959 }
Colin Cross238c1f32020-06-07 16:58:18 -0700960 `, map[string][]byte{
961 "java-res/a/a": nil,
962 "java-res/b/b": nil,
963 "java-res2/a": nil,
964 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700965
966 // Test a library with include_srcs: true
967 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
968 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
969
970 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
971 t.Errorf("foo combined jars %v does not contain %q", w, g)
972 }
973
974 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
975 t.Errorf("foo source jar args %q is not %q", w, g)
976 }
977
978 // Test a library with include_srcs: true and resources
979 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
980 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
981 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
982 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
983
984 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
985 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
986 }
987
988 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
989 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
990 }
991
992 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
993 t.Errorf("bar combined jars %v does not contain %q", w, g)
994 }
995
996 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
997 t.Errorf("bar source jar args %q is not %q", w, g)
998 }
999
1000 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1001 t.Errorf("bar resource jar args %q is not %q", w, g)
1002 }
1003}
1004
Colin Cross54190b32017-10-09 15:34:10 -07001005func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001006 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001007 java_library {
1008 name: "foo",
1009 srcs: [
1010 "a*.java",
1011 ":gen",
1012 "b*.java",
1013 ],
1014 }
1015
1016 genrule {
1017 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001018 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001019 out: ["gen.java"],
1020 }
Colin Cross238c1f32020-06-07 16:58:18 -07001021 `, map[string][]byte{
1022 "a.java": nil,
1023 "b.java": nil,
1024 })
Colin Cross54190b32017-10-09 15:34:10 -07001025
1026 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1027 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1028
Colin Cross15e86d92017-10-20 15:07:08 -07001029 if filepath.Base(genrule.Output.String()) != "gen.java" {
1030 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001031 }
1032
1033 if len(javac.Inputs) != 3 ||
1034 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001035 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001036 javac.Inputs[2].String() != "b.java" {
1037 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1038 }
1039}
1040
Nan Zhang61eaedb2017-11-02 13:28:15 -07001041func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001042 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001043 java_library {
1044 name: "foo",
1045 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001046 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001047 }
1048
1049 java_library {
1050 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001051 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001052 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001053 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001054 }
1055
1056 java_library {
1057 name: "baz",
1058 srcs: ["c.java"],
1059 libs: ["bar"],
1060 sdk_version: "14",
1061 }
1062 `)
1063
1064 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1065 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1066 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1067 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1068 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1069
1070 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1071 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1072 }
1073
1074 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1075 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1076 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1077 }
1078 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1079 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1080 }
1081 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1082 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1083 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001084 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001085 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001086 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001087 }
1088}
1089
1090func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001091 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001092 java_library {
1093 name: "bar",
1094 srcs: ["a.java","b.java","c.java"],
1095 javac_shard_size: 1
1096 }
1097 `)
1098
1099 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1100 for i := 0; i < 3; i++ {
1101 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1102 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1103 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1104 }
1105 }
1106}
1107
Nan Zhang581fd212018-01-10 16:06:12 -08001108func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001109 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001110 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001111 name: "droiddoc-templates-sdk",
1112 path: ".",
1113 }
Jiyong Park29074592019-07-07 16:27:47 +09001114 filegroup {
1115 name: "bar-doc-aidl-srcs",
1116 srcs: ["bar-doc/IBar.aidl"],
1117 path: "bar-doc",
1118 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001119 droidstubs {
1120 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001121 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001122 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001123 ],
Steve Kim3666c702020-09-01 17:58:01 +00001124 exclude_srcs: [
1125 "bar-doc/b.java"
1126 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001127 api_levels_annotations_dirs: [
1128 "droiddoc-templates-sdk",
1129 ],
1130 api_levels_annotations_enabled: true,
1131 }
1132 droiddoc {
1133 name: "bar-doc",
1134 srcs: [
1135 ":bar-stubs",
1136 "bar-doc/IFoo.aidl",
1137 ":bar-doc-aidl-srcs",
1138 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001139 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001140 hdf: [
1141 "android.whichdoc offline",
1142 ],
1143 knowntags: [
1144 "bar-doc/known_oj_tags.txt",
1145 ],
1146 proofread_file: "libcore-proofread.txt",
1147 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001148 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001149 }
Colin Cross238c1f32020-06-07 16:58:18 -07001150 `,
1151 map[string][]byte{
1152 "bar-doc/a.java": nil,
1153 "bar-doc/b.java": nil,
1154 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001155 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1156 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1157 if err != nil {
1158 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1159 }
1160 if len(barStubsOutputs) != 1 {
1161 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001162 }
Nan Zhang581fd212018-01-10 16:06:12 -08001163
Liz Kammere1ab2502020-09-10 15:29:25 +00001164 barStubsOutput := barStubsOutputs[0]
1165 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1166 javaDoc := barDoc.Rule("javadoc")
1167 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1168 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001169 }
1170
Liz Kammere1ab2502020-09-10 15:29:25 +00001171 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1172 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1173 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1174 }
1175
1176 aidl := barDoc.Rule("aidl")
1177 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001178 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1179 }
1180
1181 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1182 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001183 }
Nan Zhang581fd212018-01-10 16:06:12 -08001184}
1185
Liz Kammer585cac22020-07-06 09:12:57 -07001186func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1187 testJavaError(t, "flags is set. Cannot set args", `
1188 droiddoc_exported_dir {
1189 name: "droiddoc-templates-sdk",
1190 path: ".",
1191 }
1192 filegroup {
1193 name: "bar-doc-aidl-srcs",
1194 srcs: ["bar-doc/IBar.aidl"],
1195 path: "bar-doc",
1196 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001197 droidstubs {
1198 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001199 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001200 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001201 ],
Steve Kim3666c702020-09-01 17:58:01 +00001202 exclude_srcs: [
1203 "bar-doc/b.java"
1204 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001205 api_levels_annotations_dirs: [
1206 "droiddoc-templates-sdk",
1207 ],
1208 api_levels_annotations_enabled: true,
1209 }
1210 droiddoc {
1211 name: "bar-doc",
1212 srcs: [
1213 ":bar-stubs",
1214 "bar-doc/IFoo.aidl",
1215 ":bar-doc-aidl-srcs",
1216 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001217 custom_template: "droiddoc-templates-sdk",
1218 hdf: [
1219 "android.whichdoc offline",
1220 ],
1221 knowntags: [
1222 "bar-doc/known_oj_tags.txt",
1223 ],
1224 proofread_file: "libcore-proofread.txt",
1225 todo_file: "libcore-docs-todo.html",
1226 flags: ["-offlinemode -title \"libcore\""],
1227 args: "-offlinemode -title \"libcore\"",
1228 }
1229 `)
1230}
1231
Liz Kammer3d894b72020-08-04 09:55:13 -07001232func TestDroidstubs(t *testing.T) {
1233 ctx, _ := testJavaWithFS(t, `
1234 droiddoc_exported_dir {
1235 name: "droiddoc-templates-sdk",
1236 path: ".",
1237 }
1238
1239 droidstubs {
1240 name: "bar-stubs",
1241 srcs: [
1242 "bar-doc/a.java",
1243 ],
1244 api_levels_annotations_dirs: [
1245 "droiddoc-templates-sdk",
1246 ],
1247 api_levels_annotations_enabled: true,
1248 }
1249
1250 droidstubs {
1251 name: "bar-stubs-other",
1252 srcs: [
1253 "bar-doc/a.java",
1254 ],
1255 api_levels_annotations_dirs: [
1256 "droiddoc-templates-sdk",
1257 ],
1258 api_levels_annotations_enabled: true,
1259 api_levels_jar_filename: "android.other.jar",
1260 }
1261 `,
1262 map[string][]byte{
1263 "bar-doc/a.java": nil,
1264 })
1265 testcases := []struct {
1266 moduleName string
1267 expectedJarFilename string
1268 }{
1269 {
1270 moduleName: "bar-stubs",
1271 expectedJarFilename: "android.jar",
1272 },
1273 {
1274 moduleName: "bar-stubs-other",
1275 expectedJarFilename: "android.other.jar",
1276 },
1277 }
1278 for _, c := range testcases {
1279 m := ctx.ModuleForTests(c.moduleName, "android_common")
1280 metalava := m.Rule("metalava")
1281 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
1282 if actual := metalava.RuleParams.Command; !strings.Contains(actual, expected) {
1283 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1284 }
1285 }
1286}
1287
Paul Duffin83a2d962019-11-19 19:44:10 +00001288func TestDroidstubsWithSystemModules(t *testing.T) {
1289 ctx, _ := testJava(t, `
1290 droidstubs {
1291 name: "stubs-source-system-modules",
1292 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001293 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001294 ],
1295 sdk_version: "none",
1296 system_modules: "source-system-modules",
1297 }
1298
1299 java_library {
1300 name: "source-jar",
1301 srcs: [
1302 "a.java",
1303 ],
1304 }
1305
1306 java_system_modules {
1307 name: "source-system-modules",
1308 libs: ["source-jar"],
1309 }
1310
1311 droidstubs {
1312 name: "stubs-prebuilt-system-modules",
1313 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001314 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001315 ],
1316 sdk_version: "none",
1317 system_modules: "prebuilt-system-modules",
1318 }
1319
1320 java_import {
1321 name: "prebuilt-jar",
1322 jars: ["a.jar"],
1323 }
1324
1325 java_system_modules_import {
1326 name: "prebuilt-system-modules",
1327 libs: ["prebuilt-jar"],
1328 }
1329 `)
1330
1331 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1332
1333 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1334}
1335
1336func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1337 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1338 var systemJars []string
1339 for _, i := range metalavaRule.Implicits {
1340 systemJars = append(systemJars, i.Base())
1341 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001342 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001343 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1344 }
1345}
1346
Colin Cross54250902017-12-05 09:28:08 -08001347func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001348 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001349 java_library {
1350 name: "foo",
1351 srcs: ["a.java"],
1352 }
1353
1354 java_genrule {
1355 name: "jargen",
1356 tool_files: ["b.java"],
1357 cmd: "$(location b.java) $(in) $(out)",
1358 out: ["jargen.jar"],
1359 srcs: [":foo"],
1360 }
1361
1362 java_library {
1363 name: "bar",
1364 static_libs: ["jargen"],
1365 srcs: ["c.java"],
1366 }
1367
1368 java_library {
1369 name: "baz",
1370 libs: ["jargen"],
1371 srcs: ["c.java"],
1372 }
1373 `)
1374
1375 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1376 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1377 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1378 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1379 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1380
1381 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1382 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1383 }
1384
1385 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1386 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1387 }
1388
1389 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1390 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1391 }
1392
1393 if len(barCombined.Inputs) != 2 ||
1394 barCombined.Inputs[0].String() != bar.Output.String() ||
1395 barCombined.Inputs[1].String() != jargen.Output.String() {
1396 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1397 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1398 }
1399}
1400
Nan Zhang27e284d2018-02-09 21:03:53 +00001401func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001402 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001403 java_library {
1404 name: "foo",
1405 srcs: ["a.java", ":foo-srcs"],
1406 exclude_srcs: ["a.java", ":foo-excludes"],
1407 }
1408
1409 filegroup {
1410 name: "foo-srcs",
1411 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1412 }
1413
1414 filegroup {
1415 name: "foo-excludes",
1416 srcs: ["java-fg/a.java", "java-fg/b.java"],
1417 }
1418 `)
1419
1420 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1421
1422 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1423 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1424 }
1425}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001426
Paul Duffin52d398a2019-06-11 12:31:14 +01001427func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001428 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001429 "libcore/Android.bp": []byte(`
1430 java_library {
1431 name: "core",
1432 sdk_version: "none",
1433 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001434 }
1435
1436 filegroup {
1437 name: "core-jar",
1438 srcs: [":core{.jar}"],
1439 }
1440`),
1441 })
1442 ctx := testContext()
1443 run(t, ctx, config)
1444}
1445
1446func TestJavaImport(t *testing.T) {
1447 config := testConfig(nil, "", map[string][]byte{
1448 "libcore/Android.bp": []byte(`
1449 java_import {
1450 name: "core",
1451 sdk_version: "none",
1452 }
1453
1454 filegroup {
1455 name: "core-jar",
1456 srcs: [":core{.jar}"],
1457 }
1458`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001459 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001460 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001461 run(t, ctx, config)
1462}
1463
Jiyong Parkc678ad32018-04-10 13:07:10 +09001464func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001465 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001466 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001467 name: "droiddoc-templates-sdk",
1468 path: ".",
1469 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001470 java_sdk_library {
1471 name: "foo",
1472 srcs: ["a.java", "b.java"],
1473 api_packages: ["foo"],
1474 }
1475 java_sdk_library {
1476 name: "bar",
1477 srcs: ["a.java", "b.java"],
1478 api_packages: ["bar"],
1479 }
1480 java_library {
1481 name: "baz",
1482 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001483 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001484 sdk_version: "system_current",
1485 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001486 java_sdk_library {
1487 name: "barney",
1488 srcs: ["c.java"],
1489 api_only: true,
1490 }
1491 java_sdk_library {
1492 name: "betty",
1493 srcs: ["c.java"],
1494 shared_library: false,
1495 }
Paul Duffin859fe962020-05-15 10:20:31 +01001496 java_sdk_library_import {
1497 name: "quuz",
1498 public: {
1499 jars: ["c.jar"],
1500 },
1501 }
1502 java_sdk_library_import {
1503 name: "fred",
1504 public: {
1505 jars: ["b.jar"],
1506 },
1507 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001508 java_sdk_library_import {
1509 name: "wilma",
1510 public: {
1511 jars: ["b.jar"],
1512 },
1513 shared_library: false,
1514 }
Jiyong Park1be96912018-05-28 18:02:19 +09001515 java_library {
1516 name: "qux",
1517 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001518 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001519 sdk_version: "system_current",
1520 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001521 java_library {
1522 name: "baz-test",
1523 srcs: ["c.java"],
1524 libs: ["foo"],
1525 sdk_version: "test_current",
1526 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001527 java_library {
1528 name: "baz-29",
1529 srcs: ["c.java"],
1530 libs: ["foo"],
1531 sdk_version: "system_29",
1532 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001533 java_library {
1534 name: "baz-module-30",
1535 srcs: ["c.java"],
1536 libs: ["foo"],
1537 sdk_version: "module_30",
1538 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001539 `)
1540
1541 // check the existence of the internal modules
1542 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001543 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1544 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1545 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1546 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1547 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1548 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001549 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001550 ctx.ModuleForTests("foo.api.public.28", "")
1551 ctx.ModuleForTests("foo.api.system.28", "")
1552 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001553
1554 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1555 // tests if baz is actually linked to the stubs lib
1556 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1557 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1558 "foo.stubs.system.jar")
1559 }
1560 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001561 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001562 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001563 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001564 }
1565 // test if baz is not linked to the system variant of foo
1566 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1567 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1568 "foo.stubs.jar")
1569 }
Jiyong Park1be96912018-05-28 18:02:19 +09001570
Paul Duffin726d23c2020-01-22 16:30:37 +00001571 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1572 // tests if baz-test is actually linked to the test stubs lib
1573 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1574 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1575 "foo.stubs.test.jar")
1576 }
1577
Paul Duffina2db18f2020-01-22 17:11:15 +00001578 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1579 // tests if baz-29 is actually linked to the system 29 stubs lib
1580 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1581 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1582 "prebuilts/sdk/29/system/foo.jar")
1583 }
1584
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001585 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1586 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1587 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1588 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1589 "prebuilts/sdk/30/module-lib/foo.jar")
1590 }
1591
Jiyong Park1be96912018-05-28 18:02:19 +09001592 // test if baz has exported SDK lib names foo and bar to qux
1593 qux := ctx.ModuleForTests("qux", "android_common")
1594 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb8063c62020-08-20 11:33:12 +01001595 sdkLibs := android.SortedStringKeys(quxLib.ExportedSdkLibs())
Paul Duffin859fe962020-05-15 10:20:31 +01001596 if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
1597 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001598 }
1599 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001600}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001601
Anton Hansson7f66efa2020-10-08 14:47:23 +01001602func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
1603 ctx, _ := testJava(t, `
1604 java_sdk_library {
1605 name: "sdk_lib",
1606 srcs: ["a.java"],
1607 impl_only_libs: ["foo"],
1608 stub_only_libs: ["bar"],
1609 }
1610 java_library {
1611 name: "foo",
1612 srcs: ["a.java"],
1613 sdk_version: "current",
1614 }
1615 java_library {
1616 name: "bar",
1617 srcs: ["a.java"],
1618 sdk_version: "current",
1619 }
1620 `)
1621
1622 for _, implName := range []string{"sdk_lib", "sdk_lib.impl"} {
1623 implJavacCp := ctx.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
1624 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1625 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1626 }
1627 }
1628 stubName := apiScopePublic.stubsLibraryModuleName("sdk_lib")
1629 stubsJavacCp := ctx.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
1630 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1631 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1632 }
1633}
1634
Paul Duffindaaa3322020-05-26 18:13:57 +01001635func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1636 ctx, _ := testJava(t, `
1637 java_sdk_library {
1638 name: "foo",
1639 srcs: ["a.java"],
1640 api_only: true,
1641 public: {
1642 enabled: true,
1643 },
1644 }
1645
1646 java_library {
1647 name: "bar",
1648 srcs: ["b.java"],
1649 libs: ["foo"],
1650 }
1651 `)
1652
1653 // The bar library should depend on the stubs jar.
1654 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1655 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1656 t.Errorf("expected %q, found %#q", expected, actual)
1657 }
1658}
1659
Paul Duffin46dc45a2020-05-14 15:39:10 +01001660func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1661 testJava(t, `
1662 java_sdk_library {
1663 name: "foo",
1664 srcs: ["a.java"],
1665 api_packages: ["foo"],
1666 public: {
1667 enabled: true,
1668 },
1669 }
1670
1671 java_library {
1672 name: "bar",
1673 srcs: ["b.java", ":foo{.public.stubs.source}"],
1674 }
1675 `)
1676}
1677
1678func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1679 testJavaError(t, `"foo" does not provide api scope system`, `
1680 java_sdk_library {
1681 name: "foo",
1682 srcs: ["a.java"],
1683 api_packages: ["foo"],
1684 public: {
1685 enabled: true,
1686 },
1687 }
1688
1689 java_library {
1690 name: "bar",
1691 srcs: ["b.java", ":foo{.system.stubs.source}"],
1692 }
1693 `)
1694}
1695
Paul Duffinca8d9a52020-06-26 22:20:25 +01001696func TestJavaSdkLibrary_Deps(t *testing.T) {
1697 ctx, _ := testJava(t, `
1698 java_sdk_library {
1699 name: "sdklib",
1700 srcs: ["a.java"],
1701 sdk_version: "none",
1702 system_modules: "none",
1703 public: {
1704 enabled: true,
1705 },
1706 }
1707 `)
1708
Paul Duffincee7e662020-07-09 17:32:57 +01001709 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001710 `dex2oatd`,
1711 `sdklib.impl`,
1712 `sdklib.stubs`,
1713 `sdklib.stubs.source`,
1714 `sdklib.xml`,
1715 })
1716}
1717
Paul Duffin46dc45a2020-05-14 15:39:10 +01001718func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1719 testJava(t, `
1720 java_sdk_library_import {
1721 name: "foo",
1722 public: {
1723 jars: ["a.jar"],
1724 stub_srcs: ["a.java"],
1725 current_api: "api/current.txt",
1726 removed_api: "api/removed.txt",
1727 },
1728 }
1729
1730 java_library {
1731 name: "bar",
1732 srcs: [":foo{.public.stubs.source}"],
1733 java_resources: [
1734 ":foo{.public.api.txt}",
1735 ":foo{.public.removed-api.txt}",
1736 ],
1737 }
1738 `)
1739}
1740
1741func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1742 bp := `
1743 java_sdk_library_import {
1744 name: "foo",
1745 public: {
1746 jars: ["a.jar"],
1747 },
1748 }
1749 `
1750
1751 t.Run("stubs.source", func(t *testing.T) {
1752 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1753 java_library {
1754 name: "bar",
1755 srcs: [":foo{.public.stubs.source}"],
1756 java_resources: [
1757 ":foo{.public.api.txt}",
1758 ":foo{.public.removed-api.txt}",
1759 ],
1760 }
1761 `)
1762 })
1763
1764 t.Run("api.txt", func(t *testing.T) {
1765 testJavaError(t, `api.txt not available for api scope public`, bp+`
1766 java_library {
1767 name: "bar",
1768 srcs: ["a.java"],
1769 java_resources: [
1770 ":foo{.public.api.txt}",
1771 ],
1772 }
1773 `)
1774 })
1775
1776 t.Run("removed-api.txt", func(t *testing.T) {
1777 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1778 java_library {
1779 name: "bar",
1780 srcs: ["a.java"],
1781 java_resources: [
1782 ":foo{.public.removed-api.txt}",
1783 ],
1784 }
1785 `)
1786 })
1787}
1788
Paul Duffin3375e352020-04-28 10:44:03 +01001789func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1790 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1791 java_sdk_library {
1792 name: "foo",
1793 srcs: ["a.java", "b.java"],
1794 api_packages: ["foo"],
1795 // Explicitly disable public to test the check that ensures the set of enabled
1796 // scopes is consistent.
1797 public: {
1798 enabled: false,
1799 },
1800 system: {
1801 enabled: true,
1802 },
1803 }
1804 `)
1805}
1806
Paul Duffin87a05a32020-05-12 11:50:28 +01001807func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1808 testJava(t, `
1809 java_sdk_library {
1810 name: "foo",
1811 srcs: ["a.java", "b.java"],
1812 api_packages: ["foo"],
1813 system: {
1814 enabled: true,
1815 sdk_version: "module_current",
1816 },
1817 }
1818 `)
1819}
1820
Paul Duffin0c5bae52020-06-02 13:00:08 +01001821func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
1822 testJava(t, `
1823 java_sdk_library {
1824 name: "foo",
1825 srcs: ["a.java", "b.java"],
1826 api_packages: ["foo"],
1827 system: {
1828 enabled: true,
1829 },
1830 module_lib: {
1831 enabled: true,
1832 },
1833 }
1834 `)
1835}
1836
1837func TestJavaSdkLibrary_SystemServer(t *testing.T) {
1838 testJava(t, `
1839 java_sdk_library {
1840 name: "foo",
1841 srcs: ["a.java", "b.java"],
1842 api_packages: ["foo"],
1843 system: {
1844 enabled: true,
1845 },
1846 system_server: {
1847 enabled: true,
1848 },
1849 }
1850 `)
1851}
1852
Paul Duffin803a9562020-05-20 11:52:25 +01001853func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1854 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1855 java_sdk_library {
1856 name: "foo",
1857 srcs: ["a.java"],
1858 public: {
1859 enabled: false,
1860 },
1861 }
1862
1863 java_library {
1864 name: "baz",
1865 srcs: ["a.java"],
1866 libs: ["foo"],
1867 sdk_version: "module_current",
1868 }
1869 `)
1870}
1871
1872func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1873 testJava(t, `
1874 java_sdk_library {
1875 name: "foo",
1876 srcs: ["a.java"],
1877 system: {
1878 enabled: true,
1879 },
1880 }
1881
1882 java_library {
1883 name: "baz",
1884 srcs: ["a.java"],
1885 libs: ["foo"],
1886 // foo does not have module-lib scope so it should fallback to system
1887 sdk_version: "module_current",
1888 }
1889 `)
1890}
1891
Jiyong Park932cdfe2020-05-28 00:19:53 +09001892func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1893 ctx, _ := testJava(t, `
1894 java_sdk_library {
1895 name: "foo",
1896 srcs: ["a.java"],
1897 system: {
1898 enabled: true,
1899 },
1900 default_to_stubs: true,
1901 }
1902
1903 java_library {
1904 name: "baz",
1905 srcs: ["a.java"],
1906 libs: ["foo"],
1907 // does not have sdk_version set, should fallback to module,
1908 // which will then fallback to system because the module scope
1909 // is not enabled.
1910 }
1911 `)
1912 // The baz library should depend on the system stubs jar.
1913 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1914 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1915 t.Errorf("expected %q, found %#q", expected, actual)
1916 }
1917}
1918
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001919var compilerFlagsTestCases = []struct {
1920 in string
1921 out bool
1922}{
1923 {
1924 in: "a",
1925 out: false,
1926 },
1927 {
1928 in: "-a",
1929 out: true,
1930 },
1931 {
1932 in: "-no-jdk",
1933 out: false,
1934 },
1935 {
1936 in: "-no-stdlib",
1937 out: false,
1938 },
1939 {
1940 in: "-kotlin-home",
1941 out: false,
1942 },
1943 {
1944 in: "-kotlin-home /some/path",
1945 out: false,
1946 },
1947 {
1948 in: "-include-runtime",
1949 out: false,
1950 },
1951 {
1952 in: "-Xintellij-plugin-root",
1953 out: false,
1954 },
1955}
1956
1957type mockContext struct {
1958 android.ModuleContext
1959 result bool
1960}
1961
1962func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1963 // CheckBadCompilerFlags calls this function when the flag should be rejected
1964 ctx.result = false
1965}
1966
1967func TestCompilerFlags(t *testing.T) {
1968 for _, testCase := range compilerFlagsTestCases {
1969 ctx := &mockContext{result: true}
1970 CheckKotlincFlags(ctx, []string{testCase.in})
1971 if ctx.result != testCase.out {
1972 t.Errorf("incorrect output:")
1973 t.Errorf(" input: %#v", testCase.in)
1974 t.Errorf(" expected: %#v", testCase.out)
1975 t.Errorf(" got: %#v", ctx.result)
1976 }
1977 }
1978}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001979
1980// TODO(jungjw): Consider making this more robust by ignoring path order.
1981func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1982 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1983 flags := strings.Split(variables["javacFlags"], " ")
1984 got := ""
1985 for _, flag := range flags {
1986 keyEnd := strings.Index(flag, "=")
1987 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1988 got = flag[keyEnd+1:]
1989 break
1990 }
1991 }
1992 if expected != got {
1993 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1994 }
1995}
1996
1997func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001998 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001999 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002000 bp := `
2001 java_library {
2002 name: "foo",
2003 srcs: ["a.java"],
2004 java_version: "1.8",
2005 }
2006
2007 java_library {
2008 name: "bar",
2009 srcs: ["b.java"],
2010 sdk_version: "none",
2011 system_modules: "none",
2012 patch_module: "java.base",
2013 java_version: "1.8",
2014 }
2015
2016 java_library {
2017 name: "baz",
2018 srcs: ["c.java"],
2019 patch_module: "java.base",
2020 java_version: "1.8",
2021 }
2022 `
2023 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002024
2025 checkPatchModuleFlag(t, ctx, "foo", "")
2026 checkPatchModuleFlag(t, ctx, "bar", "")
2027 checkPatchModuleFlag(t, ctx, "baz", "")
2028 })
2029
Pete Gillin0c2143e2019-05-02 15:32:11 +01002030 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002031 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002032 bp := `
2033 java_library {
2034 name: "foo",
2035 srcs: ["a.java"],
2036 }
2037
2038 java_library {
2039 name: "bar",
2040 srcs: ["b.java"],
2041 sdk_version: "none",
2042 system_modules: "none",
2043 patch_module: "java.base",
2044 }
2045
2046 java_library {
2047 name: "baz",
2048 srcs: ["c.java"],
2049 patch_module: "java.base",
2050 }
2051 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002052 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002053
2054 checkPatchModuleFlag(t, ctx, "foo", "")
2055 expected := "java.base=.:" + buildDir
2056 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09002057 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002058 checkPatchModuleFlag(t, ctx, "baz", expected)
2059 })
2060}
Paul Duffina7b9f422020-01-10 17:12:18 +00002061
2062func TestJavaSystemModules(t *testing.T) {
2063 ctx, _ := testJava(t, `
2064 java_system_modules {
2065 name: "system-modules",
2066 libs: ["system-module1", "system-module2"],
2067 }
2068 java_library {
2069 name: "system-module1",
2070 srcs: ["a.java"],
2071 sdk_version: "none",
2072 system_modules: "none",
2073 }
2074 java_library {
2075 name: "system-module2",
2076 srcs: ["b.java"],
2077 sdk_version: "none",
2078 system_modules: "none",
2079 }
2080 `)
2081
2082 // check the existence of the module
2083 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2084
2085 cmd := systemModules.Rule("jarsTosystemModules")
2086
2087 // make sure the command compiles against the supplied modules.
2088 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2089 if !strings.Contains(cmd.Args["classpath"], module) {
2090 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2091 module)
2092 }
2093 }
2094}
Paul Duffin90169ba2020-01-10 17:16:44 +00002095
2096func TestJavaSystemModulesImport(t *testing.T) {
2097 ctx, _ := testJava(t, `
2098 java_system_modules_import {
2099 name: "system-modules",
2100 libs: ["system-module1", "system-module2"],
2101 }
2102 java_import {
2103 name: "system-module1",
2104 jars: ["a.jar"],
2105 }
2106 java_import {
2107 name: "system-module2",
2108 jars: ["b.jar"],
2109 }
2110 `)
2111
2112 // check the existence of the module
2113 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2114
2115 cmd := systemModules.Rule("jarsTosystemModules")
2116
2117 // make sure the command compiles against the supplied modules.
2118 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2119 if !strings.Contains(cmd.Args["classpath"], module) {
2120 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2121 module)
2122 }
2123 }
2124}
Paul Duffin83a2d962019-11-19 19:44:10 +00002125
2126func TestJavaLibraryWithSystemModules(t *testing.T) {
2127 ctx, _ := testJava(t, `
2128 java_library {
2129 name: "lib-with-source-system-modules",
2130 srcs: [
2131 "a.java",
2132 ],
2133 sdk_version: "none",
2134 system_modules: "source-system-modules",
2135 }
2136
2137 java_library {
2138 name: "source-jar",
2139 srcs: [
2140 "a.java",
2141 ],
2142 }
2143
2144 java_system_modules {
2145 name: "source-system-modules",
2146 libs: ["source-jar"],
2147 }
2148
2149 java_library {
2150 name: "lib-with-prebuilt-system-modules",
2151 srcs: [
2152 "a.java",
2153 ],
2154 sdk_version: "none",
2155 system_modules: "prebuilt-system-modules",
2156 }
2157
2158 java_import {
2159 name: "prebuilt-jar",
2160 jars: ["a.jar"],
2161 }
2162
2163 java_system_modules_import {
2164 name: "prebuilt-system-modules",
2165 libs: ["prebuilt-jar"],
2166 }
2167 `)
2168
2169 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2170
2171 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2172}
2173
2174func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2175 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2176 bootClasspath := javacRule.Args["bootClasspath"]
2177 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2178 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2179 }
2180}
Jiyong Park19604de2020-03-24 16:44:11 +09002181
2182func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2183 ctx, _ := testJava(t, `
2184 java_library {
2185 name: "foo",
2186 srcs: ["aidl/foo/IFoo.aidl"],
2187 libs: ["bar"],
2188 }
2189
2190 java_import {
2191 name: "bar",
2192 jars: ["a.jar"],
2193 aidl: {
2194 export_include_dirs: ["aidl/bar"],
2195 },
2196 }
2197 `)
2198
2199 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2200 expectedAidlFlag := "-Iaidl/bar"
2201 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2202 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2203 }
2204}
Liz Kammerdd849a82020-06-12 16:38:45 -07002205
2206func TestDataNativeBinaries(t *testing.T) {
2207 ctx, config := testJava(t, `
2208 java_test_host {
2209 name: "foo",
2210 srcs: ["a.java"],
2211 data_native_bins: ["bin"]
2212 }
2213
2214 python_binary_host {
2215 name: "bin",
2216 srcs: ["bin.py"],
2217 }
2218 `)
2219
2220 buildOS := android.BuildOs.String()
2221
2222 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2223 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
2224 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2225 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2226 if !reflect.DeepEqual(expected, actual) {
2227 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2228 }
2229}