blob: 48d6062fd4ab24d60e6af2e1a3ddaa6a41cb0737 [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Paul Duffin1a393322020-11-18 16:36:47 +000018 "fmt"
Colin Cross72bb3632017-07-13 16:23:21 -070019 "io/ioutil"
20 "os"
21 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070022 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010023 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070024 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070027
Jeongik Cha28df2572019-11-11 10:46:36 +090028 "github.com/google/blueprint/proptools"
29
Colin Crossa4f08812018-10-02 22:03:40 -070030 "android/soong/android"
31 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080032 "android/soong/dexpreopt"
Liz Kammerdd849a82020-06-12 16:38:45 -070033 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070034)
35
36var buildDir string
37
38func setUp() {
39 var err error
40 buildDir, err = ioutil.TempDir("", "soong_java_test")
41 if err != nil {
42 panic(err)
43 }
44}
45
46func tearDown() {
47 os.RemoveAll(buildDir)
48}
49
50func TestMain(m *testing.M) {
51 run := func() int {
52 setUp()
53 defer tearDown()
54
55 return m.Run()
56 }
57
58 os.Exit(run())
59}
Colin Cross527012a2017-11-30 22:56:16 -080060
Colin Cross98be1bb2019-12-13 20:41:13 -080061func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000062 bp += dexpreopt.BpToolModulesForTest()
63
Martin Stjernholm75a48d82020-01-10 20:32:59 +000064 config := TestConfig(buildDir, env, bp, fs)
65
66 // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that
67 // it doesn't create a real one, which would fail.
68 _ = dexpreopt.GlobalSoongConfigForTests(config)
69
70 return config
Colin Cross1369cdb2017-09-29 17:58:17 -070071}
72
Colin Crossae8600b2020-10-29 17:09:13 -070073func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070074
Colin Crossae8600b2020-10-29 17:09:13 -070075 ctx := android.NewTestArchContext(config)
Paul Duffinf9b1da02019-12-18 19:51:55 +000076 RegisterJavaBuildComponents(ctx)
77 RegisterAppBuildComponents(ctx)
78 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000079 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000080 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080081 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080082 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070083 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000084 RegisterDocsBuildComponents(ctx)
85 RegisterStubsBuildComponents(ctx)
Anton Hanssondff2c782020-12-21 17:10:01 +000086 RegisterPrebuiltApisBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000087 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070088 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010089 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000090
Liz Kammerdd849a82020-06-12 16:38:45 -070091 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070092 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross06fa5882020-10-29 18:21:38 -070093 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(ctx.Context, OverlaySingletonFactory))
94 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(ctx.Context, sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070095
Paul Duffin021f4e52020-07-30 16:04:17 +010096 android.RegisterPrebuiltMutators(ctx)
97
Colin Crossa4f08812018-10-02 22:03:40 -070098 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +000099 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700100
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000101 dexpreopt.RegisterToolModulesForTest(ctx)
102
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700103 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
104 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
105 })
106
Colin Cross527012a2017-11-30 22:56:16 -0800107 return ctx
108}
109
110func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800111 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800112
Colin Cross98be1bb2019-12-13 20:41:13 -0800113 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000114 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800115
Colin Crossae8600b2020-10-29 17:09:13 -0700116 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100117 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800118 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700119 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800120 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800121}
122
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900123func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900124 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800125 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900126}
127
Colin Cross98be1bb2019-12-13 20:41:13 -0800128func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900129 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700130 ctx := testContext(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900131
Colin Cross98be1bb2019-12-13 20:41:13 -0800132 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000133 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900134
Colin Crossae8600b2020-10-29 17:09:13 -0700135 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900136 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
137 if len(errs) > 0 {
138 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900139 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900140 }
141 _, errs = ctx.PrepareBuildActions(config)
142 if len(errs) > 0 {
143 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900144 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900145 }
146
147 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900148
149 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900150}
151
Colin Cross238c1f32020-06-07 16:58:18 -0700152func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
153 t.Helper()
154 return testJavaWithConfig(t, testConfig(nil, bp, fs))
155}
156
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700157func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800158 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700159 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900160}
161
Colin Cross98be1bb2019-12-13 20:41:13 -0800162func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900163 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700164 ctx := testContext(config)
Colin Cross527012a2017-11-30 22:56:16 -0800165 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700166
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700167 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700168}
169
Colin Cross2acdae82017-09-15 19:44:24 -0700170func moduleToPath(name string) string {
171 switch {
172 case name == `""`:
173 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700174 case strings.HasSuffix(name, ".jar"):
175 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700176 default:
177 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700178 }
179}
180
Jeongik Chae403e9e2019-12-07 00:16:24 +0900181func TestJavaLinkType(t *testing.T) {
182 testJava(t, `
183 java_library {
184 name: "foo",
185 srcs: ["a.java"],
186 libs: ["bar"],
187 static_libs: ["baz"],
188 }
189
190 java_library {
191 name: "bar",
192 sdk_version: "current",
193 srcs: ["b.java"],
194 }
195
196 java_library {
197 name: "baz",
198 sdk_version: "system_current",
199 srcs: ["c.java"],
200 }
201 `)
202
Steven Moreland00298982020-11-17 21:44:36 +0000203 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900204 java_library {
205 name: "foo",
206 srcs: ["a.java"],
207 libs: ["bar"],
208 sdk_version: "current",
209 static_libs: ["baz"],
210 }
211
212 java_library {
213 name: "bar",
214 sdk_version: "current",
215 srcs: ["b.java"],
216 }
217
218 java_library {
219 name: "baz",
220 sdk_version: "system_current",
221 srcs: ["c.java"],
222 }
223 `)
224
225 testJava(t, `
226 java_library {
227 name: "foo",
228 srcs: ["a.java"],
229 libs: ["bar"],
230 sdk_version: "system_current",
231 static_libs: ["baz"],
232 }
233
234 java_library {
235 name: "bar",
236 sdk_version: "current",
237 srcs: ["b.java"],
238 }
239
240 java_library {
241 name: "baz",
242 sdk_version: "system_current",
243 srcs: ["c.java"],
244 }
245 `)
246
Steven Moreland00298982020-11-17 21:44:36 +0000247 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900248 java_library {
249 name: "foo",
250 srcs: ["a.java"],
251 libs: ["bar"],
252 sdk_version: "system_current",
253 static_libs: ["baz"],
254 }
255
256 java_library {
257 name: "bar",
258 sdk_version: "current",
259 srcs: ["b.java"],
260 }
261
262 java_library {
263 name: "baz",
264 srcs: ["c.java"],
265 }
266 `)
267}
268
Colin Cross72bb3632017-07-13 16:23:21 -0700269func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700270 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700271 java_library {
272 name: "foo",
273 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700274 libs: ["bar"],
275 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700276 }
277
278 java_library {
279 name: "bar",
280 srcs: ["b.java"],
281 }
282
283 java_library {
284 name: "baz",
285 srcs: ["c.java"],
286 }
Colin Crossd5934c82017-10-02 13:55:26 -0700287 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700288
Colin Cross4c428df2017-09-15 17:36:05 -0700289 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700290 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700291
292 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
293 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
294 }
295
Colin Cross1ee23172017-10-18 14:44:18 -0700296 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700297 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
298 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700299
Nan Zhanged19fc32017-10-19 13:06:22 -0700300 if !strings.Contains(javac.Args["classpath"], barTurbine) {
301 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700302 }
303
Nan Zhanged19fc32017-10-19 13:06:22 -0700304 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
305 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700306 }
307
308 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
309 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700310 }
311}
312
Artur Satayev9cf46692019-11-26 18:08:34 +0000313func TestExportedPlugins(t *testing.T) {
314 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800315 library string
316 processors string
317 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000318 }
319 var tests = []struct {
320 name string
321 extra string
322 results []Result
323 }{
324 {
325 name: "Exported plugin is not a direct plugin",
326 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
327 results: []Result{{library: "exports", processors: "-proc:none"}},
328 },
329 {
330 name: "Exports plugin to dependee",
331 extra: `
332 java_library{name: "exports", exported_plugins: ["plugin"]}
333 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
334 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
335 `,
336 results: []Result{
337 {library: "foo", processors: "-processor com.android.TestPlugin"},
338 {library: "bar", processors: "-processor com.android.TestPlugin"},
339 },
340 },
341 {
342 name: "Exports plugin to android_library",
343 extra: `
344 java_library{name: "exports", exported_plugins: ["plugin"]}
345 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
346 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
347 `,
348 results: []Result{
349 {library: "foo", processors: "-processor com.android.TestPlugin"},
350 {library: "bar", processors: "-processor com.android.TestPlugin"},
351 },
352 },
353 {
354 name: "Exports plugin is not propagated via transitive deps",
355 extra: `
356 java_library{name: "exports", exported_plugins: ["plugin"]}
357 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
358 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
359 `,
360 results: []Result{
361 {library: "foo", processors: "-processor com.android.TestPlugin"},
362 {library: "bar", processors: "-proc:none"},
363 },
364 },
365 {
366 name: "Exports plugin appends to plugins",
367 extra: `
368 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
369 java_library{name: "exports", exported_plugins: ["plugin"]}
370 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
371 `,
372 results: []Result{
373 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
374 },
375 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800376 {
377 name: "Exports plugin to with generates_api to dependee",
378 extra: `
379 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
380 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
381 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
382 `,
383 results: []Result{
384 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
385 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
386 },
387 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000388 }
389
390 for _, test := range tests {
391 t.Run(test.name, func(t *testing.T) {
392 ctx, _ := testJava(t, `
393 java_plugin {
394 name: "plugin",
395 processor_class: "com.android.TestPlugin",
396 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800397 java_plugin {
398 name: "plugin_generates_api",
399 generates_api: true,
400 processor_class: "com.android.TestPlugin",
401 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000402 `+test.extra)
403
404 for _, want := range test.results {
405 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
406 if javac.Args["processor"] != want.processors {
407 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
408 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800409 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
410 disableTurbine := turbine.BuildParams.Rule == nil
411 if disableTurbine != want.disableTurbine {
412 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
413 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000414 }
415 })
416 }
417}
418
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900419func TestSdkVersionByPartition(t *testing.T) {
420 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900421 java_library {
422 name: "foo",
423 srcs: ["a.java"],
424 vendor: true,
425 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900426 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900427
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900428 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900429 java_library {
430 name: "bar",
431 srcs: ["b.java"],
432 }
433 `)
434
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900435 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900436 bp := `
437 java_library {
438 name: "foo",
439 srcs: ["a.java"],
440 product_specific: true,
441 }
442 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800443
444 config := testConfig(nil, bp, nil)
445 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900446 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800447 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 +0900448 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800449 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900450 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900451 }
452}
453
Colin Crossd5934c82017-10-02 13:55:26 -0700454func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700455 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700456 java_library {
457 name: "foo",
458 srcs: ["a.java"],
459 target: {
460 android: {
461 srcs: ["b.java"],
462 },
463 },
464 }
465 `)
466
467 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
468 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
469 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
470 }
471}
472
Colin Cross6b4a32d2017-12-05 13:42:45 -0800473func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700474 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800475 java_library_host {
476 name: "foo",
477 srcs: ["a.java"],
478 }
479
480 java_binary_host {
481 name: "bar",
482 srcs: ["b.java"],
483 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700484 jni_libs: ["libjni"],
485 }
486
487 cc_library_shared {
488 name: "libjni",
489 host_supported: true,
490 device_supported: false,
491 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800492 }
493 `)
494
495 buildOS := android.BuildOs.String()
496
497 bar := ctx.ModuleForTests("bar", buildOS+"_common")
498 barJar := bar.Output("bar.jar").Output.String()
499 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
500 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
501
Colin Cross89226d92020-10-09 19:00:54 -0700502 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
503 libjniSO := libjni.Rule("Cp").Output.String()
504
Colin Cross6b4a32d2017-12-05 13:42:45 -0800505 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700506 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
507 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800508 }
Colin Cross89226d92020-10-09 19:00:54 -0700509
510 // Test that the install binary wrapper depends on the installed JNI libraries
511 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
512 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800513 }
Alex Humesky2070e322020-06-09 20:23:08 -0400514}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800515
Alex Humesky2070e322020-06-09 20:23:08 -0400516func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
517 bp := `
518 java_library {
519 name: "target_library",
520 srcs: ["a.java"],
521 }
522
523 java_binary_host {
524 name: "host_binary",
525 srcs: ["b.java"],
526 }
527 `
528 config := testConfig(nil, bp, nil)
529 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
530
531 ctx, _ := testJavaWithConfig(t, config)
532
Liz Kammer7941b302020-07-28 13:27:34 -0700533 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400534 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
535 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
536 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
537 t.Errorf("target library javac flags %v should contain "+
538 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
539 }
540
541 // check that -g is not overridden for host modules
542 buildOS := android.BuildOs.String()
543 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
544 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
545 if strings.Contains(hostJavaFlags, "-g:source,lines") {
546 t.Errorf("java_binary_host javac flags %v should not have "+
547 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
548 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800549}
550
Colin Cross72bb3632017-07-13 16:23:21 -0700551func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700552 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700553 java_library {
554 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000555 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000556 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700557 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700558 }
559
Colin Cross74d73e22017-08-02 11:05:49 -0700560 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700561 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700562 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700563 }
564
Colin Cross74d73e22017-08-02 11:05:49 -0700565 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700566 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700567 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700568 sdk_version: "current",
569 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700570 }
Colin Cross42be7612019-02-21 18:12:14 -0800571
572 dex_import {
573 name: "qux",
574 jars: ["b.jar"],
575 }
Colin Cross79c7c262019-04-17 11:11:46 -0700576
577 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000578 name: "sdklib",
579 public: {
580 jars: ["c.jar"],
581 },
582 }
583
Paul Duffin91547182019-11-12 19:39:36 +0000584 prebuilt_stubs_sources {
585 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000586 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000587 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000588
589 java_test_import {
590 name: "test",
591 jars: ["a.jar"],
592 test_suites: ["cts"],
593 test_config: "AndroidTest.xml",
594 }
Colin Cross72bb3632017-07-13 16:23:21 -0700595 `)
596
Paul Duffin9b478b02019-12-10 13:41:51 +0000597 fooModule := ctx.ModuleForTests("foo", "android_common")
598 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700599 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700600 barModule := ctx.ModuleForTests("bar", "android_common")
601 barJar := barModule.Rule("combineJar").Output
602 bazModule := ctx.ModuleForTests("baz", "android_common")
603 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700604 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700605
Paul Duffin9b478b02019-12-10 13:41:51 +0000606 fooLibrary := fooModule.Module().(*Library)
607 assertDeepEquals(t, "foo java sources incorrect",
608 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000609
Paul Duffin9b478b02019-12-10 13:41:51 +0000610 assertDeepEquals(t, "foo java source jars incorrect",
611 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
612 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000613
Colin Cross37f6d792018-07-12 12:28:41 -0700614 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
615 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700616 }
617
Liz Kammerd6c31d22020-08-05 15:40:41 -0700618 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
619 if barDexJar != nil {
620 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
621 }
622
Colin Cross79c7c262019-04-17 11:11:46 -0700623 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
624 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
625 }
626
Colin Cross37f6d792018-07-12 12:28:41 -0700627 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
628 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700629 }
Colin Cross42be7612019-02-21 18:12:14 -0800630
Liz Kammerd6c31d22020-08-05 15:40:41 -0700631 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
632 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
633 if bazDexJar != expectedDexJar {
634 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
635 }
636
Colin Cross42be7612019-02-21 18:12:14 -0800637 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700638}
639
Paul Duffin9b478b02019-12-10 13:41:51 +0000640func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
641 if !reflect.DeepEqual(expected, actual) {
642 t.Errorf("%s: expected %q, found %q", message, expected, actual)
643 }
644}
645
Paul Duffin1a393322020-11-18 16:36:47 +0000646func TestPrebuiltStubsSources(t *testing.T) {
647 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
648 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
649prebuilt_stubs_sources {
650 name: "stubs-source",
651 srcs: ["%s"],
652}`, sourcesPath), map[string][]byte{
653 "stubs/sources/pkg/A.java": nil,
654 "stubs/sources/pkg/B.java": nil,
655 })
656
657 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
658 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
659 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
660 }
661 }
662
663 t.Run("empty/missing directory", func(t *testing.T) {
664 test(t, "empty-directory", []string{})
665 })
666
667 t.Run("non-empty set of sources", func(t *testing.T) {
668 test(t, "stubs/sources", []string{
669 "stubs/sources/pkg/A.java",
670 "stubs/sources/pkg/B.java",
671 })
672 })
673}
674
Paul Duffin56d44902020-01-31 13:36:25 +0000675func TestJavaSdkLibraryImport(t *testing.T) {
676 ctx, _ := testJava(t, `
677 java_library {
678 name: "foo",
679 srcs: ["a.java"],
680 libs: ["sdklib"],
681 sdk_version: "current",
682 }
683
684 java_library {
685 name: "foo.system",
686 srcs: ["a.java"],
687 libs: ["sdklib"],
688 sdk_version: "system_current",
689 }
690
691 java_library {
692 name: "foo.test",
693 srcs: ["a.java"],
694 libs: ["sdklib"],
695 sdk_version: "test_current",
696 }
697
698 java_sdk_library_import {
699 name: "sdklib",
700 public: {
701 jars: ["a.jar"],
702 },
703 system: {
704 jars: ["b.jar"],
705 },
706 test: {
707 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100708 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000709 },
710 }
711 `)
712
713 for _, scope := range []string{"", ".system", ".test"} {
714 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
715 javac := fooModule.Rule("javac")
716
717 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
718 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
719 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
720 }
721 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100722
Paul Duffincee7e662020-07-09 17:32:57 +0100723 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100724 `prebuilt_sdklib.stubs`,
725 `prebuilt_sdklib.stubs.source.test`,
726 `prebuilt_sdklib.stubs.system`,
727 `prebuilt_sdklib.stubs.test`,
728 })
729}
730
731func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
732 ctx, _ := testJava(t, `
733 java_sdk_library {
734 name: "sdklib",
735 srcs: ["a.java"],
736 sdk_version: "none",
737 system_modules: "none",
738 public: {
739 enabled: true,
740 },
741 }
742
743 java_sdk_library_import {
744 name: "sdklib",
745 public: {
746 jars: ["a.jar"],
747 },
748 }
749 `)
750
Paul Duffincee7e662020-07-09 17:32:57 +0100751 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100752 `dex2oatd`,
753 `prebuilt_sdklib`,
754 `sdklib.impl`,
755 `sdklib.stubs`,
756 `sdklib.stubs.source`,
757 `sdklib.xml`,
758 })
759
Paul Duffincee7e662020-07-09 17:32:57 +0100760 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100761 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100762 `sdklib.impl`,
763 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
764 // dependency is added after prebuilts may have been renamed and so has to use
765 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100766 `sdklib.xml`,
767 })
768}
769
770func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
771 ctx, _ := testJava(t, `
772 java_sdk_library {
773 name: "sdklib",
774 srcs: ["a.java"],
775 sdk_version: "none",
776 system_modules: "none",
777 public: {
778 enabled: true,
779 },
780 }
781
782 java_sdk_library_import {
783 name: "sdklib",
784 prefer: true,
785 public: {
786 jars: ["a.jar"],
787 },
788 }
789 `)
790
Paul Duffincee7e662020-07-09 17:32:57 +0100791 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100792 `dex2oatd`,
793 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100794 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100795 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100796 `sdklib.stubs.source`,
797 `sdklib.xml`,
798 })
799
Paul Duffincee7e662020-07-09 17:32:57 +0100800 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100801 `prebuilt_sdklib.stubs`,
802 `sdklib.impl`,
803 `sdklib.xml`,
804 })
Paul Duffin56d44902020-01-31 13:36:25 +0000805}
806
JaeMan Parkff715562020-10-19 17:25:58 +0900807func TestJavaSdkLibraryEnforce(t *testing.T) {
808 partitionToBpOption := func(partition string) string {
809 switch partition {
810 case "system":
811 return ""
812 case "vendor":
813 return "soc_specific: true,"
814 case "product":
815 return "product_specific: true,"
816 default:
817 panic("Invalid partition group name: " + partition)
818 }
819 }
820
821 type testConfigInfo struct {
822 libraryType string
823 fromPartition string
824 toPartition string
825 enforceVendorInterface bool
826 enforceProductInterface bool
827 enforceJavaSdkLibraryCheck bool
828 allowList []string
829 }
830
831 createTestConfig := func(info testConfigInfo) android.Config {
832 bpFileTemplate := `
833 java_library {
834 name: "foo",
835 srcs: ["foo.java"],
836 libs: ["bar"],
837 sdk_version: "current",
838 %s
839 }
840
841 %s {
842 name: "bar",
843 srcs: ["bar.java"],
844 sdk_version: "current",
845 %s
846 }
847 `
848
849 bpFile := fmt.Sprintf(bpFileTemplate,
850 partitionToBpOption(info.fromPartition),
851 info.libraryType,
852 partitionToBpOption(info.toPartition))
853
854 config := testConfig(nil, bpFile, nil)
855 configVariables := config.TestProductVariables
856
857 configVariables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
858 if info.enforceVendorInterface {
859 configVariables.DeviceVndkVersion = proptools.StringPtr("current")
860 }
861 configVariables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
862 configVariables.InterPartitionJavaLibraryAllowList = info.allowList
863
864 return config
865 }
866
867 isValidDependency := func(configInfo testConfigInfo) bool {
868 if configInfo.enforceVendorInterface == false {
869 return true
870 }
871
872 if configInfo.enforceJavaSdkLibraryCheck == false {
873 return true
874 }
875
876 if inList("bar", configInfo.allowList) {
877 return true
878 }
879
880 if configInfo.libraryType == "java_library" {
881 if configInfo.fromPartition != configInfo.toPartition {
882 if !configInfo.enforceProductInterface &&
883 ((configInfo.fromPartition == "system" && configInfo.toPartition == "product") ||
884 (configInfo.fromPartition == "product" && configInfo.toPartition == "system")) {
885 return true
886 }
887 return false
888 }
889 }
890
891 return true
892 }
893
894 errorMessage := "is not allowed across the partitions"
895
896 allPartitionCombinations := func() [][2]string {
897 var result [][2]string
898 partitions := []string{"system", "vendor", "product"}
899
900 for _, fromPartition := range partitions {
901 for _, toPartition := range partitions {
902 result = append(result, [2]string{fromPartition, toPartition})
903 }
904 }
905
906 return result
907 }
908
909 allFlagCombinations := func() [][3]bool {
910 var result [][3]bool
911 flagValues := [2]bool{false, true}
912
913 for _, vendorInterface := range flagValues {
914 for _, productInterface := range flagValues {
915 for _, enableEnforce := range flagValues {
916 result = append(result, [3]bool{vendorInterface, productInterface, enableEnforce})
917 }
918 }
919 }
920
921 return result
922 }
923
924 for _, libraryType := range []string{"java_library", "java_sdk_library"} {
925 for _, partitionValues := range allPartitionCombinations() {
926 for _, flagValues := range allFlagCombinations() {
927 testInfo := testConfigInfo{
928 libraryType: libraryType,
929 fromPartition: partitionValues[0],
930 toPartition: partitionValues[1],
931 enforceVendorInterface: flagValues[0],
932 enforceProductInterface: flagValues[1],
933 enforceJavaSdkLibraryCheck: flagValues[2],
934 }
935
936 if isValidDependency(testInfo) {
937 testJavaWithConfig(t, createTestConfig(testInfo))
938 } else {
939 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testInfo))
940 }
941 }
942 }
943 }
944
945 testJavaWithConfig(t, createTestConfig(testConfigInfo{
946 libraryType: "java_library",
947 fromPartition: "vendor",
948 toPartition: "system",
949 enforceVendorInterface: true,
950 enforceProductInterface: true,
951 enforceJavaSdkLibraryCheck: true,
952 allowList: []string{"bar"},
953 }))
954
955 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
956 libraryType: "java_library",
957 fromPartition: "vendor",
958 toPartition: "system",
959 enforceVendorInterface: true,
960 enforceProductInterface: true,
961 enforceJavaSdkLibraryCheck: true,
962 allowList: []string{"foo"},
963 }))
964}
965
Colin Cross89536d42017-07-07 14:35:50 -0700966func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700967 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700968 java_defaults {
969 name: "defaults",
970 srcs: ["a.java"],
971 libs: ["bar"],
972 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700973 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700974 }
975
976 java_library {
977 name: "foo",
978 defaults: ["defaults"],
979 }
980
981 java_library {
982 name: "bar",
983 srcs: ["b.java"],
984 }
985
986 java_library {
987 name: "baz",
988 srcs: ["c.java"],
989 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700990
991 android_test {
992 name: "atestOptimize",
993 defaults: ["defaults"],
994 optimize: {enabled: true},
995 }
996
997 android_test {
998 name: "atestNoOptimize",
999 defaults: ["defaults"],
1000 }
1001
1002 android_test {
1003 name: "atestDefault",
1004 srcs: ["a.java"],
1005 }
Colin Cross89536d42017-07-07 14:35:50 -07001006 `)
1007
Colin Cross4c428df2017-09-15 17:36:05 -07001008 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -07001009 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -07001010
1011 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
1012 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
1013 }
1014
Nan Zhanged19fc32017-10-19 13:06:22 -07001015 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1016 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1017 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -07001018 }
1019
Colin Cross1ee23172017-10-18 14:44:18 -07001020 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -07001021 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
1022 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -07001023 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001024
1025 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1026 if atestOptimize.Output == nil {
1027 t.Errorf("atestOptimize should optimize APK")
1028 }
1029
1030 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1031 if atestNoOptimize.Output == nil {
1032 t.Errorf("atestNoOptimize should not optimize APK")
1033 }
1034
1035 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1036 if atestDefault.Output == nil {
1037 t.Errorf("atestDefault should optimize APK")
1038 }
Colin Cross89536d42017-07-07 14:35:50 -07001039}
1040
Colin Cross0f37af02017-09-27 17:42:05 -07001041func TestResources(t *testing.T) {
1042 var table = []struct {
1043 name string
1044 prop string
1045 extra string
1046 args string
1047 }{
1048 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001049 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001050 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001051 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001052 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001053 },
1054 {
1055 // Test that a module with java_resources includes the files
1056 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001057 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1058 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001059 },
1060 {
1061 // Test that a module with a filegroup in java_resources includes the files with the
1062 // path prefix
1063 name: "resource filegroup",
1064 prop: `java_resources: [":foo-res"]`,
1065 extra: `
1066 filegroup {
1067 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001068 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001069 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001070 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001071 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001072 },
1073 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001074 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1075 name: "wildcard dirs",
1076 prop: `java_resource_dirs: ["java-res/*"]`,
1077 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1078 },
1079 {
1080 // Test that a module exclude_java_resource_dirs excludes the files
1081 name: "wildcard dirs",
1082 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1083 args: "-C java-res/a -f java-res/a/a",
1084 },
Colin Crosscedd4762018-09-13 11:26:19 -07001085 {
1086 // Test wildcards in java_resources
1087 name: "wildcard files",
1088 prop: `java_resources: ["java-res/**/*"]`,
1089 args: "-C . -f java-res/a/a -f java-res/b/b",
1090 },
1091 {
1092 // Test exclude_java_resources with java_resources
1093 name: "wildcard files with exclude",
1094 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1095 args: "-C . -f java-res/a/a",
1096 },
1097 {
1098 // Test exclude_java_resources with java_resource_dirs
1099 name: "resource dirs with exclude files",
1100 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1101 args: "-C java-res -f java-res/a/a",
1102 },
1103 {
1104 // Test exclude_java_resource_dirs with java_resource_dirs
1105 name: "resource dirs with exclude files",
1106 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1107 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1108 },
Colin Cross0f37af02017-09-27 17:42:05 -07001109 }
1110
1111 for _, test := range table {
1112 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001113 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001114 java_library {
1115 name: "foo",
1116 srcs: [
1117 "a.java",
1118 "b.java",
1119 "c.java",
1120 ],
1121 `+test.prop+`,
1122 }
Colin Cross238c1f32020-06-07 16:58:18 -07001123 `+test.extra,
1124 map[string][]byte{
1125 "java-res/a/a": nil,
1126 "java-res/b/b": nil,
1127 "java-res2/a": nil,
1128 },
1129 )
Colin Cross0f37af02017-09-27 17:42:05 -07001130
Colin Cross331a1212018-08-15 20:40:52 -07001131 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001132 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001133
1134 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1135 t.Errorf("foo combined jars %v does not contain %q",
1136 foo.Inputs.Strings(), fooRes.Output.String())
1137 }
1138
Colin Crossaf9c55b2017-10-03 14:50:08 -07001139 if fooRes.Args["jarArgs"] != test.args {
1140 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001141 fooRes.Args["jarArgs"], test.args)
1142 }
1143 })
1144 }
1145}
1146
Colin Cross0c4ce212019-05-03 15:28:19 -07001147func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001148 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001149 java_library {
1150 name: "foo",
1151 srcs: [
1152 "a.java",
1153 "b.java",
1154 "c.java",
1155 ],
1156 include_srcs: true,
1157 }
1158
1159 java_library {
1160 name: "bar",
1161 srcs: [
1162 "a.java",
1163 "b.java",
1164 "c.java",
1165 ],
1166 java_resource_dirs: ["java-res"],
1167 include_srcs: true,
1168 }
Colin Cross238c1f32020-06-07 16:58:18 -07001169 `, map[string][]byte{
1170 "java-res/a/a": nil,
1171 "java-res/b/b": nil,
1172 "java-res2/a": nil,
1173 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001174
1175 // Test a library with include_srcs: true
1176 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1177 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1178
1179 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1180 t.Errorf("foo combined jars %v does not contain %q", w, g)
1181 }
1182
1183 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1184 t.Errorf("foo source jar args %q is not %q", w, g)
1185 }
1186
1187 // Test a library with include_srcs: true and resources
1188 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1189 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1190 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1191 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1192
1193 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1194 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1195 }
1196
1197 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1198 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1199 }
1200
1201 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1202 t.Errorf("bar combined jars %v does not contain %q", w, g)
1203 }
1204
1205 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1206 t.Errorf("bar source jar args %q is not %q", w, g)
1207 }
1208
1209 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1210 t.Errorf("bar resource jar args %q is not %q", w, g)
1211 }
1212}
1213
Colin Cross54190b32017-10-09 15:34:10 -07001214func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001215 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001216 java_library {
1217 name: "foo",
1218 srcs: [
1219 "a*.java",
1220 ":gen",
1221 "b*.java",
1222 ],
1223 }
1224
1225 genrule {
1226 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001227 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001228 out: ["gen.java"],
1229 }
Colin Cross238c1f32020-06-07 16:58:18 -07001230 `, map[string][]byte{
1231 "a.java": nil,
1232 "b.java": nil,
1233 })
Colin Cross54190b32017-10-09 15:34:10 -07001234
1235 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1236 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1237
Colin Cross15e86d92017-10-20 15:07:08 -07001238 if filepath.Base(genrule.Output.String()) != "gen.java" {
1239 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001240 }
1241
1242 if len(javac.Inputs) != 3 ||
1243 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001244 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001245 javac.Inputs[2].String() != "b.java" {
1246 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1247 }
1248}
1249
Nan Zhang61eaedb2017-11-02 13:28:15 -07001250func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001251 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001252 java_library {
1253 name: "foo",
1254 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001255 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001256 }
1257
1258 java_library {
1259 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001260 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001261 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001262 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001263 }
1264
1265 java_library {
1266 name: "baz",
1267 srcs: ["c.java"],
1268 libs: ["bar"],
1269 sdk_version: "14",
1270 }
1271 `)
1272
1273 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1274 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1275 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1276 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1277 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1278
1279 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1280 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1281 }
1282
1283 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1284 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1285 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1286 }
1287 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1288 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1289 }
1290 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1291 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1292 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001293 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001294 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001295 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001296 }
1297}
1298
1299func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001300 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001301 java_library {
1302 name: "bar",
1303 srcs: ["a.java","b.java","c.java"],
1304 javac_shard_size: 1
1305 }
1306 `)
1307
1308 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1309 for i := 0; i < 3; i++ {
1310 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1311 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1312 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1313 }
1314 }
1315}
1316
Nan Zhang581fd212018-01-10 16:06:12 -08001317func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001318 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001319 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001320 name: "droiddoc-templates-sdk",
1321 path: ".",
1322 }
Jiyong Park29074592019-07-07 16:27:47 +09001323 filegroup {
1324 name: "bar-doc-aidl-srcs",
1325 srcs: ["bar-doc/IBar.aidl"],
1326 path: "bar-doc",
1327 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001328 droidstubs {
1329 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001330 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001331 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001332 ],
Steve Kim3666c702020-09-01 17:58:01 +00001333 exclude_srcs: [
1334 "bar-doc/b.java"
1335 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001336 api_levels_annotations_dirs: [
1337 "droiddoc-templates-sdk",
1338 ],
1339 api_levels_annotations_enabled: true,
1340 }
1341 droiddoc {
1342 name: "bar-doc",
1343 srcs: [
1344 ":bar-stubs",
1345 "bar-doc/IFoo.aidl",
1346 ":bar-doc-aidl-srcs",
1347 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001348 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001349 hdf: [
1350 "android.whichdoc offline",
1351 ],
1352 knowntags: [
1353 "bar-doc/known_oj_tags.txt",
1354 ],
1355 proofread_file: "libcore-proofread.txt",
1356 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001357 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001358 }
Colin Cross238c1f32020-06-07 16:58:18 -07001359 `,
1360 map[string][]byte{
1361 "bar-doc/a.java": nil,
1362 "bar-doc/b.java": nil,
1363 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001364 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1365 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1366 if err != nil {
1367 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1368 }
1369 if len(barStubsOutputs) != 1 {
1370 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001371 }
Nan Zhang581fd212018-01-10 16:06:12 -08001372
Liz Kammere1ab2502020-09-10 15:29:25 +00001373 barStubsOutput := barStubsOutputs[0]
1374 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1375 javaDoc := barDoc.Rule("javadoc")
1376 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1377 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001378 }
1379
Liz Kammere1ab2502020-09-10 15:29:25 +00001380 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1381 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1382 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1383 }
1384
1385 aidl := barDoc.Rule("aidl")
1386 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001387 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1388 }
1389
1390 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1391 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001392 }
Nan Zhang581fd212018-01-10 16:06:12 -08001393}
1394
Liz Kammer585cac22020-07-06 09:12:57 -07001395func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1396 testJavaError(t, "flags is set. Cannot set args", `
1397 droiddoc_exported_dir {
1398 name: "droiddoc-templates-sdk",
1399 path: ".",
1400 }
1401 filegroup {
1402 name: "bar-doc-aidl-srcs",
1403 srcs: ["bar-doc/IBar.aidl"],
1404 path: "bar-doc",
1405 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001406 droidstubs {
1407 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001408 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001409 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001410 ],
Steve Kim3666c702020-09-01 17:58:01 +00001411 exclude_srcs: [
1412 "bar-doc/b.java"
1413 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001414 api_levels_annotations_dirs: [
1415 "droiddoc-templates-sdk",
1416 ],
1417 api_levels_annotations_enabled: true,
1418 }
1419 droiddoc {
1420 name: "bar-doc",
1421 srcs: [
1422 ":bar-stubs",
1423 "bar-doc/IFoo.aidl",
1424 ":bar-doc-aidl-srcs",
1425 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001426 custom_template: "droiddoc-templates-sdk",
1427 hdf: [
1428 "android.whichdoc offline",
1429 ],
1430 knowntags: [
1431 "bar-doc/known_oj_tags.txt",
1432 ],
1433 proofread_file: "libcore-proofread.txt",
1434 todo_file: "libcore-docs-todo.html",
1435 flags: ["-offlinemode -title \"libcore\""],
1436 args: "-offlinemode -title \"libcore\"",
1437 }
1438 `)
1439}
1440
Liz Kammer3d894b72020-08-04 09:55:13 -07001441func TestDroidstubs(t *testing.T) {
1442 ctx, _ := testJavaWithFS(t, `
1443 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001444 name: "droiddoc-templates-sdk",
1445 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001446 }
1447
1448 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001449 name: "bar-stubs",
1450 srcs: ["bar-doc/a.java"],
1451 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1452 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001453 }
1454
1455 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001456 name: "bar-stubs-other",
1457 srcs: ["bar-doc/a.java"],
1458 high_mem: true,
1459 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1460 api_levels_annotations_enabled: true,
1461 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001462 }
1463 `,
1464 map[string][]byte{
1465 "bar-doc/a.java": nil,
1466 })
1467 testcases := []struct {
1468 moduleName string
1469 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001470 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001471 }{
1472 {
1473 moduleName: "bar-stubs",
1474 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001475 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001476 },
1477 {
1478 moduleName: "bar-stubs-other",
1479 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001480 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001481 },
1482 }
1483 for _, c := range testcases {
1484 m := ctx.ModuleForTests(c.moduleName, "android_common")
1485 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001486 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001487 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001488 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001489 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1490 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001491
1492 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1493 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1494 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001495 }
1496}
1497
Paul Duffin83a2d962019-11-19 19:44:10 +00001498func TestDroidstubsWithSystemModules(t *testing.T) {
1499 ctx, _ := testJava(t, `
1500 droidstubs {
1501 name: "stubs-source-system-modules",
1502 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001503 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001504 ],
1505 sdk_version: "none",
1506 system_modules: "source-system-modules",
1507 }
1508
1509 java_library {
1510 name: "source-jar",
1511 srcs: [
1512 "a.java",
1513 ],
1514 }
1515
1516 java_system_modules {
1517 name: "source-system-modules",
1518 libs: ["source-jar"],
1519 }
1520
1521 droidstubs {
1522 name: "stubs-prebuilt-system-modules",
1523 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001524 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001525 ],
1526 sdk_version: "none",
1527 system_modules: "prebuilt-system-modules",
1528 }
1529
1530 java_import {
1531 name: "prebuilt-jar",
1532 jars: ["a.jar"],
1533 }
1534
1535 java_system_modules_import {
1536 name: "prebuilt-system-modules",
1537 libs: ["prebuilt-jar"],
1538 }
1539 `)
1540
1541 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1542
1543 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1544}
1545
1546func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1547 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1548 var systemJars []string
1549 for _, i := range metalavaRule.Implicits {
1550 systemJars = append(systemJars, i.Base())
1551 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001552 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001553 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1554 }
1555}
1556
Colin Cross54250902017-12-05 09:28:08 -08001557func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001558 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001559 java_library {
1560 name: "foo",
1561 srcs: ["a.java"],
1562 }
1563
1564 java_genrule {
1565 name: "jargen",
1566 tool_files: ["b.java"],
1567 cmd: "$(location b.java) $(in) $(out)",
1568 out: ["jargen.jar"],
1569 srcs: [":foo"],
1570 }
1571
1572 java_library {
1573 name: "bar",
1574 static_libs: ["jargen"],
1575 srcs: ["c.java"],
1576 }
1577
1578 java_library {
1579 name: "baz",
1580 libs: ["jargen"],
1581 srcs: ["c.java"],
1582 }
1583 `)
1584
1585 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1586 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1587 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1588 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1589 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1590
Colin Cross3d680512020-11-13 16:23:53 -08001591 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1592 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001593 }
1594
1595 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1596 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1597 }
1598
1599 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1600 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1601 }
1602
1603 if len(barCombined.Inputs) != 2 ||
1604 barCombined.Inputs[0].String() != bar.Output.String() ||
1605 barCombined.Inputs[1].String() != jargen.Output.String() {
1606 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1607 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1608 }
1609}
1610
Nan Zhang27e284d2018-02-09 21:03:53 +00001611func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001612 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001613 java_library {
1614 name: "foo",
1615 srcs: ["a.java", ":foo-srcs"],
1616 exclude_srcs: ["a.java", ":foo-excludes"],
1617 }
1618
1619 filegroup {
1620 name: "foo-srcs",
1621 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1622 }
1623
1624 filegroup {
1625 name: "foo-excludes",
1626 srcs: ["java-fg/a.java", "java-fg/b.java"],
1627 }
1628 `)
1629
1630 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1631
1632 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1633 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1634 }
1635}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001636
Paul Duffin52d398a2019-06-11 12:31:14 +01001637func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001638 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001639 "libcore/Android.bp": []byte(`
1640 java_library {
1641 name: "core",
1642 sdk_version: "none",
1643 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001644 }
1645
1646 filegroup {
1647 name: "core-jar",
1648 srcs: [":core{.jar}"],
1649 }
1650`),
1651 })
Colin Crossae8600b2020-10-29 17:09:13 -07001652 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001653 run(t, ctx, config)
1654}
1655
1656func TestJavaImport(t *testing.T) {
1657 config := testConfig(nil, "", map[string][]byte{
1658 "libcore/Android.bp": []byte(`
1659 java_import {
1660 name: "core",
1661 sdk_version: "none",
1662 }
1663
1664 filegroup {
1665 name: "core-jar",
1666 srcs: [":core{.jar}"],
1667 }
1668`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001669 })
Colin Crossae8600b2020-10-29 17:09:13 -07001670 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001671 run(t, ctx, config)
1672}
1673
Jiyong Parkc678ad32018-04-10 13:07:10 +09001674func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001675 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001676 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001677 name: "droiddoc-templates-sdk",
1678 path: ".",
1679 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001680 java_sdk_library {
1681 name: "foo",
1682 srcs: ["a.java", "b.java"],
1683 api_packages: ["foo"],
1684 }
1685 java_sdk_library {
1686 name: "bar",
1687 srcs: ["a.java", "b.java"],
1688 api_packages: ["bar"],
1689 }
1690 java_library {
1691 name: "baz",
1692 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001693 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001694 sdk_version: "system_current",
1695 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001696 java_sdk_library {
1697 name: "barney",
1698 srcs: ["c.java"],
1699 api_only: true,
1700 }
1701 java_sdk_library {
1702 name: "betty",
1703 srcs: ["c.java"],
1704 shared_library: false,
1705 }
Paul Duffin859fe962020-05-15 10:20:31 +01001706 java_sdk_library_import {
1707 name: "quuz",
1708 public: {
1709 jars: ["c.jar"],
1710 },
1711 }
1712 java_sdk_library_import {
1713 name: "fred",
1714 public: {
1715 jars: ["b.jar"],
1716 },
1717 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001718 java_sdk_library_import {
1719 name: "wilma",
1720 public: {
1721 jars: ["b.jar"],
1722 },
1723 shared_library: false,
1724 }
Jiyong Park1be96912018-05-28 18:02:19 +09001725 java_library {
1726 name: "qux",
1727 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001728 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001729 sdk_version: "system_current",
1730 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001731 java_library {
1732 name: "baz-test",
1733 srcs: ["c.java"],
1734 libs: ["foo"],
1735 sdk_version: "test_current",
1736 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001737 java_library {
1738 name: "baz-29",
1739 srcs: ["c.java"],
1740 libs: ["foo"],
1741 sdk_version: "system_29",
1742 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001743 java_library {
1744 name: "baz-module-30",
1745 srcs: ["c.java"],
1746 libs: ["foo"],
1747 sdk_version: "module_30",
1748 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001749 `)
1750
1751 // check the existence of the internal modules
1752 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001753 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1754 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1755 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1756 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1757 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1758 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001759 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001760 ctx.ModuleForTests("foo.api.public.28", "")
1761 ctx.ModuleForTests("foo.api.system.28", "")
1762 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001763
1764 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1765 // tests if baz is actually linked to the stubs lib
1766 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1767 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1768 "foo.stubs.system.jar")
1769 }
1770 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001771 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001772 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001773 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001774 }
1775 // test if baz is not linked to the system variant of foo
1776 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1777 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1778 "foo.stubs.jar")
1779 }
Jiyong Park1be96912018-05-28 18:02:19 +09001780
Paul Duffin726d23c2020-01-22 16:30:37 +00001781 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1782 // tests if baz-test is actually linked to the test stubs lib
1783 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1784 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1785 "foo.stubs.test.jar")
1786 }
1787
Paul Duffina2db18f2020-01-22 17:11:15 +00001788 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1789 // tests if baz-29 is actually linked to the system 29 stubs lib
1790 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1791 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1792 "prebuilts/sdk/29/system/foo.jar")
1793 }
1794
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001795 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1796 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1797 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1798 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1799 "prebuilts/sdk/30/module-lib/foo.jar")
1800 }
1801
Jiyong Park1be96912018-05-28 18:02:19 +09001802 // test if baz has exported SDK lib names foo and bar to qux
1803 qux := ctx.ModuleForTests("qux", "android_common")
1804 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001805 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001806 if w := []string{"foo", "bar", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
Paul Duffin859fe962020-05-15 10:20:31 +01001807 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001808 }
1809 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001810}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001811
Anton Hansson7f66efa2020-10-08 14:47:23 +01001812func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
1813 ctx, _ := testJava(t, `
1814 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001815 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001816 srcs: ["a.java"],
1817 impl_only_libs: ["foo"],
1818 stub_only_libs: ["bar"],
1819 }
1820 java_library {
1821 name: "foo",
1822 srcs: ["a.java"],
1823 sdk_version: "current",
1824 }
1825 java_library {
1826 name: "bar",
1827 srcs: ["a.java"],
1828 sdk_version: "current",
1829 }
1830 `)
1831
Anton Hanssondff2c782020-12-21 17:10:01 +00001832 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Anton Hansson7f66efa2020-10-08 14:47:23 +01001833 implJavacCp := ctx.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
1834 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1835 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1836 }
1837 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001838 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Anton Hansson7f66efa2020-10-08 14:47:23 +01001839 stubsJavacCp := ctx.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
1840 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1841 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1842 }
1843}
1844
Paul Duffindaaa3322020-05-26 18:13:57 +01001845func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1846 ctx, _ := testJava(t, `
1847 java_sdk_library {
1848 name: "foo",
1849 srcs: ["a.java"],
1850 api_only: true,
1851 public: {
1852 enabled: true,
1853 },
1854 }
1855
1856 java_library {
1857 name: "bar",
1858 srcs: ["b.java"],
1859 libs: ["foo"],
1860 }
1861 `)
1862
1863 // The bar library should depend on the stubs jar.
1864 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1865 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1866 t.Errorf("expected %q, found %#q", expected, actual)
1867 }
1868}
1869
Paul Duffin46dc45a2020-05-14 15:39:10 +01001870func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1871 testJava(t, `
1872 java_sdk_library {
1873 name: "foo",
1874 srcs: ["a.java"],
1875 api_packages: ["foo"],
1876 public: {
1877 enabled: true,
1878 },
1879 }
1880
1881 java_library {
1882 name: "bar",
1883 srcs: ["b.java", ":foo{.public.stubs.source}"],
1884 }
1885 `)
1886}
1887
1888func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1889 testJavaError(t, `"foo" does not provide api scope system`, `
1890 java_sdk_library {
1891 name: "foo",
1892 srcs: ["a.java"],
1893 api_packages: ["foo"],
1894 public: {
1895 enabled: true,
1896 },
1897 }
1898
1899 java_library {
1900 name: "bar",
1901 srcs: ["b.java", ":foo{.system.stubs.source}"],
1902 }
1903 `)
1904}
1905
Paul Duffinca8d9a52020-06-26 22:20:25 +01001906func TestJavaSdkLibrary_Deps(t *testing.T) {
1907 ctx, _ := testJava(t, `
1908 java_sdk_library {
1909 name: "sdklib",
1910 srcs: ["a.java"],
1911 sdk_version: "none",
1912 system_modules: "none",
1913 public: {
1914 enabled: true,
1915 },
1916 }
1917 `)
1918
Paul Duffincee7e662020-07-09 17:32:57 +01001919 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001920 `dex2oatd`,
1921 `sdklib.impl`,
1922 `sdklib.stubs`,
1923 `sdklib.stubs.source`,
1924 `sdklib.xml`,
1925 })
1926}
1927
Paul Duffin46dc45a2020-05-14 15:39:10 +01001928func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1929 testJava(t, `
1930 java_sdk_library_import {
1931 name: "foo",
1932 public: {
1933 jars: ["a.jar"],
1934 stub_srcs: ["a.java"],
1935 current_api: "api/current.txt",
1936 removed_api: "api/removed.txt",
1937 },
1938 }
1939
1940 java_library {
1941 name: "bar",
1942 srcs: [":foo{.public.stubs.source}"],
1943 java_resources: [
1944 ":foo{.public.api.txt}",
1945 ":foo{.public.removed-api.txt}",
1946 ],
1947 }
1948 `)
1949}
1950
1951func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1952 bp := `
1953 java_sdk_library_import {
1954 name: "foo",
1955 public: {
1956 jars: ["a.jar"],
1957 },
1958 }
1959 `
1960
1961 t.Run("stubs.source", func(t *testing.T) {
1962 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1963 java_library {
1964 name: "bar",
1965 srcs: [":foo{.public.stubs.source}"],
1966 java_resources: [
1967 ":foo{.public.api.txt}",
1968 ":foo{.public.removed-api.txt}",
1969 ],
1970 }
1971 `)
1972 })
1973
1974 t.Run("api.txt", func(t *testing.T) {
1975 testJavaError(t, `api.txt not available for api scope public`, bp+`
1976 java_library {
1977 name: "bar",
1978 srcs: ["a.java"],
1979 java_resources: [
1980 ":foo{.public.api.txt}",
1981 ],
1982 }
1983 `)
1984 })
1985
1986 t.Run("removed-api.txt", func(t *testing.T) {
1987 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1988 java_library {
1989 name: "bar",
1990 srcs: ["a.java"],
1991 java_resources: [
1992 ":foo{.public.removed-api.txt}",
1993 ],
1994 }
1995 `)
1996 })
1997}
1998
Paul Duffin3375e352020-04-28 10:44:03 +01001999func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
2000 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
2001 java_sdk_library {
2002 name: "foo",
2003 srcs: ["a.java", "b.java"],
2004 api_packages: ["foo"],
2005 // Explicitly disable public to test the check that ensures the set of enabled
2006 // scopes is consistent.
2007 public: {
2008 enabled: false,
2009 },
2010 system: {
2011 enabled: true,
2012 },
2013 }
2014 `)
2015}
2016
Paul Duffin87a05a32020-05-12 11:50:28 +01002017func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
2018 testJava(t, `
2019 java_sdk_library {
2020 name: "foo",
2021 srcs: ["a.java", "b.java"],
2022 api_packages: ["foo"],
2023 system: {
2024 enabled: true,
2025 sdk_version: "module_current",
2026 },
2027 }
2028 `)
2029}
2030
Paul Duffin0c5bae52020-06-02 13:00:08 +01002031func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
2032 testJava(t, `
2033 java_sdk_library {
2034 name: "foo",
2035 srcs: ["a.java", "b.java"],
2036 api_packages: ["foo"],
2037 system: {
2038 enabled: true,
2039 },
2040 module_lib: {
2041 enabled: true,
2042 },
2043 }
2044 `)
2045}
2046
2047func TestJavaSdkLibrary_SystemServer(t *testing.T) {
2048 testJava(t, `
2049 java_sdk_library {
2050 name: "foo",
2051 srcs: ["a.java", "b.java"],
2052 api_packages: ["foo"],
2053 system: {
2054 enabled: true,
2055 },
2056 system_server: {
2057 enabled: true,
2058 },
2059 }
2060 `)
2061}
2062
Paul Duffin803a9562020-05-20 11:52:25 +01002063func TestJavaSdkLibrary_MissingScope(t *testing.T) {
2064 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
2065 java_sdk_library {
2066 name: "foo",
2067 srcs: ["a.java"],
2068 public: {
2069 enabled: false,
2070 },
2071 }
2072
2073 java_library {
2074 name: "baz",
2075 srcs: ["a.java"],
2076 libs: ["foo"],
2077 sdk_version: "module_current",
2078 }
2079 `)
2080}
2081
2082func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
2083 testJava(t, `
2084 java_sdk_library {
2085 name: "foo",
2086 srcs: ["a.java"],
2087 system: {
2088 enabled: true,
2089 },
2090 }
2091
2092 java_library {
2093 name: "baz",
2094 srcs: ["a.java"],
2095 libs: ["foo"],
2096 // foo does not have module-lib scope so it should fallback to system
2097 sdk_version: "module_current",
2098 }
2099 `)
2100}
2101
Jiyong Park932cdfe2020-05-28 00:19:53 +09002102func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
2103 ctx, _ := testJava(t, `
2104 java_sdk_library {
2105 name: "foo",
2106 srcs: ["a.java"],
2107 system: {
2108 enabled: true,
2109 },
2110 default_to_stubs: true,
2111 }
2112
2113 java_library {
2114 name: "baz",
2115 srcs: ["a.java"],
2116 libs: ["foo"],
2117 // does not have sdk_version set, should fallback to module,
2118 // which will then fallback to system because the module scope
2119 // is not enabled.
2120 }
2121 `)
2122 // The baz library should depend on the system stubs jar.
2123 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
2124 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2125 t.Errorf("expected %q, found %#q", expected, actual)
2126 }
2127}
2128
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002129var compilerFlagsTestCases = []struct {
2130 in string
2131 out bool
2132}{
2133 {
2134 in: "a",
2135 out: false,
2136 },
2137 {
2138 in: "-a",
2139 out: true,
2140 },
2141 {
2142 in: "-no-jdk",
2143 out: false,
2144 },
2145 {
2146 in: "-no-stdlib",
2147 out: false,
2148 },
2149 {
2150 in: "-kotlin-home",
2151 out: false,
2152 },
2153 {
2154 in: "-kotlin-home /some/path",
2155 out: false,
2156 },
2157 {
2158 in: "-include-runtime",
2159 out: false,
2160 },
2161 {
2162 in: "-Xintellij-plugin-root",
2163 out: false,
2164 },
2165}
2166
2167type mockContext struct {
2168 android.ModuleContext
2169 result bool
2170}
2171
2172func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2173 // CheckBadCompilerFlags calls this function when the flag should be rejected
2174 ctx.result = false
2175}
2176
2177func TestCompilerFlags(t *testing.T) {
2178 for _, testCase := range compilerFlagsTestCases {
2179 ctx := &mockContext{result: true}
2180 CheckKotlincFlags(ctx, []string{testCase.in})
2181 if ctx.result != testCase.out {
2182 t.Errorf("incorrect output:")
2183 t.Errorf(" input: %#v", testCase.in)
2184 t.Errorf(" expected: %#v", testCase.out)
2185 t.Errorf(" got: %#v", ctx.result)
2186 }
2187 }
2188}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002189
2190// TODO(jungjw): Consider making this more robust by ignoring path order.
2191func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2192 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2193 flags := strings.Split(variables["javacFlags"], " ")
2194 got := ""
2195 for _, flag := range flags {
2196 keyEnd := strings.Index(flag, "=")
2197 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2198 got = flag[keyEnd+1:]
2199 break
2200 }
2201 }
2202 if expected != got {
2203 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2204 }
2205}
2206
2207func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002208 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002209 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002210 bp := `
2211 java_library {
2212 name: "foo",
2213 srcs: ["a.java"],
2214 java_version: "1.8",
2215 }
2216
2217 java_library {
2218 name: "bar",
2219 srcs: ["b.java"],
2220 sdk_version: "none",
2221 system_modules: "none",
2222 patch_module: "java.base",
2223 java_version: "1.8",
2224 }
2225
2226 java_library {
2227 name: "baz",
2228 srcs: ["c.java"],
2229 patch_module: "java.base",
2230 java_version: "1.8",
2231 }
2232 `
2233 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002234
2235 checkPatchModuleFlag(t, ctx, "foo", "")
2236 checkPatchModuleFlag(t, ctx, "bar", "")
2237 checkPatchModuleFlag(t, ctx, "baz", "")
2238 })
2239
Pete Gillin0c2143e2019-05-02 15:32:11 +01002240 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002241 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002242 bp := `
2243 java_library {
2244 name: "foo",
2245 srcs: ["a.java"],
2246 }
2247
2248 java_library {
2249 name: "bar",
2250 srcs: ["b.java"],
2251 sdk_version: "none",
2252 system_modules: "none",
2253 patch_module: "java.base",
2254 }
2255
2256 java_library {
2257 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002258 srcs: [
2259 "c.java",
2260 // Tests for b/150878007
2261 "dir/d.java",
2262 "dir2/e.java",
2263 "dir2/f.java",
2264 "nested/dir/g.java"
2265 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002266 patch_module: "java.base",
2267 }
2268 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002269 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002270
2271 checkPatchModuleFlag(t, ctx, "foo", "")
2272 expected := "java.base=.:" + buildDir
2273 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002274 expected = "java.base=" + strings.Join([]string{
2275 ".", buildDir, "dir", "dir2", "nested", moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002276 checkPatchModuleFlag(t, ctx, "baz", expected)
2277 })
2278}
Paul Duffina7b9f422020-01-10 17:12:18 +00002279
2280func TestJavaSystemModules(t *testing.T) {
2281 ctx, _ := testJava(t, `
2282 java_system_modules {
2283 name: "system-modules",
2284 libs: ["system-module1", "system-module2"],
2285 }
2286 java_library {
2287 name: "system-module1",
2288 srcs: ["a.java"],
2289 sdk_version: "none",
2290 system_modules: "none",
2291 }
2292 java_library {
2293 name: "system-module2",
2294 srcs: ["b.java"],
2295 sdk_version: "none",
2296 system_modules: "none",
2297 }
2298 `)
2299
2300 // check the existence of the module
2301 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2302
2303 cmd := systemModules.Rule("jarsTosystemModules")
2304
2305 // make sure the command compiles against the supplied modules.
2306 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2307 if !strings.Contains(cmd.Args["classpath"], module) {
2308 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2309 module)
2310 }
2311 }
2312}
Paul Duffin90169ba2020-01-10 17:16:44 +00002313
2314func TestJavaSystemModulesImport(t *testing.T) {
2315 ctx, _ := testJava(t, `
2316 java_system_modules_import {
2317 name: "system-modules",
2318 libs: ["system-module1", "system-module2"],
2319 }
2320 java_import {
2321 name: "system-module1",
2322 jars: ["a.jar"],
2323 }
2324 java_import {
2325 name: "system-module2",
2326 jars: ["b.jar"],
2327 }
2328 `)
2329
2330 // check the existence of the module
2331 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2332
2333 cmd := systemModules.Rule("jarsTosystemModules")
2334
2335 // make sure the command compiles against the supplied modules.
2336 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2337 if !strings.Contains(cmd.Args["classpath"], module) {
2338 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2339 module)
2340 }
2341 }
2342}
Paul Duffin83a2d962019-11-19 19:44:10 +00002343
2344func TestJavaLibraryWithSystemModules(t *testing.T) {
2345 ctx, _ := testJava(t, `
2346 java_library {
2347 name: "lib-with-source-system-modules",
2348 srcs: [
2349 "a.java",
2350 ],
2351 sdk_version: "none",
2352 system_modules: "source-system-modules",
2353 }
2354
2355 java_library {
2356 name: "source-jar",
2357 srcs: [
2358 "a.java",
2359 ],
2360 }
2361
2362 java_system_modules {
2363 name: "source-system-modules",
2364 libs: ["source-jar"],
2365 }
2366
2367 java_library {
2368 name: "lib-with-prebuilt-system-modules",
2369 srcs: [
2370 "a.java",
2371 ],
2372 sdk_version: "none",
2373 system_modules: "prebuilt-system-modules",
2374 }
2375
2376 java_import {
2377 name: "prebuilt-jar",
2378 jars: ["a.jar"],
2379 }
2380
2381 java_system_modules_import {
2382 name: "prebuilt-system-modules",
2383 libs: ["prebuilt-jar"],
2384 }
2385 `)
2386
2387 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2388
2389 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2390}
2391
2392func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2393 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2394 bootClasspath := javacRule.Args["bootClasspath"]
2395 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2396 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2397 }
2398}
Jiyong Park19604de2020-03-24 16:44:11 +09002399
2400func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2401 ctx, _ := testJava(t, `
2402 java_library {
2403 name: "foo",
2404 srcs: ["aidl/foo/IFoo.aidl"],
2405 libs: ["bar"],
2406 }
2407
2408 java_import {
2409 name: "bar",
2410 jars: ["a.jar"],
2411 aidl: {
2412 export_include_dirs: ["aidl/bar"],
2413 },
2414 }
2415 `)
2416
2417 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2418 expectedAidlFlag := "-Iaidl/bar"
2419 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2420 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2421 }
2422}
Liz Kammerdd849a82020-06-12 16:38:45 -07002423
2424func TestDataNativeBinaries(t *testing.T) {
2425 ctx, config := testJava(t, `
2426 java_test_host {
2427 name: "foo",
2428 srcs: ["a.java"],
2429 data_native_bins: ["bin"]
2430 }
2431
2432 python_binary_host {
2433 name: "bin",
2434 srcs: ["bin.py"],
2435 }
2436 `)
2437
2438 buildOS := android.BuildOs.String()
2439
2440 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2441 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
2442 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2443 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2444 if !reflect.DeepEqual(expected, actual) {
2445 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2446 }
2447}