blob: 9112655321d3eb7c8917e385d877990d7e1d17e1 [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
Paul Duffind6ceb862021-03-04 23:02:31 +000028 "android/soong/genrule"
Jeongik Cha28df2572019-11-11 10:46:36 +090029 "github.com/google/blueprint/proptools"
30
Colin Crossa4f08812018-10-02 22:03:40 -070031 "android/soong/android"
32 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080033 "android/soong/dexpreopt"
Liz Kammerdd849a82020-06-12 16:38:45 -070034 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070035)
36
37var buildDir string
38
39func setUp() {
40 var err error
41 buildDir, err = ioutil.TempDir("", "soong_java_test")
42 if err != nil {
43 panic(err)
44 }
45}
46
47func tearDown() {
48 os.RemoveAll(buildDir)
49}
50
51func TestMain(m *testing.M) {
52 run := func() int {
53 setUp()
54 defer tearDown()
55
56 return m.Run()
57 }
58
59 os.Exit(run())
60}
Colin Cross527012a2017-11-30 22:56:16 -080061
Colin Cross98be1bb2019-12-13 20:41:13 -080062func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Paul Duffin9f045242021-01-21 15:05:11 +000063 return TestConfig(buildDir, env, bp, fs)
Colin Cross1369cdb2017-09-29 17:58:17 -070064}
65
Colin Crossae8600b2020-10-29 17:09:13 -070066func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070067
Colin Crossae8600b2020-10-29 17:09:13 -070068 ctx := android.NewTestArchContext(config)
Paul Duffinc059c8c2021-01-20 17:13:52 +000069 RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080070 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080071 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070072 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Colin Cross89536d42017-07-07 14:35:50 -070073 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010074 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000075
Liz Kammerdd849a82020-06-12 16:38:45 -070076 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070077 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Paul Duffineafc16b2021-02-24 01:43:18 +000078 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
79 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
Colin Crossa4f08812018-10-02 22:03:40 -070080
Paul Duffin021f4e52020-07-30 16:04:17 +010081 android.RegisterPrebuiltMutators(ctx)
82
Paul Duffind6ceb862021-03-04 23:02:31 +000083 genrule.RegisterGenruleBuildComponents(ctx)
84
Colin Crossa4f08812018-10-02 22:03:40 -070085 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +000086 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -070087
Jaewoong Jungc779cd42020-10-06 18:56:10 -070088 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
89 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
90 })
91
Colin Cross527012a2017-11-30 22:56:16 -080092 return ctx
93}
94
95func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -080096 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -080097
Colin Cross98be1bb2019-12-13 20:41:13 -080098 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000099 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800100
Colin Crossae8600b2020-10-29 17:09:13 -0700101 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100102 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800103 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700104 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800105 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800106}
107
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900108func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900109 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800110 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900111}
112
Colin Cross98be1bb2019-12-13 20:41:13 -0800113func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900114 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700115 ctx := testContext(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900116
Colin Cross98be1bb2019-12-13 20:41:13 -0800117 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000118 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900119
Paul Duffinec0fe172021-02-25 15:34:13 +0000120 runWithErrors(t, ctx, config, pattern)
121
122 return ctx, config
123}
124
125func runWithErrors(t *testing.T, ctx *android.TestContext, config android.Config, pattern string) {
Colin Crossae8600b2020-10-29 17:09:13 -0700126 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900127 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
128 if len(errs) > 0 {
129 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000130 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900131 }
132 _, errs = ctx.PrepareBuildActions(config)
133 if len(errs) > 0 {
134 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000135 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900136 }
137
138 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Paul Duffinec0fe172021-02-25 15:34:13 +0000139 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900140}
141
Colin Cross238c1f32020-06-07 16:58:18 -0700142func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
143 t.Helper()
144 return testJavaWithConfig(t, testConfig(nil, bp, fs))
145}
146
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700147func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800148 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700149 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900150}
151
Colin Cross98be1bb2019-12-13 20:41:13 -0800152func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900153 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700154 ctx := testContext(config)
Colin Cross527012a2017-11-30 22:56:16 -0800155 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700156
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700157 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700158}
159
Colin Cross2acdae82017-09-15 19:44:24 -0700160func moduleToPath(name string) string {
161 switch {
162 case name == `""`:
163 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700164 case strings.HasSuffix(name, ".jar"):
165 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700166 default:
167 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700168 }
169}
170
Jeongik Chae403e9e2019-12-07 00:16:24 +0900171func TestJavaLinkType(t *testing.T) {
172 testJava(t, `
173 java_library {
174 name: "foo",
175 srcs: ["a.java"],
176 libs: ["bar"],
177 static_libs: ["baz"],
178 }
179
180 java_library {
181 name: "bar",
182 sdk_version: "current",
183 srcs: ["b.java"],
184 }
185
186 java_library {
187 name: "baz",
188 sdk_version: "system_current",
189 srcs: ["c.java"],
190 }
191 `)
192
Steven Moreland00298982020-11-17 21:44:36 +0000193 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900194 java_library {
195 name: "foo",
196 srcs: ["a.java"],
197 libs: ["bar"],
198 sdk_version: "current",
199 static_libs: ["baz"],
200 }
201
202 java_library {
203 name: "bar",
204 sdk_version: "current",
205 srcs: ["b.java"],
206 }
207
208 java_library {
209 name: "baz",
210 sdk_version: "system_current",
211 srcs: ["c.java"],
212 }
213 `)
214
215 testJava(t, `
216 java_library {
217 name: "foo",
218 srcs: ["a.java"],
219 libs: ["bar"],
220 sdk_version: "system_current",
221 static_libs: ["baz"],
222 }
223
224 java_library {
225 name: "bar",
226 sdk_version: "current",
227 srcs: ["b.java"],
228 }
229
230 java_library {
231 name: "baz",
232 sdk_version: "system_current",
233 srcs: ["c.java"],
234 }
235 `)
236
Steven Moreland00298982020-11-17 21:44:36 +0000237 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900238 java_library {
239 name: "foo",
240 srcs: ["a.java"],
241 libs: ["bar"],
242 sdk_version: "system_current",
243 static_libs: ["baz"],
244 }
245
246 java_library {
247 name: "bar",
248 sdk_version: "current",
249 srcs: ["b.java"],
250 }
251
252 java_library {
253 name: "baz",
254 srcs: ["c.java"],
255 }
256 `)
257}
258
Colin Cross72bb3632017-07-13 16:23:21 -0700259func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700260 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700261 java_library {
262 name: "foo",
263 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700264 libs: ["bar"],
265 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700266 }
267
268 java_library {
269 name: "bar",
270 srcs: ["b.java"],
271 }
272
273 java_library {
274 name: "baz",
275 srcs: ["c.java"],
276 }
Colin Crossd5934c82017-10-02 13:55:26 -0700277 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700278
Colin Cross4c428df2017-09-15 17:36:05 -0700279 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700280 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700281
282 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
283 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
284 }
285
Colin Cross1ee23172017-10-18 14:44:18 -0700286 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700287 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
288 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700289
Nan Zhanged19fc32017-10-19 13:06:22 -0700290 if !strings.Contains(javac.Args["classpath"], barTurbine) {
291 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700292 }
293
Nan Zhanged19fc32017-10-19 13:06:22 -0700294 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
295 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700296 }
297
298 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
299 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700300 }
301}
302
Artur Satayev9cf46692019-11-26 18:08:34 +0000303func TestExportedPlugins(t *testing.T) {
304 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800305 library string
306 processors string
307 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000308 }
309 var tests = []struct {
310 name string
311 extra string
312 results []Result
313 }{
314 {
315 name: "Exported plugin is not a direct plugin",
316 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
317 results: []Result{{library: "exports", processors: "-proc:none"}},
318 },
319 {
320 name: "Exports plugin to dependee",
321 extra: `
322 java_library{name: "exports", exported_plugins: ["plugin"]}
323 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
324 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
325 `,
326 results: []Result{
327 {library: "foo", processors: "-processor com.android.TestPlugin"},
328 {library: "bar", processors: "-processor com.android.TestPlugin"},
329 },
330 },
331 {
332 name: "Exports plugin to android_library",
333 extra: `
334 java_library{name: "exports", exported_plugins: ["plugin"]}
335 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
336 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
337 `,
338 results: []Result{
339 {library: "foo", processors: "-processor com.android.TestPlugin"},
340 {library: "bar", processors: "-processor com.android.TestPlugin"},
341 },
342 },
343 {
344 name: "Exports plugin is not propagated via transitive deps",
345 extra: `
346 java_library{name: "exports", exported_plugins: ["plugin"]}
347 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
348 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
349 `,
350 results: []Result{
351 {library: "foo", processors: "-processor com.android.TestPlugin"},
352 {library: "bar", processors: "-proc:none"},
353 },
354 },
355 {
356 name: "Exports plugin appends to plugins",
357 extra: `
358 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
359 java_library{name: "exports", exported_plugins: ["plugin"]}
360 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
361 `,
362 results: []Result{
363 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
364 },
365 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800366 {
367 name: "Exports plugin to with generates_api to dependee",
368 extra: `
369 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
370 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
371 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
372 `,
373 results: []Result{
374 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
375 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
376 },
377 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000378 }
379
380 for _, test := range tests {
381 t.Run(test.name, func(t *testing.T) {
382 ctx, _ := testJava(t, `
383 java_plugin {
384 name: "plugin",
385 processor_class: "com.android.TestPlugin",
386 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800387 java_plugin {
388 name: "plugin_generates_api",
389 generates_api: true,
390 processor_class: "com.android.TestPlugin",
391 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000392 `+test.extra)
393
394 for _, want := range test.results {
395 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
396 if javac.Args["processor"] != want.processors {
397 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
398 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800399 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
400 disableTurbine := turbine.BuildParams.Rule == nil
401 if disableTurbine != want.disableTurbine {
402 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
403 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000404 }
405 })
406 }
407}
408
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900409func TestSdkVersionByPartition(t *testing.T) {
410 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900411 java_library {
412 name: "foo",
413 srcs: ["a.java"],
414 vendor: true,
415 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900416 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900417
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900418 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900419 java_library {
420 name: "bar",
421 srcs: ["b.java"],
422 }
423 `)
424
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900425 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900426 bp := `
427 java_library {
428 name: "foo",
429 srcs: ["a.java"],
430 product_specific: true,
431 }
432 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800433
434 config := testConfig(nil, bp, nil)
435 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900436 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800437 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 +0900438 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800439 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900440 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900441 }
442}
443
Colin Crossd5934c82017-10-02 13:55:26 -0700444func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700445 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700446 java_library {
447 name: "foo",
448 srcs: ["a.java"],
449 target: {
450 android: {
451 srcs: ["b.java"],
452 },
453 },
454 }
455 `)
456
457 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
458 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
459 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
460 }
461}
462
Colin Cross6b4a32d2017-12-05 13:42:45 -0800463func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700464 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800465 java_library_host {
466 name: "foo",
467 srcs: ["a.java"],
468 }
469
470 java_binary_host {
471 name: "bar",
472 srcs: ["b.java"],
473 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700474 jni_libs: ["libjni"],
475 }
476
477 cc_library_shared {
478 name: "libjni",
479 host_supported: true,
480 device_supported: false,
481 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800482 }
483 `)
484
485 buildOS := android.BuildOs.String()
486
487 bar := ctx.ModuleForTests("bar", buildOS+"_common")
488 barJar := bar.Output("bar.jar").Output.String()
489 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
490 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
491
Colin Cross89226d92020-10-09 19:00:54 -0700492 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
493 libjniSO := libjni.Rule("Cp").Output.String()
494
Colin Cross6b4a32d2017-12-05 13:42:45 -0800495 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700496 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
497 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800498 }
Colin Cross89226d92020-10-09 19:00:54 -0700499
500 // Test that the install binary wrapper depends on the installed JNI libraries
501 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
502 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800503 }
Alex Humesky2070e322020-06-09 20:23:08 -0400504}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800505
Alex Humesky2070e322020-06-09 20:23:08 -0400506func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
507 bp := `
508 java_library {
509 name: "target_library",
510 srcs: ["a.java"],
511 }
512
513 java_binary_host {
514 name: "host_binary",
515 srcs: ["b.java"],
516 }
517 `
518 config := testConfig(nil, bp, nil)
519 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
520
521 ctx, _ := testJavaWithConfig(t, config)
522
Liz Kammer7941b302020-07-28 13:27:34 -0700523 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400524 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
525 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
526 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
527 t.Errorf("target library javac flags %v should contain "+
528 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
529 }
530
531 // check that -g is not overridden for host modules
532 buildOS := android.BuildOs.String()
533 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
534 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
535 if strings.Contains(hostJavaFlags, "-g:source,lines") {
536 t.Errorf("java_binary_host javac flags %v should not have "+
537 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
538 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800539}
540
Colin Cross72bb3632017-07-13 16:23:21 -0700541func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700542 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700543 java_library {
544 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000545 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000546 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700547 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700548 }
549
Colin Cross74d73e22017-08-02 11:05:49 -0700550 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700551 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700552 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700553 }
554
Colin Cross74d73e22017-08-02 11:05:49 -0700555 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700556 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700557 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700558 sdk_version: "current",
559 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700560 }
Colin Cross42be7612019-02-21 18:12:14 -0800561
562 dex_import {
563 name: "qux",
564 jars: ["b.jar"],
565 }
Colin Cross79c7c262019-04-17 11:11:46 -0700566
567 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000568 name: "sdklib",
569 public: {
570 jars: ["c.jar"],
571 },
572 }
573
Paul Duffin91547182019-11-12 19:39:36 +0000574 prebuilt_stubs_sources {
575 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000576 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000577 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000578
579 java_test_import {
580 name: "test",
581 jars: ["a.jar"],
582 test_suites: ["cts"],
583 test_config: "AndroidTest.xml",
584 }
Colin Cross72bb3632017-07-13 16:23:21 -0700585 `)
586
Paul Duffin9b478b02019-12-10 13:41:51 +0000587 fooModule := ctx.ModuleForTests("foo", "android_common")
588 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700589 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700590 barModule := ctx.ModuleForTests("bar", "android_common")
591 barJar := barModule.Rule("combineJar").Output
592 bazModule := ctx.ModuleForTests("baz", "android_common")
593 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700594 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700595
Paul Duffin9b478b02019-12-10 13:41:51 +0000596 fooLibrary := fooModule.Module().(*Library)
597 assertDeepEquals(t, "foo java sources incorrect",
598 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000599
Paul Duffin9b478b02019-12-10 13:41:51 +0000600 assertDeepEquals(t, "foo java source jars incorrect",
601 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
602 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000603
Colin Cross37f6d792018-07-12 12:28:41 -0700604 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
605 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700606 }
607
Liz Kammerd6c31d22020-08-05 15:40:41 -0700608 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
609 if barDexJar != nil {
610 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
611 }
612
Colin Cross79c7c262019-04-17 11:11:46 -0700613 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
614 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
615 }
616
Colin Cross37f6d792018-07-12 12:28:41 -0700617 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
618 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700619 }
Colin Cross42be7612019-02-21 18:12:14 -0800620
Liz Kammerd6c31d22020-08-05 15:40:41 -0700621 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
622 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
623 if bazDexJar != expectedDexJar {
624 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
625 }
626
Colin Cross42be7612019-02-21 18:12:14 -0800627 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700628}
629
Paul Duffin9b478b02019-12-10 13:41:51 +0000630func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
631 if !reflect.DeepEqual(expected, actual) {
632 t.Errorf("%s: expected %q, found %q", message, expected, actual)
633 }
634}
635
Paul Duffin1a393322020-11-18 16:36:47 +0000636func TestPrebuiltStubsSources(t *testing.T) {
637 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
638 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
639prebuilt_stubs_sources {
640 name: "stubs-source",
641 srcs: ["%s"],
642}`, sourcesPath), map[string][]byte{
643 "stubs/sources/pkg/A.java": nil,
644 "stubs/sources/pkg/B.java": nil,
645 })
646
647 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
648 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
649 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
650 }
651 }
652
653 t.Run("empty/missing directory", func(t *testing.T) {
654 test(t, "empty-directory", []string{})
655 })
656
657 t.Run("non-empty set of sources", func(t *testing.T) {
658 test(t, "stubs/sources", []string{
659 "stubs/sources/pkg/A.java",
660 "stubs/sources/pkg/B.java",
661 })
662 })
663}
664
Paul Duffin56d44902020-01-31 13:36:25 +0000665func TestJavaSdkLibraryImport(t *testing.T) {
666 ctx, _ := testJava(t, `
667 java_library {
668 name: "foo",
669 srcs: ["a.java"],
670 libs: ["sdklib"],
671 sdk_version: "current",
672 }
673
674 java_library {
675 name: "foo.system",
676 srcs: ["a.java"],
677 libs: ["sdklib"],
678 sdk_version: "system_current",
679 }
680
681 java_library {
682 name: "foo.test",
683 srcs: ["a.java"],
684 libs: ["sdklib"],
685 sdk_version: "test_current",
686 }
687
688 java_sdk_library_import {
689 name: "sdklib",
690 public: {
691 jars: ["a.jar"],
692 },
693 system: {
694 jars: ["b.jar"],
695 },
696 test: {
697 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100698 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000699 },
700 }
701 `)
702
703 for _, scope := range []string{"", ".system", ".test"} {
704 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
705 javac := fooModule.Rule("javac")
706
707 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
708 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
709 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
710 }
711 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100712
Paul Duffincee7e662020-07-09 17:32:57 +0100713 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100714 `prebuilt_sdklib.stubs`,
715 `prebuilt_sdklib.stubs.source.test`,
716 `prebuilt_sdklib.stubs.system`,
717 `prebuilt_sdklib.stubs.test`,
718 })
719}
720
721func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
722 ctx, _ := testJava(t, `
723 java_sdk_library {
724 name: "sdklib",
725 srcs: ["a.java"],
726 sdk_version: "none",
727 system_modules: "none",
728 public: {
729 enabled: true,
730 },
731 }
732
733 java_sdk_library_import {
734 name: "sdklib",
735 public: {
736 jars: ["a.jar"],
737 },
738 }
739 `)
740
Paul Duffincee7e662020-07-09 17:32:57 +0100741 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100742 `dex2oatd`,
743 `prebuilt_sdklib`,
744 `sdklib.impl`,
745 `sdklib.stubs`,
746 `sdklib.stubs.source`,
747 `sdklib.xml`,
748 })
749
Paul Duffincee7e662020-07-09 17:32:57 +0100750 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100751 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100752 `sdklib.impl`,
753 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
754 // dependency is added after prebuilts may have been renamed and so has to use
755 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100756 `sdklib.xml`,
757 })
758}
759
760func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
761 ctx, _ := testJava(t, `
762 java_sdk_library {
763 name: "sdklib",
764 srcs: ["a.java"],
765 sdk_version: "none",
766 system_modules: "none",
767 public: {
768 enabled: true,
769 },
770 }
771
772 java_sdk_library_import {
773 name: "sdklib",
774 prefer: true,
775 public: {
776 jars: ["a.jar"],
777 },
778 }
779 `)
780
Paul Duffincee7e662020-07-09 17:32:57 +0100781 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100782 `dex2oatd`,
783 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100784 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100785 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100786 `sdklib.stubs.source`,
787 `sdklib.xml`,
788 })
789
Paul Duffincee7e662020-07-09 17:32:57 +0100790 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100791 `prebuilt_sdklib.stubs`,
792 `sdklib.impl`,
793 `sdklib.xml`,
794 })
Paul Duffin56d44902020-01-31 13:36:25 +0000795}
796
JaeMan Parkff715562020-10-19 17:25:58 +0900797func TestJavaSdkLibraryEnforce(t *testing.T) {
798 partitionToBpOption := func(partition string) string {
799 switch partition {
800 case "system":
801 return ""
802 case "vendor":
803 return "soc_specific: true,"
804 case "product":
805 return "product_specific: true,"
806 default:
807 panic("Invalid partition group name: " + partition)
808 }
809 }
810
811 type testConfigInfo struct {
812 libraryType string
813 fromPartition string
814 toPartition string
815 enforceVendorInterface bool
816 enforceProductInterface bool
817 enforceJavaSdkLibraryCheck bool
818 allowList []string
819 }
820
821 createTestConfig := func(info testConfigInfo) android.Config {
822 bpFileTemplate := `
823 java_library {
824 name: "foo",
825 srcs: ["foo.java"],
826 libs: ["bar"],
827 sdk_version: "current",
828 %s
829 }
830
831 %s {
832 name: "bar",
833 srcs: ["bar.java"],
834 sdk_version: "current",
835 %s
836 }
837 `
838
839 bpFile := fmt.Sprintf(bpFileTemplate,
840 partitionToBpOption(info.fromPartition),
841 info.libraryType,
842 partitionToBpOption(info.toPartition))
843
844 config := testConfig(nil, bpFile, nil)
845 configVariables := config.TestProductVariables
846
847 configVariables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
848 if info.enforceVendorInterface {
849 configVariables.DeviceVndkVersion = proptools.StringPtr("current")
850 }
851 configVariables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
852 configVariables.InterPartitionJavaLibraryAllowList = info.allowList
853
854 return config
855 }
856
JaeMan Parkff715562020-10-19 17:25:58 +0900857 errorMessage := "is not allowed across the partitions"
858
JaeMan Park90e75352021-01-14 11:56:56 +0900859 testJavaWithConfig(t, createTestConfig(testConfigInfo{
860 libraryType: "java_library",
861 fromPartition: "product",
862 toPartition: "system",
863 enforceVendorInterface: true,
864 enforceProductInterface: true,
865 enforceJavaSdkLibraryCheck: false,
866 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900867
JaeMan Park90e75352021-01-14 11:56:56 +0900868 testJavaWithConfig(t, createTestConfig(testConfigInfo{
869 libraryType: "java_library",
870 fromPartition: "product",
871 toPartition: "system",
872 enforceVendorInterface: true,
873 enforceProductInterface: false,
874 enforceJavaSdkLibraryCheck: true,
875 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900876
JaeMan Park90e75352021-01-14 11:56:56 +0900877 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
878 libraryType: "java_library",
879 fromPartition: "product",
880 toPartition: "system",
881 enforceVendorInterface: true,
882 enforceProductInterface: true,
883 enforceJavaSdkLibraryCheck: true,
884 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900885
JaeMan Park90e75352021-01-14 11:56:56 +0900886 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
887 libraryType: "java_library",
888 fromPartition: "vendor",
889 toPartition: "system",
890 enforceVendorInterface: true,
891 enforceProductInterface: true,
892 enforceJavaSdkLibraryCheck: true,
893 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900894
895 testJavaWithConfig(t, createTestConfig(testConfigInfo{
896 libraryType: "java_library",
897 fromPartition: "vendor",
898 toPartition: "system",
899 enforceVendorInterface: true,
900 enforceProductInterface: true,
901 enforceJavaSdkLibraryCheck: true,
902 allowList: []string{"bar"},
903 }))
904
905 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
906 libraryType: "java_library",
907 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +0900908 toPartition: "product",
909 enforceVendorInterface: true,
910 enforceProductInterface: true,
911 enforceJavaSdkLibraryCheck: true,
912 }))
913
914 testJavaWithConfig(t, createTestConfig(testConfigInfo{
915 libraryType: "java_sdk_library",
916 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +0900917 toPartition: "system",
918 enforceVendorInterface: true,
919 enforceProductInterface: true,
920 enforceJavaSdkLibraryCheck: true,
JaeMan Park90e75352021-01-14 11:56:56 +0900921 }))
922
923 testJavaWithConfig(t, createTestConfig(testConfigInfo{
924 libraryType: "java_sdk_library",
925 fromPartition: "vendor",
926 toPartition: "system",
927 enforceVendorInterface: true,
928 enforceProductInterface: true,
929 enforceJavaSdkLibraryCheck: true,
930 }))
931
932 testJavaWithConfig(t, createTestConfig(testConfigInfo{
933 libraryType: "java_sdk_library",
934 fromPartition: "vendor",
935 toPartition: "product",
936 enforceVendorInterface: true,
937 enforceProductInterface: true,
938 enforceJavaSdkLibraryCheck: true,
JaeMan Parkff715562020-10-19 17:25:58 +0900939 }))
940}
941
Colin Cross89536d42017-07-07 14:35:50 -0700942func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700943 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700944 java_defaults {
945 name: "defaults",
946 srcs: ["a.java"],
947 libs: ["bar"],
948 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700949 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700950 }
951
952 java_library {
953 name: "foo",
954 defaults: ["defaults"],
955 }
956
957 java_library {
958 name: "bar",
959 srcs: ["b.java"],
960 }
961
962 java_library {
963 name: "baz",
964 srcs: ["c.java"],
965 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700966
967 android_test {
968 name: "atestOptimize",
969 defaults: ["defaults"],
970 optimize: {enabled: true},
971 }
972
973 android_test {
974 name: "atestNoOptimize",
975 defaults: ["defaults"],
976 }
977
978 android_test {
979 name: "atestDefault",
980 srcs: ["a.java"],
981 }
Colin Cross89536d42017-07-07 14:35:50 -0700982 `)
983
Colin Cross4c428df2017-09-15 17:36:05 -0700984 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700985 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700986
987 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
988 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
989 }
990
Nan Zhanged19fc32017-10-19 13:06:22 -0700991 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
992 if !strings.Contains(javac.Args["classpath"], barTurbine) {
993 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700994 }
995
Colin Cross1ee23172017-10-18 14:44:18 -0700996 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700997 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
998 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700999 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001000
1001 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1002 if atestOptimize.Output == nil {
1003 t.Errorf("atestOptimize should optimize APK")
1004 }
1005
1006 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1007 if atestNoOptimize.Output == nil {
1008 t.Errorf("atestNoOptimize should not optimize APK")
1009 }
1010
1011 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1012 if atestDefault.Output == nil {
1013 t.Errorf("atestDefault should optimize APK")
1014 }
Colin Cross89536d42017-07-07 14:35:50 -07001015}
1016
Colin Cross0f37af02017-09-27 17:42:05 -07001017func TestResources(t *testing.T) {
1018 var table = []struct {
1019 name string
1020 prop string
1021 extra string
1022 args string
1023 }{
1024 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001025 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001026 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001027 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001028 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001029 },
1030 {
1031 // Test that a module with java_resources includes the files
1032 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001033 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1034 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001035 },
1036 {
1037 // Test that a module with a filegroup in java_resources includes the files with the
1038 // path prefix
1039 name: "resource filegroup",
1040 prop: `java_resources: [":foo-res"]`,
1041 extra: `
1042 filegroup {
1043 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001044 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001045 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001046 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001047 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001048 },
1049 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001050 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1051 name: "wildcard dirs",
1052 prop: `java_resource_dirs: ["java-res/*"]`,
1053 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1054 },
1055 {
1056 // Test that a module exclude_java_resource_dirs excludes the files
1057 name: "wildcard dirs",
1058 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1059 args: "-C java-res/a -f java-res/a/a",
1060 },
Colin Crosscedd4762018-09-13 11:26:19 -07001061 {
1062 // Test wildcards in java_resources
1063 name: "wildcard files",
1064 prop: `java_resources: ["java-res/**/*"]`,
1065 args: "-C . -f java-res/a/a -f java-res/b/b",
1066 },
1067 {
1068 // Test exclude_java_resources with java_resources
1069 name: "wildcard files with exclude",
1070 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1071 args: "-C . -f java-res/a/a",
1072 },
1073 {
1074 // Test exclude_java_resources with java_resource_dirs
1075 name: "resource dirs with exclude files",
1076 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1077 args: "-C java-res -f java-res/a/a",
1078 },
1079 {
1080 // Test exclude_java_resource_dirs with java_resource_dirs
1081 name: "resource dirs with exclude files",
1082 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1083 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1084 },
Colin Cross0f37af02017-09-27 17:42:05 -07001085 }
1086
1087 for _, test := range table {
1088 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001089 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001090 java_library {
1091 name: "foo",
1092 srcs: [
1093 "a.java",
1094 "b.java",
1095 "c.java",
1096 ],
1097 `+test.prop+`,
1098 }
Colin Cross238c1f32020-06-07 16:58:18 -07001099 `+test.extra,
1100 map[string][]byte{
1101 "java-res/a/a": nil,
1102 "java-res/b/b": nil,
1103 "java-res2/a": nil,
1104 },
1105 )
Colin Cross0f37af02017-09-27 17:42:05 -07001106
Colin Cross331a1212018-08-15 20:40:52 -07001107 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001108 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001109
1110 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1111 t.Errorf("foo combined jars %v does not contain %q",
1112 foo.Inputs.Strings(), fooRes.Output.String())
1113 }
1114
Colin Crossaf9c55b2017-10-03 14:50:08 -07001115 if fooRes.Args["jarArgs"] != test.args {
1116 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001117 fooRes.Args["jarArgs"], test.args)
1118 }
1119 })
1120 }
1121}
1122
Colin Cross0c4ce212019-05-03 15:28:19 -07001123func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001124 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001125 java_library {
1126 name: "foo",
1127 srcs: [
1128 "a.java",
1129 "b.java",
1130 "c.java",
1131 ],
1132 include_srcs: true,
1133 }
1134
1135 java_library {
1136 name: "bar",
1137 srcs: [
1138 "a.java",
1139 "b.java",
1140 "c.java",
1141 ],
1142 java_resource_dirs: ["java-res"],
1143 include_srcs: true,
1144 }
Colin Cross238c1f32020-06-07 16:58:18 -07001145 `, map[string][]byte{
1146 "java-res/a/a": nil,
1147 "java-res/b/b": nil,
1148 "java-res2/a": nil,
1149 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001150
1151 // Test a library with include_srcs: true
1152 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1153 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1154
1155 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1156 t.Errorf("foo combined jars %v does not contain %q", w, g)
1157 }
1158
1159 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1160 t.Errorf("foo source jar args %q is not %q", w, g)
1161 }
1162
1163 // Test a library with include_srcs: true and resources
1164 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1165 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1166 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1167 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1168
1169 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1170 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1171 }
1172
1173 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1174 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1175 }
1176
1177 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1178 t.Errorf("bar combined jars %v does not contain %q", w, g)
1179 }
1180
1181 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1182 t.Errorf("bar source jar args %q is not %q", w, g)
1183 }
1184
1185 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1186 t.Errorf("bar resource jar args %q is not %q", w, g)
1187 }
1188}
1189
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001190func TestJavaLint(t *testing.T) {
1191 ctx, _ := testJavaWithFS(t, `
1192 java_library {
1193 name: "foo",
1194 srcs: [
1195 "a.java",
1196 "b.java",
1197 "c.java",
1198 ],
1199 min_sdk_version: "29",
1200 sdk_version: "system_current",
1201 }
1202 `, map[string][]byte{
1203 "lint-baseline.xml": nil,
1204 })
1205
1206 foo := ctx.ModuleForTests("foo", "android_common")
1207 rule := foo.Rule("lint")
1208
1209 if !strings.Contains(rule.RuleParams.Command, "--baseline lint-baseline.xml") {
1210 t.Error("did not pass --baseline flag")
1211 }
1212}
1213
1214func TestJavaLintWithoutBaseline(t *testing.T) {
1215 ctx, _ := testJavaWithFS(t, `
1216 java_library {
1217 name: "foo",
1218 srcs: [
1219 "a.java",
1220 "b.java",
1221 "c.java",
1222 ],
1223 min_sdk_version: "29",
1224 sdk_version: "system_current",
1225 }
1226 `, map[string][]byte{})
1227
1228 foo := ctx.ModuleForTests("foo", "android_common")
1229 rule := foo.Rule("lint")
1230
1231 if strings.Contains(rule.RuleParams.Command, "--baseline") {
1232 t.Error("passed --baseline flag for non existent file")
1233 }
1234}
1235
1236func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
1237 config := testConfig(
1238 nil,
1239 `
1240 java_library {
1241 name: "foo",
1242 srcs: [
1243 ],
1244 min_sdk_version: "29",
1245 sdk_version: "system_current",
1246 lint: {
1247 baseline_filename: "mybaseline.xml",
1248 },
1249 }
1250 `, map[string][]byte{
1251 "build/soong/java/lint_defaults.txt": nil,
1252 "prebuilts/cmdline-tools/tools/bin/lint": nil,
1253 "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil,
1254 "framework/aidl": nil,
1255 "a.java": nil,
1256 "AndroidManifest.xml": nil,
1257 "build/make/target/product/security": nil,
1258 })
1259 config.TestAllowNonExistentPaths = false
1260 testJavaErrorWithConfig(t,
1261 "source path \"mybaseline.xml\" does not exist",
1262 config,
1263 )
1264}
1265
1266func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1267 ctx, _ := testJavaWithFS(t, `
1268 java_library {
1269 name: "foo",
1270 srcs: [
1271 "a.java",
1272 "b.java",
1273 "c.java",
1274 ],
1275 min_sdk_version: "29",
1276 sdk_version: "system_current",
1277 lint: {
1278 error_checks: ["SomeCheck"],
1279 baseline_filename: "mybaseline.xml",
1280 },
1281 }
1282 `, map[string][]byte{
1283 "mybaseline.xml": nil,
1284 })
1285
1286 foo := ctx.ModuleForTests("foo", "android_common")
1287 rule := foo.Rule("lint")
1288
1289 if !strings.Contains(rule.RuleParams.Command, "--baseline mybaseline.xml") {
1290 t.Error("did not use the correct file for baseline")
1291 }
1292}
1293
Colin Cross54190b32017-10-09 15:34:10 -07001294func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001295 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001296 java_library {
1297 name: "foo",
1298 srcs: [
1299 "a*.java",
1300 ":gen",
1301 "b*.java",
1302 ],
1303 }
1304
1305 genrule {
1306 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001307 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001308 out: ["gen.java"],
1309 }
Colin Cross238c1f32020-06-07 16:58:18 -07001310 `, map[string][]byte{
1311 "a.java": nil,
1312 "b.java": nil,
1313 })
Colin Cross54190b32017-10-09 15:34:10 -07001314
1315 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1316 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1317
Colin Cross15e86d92017-10-20 15:07:08 -07001318 if filepath.Base(genrule.Output.String()) != "gen.java" {
1319 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001320 }
1321
1322 if len(javac.Inputs) != 3 ||
1323 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001324 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001325 javac.Inputs[2].String() != "b.java" {
1326 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1327 }
1328}
1329
Nan Zhang61eaedb2017-11-02 13:28:15 -07001330func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001331 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001332 java_library {
1333 name: "foo",
1334 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001335 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001336 }
1337
1338 java_library {
1339 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001340 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001341 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001342 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001343 }
1344
1345 java_library {
1346 name: "baz",
1347 srcs: ["c.java"],
1348 libs: ["bar"],
1349 sdk_version: "14",
1350 }
1351 `)
1352
1353 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1354 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1355 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1356 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1357 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1358
1359 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1360 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1361 }
1362
1363 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1364 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1365 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1366 }
1367 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1368 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1369 }
1370 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1371 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1372 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001373 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001374 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001375 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001376 }
1377}
1378
1379func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001380 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001381 java_library {
1382 name: "bar",
1383 srcs: ["a.java","b.java","c.java"],
1384 javac_shard_size: 1
1385 }
1386 `)
1387
1388 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1389 for i := 0; i < 3; i++ {
1390 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1391 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1392 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1393 }
1394 }
1395}
1396
Nan Zhang581fd212018-01-10 16:06:12 -08001397func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001398 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001399 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001400 name: "droiddoc-templates-sdk",
1401 path: ".",
1402 }
Jiyong Park29074592019-07-07 16:27:47 +09001403 filegroup {
1404 name: "bar-doc-aidl-srcs",
1405 srcs: ["bar-doc/IBar.aidl"],
1406 path: "bar-doc",
1407 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001408 droidstubs {
1409 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001410 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001411 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001412 ],
Steve Kim3666c702020-09-01 17:58:01 +00001413 exclude_srcs: [
1414 "bar-doc/b.java"
1415 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001416 api_levels_annotations_dirs: [
1417 "droiddoc-templates-sdk",
1418 ],
1419 api_levels_annotations_enabled: true,
1420 }
1421 droiddoc {
1422 name: "bar-doc",
1423 srcs: [
1424 ":bar-stubs",
1425 "bar-doc/IFoo.aidl",
1426 ":bar-doc-aidl-srcs",
1427 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001428 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001429 hdf: [
1430 "android.whichdoc offline",
1431 ],
1432 knowntags: [
1433 "bar-doc/known_oj_tags.txt",
1434 ],
1435 proofread_file: "libcore-proofread.txt",
1436 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001437 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001438 }
Colin Cross238c1f32020-06-07 16:58:18 -07001439 `,
1440 map[string][]byte{
1441 "bar-doc/a.java": nil,
1442 "bar-doc/b.java": nil,
1443 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001444 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1445 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1446 if err != nil {
1447 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1448 }
1449 if len(barStubsOutputs) != 1 {
1450 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001451 }
Nan Zhang581fd212018-01-10 16:06:12 -08001452
Liz Kammere1ab2502020-09-10 15:29:25 +00001453 barStubsOutput := barStubsOutputs[0]
1454 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1455 javaDoc := barDoc.Rule("javadoc")
1456 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1457 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001458 }
1459
Liz Kammere1ab2502020-09-10 15:29:25 +00001460 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1461 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1462 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1463 }
1464
1465 aidl := barDoc.Rule("aidl")
1466 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001467 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1468 }
1469
1470 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1471 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001472 }
Nan Zhang581fd212018-01-10 16:06:12 -08001473}
1474
Liz Kammer585cac22020-07-06 09:12:57 -07001475func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1476 testJavaError(t, "flags is set. Cannot set args", `
1477 droiddoc_exported_dir {
1478 name: "droiddoc-templates-sdk",
1479 path: ".",
1480 }
1481 filegroup {
1482 name: "bar-doc-aidl-srcs",
1483 srcs: ["bar-doc/IBar.aidl"],
1484 path: "bar-doc",
1485 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001486 droidstubs {
1487 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001488 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001489 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001490 ],
Steve Kim3666c702020-09-01 17:58:01 +00001491 exclude_srcs: [
1492 "bar-doc/b.java"
1493 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001494 api_levels_annotations_dirs: [
1495 "droiddoc-templates-sdk",
1496 ],
1497 api_levels_annotations_enabled: true,
1498 }
1499 droiddoc {
1500 name: "bar-doc",
1501 srcs: [
1502 ":bar-stubs",
1503 "bar-doc/IFoo.aidl",
1504 ":bar-doc-aidl-srcs",
1505 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001506 custom_template: "droiddoc-templates-sdk",
1507 hdf: [
1508 "android.whichdoc offline",
1509 ],
1510 knowntags: [
1511 "bar-doc/known_oj_tags.txt",
1512 ],
1513 proofread_file: "libcore-proofread.txt",
1514 todo_file: "libcore-docs-todo.html",
1515 flags: ["-offlinemode -title \"libcore\""],
1516 args: "-offlinemode -title \"libcore\"",
1517 }
1518 `)
1519}
1520
Liz Kammer3d894b72020-08-04 09:55:13 -07001521func TestDroidstubs(t *testing.T) {
1522 ctx, _ := testJavaWithFS(t, `
1523 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001524 name: "droiddoc-templates-sdk",
1525 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001526 }
1527
1528 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001529 name: "bar-stubs",
1530 srcs: ["bar-doc/a.java"],
1531 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1532 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001533 }
1534
1535 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001536 name: "bar-stubs-other",
1537 srcs: ["bar-doc/a.java"],
1538 high_mem: true,
1539 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1540 api_levels_annotations_enabled: true,
1541 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001542 }
1543 `,
1544 map[string][]byte{
1545 "bar-doc/a.java": nil,
1546 })
1547 testcases := []struct {
1548 moduleName string
1549 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001550 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001551 }{
1552 {
1553 moduleName: "bar-stubs",
1554 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001555 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001556 },
1557 {
1558 moduleName: "bar-stubs-other",
1559 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001560 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001561 },
1562 }
1563 for _, c := range testcases {
1564 m := ctx.ModuleForTests(c.moduleName, "android_common")
1565 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001566 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001567 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001568 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001569 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1570 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001571
1572 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1573 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1574 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001575 }
1576}
1577
Paul Duffin83a2d962019-11-19 19:44:10 +00001578func TestDroidstubsWithSystemModules(t *testing.T) {
1579 ctx, _ := testJava(t, `
1580 droidstubs {
1581 name: "stubs-source-system-modules",
1582 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001583 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001584 ],
1585 sdk_version: "none",
1586 system_modules: "source-system-modules",
1587 }
1588
1589 java_library {
1590 name: "source-jar",
1591 srcs: [
1592 "a.java",
1593 ],
1594 }
1595
1596 java_system_modules {
1597 name: "source-system-modules",
1598 libs: ["source-jar"],
1599 }
1600
1601 droidstubs {
1602 name: "stubs-prebuilt-system-modules",
1603 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001604 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001605 ],
1606 sdk_version: "none",
1607 system_modules: "prebuilt-system-modules",
1608 }
1609
1610 java_import {
1611 name: "prebuilt-jar",
1612 jars: ["a.jar"],
1613 }
1614
1615 java_system_modules_import {
1616 name: "prebuilt-system-modules",
1617 libs: ["prebuilt-jar"],
1618 }
1619 `)
1620
1621 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1622
1623 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1624}
1625
1626func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1627 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1628 var systemJars []string
1629 for _, i := range metalavaRule.Implicits {
1630 systemJars = append(systemJars, i.Base())
1631 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001632 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001633 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1634 }
1635}
1636
Colin Cross54250902017-12-05 09:28:08 -08001637func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001638 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001639 java_library {
1640 name: "foo",
1641 srcs: ["a.java"],
1642 }
1643
1644 java_genrule {
1645 name: "jargen",
1646 tool_files: ["b.java"],
1647 cmd: "$(location b.java) $(in) $(out)",
1648 out: ["jargen.jar"],
1649 srcs: [":foo"],
1650 }
1651
1652 java_library {
1653 name: "bar",
1654 static_libs: ["jargen"],
1655 srcs: ["c.java"],
1656 }
1657
1658 java_library {
1659 name: "baz",
1660 libs: ["jargen"],
1661 srcs: ["c.java"],
1662 }
1663 `)
1664
1665 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1666 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1667 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1668 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1669 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1670
Colin Cross3d680512020-11-13 16:23:53 -08001671 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1672 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001673 }
1674
1675 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1676 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1677 }
1678
1679 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1680 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1681 }
1682
1683 if len(barCombined.Inputs) != 2 ||
1684 barCombined.Inputs[0].String() != bar.Output.String() ||
1685 barCombined.Inputs[1].String() != jargen.Output.String() {
1686 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1687 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1688 }
1689}
1690
Nan Zhang27e284d2018-02-09 21:03:53 +00001691func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001692 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001693 java_library {
1694 name: "foo",
1695 srcs: ["a.java", ":foo-srcs"],
1696 exclude_srcs: ["a.java", ":foo-excludes"],
1697 }
1698
1699 filegroup {
1700 name: "foo-srcs",
1701 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1702 }
1703
1704 filegroup {
1705 name: "foo-excludes",
1706 srcs: ["java-fg/a.java", "java-fg/b.java"],
1707 }
1708 `)
1709
1710 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1711
1712 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1713 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1714 }
1715}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001716
Paul Duffin52d398a2019-06-11 12:31:14 +01001717func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001718 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001719 "libcore/Android.bp": []byte(`
1720 java_library {
1721 name: "core",
1722 sdk_version: "none",
1723 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001724 }
1725
1726 filegroup {
1727 name: "core-jar",
1728 srcs: [":core{.jar}"],
1729 }
1730`),
1731 })
Colin Crossae8600b2020-10-29 17:09:13 -07001732 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001733 run(t, ctx, config)
1734}
1735
1736func TestJavaImport(t *testing.T) {
1737 config := testConfig(nil, "", map[string][]byte{
1738 "libcore/Android.bp": []byte(`
1739 java_import {
1740 name: "core",
1741 sdk_version: "none",
1742 }
1743
1744 filegroup {
1745 name: "core-jar",
1746 srcs: [":core{.jar}"],
1747 }
1748`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001749 })
Colin Crossae8600b2020-10-29 17:09:13 -07001750 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001751 run(t, ctx, config)
1752}
1753
Jiyong Parkc678ad32018-04-10 13:07:10 +09001754func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001755 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001756 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001757 name: "droiddoc-templates-sdk",
1758 path: ".",
1759 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001760 java_sdk_library {
1761 name: "foo",
1762 srcs: ["a.java", "b.java"],
1763 api_packages: ["foo"],
1764 }
1765 java_sdk_library {
1766 name: "bar",
1767 srcs: ["a.java", "b.java"],
1768 api_packages: ["bar"],
1769 }
1770 java_library {
1771 name: "baz",
1772 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001773 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001774 sdk_version: "system_current",
1775 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001776 java_sdk_library {
1777 name: "barney",
1778 srcs: ["c.java"],
1779 api_only: true,
1780 }
1781 java_sdk_library {
1782 name: "betty",
1783 srcs: ["c.java"],
1784 shared_library: false,
1785 }
Paul Duffin859fe962020-05-15 10:20:31 +01001786 java_sdk_library_import {
1787 name: "quuz",
1788 public: {
1789 jars: ["c.jar"],
1790 },
1791 }
1792 java_sdk_library_import {
1793 name: "fred",
1794 public: {
1795 jars: ["b.jar"],
1796 },
1797 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001798 java_sdk_library_import {
1799 name: "wilma",
1800 public: {
1801 jars: ["b.jar"],
1802 },
1803 shared_library: false,
1804 }
Jiyong Park1be96912018-05-28 18:02:19 +09001805 java_library {
1806 name: "qux",
1807 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001808 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001809 sdk_version: "system_current",
1810 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001811 java_library {
1812 name: "baz-test",
1813 srcs: ["c.java"],
1814 libs: ["foo"],
1815 sdk_version: "test_current",
1816 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001817 java_library {
1818 name: "baz-29",
1819 srcs: ["c.java"],
1820 libs: ["foo"],
1821 sdk_version: "system_29",
1822 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001823 java_library {
1824 name: "baz-module-30",
1825 srcs: ["c.java"],
1826 libs: ["foo"],
1827 sdk_version: "module_30",
1828 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001829 `)
1830
1831 // check the existence of the internal modules
1832 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001833 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1834 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1835 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1836 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1837 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1838 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001839 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001840 ctx.ModuleForTests("foo.api.public.28", "")
1841 ctx.ModuleForTests("foo.api.system.28", "")
1842 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001843
1844 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1845 // tests if baz is actually linked to the stubs lib
1846 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1847 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1848 "foo.stubs.system.jar")
1849 }
1850 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001851 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001852 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001853 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001854 }
1855 // test if baz is not linked to the system variant of foo
1856 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1857 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1858 "foo.stubs.jar")
1859 }
Jiyong Park1be96912018-05-28 18:02:19 +09001860
Paul Duffin726d23c2020-01-22 16:30:37 +00001861 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1862 // tests if baz-test is actually linked to the test stubs lib
1863 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1864 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1865 "foo.stubs.test.jar")
1866 }
1867
Paul Duffina2db18f2020-01-22 17:11:15 +00001868 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1869 // tests if baz-29 is actually linked to the system 29 stubs lib
1870 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1871 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1872 "prebuilts/sdk/29/system/foo.jar")
1873 }
1874
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001875 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1876 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1877 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1878 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1879 "prebuilts/sdk/30/module-lib/foo.jar")
1880 }
1881
Jiyong Park1be96912018-05-28 18:02:19 +09001882 // test if baz has exported SDK lib names foo and bar to qux
1883 qux := ctx.ModuleForTests("qux", "android_common")
1884 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001885 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001886 if w := []string{"foo", "bar", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
Paul Duffin859fe962020-05-15 10:20:31 +01001887 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001888 }
1889 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001890}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001891
Anton Hansson7f66efa2020-10-08 14:47:23 +01001892func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
1893 ctx, _ := testJava(t, `
1894 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001895 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001896 srcs: ["a.java"],
1897 impl_only_libs: ["foo"],
1898 stub_only_libs: ["bar"],
1899 }
1900 java_library {
1901 name: "foo",
1902 srcs: ["a.java"],
1903 sdk_version: "current",
1904 }
1905 java_library {
1906 name: "bar",
1907 srcs: ["a.java"],
1908 sdk_version: "current",
1909 }
1910 `)
1911
Anton Hanssondff2c782020-12-21 17:10:01 +00001912 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Anton Hansson7f66efa2020-10-08 14:47:23 +01001913 implJavacCp := ctx.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
1914 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1915 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1916 }
1917 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001918 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Anton Hansson7f66efa2020-10-08 14:47:23 +01001919 stubsJavacCp := ctx.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
1920 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1921 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1922 }
1923}
1924
Paul Duffindaaa3322020-05-26 18:13:57 +01001925func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1926 ctx, _ := testJava(t, `
1927 java_sdk_library {
1928 name: "foo",
1929 srcs: ["a.java"],
1930 api_only: true,
1931 public: {
1932 enabled: true,
1933 },
1934 }
1935
1936 java_library {
1937 name: "bar",
1938 srcs: ["b.java"],
1939 libs: ["foo"],
1940 }
1941 `)
1942
1943 // The bar library should depend on the stubs jar.
1944 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1945 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1946 t.Errorf("expected %q, found %#q", expected, actual)
1947 }
1948}
1949
Paul Duffin46dc45a2020-05-14 15:39:10 +01001950func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1951 testJava(t, `
1952 java_sdk_library {
1953 name: "foo",
1954 srcs: ["a.java"],
1955 api_packages: ["foo"],
1956 public: {
1957 enabled: true,
1958 },
1959 }
1960
1961 java_library {
1962 name: "bar",
1963 srcs: ["b.java", ":foo{.public.stubs.source}"],
1964 }
1965 `)
1966}
1967
1968func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1969 testJavaError(t, `"foo" does not provide api scope system`, `
1970 java_sdk_library {
1971 name: "foo",
1972 srcs: ["a.java"],
1973 api_packages: ["foo"],
1974 public: {
1975 enabled: true,
1976 },
1977 }
1978
1979 java_library {
1980 name: "bar",
1981 srcs: ["b.java", ":foo{.system.stubs.source}"],
1982 }
1983 `)
1984}
1985
Paul Duffinca8d9a52020-06-26 22:20:25 +01001986func TestJavaSdkLibrary_Deps(t *testing.T) {
1987 ctx, _ := testJava(t, `
1988 java_sdk_library {
1989 name: "sdklib",
1990 srcs: ["a.java"],
1991 sdk_version: "none",
1992 system_modules: "none",
1993 public: {
1994 enabled: true,
1995 },
1996 }
1997 `)
1998
Paul Duffincee7e662020-07-09 17:32:57 +01001999 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01002000 `dex2oatd`,
2001 `sdklib.impl`,
2002 `sdklib.stubs`,
2003 `sdklib.stubs.source`,
2004 `sdklib.xml`,
2005 })
2006}
2007
Paul Duffin46dc45a2020-05-14 15:39:10 +01002008func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
2009 testJava(t, `
2010 java_sdk_library_import {
2011 name: "foo",
2012 public: {
2013 jars: ["a.jar"],
2014 stub_srcs: ["a.java"],
2015 current_api: "api/current.txt",
2016 removed_api: "api/removed.txt",
2017 },
2018 }
2019
2020 java_library {
2021 name: "bar",
2022 srcs: [":foo{.public.stubs.source}"],
2023 java_resources: [
2024 ":foo{.public.api.txt}",
2025 ":foo{.public.removed-api.txt}",
2026 ],
2027 }
2028 `)
2029}
2030
2031func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2032 bp := `
2033 java_sdk_library_import {
2034 name: "foo",
2035 public: {
2036 jars: ["a.jar"],
2037 },
2038 }
2039 `
2040
2041 t.Run("stubs.source", func(t *testing.T) {
2042 testJavaError(t, `stubs.source not available for api scope public`, bp+`
2043 java_library {
2044 name: "bar",
2045 srcs: [":foo{.public.stubs.source}"],
2046 java_resources: [
2047 ":foo{.public.api.txt}",
2048 ":foo{.public.removed-api.txt}",
2049 ],
2050 }
2051 `)
2052 })
2053
2054 t.Run("api.txt", func(t *testing.T) {
2055 testJavaError(t, `api.txt not available for api scope public`, bp+`
2056 java_library {
2057 name: "bar",
2058 srcs: ["a.java"],
2059 java_resources: [
2060 ":foo{.public.api.txt}",
2061 ],
2062 }
2063 `)
2064 })
2065
2066 t.Run("removed-api.txt", func(t *testing.T) {
2067 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
2068 java_library {
2069 name: "bar",
2070 srcs: ["a.java"],
2071 java_resources: [
2072 ":foo{.public.removed-api.txt}",
2073 ],
2074 }
2075 `)
2076 })
2077}
2078
Paul Duffin3375e352020-04-28 10:44:03 +01002079func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
2080 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
2081 java_sdk_library {
2082 name: "foo",
2083 srcs: ["a.java", "b.java"],
2084 api_packages: ["foo"],
2085 // Explicitly disable public to test the check that ensures the set of enabled
2086 // scopes is consistent.
2087 public: {
2088 enabled: false,
2089 },
2090 system: {
2091 enabled: true,
2092 },
2093 }
2094 `)
2095}
2096
Paul Duffin87a05a32020-05-12 11:50:28 +01002097func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
2098 testJava(t, `
2099 java_sdk_library {
2100 name: "foo",
2101 srcs: ["a.java", "b.java"],
2102 api_packages: ["foo"],
2103 system: {
2104 enabled: true,
2105 sdk_version: "module_current",
2106 },
2107 }
2108 `)
2109}
2110
Paul Duffin0c5bae52020-06-02 13:00:08 +01002111func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
2112 testJava(t, `
2113 java_sdk_library {
2114 name: "foo",
2115 srcs: ["a.java", "b.java"],
2116 api_packages: ["foo"],
2117 system: {
2118 enabled: true,
2119 },
2120 module_lib: {
2121 enabled: true,
2122 },
2123 }
2124 `)
2125}
2126
2127func TestJavaSdkLibrary_SystemServer(t *testing.T) {
2128 testJava(t, `
2129 java_sdk_library {
2130 name: "foo",
2131 srcs: ["a.java", "b.java"],
2132 api_packages: ["foo"],
2133 system: {
2134 enabled: true,
2135 },
2136 system_server: {
2137 enabled: true,
2138 },
2139 }
2140 `)
2141}
2142
Paul Duffin803a9562020-05-20 11:52:25 +01002143func TestJavaSdkLibrary_MissingScope(t *testing.T) {
2144 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
2145 java_sdk_library {
2146 name: "foo",
2147 srcs: ["a.java"],
2148 public: {
2149 enabled: false,
2150 },
2151 }
2152
2153 java_library {
2154 name: "baz",
2155 srcs: ["a.java"],
2156 libs: ["foo"],
2157 sdk_version: "module_current",
2158 }
2159 `)
2160}
2161
2162func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
2163 testJava(t, `
2164 java_sdk_library {
2165 name: "foo",
2166 srcs: ["a.java"],
2167 system: {
2168 enabled: true,
2169 },
2170 }
2171
2172 java_library {
2173 name: "baz",
2174 srcs: ["a.java"],
2175 libs: ["foo"],
2176 // foo does not have module-lib scope so it should fallback to system
2177 sdk_version: "module_current",
2178 }
2179 `)
2180}
2181
Jiyong Park932cdfe2020-05-28 00:19:53 +09002182func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
2183 ctx, _ := testJava(t, `
2184 java_sdk_library {
2185 name: "foo",
2186 srcs: ["a.java"],
2187 system: {
2188 enabled: true,
2189 },
2190 default_to_stubs: true,
2191 }
2192
2193 java_library {
2194 name: "baz",
2195 srcs: ["a.java"],
2196 libs: ["foo"],
2197 // does not have sdk_version set, should fallback to module,
2198 // which will then fallback to system because the module scope
2199 // is not enabled.
2200 }
2201 `)
2202 // The baz library should depend on the system stubs jar.
2203 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
2204 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2205 t.Errorf("expected %q, found %#q", expected, actual)
2206 }
2207}
2208
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002209var compilerFlagsTestCases = []struct {
2210 in string
2211 out bool
2212}{
2213 {
2214 in: "a",
2215 out: false,
2216 },
2217 {
2218 in: "-a",
2219 out: true,
2220 },
2221 {
2222 in: "-no-jdk",
2223 out: false,
2224 },
2225 {
2226 in: "-no-stdlib",
2227 out: false,
2228 },
2229 {
2230 in: "-kotlin-home",
2231 out: false,
2232 },
2233 {
2234 in: "-kotlin-home /some/path",
2235 out: false,
2236 },
2237 {
2238 in: "-include-runtime",
2239 out: false,
2240 },
2241 {
2242 in: "-Xintellij-plugin-root",
2243 out: false,
2244 },
2245}
2246
2247type mockContext struct {
2248 android.ModuleContext
2249 result bool
2250}
2251
2252func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2253 // CheckBadCompilerFlags calls this function when the flag should be rejected
2254 ctx.result = false
2255}
2256
2257func TestCompilerFlags(t *testing.T) {
2258 for _, testCase := range compilerFlagsTestCases {
2259 ctx := &mockContext{result: true}
2260 CheckKotlincFlags(ctx, []string{testCase.in})
2261 if ctx.result != testCase.out {
2262 t.Errorf("incorrect output:")
2263 t.Errorf(" input: %#v", testCase.in)
2264 t.Errorf(" expected: %#v", testCase.out)
2265 t.Errorf(" got: %#v", ctx.result)
2266 }
2267 }
2268}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002269
2270// TODO(jungjw): Consider making this more robust by ignoring path order.
2271func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2272 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2273 flags := strings.Split(variables["javacFlags"], " ")
2274 got := ""
2275 for _, flag := range flags {
2276 keyEnd := strings.Index(flag, "=")
2277 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2278 got = flag[keyEnd+1:]
2279 break
2280 }
2281 }
2282 if expected != got {
2283 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2284 }
2285}
2286
2287func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002288 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002289 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002290 bp := `
2291 java_library {
2292 name: "foo",
2293 srcs: ["a.java"],
2294 java_version: "1.8",
2295 }
2296
2297 java_library {
2298 name: "bar",
2299 srcs: ["b.java"],
2300 sdk_version: "none",
2301 system_modules: "none",
2302 patch_module: "java.base",
2303 java_version: "1.8",
2304 }
2305
2306 java_library {
2307 name: "baz",
2308 srcs: ["c.java"],
2309 patch_module: "java.base",
2310 java_version: "1.8",
2311 }
2312 `
2313 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002314
2315 checkPatchModuleFlag(t, ctx, "foo", "")
2316 checkPatchModuleFlag(t, ctx, "bar", "")
2317 checkPatchModuleFlag(t, ctx, "baz", "")
2318 })
2319
Pete Gillin0c2143e2019-05-02 15:32:11 +01002320 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002321 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002322 bp := `
2323 java_library {
2324 name: "foo",
2325 srcs: ["a.java"],
2326 }
2327
2328 java_library {
2329 name: "bar",
2330 srcs: ["b.java"],
2331 sdk_version: "none",
2332 system_modules: "none",
2333 patch_module: "java.base",
2334 }
2335
2336 java_library {
2337 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002338 srcs: [
2339 "c.java",
2340 // Tests for b/150878007
2341 "dir/d.java",
2342 "dir2/e.java",
2343 "dir2/f.java",
2344 "nested/dir/g.java"
2345 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002346 patch_module: "java.base",
2347 }
2348 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002349 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002350
2351 checkPatchModuleFlag(t, ctx, "foo", "")
2352 expected := "java.base=.:" + buildDir
2353 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002354 expected = "java.base=" + strings.Join([]string{
2355 ".", buildDir, "dir", "dir2", "nested", moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002356 checkPatchModuleFlag(t, ctx, "baz", expected)
2357 })
2358}
Paul Duffina7b9f422020-01-10 17:12:18 +00002359
2360func TestJavaSystemModules(t *testing.T) {
2361 ctx, _ := testJava(t, `
2362 java_system_modules {
2363 name: "system-modules",
2364 libs: ["system-module1", "system-module2"],
2365 }
2366 java_library {
2367 name: "system-module1",
2368 srcs: ["a.java"],
2369 sdk_version: "none",
2370 system_modules: "none",
2371 }
2372 java_library {
2373 name: "system-module2",
2374 srcs: ["b.java"],
2375 sdk_version: "none",
2376 system_modules: "none",
2377 }
2378 `)
2379
2380 // check the existence of the module
2381 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2382
2383 cmd := systemModules.Rule("jarsTosystemModules")
2384
2385 // make sure the command compiles against the supplied modules.
2386 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2387 if !strings.Contains(cmd.Args["classpath"], module) {
2388 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2389 module)
2390 }
2391 }
2392}
Paul Duffin90169ba2020-01-10 17:16:44 +00002393
2394func TestJavaSystemModulesImport(t *testing.T) {
2395 ctx, _ := testJava(t, `
2396 java_system_modules_import {
2397 name: "system-modules",
2398 libs: ["system-module1", "system-module2"],
2399 }
2400 java_import {
2401 name: "system-module1",
2402 jars: ["a.jar"],
2403 }
2404 java_import {
2405 name: "system-module2",
2406 jars: ["b.jar"],
2407 }
2408 `)
2409
2410 // check the existence of the module
2411 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2412
2413 cmd := systemModules.Rule("jarsTosystemModules")
2414
2415 // make sure the command compiles against the supplied modules.
2416 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2417 if !strings.Contains(cmd.Args["classpath"], module) {
2418 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2419 module)
2420 }
2421 }
2422}
Paul Duffin83a2d962019-11-19 19:44:10 +00002423
2424func TestJavaLibraryWithSystemModules(t *testing.T) {
2425 ctx, _ := testJava(t, `
2426 java_library {
2427 name: "lib-with-source-system-modules",
2428 srcs: [
2429 "a.java",
2430 ],
2431 sdk_version: "none",
2432 system_modules: "source-system-modules",
2433 }
2434
2435 java_library {
2436 name: "source-jar",
2437 srcs: [
2438 "a.java",
2439 ],
2440 }
2441
2442 java_system_modules {
2443 name: "source-system-modules",
2444 libs: ["source-jar"],
2445 }
2446
2447 java_library {
2448 name: "lib-with-prebuilt-system-modules",
2449 srcs: [
2450 "a.java",
2451 ],
2452 sdk_version: "none",
2453 system_modules: "prebuilt-system-modules",
2454 }
2455
2456 java_import {
2457 name: "prebuilt-jar",
2458 jars: ["a.jar"],
2459 }
2460
2461 java_system_modules_import {
2462 name: "prebuilt-system-modules",
2463 libs: ["prebuilt-jar"],
2464 }
2465 `)
2466
2467 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2468
2469 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2470}
2471
2472func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2473 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2474 bootClasspath := javacRule.Args["bootClasspath"]
2475 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2476 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2477 }
2478}
Jiyong Park19604de2020-03-24 16:44:11 +09002479
2480func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2481 ctx, _ := testJava(t, `
2482 java_library {
2483 name: "foo",
2484 srcs: ["aidl/foo/IFoo.aidl"],
2485 libs: ["bar"],
2486 }
2487
2488 java_import {
2489 name: "bar",
2490 jars: ["a.jar"],
2491 aidl: {
2492 export_include_dirs: ["aidl/bar"],
2493 },
2494 }
2495 `)
2496
2497 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2498 expectedAidlFlag := "-Iaidl/bar"
2499 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2500 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2501 }
2502}
Liz Kammerdd849a82020-06-12 16:38:45 -07002503
Jooyung Hane197d8b2021-01-05 10:33:16 +09002504func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2505 ctx, _ := testJava(t, `
2506 java_library {
2507 name: "foo",
2508 srcs: ["aidl/foo/IFoo.aidl"],
2509 aidl: { flags: ["-Werror"], },
2510 }
2511 `)
2512
2513 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2514 expectedAidlFlag := "-Werror"
2515 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2516 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2517 }
2518}
2519
Liz Kammerdd849a82020-06-12 16:38:45 -07002520func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002521 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002522 java_test_host {
2523 name: "foo",
2524 srcs: ["a.java"],
2525 data_native_bins: ["bin"]
2526 }
2527
2528 python_binary_host {
2529 name: "bin",
2530 srcs: ["bin.py"],
2531 }
2532 `)
2533
2534 buildOS := android.BuildOs.String()
2535
2536 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002537 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Liz Kammerdd849a82020-06-12 16:38:45 -07002538 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2539 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2540 if !reflect.DeepEqual(expected, actual) {
2541 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2542 }
2543}