blob: 2dcf129cfab16fb96ecf0e9d10994d94874cad02 [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Colin Cross72bb3632017-07-13 16:23:21 -070018 "io/ioutil"
19 "os"
20 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070021 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010022 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070023 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070024 "strings"
25 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070026
Jeongik Cha28df2572019-11-11 10:46:36 +090027 "github.com/google/blueprint/proptools"
28
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/android"
30 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080031 "android/soong/dexpreopt"
Liz Kammerdd849a82020-06-12 16:38:45 -070032 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070033)
34
35var buildDir string
36
37func setUp() {
38 var err error
39 buildDir, err = ioutil.TempDir("", "soong_java_test")
40 if err != nil {
41 panic(err)
42 }
43}
44
45func tearDown() {
46 os.RemoveAll(buildDir)
47}
48
49func TestMain(m *testing.M) {
50 run := func() int {
51 setUp()
52 defer tearDown()
53
54 return m.Run()
55 }
56
57 os.Exit(run())
58}
Colin Cross527012a2017-11-30 22:56:16 -080059
Colin Cross98be1bb2019-12-13 20:41:13 -080060func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000061 bp += dexpreopt.BpToolModulesForTest()
62
Martin Stjernholm75a48d82020-01-10 20:32:59 +000063 config := TestConfig(buildDir, env, bp, fs)
64
65 // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that
66 // it doesn't create a real one, which would fail.
67 _ = dexpreopt.GlobalSoongConfigForTests(config)
68
69 return config
Colin Cross1369cdb2017-09-29 17:58:17 -070070}
71
Colin Crossae8600b2020-10-29 17:09:13 -070072func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070073
Colin Crossae8600b2020-10-29 17:09:13 -070074 ctx := android.NewTestArchContext(config)
Paul Duffinf9b1da02019-12-18 19:51:55 +000075 RegisterJavaBuildComponents(ctx)
76 RegisterAppBuildComponents(ctx)
77 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000078 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000079 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080080 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080081 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070082 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000083 RegisterDocsBuildComponents(ctx)
84 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000085 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070086 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010087 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000088
89 RegisterPrebuiltApisBuildComponents(ctx)
90
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 Cross3bc7ffa2017-11-22 16:19:37 -080093 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070094 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(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 {
315 library string
316 processors string
317 }
318 var tests = []struct {
319 name string
320 extra string
321 results []Result
322 }{
323 {
324 name: "Exported plugin is not a direct plugin",
325 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
326 results: []Result{{library: "exports", processors: "-proc:none"}},
327 },
328 {
329 name: "Exports plugin to dependee",
330 extra: `
331 java_library{name: "exports", exported_plugins: ["plugin"]}
332 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
333 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
334 `,
335 results: []Result{
336 {library: "foo", processors: "-processor com.android.TestPlugin"},
337 {library: "bar", processors: "-processor com.android.TestPlugin"},
338 },
339 },
340 {
341 name: "Exports plugin to android_library",
342 extra: `
343 java_library{name: "exports", exported_plugins: ["plugin"]}
344 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
345 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
346 `,
347 results: []Result{
348 {library: "foo", processors: "-processor com.android.TestPlugin"},
349 {library: "bar", processors: "-processor com.android.TestPlugin"},
350 },
351 },
352 {
353 name: "Exports plugin is not propagated via transitive deps",
354 extra: `
355 java_library{name: "exports", exported_plugins: ["plugin"]}
356 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
357 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
358 `,
359 results: []Result{
360 {library: "foo", processors: "-processor com.android.TestPlugin"},
361 {library: "bar", processors: "-proc:none"},
362 },
363 },
364 {
365 name: "Exports plugin appends to plugins",
366 extra: `
367 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
368 java_library{name: "exports", exported_plugins: ["plugin"]}
369 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
370 `,
371 results: []Result{
372 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
373 },
374 },
375 }
376
377 for _, test := range tests {
378 t.Run(test.name, func(t *testing.T) {
379 ctx, _ := testJava(t, `
380 java_plugin {
381 name: "plugin",
382 processor_class: "com.android.TestPlugin",
383 }
384 `+test.extra)
385
386 for _, want := range test.results {
387 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
388 if javac.Args["processor"] != want.processors {
389 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
390 }
391 }
392 })
393 }
394}
395
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900396func TestSdkVersionByPartition(t *testing.T) {
397 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900398 java_library {
399 name: "foo",
400 srcs: ["a.java"],
401 vendor: true,
402 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900403 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900404
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900405 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900406 java_library {
407 name: "bar",
408 srcs: ["b.java"],
409 }
410 `)
411
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900412 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900413 bp := `
414 java_library {
415 name: "foo",
416 srcs: ["a.java"],
417 product_specific: true,
418 }
419 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800420
421 config := testConfig(nil, bp, nil)
422 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900423 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800424 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 +0900425 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800426 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900427 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900428 }
429}
430
Colin Crossd5934c82017-10-02 13:55:26 -0700431func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700432 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700433 java_library {
434 name: "foo",
435 srcs: ["a.java"],
436 target: {
437 android: {
438 srcs: ["b.java"],
439 },
440 },
441 }
442 `)
443
444 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
445 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
446 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
447 }
448}
449
Colin Cross6b4a32d2017-12-05 13:42:45 -0800450func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700451 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800452 java_library_host {
453 name: "foo",
454 srcs: ["a.java"],
455 }
456
457 java_binary_host {
458 name: "bar",
459 srcs: ["b.java"],
460 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700461 jni_libs: ["libjni"],
462 }
463
464 cc_library_shared {
465 name: "libjni",
466 host_supported: true,
467 device_supported: false,
468 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800469 }
470 `)
471
472 buildOS := android.BuildOs.String()
473
474 bar := ctx.ModuleForTests("bar", buildOS+"_common")
475 barJar := bar.Output("bar.jar").Output.String()
476 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
477 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
478
Colin Cross89226d92020-10-09 19:00:54 -0700479 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
480 libjniSO := libjni.Rule("Cp").Output.String()
481
Colin Cross6b4a32d2017-12-05 13:42:45 -0800482 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700483 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
484 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800485 }
Colin Cross89226d92020-10-09 19:00:54 -0700486
487 // Test that the install binary wrapper depends on the installed JNI libraries
488 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
489 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800490 }
Alex Humesky2070e322020-06-09 20:23:08 -0400491}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800492
Alex Humesky2070e322020-06-09 20:23:08 -0400493func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
494 bp := `
495 java_library {
496 name: "target_library",
497 srcs: ["a.java"],
498 }
499
500 java_binary_host {
501 name: "host_binary",
502 srcs: ["b.java"],
503 }
504 `
505 config := testConfig(nil, bp, nil)
506 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
507
508 ctx, _ := testJavaWithConfig(t, config)
509
Liz Kammer7941b302020-07-28 13:27:34 -0700510 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400511 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
512 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
513 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
514 t.Errorf("target library javac flags %v should contain "+
515 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
516 }
517
518 // check that -g is not overridden for host modules
519 buildOS := android.BuildOs.String()
520 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
521 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
522 if strings.Contains(hostJavaFlags, "-g:source,lines") {
523 t.Errorf("java_binary_host javac flags %v should not have "+
524 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
525 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800526}
527
Colin Cross72bb3632017-07-13 16:23:21 -0700528func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700529 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700530 java_library {
531 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000532 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000533 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700534 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700535 }
536
Colin Cross74d73e22017-08-02 11:05:49 -0700537 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700538 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700539 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700540 }
541
Colin Cross74d73e22017-08-02 11:05:49 -0700542 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700543 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700544 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700545 sdk_version: "current",
546 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700547 }
Colin Cross42be7612019-02-21 18:12:14 -0800548
549 dex_import {
550 name: "qux",
551 jars: ["b.jar"],
552 }
Colin Cross79c7c262019-04-17 11:11:46 -0700553
554 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000555 name: "sdklib",
556 public: {
557 jars: ["c.jar"],
558 },
559 }
560
Paul Duffin91547182019-11-12 19:39:36 +0000561 prebuilt_stubs_sources {
562 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000563 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000564 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000565
566 java_test_import {
567 name: "test",
568 jars: ["a.jar"],
569 test_suites: ["cts"],
570 test_config: "AndroidTest.xml",
571 }
Colin Cross72bb3632017-07-13 16:23:21 -0700572 `)
573
Paul Duffin9b478b02019-12-10 13:41:51 +0000574 fooModule := ctx.ModuleForTests("foo", "android_common")
575 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700576 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700577 barModule := ctx.ModuleForTests("bar", "android_common")
578 barJar := barModule.Rule("combineJar").Output
579 bazModule := ctx.ModuleForTests("baz", "android_common")
580 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700581 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700582
Paul Duffin9b478b02019-12-10 13:41:51 +0000583 fooLibrary := fooModule.Module().(*Library)
584 assertDeepEquals(t, "foo java sources incorrect",
585 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000586
Paul Duffin9b478b02019-12-10 13:41:51 +0000587 assertDeepEquals(t, "foo java source jars incorrect",
588 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
589 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000590
Colin Cross37f6d792018-07-12 12:28:41 -0700591 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
592 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700593 }
594
Liz Kammerd6c31d22020-08-05 15:40:41 -0700595 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
596 if barDexJar != nil {
597 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
598 }
599
Colin Cross79c7c262019-04-17 11:11:46 -0700600 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
601 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
602 }
603
Colin Cross37f6d792018-07-12 12:28:41 -0700604 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
605 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700606 }
Colin Cross42be7612019-02-21 18:12:14 -0800607
Liz Kammerd6c31d22020-08-05 15:40:41 -0700608 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
609 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
610 if bazDexJar != expectedDexJar {
611 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
612 }
613
Colin Cross42be7612019-02-21 18:12:14 -0800614 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700615}
616
Paul Duffin9b478b02019-12-10 13:41:51 +0000617func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
618 if !reflect.DeepEqual(expected, actual) {
619 t.Errorf("%s: expected %q, found %q", message, expected, actual)
620 }
621}
622
Paul Duffin56d44902020-01-31 13:36:25 +0000623func TestJavaSdkLibraryImport(t *testing.T) {
624 ctx, _ := testJava(t, `
625 java_library {
626 name: "foo",
627 srcs: ["a.java"],
628 libs: ["sdklib"],
629 sdk_version: "current",
630 }
631
632 java_library {
633 name: "foo.system",
634 srcs: ["a.java"],
635 libs: ["sdklib"],
636 sdk_version: "system_current",
637 }
638
639 java_library {
640 name: "foo.test",
641 srcs: ["a.java"],
642 libs: ["sdklib"],
643 sdk_version: "test_current",
644 }
645
646 java_sdk_library_import {
647 name: "sdklib",
648 public: {
649 jars: ["a.jar"],
650 },
651 system: {
652 jars: ["b.jar"],
653 },
654 test: {
655 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100656 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000657 },
658 }
659 `)
660
661 for _, scope := range []string{"", ".system", ".test"} {
662 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
663 javac := fooModule.Rule("javac")
664
665 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
666 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
667 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
668 }
669 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100670
Paul Duffincee7e662020-07-09 17:32:57 +0100671 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100672 `prebuilt_sdklib.stubs`,
673 `prebuilt_sdklib.stubs.source.test`,
674 `prebuilt_sdklib.stubs.system`,
675 `prebuilt_sdklib.stubs.test`,
676 })
677}
678
679func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
680 ctx, _ := testJava(t, `
681 java_sdk_library {
682 name: "sdklib",
683 srcs: ["a.java"],
684 sdk_version: "none",
685 system_modules: "none",
686 public: {
687 enabled: true,
688 },
689 }
690
691 java_sdk_library_import {
692 name: "sdklib",
693 public: {
694 jars: ["a.jar"],
695 },
696 }
697 `)
698
Paul Duffincee7e662020-07-09 17:32:57 +0100699 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100700 `dex2oatd`,
701 `prebuilt_sdklib`,
702 `sdklib.impl`,
703 `sdklib.stubs`,
704 `sdklib.stubs.source`,
705 `sdklib.xml`,
706 })
707
Paul Duffincee7e662020-07-09 17:32:57 +0100708 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100709 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100710 `sdklib.impl`,
711 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
712 // dependency is added after prebuilts may have been renamed and so has to use
713 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100714 `sdklib.xml`,
715 })
716}
717
718func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
719 ctx, _ := testJava(t, `
720 java_sdk_library {
721 name: "sdklib",
722 srcs: ["a.java"],
723 sdk_version: "none",
724 system_modules: "none",
725 public: {
726 enabled: true,
727 },
728 }
729
730 java_sdk_library_import {
731 name: "sdklib",
732 prefer: true,
733 public: {
734 jars: ["a.jar"],
735 },
736 }
737 `)
738
Paul Duffincee7e662020-07-09 17:32:57 +0100739 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100740 `dex2oatd`,
741 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100742 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100743 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100744 `sdklib.stubs.source`,
745 `sdklib.xml`,
746 })
747
Paul Duffincee7e662020-07-09 17:32:57 +0100748 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100749 `prebuilt_sdklib.stubs`,
750 `sdklib.impl`,
751 `sdklib.xml`,
752 })
Paul Duffin56d44902020-01-31 13:36:25 +0000753}
754
Colin Cross89536d42017-07-07 14:35:50 -0700755func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700756 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700757 java_defaults {
758 name: "defaults",
759 srcs: ["a.java"],
760 libs: ["bar"],
761 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700762 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700763 }
764
765 java_library {
766 name: "foo",
767 defaults: ["defaults"],
768 }
769
770 java_library {
771 name: "bar",
772 srcs: ["b.java"],
773 }
774
775 java_library {
776 name: "baz",
777 srcs: ["c.java"],
778 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700779
780 android_test {
781 name: "atestOptimize",
782 defaults: ["defaults"],
783 optimize: {enabled: true},
784 }
785
786 android_test {
787 name: "atestNoOptimize",
788 defaults: ["defaults"],
789 }
790
791 android_test {
792 name: "atestDefault",
793 srcs: ["a.java"],
794 }
Colin Cross89536d42017-07-07 14:35:50 -0700795 `)
796
Colin Cross4c428df2017-09-15 17:36:05 -0700797 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700798 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700799
800 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
801 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
802 }
803
Nan Zhanged19fc32017-10-19 13:06:22 -0700804 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
805 if !strings.Contains(javac.Args["classpath"], barTurbine) {
806 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700807 }
808
Colin Cross1ee23172017-10-18 14:44:18 -0700809 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700810 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
811 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700812 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700813
814 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
815 if atestOptimize.Output == nil {
816 t.Errorf("atestOptimize should optimize APK")
817 }
818
819 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
820 if atestNoOptimize.Output == nil {
821 t.Errorf("atestNoOptimize should not optimize APK")
822 }
823
824 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
825 if atestDefault.Output == nil {
826 t.Errorf("atestDefault should optimize APK")
827 }
Colin Cross89536d42017-07-07 14:35:50 -0700828}
829
Colin Cross0f37af02017-09-27 17:42:05 -0700830func TestResources(t *testing.T) {
831 var table = []struct {
832 name string
833 prop string
834 extra string
835 args string
836 }{
837 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700838 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700839 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800840 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700841 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700842 },
843 {
844 // Test that a module with java_resources includes the files
845 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700846 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
847 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700848 },
849 {
850 // Test that a module with a filegroup in java_resources includes the files with the
851 // path prefix
852 name: "resource filegroup",
853 prop: `java_resources: [":foo-res"]`,
854 extra: `
855 filegroup {
856 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800857 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700858 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700859 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700860 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700861 },
862 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700863 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
864 name: "wildcard dirs",
865 prop: `java_resource_dirs: ["java-res/*"]`,
866 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
867 },
868 {
869 // Test that a module exclude_java_resource_dirs excludes the files
870 name: "wildcard dirs",
871 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
872 args: "-C java-res/a -f java-res/a/a",
873 },
Colin Crosscedd4762018-09-13 11:26:19 -0700874 {
875 // Test wildcards in java_resources
876 name: "wildcard files",
877 prop: `java_resources: ["java-res/**/*"]`,
878 args: "-C . -f java-res/a/a -f java-res/b/b",
879 },
880 {
881 // Test exclude_java_resources with java_resources
882 name: "wildcard files with exclude",
883 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
884 args: "-C . -f java-res/a/a",
885 },
886 {
887 // Test exclude_java_resources with java_resource_dirs
888 name: "resource dirs with exclude files",
889 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
890 args: "-C java-res -f java-res/a/a",
891 },
892 {
893 // Test exclude_java_resource_dirs with java_resource_dirs
894 name: "resource dirs with exclude files",
895 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
896 args: "-C java-res -f java-res/a/a -f java-res/b/b",
897 },
Colin Cross0f37af02017-09-27 17:42:05 -0700898 }
899
900 for _, test := range table {
901 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700902 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700903 java_library {
904 name: "foo",
905 srcs: [
906 "a.java",
907 "b.java",
908 "c.java",
909 ],
910 `+test.prop+`,
911 }
Colin Cross238c1f32020-06-07 16:58:18 -0700912 `+test.extra,
913 map[string][]byte{
914 "java-res/a/a": nil,
915 "java-res/b/b": nil,
916 "java-res2/a": nil,
917 },
918 )
Colin Cross0f37af02017-09-27 17:42:05 -0700919
Colin Cross331a1212018-08-15 20:40:52 -0700920 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700921 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700922
923 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
924 t.Errorf("foo combined jars %v does not contain %q",
925 foo.Inputs.Strings(), fooRes.Output.String())
926 }
927
Colin Crossaf9c55b2017-10-03 14:50:08 -0700928 if fooRes.Args["jarArgs"] != test.args {
929 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700930 fooRes.Args["jarArgs"], test.args)
931 }
932 })
933 }
934}
935
Colin Cross0c4ce212019-05-03 15:28:19 -0700936func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700937 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700938 java_library {
939 name: "foo",
940 srcs: [
941 "a.java",
942 "b.java",
943 "c.java",
944 ],
945 include_srcs: true,
946 }
947
948 java_library {
949 name: "bar",
950 srcs: [
951 "a.java",
952 "b.java",
953 "c.java",
954 ],
955 java_resource_dirs: ["java-res"],
956 include_srcs: true,
957 }
Colin Cross238c1f32020-06-07 16:58:18 -0700958 `, map[string][]byte{
959 "java-res/a/a": nil,
960 "java-res/b/b": nil,
961 "java-res2/a": nil,
962 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700963
964 // Test a library with include_srcs: true
965 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
966 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
967
968 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
969 t.Errorf("foo combined jars %v does not contain %q", w, g)
970 }
971
972 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
973 t.Errorf("foo source jar args %q is not %q", w, g)
974 }
975
976 // Test a library with include_srcs: true and resources
977 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
978 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
979 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
980 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
981
982 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
983 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
984 }
985
986 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
987 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
988 }
989
990 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
991 t.Errorf("bar combined jars %v does not contain %q", w, g)
992 }
993
994 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
995 t.Errorf("bar source jar args %q is not %q", w, g)
996 }
997
998 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
999 t.Errorf("bar resource jar args %q is not %q", w, g)
1000 }
1001}
1002
Colin Cross54190b32017-10-09 15:34:10 -07001003func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001004 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001005 java_library {
1006 name: "foo",
1007 srcs: [
1008 "a*.java",
1009 ":gen",
1010 "b*.java",
1011 ],
1012 }
1013
1014 genrule {
1015 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001016 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001017 out: ["gen.java"],
1018 }
Colin Cross238c1f32020-06-07 16:58:18 -07001019 `, map[string][]byte{
1020 "a.java": nil,
1021 "b.java": nil,
1022 })
Colin Cross54190b32017-10-09 15:34:10 -07001023
1024 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1025 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1026
Colin Cross15e86d92017-10-20 15:07:08 -07001027 if filepath.Base(genrule.Output.String()) != "gen.java" {
1028 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001029 }
1030
1031 if len(javac.Inputs) != 3 ||
1032 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001033 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001034 javac.Inputs[2].String() != "b.java" {
1035 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1036 }
1037}
1038
Nan Zhang61eaedb2017-11-02 13:28:15 -07001039func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001040 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001041 java_library {
1042 name: "foo",
1043 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001044 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001045 }
1046
1047 java_library {
1048 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001049 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001050 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001051 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001052 }
1053
1054 java_library {
1055 name: "baz",
1056 srcs: ["c.java"],
1057 libs: ["bar"],
1058 sdk_version: "14",
1059 }
1060 `)
1061
1062 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1063 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1064 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1065 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1066 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1067
1068 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1069 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1070 }
1071
1072 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1073 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1074 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1075 }
1076 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1077 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1078 }
1079 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1080 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1081 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001082 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001083 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001084 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001085 }
1086}
1087
1088func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001089 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001090 java_library {
1091 name: "bar",
1092 srcs: ["a.java","b.java","c.java"],
1093 javac_shard_size: 1
1094 }
1095 `)
1096
1097 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1098 for i := 0; i < 3; i++ {
1099 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1100 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1101 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1102 }
1103 }
1104}
1105
Nan Zhang581fd212018-01-10 16:06:12 -08001106func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001107 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001108 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001109 name: "droiddoc-templates-sdk",
1110 path: ".",
1111 }
Jiyong Park29074592019-07-07 16:27:47 +09001112 filegroup {
1113 name: "bar-doc-aidl-srcs",
1114 srcs: ["bar-doc/IBar.aidl"],
1115 path: "bar-doc",
1116 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001117 droidstubs {
1118 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001119 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001120 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001121 ],
Steve Kim3666c702020-09-01 17:58:01 +00001122 exclude_srcs: [
1123 "bar-doc/b.java"
1124 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001125 api_levels_annotations_dirs: [
1126 "droiddoc-templates-sdk",
1127 ],
1128 api_levels_annotations_enabled: true,
1129 }
1130 droiddoc {
1131 name: "bar-doc",
1132 srcs: [
1133 ":bar-stubs",
1134 "bar-doc/IFoo.aidl",
1135 ":bar-doc-aidl-srcs",
1136 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001137 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001138 hdf: [
1139 "android.whichdoc offline",
1140 ],
1141 knowntags: [
1142 "bar-doc/known_oj_tags.txt",
1143 ],
1144 proofread_file: "libcore-proofread.txt",
1145 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001146 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001147 }
Colin Cross238c1f32020-06-07 16:58:18 -07001148 `,
1149 map[string][]byte{
1150 "bar-doc/a.java": nil,
1151 "bar-doc/b.java": nil,
1152 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001153 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1154 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1155 if err != nil {
1156 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1157 }
1158 if len(barStubsOutputs) != 1 {
1159 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001160 }
Nan Zhang581fd212018-01-10 16:06:12 -08001161
Liz Kammere1ab2502020-09-10 15:29:25 +00001162 barStubsOutput := barStubsOutputs[0]
1163 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1164 javaDoc := barDoc.Rule("javadoc")
1165 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1166 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001167 }
1168
Liz Kammere1ab2502020-09-10 15:29:25 +00001169 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1170 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1171 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1172 }
1173
1174 aidl := barDoc.Rule("aidl")
1175 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001176 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1177 }
1178
1179 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1180 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001181 }
Nan Zhang581fd212018-01-10 16:06:12 -08001182}
1183
Liz Kammer585cac22020-07-06 09:12:57 -07001184func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1185 testJavaError(t, "flags is set. Cannot set args", `
1186 droiddoc_exported_dir {
1187 name: "droiddoc-templates-sdk",
1188 path: ".",
1189 }
1190 filegroup {
1191 name: "bar-doc-aidl-srcs",
1192 srcs: ["bar-doc/IBar.aidl"],
1193 path: "bar-doc",
1194 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001195 droidstubs {
1196 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001197 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001198 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001199 ],
Steve Kim3666c702020-09-01 17:58:01 +00001200 exclude_srcs: [
1201 "bar-doc/b.java"
1202 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001203 api_levels_annotations_dirs: [
1204 "droiddoc-templates-sdk",
1205 ],
1206 api_levels_annotations_enabled: true,
1207 }
1208 droiddoc {
1209 name: "bar-doc",
1210 srcs: [
1211 ":bar-stubs",
1212 "bar-doc/IFoo.aidl",
1213 ":bar-doc-aidl-srcs",
1214 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001215 custom_template: "droiddoc-templates-sdk",
1216 hdf: [
1217 "android.whichdoc offline",
1218 ],
1219 knowntags: [
1220 "bar-doc/known_oj_tags.txt",
1221 ],
1222 proofread_file: "libcore-proofread.txt",
1223 todo_file: "libcore-docs-todo.html",
1224 flags: ["-offlinemode -title \"libcore\""],
1225 args: "-offlinemode -title \"libcore\"",
1226 }
1227 `)
1228}
1229
Liz Kammer3d894b72020-08-04 09:55:13 -07001230func TestDroidstubs(t *testing.T) {
1231 ctx, _ := testJavaWithFS(t, `
1232 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001233 name: "droiddoc-templates-sdk",
1234 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001235 }
1236
1237 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001238 name: "bar-stubs",
1239 srcs: ["bar-doc/a.java"],
1240 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1241 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001242 }
1243
1244 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001245 name: "bar-stubs-other",
1246 srcs: ["bar-doc/a.java"],
1247 high_mem: true,
1248 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1249 api_levels_annotations_enabled: true,
1250 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001251 }
1252 `,
1253 map[string][]byte{
1254 "bar-doc/a.java": nil,
1255 })
1256 testcases := []struct {
1257 moduleName string
1258 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001259 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001260 }{
1261 {
1262 moduleName: "bar-stubs",
1263 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001264 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001265 },
1266 {
1267 moduleName: "bar-stubs-other",
1268 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001269 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001270 },
1271 }
1272 for _, c := range testcases {
1273 m := ctx.ModuleForTests(c.moduleName, "android_common")
1274 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001275 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001276 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001277 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001278 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1279 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001280
1281 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1282 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1283 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001284 }
1285}
1286
Paul Duffin83a2d962019-11-19 19:44:10 +00001287func TestDroidstubsWithSystemModules(t *testing.T) {
1288 ctx, _ := testJava(t, `
1289 droidstubs {
1290 name: "stubs-source-system-modules",
1291 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001292 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001293 ],
1294 sdk_version: "none",
1295 system_modules: "source-system-modules",
1296 }
1297
1298 java_library {
1299 name: "source-jar",
1300 srcs: [
1301 "a.java",
1302 ],
1303 }
1304
1305 java_system_modules {
1306 name: "source-system-modules",
1307 libs: ["source-jar"],
1308 }
1309
1310 droidstubs {
1311 name: "stubs-prebuilt-system-modules",
1312 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001313 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001314 ],
1315 sdk_version: "none",
1316 system_modules: "prebuilt-system-modules",
1317 }
1318
1319 java_import {
1320 name: "prebuilt-jar",
1321 jars: ["a.jar"],
1322 }
1323
1324 java_system_modules_import {
1325 name: "prebuilt-system-modules",
1326 libs: ["prebuilt-jar"],
1327 }
1328 `)
1329
1330 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1331
1332 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1333}
1334
1335func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1336 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1337 var systemJars []string
1338 for _, i := range metalavaRule.Implicits {
1339 systemJars = append(systemJars, i.Base())
1340 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001341 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001342 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1343 }
1344}
1345
Colin Cross54250902017-12-05 09:28:08 -08001346func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001347 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001348 java_library {
1349 name: "foo",
1350 srcs: ["a.java"],
1351 }
1352
1353 java_genrule {
1354 name: "jargen",
1355 tool_files: ["b.java"],
1356 cmd: "$(location b.java) $(in) $(out)",
1357 out: ["jargen.jar"],
1358 srcs: [":foo"],
1359 }
1360
1361 java_library {
1362 name: "bar",
1363 static_libs: ["jargen"],
1364 srcs: ["c.java"],
1365 }
1366
1367 java_library {
1368 name: "baz",
1369 libs: ["jargen"],
1370 srcs: ["c.java"],
1371 }
1372 `)
1373
1374 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1375 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1376 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1377 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1378 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1379
1380 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1381 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1382 }
1383
1384 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1385 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1386 }
1387
1388 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1389 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1390 }
1391
1392 if len(barCombined.Inputs) != 2 ||
1393 barCombined.Inputs[0].String() != bar.Output.String() ||
1394 barCombined.Inputs[1].String() != jargen.Output.String() {
1395 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1396 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1397 }
1398}
1399
Nan Zhang27e284d2018-02-09 21:03:53 +00001400func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001401 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001402 java_library {
1403 name: "foo",
1404 srcs: ["a.java", ":foo-srcs"],
1405 exclude_srcs: ["a.java", ":foo-excludes"],
1406 }
1407
1408 filegroup {
1409 name: "foo-srcs",
1410 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1411 }
1412
1413 filegroup {
1414 name: "foo-excludes",
1415 srcs: ["java-fg/a.java", "java-fg/b.java"],
1416 }
1417 `)
1418
1419 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1420
1421 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1422 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1423 }
1424}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001425
Paul Duffin52d398a2019-06-11 12:31:14 +01001426func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001427 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001428 "libcore/Android.bp": []byte(`
1429 java_library {
1430 name: "core",
1431 sdk_version: "none",
1432 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001433 }
1434
1435 filegroup {
1436 name: "core-jar",
1437 srcs: [":core{.jar}"],
1438 }
1439`),
1440 })
Colin Crossae8600b2020-10-29 17:09:13 -07001441 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001442 run(t, ctx, config)
1443}
1444
1445func TestJavaImport(t *testing.T) {
1446 config := testConfig(nil, "", map[string][]byte{
1447 "libcore/Android.bp": []byte(`
1448 java_import {
1449 name: "core",
1450 sdk_version: "none",
1451 }
1452
1453 filegroup {
1454 name: "core-jar",
1455 srcs: [":core{.jar}"],
1456 }
1457`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001458 })
Colin Crossae8600b2020-10-29 17:09:13 -07001459 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001460 run(t, ctx, config)
1461}
1462
Jiyong Parkc678ad32018-04-10 13:07:10 +09001463func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001464 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001465 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001466 name: "droiddoc-templates-sdk",
1467 path: ".",
1468 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001469 java_sdk_library {
1470 name: "foo",
1471 srcs: ["a.java", "b.java"],
1472 api_packages: ["foo"],
1473 }
1474 java_sdk_library {
1475 name: "bar",
1476 srcs: ["a.java", "b.java"],
1477 api_packages: ["bar"],
1478 }
1479 java_library {
1480 name: "baz",
1481 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001482 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001483 sdk_version: "system_current",
1484 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001485 java_sdk_library {
1486 name: "barney",
1487 srcs: ["c.java"],
1488 api_only: true,
1489 }
1490 java_sdk_library {
1491 name: "betty",
1492 srcs: ["c.java"],
1493 shared_library: false,
1494 }
Paul Duffin859fe962020-05-15 10:20:31 +01001495 java_sdk_library_import {
1496 name: "quuz",
1497 public: {
1498 jars: ["c.jar"],
1499 },
1500 }
1501 java_sdk_library_import {
1502 name: "fred",
1503 public: {
1504 jars: ["b.jar"],
1505 },
1506 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001507 java_sdk_library_import {
1508 name: "wilma",
1509 public: {
1510 jars: ["b.jar"],
1511 },
1512 shared_library: false,
1513 }
Jiyong Park1be96912018-05-28 18:02:19 +09001514 java_library {
1515 name: "qux",
1516 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001517 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001518 sdk_version: "system_current",
1519 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001520 java_library {
1521 name: "baz-test",
1522 srcs: ["c.java"],
1523 libs: ["foo"],
1524 sdk_version: "test_current",
1525 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001526 java_library {
1527 name: "baz-29",
1528 srcs: ["c.java"],
1529 libs: ["foo"],
1530 sdk_version: "system_29",
1531 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001532 java_library {
1533 name: "baz-module-30",
1534 srcs: ["c.java"],
1535 libs: ["foo"],
1536 sdk_version: "module_30",
1537 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001538 `)
1539
1540 // check the existence of the internal modules
1541 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001542 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1543 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1544 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1545 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1546 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1547 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001548 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001549 ctx.ModuleForTests("foo.api.public.28", "")
1550 ctx.ModuleForTests("foo.api.system.28", "")
1551 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001552
1553 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1554 // tests if baz is actually linked to the stubs lib
1555 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1556 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1557 "foo.stubs.system.jar")
1558 }
1559 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001560 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001561 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001562 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001563 }
1564 // test if baz is not linked to the system variant of foo
1565 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1566 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1567 "foo.stubs.jar")
1568 }
Jiyong Park1be96912018-05-28 18:02:19 +09001569
Paul Duffin726d23c2020-01-22 16:30:37 +00001570 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1571 // tests if baz-test is actually linked to the test stubs lib
1572 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1573 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1574 "foo.stubs.test.jar")
1575 }
1576
Paul Duffina2db18f2020-01-22 17:11:15 +00001577 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1578 // tests if baz-29 is actually linked to the system 29 stubs lib
1579 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1580 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1581 "prebuilts/sdk/29/system/foo.jar")
1582 }
1583
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001584 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1585 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1586 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1587 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1588 "prebuilts/sdk/30/module-lib/foo.jar")
1589 }
1590
Jiyong Park1be96912018-05-28 18:02:19 +09001591 // test if baz has exported SDK lib names foo and bar to qux
1592 qux := ctx.ModuleForTests("qux", "android_common")
1593 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001594 sdkLibs := quxLib.ExportedSdkLibs().UsesLibs()
1595 if w := []string{"foo", "bar", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
Paul Duffin859fe962020-05-15 10:20:31 +01001596 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001597 }
1598 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001599}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001600
Anton Hansson7f66efa2020-10-08 14:47:23 +01001601func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
1602 ctx, _ := testJava(t, `
1603 java_sdk_library {
1604 name: "sdk_lib",
1605 srcs: ["a.java"],
1606 impl_only_libs: ["foo"],
1607 stub_only_libs: ["bar"],
1608 }
1609 java_library {
1610 name: "foo",
1611 srcs: ["a.java"],
1612 sdk_version: "current",
1613 }
1614 java_library {
1615 name: "bar",
1616 srcs: ["a.java"],
1617 sdk_version: "current",
1618 }
1619 `)
1620
1621 for _, implName := range []string{"sdk_lib", "sdk_lib.impl"} {
1622 implJavacCp := ctx.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
1623 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1624 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1625 }
1626 }
1627 stubName := apiScopePublic.stubsLibraryModuleName("sdk_lib")
1628 stubsJavacCp := ctx.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
1629 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1630 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1631 }
1632}
1633
Paul Duffindaaa3322020-05-26 18:13:57 +01001634func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1635 ctx, _ := testJava(t, `
1636 java_sdk_library {
1637 name: "foo",
1638 srcs: ["a.java"],
1639 api_only: true,
1640 public: {
1641 enabled: true,
1642 },
1643 }
1644
1645 java_library {
1646 name: "bar",
1647 srcs: ["b.java"],
1648 libs: ["foo"],
1649 }
1650 `)
1651
1652 // The bar library should depend on the stubs jar.
1653 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1654 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1655 t.Errorf("expected %q, found %#q", expected, actual)
1656 }
1657}
1658
Paul Duffin46dc45a2020-05-14 15:39:10 +01001659func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1660 testJava(t, `
1661 java_sdk_library {
1662 name: "foo",
1663 srcs: ["a.java"],
1664 api_packages: ["foo"],
1665 public: {
1666 enabled: true,
1667 },
1668 }
1669
1670 java_library {
1671 name: "bar",
1672 srcs: ["b.java", ":foo{.public.stubs.source}"],
1673 }
1674 `)
1675}
1676
1677func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1678 testJavaError(t, `"foo" does not provide api scope system`, `
1679 java_sdk_library {
1680 name: "foo",
1681 srcs: ["a.java"],
1682 api_packages: ["foo"],
1683 public: {
1684 enabled: true,
1685 },
1686 }
1687
1688 java_library {
1689 name: "bar",
1690 srcs: ["b.java", ":foo{.system.stubs.source}"],
1691 }
1692 `)
1693}
1694
Paul Duffinca8d9a52020-06-26 22:20:25 +01001695func TestJavaSdkLibrary_Deps(t *testing.T) {
1696 ctx, _ := testJava(t, `
1697 java_sdk_library {
1698 name: "sdklib",
1699 srcs: ["a.java"],
1700 sdk_version: "none",
1701 system_modules: "none",
1702 public: {
1703 enabled: true,
1704 },
1705 }
1706 `)
1707
Paul Duffincee7e662020-07-09 17:32:57 +01001708 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001709 `dex2oatd`,
1710 `sdklib.impl`,
1711 `sdklib.stubs`,
1712 `sdklib.stubs.source`,
1713 `sdklib.xml`,
1714 })
1715}
1716
Paul Duffin46dc45a2020-05-14 15:39:10 +01001717func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1718 testJava(t, `
1719 java_sdk_library_import {
1720 name: "foo",
1721 public: {
1722 jars: ["a.jar"],
1723 stub_srcs: ["a.java"],
1724 current_api: "api/current.txt",
1725 removed_api: "api/removed.txt",
1726 },
1727 }
1728
1729 java_library {
1730 name: "bar",
1731 srcs: [":foo{.public.stubs.source}"],
1732 java_resources: [
1733 ":foo{.public.api.txt}",
1734 ":foo{.public.removed-api.txt}",
1735 ],
1736 }
1737 `)
1738}
1739
1740func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1741 bp := `
1742 java_sdk_library_import {
1743 name: "foo",
1744 public: {
1745 jars: ["a.jar"],
1746 },
1747 }
1748 `
1749
1750 t.Run("stubs.source", func(t *testing.T) {
1751 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1752 java_library {
1753 name: "bar",
1754 srcs: [":foo{.public.stubs.source}"],
1755 java_resources: [
1756 ":foo{.public.api.txt}",
1757 ":foo{.public.removed-api.txt}",
1758 ],
1759 }
1760 `)
1761 })
1762
1763 t.Run("api.txt", func(t *testing.T) {
1764 testJavaError(t, `api.txt not available for api scope public`, bp+`
1765 java_library {
1766 name: "bar",
1767 srcs: ["a.java"],
1768 java_resources: [
1769 ":foo{.public.api.txt}",
1770 ],
1771 }
1772 `)
1773 })
1774
1775 t.Run("removed-api.txt", func(t *testing.T) {
1776 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1777 java_library {
1778 name: "bar",
1779 srcs: ["a.java"],
1780 java_resources: [
1781 ":foo{.public.removed-api.txt}",
1782 ],
1783 }
1784 `)
1785 })
1786}
1787
Paul Duffin3375e352020-04-28 10:44:03 +01001788func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1789 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1790 java_sdk_library {
1791 name: "foo",
1792 srcs: ["a.java", "b.java"],
1793 api_packages: ["foo"],
1794 // Explicitly disable public to test the check that ensures the set of enabled
1795 // scopes is consistent.
1796 public: {
1797 enabled: false,
1798 },
1799 system: {
1800 enabled: true,
1801 },
1802 }
1803 `)
1804}
1805
Paul Duffin87a05a32020-05-12 11:50:28 +01001806func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1807 testJava(t, `
1808 java_sdk_library {
1809 name: "foo",
1810 srcs: ["a.java", "b.java"],
1811 api_packages: ["foo"],
1812 system: {
1813 enabled: true,
1814 sdk_version: "module_current",
1815 },
1816 }
1817 `)
1818}
1819
Paul Duffin0c5bae52020-06-02 13:00:08 +01001820func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
1821 testJava(t, `
1822 java_sdk_library {
1823 name: "foo",
1824 srcs: ["a.java", "b.java"],
1825 api_packages: ["foo"],
1826 system: {
1827 enabled: true,
1828 },
1829 module_lib: {
1830 enabled: true,
1831 },
1832 }
1833 `)
1834}
1835
1836func TestJavaSdkLibrary_SystemServer(t *testing.T) {
1837 testJava(t, `
1838 java_sdk_library {
1839 name: "foo",
1840 srcs: ["a.java", "b.java"],
1841 api_packages: ["foo"],
1842 system: {
1843 enabled: true,
1844 },
1845 system_server: {
1846 enabled: true,
1847 },
1848 }
1849 `)
1850}
1851
Paul Duffin803a9562020-05-20 11:52:25 +01001852func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1853 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1854 java_sdk_library {
1855 name: "foo",
1856 srcs: ["a.java"],
1857 public: {
1858 enabled: false,
1859 },
1860 }
1861
1862 java_library {
1863 name: "baz",
1864 srcs: ["a.java"],
1865 libs: ["foo"],
1866 sdk_version: "module_current",
1867 }
1868 `)
1869}
1870
1871func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1872 testJava(t, `
1873 java_sdk_library {
1874 name: "foo",
1875 srcs: ["a.java"],
1876 system: {
1877 enabled: true,
1878 },
1879 }
1880
1881 java_library {
1882 name: "baz",
1883 srcs: ["a.java"],
1884 libs: ["foo"],
1885 // foo does not have module-lib scope so it should fallback to system
1886 sdk_version: "module_current",
1887 }
1888 `)
1889}
1890
Jiyong Park932cdfe2020-05-28 00:19:53 +09001891func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1892 ctx, _ := testJava(t, `
1893 java_sdk_library {
1894 name: "foo",
1895 srcs: ["a.java"],
1896 system: {
1897 enabled: true,
1898 },
1899 default_to_stubs: true,
1900 }
1901
1902 java_library {
1903 name: "baz",
1904 srcs: ["a.java"],
1905 libs: ["foo"],
1906 // does not have sdk_version set, should fallback to module,
1907 // which will then fallback to system because the module scope
1908 // is not enabled.
1909 }
1910 `)
1911 // The baz library should depend on the system stubs jar.
1912 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1913 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1914 t.Errorf("expected %q, found %#q", expected, actual)
1915 }
1916}
1917
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001918var compilerFlagsTestCases = []struct {
1919 in string
1920 out bool
1921}{
1922 {
1923 in: "a",
1924 out: false,
1925 },
1926 {
1927 in: "-a",
1928 out: true,
1929 },
1930 {
1931 in: "-no-jdk",
1932 out: false,
1933 },
1934 {
1935 in: "-no-stdlib",
1936 out: false,
1937 },
1938 {
1939 in: "-kotlin-home",
1940 out: false,
1941 },
1942 {
1943 in: "-kotlin-home /some/path",
1944 out: false,
1945 },
1946 {
1947 in: "-include-runtime",
1948 out: false,
1949 },
1950 {
1951 in: "-Xintellij-plugin-root",
1952 out: false,
1953 },
1954}
1955
1956type mockContext struct {
1957 android.ModuleContext
1958 result bool
1959}
1960
1961func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1962 // CheckBadCompilerFlags calls this function when the flag should be rejected
1963 ctx.result = false
1964}
1965
1966func TestCompilerFlags(t *testing.T) {
1967 for _, testCase := range compilerFlagsTestCases {
1968 ctx := &mockContext{result: true}
1969 CheckKotlincFlags(ctx, []string{testCase.in})
1970 if ctx.result != testCase.out {
1971 t.Errorf("incorrect output:")
1972 t.Errorf(" input: %#v", testCase.in)
1973 t.Errorf(" expected: %#v", testCase.out)
1974 t.Errorf(" got: %#v", ctx.result)
1975 }
1976 }
1977}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001978
1979// TODO(jungjw): Consider making this more robust by ignoring path order.
1980func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1981 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1982 flags := strings.Split(variables["javacFlags"], " ")
1983 got := ""
1984 for _, flag := range flags {
1985 keyEnd := strings.Index(flag, "=")
1986 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1987 got = flag[keyEnd+1:]
1988 break
1989 }
1990 }
1991 if expected != got {
1992 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1993 }
1994}
1995
1996func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001997 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001998 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001999 bp := `
2000 java_library {
2001 name: "foo",
2002 srcs: ["a.java"],
2003 java_version: "1.8",
2004 }
2005
2006 java_library {
2007 name: "bar",
2008 srcs: ["b.java"],
2009 sdk_version: "none",
2010 system_modules: "none",
2011 patch_module: "java.base",
2012 java_version: "1.8",
2013 }
2014
2015 java_library {
2016 name: "baz",
2017 srcs: ["c.java"],
2018 patch_module: "java.base",
2019 java_version: "1.8",
2020 }
2021 `
2022 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002023
2024 checkPatchModuleFlag(t, ctx, "foo", "")
2025 checkPatchModuleFlag(t, ctx, "bar", "")
2026 checkPatchModuleFlag(t, ctx, "baz", "")
2027 })
2028
Pete Gillin0c2143e2019-05-02 15:32:11 +01002029 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002030 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002031 bp := `
2032 java_library {
2033 name: "foo",
2034 srcs: ["a.java"],
2035 }
2036
2037 java_library {
2038 name: "bar",
2039 srcs: ["b.java"],
2040 sdk_version: "none",
2041 system_modules: "none",
2042 patch_module: "java.base",
2043 }
2044
2045 java_library {
2046 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002047 srcs: [
2048 "c.java",
2049 // Tests for b/150878007
2050 "dir/d.java",
2051 "dir2/e.java",
2052 "dir2/f.java",
2053 "nested/dir/g.java"
2054 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002055 patch_module: "java.base",
2056 }
2057 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002058 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002059
2060 checkPatchModuleFlag(t, ctx, "foo", "")
2061 expected := "java.base=.:" + buildDir
2062 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002063 expected = "java.base=" + strings.Join([]string{
2064 ".", buildDir, "dir", "dir2", "nested", moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002065 checkPatchModuleFlag(t, ctx, "baz", expected)
2066 })
2067}
Paul Duffina7b9f422020-01-10 17:12:18 +00002068
2069func TestJavaSystemModules(t *testing.T) {
2070 ctx, _ := testJava(t, `
2071 java_system_modules {
2072 name: "system-modules",
2073 libs: ["system-module1", "system-module2"],
2074 }
2075 java_library {
2076 name: "system-module1",
2077 srcs: ["a.java"],
2078 sdk_version: "none",
2079 system_modules: "none",
2080 }
2081 java_library {
2082 name: "system-module2",
2083 srcs: ["b.java"],
2084 sdk_version: "none",
2085 system_modules: "none",
2086 }
2087 `)
2088
2089 // check the existence of the module
2090 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2091
2092 cmd := systemModules.Rule("jarsTosystemModules")
2093
2094 // make sure the command compiles against the supplied modules.
2095 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2096 if !strings.Contains(cmd.Args["classpath"], module) {
2097 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2098 module)
2099 }
2100 }
2101}
Paul Duffin90169ba2020-01-10 17:16:44 +00002102
2103func TestJavaSystemModulesImport(t *testing.T) {
2104 ctx, _ := testJava(t, `
2105 java_system_modules_import {
2106 name: "system-modules",
2107 libs: ["system-module1", "system-module2"],
2108 }
2109 java_import {
2110 name: "system-module1",
2111 jars: ["a.jar"],
2112 }
2113 java_import {
2114 name: "system-module2",
2115 jars: ["b.jar"],
2116 }
2117 `)
2118
2119 // check the existence of the module
2120 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2121
2122 cmd := systemModules.Rule("jarsTosystemModules")
2123
2124 // make sure the command compiles against the supplied modules.
2125 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2126 if !strings.Contains(cmd.Args["classpath"], module) {
2127 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2128 module)
2129 }
2130 }
2131}
Paul Duffin83a2d962019-11-19 19:44:10 +00002132
2133func TestJavaLibraryWithSystemModules(t *testing.T) {
2134 ctx, _ := testJava(t, `
2135 java_library {
2136 name: "lib-with-source-system-modules",
2137 srcs: [
2138 "a.java",
2139 ],
2140 sdk_version: "none",
2141 system_modules: "source-system-modules",
2142 }
2143
2144 java_library {
2145 name: "source-jar",
2146 srcs: [
2147 "a.java",
2148 ],
2149 }
2150
2151 java_system_modules {
2152 name: "source-system-modules",
2153 libs: ["source-jar"],
2154 }
2155
2156 java_library {
2157 name: "lib-with-prebuilt-system-modules",
2158 srcs: [
2159 "a.java",
2160 ],
2161 sdk_version: "none",
2162 system_modules: "prebuilt-system-modules",
2163 }
2164
2165 java_import {
2166 name: "prebuilt-jar",
2167 jars: ["a.jar"],
2168 }
2169
2170 java_system_modules_import {
2171 name: "prebuilt-system-modules",
2172 libs: ["prebuilt-jar"],
2173 }
2174 `)
2175
2176 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2177
2178 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2179}
2180
2181func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2182 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2183 bootClasspath := javacRule.Args["bootClasspath"]
2184 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2185 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2186 }
2187}
Jiyong Park19604de2020-03-24 16:44:11 +09002188
2189func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2190 ctx, _ := testJava(t, `
2191 java_library {
2192 name: "foo",
2193 srcs: ["aidl/foo/IFoo.aidl"],
2194 libs: ["bar"],
2195 }
2196
2197 java_import {
2198 name: "bar",
2199 jars: ["a.jar"],
2200 aidl: {
2201 export_include_dirs: ["aidl/bar"],
2202 },
2203 }
2204 `)
2205
2206 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2207 expectedAidlFlag := "-Iaidl/bar"
2208 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2209 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2210 }
2211}
Liz Kammerdd849a82020-06-12 16:38:45 -07002212
2213func TestDataNativeBinaries(t *testing.T) {
2214 ctx, config := testJava(t, `
2215 java_test_host {
2216 name: "foo",
2217 srcs: ["a.java"],
2218 data_native_bins: ["bin"]
2219 }
2220
2221 python_binary_host {
2222 name: "bin",
2223 srcs: ["bin.py"],
2224 }
2225 `)
2226
2227 buildOS := android.BuildOs.String()
2228
2229 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2230 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
2231 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2232 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2233 if !reflect.DeepEqual(expected, actual) {
2234 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2235 }
2236}