blob: cf01f33685b331e6e4a2a4f3d63e9ffdf9ce2b21 [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"
Colin Crossb5ae1932020-11-17 06:32:06 +000033 "android/soong/genrule"
Liz Kammerdd849a82020-06-12 16:38:45 -070034 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070035)
36
37var buildDir string
38
39func setUp() {
40 var err error
41 buildDir, err = ioutil.TempDir("", "soong_java_test")
42 if err != nil {
43 panic(err)
44 }
45}
46
47func tearDown() {
48 os.RemoveAll(buildDir)
49}
50
51func TestMain(m *testing.M) {
52 run := func() int {
53 setUp()
54 defer tearDown()
55
56 return m.Run()
57 }
58
59 os.Exit(run())
60}
Colin Cross527012a2017-11-30 22:56:16 -080061
Colin Cross98be1bb2019-12-13 20:41:13 -080062func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000063 bp += dexpreopt.BpToolModulesForTest()
64
Martin Stjernholm75a48d82020-01-10 20:32:59 +000065 config := TestConfig(buildDir, env, bp, fs)
66
67 // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that
68 // it doesn't create a real one, which would fail.
69 _ = dexpreopt.GlobalSoongConfigForTests(config)
70
71 return config
Colin Cross1369cdb2017-09-29 17:58:17 -070072}
73
Colin Crossae8600b2020-10-29 17:09:13 -070074func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070075
Colin Crossae8600b2020-10-29 17:09:13 -070076 ctx := android.NewTestArchContext(config)
Paul Duffinf9b1da02019-12-18 19:51:55 +000077 RegisterJavaBuildComponents(ctx)
78 RegisterAppBuildComponents(ctx)
79 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000080 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000081 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080082 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080083 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Colin Crossb5ae1932020-11-17 06:32:06 +000084 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070085 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000086 RegisterDocsBuildComponents(ctx)
87 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000088 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070089 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010090 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000091
92 RegisterPrebuiltApisBuildComponents(ctx)
93
Liz Kammerdd849a82020-06-12 16:38:45 -070094 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070095 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross06fa5882020-10-29 18:21:38 -070096 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(ctx.Context, OverlaySingletonFactory))
97 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(ctx.Context, sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070098
Paul Duffin021f4e52020-07-30 16:04:17 +010099 android.RegisterPrebuiltMutators(ctx)
100
Colin Crossa4f08812018-10-02 22:03:40 -0700101 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000102 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700103
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000104 dexpreopt.RegisterToolModulesForTest(ctx)
105
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700106 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
107 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
108 })
109
Colin Cross527012a2017-11-30 22:56:16 -0800110 return ctx
111}
112
113func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800114 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800115
Colin Cross98be1bb2019-12-13 20:41:13 -0800116 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000117 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800118
Colin Crossae8600b2020-10-29 17:09:13 -0700119 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100120 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800121 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700122 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800123 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800124}
125
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900126func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900127 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800128 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900129}
130
Colin Cross98be1bb2019-12-13 20:41:13 -0800131func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900132 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700133 ctx := testContext(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900134
Colin Cross98be1bb2019-12-13 20:41:13 -0800135 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000136 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900137
Colin Crossae8600b2020-10-29 17:09:13 -0700138 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900139 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
140 if len(errs) > 0 {
141 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900142 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900143 }
144 _, errs = ctx.PrepareBuildActions(config)
145 if len(errs) > 0 {
146 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900147 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900148 }
149
150 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900151
152 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900153}
154
Colin Cross238c1f32020-06-07 16:58:18 -0700155func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
156 t.Helper()
157 return testJavaWithConfig(t, testConfig(nil, bp, fs))
158}
159
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700160func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800161 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700162 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900163}
164
Colin Cross98be1bb2019-12-13 20:41:13 -0800165func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900166 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700167 ctx := testContext(config)
Colin Cross527012a2017-11-30 22:56:16 -0800168 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700169
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700170 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700171}
172
Colin Cross2acdae82017-09-15 19:44:24 -0700173func moduleToPath(name string) string {
174 switch {
175 case name == `""`:
176 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700177 case strings.HasSuffix(name, ".jar"):
178 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700179 default:
180 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700181 }
182}
183
Jeongik Chae403e9e2019-12-07 00:16:24 +0900184func TestJavaLinkType(t *testing.T) {
185 testJava(t, `
186 java_library {
187 name: "foo",
188 srcs: ["a.java"],
189 libs: ["bar"],
190 static_libs: ["baz"],
191 }
192
193 java_library {
194 name: "bar",
195 sdk_version: "current",
196 srcs: ["b.java"],
197 }
198
199 java_library {
200 name: "baz",
201 sdk_version: "system_current",
202 srcs: ["c.java"],
203 }
204 `)
205
206 testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", `
207 java_library {
208 name: "foo",
209 srcs: ["a.java"],
210 libs: ["bar"],
211 sdk_version: "current",
212 static_libs: ["baz"],
213 }
214
215 java_library {
216 name: "bar",
217 sdk_version: "current",
218 srcs: ["b.java"],
219 }
220
221 java_library {
222 name: "baz",
223 sdk_version: "system_current",
224 srcs: ["c.java"],
225 }
226 `)
227
228 testJava(t, `
229 java_library {
230 name: "foo",
231 srcs: ["a.java"],
232 libs: ["bar"],
233 sdk_version: "system_current",
234 static_libs: ["baz"],
235 }
236
237 java_library {
238 name: "bar",
239 sdk_version: "current",
240 srcs: ["b.java"],
241 }
242
243 java_library {
244 name: "baz",
245 sdk_version: "system_current",
246 srcs: ["c.java"],
247 }
248 `)
249
250 testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", `
251 java_library {
252 name: "foo",
253 srcs: ["a.java"],
254 libs: ["bar"],
255 sdk_version: "system_current",
256 static_libs: ["baz"],
257 }
258
259 java_library {
260 name: "bar",
261 sdk_version: "current",
262 srcs: ["b.java"],
263 }
264
265 java_library {
266 name: "baz",
267 srcs: ["c.java"],
268 }
269 `)
270}
271
Colin Cross72bb3632017-07-13 16:23:21 -0700272func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700273 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700274 java_library {
275 name: "foo",
276 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700277 libs: ["bar"],
278 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700279 }
280
281 java_library {
282 name: "bar",
283 srcs: ["b.java"],
284 }
285
286 java_library {
287 name: "baz",
288 srcs: ["c.java"],
289 }
Colin Crossd5934c82017-10-02 13:55:26 -0700290 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700291
Colin Cross4c428df2017-09-15 17:36:05 -0700292 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700293 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700294
295 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
296 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
297 }
298
Colin Cross1ee23172017-10-18 14:44:18 -0700299 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700300 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
301 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700302
Nan Zhanged19fc32017-10-19 13:06:22 -0700303 if !strings.Contains(javac.Args["classpath"], barTurbine) {
304 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700305 }
306
Nan Zhanged19fc32017-10-19 13:06:22 -0700307 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
308 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700309 }
310
311 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
312 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700313 }
314}
315
Artur Satayev9cf46692019-11-26 18:08:34 +0000316func TestExportedPlugins(t *testing.T) {
317 type Result struct {
318 library string
319 processors string
320 }
321 var tests = []struct {
322 name string
323 extra string
324 results []Result
325 }{
326 {
327 name: "Exported plugin is not a direct plugin",
328 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
329 results: []Result{{library: "exports", processors: "-proc:none"}},
330 },
331 {
332 name: "Exports plugin to dependee",
333 extra: `
334 java_library{name: "exports", exported_plugins: ["plugin"]}
335 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
336 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
337 `,
338 results: []Result{
339 {library: "foo", processors: "-processor com.android.TestPlugin"},
340 {library: "bar", processors: "-processor com.android.TestPlugin"},
341 },
342 },
343 {
344 name: "Exports plugin to android_library",
345 extra: `
346 java_library{name: "exports", exported_plugins: ["plugin"]}
347 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
348 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
349 `,
350 results: []Result{
351 {library: "foo", processors: "-processor com.android.TestPlugin"},
352 {library: "bar", processors: "-processor com.android.TestPlugin"},
353 },
354 },
355 {
356 name: "Exports plugin is not propagated via transitive deps",
357 extra: `
358 java_library{name: "exports", exported_plugins: ["plugin"]}
359 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
360 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
361 `,
362 results: []Result{
363 {library: "foo", processors: "-processor com.android.TestPlugin"},
364 {library: "bar", processors: "-proc:none"},
365 },
366 },
367 {
368 name: "Exports plugin appends to plugins",
369 extra: `
370 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
371 java_library{name: "exports", exported_plugins: ["plugin"]}
372 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
373 `,
374 results: []Result{
375 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
376 },
377 },
378 }
379
380 for _, test := range tests {
381 t.Run(test.name, func(t *testing.T) {
382 ctx, _ := testJava(t, `
383 java_plugin {
384 name: "plugin",
385 processor_class: "com.android.TestPlugin",
386 }
387 `+test.extra)
388
389 for _, want := range test.results {
390 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
391 if javac.Args["processor"] != want.processors {
392 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
393 }
394 }
395 })
396 }
397}
398
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900399func TestSdkVersionByPartition(t *testing.T) {
400 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900401 java_library {
402 name: "foo",
403 srcs: ["a.java"],
404 vendor: true,
405 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900406 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900407
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900408 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900409 java_library {
410 name: "bar",
411 srcs: ["b.java"],
412 }
413 `)
414
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900415 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900416 bp := `
417 java_library {
418 name: "foo",
419 srcs: ["a.java"],
420 product_specific: true,
421 }
422 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800423
424 config := testConfig(nil, bp, nil)
425 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900426 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800427 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 +0900428 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800429 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900430 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900431 }
432}
433
Colin Crossd5934c82017-10-02 13:55:26 -0700434func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700435 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700436 java_library {
437 name: "foo",
438 srcs: ["a.java"],
439 target: {
440 android: {
441 srcs: ["b.java"],
442 },
443 },
444 }
445 `)
446
447 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
448 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
449 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
450 }
451}
452
Colin Cross6b4a32d2017-12-05 13:42:45 -0800453func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700454 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800455 java_library_host {
456 name: "foo",
457 srcs: ["a.java"],
458 }
459
460 java_binary_host {
461 name: "bar",
462 srcs: ["b.java"],
463 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700464 jni_libs: ["libjni"],
465 }
466
467 cc_library_shared {
468 name: "libjni",
469 host_supported: true,
470 device_supported: false,
471 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800472 }
473 `)
474
475 buildOS := android.BuildOs.String()
476
477 bar := ctx.ModuleForTests("bar", buildOS+"_common")
478 barJar := bar.Output("bar.jar").Output.String()
479 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
480 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
481
Colin Cross89226d92020-10-09 19:00:54 -0700482 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
483 libjniSO := libjni.Rule("Cp").Output.String()
484
Colin Cross6b4a32d2017-12-05 13:42:45 -0800485 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700486 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
487 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800488 }
Colin Cross89226d92020-10-09 19:00:54 -0700489
490 // Test that the install binary wrapper depends on the installed JNI libraries
491 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
492 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800493 }
Alex Humesky2070e322020-06-09 20:23:08 -0400494}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800495
Alex Humesky2070e322020-06-09 20:23:08 -0400496func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
497 bp := `
498 java_library {
499 name: "target_library",
500 srcs: ["a.java"],
501 }
502
503 java_binary_host {
504 name: "host_binary",
505 srcs: ["b.java"],
506 }
507 `
508 config := testConfig(nil, bp, nil)
509 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
510
511 ctx, _ := testJavaWithConfig(t, config)
512
Liz Kammer7941b302020-07-28 13:27:34 -0700513 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400514 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
515 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
516 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
517 t.Errorf("target library javac flags %v should contain "+
518 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
519 }
520
521 // check that -g is not overridden for host modules
522 buildOS := android.BuildOs.String()
523 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
524 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
525 if strings.Contains(hostJavaFlags, "-g:source,lines") {
526 t.Errorf("java_binary_host javac flags %v should not have "+
527 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
528 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800529}
530
Colin Cross72bb3632017-07-13 16:23:21 -0700531func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700532 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700533 java_library {
534 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000535 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000536 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700537 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700538 }
539
Colin Cross74d73e22017-08-02 11:05:49 -0700540 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700541 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700542 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700543 }
544
Colin Cross74d73e22017-08-02 11:05:49 -0700545 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700546 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700547 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700548 sdk_version: "current",
549 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700550 }
Colin Cross42be7612019-02-21 18:12:14 -0800551
552 dex_import {
553 name: "qux",
554 jars: ["b.jar"],
555 }
Colin Cross79c7c262019-04-17 11:11:46 -0700556
557 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000558 name: "sdklib",
559 public: {
560 jars: ["c.jar"],
561 },
562 }
563
Paul Duffin91547182019-11-12 19:39:36 +0000564 prebuilt_stubs_sources {
565 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000566 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000567 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000568
569 java_test_import {
570 name: "test",
571 jars: ["a.jar"],
572 test_suites: ["cts"],
573 test_config: "AndroidTest.xml",
574 }
Colin Cross72bb3632017-07-13 16:23:21 -0700575 `)
576
Paul Duffin9b478b02019-12-10 13:41:51 +0000577 fooModule := ctx.ModuleForTests("foo", "android_common")
578 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700579 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700580 barModule := ctx.ModuleForTests("bar", "android_common")
581 barJar := barModule.Rule("combineJar").Output
582 bazModule := ctx.ModuleForTests("baz", "android_common")
583 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700584 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700585
Paul Duffin9b478b02019-12-10 13:41:51 +0000586 fooLibrary := fooModule.Module().(*Library)
587 assertDeepEquals(t, "foo java sources incorrect",
588 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000589
Paul Duffin9b478b02019-12-10 13:41:51 +0000590 assertDeepEquals(t, "foo java source jars incorrect",
591 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
592 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000593
Colin Cross37f6d792018-07-12 12:28:41 -0700594 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
595 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700596 }
597
Liz Kammerd6c31d22020-08-05 15:40:41 -0700598 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
599 if barDexJar != nil {
600 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
601 }
602
Colin Cross79c7c262019-04-17 11:11:46 -0700603 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
604 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
605 }
606
Colin Cross37f6d792018-07-12 12:28:41 -0700607 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
608 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700609 }
Colin Cross42be7612019-02-21 18:12:14 -0800610
Liz Kammerd6c31d22020-08-05 15:40:41 -0700611 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
612 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
613 if bazDexJar != expectedDexJar {
614 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
615 }
616
Colin Cross42be7612019-02-21 18:12:14 -0800617 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700618}
619
Paul Duffin9b478b02019-12-10 13:41:51 +0000620func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
621 if !reflect.DeepEqual(expected, actual) {
622 t.Errorf("%s: expected %q, found %q", message, expected, actual)
623 }
624}
625
Paul Duffin1a393322020-11-18 16:36:47 +0000626func TestPrebuiltStubsSources(t *testing.T) {
627 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
628 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
629prebuilt_stubs_sources {
630 name: "stubs-source",
631 srcs: ["%s"],
632}`, sourcesPath), map[string][]byte{
633 "stubs/sources/pkg/A.java": nil,
634 "stubs/sources/pkg/B.java": nil,
635 })
636
637 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
638 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
639 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
640 }
641 }
642
643 t.Run("empty/missing directory", func(t *testing.T) {
644 test(t, "empty-directory", []string{})
645 })
646
647 t.Run("non-empty set of sources", func(t *testing.T) {
648 test(t, "stubs/sources", []string{
649 "stubs/sources/pkg/A.java",
650 "stubs/sources/pkg/B.java",
651 })
652 })
653}
654
Paul Duffin56d44902020-01-31 13:36:25 +0000655func TestJavaSdkLibraryImport(t *testing.T) {
656 ctx, _ := testJava(t, `
657 java_library {
658 name: "foo",
659 srcs: ["a.java"],
660 libs: ["sdklib"],
661 sdk_version: "current",
662 }
663
664 java_library {
665 name: "foo.system",
666 srcs: ["a.java"],
667 libs: ["sdklib"],
668 sdk_version: "system_current",
669 }
670
671 java_library {
672 name: "foo.test",
673 srcs: ["a.java"],
674 libs: ["sdklib"],
675 sdk_version: "test_current",
676 }
677
678 java_sdk_library_import {
679 name: "sdklib",
680 public: {
681 jars: ["a.jar"],
682 },
683 system: {
684 jars: ["b.jar"],
685 },
686 test: {
687 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100688 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000689 },
690 }
691 `)
692
693 for _, scope := range []string{"", ".system", ".test"} {
694 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
695 javac := fooModule.Rule("javac")
696
697 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
698 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
699 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
700 }
701 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100702
Paul Duffincee7e662020-07-09 17:32:57 +0100703 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100704 `prebuilt_sdklib.stubs`,
705 `prebuilt_sdklib.stubs.source.test`,
706 `prebuilt_sdklib.stubs.system`,
707 `prebuilt_sdklib.stubs.test`,
708 })
709}
710
711func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
712 ctx, _ := testJava(t, `
713 java_sdk_library {
714 name: "sdklib",
715 srcs: ["a.java"],
716 sdk_version: "none",
717 system_modules: "none",
718 public: {
719 enabled: true,
720 },
721 }
722
723 java_sdk_library_import {
724 name: "sdklib",
725 public: {
726 jars: ["a.jar"],
727 },
728 }
729 `)
730
Paul Duffincee7e662020-07-09 17:32:57 +0100731 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100732 `dex2oatd`,
733 `prebuilt_sdklib`,
734 `sdklib.impl`,
735 `sdklib.stubs`,
736 `sdklib.stubs.source`,
737 `sdklib.xml`,
738 })
739
Paul Duffincee7e662020-07-09 17:32:57 +0100740 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100741 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100742 `sdklib.impl`,
743 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
744 // dependency is added after prebuilts may have been renamed and so has to use
745 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100746 `sdklib.xml`,
747 })
748}
749
750func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
751 ctx, _ := testJava(t, `
752 java_sdk_library {
753 name: "sdklib",
754 srcs: ["a.java"],
755 sdk_version: "none",
756 system_modules: "none",
757 public: {
758 enabled: true,
759 },
760 }
761
762 java_sdk_library_import {
763 name: "sdklib",
764 prefer: true,
765 public: {
766 jars: ["a.jar"],
767 },
768 }
769 `)
770
Paul Duffincee7e662020-07-09 17:32:57 +0100771 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100772 `dex2oatd`,
773 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100774 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100775 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100776 `sdklib.stubs.source`,
777 `sdklib.xml`,
778 })
779
Paul Duffincee7e662020-07-09 17:32:57 +0100780 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100781 `prebuilt_sdklib.stubs`,
782 `sdklib.impl`,
783 `sdklib.xml`,
784 })
Paul Duffin56d44902020-01-31 13:36:25 +0000785}
786
Colin Cross89536d42017-07-07 14:35:50 -0700787func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700788 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700789 java_defaults {
790 name: "defaults",
791 srcs: ["a.java"],
792 libs: ["bar"],
793 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700794 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700795 }
796
797 java_library {
798 name: "foo",
799 defaults: ["defaults"],
800 }
801
802 java_library {
803 name: "bar",
804 srcs: ["b.java"],
805 }
806
807 java_library {
808 name: "baz",
809 srcs: ["c.java"],
810 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700811
812 android_test {
813 name: "atestOptimize",
814 defaults: ["defaults"],
815 optimize: {enabled: true},
816 }
817
818 android_test {
819 name: "atestNoOptimize",
820 defaults: ["defaults"],
821 }
822
823 android_test {
824 name: "atestDefault",
825 srcs: ["a.java"],
826 }
Colin Cross89536d42017-07-07 14:35:50 -0700827 `)
828
Colin Cross4c428df2017-09-15 17:36:05 -0700829 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700830 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700831
832 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
833 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
834 }
835
Nan Zhanged19fc32017-10-19 13:06:22 -0700836 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
837 if !strings.Contains(javac.Args["classpath"], barTurbine) {
838 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700839 }
840
Colin Cross1ee23172017-10-18 14:44:18 -0700841 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700842 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
843 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700844 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700845
846 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
847 if atestOptimize.Output == nil {
848 t.Errorf("atestOptimize should optimize APK")
849 }
850
851 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
852 if atestNoOptimize.Output == nil {
853 t.Errorf("atestNoOptimize should not optimize APK")
854 }
855
856 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
857 if atestDefault.Output == nil {
858 t.Errorf("atestDefault should optimize APK")
859 }
Colin Cross89536d42017-07-07 14:35:50 -0700860}
861
Colin Cross0f37af02017-09-27 17:42:05 -0700862func TestResources(t *testing.T) {
863 var table = []struct {
864 name string
865 prop string
866 extra string
867 args string
868 }{
869 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700870 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700871 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800872 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700873 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700874 },
875 {
876 // Test that a module with java_resources includes the files
877 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700878 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
879 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700880 },
881 {
882 // Test that a module with a filegroup in java_resources includes the files with the
883 // path prefix
884 name: "resource filegroup",
885 prop: `java_resources: [":foo-res"]`,
886 extra: `
887 filegroup {
888 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800889 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700890 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700891 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700892 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700893 },
894 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700895 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
896 name: "wildcard dirs",
897 prop: `java_resource_dirs: ["java-res/*"]`,
898 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
899 },
900 {
901 // Test that a module exclude_java_resource_dirs excludes the files
902 name: "wildcard dirs",
903 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
904 args: "-C java-res/a -f java-res/a/a",
905 },
Colin Crosscedd4762018-09-13 11:26:19 -0700906 {
907 // Test wildcards in java_resources
908 name: "wildcard files",
909 prop: `java_resources: ["java-res/**/*"]`,
910 args: "-C . -f java-res/a/a -f java-res/b/b",
911 },
912 {
913 // Test exclude_java_resources with java_resources
914 name: "wildcard files with exclude",
915 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
916 args: "-C . -f java-res/a/a",
917 },
918 {
919 // Test exclude_java_resources with java_resource_dirs
920 name: "resource dirs with exclude files",
921 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
922 args: "-C java-res -f java-res/a/a",
923 },
924 {
925 // Test exclude_java_resource_dirs with java_resource_dirs
926 name: "resource dirs with exclude files",
927 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
928 args: "-C java-res -f java-res/a/a -f java-res/b/b",
929 },
Colin Cross0f37af02017-09-27 17:42:05 -0700930 }
931
932 for _, test := range table {
933 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700934 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700935 java_library {
936 name: "foo",
937 srcs: [
938 "a.java",
939 "b.java",
940 "c.java",
941 ],
942 `+test.prop+`,
943 }
Colin Cross238c1f32020-06-07 16:58:18 -0700944 `+test.extra,
945 map[string][]byte{
946 "java-res/a/a": nil,
947 "java-res/b/b": nil,
948 "java-res2/a": nil,
949 },
950 )
Colin Cross0f37af02017-09-27 17:42:05 -0700951
Colin Cross331a1212018-08-15 20:40:52 -0700952 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700953 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700954
955 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
956 t.Errorf("foo combined jars %v does not contain %q",
957 foo.Inputs.Strings(), fooRes.Output.String())
958 }
959
Colin Crossaf9c55b2017-10-03 14:50:08 -0700960 if fooRes.Args["jarArgs"] != test.args {
961 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700962 fooRes.Args["jarArgs"], test.args)
963 }
964 })
965 }
966}
967
Colin Cross0c4ce212019-05-03 15:28:19 -0700968func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700969 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700970 java_library {
971 name: "foo",
972 srcs: [
973 "a.java",
974 "b.java",
975 "c.java",
976 ],
977 include_srcs: true,
978 }
979
980 java_library {
981 name: "bar",
982 srcs: [
983 "a.java",
984 "b.java",
985 "c.java",
986 ],
987 java_resource_dirs: ["java-res"],
988 include_srcs: true,
989 }
Colin Cross238c1f32020-06-07 16:58:18 -0700990 `, map[string][]byte{
991 "java-res/a/a": nil,
992 "java-res/b/b": nil,
993 "java-res2/a": nil,
994 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700995
996 // Test a library with include_srcs: true
997 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
998 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
999
1000 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1001 t.Errorf("foo combined jars %v does not contain %q", w, g)
1002 }
1003
1004 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1005 t.Errorf("foo source jar args %q is not %q", w, g)
1006 }
1007
1008 // Test a library with include_srcs: true and resources
1009 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1010 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1011 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1012 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1013
1014 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1015 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1016 }
1017
1018 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1019 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1020 }
1021
1022 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1023 t.Errorf("bar combined jars %v does not contain %q", w, g)
1024 }
1025
1026 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1027 t.Errorf("bar source jar args %q is not %q", w, g)
1028 }
1029
1030 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1031 t.Errorf("bar resource jar args %q is not %q", w, g)
1032 }
1033}
1034
Colin Cross54190b32017-10-09 15:34:10 -07001035func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001036 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001037 java_library {
1038 name: "foo",
1039 srcs: [
1040 "a*.java",
1041 ":gen",
1042 "b*.java",
1043 ],
1044 }
1045
1046 genrule {
1047 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001048 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001049 out: ["gen.java"],
1050 }
Colin Cross238c1f32020-06-07 16:58:18 -07001051 `, map[string][]byte{
1052 "a.java": nil,
1053 "b.java": nil,
1054 })
Colin Cross54190b32017-10-09 15:34:10 -07001055
1056 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1057 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1058
Colin Cross15e86d92017-10-20 15:07:08 -07001059 if filepath.Base(genrule.Output.String()) != "gen.java" {
1060 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001061 }
1062
1063 if len(javac.Inputs) != 3 ||
1064 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001065 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001066 javac.Inputs[2].String() != "b.java" {
1067 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1068 }
1069}
1070
Nan Zhang61eaedb2017-11-02 13:28:15 -07001071func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001072 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001073 java_library {
1074 name: "foo",
1075 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001076 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001077 }
1078
1079 java_library {
1080 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001081 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001082 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001083 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001084 }
1085
1086 java_library {
1087 name: "baz",
1088 srcs: ["c.java"],
1089 libs: ["bar"],
1090 sdk_version: "14",
1091 }
1092 `)
1093
1094 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1095 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1096 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1097 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1098 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1099
1100 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1101 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1102 }
1103
1104 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1105 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1106 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1107 }
1108 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1109 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1110 }
1111 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1112 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1113 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001114 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001115 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001116 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001117 }
1118}
1119
1120func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001121 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001122 java_library {
1123 name: "bar",
1124 srcs: ["a.java","b.java","c.java"],
1125 javac_shard_size: 1
1126 }
1127 `)
1128
1129 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1130 for i := 0; i < 3; i++ {
1131 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1132 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1133 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1134 }
1135 }
1136}
1137
Nan Zhang581fd212018-01-10 16:06:12 -08001138func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001139 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001140 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001141 name: "droiddoc-templates-sdk",
1142 path: ".",
1143 }
Jiyong Park29074592019-07-07 16:27:47 +09001144 filegroup {
1145 name: "bar-doc-aidl-srcs",
1146 srcs: ["bar-doc/IBar.aidl"],
1147 path: "bar-doc",
1148 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001149 droidstubs {
1150 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001151 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001152 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001153 ],
Steve Kim3666c702020-09-01 17:58:01 +00001154 exclude_srcs: [
1155 "bar-doc/b.java"
1156 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001157 api_levels_annotations_dirs: [
1158 "droiddoc-templates-sdk",
1159 ],
1160 api_levels_annotations_enabled: true,
1161 }
1162 droiddoc {
1163 name: "bar-doc",
1164 srcs: [
1165 ":bar-stubs",
1166 "bar-doc/IFoo.aidl",
1167 ":bar-doc-aidl-srcs",
1168 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001169 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001170 hdf: [
1171 "android.whichdoc offline",
1172 ],
1173 knowntags: [
1174 "bar-doc/known_oj_tags.txt",
1175 ],
1176 proofread_file: "libcore-proofread.txt",
1177 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001178 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001179 }
Colin Cross238c1f32020-06-07 16:58:18 -07001180 `,
1181 map[string][]byte{
1182 "bar-doc/a.java": nil,
1183 "bar-doc/b.java": nil,
1184 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001185 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1186 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1187 if err != nil {
1188 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1189 }
1190 if len(barStubsOutputs) != 1 {
1191 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001192 }
Nan Zhang581fd212018-01-10 16:06:12 -08001193
Liz Kammere1ab2502020-09-10 15:29:25 +00001194 barStubsOutput := barStubsOutputs[0]
1195 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1196 javaDoc := barDoc.Rule("javadoc")
1197 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1198 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001199 }
1200
Liz Kammere1ab2502020-09-10 15:29:25 +00001201 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1202 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1203 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1204 }
1205
1206 aidl := barDoc.Rule("aidl")
1207 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001208 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1209 }
1210
1211 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1212 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001213 }
Nan Zhang581fd212018-01-10 16:06:12 -08001214}
1215
Liz Kammer585cac22020-07-06 09:12:57 -07001216func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1217 testJavaError(t, "flags is set. Cannot set args", `
1218 droiddoc_exported_dir {
1219 name: "droiddoc-templates-sdk",
1220 path: ".",
1221 }
1222 filegroup {
1223 name: "bar-doc-aidl-srcs",
1224 srcs: ["bar-doc/IBar.aidl"],
1225 path: "bar-doc",
1226 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001227 droidstubs {
1228 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001229 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001230 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001231 ],
Steve Kim3666c702020-09-01 17:58:01 +00001232 exclude_srcs: [
1233 "bar-doc/b.java"
1234 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001235 api_levels_annotations_dirs: [
1236 "droiddoc-templates-sdk",
1237 ],
1238 api_levels_annotations_enabled: true,
1239 }
1240 droiddoc {
1241 name: "bar-doc",
1242 srcs: [
1243 ":bar-stubs",
1244 "bar-doc/IFoo.aidl",
1245 ":bar-doc-aidl-srcs",
1246 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001247 custom_template: "droiddoc-templates-sdk",
1248 hdf: [
1249 "android.whichdoc offline",
1250 ],
1251 knowntags: [
1252 "bar-doc/known_oj_tags.txt",
1253 ],
1254 proofread_file: "libcore-proofread.txt",
1255 todo_file: "libcore-docs-todo.html",
1256 flags: ["-offlinemode -title \"libcore\""],
1257 args: "-offlinemode -title \"libcore\"",
1258 }
1259 `)
1260}
1261
Liz Kammer3d894b72020-08-04 09:55:13 -07001262func TestDroidstubs(t *testing.T) {
1263 ctx, _ := testJavaWithFS(t, `
1264 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001265 name: "droiddoc-templates-sdk",
1266 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001267 }
1268
1269 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001270 name: "bar-stubs",
1271 srcs: ["bar-doc/a.java"],
1272 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1273 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001274 }
1275
1276 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001277 name: "bar-stubs-other",
1278 srcs: ["bar-doc/a.java"],
1279 high_mem: true,
1280 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1281 api_levels_annotations_enabled: true,
1282 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001283 }
1284 `,
1285 map[string][]byte{
1286 "bar-doc/a.java": nil,
1287 })
1288 testcases := []struct {
1289 moduleName string
1290 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001291 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001292 }{
1293 {
1294 moduleName: "bar-stubs",
1295 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001296 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001297 },
1298 {
1299 moduleName: "bar-stubs-other",
1300 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001301 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001302 },
1303 }
1304 for _, c := range testcases {
1305 m := ctx.ModuleForTests(c.moduleName, "android_common")
1306 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001307 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001308 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001309 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001310 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1311 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001312
1313 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1314 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1315 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001316 }
1317}
1318
Paul Duffin83a2d962019-11-19 19:44:10 +00001319func TestDroidstubsWithSystemModules(t *testing.T) {
1320 ctx, _ := testJava(t, `
1321 droidstubs {
1322 name: "stubs-source-system-modules",
1323 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001324 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001325 ],
1326 sdk_version: "none",
1327 system_modules: "source-system-modules",
1328 }
1329
1330 java_library {
1331 name: "source-jar",
1332 srcs: [
1333 "a.java",
1334 ],
1335 }
1336
1337 java_system_modules {
1338 name: "source-system-modules",
1339 libs: ["source-jar"],
1340 }
1341
1342 droidstubs {
1343 name: "stubs-prebuilt-system-modules",
1344 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001345 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001346 ],
1347 sdk_version: "none",
1348 system_modules: "prebuilt-system-modules",
1349 }
1350
1351 java_import {
1352 name: "prebuilt-jar",
1353 jars: ["a.jar"],
1354 }
1355
1356 java_system_modules_import {
1357 name: "prebuilt-system-modules",
1358 libs: ["prebuilt-jar"],
1359 }
1360 `)
1361
1362 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1363
1364 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1365}
1366
1367func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1368 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1369 var systemJars []string
1370 for _, i := range metalavaRule.Implicits {
1371 systemJars = append(systemJars, i.Base())
1372 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001373 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001374 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1375 }
1376}
1377
Colin Cross54250902017-12-05 09:28:08 -08001378func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001379 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001380 java_library {
1381 name: "foo",
1382 srcs: ["a.java"],
1383 }
1384
1385 java_genrule {
1386 name: "jargen",
1387 tool_files: ["b.java"],
1388 cmd: "$(location b.java) $(in) $(out)",
1389 out: ["jargen.jar"],
1390 srcs: [":foo"],
1391 }
1392
1393 java_library {
1394 name: "bar",
1395 static_libs: ["jargen"],
1396 srcs: ["c.java"],
1397 }
1398
1399 java_library {
1400 name: "baz",
1401 libs: ["jargen"],
1402 srcs: ["c.java"],
1403 }
1404 `)
1405
1406 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1407 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1408 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1409 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1410 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1411
Colin Cross3d680512020-11-13 16:23:53 -08001412 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1413 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001414 }
1415
1416 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1417 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1418 }
1419
1420 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1421 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1422 }
1423
1424 if len(barCombined.Inputs) != 2 ||
1425 barCombined.Inputs[0].String() != bar.Output.String() ||
1426 barCombined.Inputs[1].String() != jargen.Output.String() {
1427 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1428 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1429 }
1430}
1431
Nan Zhang27e284d2018-02-09 21:03:53 +00001432func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001433 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001434 java_library {
1435 name: "foo",
1436 srcs: ["a.java", ":foo-srcs"],
1437 exclude_srcs: ["a.java", ":foo-excludes"],
1438 }
1439
1440 filegroup {
1441 name: "foo-srcs",
1442 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1443 }
1444
1445 filegroup {
1446 name: "foo-excludes",
1447 srcs: ["java-fg/a.java", "java-fg/b.java"],
1448 }
1449 `)
1450
1451 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1452
1453 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1454 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1455 }
1456}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001457
Paul Duffin52d398a2019-06-11 12:31:14 +01001458func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001459 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001460 "libcore/Android.bp": []byte(`
1461 java_library {
1462 name: "core",
1463 sdk_version: "none",
1464 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001465 }
1466
1467 filegroup {
1468 name: "core-jar",
1469 srcs: [":core{.jar}"],
1470 }
1471`),
1472 })
Colin Crossae8600b2020-10-29 17:09:13 -07001473 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001474 run(t, ctx, config)
1475}
1476
1477func TestJavaImport(t *testing.T) {
1478 config := testConfig(nil, "", map[string][]byte{
1479 "libcore/Android.bp": []byte(`
1480 java_import {
1481 name: "core",
1482 sdk_version: "none",
1483 }
1484
1485 filegroup {
1486 name: "core-jar",
1487 srcs: [":core{.jar}"],
1488 }
1489`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001490 })
Colin Crossae8600b2020-10-29 17:09:13 -07001491 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001492 run(t, ctx, config)
1493}
1494
Jiyong Parkc678ad32018-04-10 13:07:10 +09001495func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001496 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001497 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001498 name: "droiddoc-templates-sdk",
1499 path: ".",
1500 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001501 java_sdk_library {
1502 name: "foo",
1503 srcs: ["a.java", "b.java"],
1504 api_packages: ["foo"],
1505 }
1506 java_sdk_library {
1507 name: "bar",
1508 srcs: ["a.java", "b.java"],
1509 api_packages: ["bar"],
1510 }
1511 java_library {
1512 name: "baz",
1513 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001514 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001515 sdk_version: "system_current",
1516 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001517 java_sdk_library {
1518 name: "barney",
1519 srcs: ["c.java"],
1520 api_only: true,
1521 }
1522 java_sdk_library {
1523 name: "betty",
1524 srcs: ["c.java"],
1525 shared_library: false,
1526 }
Paul Duffin859fe962020-05-15 10:20:31 +01001527 java_sdk_library_import {
1528 name: "quuz",
1529 public: {
1530 jars: ["c.jar"],
1531 },
1532 }
1533 java_sdk_library_import {
1534 name: "fred",
1535 public: {
1536 jars: ["b.jar"],
1537 },
1538 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001539 java_sdk_library_import {
1540 name: "wilma",
1541 public: {
1542 jars: ["b.jar"],
1543 },
1544 shared_library: false,
1545 }
Jiyong Park1be96912018-05-28 18:02:19 +09001546 java_library {
1547 name: "qux",
1548 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001549 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001550 sdk_version: "system_current",
1551 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001552 java_library {
1553 name: "baz-test",
1554 srcs: ["c.java"],
1555 libs: ["foo"],
1556 sdk_version: "test_current",
1557 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001558 java_library {
1559 name: "baz-29",
1560 srcs: ["c.java"],
1561 libs: ["foo"],
1562 sdk_version: "system_29",
1563 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001564 java_library {
1565 name: "baz-module-30",
1566 srcs: ["c.java"],
1567 libs: ["foo"],
1568 sdk_version: "module_30",
1569 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001570 `)
1571
1572 // check the existence of the internal modules
1573 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001574 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1575 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1576 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1577 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1578 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1579 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001580 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001581 ctx.ModuleForTests("foo.api.public.28", "")
1582 ctx.ModuleForTests("foo.api.system.28", "")
1583 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001584
1585 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1586 // tests if baz is actually linked to the stubs lib
1587 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1588 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1589 "foo.stubs.system.jar")
1590 }
1591 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001592 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001593 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001594 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001595 }
1596 // test if baz is not linked to the system variant of foo
1597 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1598 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1599 "foo.stubs.jar")
1600 }
Jiyong Park1be96912018-05-28 18:02:19 +09001601
Paul Duffin726d23c2020-01-22 16:30:37 +00001602 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1603 // tests if baz-test is actually linked to the test stubs lib
1604 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1605 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1606 "foo.stubs.test.jar")
1607 }
1608
Paul Duffina2db18f2020-01-22 17:11:15 +00001609 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1610 // tests if baz-29 is actually linked to the system 29 stubs lib
1611 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1612 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1613 "prebuilts/sdk/29/system/foo.jar")
1614 }
1615
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001616 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1617 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1618 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1619 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1620 "prebuilts/sdk/30/module-lib/foo.jar")
1621 }
1622
Jiyong Park1be96912018-05-28 18:02:19 +09001623 // test if baz has exported SDK lib names foo and bar to qux
1624 qux := ctx.ModuleForTests("qux", "android_common")
1625 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001626 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001627 if w := []string{"foo", "bar", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
Paul Duffin859fe962020-05-15 10:20:31 +01001628 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001629 }
1630 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001631}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001632
Anton Hansson7f66efa2020-10-08 14:47:23 +01001633func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
1634 ctx, _ := testJava(t, `
1635 java_sdk_library {
1636 name: "sdk_lib",
1637 srcs: ["a.java"],
1638 impl_only_libs: ["foo"],
1639 stub_only_libs: ["bar"],
1640 }
1641 java_library {
1642 name: "foo",
1643 srcs: ["a.java"],
1644 sdk_version: "current",
1645 }
1646 java_library {
1647 name: "bar",
1648 srcs: ["a.java"],
1649 sdk_version: "current",
1650 }
1651 `)
1652
1653 for _, implName := range []string{"sdk_lib", "sdk_lib.impl"} {
1654 implJavacCp := ctx.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
1655 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1656 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1657 }
1658 }
1659 stubName := apiScopePublic.stubsLibraryModuleName("sdk_lib")
1660 stubsJavacCp := ctx.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
1661 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1662 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1663 }
1664}
1665
Paul Duffindaaa3322020-05-26 18:13:57 +01001666func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1667 ctx, _ := testJava(t, `
1668 java_sdk_library {
1669 name: "foo",
1670 srcs: ["a.java"],
1671 api_only: true,
1672 public: {
1673 enabled: true,
1674 },
1675 }
1676
1677 java_library {
1678 name: "bar",
1679 srcs: ["b.java"],
1680 libs: ["foo"],
1681 }
1682 `)
1683
1684 // The bar library should depend on the stubs jar.
1685 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1686 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1687 t.Errorf("expected %q, found %#q", expected, actual)
1688 }
1689}
1690
Paul Duffin46dc45a2020-05-14 15:39:10 +01001691func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1692 testJava(t, `
1693 java_sdk_library {
1694 name: "foo",
1695 srcs: ["a.java"],
1696 api_packages: ["foo"],
1697 public: {
1698 enabled: true,
1699 },
1700 }
1701
1702 java_library {
1703 name: "bar",
1704 srcs: ["b.java", ":foo{.public.stubs.source}"],
1705 }
1706 `)
1707}
1708
1709func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1710 testJavaError(t, `"foo" does not provide api scope system`, `
1711 java_sdk_library {
1712 name: "foo",
1713 srcs: ["a.java"],
1714 api_packages: ["foo"],
1715 public: {
1716 enabled: true,
1717 },
1718 }
1719
1720 java_library {
1721 name: "bar",
1722 srcs: ["b.java", ":foo{.system.stubs.source}"],
1723 }
1724 `)
1725}
1726
Paul Duffinca8d9a52020-06-26 22:20:25 +01001727func TestJavaSdkLibrary_Deps(t *testing.T) {
1728 ctx, _ := testJava(t, `
1729 java_sdk_library {
1730 name: "sdklib",
1731 srcs: ["a.java"],
1732 sdk_version: "none",
1733 system_modules: "none",
1734 public: {
1735 enabled: true,
1736 },
1737 }
1738 `)
1739
Paul Duffincee7e662020-07-09 17:32:57 +01001740 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001741 `dex2oatd`,
1742 `sdklib.impl`,
1743 `sdklib.stubs`,
1744 `sdklib.stubs.source`,
1745 `sdklib.xml`,
1746 })
1747}
1748
Paul Duffin46dc45a2020-05-14 15:39:10 +01001749func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1750 testJava(t, `
1751 java_sdk_library_import {
1752 name: "foo",
1753 public: {
1754 jars: ["a.jar"],
1755 stub_srcs: ["a.java"],
1756 current_api: "api/current.txt",
1757 removed_api: "api/removed.txt",
1758 },
1759 }
1760
1761 java_library {
1762 name: "bar",
1763 srcs: [":foo{.public.stubs.source}"],
1764 java_resources: [
1765 ":foo{.public.api.txt}",
1766 ":foo{.public.removed-api.txt}",
1767 ],
1768 }
1769 `)
1770}
1771
1772func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1773 bp := `
1774 java_sdk_library_import {
1775 name: "foo",
1776 public: {
1777 jars: ["a.jar"],
1778 },
1779 }
1780 `
1781
1782 t.Run("stubs.source", func(t *testing.T) {
1783 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1784 java_library {
1785 name: "bar",
1786 srcs: [":foo{.public.stubs.source}"],
1787 java_resources: [
1788 ":foo{.public.api.txt}",
1789 ":foo{.public.removed-api.txt}",
1790 ],
1791 }
1792 `)
1793 })
1794
1795 t.Run("api.txt", func(t *testing.T) {
1796 testJavaError(t, `api.txt not available for api scope public`, bp+`
1797 java_library {
1798 name: "bar",
1799 srcs: ["a.java"],
1800 java_resources: [
1801 ":foo{.public.api.txt}",
1802 ],
1803 }
1804 `)
1805 })
1806
1807 t.Run("removed-api.txt", func(t *testing.T) {
1808 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1809 java_library {
1810 name: "bar",
1811 srcs: ["a.java"],
1812 java_resources: [
1813 ":foo{.public.removed-api.txt}",
1814 ],
1815 }
1816 `)
1817 })
1818}
1819
Paul Duffin3375e352020-04-28 10:44:03 +01001820func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1821 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1822 java_sdk_library {
1823 name: "foo",
1824 srcs: ["a.java", "b.java"],
1825 api_packages: ["foo"],
1826 // Explicitly disable public to test the check that ensures the set of enabled
1827 // scopes is consistent.
1828 public: {
1829 enabled: false,
1830 },
1831 system: {
1832 enabled: true,
1833 },
1834 }
1835 `)
1836}
1837
Paul Duffin87a05a32020-05-12 11:50:28 +01001838func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1839 testJava(t, `
1840 java_sdk_library {
1841 name: "foo",
1842 srcs: ["a.java", "b.java"],
1843 api_packages: ["foo"],
1844 system: {
1845 enabled: true,
1846 sdk_version: "module_current",
1847 },
1848 }
1849 `)
1850}
1851
Paul Duffin0c5bae52020-06-02 13:00:08 +01001852func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
1853 testJava(t, `
1854 java_sdk_library {
1855 name: "foo",
1856 srcs: ["a.java", "b.java"],
1857 api_packages: ["foo"],
1858 system: {
1859 enabled: true,
1860 },
1861 module_lib: {
1862 enabled: true,
1863 },
1864 }
1865 `)
1866}
1867
1868func TestJavaSdkLibrary_SystemServer(t *testing.T) {
1869 testJava(t, `
1870 java_sdk_library {
1871 name: "foo",
1872 srcs: ["a.java", "b.java"],
1873 api_packages: ["foo"],
1874 system: {
1875 enabled: true,
1876 },
1877 system_server: {
1878 enabled: true,
1879 },
1880 }
1881 `)
1882}
1883
Paul Duffin803a9562020-05-20 11:52:25 +01001884func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1885 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1886 java_sdk_library {
1887 name: "foo",
1888 srcs: ["a.java"],
1889 public: {
1890 enabled: false,
1891 },
1892 }
1893
1894 java_library {
1895 name: "baz",
1896 srcs: ["a.java"],
1897 libs: ["foo"],
1898 sdk_version: "module_current",
1899 }
1900 `)
1901}
1902
1903func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1904 testJava(t, `
1905 java_sdk_library {
1906 name: "foo",
1907 srcs: ["a.java"],
1908 system: {
1909 enabled: true,
1910 },
1911 }
1912
1913 java_library {
1914 name: "baz",
1915 srcs: ["a.java"],
1916 libs: ["foo"],
1917 // foo does not have module-lib scope so it should fallback to system
1918 sdk_version: "module_current",
1919 }
1920 `)
1921}
1922
Jiyong Park932cdfe2020-05-28 00:19:53 +09001923func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1924 ctx, _ := testJava(t, `
1925 java_sdk_library {
1926 name: "foo",
1927 srcs: ["a.java"],
1928 system: {
1929 enabled: true,
1930 },
1931 default_to_stubs: true,
1932 }
1933
1934 java_library {
1935 name: "baz",
1936 srcs: ["a.java"],
1937 libs: ["foo"],
1938 // does not have sdk_version set, should fallback to module,
1939 // which will then fallback to system because the module scope
1940 // is not enabled.
1941 }
1942 `)
1943 // The baz library should depend on the system stubs jar.
1944 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1945 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1946 t.Errorf("expected %q, found %#q", expected, actual)
1947 }
1948}
1949
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001950var compilerFlagsTestCases = []struct {
1951 in string
1952 out bool
1953}{
1954 {
1955 in: "a",
1956 out: false,
1957 },
1958 {
1959 in: "-a",
1960 out: true,
1961 },
1962 {
1963 in: "-no-jdk",
1964 out: false,
1965 },
1966 {
1967 in: "-no-stdlib",
1968 out: false,
1969 },
1970 {
1971 in: "-kotlin-home",
1972 out: false,
1973 },
1974 {
1975 in: "-kotlin-home /some/path",
1976 out: false,
1977 },
1978 {
1979 in: "-include-runtime",
1980 out: false,
1981 },
1982 {
1983 in: "-Xintellij-plugin-root",
1984 out: false,
1985 },
1986}
1987
1988type mockContext struct {
1989 android.ModuleContext
1990 result bool
1991}
1992
1993func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1994 // CheckBadCompilerFlags calls this function when the flag should be rejected
1995 ctx.result = false
1996}
1997
1998func TestCompilerFlags(t *testing.T) {
1999 for _, testCase := range compilerFlagsTestCases {
2000 ctx := &mockContext{result: true}
2001 CheckKotlincFlags(ctx, []string{testCase.in})
2002 if ctx.result != testCase.out {
2003 t.Errorf("incorrect output:")
2004 t.Errorf(" input: %#v", testCase.in)
2005 t.Errorf(" expected: %#v", testCase.out)
2006 t.Errorf(" got: %#v", ctx.result)
2007 }
2008 }
2009}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002010
2011// TODO(jungjw): Consider making this more robust by ignoring path order.
2012func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2013 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2014 flags := strings.Split(variables["javacFlags"], " ")
2015 got := ""
2016 for _, flag := range flags {
2017 keyEnd := strings.Index(flag, "=")
2018 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2019 got = flag[keyEnd+1:]
2020 break
2021 }
2022 }
2023 if expected != got {
2024 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2025 }
2026}
2027
2028func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002029 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002030 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002031 bp := `
2032 java_library {
2033 name: "foo",
2034 srcs: ["a.java"],
2035 java_version: "1.8",
2036 }
2037
2038 java_library {
2039 name: "bar",
2040 srcs: ["b.java"],
2041 sdk_version: "none",
2042 system_modules: "none",
2043 patch_module: "java.base",
2044 java_version: "1.8",
2045 }
2046
2047 java_library {
2048 name: "baz",
2049 srcs: ["c.java"],
2050 patch_module: "java.base",
2051 java_version: "1.8",
2052 }
2053 `
2054 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002055
2056 checkPatchModuleFlag(t, ctx, "foo", "")
2057 checkPatchModuleFlag(t, ctx, "bar", "")
2058 checkPatchModuleFlag(t, ctx, "baz", "")
2059 })
2060
Pete Gillin0c2143e2019-05-02 15:32:11 +01002061 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002062 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002063 bp := `
2064 java_library {
2065 name: "foo",
2066 srcs: ["a.java"],
2067 }
2068
2069 java_library {
2070 name: "bar",
2071 srcs: ["b.java"],
2072 sdk_version: "none",
2073 system_modules: "none",
2074 patch_module: "java.base",
2075 }
2076
2077 java_library {
2078 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002079 srcs: [
2080 "c.java",
2081 // Tests for b/150878007
2082 "dir/d.java",
2083 "dir2/e.java",
2084 "dir2/f.java",
2085 "nested/dir/g.java"
2086 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002087 patch_module: "java.base",
2088 }
2089 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002090 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002091
2092 checkPatchModuleFlag(t, ctx, "foo", "")
2093 expected := "java.base=.:" + buildDir
2094 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002095 expected = "java.base=" + strings.Join([]string{
2096 ".", buildDir, "dir", "dir2", "nested", moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002097 checkPatchModuleFlag(t, ctx, "baz", expected)
2098 })
2099}
Paul Duffina7b9f422020-01-10 17:12:18 +00002100
2101func TestJavaSystemModules(t *testing.T) {
2102 ctx, _ := testJava(t, `
2103 java_system_modules {
2104 name: "system-modules",
2105 libs: ["system-module1", "system-module2"],
2106 }
2107 java_library {
2108 name: "system-module1",
2109 srcs: ["a.java"],
2110 sdk_version: "none",
2111 system_modules: "none",
2112 }
2113 java_library {
2114 name: "system-module2",
2115 srcs: ["b.java"],
2116 sdk_version: "none",
2117 system_modules: "none",
2118 }
2119 `)
2120
2121 // check the existence of the module
2122 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2123
2124 cmd := systemModules.Rule("jarsTosystemModules")
2125
2126 // make sure the command compiles against the supplied modules.
2127 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2128 if !strings.Contains(cmd.Args["classpath"], module) {
2129 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2130 module)
2131 }
2132 }
2133}
Paul Duffin90169ba2020-01-10 17:16:44 +00002134
2135func TestJavaSystemModulesImport(t *testing.T) {
2136 ctx, _ := testJava(t, `
2137 java_system_modules_import {
2138 name: "system-modules",
2139 libs: ["system-module1", "system-module2"],
2140 }
2141 java_import {
2142 name: "system-module1",
2143 jars: ["a.jar"],
2144 }
2145 java_import {
2146 name: "system-module2",
2147 jars: ["b.jar"],
2148 }
2149 `)
2150
2151 // check the existence of the module
2152 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2153
2154 cmd := systemModules.Rule("jarsTosystemModules")
2155
2156 // make sure the command compiles against the supplied modules.
2157 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2158 if !strings.Contains(cmd.Args["classpath"], module) {
2159 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2160 module)
2161 }
2162 }
2163}
Paul Duffin83a2d962019-11-19 19:44:10 +00002164
2165func TestJavaLibraryWithSystemModules(t *testing.T) {
2166 ctx, _ := testJava(t, `
2167 java_library {
2168 name: "lib-with-source-system-modules",
2169 srcs: [
2170 "a.java",
2171 ],
2172 sdk_version: "none",
2173 system_modules: "source-system-modules",
2174 }
2175
2176 java_library {
2177 name: "source-jar",
2178 srcs: [
2179 "a.java",
2180 ],
2181 }
2182
2183 java_system_modules {
2184 name: "source-system-modules",
2185 libs: ["source-jar"],
2186 }
2187
2188 java_library {
2189 name: "lib-with-prebuilt-system-modules",
2190 srcs: [
2191 "a.java",
2192 ],
2193 sdk_version: "none",
2194 system_modules: "prebuilt-system-modules",
2195 }
2196
2197 java_import {
2198 name: "prebuilt-jar",
2199 jars: ["a.jar"],
2200 }
2201
2202 java_system_modules_import {
2203 name: "prebuilt-system-modules",
2204 libs: ["prebuilt-jar"],
2205 }
2206 `)
2207
2208 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2209
2210 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2211}
2212
2213func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2214 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2215 bootClasspath := javacRule.Args["bootClasspath"]
2216 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2217 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2218 }
2219}
Jiyong Park19604de2020-03-24 16:44:11 +09002220
2221func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2222 ctx, _ := testJava(t, `
2223 java_library {
2224 name: "foo",
2225 srcs: ["aidl/foo/IFoo.aidl"],
2226 libs: ["bar"],
2227 }
2228
2229 java_import {
2230 name: "bar",
2231 jars: ["a.jar"],
2232 aidl: {
2233 export_include_dirs: ["aidl/bar"],
2234 },
2235 }
2236 `)
2237
2238 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2239 expectedAidlFlag := "-Iaidl/bar"
2240 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2241 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2242 }
2243}
Liz Kammerdd849a82020-06-12 16:38:45 -07002244
2245func TestDataNativeBinaries(t *testing.T) {
2246 ctx, config := testJava(t, `
2247 java_test_host {
2248 name: "foo",
2249 srcs: ["a.java"],
2250 data_native_bins: ["bin"]
2251 }
2252
2253 python_binary_host {
2254 name: "bin",
2255 srcs: ["bin.py"],
2256 }
2257 `)
2258
2259 buildOS := android.BuildOs.String()
2260
2261 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2262 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
2263 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2264 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2265 if !reflect.DeepEqual(expected, actual) {
2266 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2267 }
2268}