blob: e03e57b78b43f08fe143f7457ca3ab036b7a8e1d [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Colin Cross72bb3632017-07-13 16:23:21 -070018 "io/ioutil"
19 "os"
20 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070021 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010022 "regexp"
Paul Duffin859fe962020-05-15 10:20:31 +010023 "sort"
Nan Zhang61eaedb2017-11-02 13:28:15 -070024 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070027
Jeongik Cha28df2572019-11-11 10:46:36 +090028 "github.com/google/blueprint/proptools"
29
Colin Crossa4f08812018-10-02 22:03:40 -070030 "android/soong/android"
31 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080032 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070033 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070034)
35
36var buildDir string
37
38func setUp() {
39 var err error
40 buildDir, err = ioutil.TempDir("", "soong_java_test")
41 if err != nil {
42 panic(err)
43 }
44}
45
46func tearDown() {
47 os.RemoveAll(buildDir)
48}
49
50func TestMain(m *testing.M) {
51 run := func() int {
52 setUp()
53 defer tearDown()
54
55 return m.Run()
56 }
57
58 os.Exit(run())
59}
Colin Cross527012a2017-11-30 22:56:16 -080060
Colin Cross98be1bb2019-12-13 20:41:13 -080061func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000062 bp += dexpreopt.BpToolModulesForTest()
63
Martin Stjernholm75a48d82020-01-10 20:32:59 +000064 config := TestConfig(buildDir, env, bp, fs)
65
66 // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that
67 // it doesn't create a real one, which would fail.
68 _ = dexpreopt.GlobalSoongConfigForTests(config)
69
70 return config
Colin Cross1369cdb2017-09-29 17:58:17 -070071}
72
Colin Cross98be1bb2019-12-13 20:41:13 -080073func testContext() *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070074
Colin Cross4c428df2017-09-15 17:36:05 -070075 ctx := android.NewTestArchContext()
Paul Duffinf9b1da02019-12-18 19:51:55 +000076 RegisterJavaBuildComponents(ctx)
77 RegisterAppBuildComponents(ctx)
78 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000079 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000080 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080081 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080082 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
83 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000084 RegisterDocsBuildComponents(ctx)
85 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000086 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070087 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffina48f7582019-12-19 11:25:19 +000088
89 RegisterPrebuiltApisBuildComponents(ctx)
90
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070091 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080092 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070093 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070094
95 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +000096 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -070097
Martin Stjernholmd90676f2020-01-11 00:37:30 +000098 dexpreopt.RegisterToolModulesForTest(ctx)
99
Colin Cross527012a2017-11-30 22:56:16 -0800100 return ctx
101}
102
103func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800104 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800105
Colin Cross98be1bb2019-12-13 20:41:13 -0800106 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000107 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800108
Colin Cross98be1bb2019-12-13 20:41:13 -0800109 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100110 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800111 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700112 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800113 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800114}
115
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900116func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900117 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800118 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900119}
120
Colin Cross98be1bb2019-12-13 20:41:13 -0800121func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900122 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800123 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900124
Colin Cross98be1bb2019-12-13 20:41:13 -0800125 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000126 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900127
Colin Cross98be1bb2019-12-13 20:41:13 -0800128 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900129 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
130 if len(errs) > 0 {
131 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900132 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900133 }
134 _, errs = ctx.PrepareBuildActions(config)
135 if len(errs) > 0 {
136 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900137 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900138 }
139
140 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900141
142 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900143}
144
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700145func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800146 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800147 return testJavaWithConfig(t, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900148}
149
Colin Cross98be1bb2019-12-13 20:41:13 -0800150func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900151 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800152 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800153 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700154
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700155 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700156}
157
Colin Cross2acdae82017-09-15 19:44:24 -0700158func moduleToPath(name string) string {
159 switch {
160 case name == `""`:
161 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700162 case strings.HasSuffix(name, ".jar"):
163 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700164 default:
165 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700166 }
167}
168
Jeongik Chae403e9e2019-12-07 00:16:24 +0900169func TestJavaLinkType(t *testing.T) {
170 testJava(t, `
171 java_library {
172 name: "foo",
173 srcs: ["a.java"],
174 libs: ["bar"],
175 static_libs: ["baz"],
176 }
177
178 java_library {
179 name: "bar",
180 sdk_version: "current",
181 srcs: ["b.java"],
182 }
183
184 java_library {
185 name: "baz",
186 sdk_version: "system_current",
187 srcs: ["c.java"],
188 }
189 `)
190
191 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.", `
192 java_library {
193 name: "foo",
194 srcs: ["a.java"],
195 libs: ["bar"],
196 sdk_version: "current",
197 static_libs: ["baz"],
198 }
199
200 java_library {
201 name: "bar",
202 sdk_version: "current",
203 srcs: ["b.java"],
204 }
205
206 java_library {
207 name: "baz",
208 sdk_version: "system_current",
209 srcs: ["c.java"],
210 }
211 `)
212
213 testJava(t, `
214 java_library {
215 name: "foo",
216 srcs: ["a.java"],
217 libs: ["bar"],
218 sdk_version: "system_current",
219 static_libs: ["baz"],
220 }
221
222 java_library {
223 name: "bar",
224 sdk_version: "current",
225 srcs: ["b.java"],
226 }
227
228 java_library {
229 name: "baz",
230 sdk_version: "system_current",
231 srcs: ["c.java"],
232 }
233 `)
234
235 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.", `
236 java_library {
237 name: "foo",
238 srcs: ["a.java"],
239 libs: ["bar"],
240 sdk_version: "system_current",
241 static_libs: ["baz"],
242 }
243
244 java_library {
245 name: "bar",
246 sdk_version: "current",
247 srcs: ["b.java"],
248 }
249
250 java_library {
251 name: "baz",
252 srcs: ["c.java"],
253 }
254 `)
255}
256
Colin Cross72bb3632017-07-13 16:23:21 -0700257func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700258 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700259 java_library {
260 name: "foo",
261 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700262 libs: ["bar"],
263 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700264 }
265
266 java_library {
267 name: "bar",
268 srcs: ["b.java"],
269 }
270
271 java_library {
272 name: "baz",
273 srcs: ["c.java"],
274 }
Colin Crossd5934c82017-10-02 13:55:26 -0700275 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700276
Colin Cross4c428df2017-09-15 17:36:05 -0700277 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700278 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700279
280 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
281 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
282 }
283
Colin Cross1ee23172017-10-18 14:44:18 -0700284 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700285 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
286 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700287
Nan Zhanged19fc32017-10-19 13:06:22 -0700288 if !strings.Contains(javac.Args["classpath"], barTurbine) {
289 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700290 }
291
Nan Zhanged19fc32017-10-19 13:06:22 -0700292 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
293 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700294 }
295
296 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
297 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700298 }
299}
300
Artur Satayev9cf46692019-11-26 18:08:34 +0000301func TestExportedPlugins(t *testing.T) {
302 type Result struct {
303 library string
304 processors string
305 }
306 var tests = []struct {
307 name string
308 extra string
309 results []Result
310 }{
311 {
312 name: "Exported plugin is not a direct plugin",
313 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
314 results: []Result{{library: "exports", processors: "-proc:none"}},
315 },
316 {
317 name: "Exports plugin to dependee",
318 extra: `
319 java_library{name: "exports", exported_plugins: ["plugin"]}
320 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
321 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
322 `,
323 results: []Result{
324 {library: "foo", processors: "-processor com.android.TestPlugin"},
325 {library: "bar", processors: "-processor com.android.TestPlugin"},
326 },
327 },
328 {
329 name: "Exports plugin to android_library",
330 extra: `
331 java_library{name: "exports", exported_plugins: ["plugin"]}
332 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
333 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
334 `,
335 results: []Result{
336 {library: "foo", processors: "-processor com.android.TestPlugin"},
337 {library: "bar", processors: "-processor com.android.TestPlugin"},
338 },
339 },
340 {
341 name: "Exports plugin is not propagated via transitive deps",
342 extra: `
343 java_library{name: "exports", exported_plugins: ["plugin"]}
344 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
345 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
346 `,
347 results: []Result{
348 {library: "foo", processors: "-processor com.android.TestPlugin"},
349 {library: "bar", processors: "-proc:none"},
350 },
351 },
352 {
353 name: "Exports plugin appends to plugins",
354 extra: `
355 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
356 java_library{name: "exports", exported_plugins: ["plugin"]}
357 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
358 `,
359 results: []Result{
360 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
361 },
362 },
363 }
364
365 for _, test := range tests {
366 t.Run(test.name, func(t *testing.T) {
367 ctx, _ := testJava(t, `
368 java_plugin {
369 name: "plugin",
370 processor_class: "com.android.TestPlugin",
371 }
372 `+test.extra)
373
374 for _, want := range test.results {
375 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
376 if javac.Args["processor"] != want.processors {
377 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
378 }
379 }
380 })
381 }
382}
383
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900384func TestSdkVersionByPartition(t *testing.T) {
385 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900386 java_library {
387 name: "foo",
388 srcs: ["a.java"],
389 vendor: true,
390 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900391 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900392
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900393 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900394 java_library {
395 name: "bar",
396 srcs: ["b.java"],
397 }
398 `)
399
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900400 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900401 bp := `
402 java_library {
403 name: "foo",
404 srcs: ["a.java"],
405 product_specific: true,
406 }
407 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800408
409 config := testConfig(nil, bp, nil)
410 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900411 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800412 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 +0900413 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800414 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900415 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900416 }
417}
418
Colin Crossd5934c82017-10-02 13:55:26 -0700419func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700420 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700421 java_library {
422 name: "foo",
423 srcs: ["a.java"],
424 target: {
425 android: {
426 srcs: ["b.java"],
427 },
428 },
429 }
430 `)
431
432 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
433 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
434 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
435 }
436}
437
Colin Cross6b4a32d2017-12-05 13:42:45 -0800438func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700439 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800440 java_library_host {
441 name: "foo",
442 srcs: ["a.java"],
443 }
444
445 java_binary_host {
446 name: "bar",
447 srcs: ["b.java"],
448 static_libs: ["foo"],
449 }
450 `)
451
452 buildOS := android.BuildOs.String()
453
454 bar := ctx.ModuleForTests("bar", buildOS+"_common")
455 barJar := bar.Output("bar.jar").Output.String()
456 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
457 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
458
459 // Test that the install binary wrapper depends on the installed jar file
460 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
461 t.Errorf("expected binary wrapper implicits [%q], got %v",
462 barJar, barWrapperDeps)
463 }
464
465}
466
Colin Cross72bb3632017-07-13 16:23:21 -0700467func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700468 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700469 java_library {
470 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000471 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000472 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700473 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700474 }
475
Colin Cross74d73e22017-08-02 11:05:49 -0700476 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700477 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700478 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700479 }
480
Colin Cross74d73e22017-08-02 11:05:49 -0700481 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700482 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700483 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700484 }
Colin Cross42be7612019-02-21 18:12:14 -0800485
486 dex_import {
487 name: "qux",
488 jars: ["b.jar"],
489 }
Colin Cross79c7c262019-04-17 11:11:46 -0700490
491 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000492 name: "sdklib",
493 public: {
494 jars: ["c.jar"],
495 },
496 }
497
Paul Duffin91547182019-11-12 19:39:36 +0000498 prebuilt_stubs_sources {
499 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000500 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000501 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000502
503 java_test_import {
504 name: "test",
505 jars: ["a.jar"],
506 test_suites: ["cts"],
507 test_config: "AndroidTest.xml",
508 }
Colin Cross72bb3632017-07-13 16:23:21 -0700509 `)
510
Paul Duffin9b478b02019-12-10 13:41:51 +0000511 fooModule := ctx.ModuleForTests("foo", "android_common")
512 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700513 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700514 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
515 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700516 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700517
Paul Duffin9b478b02019-12-10 13:41:51 +0000518 fooLibrary := fooModule.Module().(*Library)
519 assertDeepEquals(t, "foo java sources incorrect",
520 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000521
Paul Duffin9b478b02019-12-10 13:41:51 +0000522 assertDeepEquals(t, "foo java source jars incorrect",
523 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
524 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000525
Colin Cross37f6d792018-07-12 12:28:41 -0700526 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
527 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700528 }
529
Colin Cross79c7c262019-04-17 11:11:46 -0700530 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
531 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
532 }
533
Colin Cross37f6d792018-07-12 12:28:41 -0700534 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
535 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700536 }
Colin Cross42be7612019-02-21 18:12:14 -0800537
538 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700539}
540
Paul Duffin9b478b02019-12-10 13:41:51 +0000541func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
542 if !reflect.DeepEqual(expected, actual) {
543 t.Errorf("%s: expected %q, found %q", message, expected, actual)
544 }
545}
546
Paul Duffin56d44902020-01-31 13:36:25 +0000547func TestJavaSdkLibraryImport(t *testing.T) {
548 ctx, _ := testJava(t, `
549 java_library {
550 name: "foo",
551 srcs: ["a.java"],
552 libs: ["sdklib"],
553 sdk_version: "current",
554 }
555
556 java_library {
557 name: "foo.system",
558 srcs: ["a.java"],
559 libs: ["sdklib"],
560 sdk_version: "system_current",
561 }
562
563 java_library {
564 name: "foo.test",
565 srcs: ["a.java"],
566 libs: ["sdklib"],
567 sdk_version: "test_current",
568 }
569
570 java_sdk_library_import {
571 name: "sdklib",
572 public: {
573 jars: ["a.jar"],
574 },
575 system: {
576 jars: ["b.jar"],
577 },
578 test: {
579 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100580 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000581 },
582 }
583 `)
584
585 for _, scope := range []string{"", ".system", ".test"} {
586 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
587 javac := fooModule.Rule("javac")
588
589 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
590 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
591 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
592 }
593 }
594}
595
Colin Cross89536d42017-07-07 14:35:50 -0700596func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700597 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700598 java_defaults {
599 name: "defaults",
600 srcs: ["a.java"],
601 libs: ["bar"],
602 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700603 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700604 }
605
606 java_library {
607 name: "foo",
608 defaults: ["defaults"],
609 }
610
611 java_library {
612 name: "bar",
613 srcs: ["b.java"],
614 }
615
616 java_library {
617 name: "baz",
618 srcs: ["c.java"],
619 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700620
621 android_test {
622 name: "atestOptimize",
623 defaults: ["defaults"],
624 optimize: {enabled: true},
625 }
626
627 android_test {
628 name: "atestNoOptimize",
629 defaults: ["defaults"],
630 }
631
632 android_test {
633 name: "atestDefault",
634 srcs: ["a.java"],
635 }
Colin Cross89536d42017-07-07 14:35:50 -0700636 `)
637
Colin Cross4c428df2017-09-15 17:36:05 -0700638 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700639 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700640
641 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
642 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
643 }
644
Nan Zhanged19fc32017-10-19 13:06:22 -0700645 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
646 if !strings.Contains(javac.Args["classpath"], barTurbine) {
647 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700648 }
649
Colin Cross1ee23172017-10-18 14:44:18 -0700650 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700651 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
652 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700653 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700654
655 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
656 if atestOptimize.Output == nil {
657 t.Errorf("atestOptimize should optimize APK")
658 }
659
660 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
661 if atestNoOptimize.Output == nil {
662 t.Errorf("atestNoOptimize should not optimize APK")
663 }
664
665 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
666 if atestDefault.Output == nil {
667 t.Errorf("atestDefault should optimize APK")
668 }
Colin Cross89536d42017-07-07 14:35:50 -0700669}
670
Colin Cross0f37af02017-09-27 17:42:05 -0700671func TestResources(t *testing.T) {
672 var table = []struct {
673 name string
674 prop string
675 extra string
676 args string
677 }{
678 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700679 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700680 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800681 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700682 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700683 },
684 {
685 // Test that a module with java_resources includes the files
686 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700687 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
688 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700689 },
690 {
691 // Test that a module with a filegroup in java_resources includes the files with the
692 // path prefix
693 name: "resource filegroup",
694 prop: `java_resources: [":foo-res"]`,
695 extra: `
696 filegroup {
697 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800698 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700699 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700700 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700701 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700702 },
703 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700704 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
705 name: "wildcard dirs",
706 prop: `java_resource_dirs: ["java-res/*"]`,
707 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
708 },
709 {
710 // Test that a module exclude_java_resource_dirs excludes the files
711 name: "wildcard dirs",
712 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
713 args: "-C java-res/a -f java-res/a/a",
714 },
Colin Crosscedd4762018-09-13 11:26:19 -0700715 {
716 // Test wildcards in java_resources
717 name: "wildcard files",
718 prop: `java_resources: ["java-res/**/*"]`,
719 args: "-C . -f java-res/a/a -f java-res/b/b",
720 },
721 {
722 // Test exclude_java_resources with java_resources
723 name: "wildcard files with exclude",
724 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
725 args: "-C . -f java-res/a/a",
726 },
727 {
728 // Test exclude_java_resources with java_resource_dirs
729 name: "resource dirs with exclude files",
730 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
731 args: "-C java-res -f java-res/a/a",
732 },
733 {
734 // Test exclude_java_resource_dirs with java_resource_dirs
735 name: "resource dirs with exclude files",
736 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
737 args: "-C java-res -f java-res/a/a -f java-res/b/b",
738 },
Colin Cross0f37af02017-09-27 17:42:05 -0700739 }
740
741 for _, test := range table {
742 t.Run(test.name, func(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700743 ctx, _ := testJava(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700744 java_library {
745 name: "foo",
746 srcs: [
747 "a.java",
748 "b.java",
749 "c.java",
750 ],
751 `+test.prop+`,
752 }
753 `+test.extra)
754
Colin Cross331a1212018-08-15 20:40:52 -0700755 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700756 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700757
758 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
759 t.Errorf("foo combined jars %v does not contain %q",
760 foo.Inputs.Strings(), fooRes.Output.String())
761 }
762
Colin Crossaf9c55b2017-10-03 14:50:08 -0700763 if fooRes.Args["jarArgs"] != test.args {
764 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700765 fooRes.Args["jarArgs"], test.args)
766 }
767 })
768 }
769}
770
Colin Cross0c4ce212019-05-03 15:28:19 -0700771func TestIncludeSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700772 ctx, _ := testJava(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700773 java_library {
774 name: "foo",
775 srcs: [
776 "a.java",
777 "b.java",
778 "c.java",
779 ],
780 include_srcs: true,
781 }
782
783 java_library {
784 name: "bar",
785 srcs: [
786 "a.java",
787 "b.java",
788 "c.java",
789 ],
790 java_resource_dirs: ["java-res"],
791 include_srcs: true,
792 }
793 `)
794
795 // Test a library with include_srcs: true
796 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
797 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
798
799 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
800 t.Errorf("foo combined jars %v does not contain %q", w, g)
801 }
802
803 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
804 t.Errorf("foo source jar args %q is not %q", w, g)
805 }
806
807 // Test a library with include_srcs: true and resources
808 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
809 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
810 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
811 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
812
813 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
814 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
815 }
816
817 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
818 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
819 }
820
821 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
822 t.Errorf("bar combined jars %v does not contain %q", w, g)
823 }
824
825 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
826 t.Errorf("bar source jar args %q is not %q", w, g)
827 }
828
829 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
830 t.Errorf("bar resource jar args %q is not %q", w, g)
831 }
832}
833
Colin Cross54190b32017-10-09 15:34:10 -0700834func TestGeneratedSources(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700835 ctx, _ := testJava(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700836 java_library {
837 name: "foo",
838 srcs: [
839 "a*.java",
840 ":gen",
841 "b*.java",
842 ],
843 }
844
845 genrule {
846 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800847 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700848 out: ["gen.java"],
849 }
850 `)
851
852 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
853 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
854
Colin Cross15e86d92017-10-20 15:07:08 -0700855 if filepath.Base(genrule.Output.String()) != "gen.java" {
856 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700857 }
858
859 if len(javac.Inputs) != 3 ||
860 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700861 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700862 javac.Inputs[2].String() != "b.java" {
863 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
864 }
865}
866
Nan Zhang61eaedb2017-11-02 13:28:15 -0700867func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700868 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700869 java_library {
870 name: "foo",
871 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900872 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700873 }
874
875 java_library {
876 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800877 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700878 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900879 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700880 }
881
882 java_library {
883 name: "baz",
884 srcs: ["c.java"],
885 libs: ["bar"],
886 sdk_version: "14",
887 }
888 `)
889
890 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
891 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
892 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
893 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
894 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
895
896 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
897 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
898 }
899
900 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
901 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
902 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
903 }
904 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
905 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
906 }
907 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
908 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
909 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100910 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700911 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100912 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700913 }
914}
915
916func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700917 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700918 java_library {
919 name: "bar",
920 srcs: ["a.java","b.java","c.java"],
921 javac_shard_size: 1
922 }
923 `)
924
925 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
926 for i := 0; i < 3; i++ {
927 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
928 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
929 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
930 }
931 }
932}
933
Nan Zhang581fd212018-01-10 16:06:12 -0800934func TestDroiddoc(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700935 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +0000936 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -0800937 name: "droiddoc-templates-sdk",
938 path: ".",
939 }
Jiyong Park29074592019-07-07 16:27:47 +0900940 filegroup {
941 name: "bar-doc-aidl-srcs",
942 srcs: ["bar-doc/IBar.aidl"],
943 path: "bar-doc",
944 }
Nan Zhang581fd212018-01-10 16:06:12 -0800945 droiddoc {
946 name: "bar-doc",
947 srcs: [
948 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900949 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +0900950 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -0800951 ],
952 exclude_srcs: [
953 "bar-doc/b.java"
954 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800955 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800956 hdf: [
957 "android.whichdoc offline",
958 ],
959 knowntags: [
960 "bar-doc/known_oj_tags.txt",
961 ],
962 proofread_file: "libcore-proofread.txt",
963 todo_file: "libcore-docs-todo.html",
964 args: "-offlinemode -title \"libcore\"",
965 }
966 `)
967
Colin Crossc0806172019-06-14 18:51:47 -0700968 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +0900969 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -0700970 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +0900971 javaSrcs = append(javaSrcs, i.Base())
972 }
Colin Crossc0806172019-06-14 18:51:47 -0700973 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
974 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +0900975 }
976
Colin Crossc0806172019-06-14 18:51:47 -0700977 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
978 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
979 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
980 }
981
982 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
983 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +0900984 }
Nan Zhang581fd212018-01-10 16:06:12 -0800985}
986
Paul Duffin83a2d962019-11-19 19:44:10 +0000987func TestDroidstubsWithSystemModules(t *testing.T) {
988 ctx, _ := testJava(t, `
989 droidstubs {
990 name: "stubs-source-system-modules",
991 srcs: [
992 "bar-doc/*.java",
993 ],
994 sdk_version: "none",
995 system_modules: "source-system-modules",
996 }
997
998 java_library {
999 name: "source-jar",
1000 srcs: [
1001 "a.java",
1002 ],
1003 }
1004
1005 java_system_modules {
1006 name: "source-system-modules",
1007 libs: ["source-jar"],
1008 }
1009
1010 droidstubs {
1011 name: "stubs-prebuilt-system-modules",
1012 srcs: [
1013 "bar-doc/*.java",
1014 ],
1015 sdk_version: "none",
1016 system_modules: "prebuilt-system-modules",
1017 }
1018
1019 java_import {
1020 name: "prebuilt-jar",
1021 jars: ["a.jar"],
1022 }
1023
1024 java_system_modules_import {
1025 name: "prebuilt-system-modules",
1026 libs: ["prebuilt-jar"],
1027 }
1028 `)
1029
1030 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1031
1032 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1033}
1034
1035func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1036 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1037 var systemJars []string
1038 for _, i := range metalavaRule.Implicits {
1039 systemJars = append(systemJars, i.Base())
1040 }
1041 if len(systemJars) != 1 || systemJars[0] != systemJar {
1042 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1043 }
1044}
1045
Colin Cross54250902017-12-05 09:28:08 -08001046func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001047 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001048 java_library {
1049 name: "foo",
1050 srcs: ["a.java"],
1051 }
1052
1053 java_genrule {
1054 name: "jargen",
1055 tool_files: ["b.java"],
1056 cmd: "$(location b.java) $(in) $(out)",
1057 out: ["jargen.jar"],
1058 srcs: [":foo"],
1059 }
1060
1061 java_library {
1062 name: "bar",
1063 static_libs: ["jargen"],
1064 srcs: ["c.java"],
1065 }
1066
1067 java_library {
1068 name: "baz",
1069 libs: ["jargen"],
1070 srcs: ["c.java"],
1071 }
1072 `)
1073
1074 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1075 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1076 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1077 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1078 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1079
1080 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1081 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1082 }
1083
1084 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1085 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1086 }
1087
1088 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1089 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1090 }
1091
1092 if len(barCombined.Inputs) != 2 ||
1093 barCombined.Inputs[0].String() != bar.Output.String() ||
1094 barCombined.Inputs[1].String() != jargen.Output.String() {
1095 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1096 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1097 }
1098}
1099
Nan Zhang27e284d2018-02-09 21:03:53 +00001100func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001101 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001102 java_library {
1103 name: "foo",
1104 srcs: ["a.java", ":foo-srcs"],
1105 exclude_srcs: ["a.java", ":foo-excludes"],
1106 }
1107
1108 filegroup {
1109 name: "foo-srcs",
1110 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1111 }
1112
1113 filegroup {
1114 name: "foo-excludes",
1115 srcs: ["java-fg/a.java", "java-fg/b.java"],
1116 }
1117 `)
1118
1119 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1120
1121 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1122 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1123 }
1124}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001125
Paul Duffin52d398a2019-06-11 12:31:14 +01001126func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001127 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001128 "libcore/Android.bp": []byte(`
1129 java_library {
1130 name: "core",
1131 sdk_version: "none",
1132 system_modules: "none",
1133 }`),
1134 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001135 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001136 run(t, ctx, config)
1137}
1138
Jiyong Parkc678ad32018-04-10 13:07:10 +09001139func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001140 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001141 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001142 name: "droiddoc-templates-sdk",
1143 path: ".",
1144 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001145 java_sdk_library {
1146 name: "foo",
1147 srcs: ["a.java", "b.java"],
1148 api_packages: ["foo"],
1149 }
1150 java_sdk_library {
1151 name: "bar",
1152 srcs: ["a.java", "b.java"],
1153 api_packages: ["bar"],
1154 }
1155 java_library {
1156 name: "baz",
1157 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001158 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001159 sdk_version: "system_current",
1160 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001161 java_sdk_library {
1162 name: "barney",
1163 srcs: ["c.java"],
1164 api_only: true,
1165 }
1166 java_sdk_library {
1167 name: "betty",
1168 srcs: ["c.java"],
1169 shared_library: false,
1170 }
Paul Duffin859fe962020-05-15 10:20:31 +01001171 java_sdk_library_import {
1172 name: "quuz",
1173 public: {
1174 jars: ["c.jar"],
1175 },
1176 }
1177 java_sdk_library_import {
1178 name: "fred",
1179 public: {
1180 jars: ["b.jar"],
1181 },
1182 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001183 java_sdk_library_import {
1184 name: "wilma",
1185 public: {
1186 jars: ["b.jar"],
1187 },
1188 shared_library: false,
1189 }
Jiyong Park1be96912018-05-28 18:02:19 +09001190 java_library {
1191 name: "qux",
1192 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001193 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001194 sdk_version: "system_current",
1195 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001196 java_library {
1197 name: "baz-test",
1198 srcs: ["c.java"],
1199 libs: ["foo"],
1200 sdk_version: "test_current",
1201 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001202 java_library {
1203 name: "baz-29",
1204 srcs: ["c.java"],
1205 libs: ["foo"],
1206 sdk_version: "system_29",
1207 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001208 `)
1209
1210 // check the existence of the internal modules
1211 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001212 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1213 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1214 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1215 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1216 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1217 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001218 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001219 ctx.ModuleForTests("foo.api.public.28", "")
1220 ctx.ModuleForTests("foo.api.system.28", "")
1221 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001222
1223 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1224 // tests if baz is actually linked to the stubs lib
1225 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1226 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1227 "foo.stubs.system.jar")
1228 }
1229 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001230 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001231 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001232 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001233 }
1234 // test if baz is not linked to the system variant of foo
1235 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1236 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1237 "foo.stubs.jar")
1238 }
Jiyong Park1be96912018-05-28 18:02:19 +09001239
Paul Duffin726d23c2020-01-22 16:30:37 +00001240 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1241 // tests if baz-test is actually linked to the test stubs lib
1242 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1243 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1244 "foo.stubs.test.jar")
1245 }
1246
Paul Duffina2db18f2020-01-22 17:11:15 +00001247 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1248 // tests if baz-29 is actually linked to the system 29 stubs lib
1249 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1250 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1251 "prebuilts/sdk/29/system/foo.jar")
1252 }
1253
Jiyong Park1be96912018-05-28 18:02:19 +09001254 // test if baz has exported SDK lib names foo and bar to qux
1255 qux := ctx.ModuleForTests("qux", "android_common")
1256 if quxLib, ok := qux.Module().(*Library); ok {
1257 sdkLibs := quxLib.ExportedSdkLibs()
Paul Duffin859fe962020-05-15 10:20:31 +01001258 sort.Strings(sdkLibs)
1259 if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
1260 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001261 }
1262 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001263}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001264
Paul Duffindaaa3322020-05-26 18:13:57 +01001265func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1266 ctx, _ := testJava(t, `
1267 java_sdk_library {
1268 name: "foo",
1269 srcs: ["a.java"],
1270 api_only: true,
1271 public: {
1272 enabled: true,
1273 },
1274 }
1275
1276 java_library {
1277 name: "bar",
1278 srcs: ["b.java"],
1279 libs: ["foo"],
1280 }
1281 `)
1282
1283 // The bar library should depend on the stubs jar.
1284 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1285 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1286 t.Errorf("expected %q, found %#q", expected, actual)
1287 }
1288}
1289
Paul Duffin46dc45a2020-05-14 15:39:10 +01001290func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1291 testJava(t, `
1292 java_sdk_library {
1293 name: "foo",
1294 srcs: ["a.java"],
1295 api_packages: ["foo"],
1296 public: {
1297 enabled: true,
1298 },
1299 }
1300
1301 java_library {
1302 name: "bar",
1303 srcs: ["b.java", ":foo{.public.stubs.source}"],
1304 }
1305 `)
1306}
1307
1308func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1309 testJavaError(t, `"foo" does not provide api scope system`, `
1310 java_sdk_library {
1311 name: "foo",
1312 srcs: ["a.java"],
1313 api_packages: ["foo"],
1314 public: {
1315 enabled: true,
1316 },
1317 }
1318
1319 java_library {
1320 name: "bar",
1321 srcs: ["b.java", ":foo{.system.stubs.source}"],
1322 }
1323 `)
1324}
1325
1326func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1327 testJava(t, `
1328 java_sdk_library_import {
1329 name: "foo",
1330 public: {
1331 jars: ["a.jar"],
1332 stub_srcs: ["a.java"],
1333 current_api: "api/current.txt",
1334 removed_api: "api/removed.txt",
1335 },
1336 }
1337
1338 java_library {
1339 name: "bar",
1340 srcs: [":foo{.public.stubs.source}"],
1341 java_resources: [
1342 ":foo{.public.api.txt}",
1343 ":foo{.public.removed-api.txt}",
1344 ],
1345 }
1346 `)
1347}
1348
1349func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1350 bp := `
1351 java_sdk_library_import {
1352 name: "foo",
1353 public: {
1354 jars: ["a.jar"],
1355 },
1356 }
1357 `
1358
1359 t.Run("stubs.source", func(t *testing.T) {
1360 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1361 java_library {
1362 name: "bar",
1363 srcs: [":foo{.public.stubs.source}"],
1364 java_resources: [
1365 ":foo{.public.api.txt}",
1366 ":foo{.public.removed-api.txt}",
1367 ],
1368 }
1369 `)
1370 })
1371
1372 t.Run("api.txt", func(t *testing.T) {
1373 testJavaError(t, `api.txt not available for api scope public`, bp+`
1374 java_library {
1375 name: "bar",
1376 srcs: ["a.java"],
1377 java_resources: [
1378 ":foo{.public.api.txt}",
1379 ],
1380 }
1381 `)
1382 })
1383
1384 t.Run("removed-api.txt", func(t *testing.T) {
1385 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1386 java_library {
1387 name: "bar",
1388 srcs: ["a.java"],
1389 java_resources: [
1390 ":foo{.public.removed-api.txt}",
1391 ],
1392 }
1393 `)
1394 })
1395}
1396
Paul Duffin3375e352020-04-28 10:44:03 +01001397func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1398 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1399 java_sdk_library {
1400 name: "foo",
1401 srcs: ["a.java", "b.java"],
1402 api_packages: ["foo"],
1403 // Explicitly disable public to test the check that ensures the set of enabled
1404 // scopes is consistent.
1405 public: {
1406 enabled: false,
1407 },
1408 system: {
1409 enabled: true,
1410 },
1411 }
1412 `)
1413}
1414
Paul Duffin87a05a32020-05-12 11:50:28 +01001415func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1416 testJava(t, `
1417 java_sdk_library {
1418 name: "foo",
1419 srcs: ["a.java", "b.java"],
1420 api_packages: ["foo"],
1421 system: {
1422 enabled: true,
1423 sdk_version: "module_current",
1424 },
1425 }
1426 `)
1427}
1428
Paul Duffin803a9562020-05-20 11:52:25 +01001429func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1430 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1431 java_sdk_library {
1432 name: "foo",
1433 srcs: ["a.java"],
1434 public: {
1435 enabled: false,
1436 },
1437 }
1438
1439 java_library {
1440 name: "baz",
1441 srcs: ["a.java"],
1442 libs: ["foo"],
1443 sdk_version: "module_current",
1444 }
1445 `)
1446}
1447
1448func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1449 testJava(t, `
1450 java_sdk_library {
1451 name: "foo",
1452 srcs: ["a.java"],
1453 system: {
1454 enabled: true,
1455 },
1456 }
1457
1458 java_library {
1459 name: "baz",
1460 srcs: ["a.java"],
1461 libs: ["foo"],
1462 // foo does not have module-lib scope so it should fallback to system
1463 sdk_version: "module_current",
1464 }
1465 `)
1466}
1467
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001468var compilerFlagsTestCases = []struct {
1469 in string
1470 out bool
1471}{
1472 {
1473 in: "a",
1474 out: false,
1475 },
1476 {
1477 in: "-a",
1478 out: true,
1479 },
1480 {
1481 in: "-no-jdk",
1482 out: false,
1483 },
1484 {
1485 in: "-no-stdlib",
1486 out: false,
1487 },
1488 {
1489 in: "-kotlin-home",
1490 out: false,
1491 },
1492 {
1493 in: "-kotlin-home /some/path",
1494 out: false,
1495 },
1496 {
1497 in: "-include-runtime",
1498 out: false,
1499 },
1500 {
1501 in: "-Xintellij-plugin-root",
1502 out: false,
1503 },
1504}
1505
1506type mockContext struct {
1507 android.ModuleContext
1508 result bool
1509}
1510
1511func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1512 // CheckBadCompilerFlags calls this function when the flag should be rejected
1513 ctx.result = false
1514}
1515
1516func TestCompilerFlags(t *testing.T) {
1517 for _, testCase := range compilerFlagsTestCases {
1518 ctx := &mockContext{result: true}
1519 CheckKotlincFlags(ctx, []string{testCase.in})
1520 if ctx.result != testCase.out {
1521 t.Errorf("incorrect output:")
1522 t.Errorf(" input: %#v", testCase.in)
1523 t.Errorf(" expected: %#v", testCase.out)
1524 t.Errorf(" got: %#v", ctx.result)
1525 }
1526 }
1527}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001528
1529// TODO(jungjw): Consider making this more robust by ignoring path order.
1530func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1531 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1532 flags := strings.Split(variables["javacFlags"], " ")
1533 got := ""
1534 for _, flag := range flags {
1535 keyEnd := strings.Index(flag, "=")
1536 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1537 got = flag[keyEnd+1:]
1538 break
1539 }
1540 }
1541 if expected != got {
1542 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1543 }
1544}
1545
1546func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001547 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001548 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001549 bp := `
1550 java_library {
1551 name: "foo",
1552 srcs: ["a.java"],
1553 java_version: "1.8",
1554 }
1555
1556 java_library {
1557 name: "bar",
1558 srcs: ["b.java"],
1559 sdk_version: "none",
1560 system_modules: "none",
1561 patch_module: "java.base",
1562 java_version: "1.8",
1563 }
1564
1565 java_library {
1566 name: "baz",
1567 srcs: ["c.java"],
1568 patch_module: "java.base",
1569 java_version: "1.8",
1570 }
1571 `
1572 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001573
1574 checkPatchModuleFlag(t, ctx, "foo", "")
1575 checkPatchModuleFlag(t, ctx, "bar", "")
1576 checkPatchModuleFlag(t, ctx, "baz", "")
1577 })
1578
Pete Gillin0c2143e2019-05-02 15:32:11 +01001579 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001580 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001581 bp := `
1582 java_library {
1583 name: "foo",
1584 srcs: ["a.java"],
1585 }
1586
1587 java_library {
1588 name: "bar",
1589 srcs: ["b.java"],
1590 sdk_version: "none",
1591 system_modules: "none",
1592 patch_module: "java.base",
1593 }
1594
1595 java_library {
1596 name: "baz",
1597 srcs: ["c.java"],
1598 patch_module: "java.base",
1599 }
1600 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001601 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001602
1603 checkPatchModuleFlag(t, ctx, "foo", "")
1604 expected := "java.base=.:" + buildDir
1605 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09001606 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001607 checkPatchModuleFlag(t, ctx, "baz", expected)
1608 })
1609}
Paul Duffina7b9f422020-01-10 17:12:18 +00001610
1611func TestJavaSystemModules(t *testing.T) {
1612 ctx, _ := testJava(t, `
1613 java_system_modules {
1614 name: "system-modules",
1615 libs: ["system-module1", "system-module2"],
1616 }
1617 java_library {
1618 name: "system-module1",
1619 srcs: ["a.java"],
1620 sdk_version: "none",
1621 system_modules: "none",
1622 }
1623 java_library {
1624 name: "system-module2",
1625 srcs: ["b.java"],
1626 sdk_version: "none",
1627 system_modules: "none",
1628 }
1629 `)
1630
1631 // check the existence of the module
1632 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1633
1634 cmd := systemModules.Rule("jarsTosystemModules")
1635
1636 // make sure the command compiles against the supplied modules.
1637 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1638 if !strings.Contains(cmd.Args["classpath"], module) {
1639 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1640 module)
1641 }
1642 }
1643}
Paul Duffin90169ba2020-01-10 17:16:44 +00001644
1645func TestJavaSystemModulesImport(t *testing.T) {
1646 ctx, _ := testJava(t, `
1647 java_system_modules_import {
1648 name: "system-modules",
1649 libs: ["system-module1", "system-module2"],
1650 }
1651 java_import {
1652 name: "system-module1",
1653 jars: ["a.jar"],
1654 }
1655 java_import {
1656 name: "system-module2",
1657 jars: ["b.jar"],
1658 }
1659 `)
1660
1661 // check the existence of the module
1662 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1663
1664 cmd := systemModules.Rule("jarsTosystemModules")
1665
1666 // make sure the command compiles against the supplied modules.
1667 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1668 if !strings.Contains(cmd.Args["classpath"], module) {
1669 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1670 module)
1671 }
1672 }
1673}
Paul Duffin83a2d962019-11-19 19:44:10 +00001674
1675func TestJavaLibraryWithSystemModules(t *testing.T) {
1676 ctx, _ := testJava(t, `
1677 java_library {
1678 name: "lib-with-source-system-modules",
1679 srcs: [
1680 "a.java",
1681 ],
1682 sdk_version: "none",
1683 system_modules: "source-system-modules",
1684 }
1685
1686 java_library {
1687 name: "source-jar",
1688 srcs: [
1689 "a.java",
1690 ],
1691 }
1692
1693 java_system_modules {
1694 name: "source-system-modules",
1695 libs: ["source-jar"],
1696 }
1697
1698 java_library {
1699 name: "lib-with-prebuilt-system-modules",
1700 srcs: [
1701 "a.java",
1702 ],
1703 sdk_version: "none",
1704 system_modules: "prebuilt-system-modules",
1705 }
1706
1707 java_import {
1708 name: "prebuilt-jar",
1709 jars: ["a.jar"],
1710 }
1711
1712 java_system_modules_import {
1713 name: "prebuilt-system-modules",
1714 libs: ["prebuilt-jar"],
1715 }
1716 `)
1717
1718 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
1719
1720 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
1721}
1722
1723func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
1724 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
1725 bootClasspath := javacRule.Args["bootClasspath"]
1726 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
1727 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
1728 }
1729}
Jiyong Park19604de2020-03-24 16:44:11 +09001730
1731func TestAidlExportIncludeDirsFromImports(t *testing.T) {
1732 ctx, _ := testJava(t, `
1733 java_library {
1734 name: "foo",
1735 srcs: ["aidl/foo/IFoo.aidl"],
1736 libs: ["bar"],
1737 }
1738
1739 java_import {
1740 name: "bar",
1741 jars: ["a.jar"],
1742 aidl: {
1743 export_include_dirs: ["aidl/bar"],
1744 },
1745 }
1746 `)
1747
1748 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
1749 expectedAidlFlag := "-Iaidl/bar"
1750 if !strings.Contains(aidlCommand, expectedAidlFlag) {
1751 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
1752 }
1753}