blob: a2466f9951cf2f7a8eedb3c8e5382f0605ddcbf6 [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)
Jaewoong Jungf9b44652020-12-21 12:29:12 -080078 RegisterAppImportBuildComponents(ctx)
79 RegisterAppSetBuildComponents(ctx)
Paul Duffinf9b1da02019-12-18 19:51:55 +000080 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000081 RegisterGenRuleBuildComponents(ctx)
Jaewoong Jungf9b44652020-12-21 12:29:12 -080082 RegisterRuntimeResourceOverlayBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000083 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080084 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080085 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070086 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000087 RegisterDocsBuildComponents(ctx)
88 RegisterStubsBuildComponents(ctx)
Anton Hanssondff2c782020-12-21 17:10:01 +000089 RegisterPrebuiltApisBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000090 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070091 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010092 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000093
Liz Kammerdd849a82020-06-12 16:38:45 -070094 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070095 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross06fa5882020-10-29 18:21:38 -070096 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(ctx.Context, OverlaySingletonFactory))
97 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(ctx.Context, sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070098
Paul Duffin021f4e52020-07-30 16:04:17 +010099 android.RegisterPrebuiltMutators(ctx)
100
Colin Crossa4f08812018-10-02 22:03:40 -0700101 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000102 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700103
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000104 dexpreopt.RegisterToolModulesForTest(ctx)
105
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700106 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
107 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
108 })
109
Colin Cross527012a2017-11-30 22:56:16 -0800110 return ctx
111}
112
113func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800114 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800115
Colin Cross98be1bb2019-12-13 20:41:13 -0800116 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000117 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800118
Colin Crossae8600b2020-10-29 17:09:13 -0700119 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100120 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800121 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700122 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800123 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800124}
125
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900126func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900127 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800128 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900129}
130
Colin Cross98be1bb2019-12-13 20:41:13 -0800131func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900132 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700133 ctx := testContext(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900134
Colin Cross98be1bb2019-12-13 20:41:13 -0800135 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000136 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900137
Colin Crossae8600b2020-10-29 17:09:13 -0700138 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900139 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
140 if len(errs) > 0 {
141 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900142 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900143 }
144 _, errs = ctx.PrepareBuildActions(config)
145 if len(errs) > 0 {
146 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900147 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900148 }
149
150 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900151
152 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900153}
154
Colin Cross238c1f32020-06-07 16:58:18 -0700155func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
156 t.Helper()
157 return testJavaWithConfig(t, testConfig(nil, bp, fs))
158}
159
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700160func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800161 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700162 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900163}
164
Colin Cross98be1bb2019-12-13 20:41:13 -0800165func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900166 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700167 ctx := testContext(config)
Colin Cross527012a2017-11-30 22:56:16 -0800168 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700169
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700170 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700171}
172
Colin Cross2acdae82017-09-15 19:44:24 -0700173func moduleToPath(name string) string {
174 switch {
175 case name == `""`:
176 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700177 case strings.HasSuffix(name, ".jar"):
178 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700179 default:
180 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700181 }
182}
183
Jeongik Chae403e9e2019-12-07 00:16:24 +0900184func TestJavaLinkType(t *testing.T) {
185 testJava(t, `
186 java_library {
187 name: "foo",
188 srcs: ["a.java"],
189 libs: ["bar"],
190 static_libs: ["baz"],
191 }
192
193 java_library {
194 name: "bar",
195 sdk_version: "current",
196 srcs: ["b.java"],
197 }
198
199 java_library {
200 name: "baz",
201 sdk_version: "system_current",
202 srcs: ["c.java"],
203 }
204 `)
205
Steven Moreland00298982020-11-17 21:44:36 +0000206 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900207 java_library {
208 name: "foo",
209 srcs: ["a.java"],
210 libs: ["bar"],
211 sdk_version: "current",
212 static_libs: ["baz"],
213 }
214
215 java_library {
216 name: "bar",
217 sdk_version: "current",
218 srcs: ["b.java"],
219 }
220
221 java_library {
222 name: "baz",
223 sdk_version: "system_current",
224 srcs: ["c.java"],
225 }
226 `)
227
228 testJava(t, `
229 java_library {
230 name: "foo",
231 srcs: ["a.java"],
232 libs: ["bar"],
233 sdk_version: "system_current",
234 static_libs: ["baz"],
235 }
236
237 java_library {
238 name: "bar",
239 sdk_version: "current",
240 srcs: ["b.java"],
241 }
242
243 java_library {
244 name: "baz",
245 sdk_version: "system_current",
246 srcs: ["c.java"],
247 }
248 `)
249
Steven Moreland00298982020-11-17 21:44:36 +0000250 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900251 java_library {
252 name: "foo",
253 srcs: ["a.java"],
254 libs: ["bar"],
255 sdk_version: "system_current",
256 static_libs: ["baz"],
257 }
258
259 java_library {
260 name: "bar",
261 sdk_version: "current",
262 srcs: ["b.java"],
263 }
264
265 java_library {
266 name: "baz",
267 srcs: ["c.java"],
268 }
269 `)
270}
271
Colin Cross72bb3632017-07-13 16:23:21 -0700272func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700273 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700274 java_library {
275 name: "foo",
276 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700277 libs: ["bar"],
278 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700279 }
280
281 java_library {
282 name: "bar",
283 srcs: ["b.java"],
284 }
285
286 java_library {
287 name: "baz",
288 srcs: ["c.java"],
289 }
Colin Crossd5934c82017-10-02 13:55:26 -0700290 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700291
Colin Cross4c428df2017-09-15 17:36:05 -0700292 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700293 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700294
295 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
296 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
297 }
298
Colin Cross1ee23172017-10-18 14:44:18 -0700299 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700300 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
301 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700302
Nan Zhanged19fc32017-10-19 13:06:22 -0700303 if !strings.Contains(javac.Args["classpath"], barTurbine) {
304 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700305 }
306
Nan Zhanged19fc32017-10-19 13:06:22 -0700307 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
308 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700309 }
310
311 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
312 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700313 }
314}
315
Artur Satayev9cf46692019-11-26 18:08:34 +0000316func TestExportedPlugins(t *testing.T) {
317 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800318 library string
319 processors string
320 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000321 }
322 var tests = []struct {
323 name string
324 extra string
325 results []Result
326 }{
327 {
328 name: "Exported plugin is not a direct plugin",
329 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
330 results: []Result{{library: "exports", processors: "-proc:none"}},
331 },
332 {
333 name: "Exports plugin to dependee",
334 extra: `
335 java_library{name: "exports", exported_plugins: ["plugin"]}
336 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
337 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
338 `,
339 results: []Result{
340 {library: "foo", processors: "-processor com.android.TestPlugin"},
341 {library: "bar", processors: "-processor com.android.TestPlugin"},
342 },
343 },
344 {
345 name: "Exports plugin to android_library",
346 extra: `
347 java_library{name: "exports", exported_plugins: ["plugin"]}
348 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
349 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
350 `,
351 results: []Result{
352 {library: "foo", processors: "-processor com.android.TestPlugin"},
353 {library: "bar", processors: "-processor com.android.TestPlugin"},
354 },
355 },
356 {
357 name: "Exports plugin is not propagated via transitive deps",
358 extra: `
359 java_library{name: "exports", exported_plugins: ["plugin"]}
360 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
361 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
362 `,
363 results: []Result{
364 {library: "foo", processors: "-processor com.android.TestPlugin"},
365 {library: "bar", processors: "-proc:none"},
366 },
367 },
368 {
369 name: "Exports plugin appends to plugins",
370 extra: `
371 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
372 java_library{name: "exports", exported_plugins: ["plugin"]}
373 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
374 `,
375 results: []Result{
376 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
377 },
378 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800379 {
380 name: "Exports plugin to with generates_api to dependee",
381 extra: `
382 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
383 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
384 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
385 `,
386 results: []Result{
387 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
388 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
389 },
390 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000391 }
392
393 for _, test := range tests {
394 t.Run(test.name, func(t *testing.T) {
395 ctx, _ := testJava(t, `
396 java_plugin {
397 name: "plugin",
398 processor_class: "com.android.TestPlugin",
399 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800400 java_plugin {
401 name: "plugin_generates_api",
402 generates_api: true,
403 processor_class: "com.android.TestPlugin",
404 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000405 `+test.extra)
406
407 for _, want := range test.results {
408 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
409 if javac.Args["processor"] != want.processors {
410 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
411 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800412 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
413 disableTurbine := turbine.BuildParams.Rule == nil
414 if disableTurbine != want.disableTurbine {
415 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
416 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000417 }
418 })
419 }
420}
421
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900422func TestSdkVersionByPartition(t *testing.T) {
423 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900424 java_library {
425 name: "foo",
426 srcs: ["a.java"],
427 vendor: true,
428 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900429 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900430
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900431 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900432 java_library {
433 name: "bar",
434 srcs: ["b.java"],
435 }
436 `)
437
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900438 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900439 bp := `
440 java_library {
441 name: "foo",
442 srcs: ["a.java"],
443 product_specific: true,
444 }
445 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800446
447 config := testConfig(nil, bp, nil)
448 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900449 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800450 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 +0900451 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800452 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900453 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900454 }
455}
456
Colin Crossd5934c82017-10-02 13:55:26 -0700457func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700458 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700459 java_library {
460 name: "foo",
461 srcs: ["a.java"],
462 target: {
463 android: {
464 srcs: ["b.java"],
465 },
466 },
467 }
468 `)
469
470 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
471 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
472 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
473 }
474}
475
Colin Cross6b4a32d2017-12-05 13:42:45 -0800476func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700477 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800478 java_library_host {
479 name: "foo",
480 srcs: ["a.java"],
481 }
482
483 java_binary_host {
484 name: "bar",
485 srcs: ["b.java"],
486 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700487 jni_libs: ["libjni"],
488 }
489
490 cc_library_shared {
491 name: "libjni",
492 host_supported: true,
493 device_supported: false,
494 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800495 }
496 `)
497
498 buildOS := android.BuildOs.String()
499
500 bar := ctx.ModuleForTests("bar", buildOS+"_common")
501 barJar := bar.Output("bar.jar").Output.String()
502 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
503 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
504
Colin Cross89226d92020-10-09 19:00:54 -0700505 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
506 libjniSO := libjni.Rule("Cp").Output.String()
507
Colin Cross6b4a32d2017-12-05 13:42:45 -0800508 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700509 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
510 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800511 }
Colin Cross89226d92020-10-09 19:00:54 -0700512
513 // Test that the install binary wrapper depends on the installed JNI libraries
514 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
515 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800516 }
Alex Humesky2070e322020-06-09 20:23:08 -0400517}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800518
Alex Humesky2070e322020-06-09 20:23:08 -0400519func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
520 bp := `
521 java_library {
522 name: "target_library",
523 srcs: ["a.java"],
524 }
525
526 java_binary_host {
527 name: "host_binary",
528 srcs: ["b.java"],
529 }
530 `
531 config := testConfig(nil, bp, nil)
532 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
533
534 ctx, _ := testJavaWithConfig(t, config)
535
Liz Kammer7941b302020-07-28 13:27:34 -0700536 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400537 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
538 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
539 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
540 t.Errorf("target library javac flags %v should contain "+
541 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
542 }
543
544 // check that -g is not overridden for host modules
545 buildOS := android.BuildOs.String()
546 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
547 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
548 if strings.Contains(hostJavaFlags, "-g:source,lines") {
549 t.Errorf("java_binary_host javac flags %v should not have "+
550 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
551 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800552}
553
Colin Cross72bb3632017-07-13 16:23:21 -0700554func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700555 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700556 java_library {
557 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000558 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000559 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700560 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700561 }
562
Colin Cross74d73e22017-08-02 11:05:49 -0700563 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700564 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700565 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700566 }
567
Colin Cross74d73e22017-08-02 11:05:49 -0700568 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700569 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700570 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700571 sdk_version: "current",
572 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700573 }
Colin Cross42be7612019-02-21 18:12:14 -0800574
575 dex_import {
576 name: "qux",
577 jars: ["b.jar"],
578 }
Colin Cross79c7c262019-04-17 11:11:46 -0700579
580 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000581 name: "sdklib",
582 public: {
583 jars: ["c.jar"],
584 },
585 }
586
Paul Duffin91547182019-11-12 19:39:36 +0000587 prebuilt_stubs_sources {
588 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000589 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000590 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000591
592 java_test_import {
593 name: "test",
594 jars: ["a.jar"],
595 test_suites: ["cts"],
596 test_config: "AndroidTest.xml",
597 }
Colin Cross72bb3632017-07-13 16:23:21 -0700598 `)
599
Paul Duffin9b478b02019-12-10 13:41:51 +0000600 fooModule := ctx.ModuleForTests("foo", "android_common")
601 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700602 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700603 barModule := ctx.ModuleForTests("bar", "android_common")
604 barJar := barModule.Rule("combineJar").Output
605 bazModule := ctx.ModuleForTests("baz", "android_common")
606 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700607 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700608
Paul Duffin9b478b02019-12-10 13:41:51 +0000609 fooLibrary := fooModule.Module().(*Library)
610 assertDeepEquals(t, "foo java sources incorrect",
611 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000612
Paul Duffin9b478b02019-12-10 13:41:51 +0000613 assertDeepEquals(t, "foo java source jars incorrect",
614 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
615 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000616
Colin Cross37f6d792018-07-12 12:28:41 -0700617 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
618 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700619 }
620
Liz Kammerd6c31d22020-08-05 15:40:41 -0700621 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
622 if barDexJar != nil {
623 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
624 }
625
Colin Cross79c7c262019-04-17 11:11:46 -0700626 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
627 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
628 }
629
Colin Cross37f6d792018-07-12 12:28:41 -0700630 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
631 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700632 }
Colin Cross42be7612019-02-21 18:12:14 -0800633
Liz Kammerd6c31d22020-08-05 15:40:41 -0700634 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
635 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
636 if bazDexJar != expectedDexJar {
637 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
638 }
639
Colin Cross42be7612019-02-21 18:12:14 -0800640 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700641}
642
Paul Duffin9b478b02019-12-10 13:41:51 +0000643func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
644 if !reflect.DeepEqual(expected, actual) {
645 t.Errorf("%s: expected %q, found %q", message, expected, actual)
646 }
647}
648
Paul Duffin1a393322020-11-18 16:36:47 +0000649func TestPrebuiltStubsSources(t *testing.T) {
650 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
651 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
652prebuilt_stubs_sources {
653 name: "stubs-source",
654 srcs: ["%s"],
655}`, sourcesPath), map[string][]byte{
656 "stubs/sources/pkg/A.java": nil,
657 "stubs/sources/pkg/B.java": nil,
658 })
659
660 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
661 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
662 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
663 }
664 }
665
666 t.Run("empty/missing directory", func(t *testing.T) {
667 test(t, "empty-directory", []string{})
668 })
669
670 t.Run("non-empty set of sources", func(t *testing.T) {
671 test(t, "stubs/sources", []string{
672 "stubs/sources/pkg/A.java",
673 "stubs/sources/pkg/B.java",
674 })
675 })
676}
677
Paul Duffin56d44902020-01-31 13:36:25 +0000678func TestJavaSdkLibraryImport(t *testing.T) {
679 ctx, _ := testJava(t, `
680 java_library {
681 name: "foo",
682 srcs: ["a.java"],
683 libs: ["sdklib"],
684 sdk_version: "current",
685 }
686
687 java_library {
688 name: "foo.system",
689 srcs: ["a.java"],
690 libs: ["sdklib"],
691 sdk_version: "system_current",
692 }
693
694 java_library {
695 name: "foo.test",
696 srcs: ["a.java"],
697 libs: ["sdklib"],
698 sdk_version: "test_current",
699 }
700
701 java_sdk_library_import {
702 name: "sdklib",
703 public: {
704 jars: ["a.jar"],
705 },
706 system: {
707 jars: ["b.jar"],
708 },
709 test: {
710 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100711 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000712 },
713 }
714 `)
715
716 for _, scope := range []string{"", ".system", ".test"} {
717 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
718 javac := fooModule.Rule("javac")
719
720 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
721 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
722 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
723 }
724 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100725
Paul Duffincee7e662020-07-09 17:32:57 +0100726 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100727 `prebuilt_sdklib.stubs`,
728 `prebuilt_sdklib.stubs.source.test`,
729 `prebuilt_sdklib.stubs.system`,
730 `prebuilt_sdklib.stubs.test`,
731 })
732}
733
734func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
735 ctx, _ := testJava(t, `
736 java_sdk_library {
737 name: "sdklib",
738 srcs: ["a.java"],
739 sdk_version: "none",
740 system_modules: "none",
741 public: {
742 enabled: true,
743 },
744 }
745
746 java_sdk_library_import {
747 name: "sdklib",
748 public: {
749 jars: ["a.jar"],
750 },
751 }
752 `)
753
Paul Duffincee7e662020-07-09 17:32:57 +0100754 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100755 `dex2oatd`,
756 `prebuilt_sdklib`,
757 `sdklib.impl`,
758 `sdklib.stubs`,
759 `sdklib.stubs.source`,
760 `sdklib.xml`,
761 })
762
Paul Duffincee7e662020-07-09 17:32:57 +0100763 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100764 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100765 `sdklib.impl`,
766 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
767 // dependency is added after prebuilts may have been renamed and so has to use
768 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100769 `sdklib.xml`,
770 })
771}
772
773func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
774 ctx, _ := testJava(t, `
775 java_sdk_library {
776 name: "sdklib",
777 srcs: ["a.java"],
778 sdk_version: "none",
779 system_modules: "none",
780 public: {
781 enabled: true,
782 },
783 }
784
785 java_sdk_library_import {
786 name: "sdklib",
787 prefer: true,
788 public: {
789 jars: ["a.jar"],
790 },
791 }
792 `)
793
Paul Duffincee7e662020-07-09 17:32:57 +0100794 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100795 `dex2oatd`,
796 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100797 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100798 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100799 `sdklib.stubs.source`,
800 `sdklib.xml`,
801 })
802
Paul Duffincee7e662020-07-09 17:32:57 +0100803 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100804 `prebuilt_sdklib.stubs`,
805 `sdklib.impl`,
806 `sdklib.xml`,
807 })
Paul Duffin56d44902020-01-31 13:36:25 +0000808}
809
JaeMan Parkff715562020-10-19 17:25:58 +0900810func TestJavaSdkLibraryEnforce(t *testing.T) {
811 partitionToBpOption := func(partition string) string {
812 switch partition {
813 case "system":
814 return ""
815 case "vendor":
816 return "soc_specific: true,"
817 case "product":
818 return "product_specific: true,"
819 default:
820 panic("Invalid partition group name: " + partition)
821 }
822 }
823
824 type testConfigInfo struct {
825 libraryType string
826 fromPartition string
827 toPartition string
828 enforceVendorInterface bool
829 enforceProductInterface bool
830 enforceJavaSdkLibraryCheck bool
831 allowList []string
832 }
833
834 createTestConfig := func(info testConfigInfo) android.Config {
835 bpFileTemplate := `
836 java_library {
837 name: "foo",
838 srcs: ["foo.java"],
839 libs: ["bar"],
840 sdk_version: "current",
841 %s
842 }
843
844 %s {
845 name: "bar",
846 srcs: ["bar.java"],
847 sdk_version: "current",
848 %s
849 }
850 `
851
852 bpFile := fmt.Sprintf(bpFileTemplate,
853 partitionToBpOption(info.fromPartition),
854 info.libraryType,
855 partitionToBpOption(info.toPartition))
856
857 config := testConfig(nil, bpFile, nil)
858 configVariables := config.TestProductVariables
859
860 configVariables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
861 if info.enforceVendorInterface {
862 configVariables.DeviceVndkVersion = proptools.StringPtr("current")
863 }
864 configVariables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
865 configVariables.InterPartitionJavaLibraryAllowList = info.allowList
866
867 return config
868 }
869
870 isValidDependency := func(configInfo testConfigInfo) bool {
871 if configInfo.enforceVendorInterface == false {
872 return true
873 }
874
875 if configInfo.enforceJavaSdkLibraryCheck == false {
876 return true
877 }
878
879 if inList("bar", configInfo.allowList) {
880 return true
881 }
882
883 if configInfo.libraryType == "java_library" {
884 if configInfo.fromPartition != configInfo.toPartition {
885 if !configInfo.enforceProductInterface &&
886 ((configInfo.fromPartition == "system" && configInfo.toPartition == "product") ||
887 (configInfo.fromPartition == "product" && configInfo.toPartition == "system")) {
888 return true
889 }
890 return false
891 }
892 }
893
894 return true
895 }
896
897 errorMessage := "is not allowed across the partitions"
898
899 allPartitionCombinations := func() [][2]string {
900 var result [][2]string
901 partitions := []string{"system", "vendor", "product"}
902
903 for _, fromPartition := range partitions {
904 for _, toPartition := range partitions {
905 result = append(result, [2]string{fromPartition, toPartition})
906 }
907 }
908
909 return result
910 }
911
912 allFlagCombinations := func() [][3]bool {
913 var result [][3]bool
914 flagValues := [2]bool{false, true}
915
916 for _, vendorInterface := range flagValues {
917 for _, productInterface := range flagValues {
918 for _, enableEnforce := range flagValues {
919 result = append(result, [3]bool{vendorInterface, productInterface, enableEnforce})
920 }
921 }
922 }
923
924 return result
925 }
926
927 for _, libraryType := range []string{"java_library", "java_sdk_library"} {
928 for _, partitionValues := range allPartitionCombinations() {
929 for _, flagValues := range allFlagCombinations() {
930 testInfo := testConfigInfo{
931 libraryType: libraryType,
932 fromPartition: partitionValues[0],
933 toPartition: partitionValues[1],
934 enforceVendorInterface: flagValues[0],
935 enforceProductInterface: flagValues[1],
936 enforceJavaSdkLibraryCheck: flagValues[2],
937 }
938
939 if isValidDependency(testInfo) {
940 testJavaWithConfig(t, createTestConfig(testInfo))
941 } else {
942 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testInfo))
943 }
944 }
945 }
946 }
947
948 testJavaWithConfig(t, createTestConfig(testConfigInfo{
949 libraryType: "java_library",
950 fromPartition: "vendor",
951 toPartition: "system",
952 enforceVendorInterface: true,
953 enforceProductInterface: true,
954 enforceJavaSdkLibraryCheck: true,
955 allowList: []string{"bar"},
956 }))
957
958 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
959 libraryType: "java_library",
960 fromPartition: "vendor",
961 toPartition: "system",
962 enforceVendorInterface: true,
963 enforceProductInterface: true,
964 enforceJavaSdkLibraryCheck: true,
965 allowList: []string{"foo"},
966 }))
967}
968
Colin Cross89536d42017-07-07 14:35:50 -0700969func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700970 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700971 java_defaults {
972 name: "defaults",
973 srcs: ["a.java"],
974 libs: ["bar"],
975 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700976 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700977 }
978
979 java_library {
980 name: "foo",
981 defaults: ["defaults"],
982 }
983
984 java_library {
985 name: "bar",
986 srcs: ["b.java"],
987 }
988
989 java_library {
990 name: "baz",
991 srcs: ["c.java"],
992 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700993
994 android_test {
995 name: "atestOptimize",
996 defaults: ["defaults"],
997 optimize: {enabled: true},
998 }
999
1000 android_test {
1001 name: "atestNoOptimize",
1002 defaults: ["defaults"],
1003 }
1004
1005 android_test {
1006 name: "atestDefault",
1007 srcs: ["a.java"],
1008 }
Colin Cross89536d42017-07-07 14:35:50 -07001009 `)
1010
Colin Cross4c428df2017-09-15 17:36:05 -07001011 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -07001012 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -07001013
1014 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
1015 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
1016 }
1017
Nan Zhanged19fc32017-10-19 13:06:22 -07001018 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1019 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1020 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -07001021 }
1022
Colin Cross1ee23172017-10-18 14:44:18 -07001023 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -07001024 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
1025 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -07001026 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001027
1028 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1029 if atestOptimize.Output == nil {
1030 t.Errorf("atestOptimize should optimize APK")
1031 }
1032
1033 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1034 if atestNoOptimize.Output == nil {
1035 t.Errorf("atestNoOptimize should not optimize APK")
1036 }
1037
1038 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1039 if atestDefault.Output == nil {
1040 t.Errorf("atestDefault should optimize APK")
1041 }
Colin Cross89536d42017-07-07 14:35:50 -07001042}
1043
Colin Cross0f37af02017-09-27 17:42:05 -07001044func TestResources(t *testing.T) {
1045 var table = []struct {
1046 name string
1047 prop string
1048 extra string
1049 args string
1050 }{
1051 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001052 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001053 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001054 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001055 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001056 },
1057 {
1058 // Test that a module with java_resources includes the files
1059 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001060 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1061 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001062 },
1063 {
1064 // Test that a module with a filegroup in java_resources includes the files with the
1065 // path prefix
1066 name: "resource filegroup",
1067 prop: `java_resources: [":foo-res"]`,
1068 extra: `
1069 filegroup {
1070 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001071 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001072 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001073 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001074 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001075 },
1076 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001077 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1078 name: "wildcard dirs",
1079 prop: `java_resource_dirs: ["java-res/*"]`,
1080 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1081 },
1082 {
1083 // Test that a module exclude_java_resource_dirs excludes the files
1084 name: "wildcard dirs",
1085 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1086 args: "-C java-res/a -f java-res/a/a",
1087 },
Colin Crosscedd4762018-09-13 11:26:19 -07001088 {
1089 // Test wildcards in java_resources
1090 name: "wildcard files",
1091 prop: `java_resources: ["java-res/**/*"]`,
1092 args: "-C . -f java-res/a/a -f java-res/b/b",
1093 },
1094 {
1095 // Test exclude_java_resources with java_resources
1096 name: "wildcard files with exclude",
1097 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1098 args: "-C . -f java-res/a/a",
1099 },
1100 {
1101 // Test exclude_java_resources with java_resource_dirs
1102 name: "resource dirs with exclude files",
1103 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1104 args: "-C java-res -f java-res/a/a",
1105 },
1106 {
1107 // Test exclude_java_resource_dirs with java_resource_dirs
1108 name: "resource dirs with exclude files",
1109 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1110 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1111 },
Colin Cross0f37af02017-09-27 17:42:05 -07001112 }
1113
1114 for _, test := range table {
1115 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001116 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001117 java_library {
1118 name: "foo",
1119 srcs: [
1120 "a.java",
1121 "b.java",
1122 "c.java",
1123 ],
1124 `+test.prop+`,
1125 }
Colin Cross238c1f32020-06-07 16:58:18 -07001126 `+test.extra,
1127 map[string][]byte{
1128 "java-res/a/a": nil,
1129 "java-res/b/b": nil,
1130 "java-res2/a": nil,
1131 },
1132 )
Colin Cross0f37af02017-09-27 17:42:05 -07001133
Colin Cross331a1212018-08-15 20:40:52 -07001134 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001135 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001136
1137 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1138 t.Errorf("foo combined jars %v does not contain %q",
1139 foo.Inputs.Strings(), fooRes.Output.String())
1140 }
1141
Colin Crossaf9c55b2017-10-03 14:50:08 -07001142 if fooRes.Args["jarArgs"] != test.args {
1143 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001144 fooRes.Args["jarArgs"], test.args)
1145 }
1146 })
1147 }
1148}
1149
Colin Cross0c4ce212019-05-03 15:28:19 -07001150func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001151 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001152 java_library {
1153 name: "foo",
1154 srcs: [
1155 "a.java",
1156 "b.java",
1157 "c.java",
1158 ],
1159 include_srcs: true,
1160 }
1161
1162 java_library {
1163 name: "bar",
1164 srcs: [
1165 "a.java",
1166 "b.java",
1167 "c.java",
1168 ],
1169 java_resource_dirs: ["java-res"],
1170 include_srcs: true,
1171 }
Colin Cross238c1f32020-06-07 16:58:18 -07001172 `, map[string][]byte{
1173 "java-res/a/a": nil,
1174 "java-res/b/b": nil,
1175 "java-res2/a": nil,
1176 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001177
1178 // Test a library with include_srcs: true
1179 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1180 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1181
1182 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1183 t.Errorf("foo combined jars %v does not contain %q", w, g)
1184 }
1185
1186 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1187 t.Errorf("foo source jar args %q is not %q", w, g)
1188 }
1189
1190 // Test a library with include_srcs: true and resources
1191 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1192 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1193 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1194 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1195
1196 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1197 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1198 }
1199
1200 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1201 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1202 }
1203
1204 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1205 t.Errorf("bar combined jars %v does not contain %q", w, g)
1206 }
1207
1208 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1209 t.Errorf("bar source jar args %q is not %q", w, g)
1210 }
1211
1212 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1213 t.Errorf("bar resource jar args %q is not %q", w, g)
1214 }
1215}
1216
Colin Cross54190b32017-10-09 15:34:10 -07001217func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001218 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001219 java_library {
1220 name: "foo",
1221 srcs: [
1222 "a*.java",
1223 ":gen",
1224 "b*.java",
1225 ],
1226 }
1227
1228 genrule {
1229 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001230 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001231 out: ["gen.java"],
1232 }
Colin Cross238c1f32020-06-07 16:58:18 -07001233 `, map[string][]byte{
1234 "a.java": nil,
1235 "b.java": nil,
1236 })
Colin Cross54190b32017-10-09 15:34:10 -07001237
1238 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1239 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1240
Colin Cross15e86d92017-10-20 15:07:08 -07001241 if filepath.Base(genrule.Output.String()) != "gen.java" {
1242 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001243 }
1244
1245 if len(javac.Inputs) != 3 ||
1246 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001247 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001248 javac.Inputs[2].String() != "b.java" {
1249 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1250 }
1251}
1252
Nan Zhang61eaedb2017-11-02 13:28:15 -07001253func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001254 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001255 java_library {
1256 name: "foo",
1257 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001258 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001259 }
1260
1261 java_library {
1262 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001263 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001264 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001265 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001266 }
1267
1268 java_library {
1269 name: "baz",
1270 srcs: ["c.java"],
1271 libs: ["bar"],
1272 sdk_version: "14",
1273 }
1274 `)
1275
1276 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1277 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1278 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1279 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1280 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1281
1282 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1283 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1284 }
1285
1286 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1287 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1288 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1289 }
1290 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1291 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1292 }
1293 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1294 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1295 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001296 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001297 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001298 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001299 }
1300}
1301
1302func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001303 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001304 java_library {
1305 name: "bar",
1306 srcs: ["a.java","b.java","c.java"],
1307 javac_shard_size: 1
1308 }
1309 `)
1310
1311 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1312 for i := 0; i < 3; i++ {
1313 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1314 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1315 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1316 }
1317 }
1318}
1319
Nan Zhang581fd212018-01-10 16:06:12 -08001320func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001321 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001322 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001323 name: "droiddoc-templates-sdk",
1324 path: ".",
1325 }
Jiyong Park29074592019-07-07 16:27:47 +09001326 filegroup {
1327 name: "bar-doc-aidl-srcs",
1328 srcs: ["bar-doc/IBar.aidl"],
1329 path: "bar-doc",
1330 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001331 droidstubs {
1332 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001333 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001334 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001335 ],
Steve Kim3666c702020-09-01 17:58:01 +00001336 exclude_srcs: [
1337 "bar-doc/b.java"
1338 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001339 api_levels_annotations_dirs: [
1340 "droiddoc-templates-sdk",
1341 ],
1342 api_levels_annotations_enabled: true,
1343 }
1344 droiddoc {
1345 name: "bar-doc",
1346 srcs: [
1347 ":bar-stubs",
1348 "bar-doc/IFoo.aidl",
1349 ":bar-doc-aidl-srcs",
1350 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001351 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001352 hdf: [
1353 "android.whichdoc offline",
1354 ],
1355 knowntags: [
1356 "bar-doc/known_oj_tags.txt",
1357 ],
1358 proofread_file: "libcore-proofread.txt",
1359 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001360 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001361 }
Colin Cross238c1f32020-06-07 16:58:18 -07001362 `,
1363 map[string][]byte{
1364 "bar-doc/a.java": nil,
1365 "bar-doc/b.java": nil,
1366 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001367 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1368 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1369 if err != nil {
1370 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1371 }
1372 if len(barStubsOutputs) != 1 {
1373 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001374 }
Nan Zhang581fd212018-01-10 16:06:12 -08001375
Liz Kammere1ab2502020-09-10 15:29:25 +00001376 barStubsOutput := barStubsOutputs[0]
1377 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1378 javaDoc := barDoc.Rule("javadoc")
1379 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1380 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001381 }
1382
Liz Kammere1ab2502020-09-10 15:29:25 +00001383 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1384 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1385 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1386 }
1387
1388 aidl := barDoc.Rule("aidl")
1389 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001390 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1391 }
1392
1393 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1394 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001395 }
Nan Zhang581fd212018-01-10 16:06:12 -08001396}
1397
Liz Kammer585cac22020-07-06 09:12:57 -07001398func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1399 testJavaError(t, "flags is set. Cannot set args", `
1400 droiddoc_exported_dir {
1401 name: "droiddoc-templates-sdk",
1402 path: ".",
1403 }
1404 filegroup {
1405 name: "bar-doc-aidl-srcs",
1406 srcs: ["bar-doc/IBar.aidl"],
1407 path: "bar-doc",
1408 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001409 droidstubs {
1410 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001411 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001412 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001413 ],
Steve Kim3666c702020-09-01 17:58:01 +00001414 exclude_srcs: [
1415 "bar-doc/b.java"
1416 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001417 api_levels_annotations_dirs: [
1418 "droiddoc-templates-sdk",
1419 ],
1420 api_levels_annotations_enabled: true,
1421 }
1422 droiddoc {
1423 name: "bar-doc",
1424 srcs: [
1425 ":bar-stubs",
1426 "bar-doc/IFoo.aidl",
1427 ":bar-doc-aidl-srcs",
1428 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001429 custom_template: "droiddoc-templates-sdk",
1430 hdf: [
1431 "android.whichdoc offline",
1432 ],
1433 knowntags: [
1434 "bar-doc/known_oj_tags.txt",
1435 ],
1436 proofread_file: "libcore-proofread.txt",
1437 todo_file: "libcore-docs-todo.html",
1438 flags: ["-offlinemode -title \"libcore\""],
1439 args: "-offlinemode -title \"libcore\"",
1440 }
1441 `)
1442}
1443
Liz Kammer3d894b72020-08-04 09:55:13 -07001444func TestDroidstubs(t *testing.T) {
1445 ctx, _ := testJavaWithFS(t, `
1446 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001447 name: "droiddoc-templates-sdk",
1448 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001449 }
1450
1451 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001452 name: "bar-stubs",
1453 srcs: ["bar-doc/a.java"],
1454 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1455 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001456 }
1457
1458 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001459 name: "bar-stubs-other",
1460 srcs: ["bar-doc/a.java"],
1461 high_mem: true,
1462 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1463 api_levels_annotations_enabled: true,
1464 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001465 }
1466 `,
1467 map[string][]byte{
1468 "bar-doc/a.java": nil,
1469 })
1470 testcases := []struct {
1471 moduleName string
1472 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001473 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001474 }{
1475 {
1476 moduleName: "bar-stubs",
1477 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001478 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001479 },
1480 {
1481 moduleName: "bar-stubs-other",
1482 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001483 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001484 },
1485 }
1486 for _, c := range testcases {
1487 m := ctx.ModuleForTests(c.moduleName, "android_common")
1488 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001489 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001490 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001491 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001492 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1493 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001494
1495 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1496 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1497 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001498 }
1499}
1500
Paul Duffin83a2d962019-11-19 19:44:10 +00001501func TestDroidstubsWithSystemModules(t *testing.T) {
1502 ctx, _ := testJava(t, `
1503 droidstubs {
1504 name: "stubs-source-system-modules",
1505 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001506 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001507 ],
1508 sdk_version: "none",
1509 system_modules: "source-system-modules",
1510 }
1511
1512 java_library {
1513 name: "source-jar",
1514 srcs: [
1515 "a.java",
1516 ],
1517 }
1518
1519 java_system_modules {
1520 name: "source-system-modules",
1521 libs: ["source-jar"],
1522 }
1523
1524 droidstubs {
1525 name: "stubs-prebuilt-system-modules",
1526 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001527 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001528 ],
1529 sdk_version: "none",
1530 system_modules: "prebuilt-system-modules",
1531 }
1532
1533 java_import {
1534 name: "prebuilt-jar",
1535 jars: ["a.jar"],
1536 }
1537
1538 java_system_modules_import {
1539 name: "prebuilt-system-modules",
1540 libs: ["prebuilt-jar"],
1541 }
1542 `)
1543
1544 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1545
1546 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1547}
1548
1549func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1550 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1551 var systemJars []string
1552 for _, i := range metalavaRule.Implicits {
1553 systemJars = append(systemJars, i.Base())
1554 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001555 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001556 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1557 }
1558}
1559
Colin Cross54250902017-12-05 09:28:08 -08001560func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001561 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001562 java_library {
1563 name: "foo",
1564 srcs: ["a.java"],
1565 }
1566
1567 java_genrule {
1568 name: "jargen",
1569 tool_files: ["b.java"],
1570 cmd: "$(location b.java) $(in) $(out)",
1571 out: ["jargen.jar"],
1572 srcs: [":foo"],
1573 }
1574
1575 java_library {
1576 name: "bar",
1577 static_libs: ["jargen"],
1578 srcs: ["c.java"],
1579 }
1580
1581 java_library {
1582 name: "baz",
1583 libs: ["jargen"],
1584 srcs: ["c.java"],
1585 }
1586 `)
1587
1588 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1589 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1590 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1591 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1592 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1593
Colin Cross3d680512020-11-13 16:23:53 -08001594 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1595 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001596 }
1597
1598 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1599 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1600 }
1601
1602 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1603 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1604 }
1605
1606 if len(barCombined.Inputs) != 2 ||
1607 barCombined.Inputs[0].String() != bar.Output.String() ||
1608 barCombined.Inputs[1].String() != jargen.Output.String() {
1609 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1610 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1611 }
1612}
1613
Nan Zhang27e284d2018-02-09 21:03:53 +00001614func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001615 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001616 java_library {
1617 name: "foo",
1618 srcs: ["a.java", ":foo-srcs"],
1619 exclude_srcs: ["a.java", ":foo-excludes"],
1620 }
1621
1622 filegroup {
1623 name: "foo-srcs",
1624 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1625 }
1626
1627 filegroup {
1628 name: "foo-excludes",
1629 srcs: ["java-fg/a.java", "java-fg/b.java"],
1630 }
1631 `)
1632
1633 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1634
1635 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1636 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1637 }
1638}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001639
Paul Duffin52d398a2019-06-11 12:31:14 +01001640func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001641 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001642 "libcore/Android.bp": []byte(`
1643 java_library {
1644 name: "core",
1645 sdk_version: "none",
1646 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001647 }
1648
1649 filegroup {
1650 name: "core-jar",
1651 srcs: [":core{.jar}"],
1652 }
1653`),
1654 })
Colin Crossae8600b2020-10-29 17:09:13 -07001655 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001656 run(t, ctx, config)
1657}
1658
1659func TestJavaImport(t *testing.T) {
1660 config := testConfig(nil, "", map[string][]byte{
1661 "libcore/Android.bp": []byte(`
1662 java_import {
1663 name: "core",
1664 sdk_version: "none",
1665 }
1666
1667 filegroup {
1668 name: "core-jar",
1669 srcs: [":core{.jar}"],
1670 }
1671`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001672 })
Colin Crossae8600b2020-10-29 17:09:13 -07001673 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001674 run(t, ctx, config)
1675}
1676
Jiyong Parkc678ad32018-04-10 13:07:10 +09001677func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001678 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001679 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001680 name: "droiddoc-templates-sdk",
1681 path: ".",
1682 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001683 java_sdk_library {
1684 name: "foo",
1685 srcs: ["a.java", "b.java"],
1686 api_packages: ["foo"],
1687 }
1688 java_sdk_library {
1689 name: "bar",
1690 srcs: ["a.java", "b.java"],
1691 api_packages: ["bar"],
1692 }
1693 java_library {
1694 name: "baz",
1695 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001696 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001697 sdk_version: "system_current",
1698 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001699 java_sdk_library {
1700 name: "barney",
1701 srcs: ["c.java"],
1702 api_only: true,
1703 }
1704 java_sdk_library {
1705 name: "betty",
1706 srcs: ["c.java"],
1707 shared_library: false,
1708 }
Paul Duffin859fe962020-05-15 10:20:31 +01001709 java_sdk_library_import {
1710 name: "quuz",
1711 public: {
1712 jars: ["c.jar"],
1713 },
1714 }
1715 java_sdk_library_import {
1716 name: "fred",
1717 public: {
1718 jars: ["b.jar"],
1719 },
1720 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001721 java_sdk_library_import {
1722 name: "wilma",
1723 public: {
1724 jars: ["b.jar"],
1725 },
1726 shared_library: false,
1727 }
Jiyong Park1be96912018-05-28 18:02:19 +09001728 java_library {
1729 name: "qux",
1730 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001731 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001732 sdk_version: "system_current",
1733 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001734 java_library {
1735 name: "baz-test",
1736 srcs: ["c.java"],
1737 libs: ["foo"],
1738 sdk_version: "test_current",
1739 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001740 java_library {
1741 name: "baz-29",
1742 srcs: ["c.java"],
1743 libs: ["foo"],
1744 sdk_version: "system_29",
1745 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001746 java_library {
1747 name: "baz-module-30",
1748 srcs: ["c.java"],
1749 libs: ["foo"],
1750 sdk_version: "module_30",
1751 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001752 `)
1753
1754 // check the existence of the internal modules
1755 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001756 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1757 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1758 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1759 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1760 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1761 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001762 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001763 ctx.ModuleForTests("foo.api.public.28", "")
1764 ctx.ModuleForTests("foo.api.system.28", "")
1765 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001766
1767 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1768 // tests if baz is actually linked to the stubs lib
1769 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1770 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1771 "foo.stubs.system.jar")
1772 }
1773 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001774 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001775 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001776 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001777 }
1778 // test if baz is not linked to the system variant of foo
1779 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1780 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1781 "foo.stubs.jar")
1782 }
Jiyong Park1be96912018-05-28 18:02:19 +09001783
Paul Duffin726d23c2020-01-22 16:30:37 +00001784 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1785 // tests if baz-test is actually linked to the test stubs lib
1786 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1787 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1788 "foo.stubs.test.jar")
1789 }
1790
Paul Duffina2db18f2020-01-22 17:11:15 +00001791 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1792 // tests if baz-29 is actually linked to the system 29 stubs lib
1793 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1794 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1795 "prebuilts/sdk/29/system/foo.jar")
1796 }
1797
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001798 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1799 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1800 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1801 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1802 "prebuilts/sdk/30/module-lib/foo.jar")
1803 }
1804
Jiyong Park1be96912018-05-28 18:02:19 +09001805 // test if baz has exported SDK lib names foo and bar to qux
1806 qux := ctx.ModuleForTests("qux", "android_common")
1807 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001808 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001809 if w := []string{"foo", "bar", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
Paul Duffin859fe962020-05-15 10:20:31 +01001810 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001811 }
1812 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001813}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001814
Anton Hansson7f66efa2020-10-08 14:47:23 +01001815func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
1816 ctx, _ := testJava(t, `
1817 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001818 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001819 srcs: ["a.java"],
1820 impl_only_libs: ["foo"],
1821 stub_only_libs: ["bar"],
1822 }
1823 java_library {
1824 name: "foo",
1825 srcs: ["a.java"],
1826 sdk_version: "current",
1827 }
1828 java_library {
1829 name: "bar",
1830 srcs: ["a.java"],
1831 sdk_version: "current",
1832 }
1833 `)
1834
Anton Hanssondff2c782020-12-21 17:10:01 +00001835 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Anton Hansson7f66efa2020-10-08 14:47:23 +01001836 implJavacCp := ctx.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
1837 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1838 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1839 }
1840 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001841 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Anton Hansson7f66efa2020-10-08 14:47:23 +01001842 stubsJavacCp := ctx.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
1843 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1844 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1845 }
1846}
1847
Paul Duffindaaa3322020-05-26 18:13:57 +01001848func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1849 ctx, _ := testJava(t, `
1850 java_sdk_library {
1851 name: "foo",
1852 srcs: ["a.java"],
1853 api_only: true,
1854 public: {
1855 enabled: true,
1856 },
1857 }
1858
1859 java_library {
1860 name: "bar",
1861 srcs: ["b.java"],
1862 libs: ["foo"],
1863 }
1864 `)
1865
1866 // The bar library should depend on the stubs jar.
1867 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1868 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1869 t.Errorf("expected %q, found %#q", expected, actual)
1870 }
1871}
1872
Paul Duffin46dc45a2020-05-14 15:39:10 +01001873func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1874 testJava(t, `
1875 java_sdk_library {
1876 name: "foo",
1877 srcs: ["a.java"],
1878 api_packages: ["foo"],
1879 public: {
1880 enabled: true,
1881 },
1882 }
1883
1884 java_library {
1885 name: "bar",
1886 srcs: ["b.java", ":foo{.public.stubs.source}"],
1887 }
1888 `)
1889}
1890
1891func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1892 testJavaError(t, `"foo" does not provide api scope system`, `
1893 java_sdk_library {
1894 name: "foo",
1895 srcs: ["a.java"],
1896 api_packages: ["foo"],
1897 public: {
1898 enabled: true,
1899 },
1900 }
1901
1902 java_library {
1903 name: "bar",
1904 srcs: ["b.java", ":foo{.system.stubs.source}"],
1905 }
1906 `)
1907}
1908
Paul Duffinca8d9a52020-06-26 22:20:25 +01001909func TestJavaSdkLibrary_Deps(t *testing.T) {
1910 ctx, _ := testJava(t, `
1911 java_sdk_library {
1912 name: "sdklib",
1913 srcs: ["a.java"],
1914 sdk_version: "none",
1915 system_modules: "none",
1916 public: {
1917 enabled: true,
1918 },
1919 }
1920 `)
1921
Paul Duffincee7e662020-07-09 17:32:57 +01001922 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001923 `dex2oatd`,
1924 `sdklib.impl`,
1925 `sdklib.stubs`,
1926 `sdklib.stubs.source`,
1927 `sdklib.xml`,
1928 })
1929}
1930
Paul Duffin46dc45a2020-05-14 15:39:10 +01001931func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1932 testJava(t, `
1933 java_sdk_library_import {
1934 name: "foo",
1935 public: {
1936 jars: ["a.jar"],
1937 stub_srcs: ["a.java"],
1938 current_api: "api/current.txt",
1939 removed_api: "api/removed.txt",
1940 },
1941 }
1942
1943 java_library {
1944 name: "bar",
1945 srcs: [":foo{.public.stubs.source}"],
1946 java_resources: [
1947 ":foo{.public.api.txt}",
1948 ":foo{.public.removed-api.txt}",
1949 ],
1950 }
1951 `)
1952}
1953
1954func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1955 bp := `
1956 java_sdk_library_import {
1957 name: "foo",
1958 public: {
1959 jars: ["a.jar"],
1960 },
1961 }
1962 `
1963
1964 t.Run("stubs.source", func(t *testing.T) {
1965 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1966 java_library {
1967 name: "bar",
1968 srcs: [":foo{.public.stubs.source}"],
1969 java_resources: [
1970 ":foo{.public.api.txt}",
1971 ":foo{.public.removed-api.txt}",
1972 ],
1973 }
1974 `)
1975 })
1976
1977 t.Run("api.txt", func(t *testing.T) {
1978 testJavaError(t, `api.txt not available for api scope public`, bp+`
1979 java_library {
1980 name: "bar",
1981 srcs: ["a.java"],
1982 java_resources: [
1983 ":foo{.public.api.txt}",
1984 ],
1985 }
1986 `)
1987 })
1988
1989 t.Run("removed-api.txt", func(t *testing.T) {
1990 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1991 java_library {
1992 name: "bar",
1993 srcs: ["a.java"],
1994 java_resources: [
1995 ":foo{.public.removed-api.txt}",
1996 ],
1997 }
1998 `)
1999 })
2000}
2001
Paul Duffin3375e352020-04-28 10:44:03 +01002002func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
2003 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
2004 java_sdk_library {
2005 name: "foo",
2006 srcs: ["a.java", "b.java"],
2007 api_packages: ["foo"],
2008 // Explicitly disable public to test the check that ensures the set of enabled
2009 // scopes is consistent.
2010 public: {
2011 enabled: false,
2012 },
2013 system: {
2014 enabled: true,
2015 },
2016 }
2017 `)
2018}
2019
Paul Duffin87a05a32020-05-12 11:50:28 +01002020func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
2021 testJava(t, `
2022 java_sdk_library {
2023 name: "foo",
2024 srcs: ["a.java", "b.java"],
2025 api_packages: ["foo"],
2026 system: {
2027 enabled: true,
2028 sdk_version: "module_current",
2029 },
2030 }
2031 `)
2032}
2033
Paul Duffin0c5bae52020-06-02 13:00:08 +01002034func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
2035 testJava(t, `
2036 java_sdk_library {
2037 name: "foo",
2038 srcs: ["a.java", "b.java"],
2039 api_packages: ["foo"],
2040 system: {
2041 enabled: true,
2042 },
2043 module_lib: {
2044 enabled: true,
2045 },
2046 }
2047 `)
2048}
2049
2050func TestJavaSdkLibrary_SystemServer(t *testing.T) {
2051 testJava(t, `
2052 java_sdk_library {
2053 name: "foo",
2054 srcs: ["a.java", "b.java"],
2055 api_packages: ["foo"],
2056 system: {
2057 enabled: true,
2058 },
2059 system_server: {
2060 enabled: true,
2061 },
2062 }
2063 `)
2064}
2065
Paul Duffin803a9562020-05-20 11:52:25 +01002066func TestJavaSdkLibrary_MissingScope(t *testing.T) {
2067 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
2068 java_sdk_library {
2069 name: "foo",
2070 srcs: ["a.java"],
2071 public: {
2072 enabled: false,
2073 },
2074 }
2075
2076 java_library {
2077 name: "baz",
2078 srcs: ["a.java"],
2079 libs: ["foo"],
2080 sdk_version: "module_current",
2081 }
2082 `)
2083}
2084
2085func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
2086 testJava(t, `
2087 java_sdk_library {
2088 name: "foo",
2089 srcs: ["a.java"],
2090 system: {
2091 enabled: true,
2092 },
2093 }
2094
2095 java_library {
2096 name: "baz",
2097 srcs: ["a.java"],
2098 libs: ["foo"],
2099 // foo does not have module-lib scope so it should fallback to system
2100 sdk_version: "module_current",
2101 }
2102 `)
2103}
2104
Jiyong Park932cdfe2020-05-28 00:19:53 +09002105func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
2106 ctx, _ := testJava(t, `
2107 java_sdk_library {
2108 name: "foo",
2109 srcs: ["a.java"],
2110 system: {
2111 enabled: true,
2112 },
2113 default_to_stubs: true,
2114 }
2115
2116 java_library {
2117 name: "baz",
2118 srcs: ["a.java"],
2119 libs: ["foo"],
2120 // does not have sdk_version set, should fallback to module,
2121 // which will then fallback to system because the module scope
2122 // is not enabled.
2123 }
2124 `)
2125 // The baz library should depend on the system stubs jar.
2126 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
2127 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2128 t.Errorf("expected %q, found %#q", expected, actual)
2129 }
2130}
2131
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002132var compilerFlagsTestCases = []struct {
2133 in string
2134 out bool
2135}{
2136 {
2137 in: "a",
2138 out: false,
2139 },
2140 {
2141 in: "-a",
2142 out: true,
2143 },
2144 {
2145 in: "-no-jdk",
2146 out: false,
2147 },
2148 {
2149 in: "-no-stdlib",
2150 out: false,
2151 },
2152 {
2153 in: "-kotlin-home",
2154 out: false,
2155 },
2156 {
2157 in: "-kotlin-home /some/path",
2158 out: false,
2159 },
2160 {
2161 in: "-include-runtime",
2162 out: false,
2163 },
2164 {
2165 in: "-Xintellij-plugin-root",
2166 out: false,
2167 },
2168}
2169
2170type mockContext struct {
2171 android.ModuleContext
2172 result bool
2173}
2174
2175func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2176 // CheckBadCompilerFlags calls this function when the flag should be rejected
2177 ctx.result = false
2178}
2179
2180func TestCompilerFlags(t *testing.T) {
2181 for _, testCase := range compilerFlagsTestCases {
2182 ctx := &mockContext{result: true}
2183 CheckKotlincFlags(ctx, []string{testCase.in})
2184 if ctx.result != testCase.out {
2185 t.Errorf("incorrect output:")
2186 t.Errorf(" input: %#v", testCase.in)
2187 t.Errorf(" expected: %#v", testCase.out)
2188 t.Errorf(" got: %#v", ctx.result)
2189 }
2190 }
2191}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002192
2193// TODO(jungjw): Consider making this more robust by ignoring path order.
2194func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2195 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2196 flags := strings.Split(variables["javacFlags"], " ")
2197 got := ""
2198 for _, flag := range flags {
2199 keyEnd := strings.Index(flag, "=")
2200 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2201 got = flag[keyEnd+1:]
2202 break
2203 }
2204 }
2205 if expected != got {
2206 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2207 }
2208}
2209
2210func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002211 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002212 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002213 bp := `
2214 java_library {
2215 name: "foo",
2216 srcs: ["a.java"],
2217 java_version: "1.8",
2218 }
2219
2220 java_library {
2221 name: "bar",
2222 srcs: ["b.java"],
2223 sdk_version: "none",
2224 system_modules: "none",
2225 patch_module: "java.base",
2226 java_version: "1.8",
2227 }
2228
2229 java_library {
2230 name: "baz",
2231 srcs: ["c.java"],
2232 patch_module: "java.base",
2233 java_version: "1.8",
2234 }
2235 `
2236 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002237
2238 checkPatchModuleFlag(t, ctx, "foo", "")
2239 checkPatchModuleFlag(t, ctx, "bar", "")
2240 checkPatchModuleFlag(t, ctx, "baz", "")
2241 })
2242
Pete Gillin0c2143e2019-05-02 15:32:11 +01002243 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002244 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002245 bp := `
2246 java_library {
2247 name: "foo",
2248 srcs: ["a.java"],
2249 }
2250
2251 java_library {
2252 name: "bar",
2253 srcs: ["b.java"],
2254 sdk_version: "none",
2255 system_modules: "none",
2256 patch_module: "java.base",
2257 }
2258
2259 java_library {
2260 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002261 srcs: [
2262 "c.java",
2263 // Tests for b/150878007
2264 "dir/d.java",
2265 "dir2/e.java",
2266 "dir2/f.java",
2267 "nested/dir/g.java"
2268 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002269 patch_module: "java.base",
2270 }
2271 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002272 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002273
2274 checkPatchModuleFlag(t, ctx, "foo", "")
2275 expected := "java.base=.:" + buildDir
2276 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002277 expected = "java.base=" + strings.Join([]string{
2278 ".", buildDir, "dir", "dir2", "nested", moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002279 checkPatchModuleFlag(t, ctx, "baz", expected)
2280 })
2281}
Paul Duffina7b9f422020-01-10 17:12:18 +00002282
2283func TestJavaSystemModules(t *testing.T) {
2284 ctx, _ := testJava(t, `
2285 java_system_modules {
2286 name: "system-modules",
2287 libs: ["system-module1", "system-module2"],
2288 }
2289 java_library {
2290 name: "system-module1",
2291 srcs: ["a.java"],
2292 sdk_version: "none",
2293 system_modules: "none",
2294 }
2295 java_library {
2296 name: "system-module2",
2297 srcs: ["b.java"],
2298 sdk_version: "none",
2299 system_modules: "none",
2300 }
2301 `)
2302
2303 // check the existence of the module
2304 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2305
2306 cmd := systemModules.Rule("jarsTosystemModules")
2307
2308 // make sure the command compiles against the supplied modules.
2309 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2310 if !strings.Contains(cmd.Args["classpath"], module) {
2311 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2312 module)
2313 }
2314 }
2315}
Paul Duffin90169ba2020-01-10 17:16:44 +00002316
2317func TestJavaSystemModulesImport(t *testing.T) {
2318 ctx, _ := testJava(t, `
2319 java_system_modules_import {
2320 name: "system-modules",
2321 libs: ["system-module1", "system-module2"],
2322 }
2323 java_import {
2324 name: "system-module1",
2325 jars: ["a.jar"],
2326 }
2327 java_import {
2328 name: "system-module2",
2329 jars: ["b.jar"],
2330 }
2331 `)
2332
2333 // check the existence of the module
2334 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2335
2336 cmd := systemModules.Rule("jarsTosystemModules")
2337
2338 // make sure the command compiles against the supplied modules.
2339 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2340 if !strings.Contains(cmd.Args["classpath"], module) {
2341 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2342 module)
2343 }
2344 }
2345}
Paul Duffin83a2d962019-11-19 19:44:10 +00002346
2347func TestJavaLibraryWithSystemModules(t *testing.T) {
2348 ctx, _ := testJava(t, `
2349 java_library {
2350 name: "lib-with-source-system-modules",
2351 srcs: [
2352 "a.java",
2353 ],
2354 sdk_version: "none",
2355 system_modules: "source-system-modules",
2356 }
2357
2358 java_library {
2359 name: "source-jar",
2360 srcs: [
2361 "a.java",
2362 ],
2363 }
2364
2365 java_system_modules {
2366 name: "source-system-modules",
2367 libs: ["source-jar"],
2368 }
2369
2370 java_library {
2371 name: "lib-with-prebuilt-system-modules",
2372 srcs: [
2373 "a.java",
2374 ],
2375 sdk_version: "none",
2376 system_modules: "prebuilt-system-modules",
2377 }
2378
2379 java_import {
2380 name: "prebuilt-jar",
2381 jars: ["a.jar"],
2382 }
2383
2384 java_system_modules_import {
2385 name: "prebuilt-system-modules",
2386 libs: ["prebuilt-jar"],
2387 }
2388 `)
2389
2390 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2391
2392 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2393}
2394
2395func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2396 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2397 bootClasspath := javacRule.Args["bootClasspath"]
2398 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2399 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2400 }
2401}
Jiyong Park19604de2020-03-24 16:44:11 +09002402
2403func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2404 ctx, _ := testJava(t, `
2405 java_library {
2406 name: "foo",
2407 srcs: ["aidl/foo/IFoo.aidl"],
2408 libs: ["bar"],
2409 }
2410
2411 java_import {
2412 name: "bar",
2413 jars: ["a.jar"],
2414 aidl: {
2415 export_include_dirs: ["aidl/bar"],
2416 },
2417 }
2418 `)
2419
2420 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2421 expectedAidlFlag := "-Iaidl/bar"
2422 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2423 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2424 }
2425}
Liz Kammerdd849a82020-06-12 16:38:45 -07002426
Jooyung Hane197d8b2021-01-05 10:33:16 +09002427func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2428 ctx, _ := testJava(t, `
2429 java_library {
2430 name: "foo",
2431 srcs: ["aidl/foo/IFoo.aidl"],
2432 aidl: { flags: ["-Werror"], },
2433 }
2434 `)
2435
2436 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2437 expectedAidlFlag := "-Werror"
2438 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2439 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2440 }
2441}
2442
Liz Kammerdd849a82020-06-12 16:38:45 -07002443func TestDataNativeBinaries(t *testing.T) {
2444 ctx, config := testJava(t, `
2445 java_test_host {
2446 name: "foo",
2447 srcs: ["a.java"],
2448 data_native_bins: ["bin"]
2449 }
2450
2451 python_binary_host {
2452 name: "bin",
2453 srcs: ["bin.py"],
2454 }
2455 `)
2456
2457 buildOS := android.BuildOs.String()
2458
2459 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2460 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
2461 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2462 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2463 if !reflect.DeepEqual(expected, actual) {
2464 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2465 }
2466}