blob: f7cf03f4db6bf0eea5314401023e5ce568a3ad74 [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 (
Paul Duffin1a393322020-11-18 16:36:47 +000018 "fmt"
Colin Cross72bb3632017-07-13 16:23:21 -070019 "io/ioutil"
20 "os"
21 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070022 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010023 "regexp"
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"
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 Crossae8600b2020-10-29 17:09:13 -070073func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070074
Colin Crossae8600b2020-10-29 17:09:13 -070075 ctx := android.NewTestArchContext(config)
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)
Liz Kammerdd849a82020-06-12 16:38:45 -070083 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
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 Duffin44f1d842020-06-26 20:17:02 +010088 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000089
90 RegisterPrebuiltApisBuildComponents(ctx)
91
Liz Kammerdd849a82020-06-12 16:38:45 -070092 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070093 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross06fa5882020-10-29 18:21:38 -070094 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(ctx.Context, OverlaySingletonFactory))
95 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(ctx.Context, sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070096
Paul Duffin021f4e52020-07-30 16:04:17 +010097 android.RegisterPrebuiltMutators(ctx)
98
Colin Crossa4f08812018-10-02 22:03:40 -070099 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000100 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700101
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000102 dexpreopt.RegisterToolModulesForTest(ctx)
103
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700104 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
105 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
106 })
107
Colin Cross527012a2017-11-30 22:56:16 -0800108 return ctx
109}
110
111func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800112 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800113
Colin Cross98be1bb2019-12-13 20:41:13 -0800114 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000115 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800116
Colin Crossae8600b2020-10-29 17:09:13 -0700117 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100118 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800119 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700120 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800121 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800122}
123
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900124func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900125 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800126 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900127}
128
Colin Cross98be1bb2019-12-13 20:41:13 -0800129func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900130 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700131 ctx := testContext(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900132
Colin Cross98be1bb2019-12-13 20:41:13 -0800133 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000134 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900135
Colin Crossae8600b2020-10-29 17:09:13 -0700136 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900137 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
138 if len(errs) > 0 {
139 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900140 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900141 }
142 _, errs = ctx.PrepareBuildActions(config)
143 if len(errs) > 0 {
144 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900145 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900146 }
147
148 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900149
150 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900151}
152
Colin Cross238c1f32020-06-07 16:58:18 -0700153func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
154 t.Helper()
155 return testJavaWithConfig(t, testConfig(nil, bp, fs))
156}
157
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700158func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800159 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700160 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900161}
162
Colin Cross98be1bb2019-12-13 20:41:13 -0800163func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900164 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700165 ctx := testContext(config)
Colin Cross527012a2017-11-30 22:56:16 -0800166 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700167
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700168 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700169}
170
Colin Cross2acdae82017-09-15 19:44:24 -0700171func moduleToPath(name string) string {
172 switch {
173 case name == `""`:
174 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700175 case strings.HasSuffix(name, ".jar"):
176 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700177 default:
178 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700179 }
180}
181
Jeongik Chae403e9e2019-12-07 00:16:24 +0900182func TestJavaLinkType(t *testing.T) {
183 testJava(t, `
184 java_library {
185 name: "foo",
186 srcs: ["a.java"],
187 libs: ["bar"],
188 static_libs: ["baz"],
189 }
190
191 java_library {
192 name: "bar",
193 sdk_version: "current",
194 srcs: ["b.java"],
195 }
196
197 java_library {
198 name: "baz",
199 sdk_version: "system_current",
200 srcs: ["c.java"],
201 }
202 `)
203
Steven Moreland00298982020-11-17 21:44:36 +0000204 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900205 java_library {
206 name: "foo",
207 srcs: ["a.java"],
208 libs: ["bar"],
209 sdk_version: "current",
210 static_libs: ["baz"],
211 }
212
213 java_library {
214 name: "bar",
215 sdk_version: "current",
216 srcs: ["b.java"],
217 }
218
219 java_library {
220 name: "baz",
221 sdk_version: "system_current",
222 srcs: ["c.java"],
223 }
224 `)
225
226 testJava(t, `
227 java_library {
228 name: "foo",
229 srcs: ["a.java"],
230 libs: ["bar"],
231 sdk_version: "system_current",
232 static_libs: ["baz"],
233 }
234
235 java_library {
236 name: "bar",
237 sdk_version: "current",
238 srcs: ["b.java"],
239 }
240
241 java_library {
242 name: "baz",
243 sdk_version: "system_current",
244 srcs: ["c.java"],
245 }
246 `)
247
Steven Moreland00298982020-11-17 21:44:36 +0000248 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900249 java_library {
250 name: "foo",
251 srcs: ["a.java"],
252 libs: ["bar"],
253 sdk_version: "system_current",
254 static_libs: ["baz"],
255 }
256
257 java_library {
258 name: "bar",
259 sdk_version: "current",
260 srcs: ["b.java"],
261 }
262
263 java_library {
264 name: "baz",
265 srcs: ["c.java"],
266 }
267 `)
268}
269
Colin Cross72bb3632017-07-13 16:23:21 -0700270func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700271 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700272 java_library {
273 name: "foo",
274 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700275 libs: ["bar"],
276 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700277 }
278
279 java_library {
280 name: "bar",
281 srcs: ["b.java"],
282 }
283
284 java_library {
285 name: "baz",
286 srcs: ["c.java"],
287 }
Colin Crossd5934c82017-10-02 13:55:26 -0700288 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700289
Colin Cross4c428df2017-09-15 17:36:05 -0700290 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700291 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700292
293 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
294 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
295 }
296
Colin Cross1ee23172017-10-18 14:44:18 -0700297 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700298 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
299 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700300
Nan Zhanged19fc32017-10-19 13:06:22 -0700301 if !strings.Contains(javac.Args["classpath"], barTurbine) {
302 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700303 }
304
Nan Zhanged19fc32017-10-19 13:06:22 -0700305 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
306 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700307 }
308
309 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
310 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700311 }
312}
313
Artur Satayev9cf46692019-11-26 18:08:34 +0000314func TestExportedPlugins(t *testing.T) {
315 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800316 library string
317 processors string
318 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000319 }
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 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800377 {
378 name: "Exports plugin to with generates_api to dependee",
379 extra: `
380 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
381 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
382 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
383 `,
384 results: []Result{
385 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
386 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
387 },
388 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000389 }
390
391 for _, test := range tests {
392 t.Run(test.name, func(t *testing.T) {
393 ctx, _ := testJava(t, `
394 java_plugin {
395 name: "plugin",
396 processor_class: "com.android.TestPlugin",
397 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800398 java_plugin {
399 name: "plugin_generates_api",
400 generates_api: true,
401 processor_class: "com.android.TestPlugin",
402 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000403 `+test.extra)
404
405 for _, want := range test.results {
406 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
407 if javac.Args["processor"] != want.processors {
408 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
409 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800410 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
411 disableTurbine := turbine.BuildParams.Rule == nil
412 if disableTurbine != want.disableTurbine {
413 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
414 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000415 }
416 })
417 }
418}
419
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900420func TestSdkVersionByPartition(t *testing.T) {
421 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900422 java_library {
423 name: "foo",
424 srcs: ["a.java"],
425 vendor: true,
426 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900427 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900428
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900429 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900430 java_library {
431 name: "bar",
432 srcs: ["b.java"],
433 }
434 `)
435
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900436 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900437 bp := `
438 java_library {
439 name: "foo",
440 srcs: ["a.java"],
441 product_specific: true,
442 }
443 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800444
445 config := testConfig(nil, bp, nil)
446 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900447 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800448 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 +0900449 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800450 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900451 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900452 }
453}
454
Colin Crossd5934c82017-10-02 13:55:26 -0700455func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700456 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700457 java_library {
458 name: "foo",
459 srcs: ["a.java"],
460 target: {
461 android: {
462 srcs: ["b.java"],
463 },
464 },
465 }
466 `)
467
468 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
469 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
470 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
471 }
472}
473
Colin Cross6b4a32d2017-12-05 13:42:45 -0800474func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700475 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800476 java_library_host {
477 name: "foo",
478 srcs: ["a.java"],
479 }
480
481 java_binary_host {
482 name: "bar",
483 srcs: ["b.java"],
484 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700485 jni_libs: ["libjni"],
486 }
487
488 cc_library_shared {
489 name: "libjni",
490 host_supported: true,
491 device_supported: false,
492 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800493 }
494 `)
495
496 buildOS := android.BuildOs.String()
497
498 bar := ctx.ModuleForTests("bar", buildOS+"_common")
499 barJar := bar.Output("bar.jar").Output.String()
500 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
501 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
502
Colin Cross89226d92020-10-09 19:00:54 -0700503 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
504 libjniSO := libjni.Rule("Cp").Output.String()
505
Colin Cross6b4a32d2017-12-05 13:42:45 -0800506 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700507 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
508 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800509 }
Colin Cross89226d92020-10-09 19:00:54 -0700510
511 // Test that the install binary wrapper depends on the installed JNI libraries
512 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
513 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800514 }
Alex Humesky2070e322020-06-09 20:23:08 -0400515}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800516
Alex Humesky2070e322020-06-09 20:23:08 -0400517func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
518 bp := `
519 java_library {
520 name: "target_library",
521 srcs: ["a.java"],
522 }
523
524 java_binary_host {
525 name: "host_binary",
526 srcs: ["b.java"],
527 }
528 `
529 config := testConfig(nil, bp, nil)
530 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
531
532 ctx, _ := testJavaWithConfig(t, config)
533
Liz Kammer7941b302020-07-28 13:27:34 -0700534 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400535 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
536 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
537 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
538 t.Errorf("target library javac flags %v should contain "+
539 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
540 }
541
542 // check that -g is not overridden for host modules
543 buildOS := android.BuildOs.String()
544 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
545 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
546 if strings.Contains(hostJavaFlags, "-g:source,lines") {
547 t.Errorf("java_binary_host javac flags %v should not have "+
548 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
549 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800550}
551
Colin Cross72bb3632017-07-13 16:23:21 -0700552func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700553 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700554 java_library {
555 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000556 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000557 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700558 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700559 }
560
Colin Cross74d73e22017-08-02 11:05:49 -0700561 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700562 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700563 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700564 }
565
Colin Cross74d73e22017-08-02 11:05:49 -0700566 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700567 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700568 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700569 sdk_version: "current",
570 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700571 }
Colin Cross42be7612019-02-21 18:12:14 -0800572
573 dex_import {
574 name: "qux",
575 jars: ["b.jar"],
576 }
Colin Cross79c7c262019-04-17 11:11:46 -0700577
578 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000579 name: "sdklib",
580 public: {
581 jars: ["c.jar"],
582 },
583 }
584
Paul Duffin91547182019-11-12 19:39:36 +0000585 prebuilt_stubs_sources {
586 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000587 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000588 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000589
590 java_test_import {
591 name: "test",
592 jars: ["a.jar"],
593 test_suites: ["cts"],
594 test_config: "AndroidTest.xml",
595 }
Colin Cross72bb3632017-07-13 16:23:21 -0700596 `)
597
Paul Duffin9b478b02019-12-10 13:41:51 +0000598 fooModule := ctx.ModuleForTests("foo", "android_common")
599 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700600 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700601 barModule := ctx.ModuleForTests("bar", "android_common")
602 barJar := barModule.Rule("combineJar").Output
603 bazModule := ctx.ModuleForTests("baz", "android_common")
604 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700605 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700606
Paul Duffin9b478b02019-12-10 13:41:51 +0000607 fooLibrary := fooModule.Module().(*Library)
608 assertDeepEquals(t, "foo java sources incorrect",
609 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000610
Paul Duffin9b478b02019-12-10 13:41:51 +0000611 assertDeepEquals(t, "foo java source jars incorrect",
612 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
613 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000614
Colin Cross37f6d792018-07-12 12:28:41 -0700615 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
616 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700617 }
618
Liz Kammerd6c31d22020-08-05 15:40:41 -0700619 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
620 if barDexJar != nil {
621 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
622 }
623
Colin Cross79c7c262019-04-17 11:11:46 -0700624 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
625 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
626 }
627
Colin Cross37f6d792018-07-12 12:28:41 -0700628 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
629 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700630 }
Colin Cross42be7612019-02-21 18:12:14 -0800631
Liz Kammerd6c31d22020-08-05 15:40:41 -0700632 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
633 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
634 if bazDexJar != expectedDexJar {
635 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
636 }
637
Colin Cross42be7612019-02-21 18:12:14 -0800638 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700639}
640
Paul Duffin9b478b02019-12-10 13:41:51 +0000641func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
642 if !reflect.DeepEqual(expected, actual) {
643 t.Errorf("%s: expected %q, found %q", message, expected, actual)
644 }
645}
646
Paul Duffin1a393322020-11-18 16:36:47 +0000647func TestPrebuiltStubsSources(t *testing.T) {
648 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
649 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
650prebuilt_stubs_sources {
651 name: "stubs-source",
652 srcs: ["%s"],
653}`, sourcesPath), map[string][]byte{
654 "stubs/sources/pkg/A.java": nil,
655 "stubs/sources/pkg/B.java": nil,
656 })
657
658 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
659 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
660 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
661 }
662 }
663
664 t.Run("empty/missing directory", func(t *testing.T) {
665 test(t, "empty-directory", []string{})
666 })
667
668 t.Run("non-empty set of sources", func(t *testing.T) {
669 test(t, "stubs/sources", []string{
670 "stubs/sources/pkg/A.java",
671 "stubs/sources/pkg/B.java",
672 })
673 })
674}
675
Paul Duffin56d44902020-01-31 13:36:25 +0000676func TestJavaSdkLibraryImport(t *testing.T) {
677 ctx, _ := testJava(t, `
678 java_library {
679 name: "foo",
680 srcs: ["a.java"],
681 libs: ["sdklib"],
682 sdk_version: "current",
683 }
684
685 java_library {
686 name: "foo.system",
687 srcs: ["a.java"],
688 libs: ["sdklib"],
689 sdk_version: "system_current",
690 }
691
692 java_library {
693 name: "foo.test",
694 srcs: ["a.java"],
695 libs: ["sdklib"],
696 sdk_version: "test_current",
697 }
698
699 java_sdk_library_import {
700 name: "sdklib",
701 public: {
702 jars: ["a.jar"],
703 },
704 system: {
705 jars: ["b.jar"],
706 },
707 test: {
708 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100709 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000710 },
711 }
712 `)
713
714 for _, scope := range []string{"", ".system", ".test"} {
715 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
716 javac := fooModule.Rule("javac")
717
718 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
719 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
720 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
721 }
722 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100723
Paul Duffincee7e662020-07-09 17:32:57 +0100724 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100725 `prebuilt_sdklib.stubs`,
726 `prebuilt_sdklib.stubs.source.test`,
727 `prebuilt_sdklib.stubs.system`,
728 `prebuilt_sdklib.stubs.test`,
729 })
730}
731
732func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
733 ctx, _ := testJava(t, `
734 java_sdk_library {
735 name: "sdklib",
736 srcs: ["a.java"],
737 sdk_version: "none",
738 system_modules: "none",
739 public: {
740 enabled: true,
741 },
742 }
743
744 java_sdk_library_import {
745 name: "sdklib",
746 public: {
747 jars: ["a.jar"],
748 },
749 }
750 `)
751
Paul Duffincee7e662020-07-09 17:32:57 +0100752 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100753 `dex2oatd`,
754 `prebuilt_sdklib`,
755 `sdklib.impl`,
756 `sdklib.stubs`,
757 `sdklib.stubs.source`,
758 `sdklib.xml`,
759 })
760
Paul Duffincee7e662020-07-09 17:32:57 +0100761 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100762 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100763 `sdklib.impl`,
764 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
765 // dependency is added after prebuilts may have been renamed and so has to use
766 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100767 `sdklib.xml`,
768 })
769}
770
771func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
772 ctx, _ := testJava(t, `
773 java_sdk_library {
774 name: "sdklib",
775 srcs: ["a.java"],
776 sdk_version: "none",
777 system_modules: "none",
778 public: {
779 enabled: true,
780 },
781 }
782
783 java_sdk_library_import {
784 name: "sdklib",
785 prefer: true,
786 public: {
787 jars: ["a.jar"],
788 },
789 }
790 `)
791
Paul Duffincee7e662020-07-09 17:32:57 +0100792 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100793 `dex2oatd`,
794 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100795 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100796 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100797 `sdklib.stubs.source`,
798 `sdklib.xml`,
799 })
800
Paul Duffincee7e662020-07-09 17:32:57 +0100801 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100802 `prebuilt_sdklib.stubs`,
803 `sdklib.impl`,
804 `sdklib.xml`,
805 })
Paul Duffin56d44902020-01-31 13:36:25 +0000806}
807
JaeMan Parkff715562020-10-19 17:25:58 +0900808func TestJavaSdkLibraryEnforce(t *testing.T) {
809 partitionToBpOption := func(partition string) string {
810 switch partition {
811 case "system":
812 return ""
813 case "vendor":
814 return "soc_specific: true,"
815 case "product":
816 return "product_specific: true,"
817 default:
818 panic("Invalid partition group name: " + partition)
819 }
820 }
821
822 type testConfigInfo struct {
823 libraryType string
824 fromPartition string
825 toPartition string
826 enforceVendorInterface bool
827 enforceProductInterface bool
828 enforceJavaSdkLibraryCheck bool
829 allowList []string
830 }
831
832 createTestConfig := func(info testConfigInfo) android.Config {
833 bpFileTemplate := `
834 java_library {
835 name: "foo",
836 srcs: ["foo.java"],
837 libs: ["bar"],
838 sdk_version: "current",
839 %s
840 }
841
842 %s {
843 name: "bar",
844 srcs: ["bar.java"],
845 sdk_version: "current",
846 %s
847 }
848 `
849
850 bpFile := fmt.Sprintf(bpFileTemplate,
851 partitionToBpOption(info.fromPartition),
852 info.libraryType,
853 partitionToBpOption(info.toPartition))
854
855 config := testConfig(nil, bpFile, nil)
856 configVariables := config.TestProductVariables
857
858 configVariables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
859 if info.enforceVendorInterface {
860 configVariables.DeviceVndkVersion = proptools.StringPtr("current")
861 }
862 configVariables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
863 configVariables.InterPartitionJavaLibraryAllowList = info.allowList
864
865 return config
866 }
867
868 isValidDependency := func(configInfo testConfigInfo) bool {
869 if configInfo.enforceVendorInterface == false {
870 return true
871 }
872
873 if configInfo.enforceJavaSdkLibraryCheck == false {
874 return true
875 }
876
877 if inList("bar", configInfo.allowList) {
878 return true
879 }
880
881 if configInfo.libraryType == "java_library" {
882 if configInfo.fromPartition != configInfo.toPartition {
883 if !configInfo.enforceProductInterface &&
884 ((configInfo.fromPartition == "system" && configInfo.toPartition == "product") ||
885 (configInfo.fromPartition == "product" && configInfo.toPartition == "system")) {
886 return true
887 }
888 return false
889 }
890 }
891
892 return true
893 }
894
895 errorMessage := "is not allowed across the partitions"
896
897 allPartitionCombinations := func() [][2]string {
898 var result [][2]string
899 partitions := []string{"system", "vendor", "product"}
900
901 for _, fromPartition := range partitions {
902 for _, toPartition := range partitions {
903 result = append(result, [2]string{fromPartition, toPartition})
904 }
905 }
906
907 return result
908 }
909
910 allFlagCombinations := func() [][3]bool {
911 var result [][3]bool
912 flagValues := [2]bool{false, true}
913
914 for _, vendorInterface := range flagValues {
915 for _, productInterface := range flagValues {
916 for _, enableEnforce := range flagValues {
917 result = append(result, [3]bool{vendorInterface, productInterface, enableEnforce})
918 }
919 }
920 }
921
922 return result
923 }
924
925 for _, libraryType := range []string{"java_library", "java_sdk_library"} {
926 for _, partitionValues := range allPartitionCombinations() {
927 for _, flagValues := range allFlagCombinations() {
928 testInfo := testConfigInfo{
929 libraryType: libraryType,
930 fromPartition: partitionValues[0],
931 toPartition: partitionValues[1],
932 enforceVendorInterface: flagValues[0],
933 enforceProductInterface: flagValues[1],
934 enforceJavaSdkLibraryCheck: flagValues[2],
935 }
936
937 if isValidDependency(testInfo) {
938 testJavaWithConfig(t, createTestConfig(testInfo))
939 } else {
940 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testInfo))
941 }
942 }
943 }
944 }
945
946 testJavaWithConfig(t, createTestConfig(testConfigInfo{
947 libraryType: "java_library",
948 fromPartition: "vendor",
949 toPartition: "system",
950 enforceVendorInterface: true,
951 enforceProductInterface: true,
952 enforceJavaSdkLibraryCheck: true,
953 allowList: []string{"bar"},
954 }))
955
956 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
957 libraryType: "java_library",
958 fromPartition: "vendor",
959 toPartition: "system",
960 enforceVendorInterface: true,
961 enforceProductInterface: true,
962 enforceJavaSdkLibraryCheck: true,
963 allowList: []string{"foo"},
964 }))
965}
966
Colin Cross89536d42017-07-07 14:35:50 -0700967func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700968 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700969 java_defaults {
970 name: "defaults",
971 srcs: ["a.java"],
972 libs: ["bar"],
973 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700974 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700975 }
976
977 java_library {
978 name: "foo",
979 defaults: ["defaults"],
980 }
981
982 java_library {
983 name: "bar",
984 srcs: ["b.java"],
985 }
986
987 java_library {
988 name: "baz",
989 srcs: ["c.java"],
990 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700991
992 android_test {
993 name: "atestOptimize",
994 defaults: ["defaults"],
995 optimize: {enabled: true},
996 }
997
998 android_test {
999 name: "atestNoOptimize",
1000 defaults: ["defaults"],
1001 }
1002
1003 android_test {
1004 name: "atestDefault",
1005 srcs: ["a.java"],
1006 }
Colin Cross89536d42017-07-07 14:35:50 -07001007 `)
1008
Colin Cross4c428df2017-09-15 17:36:05 -07001009 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -07001010 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -07001011
1012 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
1013 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
1014 }
1015
Nan Zhanged19fc32017-10-19 13:06:22 -07001016 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1017 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1018 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -07001019 }
1020
Colin Cross1ee23172017-10-18 14:44:18 -07001021 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -07001022 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
1023 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -07001024 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001025
1026 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1027 if atestOptimize.Output == nil {
1028 t.Errorf("atestOptimize should optimize APK")
1029 }
1030
1031 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1032 if atestNoOptimize.Output == nil {
1033 t.Errorf("atestNoOptimize should not optimize APK")
1034 }
1035
1036 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1037 if atestDefault.Output == nil {
1038 t.Errorf("atestDefault should optimize APK")
1039 }
Colin Cross89536d42017-07-07 14:35:50 -07001040}
1041
Colin Cross0f37af02017-09-27 17:42:05 -07001042func TestResources(t *testing.T) {
1043 var table = []struct {
1044 name string
1045 prop string
1046 extra string
1047 args string
1048 }{
1049 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001050 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001051 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001052 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001053 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001054 },
1055 {
1056 // Test that a module with java_resources includes the files
1057 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001058 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1059 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001060 },
1061 {
1062 // Test that a module with a filegroup in java_resources includes the files with the
1063 // path prefix
1064 name: "resource filegroup",
1065 prop: `java_resources: [":foo-res"]`,
1066 extra: `
1067 filegroup {
1068 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001069 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001070 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001071 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001072 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001073 },
1074 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001075 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1076 name: "wildcard dirs",
1077 prop: `java_resource_dirs: ["java-res/*"]`,
1078 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1079 },
1080 {
1081 // Test that a module exclude_java_resource_dirs excludes the files
1082 name: "wildcard dirs",
1083 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1084 args: "-C java-res/a -f java-res/a/a",
1085 },
Colin Crosscedd4762018-09-13 11:26:19 -07001086 {
1087 // Test wildcards in java_resources
1088 name: "wildcard files",
1089 prop: `java_resources: ["java-res/**/*"]`,
1090 args: "-C . -f java-res/a/a -f java-res/b/b",
1091 },
1092 {
1093 // Test exclude_java_resources with java_resources
1094 name: "wildcard files with exclude",
1095 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1096 args: "-C . -f java-res/a/a",
1097 },
1098 {
1099 // Test exclude_java_resources with java_resource_dirs
1100 name: "resource dirs with exclude files",
1101 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1102 args: "-C java-res -f java-res/a/a",
1103 },
1104 {
1105 // Test exclude_java_resource_dirs with java_resource_dirs
1106 name: "resource dirs with exclude files",
1107 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1108 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1109 },
Colin Cross0f37af02017-09-27 17:42:05 -07001110 }
1111
1112 for _, test := range table {
1113 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001114 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001115 java_library {
1116 name: "foo",
1117 srcs: [
1118 "a.java",
1119 "b.java",
1120 "c.java",
1121 ],
1122 `+test.prop+`,
1123 }
Colin Cross238c1f32020-06-07 16:58:18 -07001124 `+test.extra,
1125 map[string][]byte{
1126 "java-res/a/a": nil,
1127 "java-res/b/b": nil,
1128 "java-res2/a": nil,
1129 },
1130 )
Colin Cross0f37af02017-09-27 17:42:05 -07001131
Colin Cross331a1212018-08-15 20:40:52 -07001132 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001133 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001134
1135 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1136 t.Errorf("foo combined jars %v does not contain %q",
1137 foo.Inputs.Strings(), fooRes.Output.String())
1138 }
1139
Colin Crossaf9c55b2017-10-03 14:50:08 -07001140 if fooRes.Args["jarArgs"] != test.args {
1141 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001142 fooRes.Args["jarArgs"], test.args)
1143 }
1144 })
1145 }
1146}
1147
Colin Cross0c4ce212019-05-03 15:28:19 -07001148func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001149 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001150 java_library {
1151 name: "foo",
1152 srcs: [
1153 "a.java",
1154 "b.java",
1155 "c.java",
1156 ],
1157 include_srcs: true,
1158 }
1159
1160 java_library {
1161 name: "bar",
1162 srcs: [
1163 "a.java",
1164 "b.java",
1165 "c.java",
1166 ],
1167 java_resource_dirs: ["java-res"],
1168 include_srcs: true,
1169 }
Colin Cross238c1f32020-06-07 16:58:18 -07001170 `, map[string][]byte{
1171 "java-res/a/a": nil,
1172 "java-res/b/b": nil,
1173 "java-res2/a": nil,
1174 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001175
1176 // Test a library with include_srcs: true
1177 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1178 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1179
1180 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1181 t.Errorf("foo combined jars %v does not contain %q", w, g)
1182 }
1183
1184 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1185 t.Errorf("foo source jar args %q is not %q", w, g)
1186 }
1187
1188 // Test a library with include_srcs: true and resources
1189 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1190 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1191 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1192 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1193
1194 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1195 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1196 }
1197
1198 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1199 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1200 }
1201
1202 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1203 t.Errorf("bar combined jars %v does not contain %q", w, g)
1204 }
1205
1206 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1207 t.Errorf("bar source jar args %q is not %q", w, g)
1208 }
1209
1210 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1211 t.Errorf("bar resource jar args %q is not %q", w, g)
1212 }
1213}
1214
Colin Cross54190b32017-10-09 15:34:10 -07001215func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001216 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001217 java_library {
1218 name: "foo",
1219 srcs: [
1220 "a*.java",
1221 ":gen",
1222 "b*.java",
1223 ],
1224 }
1225
1226 genrule {
1227 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001228 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001229 out: ["gen.java"],
1230 }
Colin Cross238c1f32020-06-07 16:58:18 -07001231 `, map[string][]byte{
1232 "a.java": nil,
1233 "b.java": nil,
1234 })
Colin Cross54190b32017-10-09 15:34:10 -07001235
1236 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1237 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1238
Colin Cross15e86d92017-10-20 15:07:08 -07001239 if filepath.Base(genrule.Output.String()) != "gen.java" {
1240 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001241 }
1242
1243 if len(javac.Inputs) != 3 ||
1244 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001245 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001246 javac.Inputs[2].String() != "b.java" {
1247 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1248 }
1249}
1250
Nan Zhang61eaedb2017-11-02 13:28:15 -07001251func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001252 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001253 java_library {
1254 name: "foo",
1255 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001256 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001257 }
1258
1259 java_library {
1260 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001261 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001262 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001263 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001264 }
1265
1266 java_library {
1267 name: "baz",
1268 srcs: ["c.java"],
1269 libs: ["bar"],
1270 sdk_version: "14",
1271 }
1272 `)
1273
1274 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1275 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1276 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1277 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1278 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1279
1280 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1281 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1282 }
1283
1284 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1285 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1286 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1287 }
1288 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1289 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1290 }
1291 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1292 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1293 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001294 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001295 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001296 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001297 }
1298}
1299
1300func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001301 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001302 java_library {
1303 name: "bar",
1304 srcs: ["a.java","b.java","c.java"],
1305 javac_shard_size: 1
1306 }
1307 `)
1308
1309 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1310 for i := 0; i < 3; i++ {
1311 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1312 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1313 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1314 }
1315 }
1316}
1317
Nan Zhang581fd212018-01-10 16:06:12 -08001318func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001319 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001320 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001321 name: "droiddoc-templates-sdk",
1322 path: ".",
1323 }
Jiyong Park29074592019-07-07 16:27:47 +09001324 filegroup {
1325 name: "bar-doc-aidl-srcs",
1326 srcs: ["bar-doc/IBar.aidl"],
1327 path: "bar-doc",
1328 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001329 droidstubs {
1330 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001331 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001332 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001333 ],
Steve Kim3666c702020-09-01 17:58:01 +00001334 exclude_srcs: [
1335 "bar-doc/b.java"
1336 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001337 api_levels_annotations_dirs: [
1338 "droiddoc-templates-sdk",
1339 ],
1340 api_levels_annotations_enabled: true,
1341 }
1342 droiddoc {
1343 name: "bar-doc",
1344 srcs: [
1345 ":bar-stubs",
1346 "bar-doc/IFoo.aidl",
1347 ":bar-doc-aidl-srcs",
1348 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001349 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001350 hdf: [
1351 "android.whichdoc offline",
1352 ],
1353 knowntags: [
1354 "bar-doc/known_oj_tags.txt",
1355 ],
1356 proofread_file: "libcore-proofread.txt",
1357 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001358 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001359 }
Colin Cross238c1f32020-06-07 16:58:18 -07001360 `,
1361 map[string][]byte{
1362 "bar-doc/a.java": nil,
1363 "bar-doc/b.java": nil,
1364 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001365 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1366 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1367 if err != nil {
1368 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1369 }
1370 if len(barStubsOutputs) != 1 {
1371 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001372 }
Nan Zhang581fd212018-01-10 16:06:12 -08001373
Liz Kammere1ab2502020-09-10 15:29:25 +00001374 barStubsOutput := barStubsOutputs[0]
1375 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1376 javaDoc := barDoc.Rule("javadoc")
1377 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1378 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001379 }
1380
Liz Kammere1ab2502020-09-10 15:29:25 +00001381 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1382 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1383 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1384 }
1385
1386 aidl := barDoc.Rule("aidl")
1387 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001388 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1389 }
1390
1391 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1392 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001393 }
Nan Zhang581fd212018-01-10 16:06:12 -08001394}
1395
Liz Kammer585cac22020-07-06 09:12:57 -07001396func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1397 testJavaError(t, "flags is set. Cannot set args", `
1398 droiddoc_exported_dir {
1399 name: "droiddoc-templates-sdk",
1400 path: ".",
1401 }
1402 filegroup {
1403 name: "bar-doc-aidl-srcs",
1404 srcs: ["bar-doc/IBar.aidl"],
1405 path: "bar-doc",
1406 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001407 droidstubs {
1408 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001409 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001410 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001411 ],
Steve Kim3666c702020-09-01 17:58:01 +00001412 exclude_srcs: [
1413 "bar-doc/b.java"
1414 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001415 api_levels_annotations_dirs: [
1416 "droiddoc-templates-sdk",
1417 ],
1418 api_levels_annotations_enabled: true,
1419 }
1420 droiddoc {
1421 name: "bar-doc",
1422 srcs: [
1423 ":bar-stubs",
1424 "bar-doc/IFoo.aidl",
1425 ":bar-doc-aidl-srcs",
1426 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001427 custom_template: "droiddoc-templates-sdk",
1428 hdf: [
1429 "android.whichdoc offline",
1430 ],
1431 knowntags: [
1432 "bar-doc/known_oj_tags.txt",
1433 ],
1434 proofread_file: "libcore-proofread.txt",
1435 todo_file: "libcore-docs-todo.html",
1436 flags: ["-offlinemode -title \"libcore\""],
1437 args: "-offlinemode -title \"libcore\"",
1438 }
1439 `)
1440}
1441
Liz Kammer3d894b72020-08-04 09:55:13 -07001442func TestDroidstubs(t *testing.T) {
1443 ctx, _ := testJavaWithFS(t, `
1444 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001445 name: "droiddoc-templates-sdk",
1446 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001447 }
1448
1449 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001450 name: "bar-stubs",
1451 srcs: ["bar-doc/a.java"],
1452 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1453 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001454 }
1455
1456 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001457 name: "bar-stubs-other",
1458 srcs: ["bar-doc/a.java"],
1459 high_mem: true,
1460 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1461 api_levels_annotations_enabled: true,
1462 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001463 }
1464 `,
1465 map[string][]byte{
1466 "bar-doc/a.java": nil,
1467 })
1468 testcases := []struct {
1469 moduleName string
1470 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001471 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001472 }{
1473 {
1474 moduleName: "bar-stubs",
1475 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001476 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001477 },
1478 {
1479 moduleName: "bar-stubs-other",
1480 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001481 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001482 },
1483 }
1484 for _, c := range testcases {
1485 m := ctx.ModuleForTests(c.moduleName, "android_common")
1486 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001487 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001488 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001489 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001490 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1491 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001492
1493 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1494 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1495 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001496 }
1497}
1498
Paul Duffin83a2d962019-11-19 19:44:10 +00001499func TestDroidstubsWithSystemModules(t *testing.T) {
1500 ctx, _ := testJava(t, `
1501 droidstubs {
1502 name: "stubs-source-system-modules",
1503 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001504 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001505 ],
1506 sdk_version: "none",
1507 system_modules: "source-system-modules",
1508 }
1509
1510 java_library {
1511 name: "source-jar",
1512 srcs: [
1513 "a.java",
1514 ],
1515 }
1516
1517 java_system_modules {
1518 name: "source-system-modules",
1519 libs: ["source-jar"],
1520 }
1521
1522 droidstubs {
1523 name: "stubs-prebuilt-system-modules",
1524 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001525 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001526 ],
1527 sdk_version: "none",
1528 system_modules: "prebuilt-system-modules",
1529 }
1530
1531 java_import {
1532 name: "prebuilt-jar",
1533 jars: ["a.jar"],
1534 }
1535
1536 java_system_modules_import {
1537 name: "prebuilt-system-modules",
1538 libs: ["prebuilt-jar"],
1539 }
1540 `)
1541
1542 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1543
1544 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1545}
1546
1547func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1548 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1549 var systemJars []string
1550 for _, i := range metalavaRule.Implicits {
1551 systemJars = append(systemJars, i.Base())
1552 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001553 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001554 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1555 }
1556}
1557
Colin Cross54250902017-12-05 09:28:08 -08001558func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001559 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001560 java_library {
1561 name: "foo",
1562 srcs: ["a.java"],
1563 }
1564
1565 java_genrule {
1566 name: "jargen",
1567 tool_files: ["b.java"],
1568 cmd: "$(location b.java) $(in) $(out)",
1569 out: ["jargen.jar"],
1570 srcs: [":foo"],
1571 }
1572
1573 java_library {
1574 name: "bar",
1575 static_libs: ["jargen"],
1576 srcs: ["c.java"],
1577 }
1578
1579 java_library {
1580 name: "baz",
1581 libs: ["jargen"],
1582 srcs: ["c.java"],
1583 }
1584 `)
1585
1586 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1587 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1588 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1589 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1590 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1591
Colin Cross3d680512020-11-13 16:23:53 -08001592 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1593 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001594 }
1595
1596 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1597 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1598 }
1599
1600 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1601 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1602 }
1603
1604 if len(barCombined.Inputs) != 2 ||
1605 barCombined.Inputs[0].String() != bar.Output.String() ||
1606 barCombined.Inputs[1].String() != jargen.Output.String() {
1607 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1608 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1609 }
1610}
1611
Nan Zhang27e284d2018-02-09 21:03:53 +00001612func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001613 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001614 java_library {
1615 name: "foo",
1616 srcs: ["a.java", ":foo-srcs"],
1617 exclude_srcs: ["a.java", ":foo-excludes"],
1618 }
1619
1620 filegroup {
1621 name: "foo-srcs",
1622 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1623 }
1624
1625 filegroup {
1626 name: "foo-excludes",
1627 srcs: ["java-fg/a.java", "java-fg/b.java"],
1628 }
1629 `)
1630
1631 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1632
1633 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1634 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1635 }
1636}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001637
Paul Duffin52d398a2019-06-11 12:31:14 +01001638func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001639 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001640 "libcore/Android.bp": []byte(`
1641 java_library {
1642 name: "core",
1643 sdk_version: "none",
1644 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001645 }
1646
1647 filegroup {
1648 name: "core-jar",
1649 srcs: [":core{.jar}"],
1650 }
1651`),
1652 })
Colin Crossae8600b2020-10-29 17:09:13 -07001653 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001654 run(t, ctx, config)
1655}
1656
1657func TestJavaImport(t *testing.T) {
1658 config := testConfig(nil, "", map[string][]byte{
1659 "libcore/Android.bp": []byte(`
1660 java_import {
1661 name: "core",
1662 sdk_version: "none",
1663 }
1664
1665 filegroup {
1666 name: "core-jar",
1667 srcs: [":core{.jar}"],
1668 }
1669`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001670 })
Colin Crossae8600b2020-10-29 17:09:13 -07001671 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001672 run(t, ctx, config)
1673}
1674
Jiyong Parkc678ad32018-04-10 13:07:10 +09001675func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001676 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001677 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001678 name: "droiddoc-templates-sdk",
1679 path: ".",
1680 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001681 java_sdk_library {
1682 name: "foo",
1683 srcs: ["a.java", "b.java"],
1684 api_packages: ["foo"],
1685 }
1686 java_sdk_library {
1687 name: "bar",
1688 srcs: ["a.java", "b.java"],
1689 api_packages: ["bar"],
1690 }
1691 java_library {
1692 name: "baz",
1693 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001694 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001695 sdk_version: "system_current",
1696 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001697 java_sdk_library {
1698 name: "barney",
1699 srcs: ["c.java"],
1700 api_only: true,
1701 }
1702 java_sdk_library {
1703 name: "betty",
1704 srcs: ["c.java"],
1705 shared_library: false,
1706 }
Paul Duffin859fe962020-05-15 10:20:31 +01001707 java_sdk_library_import {
1708 name: "quuz",
1709 public: {
1710 jars: ["c.jar"],
1711 },
1712 }
1713 java_sdk_library_import {
1714 name: "fred",
1715 public: {
1716 jars: ["b.jar"],
1717 },
1718 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001719 java_sdk_library_import {
1720 name: "wilma",
1721 public: {
1722 jars: ["b.jar"],
1723 },
1724 shared_library: false,
1725 }
Jiyong Park1be96912018-05-28 18:02:19 +09001726 java_library {
1727 name: "qux",
1728 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001729 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001730 sdk_version: "system_current",
1731 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001732 java_library {
1733 name: "baz-test",
1734 srcs: ["c.java"],
1735 libs: ["foo"],
1736 sdk_version: "test_current",
1737 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001738 java_library {
1739 name: "baz-29",
1740 srcs: ["c.java"],
1741 libs: ["foo"],
1742 sdk_version: "system_29",
1743 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001744 java_library {
1745 name: "baz-module-30",
1746 srcs: ["c.java"],
1747 libs: ["foo"],
1748 sdk_version: "module_30",
1749 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001750 `)
1751
1752 // check the existence of the internal modules
1753 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001754 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1755 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1756 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1757 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1758 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1759 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001760 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001761 ctx.ModuleForTests("foo.api.public.28", "")
1762 ctx.ModuleForTests("foo.api.system.28", "")
1763 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001764
1765 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1766 // tests if baz is actually linked to the stubs lib
1767 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1768 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1769 "foo.stubs.system.jar")
1770 }
1771 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001772 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001773 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001774 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001775 }
1776 // test if baz is not linked to the system variant of foo
1777 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1778 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1779 "foo.stubs.jar")
1780 }
Jiyong Park1be96912018-05-28 18:02:19 +09001781
Paul Duffin726d23c2020-01-22 16:30:37 +00001782 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1783 // tests if baz-test is actually linked to the test stubs lib
1784 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1785 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1786 "foo.stubs.test.jar")
1787 }
1788
Paul Duffina2db18f2020-01-22 17:11:15 +00001789 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1790 // tests if baz-29 is actually linked to the system 29 stubs lib
1791 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1792 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1793 "prebuilts/sdk/29/system/foo.jar")
1794 }
1795
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001796 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1797 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1798 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1799 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1800 "prebuilts/sdk/30/module-lib/foo.jar")
1801 }
1802
Jiyong Park1be96912018-05-28 18:02:19 +09001803 // test if baz has exported SDK lib names foo and bar to qux
1804 qux := ctx.ModuleForTests("qux", "android_common")
1805 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001806 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001807 if w := []string{"foo", "bar", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
Paul Duffin859fe962020-05-15 10:20:31 +01001808 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001809 }
1810 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001811}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001812
Anton Hansson7f66efa2020-10-08 14:47:23 +01001813func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
1814 ctx, _ := testJava(t, `
1815 java_sdk_library {
1816 name: "sdk_lib",
1817 srcs: ["a.java"],
1818 impl_only_libs: ["foo"],
1819 stub_only_libs: ["bar"],
1820 }
1821 java_library {
1822 name: "foo",
1823 srcs: ["a.java"],
1824 sdk_version: "current",
1825 }
1826 java_library {
1827 name: "bar",
1828 srcs: ["a.java"],
1829 sdk_version: "current",
1830 }
1831 `)
1832
1833 for _, implName := range []string{"sdk_lib", "sdk_lib.impl"} {
1834 implJavacCp := ctx.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
1835 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1836 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1837 }
1838 }
1839 stubName := apiScopePublic.stubsLibraryModuleName("sdk_lib")
1840 stubsJavacCp := ctx.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
1841 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1842 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1843 }
1844}
1845
Paul Duffindaaa3322020-05-26 18:13:57 +01001846func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1847 ctx, _ := testJava(t, `
1848 java_sdk_library {
1849 name: "foo",
1850 srcs: ["a.java"],
1851 api_only: true,
1852 public: {
1853 enabled: true,
1854 },
1855 }
1856
1857 java_library {
1858 name: "bar",
1859 srcs: ["b.java"],
1860 libs: ["foo"],
1861 }
1862 `)
1863
1864 // The bar library should depend on the stubs jar.
1865 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1866 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1867 t.Errorf("expected %q, found %#q", expected, actual)
1868 }
1869}
1870
Paul Duffin46dc45a2020-05-14 15:39:10 +01001871func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1872 testJava(t, `
1873 java_sdk_library {
1874 name: "foo",
1875 srcs: ["a.java"],
1876 api_packages: ["foo"],
1877 public: {
1878 enabled: true,
1879 },
1880 }
1881
1882 java_library {
1883 name: "bar",
1884 srcs: ["b.java", ":foo{.public.stubs.source}"],
1885 }
1886 `)
1887}
1888
1889func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1890 testJavaError(t, `"foo" does not provide api scope system`, `
1891 java_sdk_library {
1892 name: "foo",
1893 srcs: ["a.java"],
1894 api_packages: ["foo"],
1895 public: {
1896 enabled: true,
1897 },
1898 }
1899
1900 java_library {
1901 name: "bar",
1902 srcs: ["b.java", ":foo{.system.stubs.source}"],
1903 }
1904 `)
1905}
1906
Paul Duffinca8d9a52020-06-26 22:20:25 +01001907func TestJavaSdkLibrary_Deps(t *testing.T) {
1908 ctx, _ := testJava(t, `
1909 java_sdk_library {
1910 name: "sdklib",
1911 srcs: ["a.java"],
1912 sdk_version: "none",
1913 system_modules: "none",
1914 public: {
1915 enabled: true,
1916 },
1917 }
1918 `)
1919
Paul Duffincee7e662020-07-09 17:32:57 +01001920 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001921 `dex2oatd`,
1922 `sdklib.impl`,
1923 `sdklib.stubs`,
1924 `sdklib.stubs.source`,
1925 `sdklib.xml`,
1926 })
1927}
1928
Paul Duffin46dc45a2020-05-14 15:39:10 +01001929func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1930 testJava(t, `
1931 java_sdk_library_import {
1932 name: "foo",
1933 public: {
1934 jars: ["a.jar"],
1935 stub_srcs: ["a.java"],
1936 current_api: "api/current.txt",
1937 removed_api: "api/removed.txt",
1938 },
1939 }
1940
1941 java_library {
1942 name: "bar",
1943 srcs: [":foo{.public.stubs.source}"],
1944 java_resources: [
1945 ":foo{.public.api.txt}",
1946 ":foo{.public.removed-api.txt}",
1947 ],
1948 }
1949 `)
1950}
1951
1952func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1953 bp := `
1954 java_sdk_library_import {
1955 name: "foo",
1956 public: {
1957 jars: ["a.jar"],
1958 },
1959 }
1960 `
1961
1962 t.Run("stubs.source", func(t *testing.T) {
1963 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1964 java_library {
1965 name: "bar",
1966 srcs: [":foo{.public.stubs.source}"],
1967 java_resources: [
1968 ":foo{.public.api.txt}",
1969 ":foo{.public.removed-api.txt}",
1970 ],
1971 }
1972 `)
1973 })
1974
1975 t.Run("api.txt", func(t *testing.T) {
1976 testJavaError(t, `api.txt not available for api scope public`, bp+`
1977 java_library {
1978 name: "bar",
1979 srcs: ["a.java"],
1980 java_resources: [
1981 ":foo{.public.api.txt}",
1982 ],
1983 }
1984 `)
1985 })
1986
1987 t.Run("removed-api.txt", func(t *testing.T) {
1988 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1989 java_library {
1990 name: "bar",
1991 srcs: ["a.java"],
1992 java_resources: [
1993 ":foo{.public.removed-api.txt}",
1994 ],
1995 }
1996 `)
1997 })
1998}
1999
Paul Duffin3375e352020-04-28 10:44:03 +01002000func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
2001 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
2002 java_sdk_library {
2003 name: "foo",
2004 srcs: ["a.java", "b.java"],
2005 api_packages: ["foo"],
2006 // Explicitly disable public to test the check that ensures the set of enabled
2007 // scopes is consistent.
2008 public: {
2009 enabled: false,
2010 },
2011 system: {
2012 enabled: true,
2013 },
2014 }
2015 `)
2016}
2017
Paul Duffin87a05a32020-05-12 11:50:28 +01002018func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
2019 testJava(t, `
2020 java_sdk_library {
2021 name: "foo",
2022 srcs: ["a.java", "b.java"],
2023 api_packages: ["foo"],
2024 system: {
2025 enabled: true,
2026 sdk_version: "module_current",
2027 },
2028 }
2029 `)
2030}
2031
Paul Duffin0c5bae52020-06-02 13:00:08 +01002032func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
2033 testJava(t, `
2034 java_sdk_library {
2035 name: "foo",
2036 srcs: ["a.java", "b.java"],
2037 api_packages: ["foo"],
2038 system: {
2039 enabled: true,
2040 },
2041 module_lib: {
2042 enabled: true,
2043 },
2044 }
2045 `)
2046}
2047
2048func TestJavaSdkLibrary_SystemServer(t *testing.T) {
2049 testJava(t, `
2050 java_sdk_library {
2051 name: "foo",
2052 srcs: ["a.java", "b.java"],
2053 api_packages: ["foo"],
2054 system: {
2055 enabled: true,
2056 },
2057 system_server: {
2058 enabled: true,
2059 },
2060 }
2061 `)
2062}
2063
Paul Duffin803a9562020-05-20 11:52:25 +01002064func TestJavaSdkLibrary_MissingScope(t *testing.T) {
2065 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
2066 java_sdk_library {
2067 name: "foo",
2068 srcs: ["a.java"],
2069 public: {
2070 enabled: false,
2071 },
2072 }
2073
2074 java_library {
2075 name: "baz",
2076 srcs: ["a.java"],
2077 libs: ["foo"],
2078 sdk_version: "module_current",
2079 }
2080 `)
2081}
2082
2083func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
2084 testJava(t, `
2085 java_sdk_library {
2086 name: "foo",
2087 srcs: ["a.java"],
2088 system: {
2089 enabled: true,
2090 },
2091 }
2092
2093 java_library {
2094 name: "baz",
2095 srcs: ["a.java"],
2096 libs: ["foo"],
2097 // foo does not have module-lib scope so it should fallback to system
2098 sdk_version: "module_current",
2099 }
2100 `)
2101}
2102
Jiyong Park932cdfe2020-05-28 00:19:53 +09002103func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
2104 ctx, _ := testJava(t, `
2105 java_sdk_library {
2106 name: "foo",
2107 srcs: ["a.java"],
2108 system: {
2109 enabled: true,
2110 },
2111 default_to_stubs: true,
2112 }
2113
2114 java_library {
2115 name: "baz",
2116 srcs: ["a.java"],
2117 libs: ["foo"],
2118 // does not have sdk_version set, should fallback to module,
2119 // which will then fallback to system because the module scope
2120 // is not enabled.
2121 }
2122 `)
2123 // The baz library should depend on the system stubs jar.
2124 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
2125 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2126 t.Errorf("expected %q, found %#q", expected, actual)
2127 }
2128}
2129
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002130var compilerFlagsTestCases = []struct {
2131 in string
2132 out bool
2133}{
2134 {
2135 in: "a",
2136 out: false,
2137 },
2138 {
2139 in: "-a",
2140 out: true,
2141 },
2142 {
2143 in: "-no-jdk",
2144 out: false,
2145 },
2146 {
2147 in: "-no-stdlib",
2148 out: false,
2149 },
2150 {
2151 in: "-kotlin-home",
2152 out: false,
2153 },
2154 {
2155 in: "-kotlin-home /some/path",
2156 out: false,
2157 },
2158 {
2159 in: "-include-runtime",
2160 out: false,
2161 },
2162 {
2163 in: "-Xintellij-plugin-root",
2164 out: false,
2165 },
2166}
2167
2168type mockContext struct {
2169 android.ModuleContext
2170 result bool
2171}
2172
2173func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2174 // CheckBadCompilerFlags calls this function when the flag should be rejected
2175 ctx.result = false
2176}
2177
2178func TestCompilerFlags(t *testing.T) {
2179 for _, testCase := range compilerFlagsTestCases {
2180 ctx := &mockContext{result: true}
2181 CheckKotlincFlags(ctx, []string{testCase.in})
2182 if ctx.result != testCase.out {
2183 t.Errorf("incorrect output:")
2184 t.Errorf(" input: %#v", testCase.in)
2185 t.Errorf(" expected: %#v", testCase.out)
2186 t.Errorf(" got: %#v", ctx.result)
2187 }
2188 }
2189}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002190
2191// TODO(jungjw): Consider making this more robust by ignoring path order.
2192func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2193 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2194 flags := strings.Split(variables["javacFlags"], " ")
2195 got := ""
2196 for _, flag := range flags {
2197 keyEnd := strings.Index(flag, "=")
2198 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2199 got = flag[keyEnd+1:]
2200 break
2201 }
2202 }
2203 if expected != got {
2204 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2205 }
2206}
2207
2208func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002209 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002210 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002211 bp := `
2212 java_library {
2213 name: "foo",
2214 srcs: ["a.java"],
2215 java_version: "1.8",
2216 }
2217
2218 java_library {
2219 name: "bar",
2220 srcs: ["b.java"],
2221 sdk_version: "none",
2222 system_modules: "none",
2223 patch_module: "java.base",
2224 java_version: "1.8",
2225 }
2226
2227 java_library {
2228 name: "baz",
2229 srcs: ["c.java"],
2230 patch_module: "java.base",
2231 java_version: "1.8",
2232 }
2233 `
2234 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002235
2236 checkPatchModuleFlag(t, ctx, "foo", "")
2237 checkPatchModuleFlag(t, ctx, "bar", "")
2238 checkPatchModuleFlag(t, ctx, "baz", "")
2239 })
2240
Pete Gillin0c2143e2019-05-02 15:32:11 +01002241 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002242 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002243 bp := `
2244 java_library {
2245 name: "foo",
2246 srcs: ["a.java"],
2247 }
2248
2249 java_library {
2250 name: "bar",
2251 srcs: ["b.java"],
2252 sdk_version: "none",
2253 system_modules: "none",
2254 patch_module: "java.base",
2255 }
2256
2257 java_library {
2258 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002259 srcs: [
2260 "c.java",
2261 // Tests for b/150878007
2262 "dir/d.java",
2263 "dir2/e.java",
2264 "dir2/f.java",
2265 "nested/dir/g.java"
2266 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002267 patch_module: "java.base",
2268 }
2269 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002270 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002271
2272 checkPatchModuleFlag(t, ctx, "foo", "")
2273 expected := "java.base=.:" + buildDir
2274 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002275 expected = "java.base=" + strings.Join([]string{
2276 ".", buildDir, "dir", "dir2", "nested", moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002277 checkPatchModuleFlag(t, ctx, "baz", expected)
2278 })
2279}
Paul Duffina7b9f422020-01-10 17:12:18 +00002280
2281func TestJavaSystemModules(t *testing.T) {
2282 ctx, _ := testJava(t, `
2283 java_system_modules {
2284 name: "system-modules",
2285 libs: ["system-module1", "system-module2"],
2286 }
2287 java_library {
2288 name: "system-module1",
2289 srcs: ["a.java"],
2290 sdk_version: "none",
2291 system_modules: "none",
2292 }
2293 java_library {
2294 name: "system-module2",
2295 srcs: ["b.java"],
2296 sdk_version: "none",
2297 system_modules: "none",
2298 }
2299 `)
2300
2301 // check the existence of the module
2302 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2303
2304 cmd := systemModules.Rule("jarsTosystemModules")
2305
2306 // make sure the command compiles against the supplied modules.
2307 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2308 if !strings.Contains(cmd.Args["classpath"], module) {
2309 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2310 module)
2311 }
2312 }
2313}
Paul Duffin90169ba2020-01-10 17:16:44 +00002314
2315func TestJavaSystemModulesImport(t *testing.T) {
2316 ctx, _ := testJava(t, `
2317 java_system_modules_import {
2318 name: "system-modules",
2319 libs: ["system-module1", "system-module2"],
2320 }
2321 java_import {
2322 name: "system-module1",
2323 jars: ["a.jar"],
2324 }
2325 java_import {
2326 name: "system-module2",
2327 jars: ["b.jar"],
2328 }
2329 `)
2330
2331 // check the existence of the module
2332 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2333
2334 cmd := systemModules.Rule("jarsTosystemModules")
2335
2336 // make sure the command compiles against the supplied modules.
2337 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2338 if !strings.Contains(cmd.Args["classpath"], module) {
2339 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2340 module)
2341 }
2342 }
2343}
Paul Duffin83a2d962019-11-19 19:44:10 +00002344
2345func TestJavaLibraryWithSystemModules(t *testing.T) {
2346 ctx, _ := testJava(t, `
2347 java_library {
2348 name: "lib-with-source-system-modules",
2349 srcs: [
2350 "a.java",
2351 ],
2352 sdk_version: "none",
2353 system_modules: "source-system-modules",
2354 }
2355
2356 java_library {
2357 name: "source-jar",
2358 srcs: [
2359 "a.java",
2360 ],
2361 }
2362
2363 java_system_modules {
2364 name: "source-system-modules",
2365 libs: ["source-jar"],
2366 }
2367
2368 java_library {
2369 name: "lib-with-prebuilt-system-modules",
2370 srcs: [
2371 "a.java",
2372 ],
2373 sdk_version: "none",
2374 system_modules: "prebuilt-system-modules",
2375 }
2376
2377 java_import {
2378 name: "prebuilt-jar",
2379 jars: ["a.jar"],
2380 }
2381
2382 java_system_modules_import {
2383 name: "prebuilt-system-modules",
2384 libs: ["prebuilt-jar"],
2385 }
2386 `)
2387
2388 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2389
2390 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2391}
2392
2393func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2394 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2395 bootClasspath := javacRule.Args["bootClasspath"]
2396 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2397 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2398 }
2399}
Jiyong Park19604de2020-03-24 16:44:11 +09002400
2401func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2402 ctx, _ := testJava(t, `
2403 java_library {
2404 name: "foo",
2405 srcs: ["aidl/foo/IFoo.aidl"],
2406 libs: ["bar"],
2407 }
2408
2409 java_import {
2410 name: "bar",
2411 jars: ["a.jar"],
2412 aidl: {
2413 export_include_dirs: ["aidl/bar"],
2414 },
2415 }
2416 `)
2417
2418 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2419 expectedAidlFlag := "-Iaidl/bar"
2420 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2421 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2422 }
2423}
Liz Kammerdd849a82020-06-12 16:38:45 -07002424
2425func TestDataNativeBinaries(t *testing.T) {
2426 ctx, config := testJava(t, `
2427 java_test_host {
2428 name: "foo",
2429 srcs: ["a.java"],
2430 data_native_bins: ["bin"]
2431 }
2432
2433 python_binary_host {
2434 name: "bin",
2435 srcs: ["bin.py"],
2436 }
2437 `)
2438
2439 buildOS := android.BuildOs.String()
2440
2441 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2442 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
2443 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2444 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2445 if !reflect.DeepEqual(expected, actual) {
2446 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2447 }
2448}