blob: 2fd4121aab04bf38ce36817d72b4c295cdf2eca2 [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"
Colin Crossa4f08812018-10-02 22:03:40 -070032 "android/soong/genrule"
Liz Kammerdd849a82020-06-12 16:38:45 -070033 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070034)
35
36var buildDir string
37
38func setUp() {
39 var err error
40 buildDir, err = ioutil.TempDir("", "soong_java_test")
41 if err != nil {
42 panic(err)
43 }
44}
45
46func tearDown() {
47 os.RemoveAll(buildDir)
48}
49
50func TestMain(m *testing.M) {
51 run := func() int {
52 setUp()
53 defer tearDown()
54
55 return m.Run()
56 }
57
58 os.Exit(run())
59}
Colin Cross527012a2017-11-30 22:56:16 -080060
Colin Cross98be1bb2019-12-13 20:41:13 -080061func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000062 bp += dexpreopt.BpToolModulesForTest()
63
Martin Stjernholm75a48d82020-01-10 20:32:59 +000064 config := TestConfig(buildDir, env, bp, fs)
65
66 // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that
67 // it doesn't create a real one, which would fail.
68 _ = dexpreopt.GlobalSoongConfigForTests(config)
69
70 return config
Colin Cross1369cdb2017-09-29 17:58:17 -070071}
72
Colin Cross98be1bb2019-12-13 20:41:13 -080073func testContext() *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070074
Colin Cross4c428df2017-09-15 17:36:05 -070075 ctx := android.NewTestArchContext()
Paul Duffinf9b1da02019-12-18 19:51:55 +000076 RegisterJavaBuildComponents(ctx)
77 RegisterAppBuildComponents(ctx)
78 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000079 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000080 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080081 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080082 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
83 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070084 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000085 RegisterDocsBuildComponents(ctx)
86 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000087 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070088 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010089 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000090
91 RegisterPrebuiltApisBuildComponents(ctx)
92
Liz Kammerdd849a82020-06-12 16:38:45 -070093 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070094 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080095 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070096 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070097
Paul Duffin021f4e52020-07-30 16:04:17 +010098 android.RegisterPrebuiltMutators(ctx)
99
Colin Crossa4f08812018-10-02 22:03:40 -0700100 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000101 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700102
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000103 dexpreopt.RegisterToolModulesForTest(ctx)
104
Colin Cross527012a2017-11-30 22:56:16 -0800105 return ctx
106}
107
108func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800109 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800110
Colin Cross98be1bb2019-12-13 20:41:13 -0800111 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000112 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800113
Colin Cross98be1bb2019-12-13 20:41:13 -0800114 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100115 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800116 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700117 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800118 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800119}
120
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900121func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900122 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800123 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900124}
125
Colin Cross98be1bb2019-12-13 20:41:13 -0800126func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900127 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800128 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900129
Colin Cross98be1bb2019-12-13 20:41:13 -0800130 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000131 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900132
Colin Cross98be1bb2019-12-13 20:41:13 -0800133 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900134 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
135 if len(errs) > 0 {
136 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900137 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900138 }
139 _, errs = ctx.PrepareBuildActions(config)
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
145 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900146
147 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900148}
149
Colin Cross238c1f32020-06-07 16:58:18 -0700150func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
151 t.Helper()
152 return testJavaWithConfig(t, testConfig(nil, bp, fs))
153}
154
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700155func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800156 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700157 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900158}
159
Colin Cross98be1bb2019-12-13 20:41:13 -0800160func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900161 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800162 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800163 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700164
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700165 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700166}
167
Colin Cross2acdae82017-09-15 19:44:24 -0700168func moduleToPath(name string) string {
169 switch {
170 case name == `""`:
171 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700172 case strings.HasSuffix(name, ".jar"):
173 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700174 default:
175 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700176 }
177}
178
Jeongik Chae403e9e2019-12-07 00:16:24 +0900179func TestJavaLinkType(t *testing.T) {
180 testJava(t, `
181 java_library {
182 name: "foo",
183 srcs: ["a.java"],
184 libs: ["bar"],
185 static_libs: ["baz"],
186 }
187
188 java_library {
189 name: "bar",
190 sdk_version: "current",
191 srcs: ["b.java"],
192 }
193
194 java_library {
195 name: "baz",
196 sdk_version: "system_current",
197 srcs: ["c.java"],
198 }
199 `)
200
201 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.", `
202 java_library {
203 name: "foo",
204 srcs: ["a.java"],
205 libs: ["bar"],
206 sdk_version: "current",
207 static_libs: ["baz"],
208 }
209
210 java_library {
211 name: "bar",
212 sdk_version: "current",
213 srcs: ["b.java"],
214 }
215
216 java_library {
217 name: "baz",
218 sdk_version: "system_current",
219 srcs: ["c.java"],
220 }
221 `)
222
223 testJava(t, `
224 java_library {
225 name: "foo",
226 srcs: ["a.java"],
227 libs: ["bar"],
228 sdk_version: "system_current",
229 static_libs: ["baz"],
230 }
231
232 java_library {
233 name: "bar",
234 sdk_version: "current",
235 srcs: ["b.java"],
236 }
237
238 java_library {
239 name: "baz",
240 sdk_version: "system_current",
241 srcs: ["c.java"],
242 }
243 `)
244
245 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.", `
246 java_library {
247 name: "foo",
248 srcs: ["a.java"],
249 libs: ["bar"],
250 sdk_version: "system_current",
251 static_libs: ["baz"],
252 }
253
254 java_library {
255 name: "bar",
256 sdk_version: "current",
257 srcs: ["b.java"],
258 }
259
260 java_library {
261 name: "baz",
262 srcs: ["c.java"],
263 }
264 `)
265}
266
Colin Cross72bb3632017-07-13 16:23:21 -0700267func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700268 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700269 java_library {
270 name: "foo",
271 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700272 libs: ["bar"],
273 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700274 }
275
276 java_library {
277 name: "bar",
278 srcs: ["b.java"],
279 }
280
281 java_library {
282 name: "baz",
283 srcs: ["c.java"],
284 }
Colin Crossd5934c82017-10-02 13:55:26 -0700285 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700286
Colin Cross4c428df2017-09-15 17:36:05 -0700287 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700288 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700289
290 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
291 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
292 }
293
Colin Cross1ee23172017-10-18 14:44:18 -0700294 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700295 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
296 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700297
Nan Zhanged19fc32017-10-19 13:06:22 -0700298 if !strings.Contains(javac.Args["classpath"], barTurbine) {
299 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700300 }
301
Nan Zhanged19fc32017-10-19 13:06:22 -0700302 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
303 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700304 }
305
306 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
307 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700308 }
309}
310
Artur Satayev9cf46692019-11-26 18:08:34 +0000311func TestExportedPlugins(t *testing.T) {
312 type Result struct {
313 library string
314 processors string
315 }
316 var tests = []struct {
317 name string
318 extra string
319 results []Result
320 }{
321 {
322 name: "Exported plugin is not a direct plugin",
323 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
324 results: []Result{{library: "exports", processors: "-proc:none"}},
325 },
326 {
327 name: "Exports plugin to dependee",
328 extra: `
329 java_library{name: "exports", exported_plugins: ["plugin"]}
330 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
331 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
332 `,
333 results: []Result{
334 {library: "foo", processors: "-processor com.android.TestPlugin"},
335 {library: "bar", processors: "-processor com.android.TestPlugin"},
336 },
337 },
338 {
339 name: "Exports plugin to android_library",
340 extra: `
341 java_library{name: "exports", exported_plugins: ["plugin"]}
342 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
343 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
344 `,
345 results: []Result{
346 {library: "foo", processors: "-processor com.android.TestPlugin"},
347 {library: "bar", processors: "-processor com.android.TestPlugin"},
348 },
349 },
350 {
351 name: "Exports plugin is not propagated via transitive deps",
352 extra: `
353 java_library{name: "exports", exported_plugins: ["plugin"]}
354 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
355 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
356 `,
357 results: []Result{
358 {library: "foo", processors: "-processor com.android.TestPlugin"},
359 {library: "bar", processors: "-proc:none"},
360 },
361 },
362 {
363 name: "Exports plugin appends to plugins",
364 extra: `
365 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
366 java_library{name: "exports", exported_plugins: ["plugin"]}
367 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
368 `,
369 results: []Result{
370 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
371 },
372 },
373 }
374
375 for _, test := range tests {
376 t.Run(test.name, func(t *testing.T) {
377 ctx, _ := testJava(t, `
378 java_plugin {
379 name: "plugin",
380 processor_class: "com.android.TestPlugin",
381 }
382 `+test.extra)
383
384 for _, want := range test.results {
385 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
386 if javac.Args["processor"] != want.processors {
387 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
388 }
389 }
390 })
391 }
392}
393
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900394func TestSdkVersionByPartition(t *testing.T) {
395 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900396 java_library {
397 name: "foo",
398 srcs: ["a.java"],
399 vendor: true,
400 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900401 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900402
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900403 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900404 java_library {
405 name: "bar",
406 srcs: ["b.java"],
407 }
408 `)
409
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900410 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900411 bp := `
412 java_library {
413 name: "foo",
414 srcs: ["a.java"],
415 product_specific: true,
416 }
417 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800418
419 config := testConfig(nil, bp, nil)
420 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900421 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800422 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 +0900423 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800424 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900425 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900426 }
427}
428
Colin Crossd5934c82017-10-02 13:55:26 -0700429func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700430 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700431 java_library {
432 name: "foo",
433 srcs: ["a.java"],
434 target: {
435 android: {
436 srcs: ["b.java"],
437 },
438 },
439 }
440 `)
441
442 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
443 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
444 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
445 }
446}
447
Colin Cross6b4a32d2017-12-05 13:42:45 -0800448func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700449 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800450 java_library_host {
451 name: "foo",
452 srcs: ["a.java"],
453 }
454
455 java_binary_host {
456 name: "bar",
457 srcs: ["b.java"],
458 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700459 jni_libs: ["libjni"],
460 }
461
462 cc_library_shared {
463 name: "libjni",
464 host_supported: true,
465 device_supported: false,
466 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800467 }
468 `)
469
470 buildOS := android.BuildOs.String()
471
472 bar := ctx.ModuleForTests("bar", buildOS+"_common")
473 barJar := bar.Output("bar.jar").Output.String()
474 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
475 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
476
Colin Cross89226d92020-10-09 19:00:54 -0700477 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
478 libjniSO := libjni.Rule("Cp").Output.String()
479
Colin Cross6b4a32d2017-12-05 13:42:45 -0800480 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700481 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
482 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800483 }
Colin Cross89226d92020-10-09 19:00:54 -0700484
485 // Test that the install binary wrapper depends on the installed JNI libraries
486 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
487 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
488 }
Alex Humesky2070e322020-06-09 20:23:08 -0400489}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800490
Alex Humesky2070e322020-06-09 20:23:08 -0400491func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
492 bp := `
493 java_library {
494 name: "target_library",
495 srcs: ["a.java"],
496 }
497
498 java_binary_host {
499 name: "host_binary",
500 srcs: ["b.java"],
501 }
502 `
503 config := testConfig(nil, bp, nil)
504 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
505
506 ctx, _ := testJavaWithConfig(t, config)
507
Liz Kammer7941b302020-07-28 13:27:34 -0700508 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400509 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
510 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
511 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
512 t.Errorf("target library javac flags %v should contain "+
513 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
514 }
515
516 // check that -g is not overridden for host modules
517 buildOS := android.BuildOs.String()
518 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
519 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
520 if strings.Contains(hostJavaFlags, "-g:source,lines") {
521 t.Errorf("java_binary_host javac flags %v should not have "+
522 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
523 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800524}
525
Colin Cross72bb3632017-07-13 16:23:21 -0700526func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700527 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700528 java_library {
529 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000530 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000531 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700532 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700533 }
534
Colin Cross74d73e22017-08-02 11:05:49 -0700535 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700536 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700537 jars: ["a.jar"],
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: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700542 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700543 sdk_version: "current",
544 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700545 }
Colin Cross42be7612019-02-21 18:12:14 -0800546
547 dex_import {
548 name: "qux",
549 jars: ["b.jar"],
550 }
Colin Cross79c7c262019-04-17 11:11:46 -0700551
552 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000553 name: "sdklib",
554 public: {
555 jars: ["c.jar"],
556 },
557 }
558
Paul Duffin91547182019-11-12 19:39:36 +0000559 prebuilt_stubs_sources {
560 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000561 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000562 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000563
564 java_test_import {
565 name: "test",
566 jars: ["a.jar"],
567 test_suites: ["cts"],
568 test_config: "AndroidTest.xml",
569 }
Colin Cross72bb3632017-07-13 16:23:21 -0700570 `)
571
Paul Duffin9b478b02019-12-10 13:41:51 +0000572 fooModule := ctx.ModuleForTests("foo", "android_common")
573 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700574 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700575 barModule := ctx.ModuleForTests("bar", "android_common")
576 barJar := barModule.Rule("combineJar").Output
577 bazModule := ctx.ModuleForTests("baz", "android_common")
578 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700579 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700580
Paul Duffin9b478b02019-12-10 13:41:51 +0000581 fooLibrary := fooModule.Module().(*Library)
582 assertDeepEquals(t, "foo java sources incorrect",
583 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000584
Paul Duffin9b478b02019-12-10 13:41:51 +0000585 assertDeepEquals(t, "foo java source jars incorrect",
586 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
587 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000588
Colin Cross37f6d792018-07-12 12:28:41 -0700589 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
590 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700591 }
592
Liz Kammerd6c31d22020-08-05 15:40:41 -0700593 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
594 if barDexJar != nil {
595 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
596 }
597
Colin Cross79c7c262019-04-17 11:11:46 -0700598 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
599 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
600 }
601
Colin Cross37f6d792018-07-12 12:28:41 -0700602 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
603 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700604 }
Colin Cross42be7612019-02-21 18:12:14 -0800605
Liz Kammerd6c31d22020-08-05 15:40:41 -0700606 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
607 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
608 if bazDexJar != expectedDexJar {
609 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
610 }
611
Colin Cross42be7612019-02-21 18:12:14 -0800612 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700613}
614
Paul Duffin9b478b02019-12-10 13:41:51 +0000615func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
616 if !reflect.DeepEqual(expected, actual) {
617 t.Errorf("%s: expected %q, found %q", message, expected, actual)
618 }
619}
620
Paul Duffin56d44902020-01-31 13:36:25 +0000621func TestJavaSdkLibraryImport(t *testing.T) {
622 ctx, _ := testJava(t, `
623 java_library {
624 name: "foo",
625 srcs: ["a.java"],
626 libs: ["sdklib"],
627 sdk_version: "current",
628 }
629
630 java_library {
631 name: "foo.system",
632 srcs: ["a.java"],
633 libs: ["sdklib"],
634 sdk_version: "system_current",
635 }
636
637 java_library {
638 name: "foo.test",
639 srcs: ["a.java"],
640 libs: ["sdklib"],
641 sdk_version: "test_current",
642 }
643
644 java_sdk_library_import {
645 name: "sdklib",
646 public: {
647 jars: ["a.jar"],
648 },
649 system: {
650 jars: ["b.jar"],
651 },
652 test: {
653 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100654 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000655 },
656 }
657 `)
658
659 for _, scope := range []string{"", ".system", ".test"} {
660 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
661 javac := fooModule.Rule("javac")
662
663 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
664 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
665 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
666 }
667 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100668
Paul Duffincee7e662020-07-09 17:32:57 +0100669 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100670 `prebuilt_sdklib.stubs`,
671 `prebuilt_sdklib.stubs.source.test`,
672 `prebuilt_sdklib.stubs.system`,
673 `prebuilt_sdklib.stubs.test`,
674 })
675}
676
677func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
678 ctx, _ := testJava(t, `
679 java_sdk_library {
680 name: "sdklib",
681 srcs: ["a.java"],
682 sdk_version: "none",
683 system_modules: "none",
684 public: {
685 enabled: true,
686 },
687 }
688
689 java_sdk_library_import {
690 name: "sdklib",
691 public: {
692 jars: ["a.jar"],
693 },
694 }
695 `)
696
Paul Duffincee7e662020-07-09 17:32:57 +0100697 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100698 `dex2oatd`,
699 `prebuilt_sdklib`,
700 `sdklib.impl`,
701 `sdklib.stubs`,
702 `sdklib.stubs.source`,
703 `sdklib.xml`,
704 })
705
Paul Duffincee7e662020-07-09 17:32:57 +0100706 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100707 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100708 `sdklib.impl`,
709 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
710 // dependency is added after prebuilts may have been renamed and so has to use
711 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100712 `sdklib.xml`,
713 })
714}
715
716func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
717 ctx, _ := testJava(t, `
718 java_sdk_library {
719 name: "sdklib",
720 srcs: ["a.java"],
721 sdk_version: "none",
722 system_modules: "none",
723 public: {
724 enabled: true,
725 },
726 }
727
728 java_sdk_library_import {
729 name: "sdklib",
730 prefer: true,
731 public: {
732 jars: ["a.jar"],
733 },
734 }
735 `)
736
Paul Duffincee7e662020-07-09 17:32:57 +0100737 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100738 `dex2oatd`,
739 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100740 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100741 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100742 `sdklib.stubs.source`,
743 `sdklib.xml`,
744 })
745
Paul Duffincee7e662020-07-09 17:32:57 +0100746 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100747 `prebuilt_sdklib.stubs`,
748 `sdklib.impl`,
749 `sdklib.xml`,
750 })
Paul Duffin56d44902020-01-31 13:36:25 +0000751}
752
Colin Cross89536d42017-07-07 14:35:50 -0700753func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700754 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700755 java_defaults {
756 name: "defaults",
757 srcs: ["a.java"],
758 libs: ["bar"],
759 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700760 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700761 }
762
763 java_library {
764 name: "foo",
765 defaults: ["defaults"],
766 }
767
768 java_library {
769 name: "bar",
770 srcs: ["b.java"],
771 }
772
773 java_library {
774 name: "baz",
775 srcs: ["c.java"],
776 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700777
778 android_test {
779 name: "atestOptimize",
780 defaults: ["defaults"],
781 optimize: {enabled: true},
782 }
783
784 android_test {
785 name: "atestNoOptimize",
786 defaults: ["defaults"],
787 }
788
789 android_test {
790 name: "atestDefault",
791 srcs: ["a.java"],
792 }
Colin Cross89536d42017-07-07 14:35:50 -0700793 `)
794
Colin Cross4c428df2017-09-15 17:36:05 -0700795 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700796 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700797
798 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
799 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
800 }
801
Nan Zhanged19fc32017-10-19 13:06:22 -0700802 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
803 if !strings.Contains(javac.Args["classpath"], barTurbine) {
804 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700805 }
806
Colin Cross1ee23172017-10-18 14:44:18 -0700807 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700808 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
809 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700810 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700811
812 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
813 if atestOptimize.Output == nil {
814 t.Errorf("atestOptimize should optimize APK")
815 }
816
817 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
818 if atestNoOptimize.Output == nil {
819 t.Errorf("atestNoOptimize should not optimize APK")
820 }
821
822 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
823 if atestDefault.Output == nil {
824 t.Errorf("atestDefault should optimize APK")
825 }
Colin Cross89536d42017-07-07 14:35:50 -0700826}
827
Colin Cross0f37af02017-09-27 17:42:05 -0700828func TestResources(t *testing.T) {
829 var table = []struct {
830 name string
831 prop string
832 extra string
833 args string
834 }{
835 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700836 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700837 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800838 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700839 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700840 },
841 {
842 // Test that a module with java_resources includes the files
843 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700844 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
845 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700846 },
847 {
848 // Test that a module with a filegroup in java_resources includes the files with the
849 // path prefix
850 name: "resource filegroup",
851 prop: `java_resources: [":foo-res"]`,
852 extra: `
853 filegroup {
854 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800855 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700856 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700857 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700858 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700859 },
860 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700861 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
862 name: "wildcard dirs",
863 prop: `java_resource_dirs: ["java-res/*"]`,
864 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
865 },
866 {
867 // Test that a module exclude_java_resource_dirs excludes the files
868 name: "wildcard dirs",
869 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
870 args: "-C java-res/a -f java-res/a/a",
871 },
Colin Crosscedd4762018-09-13 11:26:19 -0700872 {
873 // Test wildcards in java_resources
874 name: "wildcard files",
875 prop: `java_resources: ["java-res/**/*"]`,
876 args: "-C . -f java-res/a/a -f java-res/b/b",
877 },
878 {
879 // Test exclude_java_resources with java_resources
880 name: "wildcard files with exclude",
881 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
882 args: "-C . -f java-res/a/a",
883 },
884 {
885 // Test exclude_java_resources with java_resource_dirs
886 name: "resource dirs with exclude files",
887 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
888 args: "-C java-res -f java-res/a/a",
889 },
890 {
891 // Test exclude_java_resource_dirs with java_resource_dirs
892 name: "resource dirs with exclude files",
893 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
894 args: "-C java-res -f java-res/a/a -f java-res/b/b",
895 },
Colin Cross0f37af02017-09-27 17:42:05 -0700896 }
897
898 for _, test := range table {
899 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700900 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700901 java_library {
902 name: "foo",
903 srcs: [
904 "a.java",
905 "b.java",
906 "c.java",
907 ],
908 `+test.prop+`,
909 }
Colin Cross238c1f32020-06-07 16:58:18 -0700910 `+test.extra,
911 map[string][]byte{
912 "java-res/a/a": nil,
913 "java-res/b/b": nil,
914 "java-res2/a": nil,
915 },
916 )
Colin Cross0f37af02017-09-27 17:42:05 -0700917
Colin Cross331a1212018-08-15 20:40:52 -0700918 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700919 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700920
921 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
922 t.Errorf("foo combined jars %v does not contain %q",
923 foo.Inputs.Strings(), fooRes.Output.String())
924 }
925
Colin Crossaf9c55b2017-10-03 14:50:08 -0700926 if fooRes.Args["jarArgs"] != test.args {
927 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700928 fooRes.Args["jarArgs"], test.args)
929 }
930 })
931 }
932}
933
Colin Cross0c4ce212019-05-03 15:28:19 -0700934func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700935 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700936 java_library {
937 name: "foo",
938 srcs: [
939 "a.java",
940 "b.java",
941 "c.java",
942 ],
943 include_srcs: true,
944 }
945
946 java_library {
947 name: "bar",
948 srcs: [
949 "a.java",
950 "b.java",
951 "c.java",
952 ],
953 java_resource_dirs: ["java-res"],
954 include_srcs: true,
955 }
Colin Cross238c1f32020-06-07 16:58:18 -0700956 `, map[string][]byte{
957 "java-res/a/a": nil,
958 "java-res/b/b": nil,
959 "java-res2/a": nil,
960 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700961
962 // Test a library with include_srcs: true
963 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
964 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
965
966 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
967 t.Errorf("foo combined jars %v does not contain %q", w, g)
968 }
969
970 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
971 t.Errorf("foo source jar args %q is not %q", w, g)
972 }
973
974 // Test a library with include_srcs: true and resources
975 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
976 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
977 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
978 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
979
980 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
981 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
982 }
983
984 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
985 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
986 }
987
988 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
989 t.Errorf("bar combined jars %v does not contain %q", w, g)
990 }
991
992 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
993 t.Errorf("bar source jar args %q is not %q", w, g)
994 }
995
996 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
997 t.Errorf("bar resource jar args %q is not %q", w, g)
998 }
999}
1000
Colin Cross54190b32017-10-09 15:34:10 -07001001func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001002 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001003 java_library {
1004 name: "foo",
1005 srcs: [
1006 "a*.java",
1007 ":gen",
1008 "b*.java",
1009 ],
1010 }
1011
1012 genrule {
1013 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001014 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001015 out: ["gen.java"],
1016 }
Colin Cross238c1f32020-06-07 16:58:18 -07001017 `, map[string][]byte{
1018 "a.java": nil,
1019 "b.java": nil,
1020 })
Colin Cross54190b32017-10-09 15:34:10 -07001021
1022 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1023 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1024
Colin Cross15e86d92017-10-20 15:07:08 -07001025 if filepath.Base(genrule.Output.String()) != "gen.java" {
1026 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001027 }
1028
1029 if len(javac.Inputs) != 3 ||
1030 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001031 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001032 javac.Inputs[2].String() != "b.java" {
1033 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1034 }
1035}
1036
Nan Zhang61eaedb2017-11-02 13:28:15 -07001037func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001038 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001039 java_library {
1040 name: "foo",
1041 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001042 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001043 }
1044
1045 java_library {
1046 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001047 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001048 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001049 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001050 }
1051
1052 java_library {
1053 name: "baz",
1054 srcs: ["c.java"],
1055 libs: ["bar"],
1056 sdk_version: "14",
1057 }
1058 `)
1059
1060 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1061 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1062 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1063 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1064 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1065
1066 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1067 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1068 }
1069
1070 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1071 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1072 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1073 }
1074 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1075 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1076 }
1077 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1078 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1079 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001080 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001081 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001082 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001083 }
1084}
1085
1086func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001087 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001088 java_library {
1089 name: "bar",
1090 srcs: ["a.java","b.java","c.java"],
1091 javac_shard_size: 1
1092 }
1093 `)
1094
1095 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1096 for i := 0; i < 3; i++ {
1097 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1098 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1099 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1100 }
1101 }
1102}
1103
Nan Zhang581fd212018-01-10 16:06:12 -08001104func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001105 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001106 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001107 name: "droiddoc-templates-sdk",
1108 path: ".",
1109 }
Jiyong Park29074592019-07-07 16:27:47 +09001110 filegroup {
1111 name: "bar-doc-aidl-srcs",
1112 srcs: ["bar-doc/IBar.aidl"],
1113 path: "bar-doc",
1114 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001115 droidstubs {
1116 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001117 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001118 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001119 ],
Steve Kim3666c702020-09-01 17:58:01 +00001120 exclude_srcs: [
1121 "bar-doc/b.java"
1122 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001123 api_levels_annotations_dirs: [
1124 "droiddoc-templates-sdk",
1125 ],
1126 api_levels_annotations_enabled: true,
1127 }
1128 droiddoc {
1129 name: "bar-doc",
1130 srcs: [
1131 ":bar-stubs",
1132 "bar-doc/IFoo.aidl",
1133 ":bar-doc-aidl-srcs",
1134 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001135 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001136 hdf: [
1137 "android.whichdoc offline",
1138 ],
1139 knowntags: [
1140 "bar-doc/known_oj_tags.txt",
1141 ],
1142 proofread_file: "libcore-proofread.txt",
1143 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001144 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001145 }
Colin Cross238c1f32020-06-07 16:58:18 -07001146 `,
1147 map[string][]byte{
1148 "bar-doc/a.java": nil,
1149 "bar-doc/b.java": nil,
1150 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001151 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1152 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1153 if err != nil {
1154 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1155 }
1156 if len(barStubsOutputs) != 1 {
1157 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001158 }
Nan Zhang581fd212018-01-10 16:06:12 -08001159
Liz Kammere1ab2502020-09-10 15:29:25 +00001160 barStubsOutput := barStubsOutputs[0]
1161 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1162 javaDoc := barDoc.Rule("javadoc")
1163 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1164 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001165 }
1166
Liz Kammere1ab2502020-09-10 15:29:25 +00001167 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1168 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1169 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1170 }
1171
1172 aidl := barDoc.Rule("aidl")
1173 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001174 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1175 }
1176
1177 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1178 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001179 }
Nan Zhang581fd212018-01-10 16:06:12 -08001180}
1181
Liz Kammer585cac22020-07-06 09:12:57 -07001182func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1183 testJavaError(t, "flags is set. Cannot set args", `
1184 droiddoc_exported_dir {
1185 name: "droiddoc-templates-sdk",
1186 path: ".",
1187 }
1188 filegroup {
1189 name: "bar-doc-aidl-srcs",
1190 srcs: ["bar-doc/IBar.aidl"],
1191 path: "bar-doc",
1192 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001193 droidstubs {
1194 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001195 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001196 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001197 ],
Steve Kim3666c702020-09-01 17:58:01 +00001198 exclude_srcs: [
1199 "bar-doc/b.java"
1200 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001201 api_levels_annotations_dirs: [
1202 "droiddoc-templates-sdk",
1203 ],
1204 api_levels_annotations_enabled: true,
1205 }
1206 droiddoc {
1207 name: "bar-doc",
1208 srcs: [
1209 ":bar-stubs",
1210 "bar-doc/IFoo.aidl",
1211 ":bar-doc-aidl-srcs",
1212 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001213 custom_template: "droiddoc-templates-sdk",
1214 hdf: [
1215 "android.whichdoc offline",
1216 ],
1217 knowntags: [
1218 "bar-doc/known_oj_tags.txt",
1219 ],
1220 proofread_file: "libcore-proofread.txt",
1221 todo_file: "libcore-docs-todo.html",
1222 flags: ["-offlinemode -title \"libcore\""],
1223 args: "-offlinemode -title \"libcore\"",
1224 }
1225 `)
1226}
1227
Liz Kammer3d894b72020-08-04 09:55:13 -07001228func TestDroidstubs(t *testing.T) {
1229 ctx, _ := testJavaWithFS(t, `
1230 droiddoc_exported_dir {
1231 name: "droiddoc-templates-sdk",
1232 path: ".",
1233 }
1234
1235 droidstubs {
1236 name: "bar-stubs",
1237 srcs: [
1238 "bar-doc/a.java",
1239 ],
1240 api_levels_annotations_dirs: [
1241 "droiddoc-templates-sdk",
1242 ],
1243 api_levels_annotations_enabled: true,
1244 }
1245
1246 droidstubs {
1247 name: "bar-stubs-other",
1248 srcs: [
1249 "bar-doc/a.java",
1250 ],
1251 api_levels_annotations_dirs: [
1252 "droiddoc-templates-sdk",
1253 ],
1254 api_levels_annotations_enabled: true,
1255 api_levels_jar_filename: "android.other.jar",
1256 }
1257 `,
1258 map[string][]byte{
1259 "bar-doc/a.java": nil,
1260 })
1261 testcases := []struct {
1262 moduleName string
1263 expectedJarFilename string
1264 }{
1265 {
1266 moduleName: "bar-stubs",
1267 expectedJarFilename: "android.jar",
1268 },
1269 {
1270 moduleName: "bar-stubs-other",
1271 expectedJarFilename: "android.other.jar",
1272 },
1273 }
1274 for _, c := range testcases {
1275 m := ctx.ModuleForTests(c.moduleName, "android_common")
1276 metalava := m.Rule("metalava")
1277 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
1278 if actual := metalava.RuleParams.Command; !strings.Contains(actual, expected) {
1279 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1280 }
1281 }
1282}
1283
Paul Duffin83a2d962019-11-19 19:44:10 +00001284func TestDroidstubsWithSystemModules(t *testing.T) {
1285 ctx, _ := testJava(t, `
1286 droidstubs {
1287 name: "stubs-source-system-modules",
1288 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001289 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001290 ],
1291 sdk_version: "none",
1292 system_modules: "source-system-modules",
1293 }
1294
1295 java_library {
1296 name: "source-jar",
1297 srcs: [
1298 "a.java",
1299 ],
1300 }
1301
1302 java_system_modules {
1303 name: "source-system-modules",
1304 libs: ["source-jar"],
1305 }
1306
1307 droidstubs {
1308 name: "stubs-prebuilt-system-modules",
1309 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001310 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001311 ],
1312 sdk_version: "none",
1313 system_modules: "prebuilt-system-modules",
1314 }
1315
1316 java_import {
1317 name: "prebuilt-jar",
1318 jars: ["a.jar"],
1319 }
1320
1321 java_system_modules_import {
1322 name: "prebuilt-system-modules",
1323 libs: ["prebuilt-jar"],
1324 }
1325 `)
1326
1327 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1328
1329 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1330}
1331
1332func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1333 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1334 var systemJars []string
1335 for _, i := range metalavaRule.Implicits {
1336 systemJars = append(systemJars, i.Base())
1337 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001338 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001339 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1340 }
1341}
1342
Colin Cross54250902017-12-05 09:28:08 -08001343func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001344 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001345 java_library {
1346 name: "foo",
1347 srcs: ["a.java"],
1348 }
1349
1350 java_genrule {
1351 name: "jargen",
1352 tool_files: ["b.java"],
1353 cmd: "$(location b.java) $(in) $(out)",
1354 out: ["jargen.jar"],
1355 srcs: [":foo"],
1356 }
1357
1358 java_library {
1359 name: "bar",
1360 static_libs: ["jargen"],
1361 srcs: ["c.java"],
1362 }
1363
1364 java_library {
1365 name: "baz",
1366 libs: ["jargen"],
1367 srcs: ["c.java"],
1368 }
1369 `)
1370
1371 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1372 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1373 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1374 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1375 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1376
1377 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1378 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1379 }
1380
1381 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1382 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1383 }
1384
1385 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1386 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1387 }
1388
1389 if len(barCombined.Inputs) != 2 ||
1390 barCombined.Inputs[0].String() != bar.Output.String() ||
1391 barCombined.Inputs[1].String() != jargen.Output.String() {
1392 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1393 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1394 }
1395}
1396
Nan Zhang27e284d2018-02-09 21:03:53 +00001397func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001398 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001399 java_library {
1400 name: "foo",
1401 srcs: ["a.java", ":foo-srcs"],
1402 exclude_srcs: ["a.java", ":foo-excludes"],
1403 }
1404
1405 filegroup {
1406 name: "foo-srcs",
1407 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1408 }
1409
1410 filegroup {
1411 name: "foo-excludes",
1412 srcs: ["java-fg/a.java", "java-fg/b.java"],
1413 }
1414 `)
1415
1416 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1417
1418 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1419 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1420 }
1421}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001422
Paul Duffin52d398a2019-06-11 12:31:14 +01001423func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001424 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001425 "libcore/Android.bp": []byte(`
1426 java_library {
1427 name: "core",
1428 sdk_version: "none",
1429 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001430 }
1431
1432 filegroup {
1433 name: "core-jar",
1434 srcs: [":core{.jar}"],
1435 }
1436`),
1437 })
1438 ctx := testContext()
1439 run(t, ctx, config)
1440}
1441
1442func TestJavaImport(t *testing.T) {
1443 config := testConfig(nil, "", map[string][]byte{
1444 "libcore/Android.bp": []byte(`
1445 java_import {
1446 name: "core",
1447 sdk_version: "none",
1448 }
1449
1450 filegroup {
1451 name: "core-jar",
1452 srcs: [":core{.jar}"],
1453 }
1454`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001455 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001456 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001457 run(t, ctx, config)
1458}
1459
Jiyong Parkc678ad32018-04-10 13:07:10 +09001460func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001461 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001462 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001463 name: "droiddoc-templates-sdk",
1464 path: ".",
1465 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001466 java_sdk_library {
1467 name: "foo",
1468 srcs: ["a.java", "b.java"],
1469 api_packages: ["foo"],
1470 }
1471 java_sdk_library {
1472 name: "bar",
1473 srcs: ["a.java", "b.java"],
1474 api_packages: ["bar"],
1475 }
1476 java_library {
1477 name: "baz",
1478 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001479 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001480 sdk_version: "system_current",
1481 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001482 java_sdk_library {
1483 name: "barney",
1484 srcs: ["c.java"],
1485 api_only: true,
1486 }
1487 java_sdk_library {
1488 name: "betty",
1489 srcs: ["c.java"],
1490 shared_library: false,
1491 }
Paul Duffin859fe962020-05-15 10:20:31 +01001492 java_sdk_library_import {
1493 name: "quuz",
1494 public: {
1495 jars: ["c.jar"],
1496 },
1497 }
1498 java_sdk_library_import {
1499 name: "fred",
1500 public: {
1501 jars: ["b.jar"],
1502 },
1503 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001504 java_sdk_library_import {
1505 name: "wilma",
1506 public: {
1507 jars: ["b.jar"],
1508 },
1509 shared_library: false,
1510 }
Jiyong Park1be96912018-05-28 18:02:19 +09001511 java_library {
1512 name: "qux",
1513 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001514 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001515 sdk_version: "system_current",
1516 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001517 java_library {
1518 name: "baz-test",
1519 srcs: ["c.java"],
1520 libs: ["foo"],
1521 sdk_version: "test_current",
1522 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001523 java_library {
1524 name: "baz-29",
1525 srcs: ["c.java"],
1526 libs: ["foo"],
1527 sdk_version: "system_29",
1528 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001529 java_library {
1530 name: "baz-module-30",
1531 srcs: ["c.java"],
1532 libs: ["foo"],
1533 sdk_version: "module_30",
1534 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001535 `)
1536
1537 // check the existence of the internal modules
1538 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001539 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1540 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1541 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1542 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1543 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1544 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001545 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001546 ctx.ModuleForTests("foo.api.public.28", "")
1547 ctx.ModuleForTests("foo.api.system.28", "")
1548 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001549
1550 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1551 // tests if baz is actually linked to the stubs lib
1552 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1553 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1554 "foo.stubs.system.jar")
1555 }
1556 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001557 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001558 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001559 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001560 }
1561 // test if baz is not linked to the system variant of foo
1562 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1563 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1564 "foo.stubs.jar")
1565 }
Jiyong Park1be96912018-05-28 18:02:19 +09001566
Paul Duffin726d23c2020-01-22 16:30:37 +00001567 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1568 // tests if baz-test is actually linked to the test stubs lib
1569 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1570 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1571 "foo.stubs.test.jar")
1572 }
1573
Paul Duffina2db18f2020-01-22 17:11:15 +00001574 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1575 // tests if baz-29 is actually linked to the system 29 stubs lib
1576 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1577 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1578 "prebuilts/sdk/29/system/foo.jar")
1579 }
1580
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001581 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1582 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1583 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1584 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1585 "prebuilts/sdk/30/module-lib/foo.jar")
1586 }
1587
Jiyong Park1be96912018-05-28 18:02:19 +09001588 // test if baz has exported SDK lib names foo and bar to qux
1589 qux := ctx.ModuleForTests("qux", "android_common")
1590 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb8063c62020-08-20 11:33:12 +01001591 sdkLibs := android.SortedStringKeys(quxLib.ExportedSdkLibs())
Paul Duffin859fe962020-05-15 10:20:31 +01001592 if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
1593 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001594 }
1595 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001596}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001597
Paul Duffindaaa3322020-05-26 18:13:57 +01001598func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1599 ctx, _ := testJava(t, `
1600 java_sdk_library {
1601 name: "foo",
1602 srcs: ["a.java"],
1603 api_only: true,
1604 public: {
1605 enabled: true,
1606 },
1607 }
1608
1609 java_library {
1610 name: "bar",
1611 srcs: ["b.java"],
1612 libs: ["foo"],
1613 }
1614 `)
1615
1616 // The bar library should depend on the stubs jar.
1617 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1618 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1619 t.Errorf("expected %q, found %#q", expected, actual)
1620 }
1621}
1622
Paul Duffin46dc45a2020-05-14 15:39:10 +01001623func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1624 testJava(t, `
1625 java_sdk_library {
1626 name: "foo",
1627 srcs: ["a.java"],
1628 api_packages: ["foo"],
1629 public: {
1630 enabled: true,
1631 },
1632 }
1633
1634 java_library {
1635 name: "bar",
1636 srcs: ["b.java", ":foo{.public.stubs.source}"],
1637 }
1638 `)
1639}
1640
1641func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1642 testJavaError(t, `"foo" does not provide api scope system`, `
1643 java_sdk_library {
1644 name: "foo",
1645 srcs: ["a.java"],
1646 api_packages: ["foo"],
1647 public: {
1648 enabled: true,
1649 },
1650 }
1651
1652 java_library {
1653 name: "bar",
1654 srcs: ["b.java", ":foo{.system.stubs.source}"],
1655 }
1656 `)
1657}
1658
Paul Duffinca8d9a52020-06-26 22:20:25 +01001659func TestJavaSdkLibrary_Deps(t *testing.T) {
1660 ctx, _ := testJava(t, `
1661 java_sdk_library {
1662 name: "sdklib",
1663 srcs: ["a.java"],
1664 sdk_version: "none",
1665 system_modules: "none",
1666 public: {
1667 enabled: true,
1668 },
1669 }
1670 `)
1671
Paul Duffincee7e662020-07-09 17:32:57 +01001672 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001673 `dex2oatd`,
1674 `sdklib.impl`,
1675 `sdklib.stubs`,
1676 `sdklib.stubs.source`,
1677 `sdklib.xml`,
1678 })
1679}
1680
Paul Duffin46dc45a2020-05-14 15:39:10 +01001681func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1682 testJava(t, `
1683 java_sdk_library_import {
1684 name: "foo",
1685 public: {
1686 jars: ["a.jar"],
1687 stub_srcs: ["a.java"],
1688 current_api: "api/current.txt",
1689 removed_api: "api/removed.txt",
1690 },
1691 }
1692
1693 java_library {
1694 name: "bar",
1695 srcs: [":foo{.public.stubs.source}"],
1696 java_resources: [
1697 ":foo{.public.api.txt}",
1698 ":foo{.public.removed-api.txt}",
1699 ],
1700 }
1701 `)
1702}
1703
1704func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1705 bp := `
1706 java_sdk_library_import {
1707 name: "foo",
1708 public: {
1709 jars: ["a.jar"],
1710 },
1711 }
1712 `
1713
1714 t.Run("stubs.source", func(t *testing.T) {
1715 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1716 java_library {
1717 name: "bar",
1718 srcs: [":foo{.public.stubs.source}"],
1719 java_resources: [
1720 ":foo{.public.api.txt}",
1721 ":foo{.public.removed-api.txt}",
1722 ],
1723 }
1724 `)
1725 })
1726
1727 t.Run("api.txt", func(t *testing.T) {
1728 testJavaError(t, `api.txt not available for api scope public`, bp+`
1729 java_library {
1730 name: "bar",
1731 srcs: ["a.java"],
1732 java_resources: [
1733 ":foo{.public.api.txt}",
1734 ],
1735 }
1736 `)
1737 })
1738
1739 t.Run("removed-api.txt", func(t *testing.T) {
1740 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1741 java_library {
1742 name: "bar",
1743 srcs: ["a.java"],
1744 java_resources: [
1745 ":foo{.public.removed-api.txt}",
1746 ],
1747 }
1748 `)
1749 })
1750}
1751
Paul Duffin3375e352020-04-28 10:44:03 +01001752func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1753 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1754 java_sdk_library {
1755 name: "foo",
1756 srcs: ["a.java", "b.java"],
1757 api_packages: ["foo"],
1758 // Explicitly disable public to test the check that ensures the set of enabled
1759 // scopes is consistent.
1760 public: {
1761 enabled: false,
1762 },
1763 system: {
1764 enabled: true,
1765 },
1766 }
1767 `)
1768}
1769
Paul Duffin87a05a32020-05-12 11:50:28 +01001770func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1771 testJava(t, `
1772 java_sdk_library {
1773 name: "foo",
1774 srcs: ["a.java", "b.java"],
1775 api_packages: ["foo"],
1776 system: {
1777 enabled: true,
1778 sdk_version: "module_current",
1779 },
1780 }
1781 `)
1782}
1783
Paul Duffin0c5bae52020-06-02 13:00:08 +01001784func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
1785 testJava(t, `
1786 java_sdk_library {
1787 name: "foo",
1788 srcs: ["a.java", "b.java"],
1789 api_packages: ["foo"],
1790 system: {
1791 enabled: true,
1792 },
1793 module_lib: {
1794 enabled: true,
1795 },
1796 }
1797 `)
1798}
1799
1800func TestJavaSdkLibrary_SystemServer(t *testing.T) {
1801 testJava(t, `
1802 java_sdk_library {
1803 name: "foo",
1804 srcs: ["a.java", "b.java"],
1805 api_packages: ["foo"],
1806 system: {
1807 enabled: true,
1808 },
1809 system_server: {
1810 enabled: true,
1811 },
1812 }
1813 `)
1814}
1815
Paul Duffin803a9562020-05-20 11:52:25 +01001816func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1817 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1818 java_sdk_library {
1819 name: "foo",
1820 srcs: ["a.java"],
1821 public: {
1822 enabled: false,
1823 },
1824 }
1825
1826 java_library {
1827 name: "baz",
1828 srcs: ["a.java"],
1829 libs: ["foo"],
1830 sdk_version: "module_current",
1831 }
1832 `)
1833}
1834
1835func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1836 testJava(t, `
1837 java_sdk_library {
1838 name: "foo",
1839 srcs: ["a.java"],
1840 system: {
1841 enabled: true,
1842 },
1843 }
1844
1845 java_library {
1846 name: "baz",
1847 srcs: ["a.java"],
1848 libs: ["foo"],
1849 // foo does not have module-lib scope so it should fallback to system
1850 sdk_version: "module_current",
1851 }
1852 `)
1853}
1854
Jiyong Park932cdfe2020-05-28 00:19:53 +09001855func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1856 ctx, _ := testJava(t, `
1857 java_sdk_library {
1858 name: "foo",
1859 srcs: ["a.java"],
1860 system: {
1861 enabled: true,
1862 },
1863 default_to_stubs: true,
1864 }
1865
1866 java_library {
1867 name: "baz",
1868 srcs: ["a.java"],
1869 libs: ["foo"],
1870 // does not have sdk_version set, should fallback to module,
1871 // which will then fallback to system because the module scope
1872 // is not enabled.
1873 }
1874 `)
1875 // The baz library should depend on the system stubs jar.
1876 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1877 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1878 t.Errorf("expected %q, found %#q", expected, actual)
1879 }
1880}
1881
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001882var compilerFlagsTestCases = []struct {
1883 in string
1884 out bool
1885}{
1886 {
1887 in: "a",
1888 out: false,
1889 },
1890 {
1891 in: "-a",
1892 out: true,
1893 },
1894 {
1895 in: "-no-jdk",
1896 out: false,
1897 },
1898 {
1899 in: "-no-stdlib",
1900 out: false,
1901 },
1902 {
1903 in: "-kotlin-home",
1904 out: false,
1905 },
1906 {
1907 in: "-kotlin-home /some/path",
1908 out: false,
1909 },
1910 {
1911 in: "-include-runtime",
1912 out: false,
1913 },
1914 {
1915 in: "-Xintellij-plugin-root",
1916 out: false,
1917 },
1918}
1919
1920type mockContext struct {
1921 android.ModuleContext
1922 result bool
1923}
1924
1925func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1926 // CheckBadCompilerFlags calls this function when the flag should be rejected
1927 ctx.result = false
1928}
1929
1930func TestCompilerFlags(t *testing.T) {
1931 for _, testCase := range compilerFlagsTestCases {
1932 ctx := &mockContext{result: true}
1933 CheckKotlincFlags(ctx, []string{testCase.in})
1934 if ctx.result != testCase.out {
1935 t.Errorf("incorrect output:")
1936 t.Errorf(" input: %#v", testCase.in)
1937 t.Errorf(" expected: %#v", testCase.out)
1938 t.Errorf(" got: %#v", ctx.result)
1939 }
1940 }
1941}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001942
1943// TODO(jungjw): Consider making this more robust by ignoring path order.
1944func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1945 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1946 flags := strings.Split(variables["javacFlags"], " ")
1947 got := ""
1948 for _, flag := range flags {
1949 keyEnd := strings.Index(flag, "=")
1950 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1951 got = flag[keyEnd+1:]
1952 break
1953 }
1954 }
1955 if expected != got {
1956 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1957 }
1958}
1959
1960func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001961 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001962 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001963 bp := `
1964 java_library {
1965 name: "foo",
1966 srcs: ["a.java"],
1967 java_version: "1.8",
1968 }
1969
1970 java_library {
1971 name: "bar",
1972 srcs: ["b.java"],
1973 sdk_version: "none",
1974 system_modules: "none",
1975 patch_module: "java.base",
1976 java_version: "1.8",
1977 }
1978
1979 java_library {
1980 name: "baz",
1981 srcs: ["c.java"],
1982 patch_module: "java.base",
1983 java_version: "1.8",
1984 }
1985 `
1986 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001987
1988 checkPatchModuleFlag(t, ctx, "foo", "")
1989 checkPatchModuleFlag(t, ctx, "bar", "")
1990 checkPatchModuleFlag(t, ctx, "baz", "")
1991 })
1992
Pete Gillin0c2143e2019-05-02 15:32:11 +01001993 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001994 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001995 bp := `
1996 java_library {
1997 name: "foo",
1998 srcs: ["a.java"],
1999 }
2000
2001 java_library {
2002 name: "bar",
2003 srcs: ["b.java"],
2004 sdk_version: "none",
2005 system_modules: "none",
2006 patch_module: "java.base",
2007 }
2008
2009 java_library {
2010 name: "baz",
2011 srcs: ["c.java"],
2012 patch_module: "java.base",
2013 }
2014 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002015 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002016
2017 checkPatchModuleFlag(t, ctx, "foo", "")
2018 expected := "java.base=.:" + buildDir
2019 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09002020 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002021 checkPatchModuleFlag(t, ctx, "baz", expected)
2022 })
2023}
Paul Duffina7b9f422020-01-10 17:12:18 +00002024
2025func TestJavaSystemModules(t *testing.T) {
2026 ctx, _ := testJava(t, `
2027 java_system_modules {
2028 name: "system-modules",
2029 libs: ["system-module1", "system-module2"],
2030 }
2031 java_library {
2032 name: "system-module1",
2033 srcs: ["a.java"],
2034 sdk_version: "none",
2035 system_modules: "none",
2036 }
2037 java_library {
2038 name: "system-module2",
2039 srcs: ["b.java"],
2040 sdk_version: "none",
2041 system_modules: "none",
2042 }
2043 `)
2044
2045 // check the existence of the module
2046 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2047
2048 cmd := systemModules.Rule("jarsTosystemModules")
2049
2050 // make sure the command compiles against the supplied modules.
2051 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2052 if !strings.Contains(cmd.Args["classpath"], module) {
2053 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2054 module)
2055 }
2056 }
2057}
Paul Duffin90169ba2020-01-10 17:16:44 +00002058
2059func TestJavaSystemModulesImport(t *testing.T) {
2060 ctx, _ := testJava(t, `
2061 java_system_modules_import {
2062 name: "system-modules",
2063 libs: ["system-module1", "system-module2"],
2064 }
2065 java_import {
2066 name: "system-module1",
2067 jars: ["a.jar"],
2068 }
2069 java_import {
2070 name: "system-module2",
2071 jars: ["b.jar"],
2072 }
2073 `)
2074
2075 // check the existence of the module
2076 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2077
2078 cmd := systemModules.Rule("jarsTosystemModules")
2079
2080 // make sure the command compiles against the supplied modules.
2081 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2082 if !strings.Contains(cmd.Args["classpath"], module) {
2083 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2084 module)
2085 }
2086 }
2087}
Paul Duffin83a2d962019-11-19 19:44:10 +00002088
2089func TestJavaLibraryWithSystemModules(t *testing.T) {
2090 ctx, _ := testJava(t, `
2091 java_library {
2092 name: "lib-with-source-system-modules",
2093 srcs: [
2094 "a.java",
2095 ],
2096 sdk_version: "none",
2097 system_modules: "source-system-modules",
2098 }
2099
2100 java_library {
2101 name: "source-jar",
2102 srcs: [
2103 "a.java",
2104 ],
2105 }
2106
2107 java_system_modules {
2108 name: "source-system-modules",
2109 libs: ["source-jar"],
2110 }
2111
2112 java_library {
2113 name: "lib-with-prebuilt-system-modules",
2114 srcs: [
2115 "a.java",
2116 ],
2117 sdk_version: "none",
2118 system_modules: "prebuilt-system-modules",
2119 }
2120
2121 java_import {
2122 name: "prebuilt-jar",
2123 jars: ["a.jar"],
2124 }
2125
2126 java_system_modules_import {
2127 name: "prebuilt-system-modules",
2128 libs: ["prebuilt-jar"],
2129 }
2130 `)
2131
2132 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2133
2134 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2135}
2136
2137func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2138 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2139 bootClasspath := javacRule.Args["bootClasspath"]
2140 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2141 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2142 }
2143}
Jiyong Park19604de2020-03-24 16:44:11 +09002144
2145func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2146 ctx, _ := testJava(t, `
2147 java_library {
2148 name: "foo",
2149 srcs: ["aidl/foo/IFoo.aidl"],
2150 libs: ["bar"],
2151 }
2152
2153 java_import {
2154 name: "bar",
2155 jars: ["a.jar"],
2156 aidl: {
2157 export_include_dirs: ["aidl/bar"],
2158 },
2159 }
2160 `)
2161
2162 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2163 expectedAidlFlag := "-Iaidl/bar"
2164 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2165 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2166 }
2167}
Liz Kammerdd849a82020-06-12 16:38:45 -07002168
2169func TestDataNativeBinaries(t *testing.T) {
2170 ctx, config := testJava(t, `
2171 java_test_host {
2172 name: "foo",
2173 srcs: ["a.java"],
2174 data_native_bins: ["bin"]
2175 }
2176
2177 python_binary_host {
2178 name: "bin",
2179 srcs: ["bin.py"],
2180 }
2181 `)
2182
2183 buildOS := android.BuildOs.String()
2184
2185 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2186 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
2187 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2188 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2189 if !reflect.DeepEqual(expected, actual) {
2190 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2191 }
2192}