blob: 4ab178fa9702aabae9c331f3bc1b00ae6636b3b2 [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Paul Duffin1a393322020-11-18 16:36:47 +000018 "fmt"
Colin Cross72bb3632017-07-13 16:23:21 -070019 "io/ioutil"
20 "os"
21 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070022 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010023 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070024 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070027
Jeongik Cha28df2572019-11-11 10:46:36 +090028 "github.com/google/blueprint/proptools"
29
Colin Crossa4f08812018-10-02 22:03:40 -070030 "android/soong/android"
31 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080032 "android/soong/dexpreopt"
Liz Kammerdd849a82020-06-12 16:38:45 -070033 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070034)
35
36var buildDir string
37
38func setUp() {
39 var err error
40 buildDir, err = ioutil.TempDir("", "soong_java_test")
41 if err != nil {
42 panic(err)
43 }
44}
45
46func tearDown() {
47 os.RemoveAll(buildDir)
48}
49
50func TestMain(m *testing.M) {
51 run := func() int {
52 setUp()
53 defer tearDown()
54
55 return m.Run()
56 }
57
58 os.Exit(run())
59}
Colin Cross527012a2017-11-30 22:56:16 -080060
Colin Cross98be1bb2019-12-13 20:41:13 -080061func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Paul Duffin9f045242021-01-21 15:05:11 +000062 return TestConfig(buildDir, env, bp, fs)
Colin Cross1369cdb2017-09-29 17:58:17 -070063}
64
Colin Crossae8600b2020-10-29 17:09:13 -070065func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070066
Colin Crossae8600b2020-10-29 17:09:13 -070067 ctx := android.NewTestArchContext(config)
Paul Duffinc059c8c2021-01-20 17:13:52 +000068 RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080069 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080070 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070071 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Colin Cross89536d42017-07-07 14:35:50 -070072 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010073 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000074
Liz Kammerdd849a82020-06-12 16:38:45 -070075 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070076 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross06fa5882020-10-29 18:21:38 -070077 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(ctx.Context, OverlaySingletonFactory))
78 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(ctx.Context, sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070079
Paul Duffin021f4e52020-07-30 16:04:17 +010080 android.RegisterPrebuiltMutators(ctx)
81
Colin Crossa4f08812018-10-02 22:03:40 -070082 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +000083 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -070084
Jaewoong Jungc779cd42020-10-06 18:56:10 -070085 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
86 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
87 })
88
Colin Cross527012a2017-11-30 22:56:16 -080089 return ctx
90}
91
92func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -080093 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -080094
Colin Cross98be1bb2019-12-13 20:41:13 -080095 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000096 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -080097
Colin Crossae8600b2020-10-29 17:09:13 -070098 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +010099 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800100 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700101 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800102 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800103}
104
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900105func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900106 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800107 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900108}
109
Colin Cross98be1bb2019-12-13 20:41:13 -0800110func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900111 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700112 ctx := testContext(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900113
Colin Cross98be1bb2019-12-13 20:41:13 -0800114 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000115 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900116
Colin Crossae8600b2020-10-29 17:09:13 -0700117 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900118 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
119 if len(errs) > 0 {
120 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900121 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900122 }
123 _, errs = ctx.PrepareBuildActions(config)
124 if len(errs) > 0 {
125 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900126 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900127 }
128
129 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900130
131 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900132}
133
Colin Cross238c1f32020-06-07 16:58:18 -0700134func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
135 t.Helper()
136 return testJavaWithConfig(t, testConfig(nil, bp, fs))
137}
138
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700139func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800140 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700141 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900142}
143
Colin Cross98be1bb2019-12-13 20:41:13 -0800144func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900145 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700146 ctx := testContext(config)
Colin Cross527012a2017-11-30 22:56:16 -0800147 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700148
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700149 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700150}
151
Colin Cross2acdae82017-09-15 19:44:24 -0700152func moduleToPath(name string) string {
153 switch {
154 case name == `""`:
155 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700156 case strings.HasSuffix(name, ".jar"):
157 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700158 default:
159 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700160 }
161}
162
Jeongik Chae403e9e2019-12-07 00:16:24 +0900163func TestJavaLinkType(t *testing.T) {
164 testJava(t, `
165 java_library {
166 name: "foo",
167 srcs: ["a.java"],
168 libs: ["bar"],
169 static_libs: ["baz"],
170 }
171
172 java_library {
173 name: "bar",
174 sdk_version: "current",
175 srcs: ["b.java"],
176 }
177
178 java_library {
179 name: "baz",
180 sdk_version: "system_current",
181 srcs: ["c.java"],
182 }
183 `)
184
Steven Moreland00298982020-11-17 21:44:36 +0000185 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900186 java_library {
187 name: "foo",
188 srcs: ["a.java"],
189 libs: ["bar"],
190 sdk_version: "current",
191 static_libs: ["baz"],
192 }
193
194 java_library {
195 name: "bar",
196 sdk_version: "current",
197 srcs: ["b.java"],
198 }
199
200 java_library {
201 name: "baz",
202 sdk_version: "system_current",
203 srcs: ["c.java"],
204 }
205 `)
206
207 testJava(t, `
208 java_library {
209 name: "foo",
210 srcs: ["a.java"],
211 libs: ["bar"],
212 sdk_version: "system_current",
213 static_libs: ["baz"],
214 }
215
216 java_library {
217 name: "bar",
218 sdk_version: "current",
219 srcs: ["b.java"],
220 }
221
222 java_library {
223 name: "baz",
224 sdk_version: "system_current",
225 srcs: ["c.java"],
226 }
227 `)
228
Steven Moreland00298982020-11-17 21:44:36 +0000229 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900230 java_library {
231 name: "foo",
232 srcs: ["a.java"],
233 libs: ["bar"],
234 sdk_version: "system_current",
235 static_libs: ["baz"],
236 }
237
238 java_library {
239 name: "bar",
240 sdk_version: "current",
241 srcs: ["b.java"],
242 }
243
244 java_library {
245 name: "baz",
246 srcs: ["c.java"],
247 }
248 `)
249}
250
Colin Cross72bb3632017-07-13 16:23:21 -0700251func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700252 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700253 java_library {
254 name: "foo",
255 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700256 libs: ["bar"],
257 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700258 }
259
260 java_library {
261 name: "bar",
262 srcs: ["b.java"],
263 }
264
265 java_library {
266 name: "baz",
267 srcs: ["c.java"],
268 }
Colin Crossd5934c82017-10-02 13:55:26 -0700269 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700270
Colin Cross4c428df2017-09-15 17:36:05 -0700271 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700272 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700273
274 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
275 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
276 }
277
Colin Cross1ee23172017-10-18 14:44:18 -0700278 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700279 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
280 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700281
Nan Zhanged19fc32017-10-19 13:06:22 -0700282 if !strings.Contains(javac.Args["classpath"], barTurbine) {
283 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700284 }
285
Nan Zhanged19fc32017-10-19 13:06:22 -0700286 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
287 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700288 }
289
290 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
291 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700292 }
293}
294
Artur Satayev9cf46692019-11-26 18:08:34 +0000295func TestExportedPlugins(t *testing.T) {
296 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800297 library string
298 processors string
299 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000300 }
301 var tests = []struct {
302 name string
303 extra string
304 results []Result
305 }{
306 {
307 name: "Exported plugin is not a direct plugin",
308 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
309 results: []Result{{library: "exports", processors: "-proc:none"}},
310 },
311 {
312 name: "Exports plugin to dependee",
313 extra: `
314 java_library{name: "exports", exported_plugins: ["plugin"]}
315 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
316 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
317 `,
318 results: []Result{
319 {library: "foo", processors: "-processor com.android.TestPlugin"},
320 {library: "bar", processors: "-processor com.android.TestPlugin"},
321 },
322 },
323 {
324 name: "Exports plugin to android_library",
325 extra: `
326 java_library{name: "exports", exported_plugins: ["plugin"]}
327 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
328 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
329 `,
330 results: []Result{
331 {library: "foo", processors: "-processor com.android.TestPlugin"},
332 {library: "bar", processors: "-processor com.android.TestPlugin"},
333 },
334 },
335 {
336 name: "Exports plugin is not propagated via transitive deps",
337 extra: `
338 java_library{name: "exports", exported_plugins: ["plugin"]}
339 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
340 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
341 `,
342 results: []Result{
343 {library: "foo", processors: "-processor com.android.TestPlugin"},
344 {library: "bar", processors: "-proc:none"},
345 },
346 },
347 {
348 name: "Exports plugin appends to plugins",
349 extra: `
350 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
351 java_library{name: "exports", exported_plugins: ["plugin"]}
352 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
353 `,
354 results: []Result{
355 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
356 },
357 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800358 {
359 name: "Exports plugin to with generates_api to dependee",
360 extra: `
361 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
362 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
363 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
364 `,
365 results: []Result{
366 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
367 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
368 },
369 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000370 }
371
372 for _, test := range tests {
373 t.Run(test.name, func(t *testing.T) {
374 ctx, _ := testJava(t, `
375 java_plugin {
376 name: "plugin",
377 processor_class: "com.android.TestPlugin",
378 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800379 java_plugin {
380 name: "plugin_generates_api",
381 generates_api: true,
382 processor_class: "com.android.TestPlugin",
383 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000384 `+test.extra)
385
386 for _, want := range test.results {
387 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
388 if javac.Args["processor"] != want.processors {
389 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
390 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800391 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
392 disableTurbine := turbine.BuildParams.Rule == nil
393 if disableTurbine != want.disableTurbine {
394 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
395 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000396 }
397 })
398 }
399}
400
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900401func TestSdkVersionByPartition(t *testing.T) {
402 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900403 java_library {
404 name: "foo",
405 srcs: ["a.java"],
406 vendor: true,
407 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900408 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900409
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900410 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900411 java_library {
412 name: "bar",
413 srcs: ["b.java"],
414 }
415 `)
416
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900417 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900418 bp := `
419 java_library {
420 name: "foo",
421 srcs: ["a.java"],
422 product_specific: true,
423 }
424 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800425
426 config := testConfig(nil, bp, nil)
427 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900428 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800429 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 +0900430 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800431 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900432 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900433 }
434}
435
Colin Crossd5934c82017-10-02 13:55:26 -0700436func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700437 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700438 java_library {
439 name: "foo",
440 srcs: ["a.java"],
441 target: {
442 android: {
443 srcs: ["b.java"],
444 },
445 },
446 }
447 `)
448
449 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
450 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
451 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
452 }
453}
454
Colin Cross6b4a32d2017-12-05 13:42:45 -0800455func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700456 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800457 java_library_host {
458 name: "foo",
459 srcs: ["a.java"],
460 }
461
462 java_binary_host {
463 name: "bar",
464 srcs: ["b.java"],
465 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700466 jni_libs: ["libjni"],
467 }
468
469 cc_library_shared {
470 name: "libjni",
471 host_supported: true,
472 device_supported: false,
473 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800474 }
475 `)
476
477 buildOS := android.BuildOs.String()
478
479 bar := ctx.ModuleForTests("bar", buildOS+"_common")
480 barJar := bar.Output("bar.jar").Output.String()
481 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
482 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
483
Colin Cross89226d92020-10-09 19:00:54 -0700484 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
485 libjniSO := libjni.Rule("Cp").Output.String()
486
Colin Cross6b4a32d2017-12-05 13:42:45 -0800487 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700488 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
489 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800490 }
Colin Cross89226d92020-10-09 19:00:54 -0700491
492 // Test that the install binary wrapper depends on the installed JNI libraries
493 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
494 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800495 }
Alex Humesky2070e322020-06-09 20:23:08 -0400496}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800497
Alex Humesky2070e322020-06-09 20:23:08 -0400498func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
499 bp := `
500 java_library {
501 name: "target_library",
502 srcs: ["a.java"],
503 }
504
505 java_binary_host {
506 name: "host_binary",
507 srcs: ["b.java"],
508 }
509 `
510 config := testConfig(nil, bp, nil)
511 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
512
513 ctx, _ := testJavaWithConfig(t, config)
514
Liz Kammer7941b302020-07-28 13:27:34 -0700515 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400516 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
517 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
518 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
519 t.Errorf("target library javac flags %v should contain "+
520 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
521 }
522
523 // check that -g is not overridden for host modules
524 buildOS := android.BuildOs.String()
525 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
526 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
527 if strings.Contains(hostJavaFlags, "-g:source,lines") {
528 t.Errorf("java_binary_host javac flags %v should not have "+
529 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
530 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800531}
532
Colin Cross72bb3632017-07-13 16:23:21 -0700533func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700534 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700535 java_library {
536 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000537 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000538 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700539 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700540 }
541
Colin Cross74d73e22017-08-02 11:05:49 -0700542 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700543 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700544 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700545 }
546
Colin Cross74d73e22017-08-02 11:05:49 -0700547 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700548 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700549 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700550 sdk_version: "current",
551 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700552 }
Colin Cross42be7612019-02-21 18:12:14 -0800553
554 dex_import {
555 name: "qux",
556 jars: ["b.jar"],
557 }
Colin Cross79c7c262019-04-17 11:11:46 -0700558
559 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000560 name: "sdklib",
561 public: {
562 jars: ["c.jar"],
563 },
564 }
565
Paul Duffin91547182019-11-12 19:39:36 +0000566 prebuilt_stubs_sources {
567 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000568 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000569 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000570
571 java_test_import {
572 name: "test",
573 jars: ["a.jar"],
574 test_suites: ["cts"],
575 test_config: "AndroidTest.xml",
576 }
Colin Cross72bb3632017-07-13 16:23:21 -0700577 `)
578
Paul Duffin9b478b02019-12-10 13:41:51 +0000579 fooModule := ctx.ModuleForTests("foo", "android_common")
580 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700581 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700582 barModule := ctx.ModuleForTests("bar", "android_common")
583 barJar := barModule.Rule("combineJar").Output
584 bazModule := ctx.ModuleForTests("baz", "android_common")
585 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700586 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700587
Paul Duffin9b478b02019-12-10 13:41:51 +0000588 fooLibrary := fooModule.Module().(*Library)
589 assertDeepEquals(t, "foo java sources incorrect",
590 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000591
Paul Duffin9b478b02019-12-10 13:41:51 +0000592 assertDeepEquals(t, "foo java source jars incorrect",
593 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
594 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000595
Colin Cross37f6d792018-07-12 12:28:41 -0700596 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
597 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700598 }
599
Liz Kammerd6c31d22020-08-05 15:40:41 -0700600 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
601 if barDexJar != nil {
602 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
603 }
604
Colin Cross79c7c262019-04-17 11:11:46 -0700605 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
606 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
607 }
608
Colin Cross37f6d792018-07-12 12:28:41 -0700609 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
610 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700611 }
Colin Cross42be7612019-02-21 18:12:14 -0800612
Liz Kammerd6c31d22020-08-05 15:40:41 -0700613 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
614 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
615 if bazDexJar != expectedDexJar {
616 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
617 }
618
Colin Cross42be7612019-02-21 18:12:14 -0800619 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700620}
621
Paul Duffin9b478b02019-12-10 13:41:51 +0000622func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
623 if !reflect.DeepEqual(expected, actual) {
624 t.Errorf("%s: expected %q, found %q", message, expected, actual)
625 }
626}
627
Paul Duffin1a393322020-11-18 16:36:47 +0000628func TestPrebuiltStubsSources(t *testing.T) {
629 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
630 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
631prebuilt_stubs_sources {
632 name: "stubs-source",
633 srcs: ["%s"],
634}`, sourcesPath), map[string][]byte{
635 "stubs/sources/pkg/A.java": nil,
636 "stubs/sources/pkg/B.java": nil,
637 })
638
639 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
640 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
641 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
642 }
643 }
644
645 t.Run("empty/missing directory", func(t *testing.T) {
646 test(t, "empty-directory", []string{})
647 })
648
649 t.Run("non-empty set of sources", func(t *testing.T) {
650 test(t, "stubs/sources", []string{
651 "stubs/sources/pkg/A.java",
652 "stubs/sources/pkg/B.java",
653 })
654 })
655}
656
Paul Duffin56d44902020-01-31 13:36:25 +0000657func TestJavaSdkLibraryImport(t *testing.T) {
658 ctx, _ := testJava(t, `
659 java_library {
660 name: "foo",
661 srcs: ["a.java"],
662 libs: ["sdklib"],
663 sdk_version: "current",
664 }
665
666 java_library {
667 name: "foo.system",
668 srcs: ["a.java"],
669 libs: ["sdklib"],
670 sdk_version: "system_current",
671 }
672
673 java_library {
674 name: "foo.test",
675 srcs: ["a.java"],
676 libs: ["sdklib"],
677 sdk_version: "test_current",
678 }
679
680 java_sdk_library_import {
681 name: "sdklib",
682 public: {
683 jars: ["a.jar"],
684 },
685 system: {
686 jars: ["b.jar"],
687 },
688 test: {
689 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100690 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000691 },
692 }
693 `)
694
695 for _, scope := range []string{"", ".system", ".test"} {
696 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
697 javac := fooModule.Rule("javac")
698
699 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
700 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
701 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
702 }
703 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100704
Paul Duffincee7e662020-07-09 17:32:57 +0100705 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100706 `prebuilt_sdklib.stubs`,
707 `prebuilt_sdklib.stubs.source.test`,
708 `prebuilt_sdklib.stubs.system`,
709 `prebuilt_sdklib.stubs.test`,
710 })
711}
712
713func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
714 ctx, _ := testJava(t, `
715 java_sdk_library {
716 name: "sdklib",
717 srcs: ["a.java"],
718 sdk_version: "none",
719 system_modules: "none",
720 public: {
721 enabled: true,
722 },
723 }
724
725 java_sdk_library_import {
726 name: "sdklib",
727 public: {
728 jars: ["a.jar"],
729 },
730 }
731 `)
732
Paul Duffincee7e662020-07-09 17:32:57 +0100733 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100734 `dex2oatd`,
735 `prebuilt_sdklib`,
736 `sdklib.impl`,
737 `sdklib.stubs`,
738 `sdklib.stubs.source`,
739 `sdklib.xml`,
740 })
741
Paul Duffincee7e662020-07-09 17:32:57 +0100742 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100743 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100744 `sdklib.impl`,
745 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
746 // dependency is added after prebuilts may have been renamed and so has to use
747 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100748 `sdklib.xml`,
749 })
750}
751
752func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
753 ctx, _ := testJava(t, `
754 java_sdk_library {
755 name: "sdklib",
756 srcs: ["a.java"],
757 sdk_version: "none",
758 system_modules: "none",
759 public: {
760 enabled: true,
761 },
762 }
763
764 java_sdk_library_import {
765 name: "sdklib",
766 prefer: true,
767 public: {
768 jars: ["a.jar"],
769 },
770 }
771 `)
772
Paul Duffincee7e662020-07-09 17:32:57 +0100773 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100774 `dex2oatd`,
775 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100776 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100777 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100778 `sdklib.stubs.source`,
779 `sdklib.xml`,
780 })
781
Paul Duffincee7e662020-07-09 17:32:57 +0100782 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100783 `prebuilt_sdklib.stubs`,
784 `sdklib.impl`,
785 `sdklib.xml`,
786 })
Paul Duffin56d44902020-01-31 13:36:25 +0000787}
788
JaeMan Parkff715562020-10-19 17:25:58 +0900789func TestJavaSdkLibraryEnforce(t *testing.T) {
790 partitionToBpOption := func(partition string) string {
791 switch partition {
792 case "system":
793 return ""
794 case "vendor":
795 return "soc_specific: true,"
796 case "product":
797 return "product_specific: true,"
798 default:
799 panic("Invalid partition group name: " + partition)
800 }
801 }
802
803 type testConfigInfo struct {
804 libraryType string
805 fromPartition string
806 toPartition string
807 enforceVendorInterface bool
808 enforceProductInterface bool
809 enforceJavaSdkLibraryCheck bool
810 allowList []string
811 }
812
813 createTestConfig := func(info testConfigInfo) android.Config {
814 bpFileTemplate := `
815 java_library {
816 name: "foo",
817 srcs: ["foo.java"],
818 libs: ["bar"],
819 sdk_version: "current",
820 %s
821 }
822
823 %s {
824 name: "bar",
825 srcs: ["bar.java"],
826 sdk_version: "current",
827 %s
828 }
829 `
830
831 bpFile := fmt.Sprintf(bpFileTemplate,
832 partitionToBpOption(info.fromPartition),
833 info.libraryType,
834 partitionToBpOption(info.toPartition))
835
836 config := testConfig(nil, bpFile, nil)
837 configVariables := config.TestProductVariables
838
839 configVariables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
840 if info.enforceVendorInterface {
841 configVariables.DeviceVndkVersion = proptools.StringPtr("current")
842 }
843 configVariables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
844 configVariables.InterPartitionJavaLibraryAllowList = info.allowList
845
846 return config
847 }
848
JaeMan Parkff715562020-10-19 17:25:58 +0900849 errorMessage := "is not allowed across the partitions"
850
JaeMan Park90e75352021-01-14 11:56:56 +0900851 testJavaWithConfig(t, createTestConfig(testConfigInfo{
852 libraryType: "java_library",
853 fromPartition: "product",
854 toPartition: "system",
855 enforceVendorInterface: true,
856 enforceProductInterface: true,
857 enforceJavaSdkLibraryCheck: false,
858 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900859
JaeMan Park90e75352021-01-14 11:56:56 +0900860 testJavaWithConfig(t, createTestConfig(testConfigInfo{
861 libraryType: "java_library",
862 fromPartition: "product",
863 toPartition: "system",
864 enforceVendorInterface: true,
865 enforceProductInterface: false,
866 enforceJavaSdkLibraryCheck: true,
867 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900868
JaeMan Park90e75352021-01-14 11:56:56 +0900869 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
870 libraryType: "java_library",
871 fromPartition: "product",
872 toPartition: "system",
873 enforceVendorInterface: true,
874 enforceProductInterface: true,
875 enforceJavaSdkLibraryCheck: true,
876 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900877
JaeMan Park90e75352021-01-14 11:56:56 +0900878 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
879 libraryType: "java_library",
880 fromPartition: "vendor",
881 toPartition: "system",
882 enforceVendorInterface: true,
883 enforceProductInterface: true,
884 enforceJavaSdkLibraryCheck: true,
885 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900886
887 testJavaWithConfig(t, createTestConfig(testConfigInfo{
888 libraryType: "java_library",
889 fromPartition: "vendor",
890 toPartition: "system",
891 enforceVendorInterface: true,
892 enforceProductInterface: true,
893 enforceJavaSdkLibraryCheck: true,
894 allowList: []string{"bar"},
895 }))
896
897 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
898 libraryType: "java_library",
899 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +0900900 toPartition: "product",
901 enforceVendorInterface: true,
902 enforceProductInterface: true,
903 enforceJavaSdkLibraryCheck: true,
904 }))
905
906 testJavaWithConfig(t, createTestConfig(testConfigInfo{
907 libraryType: "java_sdk_library",
908 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +0900909 toPartition: "system",
910 enforceVendorInterface: true,
911 enforceProductInterface: true,
912 enforceJavaSdkLibraryCheck: true,
JaeMan Park90e75352021-01-14 11:56:56 +0900913 }))
914
915 testJavaWithConfig(t, createTestConfig(testConfigInfo{
916 libraryType: "java_sdk_library",
917 fromPartition: "vendor",
918 toPartition: "system",
919 enforceVendorInterface: true,
920 enforceProductInterface: true,
921 enforceJavaSdkLibraryCheck: true,
922 }))
923
924 testJavaWithConfig(t, createTestConfig(testConfigInfo{
925 libraryType: "java_sdk_library",
926 fromPartition: "vendor",
927 toPartition: "product",
928 enforceVendorInterface: true,
929 enforceProductInterface: true,
930 enforceJavaSdkLibraryCheck: true,
JaeMan Parkff715562020-10-19 17:25:58 +0900931 }))
932}
933
Colin Cross89536d42017-07-07 14:35:50 -0700934func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700935 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700936 java_defaults {
937 name: "defaults",
938 srcs: ["a.java"],
939 libs: ["bar"],
940 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700941 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700942 }
943
944 java_library {
945 name: "foo",
946 defaults: ["defaults"],
947 }
948
949 java_library {
950 name: "bar",
951 srcs: ["b.java"],
952 }
953
954 java_library {
955 name: "baz",
956 srcs: ["c.java"],
957 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700958
959 android_test {
960 name: "atestOptimize",
961 defaults: ["defaults"],
962 optimize: {enabled: true},
963 }
964
965 android_test {
966 name: "atestNoOptimize",
967 defaults: ["defaults"],
968 }
969
970 android_test {
971 name: "atestDefault",
972 srcs: ["a.java"],
973 }
Colin Cross89536d42017-07-07 14:35:50 -0700974 `)
975
Colin Cross4c428df2017-09-15 17:36:05 -0700976 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700977 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700978
979 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
980 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
981 }
982
Nan Zhanged19fc32017-10-19 13:06:22 -0700983 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
984 if !strings.Contains(javac.Args["classpath"], barTurbine) {
985 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700986 }
987
Colin Cross1ee23172017-10-18 14:44:18 -0700988 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700989 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
990 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700991 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700992
993 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
994 if atestOptimize.Output == nil {
995 t.Errorf("atestOptimize should optimize APK")
996 }
997
998 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
999 if atestNoOptimize.Output == nil {
1000 t.Errorf("atestNoOptimize should not optimize APK")
1001 }
1002
1003 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1004 if atestDefault.Output == nil {
1005 t.Errorf("atestDefault should optimize APK")
1006 }
Colin Cross89536d42017-07-07 14:35:50 -07001007}
1008
Colin Cross0f37af02017-09-27 17:42:05 -07001009func TestResources(t *testing.T) {
1010 var table = []struct {
1011 name string
1012 prop string
1013 extra string
1014 args string
1015 }{
1016 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001017 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001018 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001019 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001020 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001021 },
1022 {
1023 // Test that a module with java_resources includes the files
1024 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001025 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1026 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001027 },
1028 {
1029 // Test that a module with a filegroup in java_resources includes the files with the
1030 // path prefix
1031 name: "resource filegroup",
1032 prop: `java_resources: [":foo-res"]`,
1033 extra: `
1034 filegroup {
1035 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001036 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001037 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001038 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001039 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001040 },
1041 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001042 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1043 name: "wildcard dirs",
1044 prop: `java_resource_dirs: ["java-res/*"]`,
1045 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1046 },
1047 {
1048 // Test that a module exclude_java_resource_dirs excludes the files
1049 name: "wildcard dirs",
1050 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1051 args: "-C java-res/a -f java-res/a/a",
1052 },
Colin Crosscedd4762018-09-13 11:26:19 -07001053 {
1054 // Test wildcards in java_resources
1055 name: "wildcard files",
1056 prop: `java_resources: ["java-res/**/*"]`,
1057 args: "-C . -f java-res/a/a -f java-res/b/b",
1058 },
1059 {
1060 // Test exclude_java_resources with java_resources
1061 name: "wildcard files with exclude",
1062 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1063 args: "-C . -f java-res/a/a",
1064 },
1065 {
1066 // Test exclude_java_resources with java_resource_dirs
1067 name: "resource dirs with exclude files",
1068 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1069 args: "-C java-res -f java-res/a/a",
1070 },
1071 {
1072 // Test exclude_java_resource_dirs with java_resource_dirs
1073 name: "resource dirs with exclude files",
1074 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1075 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1076 },
Colin Cross0f37af02017-09-27 17:42:05 -07001077 }
1078
1079 for _, test := range table {
1080 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001081 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001082 java_library {
1083 name: "foo",
1084 srcs: [
1085 "a.java",
1086 "b.java",
1087 "c.java",
1088 ],
1089 `+test.prop+`,
1090 }
Colin Cross238c1f32020-06-07 16:58:18 -07001091 `+test.extra,
1092 map[string][]byte{
1093 "java-res/a/a": nil,
1094 "java-res/b/b": nil,
1095 "java-res2/a": nil,
1096 },
1097 )
Colin Cross0f37af02017-09-27 17:42:05 -07001098
Colin Cross331a1212018-08-15 20:40:52 -07001099 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001100 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001101
1102 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1103 t.Errorf("foo combined jars %v does not contain %q",
1104 foo.Inputs.Strings(), fooRes.Output.String())
1105 }
1106
Colin Crossaf9c55b2017-10-03 14:50:08 -07001107 if fooRes.Args["jarArgs"] != test.args {
1108 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001109 fooRes.Args["jarArgs"], test.args)
1110 }
1111 })
1112 }
1113}
1114
Colin Cross0c4ce212019-05-03 15:28:19 -07001115func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001116 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001117 java_library {
1118 name: "foo",
1119 srcs: [
1120 "a.java",
1121 "b.java",
1122 "c.java",
1123 ],
1124 include_srcs: true,
1125 }
1126
1127 java_library {
1128 name: "bar",
1129 srcs: [
1130 "a.java",
1131 "b.java",
1132 "c.java",
1133 ],
1134 java_resource_dirs: ["java-res"],
1135 include_srcs: true,
1136 }
Colin Cross238c1f32020-06-07 16:58:18 -07001137 `, map[string][]byte{
1138 "java-res/a/a": nil,
1139 "java-res/b/b": nil,
1140 "java-res2/a": nil,
1141 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001142
1143 // Test a library with include_srcs: true
1144 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1145 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1146
1147 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1148 t.Errorf("foo combined jars %v does not contain %q", w, g)
1149 }
1150
1151 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1152 t.Errorf("foo source jar args %q is not %q", w, g)
1153 }
1154
1155 // Test a library with include_srcs: true and resources
1156 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1157 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1158 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1159 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1160
1161 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1162 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1163 }
1164
1165 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1166 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1167 }
1168
1169 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1170 t.Errorf("bar combined jars %v does not contain %q", w, g)
1171 }
1172
1173 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1174 t.Errorf("bar source jar args %q is not %q", w, g)
1175 }
1176
1177 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1178 t.Errorf("bar resource jar args %q is not %q", w, g)
1179 }
1180}
1181
Colin Cross54190b32017-10-09 15:34:10 -07001182func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001183 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001184 java_library {
1185 name: "foo",
1186 srcs: [
1187 "a*.java",
1188 ":gen",
1189 "b*.java",
1190 ],
1191 }
1192
1193 genrule {
1194 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001195 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001196 out: ["gen.java"],
1197 }
Colin Cross238c1f32020-06-07 16:58:18 -07001198 `, map[string][]byte{
1199 "a.java": nil,
1200 "b.java": nil,
1201 })
Colin Cross54190b32017-10-09 15:34:10 -07001202
1203 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1204 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1205
Colin Cross15e86d92017-10-20 15:07:08 -07001206 if filepath.Base(genrule.Output.String()) != "gen.java" {
1207 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001208 }
1209
1210 if len(javac.Inputs) != 3 ||
1211 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001212 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001213 javac.Inputs[2].String() != "b.java" {
1214 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1215 }
1216}
1217
Nan Zhang61eaedb2017-11-02 13:28:15 -07001218func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001219 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001220 java_library {
1221 name: "foo",
1222 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001223 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001224 }
1225
1226 java_library {
1227 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001228 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001229 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001230 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001231 }
1232
1233 java_library {
1234 name: "baz",
1235 srcs: ["c.java"],
1236 libs: ["bar"],
1237 sdk_version: "14",
1238 }
1239 `)
1240
1241 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1242 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1243 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1244 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1245 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1246
1247 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1248 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1249 }
1250
1251 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1252 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1253 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1254 }
1255 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1256 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1257 }
1258 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1259 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1260 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001261 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001262 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001263 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001264 }
1265}
1266
1267func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001268 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001269 java_library {
1270 name: "bar",
1271 srcs: ["a.java","b.java","c.java"],
1272 javac_shard_size: 1
1273 }
1274 `)
1275
1276 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1277 for i := 0; i < 3; i++ {
1278 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1279 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1280 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1281 }
1282 }
1283}
1284
Nan Zhang581fd212018-01-10 16:06:12 -08001285func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001286 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001287 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001288 name: "droiddoc-templates-sdk",
1289 path: ".",
1290 }
Jiyong Park29074592019-07-07 16:27:47 +09001291 filegroup {
1292 name: "bar-doc-aidl-srcs",
1293 srcs: ["bar-doc/IBar.aidl"],
1294 path: "bar-doc",
1295 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001296 droidstubs {
1297 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001298 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001299 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001300 ],
Steve Kim3666c702020-09-01 17:58:01 +00001301 exclude_srcs: [
1302 "bar-doc/b.java"
1303 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001304 api_levels_annotations_dirs: [
1305 "droiddoc-templates-sdk",
1306 ],
1307 api_levels_annotations_enabled: true,
1308 }
1309 droiddoc {
1310 name: "bar-doc",
1311 srcs: [
1312 ":bar-stubs",
1313 "bar-doc/IFoo.aidl",
1314 ":bar-doc-aidl-srcs",
1315 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001316 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001317 hdf: [
1318 "android.whichdoc offline",
1319 ],
1320 knowntags: [
1321 "bar-doc/known_oj_tags.txt",
1322 ],
1323 proofread_file: "libcore-proofread.txt",
1324 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001325 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001326 }
Colin Cross238c1f32020-06-07 16:58:18 -07001327 `,
1328 map[string][]byte{
1329 "bar-doc/a.java": nil,
1330 "bar-doc/b.java": nil,
1331 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001332 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1333 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1334 if err != nil {
1335 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1336 }
1337 if len(barStubsOutputs) != 1 {
1338 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001339 }
Nan Zhang581fd212018-01-10 16:06:12 -08001340
Liz Kammere1ab2502020-09-10 15:29:25 +00001341 barStubsOutput := barStubsOutputs[0]
1342 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1343 javaDoc := barDoc.Rule("javadoc")
1344 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1345 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001346 }
1347
Liz Kammere1ab2502020-09-10 15:29:25 +00001348 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1349 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1350 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1351 }
1352
1353 aidl := barDoc.Rule("aidl")
1354 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001355 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1356 }
1357
1358 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1359 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001360 }
Nan Zhang581fd212018-01-10 16:06:12 -08001361}
1362
Liz Kammer585cac22020-07-06 09:12:57 -07001363func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1364 testJavaError(t, "flags is set. Cannot set args", `
1365 droiddoc_exported_dir {
1366 name: "droiddoc-templates-sdk",
1367 path: ".",
1368 }
1369 filegroup {
1370 name: "bar-doc-aidl-srcs",
1371 srcs: ["bar-doc/IBar.aidl"],
1372 path: "bar-doc",
1373 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001374 droidstubs {
1375 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001376 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001377 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001378 ],
Steve Kim3666c702020-09-01 17:58:01 +00001379 exclude_srcs: [
1380 "bar-doc/b.java"
1381 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001382 api_levels_annotations_dirs: [
1383 "droiddoc-templates-sdk",
1384 ],
1385 api_levels_annotations_enabled: true,
1386 }
1387 droiddoc {
1388 name: "bar-doc",
1389 srcs: [
1390 ":bar-stubs",
1391 "bar-doc/IFoo.aidl",
1392 ":bar-doc-aidl-srcs",
1393 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001394 custom_template: "droiddoc-templates-sdk",
1395 hdf: [
1396 "android.whichdoc offline",
1397 ],
1398 knowntags: [
1399 "bar-doc/known_oj_tags.txt",
1400 ],
1401 proofread_file: "libcore-proofread.txt",
1402 todo_file: "libcore-docs-todo.html",
1403 flags: ["-offlinemode -title \"libcore\""],
1404 args: "-offlinemode -title \"libcore\"",
1405 }
1406 `)
1407}
1408
Liz Kammer3d894b72020-08-04 09:55:13 -07001409func TestDroidstubs(t *testing.T) {
1410 ctx, _ := testJavaWithFS(t, `
1411 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001412 name: "droiddoc-templates-sdk",
1413 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001414 }
1415
1416 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001417 name: "bar-stubs",
1418 srcs: ["bar-doc/a.java"],
1419 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1420 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001421 }
1422
1423 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001424 name: "bar-stubs-other",
1425 srcs: ["bar-doc/a.java"],
1426 high_mem: true,
1427 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1428 api_levels_annotations_enabled: true,
1429 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001430 }
1431 `,
1432 map[string][]byte{
1433 "bar-doc/a.java": nil,
1434 })
1435 testcases := []struct {
1436 moduleName string
1437 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001438 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001439 }{
1440 {
1441 moduleName: "bar-stubs",
1442 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001443 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001444 },
1445 {
1446 moduleName: "bar-stubs-other",
1447 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001448 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001449 },
1450 }
1451 for _, c := range testcases {
1452 m := ctx.ModuleForTests(c.moduleName, "android_common")
1453 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001454 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001455 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001456 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001457 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1458 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001459
1460 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1461 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1462 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001463 }
1464}
1465
Paul Duffin83a2d962019-11-19 19:44:10 +00001466func TestDroidstubsWithSystemModules(t *testing.T) {
1467 ctx, _ := testJava(t, `
1468 droidstubs {
1469 name: "stubs-source-system-modules",
1470 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001471 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001472 ],
1473 sdk_version: "none",
1474 system_modules: "source-system-modules",
1475 }
1476
1477 java_library {
1478 name: "source-jar",
1479 srcs: [
1480 "a.java",
1481 ],
1482 }
1483
1484 java_system_modules {
1485 name: "source-system-modules",
1486 libs: ["source-jar"],
1487 }
1488
1489 droidstubs {
1490 name: "stubs-prebuilt-system-modules",
1491 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001492 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001493 ],
1494 sdk_version: "none",
1495 system_modules: "prebuilt-system-modules",
1496 }
1497
1498 java_import {
1499 name: "prebuilt-jar",
1500 jars: ["a.jar"],
1501 }
1502
1503 java_system_modules_import {
1504 name: "prebuilt-system-modules",
1505 libs: ["prebuilt-jar"],
1506 }
1507 `)
1508
1509 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1510
1511 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1512}
1513
1514func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1515 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1516 var systemJars []string
1517 for _, i := range metalavaRule.Implicits {
1518 systemJars = append(systemJars, i.Base())
1519 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001520 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001521 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1522 }
1523}
1524
Colin Cross54250902017-12-05 09:28:08 -08001525func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001526 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001527 java_library {
1528 name: "foo",
1529 srcs: ["a.java"],
1530 }
1531
1532 java_genrule {
1533 name: "jargen",
1534 tool_files: ["b.java"],
1535 cmd: "$(location b.java) $(in) $(out)",
1536 out: ["jargen.jar"],
1537 srcs: [":foo"],
1538 }
1539
1540 java_library {
1541 name: "bar",
1542 static_libs: ["jargen"],
1543 srcs: ["c.java"],
1544 }
1545
1546 java_library {
1547 name: "baz",
1548 libs: ["jargen"],
1549 srcs: ["c.java"],
1550 }
1551 `)
1552
1553 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1554 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1555 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1556 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1557 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1558
Colin Cross3d680512020-11-13 16:23:53 -08001559 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1560 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001561 }
1562
1563 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1564 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1565 }
1566
1567 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1568 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1569 }
1570
1571 if len(barCombined.Inputs) != 2 ||
1572 barCombined.Inputs[0].String() != bar.Output.String() ||
1573 barCombined.Inputs[1].String() != jargen.Output.String() {
1574 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1575 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1576 }
1577}
1578
Nan Zhang27e284d2018-02-09 21:03:53 +00001579func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001580 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001581 java_library {
1582 name: "foo",
1583 srcs: ["a.java", ":foo-srcs"],
1584 exclude_srcs: ["a.java", ":foo-excludes"],
1585 }
1586
1587 filegroup {
1588 name: "foo-srcs",
1589 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1590 }
1591
1592 filegroup {
1593 name: "foo-excludes",
1594 srcs: ["java-fg/a.java", "java-fg/b.java"],
1595 }
1596 `)
1597
1598 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1599
1600 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1601 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1602 }
1603}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001604
Paul Duffin52d398a2019-06-11 12:31:14 +01001605func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001606 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001607 "libcore/Android.bp": []byte(`
1608 java_library {
1609 name: "core",
1610 sdk_version: "none",
1611 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001612 }
1613
1614 filegroup {
1615 name: "core-jar",
1616 srcs: [":core{.jar}"],
1617 }
1618`),
1619 })
Colin Crossae8600b2020-10-29 17:09:13 -07001620 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001621 run(t, ctx, config)
1622}
1623
1624func TestJavaImport(t *testing.T) {
1625 config := testConfig(nil, "", map[string][]byte{
1626 "libcore/Android.bp": []byte(`
1627 java_import {
1628 name: "core",
1629 sdk_version: "none",
1630 }
1631
1632 filegroup {
1633 name: "core-jar",
1634 srcs: [":core{.jar}"],
1635 }
1636`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001637 })
Colin Crossae8600b2020-10-29 17:09:13 -07001638 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001639 run(t, ctx, config)
1640}
1641
Jiyong Parkc678ad32018-04-10 13:07:10 +09001642func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001643 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001644 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001645 name: "droiddoc-templates-sdk",
1646 path: ".",
1647 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001648 java_sdk_library {
1649 name: "foo",
1650 srcs: ["a.java", "b.java"],
1651 api_packages: ["foo"],
1652 }
1653 java_sdk_library {
1654 name: "bar",
1655 srcs: ["a.java", "b.java"],
1656 api_packages: ["bar"],
1657 }
1658 java_library {
1659 name: "baz",
1660 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001661 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001662 sdk_version: "system_current",
1663 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001664 java_sdk_library {
1665 name: "barney",
1666 srcs: ["c.java"],
1667 api_only: true,
1668 }
1669 java_sdk_library {
1670 name: "betty",
1671 srcs: ["c.java"],
1672 shared_library: false,
1673 }
Paul Duffin859fe962020-05-15 10:20:31 +01001674 java_sdk_library_import {
1675 name: "quuz",
1676 public: {
1677 jars: ["c.jar"],
1678 },
1679 }
1680 java_sdk_library_import {
1681 name: "fred",
1682 public: {
1683 jars: ["b.jar"],
1684 },
1685 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001686 java_sdk_library_import {
1687 name: "wilma",
1688 public: {
1689 jars: ["b.jar"],
1690 },
1691 shared_library: false,
1692 }
Jiyong Park1be96912018-05-28 18:02:19 +09001693 java_library {
1694 name: "qux",
1695 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001696 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001697 sdk_version: "system_current",
1698 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001699 java_library {
1700 name: "baz-test",
1701 srcs: ["c.java"],
1702 libs: ["foo"],
1703 sdk_version: "test_current",
1704 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001705 java_library {
1706 name: "baz-29",
1707 srcs: ["c.java"],
1708 libs: ["foo"],
1709 sdk_version: "system_29",
1710 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001711 java_library {
1712 name: "baz-module-30",
1713 srcs: ["c.java"],
1714 libs: ["foo"],
1715 sdk_version: "module_30",
1716 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001717 `)
1718
1719 // check the existence of the internal modules
1720 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001721 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1722 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1723 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1724 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1725 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1726 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001727 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001728 ctx.ModuleForTests("foo.api.public.28", "")
1729 ctx.ModuleForTests("foo.api.system.28", "")
1730 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001731
1732 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1733 // tests if baz is actually linked to the stubs lib
1734 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1735 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1736 "foo.stubs.system.jar")
1737 }
1738 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001739 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001740 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001741 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001742 }
1743 // test if baz is not linked to the system variant of foo
1744 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1745 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1746 "foo.stubs.jar")
1747 }
Jiyong Park1be96912018-05-28 18:02:19 +09001748
Paul Duffin726d23c2020-01-22 16:30:37 +00001749 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1750 // tests if baz-test is actually linked to the test stubs lib
1751 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1752 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1753 "foo.stubs.test.jar")
1754 }
1755
Paul Duffina2db18f2020-01-22 17:11:15 +00001756 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1757 // tests if baz-29 is actually linked to the system 29 stubs lib
1758 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1759 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1760 "prebuilts/sdk/29/system/foo.jar")
1761 }
1762
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001763 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1764 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1765 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1766 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1767 "prebuilts/sdk/30/module-lib/foo.jar")
1768 }
1769
Jiyong Park1be96912018-05-28 18:02:19 +09001770 // test if baz has exported SDK lib names foo and bar to qux
1771 qux := ctx.ModuleForTests("qux", "android_common")
1772 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001773 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001774 if w := []string{"foo", "bar", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
Paul Duffin859fe962020-05-15 10:20:31 +01001775 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001776 }
1777 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001778}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001779
Anton Hansson7f66efa2020-10-08 14:47:23 +01001780func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
1781 ctx, _ := testJava(t, `
1782 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001783 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001784 srcs: ["a.java"],
1785 impl_only_libs: ["foo"],
1786 stub_only_libs: ["bar"],
1787 }
1788 java_library {
1789 name: "foo",
1790 srcs: ["a.java"],
1791 sdk_version: "current",
1792 }
1793 java_library {
1794 name: "bar",
1795 srcs: ["a.java"],
1796 sdk_version: "current",
1797 }
1798 `)
1799
Anton Hanssondff2c782020-12-21 17:10:01 +00001800 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Anton Hansson7f66efa2020-10-08 14:47:23 +01001801 implJavacCp := ctx.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
1802 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1803 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1804 }
1805 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001806 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Anton Hansson7f66efa2020-10-08 14:47:23 +01001807 stubsJavacCp := ctx.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
1808 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1809 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1810 }
1811}
1812
Paul Duffindaaa3322020-05-26 18:13:57 +01001813func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1814 ctx, _ := testJava(t, `
1815 java_sdk_library {
1816 name: "foo",
1817 srcs: ["a.java"],
1818 api_only: true,
1819 public: {
1820 enabled: true,
1821 },
1822 }
1823
1824 java_library {
1825 name: "bar",
1826 srcs: ["b.java"],
1827 libs: ["foo"],
1828 }
1829 `)
1830
1831 // The bar library should depend on the stubs jar.
1832 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1833 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1834 t.Errorf("expected %q, found %#q", expected, actual)
1835 }
1836}
1837
Paul Duffin46dc45a2020-05-14 15:39:10 +01001838func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1839 testJava(t, `
1840 java_sdk_library {
1841 name: "foo",
1842 srcs: ["a.java"],
1843 api_packages: ["foo"],
1844 public: {
1845 enabled: true,
1846 },
1847 }
1848
1849 java_library {
1850 name: "bar",
1851 srcs: ["b.java", ":foo{.public.stubs.source}"],
1852 }
1853 `)
1854}
1855
1856func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1857 testJavaError(t, `"foo" does not provide api scope system`, `
1858 java_sdk_library {
1859 name: "foo",
1860 srcs: ["a.java"],
1861 api_packages: ["foo"],
1862 public: {
1863 enabled: true,
1864 },
1865 }
1866
1867 java_library {
1868 name: "bar",
1869 srcs: ["b.java", ":foo{.system.stubs.source}"],
1870 }
1871 `)
1872}
1873
Paul Duffinca8d9a52020-06-26 22:20:25 +01001874func TestJavaSdkLibrary_Deps(t *testing.T) {
1875 ctx, _ := testJava(t, `
1876 java_sdk_library {
1877 name: "sdklib",
1878 srcs: ["a.java"],
1879 sdk_version: "none",
1880 system_modules: "none",
1881 public: {
1882 enabled: true,
1883 },
1884 }
1885 `)
1886
Paul Duffincee7e662020-07-09 17:32:57 +01001887 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001888 `dex2oatd`,
1889 `sdklib.impl`,
1890 `sdklib.stubs`,
1891 `sdklib.stubs.source`,
1892 `sdklib.xml`,
1893 })
1894}
1895
Paul Duffin46dc45a2020-05-14 15:39:10 +01001896func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1897 testJava(t, `
1898 java_sdk_library_import {
1899 name: "foo",
1900 public: {
1901 jars: ["a.jar"],
1902 stub_srcs: ["a.java"],
1903 current_api: "api/current.txt",
1904 removed_api: "api/removed.txt",
1905 },
1906 }
1907
1908 java_library {
1909 name: "bar",
1910 srcs: [":foo{.public.stubs.source}"],
1911 java_resources: [
1912 ":foo{.public.api.txt}",
1913 ":foo{.public.removed-api.txt}",
1914 ],
1915 }
1916 `)
1917}
1918
1919func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1920 bp := `
1921 java_sdk_library_import {
1922 name: "foo",
1923 public: {
1924 jars: ["a.jar"],
1925 },
1926 }
1927 `
1928
1929 t.Run("stubs.source", func(t *testing.T) {
1930 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1931 java_library {
1932 name: "bar",
1933 srcs: [":foo{.public.stubs.source}"],
1934 java_resources: [
1935 ":foo{.public.api.txt}",
1936 ":foo{.public.removed-api.txt}",
1937 ],
1938 }
1939 `)
1940 })
1941
1942 t.Run("api.txt", func(t *testing.T) {
1943 testJavaError(t, `api.txt not available for api scope public`, bp+`
1944 java_library {
1945 name: "bar",
1946 srcs: ["a.java"],
1947 java_resources: [
1948 ":foo{.public.api.txt}",
1949 ],
1950 }
1951 `)
1952 })
1953
1954 t.Run("removed-api.txt", func(t *testing.T) {
1955 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1956 java_library {
1957 name: "bar",
1958 srcs: ["a.java"],
1959 java_resources: [
1960 ":foo{.public.removed-api.txt}",
1961 ],
1962 }
1963 `)
1964 })
1965}
1966
Paul Duffin3375e352020-04-28 10:44:03 +01001967func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1968 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1969 java_sdk_library {
1970 name: "foo",
1971 srcs: ["a.java", "b.java"],
1972 api_packages: ["foo"],
1973 // Explicitly disable public to test the check that ensures the set of enabled
1974 // scopes is consistent.
1975 public: {
1976 enabled: false,
1977 },
1978 system: {
1979 enabled: true,
1980 },
1981 }
1982 `)
1983}
1984
Paul Duffin87a05a32020-05-12 11:50:28 +01001985func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1986 testJava(t, `
1987 java_sdk_library {
1988 name: "foo",
1989 srcs: ["a.java", "b.java"],
1990 api_packages: ["foo"],
1991 system: {
1992 enabled: true,
1993 sdk_version: "module_current",
1994 },
1995 }
1996 `)
1997}
1998
Paul Duffin0c5bae52020-06-02 13:00:08 +01001999func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
2000 testJava(t, `
2001 java_sdk_library {
2002 name: "foo",
2003 srcs: ["a.java", "b.java"],
2004 api_packages: ["foo"],
2005 system: {
2006 enabled: true,
2007 },
2008 module_lib: {
2009 enabled: true,
2010 },
2011 }
2012 `)
2013}
2014
2015func TestJavaSdkLibrary_SystemServer(t *testing.T) {
2016 testJava(t, `
2017 java_sdk_library {
2018 name: "foo",
2019 srcs: ["a.java", "b.java"],
2020 api_packages: ["foo"],
2021 system: {
2022 enabled: true,
2023 },
2024 system_server: {
2025 enabled: true,
2026 },
2027 }
2028 `)
2029}
2030
Paul Duffin803a9562020-05-20 11:52:25 +01002031func TestJavaSdkLibrary_MissingScope(t *testing.T) {
2032 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
2033 java_sdk_library {
2034 name: "foo",
2035 srcs: ["a.java"],
2036 public: {
2037 enabled: false,
2038 },
2039 }
2040
2041 java_library {
2042 name: "baz",
2043 srcs: ["a.java"],
2044 libs: ["foo"],
2045 sdk_version: "module_current",
2046 }
2047 `)
2048}
2049
2050func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
2051 testJava(t, `
2052 java_sdk_library {
2053 name: "foo",
2054 srcs: ["a.java"],
2055 system: {
2056 enabled: true,
2057 },
2058 }
2059
2060 java_library {
2061 name: "baz",
2062 srcs: ["a.java"],
2063 libs: ["foo"],
2064 // foo does not have module-lib scope so it should fallback to system
2065 sdk_version: "module_current",
2066 }
2067 `)
2068}
2069
Jiyong Park932cdfe2020-05-28 00:19:53 +09002070func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
2071 ctx, _ := testJava(t, `
2072 java_sdk_library {
2073 name: "foo",
2074 srcs: ["a.java"],
2075 system: {
2076 enabled: true,
2077 },
2078 default_to_stubs: true,
2079 }
2080
2081 java_library {
2082 name: "baz",
2083 srcs: ["a.java"],
2084 libs: ["foo"],
2085 // does not have sdk_version set, should fallback to module,
2086 // which will then fallback to system because the module scope
2087 // is not enabled.
2088 }
2089 `)
2090 // The baz library should depend on the system stubs jar.
2091 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
2092 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2093 t.Errorf("expected %q, found %#q", expected, actual)
2094 }
2095}
2096
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002097var compilerFlagsTestCases = []struct {
2098 in string
2099 out bool
2100}{
2101 {
2102 in: "a",
2103 out: false,
2104 },
2105 {
2106 in: "-a",
2107 out: true,
2108 },
2109 {
2110 in: "-no-jdk",
2111 out: false,
2112 },
2113 {
2114 in: "-no-stdlib",
2115 out: false,
2116 },
2117 {
2118 in: "-kotlin-home",
2119 out: false,
2120 },
2121 {
2122 in: "-kotlin-home /some/path",
2123 out: false,
2124 },
2125 {
2126 in: "-include-runtime",
2127 out: false,
2128 },
2129 {
2130 in: "-Xintellij-plugin-root",
2131 out: false,
2132 },
2133}
2134
2135type mockContext struct {
2136 android.ModuleContext
2137 result bool
2138}
2139
2140func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2141 // CheckBadCompilerFlags calls this function when the flag should be rejected
2142 ctx.result = false
2143}
2144
2145func TestCompilerFlags(t *testing.T) {
2146 for _, testCase := range compilerFlagsTestCases {
2147 ctx := &mockContext{result: true}
2148 CheckKotlincFlags(ctx, []string{testCase.in})
2149 if ctx.result != testCase.out {
2150 t.Errorf("incorrect output:")
2151 t.Errorf(" input: %#v", testCase.in)
2152 t.Errorf(" expected: %#v", testCase.out)
2153 t.Errorf(" got: %#v", ctx.result)
2154 }
2155 }
2156}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002157
2158// TODO(jungjw): Consider making this more robust by ignoring path order.
2159func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2160 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2161 flags := strings.Split(variables["javacFlags"], " ")
2162 got := ""
2163 for _, flag := range flags {
2164 keyEnd := strings.Index(flag, "=")
2165 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2166 got = flag[keyEnd+1:]
2167 break
2168 }
2169 }
2170 if expected != got {
2171 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2172 }
2173}
2174
2175func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002176 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002177 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002178 bp := `
2179 java_library {
2180 name: "foo",
2181 srcs: ["a.java"],
2182 java_version: "1.8",
2183 }
2184
2185 java_library {
2186 name: "bar",
2187 srcs: ["b.java"],
2188 sdk_version: "none",
2189 system_modules: "none",
2190 patch_module: "java.base",
2191 java_version: "1.8",
2192 }
2193
2194 java_library {
2195 name: "baz",
2196 srcs: ["c.java"],
2197 patch_module: "java.base",
2198 java_version: "1.8",
2199 }
2200 `
2201 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002202
2203 checkPatchModuleFlag(t, ctx, "foo", "")
2204 checkPatchModuleFlag(t, ctx, "bar", "")
2205 checkPatchModuleFlag(t, ctx, "baz", "")
2206 })
2207
Pete Gillin0c2143e2019-05-02 15:32:11 +01002208 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002209 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002210 bp := `
2211 java_library {
2212 name: "foo",
2213 srcs: ["a.java"],
2214 }
2215
2216 java_library {
2217 name: "bar",
2218 srcs: ["b.java"],
2219 sdk_version: "none",
2220 system_modules: "none",
2221 patch_module: "java.base",
2222 }
2223
2224 java_library {
2225 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002226 srcs: [
2227 "c.java",
2228 // Tests for b/150878007
2229 "dir/d.java",
2230 "dir2/e.java",
2231 "dir2/f.java",
2232 "nested/dir/g.java"
2233 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002234 patch_module: "java.base",
2235 }
2236 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002237 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002238
2239 checkPatchModuleFlag(t, ctx, "foo", "")
2240 expected := "java.base=.:" + buildDir
2241 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002242 expected = "java.base=" + strings.Join([]string{
2243 ".", buildDir, "dir", "dir2", "nested", moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002244 checkPatchModuleFlag(t, ctx, "baz", expected)
2245 })
2246}
Paul Duffina7b9f422020-01-10 17:12:18 +00002247
2248func TestJavaSystemModules(t *testing.T) {
2249 ctx, _ := testJava(t, `
2250 java_system_modules {
2251 name: "system-modules",
2252 libs: ["system-module1", "system-module2"],
2253 }
2254 java_library {
2255 name: "system-module1",
2256 srcs: ["a.java"],
2257 sdk_version: "none",
2258 system_modules: "none",
2259 }
2260 java_library {
2261 name: "system-module2",
2262 srcs: ["b.java"],
2263 sdk_version: "none",
2264 system_modules: "none",
2265 }
2266 `)
2267
2268 // check the existence of the module
2269 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2270
2271 cmd := systemModules.Rule("jarsTosystemModules")
2272
2273 // make sure the command compiles against the supplied modules.
2274 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2275 if !strings.Contains(cmd.Args["classpath"], module) {
2276 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2277 module)
2278 }
2279 }
2280}
Paul Duffin90169ba2020-01-10 17:16:44 +00002281
2282func TestJavaSystemModulesImport(t *testing.T) {
2283 ctx, _ := testJava(t, `
2284 java_system_modules_import {
2285 name: "system-modules",
2286 libs: ["system-module1", "system-module2"],
2287 }
2288 java_import {
2289 name: "system-module1",
2290 jars: ["a.jar"],
2291 }
2292 java_import {
2293 name: "system-module2",
2294 jars: ["b.jar"],
2295 }
2296 `)
2297
2298 // check the existence of the module
2299 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2300
2301 cmd := systemModules.Rule("jarsTosystemModules")
2302
2303 // make sure the command compiles against the supplied modules.
2304 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2305 if !strings.Contains(cmd.Args["classpath"], module) {
2306 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2307 module)
2308 }
2309 }
2310}
Paul Duffin83a2d962019-11-19 19:44:10 +00002311
2312func TestJavaLibraryWithSystemModules(t *testing.T) {
2313 ctx, _ := testJava(t, `
2314 java_library {
2315 name: "lib-with-source-system-modules",
2316 srcs: [
2317 "a.java",
2318 ],
2319 sdk_version: "none",
2320 system_modules: "source-system-modules",
2321 }
2322
2323 java_library {
2324 name: "source-jar",
2325 srcs: [
2326 "a.java",
2327 ],
2328 }
2329
2330 java_system_modules {
2331 name: "source-system-modules",
2332 libs: ["source-jar"],
2333 }
2334
2335 java_library {
2336 name: "lib-with-prebuilt-system-modules",
2337 srcs: [
2338 "a.java",
2339 ],
2340 sdk_version: "none",
2341 system_modules: "prebuilt-system-modules",
2342 }
2343
2344 java_import {
2345 name: "prebuilt-jar",
2346 jars: ["a.jar"],
2347 }
2348
2349 java_system_modules_import {
2350 name: "prebuilt-system-modules",
2351 libs: ["prebuilt-jar"],
2352 }
2353 `)
2354
2355 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2356
2357 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2358}
2359
2360func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2361 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2362 bootClasspath := javacRule.Args["bootClasspath"]
2363 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2364 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2365 }
2366}
Jiyong Park19604de2020-03-24 16:44:11 +09002367
2368func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2369 ctx, _ := testJava(t, `
2370 java_library {
2371 name: "foo",
2372 srcs: ["aidl/foo/IFoo.aidl"],
2373 libs: ["bar"],
2374 }
2375
2376 java_import {
2377 name: "bar",
2378 jars: ["a.jar"],
2379 aidl: {
2380 export_include_dirs: ["aidl/bar"],
2381 },
2382 }
2383 `)
2384
2385 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2386 expectedAidlFlag := "-Iaidl/bar"
2387 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2388 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2389 }
2390}
Liz Kammerdd849a82020-06-12 16:38:45 -07002391
Jooyung Hane197d8b2021-01-05 10:33:16 +09002392func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2393 ctx, _ := testJava(t, `
2394 java_library {
2395 name: "foo",
2396 srcs: ["aidl/foo/IFoo.aidl"],
2397 aidl: { flags: ["-Werror"], },
2398 }
2399 `)
2400
2401 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2402 expectedAidlFlag := "-Werror"
2403 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2404 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2405 }
2406}
2407
Liz Kammerdd849a82020-06-12 16:38:45 -07002408func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002409 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002410 java_test_host {
2411 name: "foo",
2412 srcs: ["a.java"],
2413 data_native_bins: ["bin"]
2414 }
2415
2416 python_binary_host {
2417 name: "bin",
2418 srcs: ["bin.py"],
2419 }
2420 `)
2421
2422 buildOS := android.BuildOs.String()
2423
2424 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002425 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Liz Kammerdd849a82020-06-12 16:38:45 -07002426 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2427 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2428 if !reflect.DeepEqual(expected, actual) {
2429 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2430 }
2431}