blob: dc498a492049a6f36eefccc551dd8868378d85df [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"
Nan Zhang61eaedb2017-11-02 13:28:15 -070022 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070023 "strings"
24 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070025
Jeongik Cha28df2572019-11-11 10:46:36 +090026 "github.com/google/blueprint/proptools"
27
Colin Crossa4f08812018-10-02 22:03:40 -070028 "android/soong/android"
29 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080030 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070031 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070032)
33
34var buildDir string
35
36func setUp() {
37 var err error
38 buildDir, err = ioutil.TempDir("", "soong_java_test")
39 if err != nil {
40 panic(err)
41 }
42}
43
44func tearDown() {
45 os.RemoveAll(buildDir)
46}
47
48func TestMain(m *testing.M) {
49 run := func() int {
50 setUp()
51 defer tearDown()
52
53 return m.Run()
54 }
55
56 os.Exit(run())
57}
Colin Cross527012a2017-11-30 22:56:16 -080058
59func testConfig(env map[string]string) android.Config {
Colin Crossb1974532019-02-15 10:37:39 -080060 return TestConfig(buildDir, env)
Colin Cross1369cdb2017-09-29 17:58:17 -070061}
62
Jaewoong Jungf9a04432019-07-17 11:15:09 -070063func testContext(bp string, fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070064
Colin Cross4c428df2017-09-15 17:36:05 -070065 ctx := android.NewTestArchContext()
Colin Cross4b49b762019-11-22 15:25:03 -080066 ctx.RegisterModuleType("android_app", AndroidAppFactory)
67 ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
68 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
69 ctx.RegisterModuleType("android_library", AndroidLibraryFactory)
70 ctx.RegisterModuleType("android_test", AndroidTestFactory)
71 ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
72 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
73 ctx.RegisterModuleType("java_binary", BinaryFactory)
74 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
75 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
76 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
77 ctx.RegisterModuleType("java_library", LibraryFactory)
78 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
79 ctx.RegisterModuleType("java_test", TestFactory)
80 ctx.RegisterModuleType("java_import", ImportFactory)
81 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
82 ctx.RegisterModuleType("java_defaults", defaultsFactory)
83 ctx.RegisterModuleType("java_system_modules", SystemModulesFactory)
84 ctx.RegisterModuleType("java_genrule", genRuleFactory)
85 ctx.RegisterModuleType("java_plugin", PluginFactory)
86 ctx.RegisterModuleType("dex_import", DexImportFactory)
87 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
88 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
89 ctx.RegisterModuleType("droiddoc", DroiddocFactory)
90 ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
91 ctx.RegisterModuleType("droiddoc_template", ExportedDroiddocDirFactory)
92 ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory)
93 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
94 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
95 ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
96 ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory)
97 ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory)
Colin Cross5ea9bcc2017-07-27 15:41:32 -070098 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
99 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -0700100 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900101 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Inseob Kimc0907f12019-02-08 21:00:45 +0900102 ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900103 })
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700104 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800105 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -0700106 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700107
108 // Register module types and mutators from cc needed for JNI testing
Colin Cross4b49b762019-11-22 15:25:03 -0800109 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
110 ctx.RegisterModuleType("cc_object", cc.ObjectFactory)
111 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
112 ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory)
113 ctx.RegisterModuleType("ndk_prebuilt_shared_stl", cc.NdkPrebuiltSharedStlFactory)
Colin Crossa4f08812018-10-02 22:03:40 -0700114 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
115 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
116 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
117 })
118
Colin Crossb1974532019-02-15 10:37:39 -0800119 bp += GatherRequiredDepsForTest()
Colin Cross1369cdb2017-09-29 17:58:17 -0700120
Colin Cross824bee32017-11-22 17:27:51 -0800121 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900122 "Android.bp": []byte(bp),
123 "a.java": nil,
124 "b.java": nil,
125 "c.java": nil,
126 "b.kt": nil,
127 "a.jar": nil,
128 "b.jar": nil,
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700129 "APP_NOTICE": nil,
130 "GENRULE_NOTICE": nil,
131 "LIB_NOTICE": nil,
132 "TOOL_NOTICE": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900133 "java-res/a/a": nil,
134 "java-res/b/b": nil,
135 "java-res2/a": nil,
136 "java-fg/a.java": nil,
137 "java-fg/b.java": nil,
138 "java-fg/c.java": nil,
139 "api/current.txt": nil,
140 "api/removed.txt": nil,
141 "api/system-current.txt": nil,
142 "api/system-removed.txt": nil,
143 "api/test-current.txt": nil,
144 "api/test-removed.txt": nil,
Colin Cross3047fa22019-04-18 10:56:44 -0700145 "framework/aidl/a.aidl": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700146
Jaewoong Jung710756a2019-06-04 11:53:47 -0700147 "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so": nil,
148
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100149 "prebuilts/sdk/14/public/android.jar": nil,
150 "prebuilts/sdk/14/public/framework.aidl": nil,
151 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross98fd5742019-01-09 23:04:25 -0800152 "prebuilts/sdk/17/public/android.jar": nil,
153 "prebuilts/sdk/17/public/framework.aidl": nil,
154 "prebuilts/sdk/17/system/android.jar": nil,
Colin Cross74362a42019-10-28 08:05:02 -0700155 "prebuilts/sdk/29/public/android.jar": nil,
156 "prebuilts/sdk/29/public/framework.aidl": nil,
157 "prebuilts/sdk/29/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700158 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100159 "prebuilts/sdk/current/public/android.jar": nil,
160 "prebuilts/sdk/current/public/framework.aidl": nil,
161 "prebuilts/sdk/current/public/core.jar": nil,
162 "prebuilts/sdk/current/system/android.jar": nil,
163 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900164 "prebuilts/sdk/28/public/api/foo.txt": nil,
165 "prebuilts/sdk/28/system/api/foo.txt": nil,
166 "prebuilts/sdk/28/test/api/foo.txt": nil,
167 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
168 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
169 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
170 "prebuilts/sdk/28/public/api/bar.txt": nil,
171 "prebuilts/sdk/28/system/api/bar.txt": nil,
172 "prebuilts/sdk/28/test/api/bar.txt": nil,
173 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
174 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
175 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700176 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900177 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800178
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700179 "prebuilts/apk/app.apk": nil,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700180 "prebuilts/apk/app_arm.apk": nil,
181 "prebuilts/apk/app_arm64.apk": nil,
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700182 "prebuilts/apk/app_xhdpi.apk": nil,
183 "prebuilts/apk/app_xxhdpi.apk": nil,
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700184
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800185 // For framework-res, which is an implicit dependency for framework
Dan Willemsen412160e2019-04-09 21:36:26 -0700186 "AndroidManifest.xml": nil,
187 "build/make/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800188
189 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800190
Colin Cross50ddcc42019-05-16 12:28:22 -0700191 "build/make/core/verify_uses_libraries.sh": nil,
192
Colin Cross66dbc0b2017-12-28 12:23:20 -0800193 "build/make/core/proguard.flags": nil,
194 "build/make/core/proguard_basic_keeps.flags": nil,
195
Colin Cross7fdd2b72018-01-02 18:14:25 -0800196 "jdk8/jre/lib/jce.jar": nil,
197 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700198 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800199
200 "bar-doc/a.java": nil,
201 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900202 "bar-doc/IFoo.aidl": nil,
Jiyong Park29074592019-07-07 16:27:47 +0900203 "bar-doc/IBar.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800204 "bar-doc/known_oj_tags.txt": nil,
205 "external/doclava/templates-sdk": nil,
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800206
207 "cert/new_cert.x509.pem": nil,
208 "cert/new_cert.pk8": nil,
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -0700209
210 "testdata/data": nil,
Paul Duffin91547182019-11-12 19:39:36 +0000211
212 "stubs-sources/foo/Foo.java": nil,
213 "stubs/sources/foo/Foo.java": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800214 }
215
216 for k, v := range fs {
217 mockFS[k] = v
218 }
219
220 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700221
Colin Cross527012a2017-11-30 22:56:16 -0800222 return ctx
223}
224
225func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800226 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800227
228 pathCtx := android.PathContextForTesting(config, nil)
229 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
230
231 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100232 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800233 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700234 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800235 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800236}
237
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900238func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900239 t.Helper()
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900240 return testJavaErrorWithConfig(t, pattern, bp, testConfig(nil))
241}
242
243func testJavaErrorWithConfig(t *testing.T, pattern string, bp string, config android.Config) (*android.TestContext, android.Config) {
244 t.Helper()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900245 ctx := testContext(bp, nil)
246
247 pathCtx := android.PathContextForTesting(config, nil)
248 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
249
250 ctx.Register()
251 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
252 if len(errs) > 0 {
253 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900254 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900255 }
256 _, errs = ctx.PrepareBuildActions(config)
257 if len(errs) > 0 {
258 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900259 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900260 }
261
262 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900263
264 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900265}
266
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700267func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800268 t.Helper()
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900269 return testJavaWithConfig(t, bp, testConfig(nil))
270}
271
272func testJavaWithConfig(t *testing.T, bp string, config android.Config) (*android.TestContext, android.Config) {
273 t.Helper()
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700274 ctx := testContext(bp, nil)
Colin Cross527012a2017-11-30 22:56:16 -0800275 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700276
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700277 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700278}
279
Colin Cross2acdae82017-09-15 19:44:24 -0700280func moduleToPath(name string) string {
281 switch {
282 case name == `""`:
283 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700284 case strings.HasSuffix(name, ".jar"):
285 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700286 default:
287 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700288 }
289}
290
Colin Cross72bb3632017-07-13 16:23:21 -0700291func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700292 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700293 java_library {
294 name: "foo",
295 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700296 libs: ["bar"],
297 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700298 }
299
300 java_library {
301 name: "bar",
302 srcs: ["b.java"],
303 }
304
305 java_library {
306 name: "baz",
307 srcs: ["c.java"],
308 }
Colin Crossd5934c82017-10-02 13:55:26 -0700309 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700310
Colin Cross4c428df2017-09-15 17:36:05 -0700311 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700312 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700313
314 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
315 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
316 }
317
Colin Cross1ee23172017-10-18 14:44:18 -0700318 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700319 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
320 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700321
Nan Zhanged19fc32017-10-19 13:06:22 -0700322 if !strings.Contains(javac.Args["classpath"], barTurbine) {
323 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700324 }
325
Nan Zhanged19fc32017-10-19 13:06:22 -0700326 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
327 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700328 }
329
330 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
331 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700332 }
333}
334
Artur Satayev9cf46692019-11-26 18:08:34 +0000335func TestExportedPlugins(t *testing.T) {
336 type Result struct {
337 library string
338 processors string
339 }
340 var tests = []struct {
341 name string
342 extra string
343 results []Result
344 }{
345 {
346 name: "Exported plugin is not a direct plugin",
347 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
348 results: []Result{{library: "exports", processors: "-proc:none"}},
349 },
350 {
351 name: "Exports plugin to dependee",
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: ["exports"]}
356 `,
357 results: []Result{
358 {library: "foo", processors: "-processor com.android.TestPlugin"},
359 {library: "bar", processors: "-processor com.android.TestPlugin"},
360 },
361 },
362 {
363 name: "Exports plugin to android_library",
364 extra: `
365 java_library{name: "exports", exported_plugins: ["plugin"]}
366 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
367 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
368 `,
369 results: []Result{
370 {library: "foo", processors: "-processor com.android.TestPlugin"},
371 {library: "bar", processors: "-processor com.android.TestPlugin"},
372 },
373 },
374 {
375 name: "Exports plugin is not propagated via transitive deps",
376 extra: `
377 java_library{name: "exports", exported_plugins: ["plugin"]}
378 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
379 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
380 `,
381 results: []Result{
382 {library: "foo", processors: "-processor com.android.TestPlugin"},
383 {library: "bar", processors: "-proc:none"},
384 },
385 },
386 {
387 name: "Exports plugin appends to plugins",
388 extra: `
389 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
390 java_library{name: "exports", exported_plugins: ["plugin"]}
391 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
392 `,
393 results: []Result{
394 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
395 },
396 },
397 }
398
399 for _, test := range tests {
400 t.Run(test.name, func(t *testing.T) {
401 ctx, _ := testJava(t, `
402 java_plugin {
403 name: "plugin",
404 processor_class: "com.android.TestPlugin",
405 }
406 `+test.extra)
407
408 for _, want := range test.results {
409 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
410 if javac.Args["processor"] != want.processors {
411 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
412 }
413 }
414 })
415 }
416}
417
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900418func TestSdkVersionByPartition(t *testing.T) {
419 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900420 java_library {
421 name: "foo",
422 srcs: ["a.java"],
423 vendor: true,
424 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900425 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900426
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900427 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900428 java_library {
429 name: "bar",
430 srcs: ["b.java"],
431 }
432 `)
433
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900434 for _, enforce := range []bool{true, false} {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900435
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900436 config := testConfig(nil)
437 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
438 bp := `
439 java_library {
440 name: "foo",
441 srcs: ["a.java"],
442 product_specific: true,
443 }
444 `
445 if enforce {
446 testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", bp, config)
447 } else {
448 testJavaWithConfig(t, bp, config)
449 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900450 }
451}
452
Colin Crossd5934c82017-10-02 13:55:26 -0700453func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700454 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700455 java_library {
456 name: "foo",
457 srcs: ["a.java"],
458 target: {
459 android: {
460 srcs: ["b.java"],
461 },
462 },
463 }
464 `)
465
466 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
467 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
468 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
469 }
470}
471
Colin Cross6b4a32d2017-12-05 13:42:45 -0800472func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700473 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800474 java_library_host {
475 name: "foo",
476 srcs: ["a.java"],
477 }
478
479 java_binary_host {
480 name: "bar",
481 srcs: ["b.java"],
482 static_libs: ["foo"],
483 }
484 `)
485
486 buildOS := android.BuildOs.String()
487
488 bar := ctx.ModuleForTests("bar", buildOS+"_common")
489 barJar := bar.Output("bar.jar").Output.String()
490 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
491 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
492
493 // Test that the install binary wrapper depends on the installed jar file
494 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
495 t.Errorf("expected binary wrapper implicits [%q], got %v",
496 barJar, barWrapperDeps)
497 }
498
499}
500
Colin Cross72bb3632017-07-13 16:23:21 -0700501func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700502 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700503 java_library {
504 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000505 srcs: ["a.java", ":stubs-source"],
Colin Cross79c7c262019-04-17 11:11:46 -0700506 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700507 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700508 }
509
Colin Cross74d73e22017-08-02 11:05:49 -0700510 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700511 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700512 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700513 }
514
Colin Cross74d73e22017-08-02 11:05:49 -0700515 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700516 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700517 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700518 }
Colin Cross42be7612019-02-21 18:12:14 -0800519
520 dex_import {
521 name: "qux",
522 jars: ["b.jar"],
523 }
Colin Cross79c7c262019-04-17 11:11:46 -0700524
525 java_sdk_library_import {
526 name: "sdklib",
527 jars: ["b.jar"],
528 }
Paul Duffin91547182019-11-12 19:39:36 +0000529
530 prebuilt_stubs_sources {
531 name: "stubs-source",
532 srcs: ["stubs/sources/**/*.java"],
533 }
Colin Cross72bb3632017-07-13 16:23:21 -0700534 `)
535
Colin Cross4c428df2017-09-15 17:36:05 -0700536 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700537 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700538 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
539 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700540 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700541
Paul Duffin91547182019-11-12 19:39:36 +0000542 inputs := []string{}
543 for _, p := range javac.BuildParams.Inputs {
544 inputs = append(inputs, p.String())
545 }
546
547 expected := []string{
548 "a.java",
549 "stubs/sources/foo/Foo.java",
550 }
551 if !reflect.DeepEqual(expected, inputs) {
552 t.Errorf("foo inputs incorrect: expected %q, found %q", expected, inputs)
553 }
554
Colin Cross37f6d792018-07-12 12:28:41 -0700555 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
556 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700557 }
558
Colin Cross79c7c262019-04-17 11:11:46 -0700559 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
560 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
561 }
562
Colin Cross37f6d792018-07-12 12:28:41 -0700563 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
564 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700565 }
Colin Cross42be7612019-02-21 18:12:14 -0800566
567 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700568}
569
Colin Cross89536d42017-07-07 14:35:50 -0700570func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700571 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700572 java_defaults {
573 name: "defaults",
574 srcs: ["a.java"],
575 libs: ["bar"],
576 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700577 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700578 }
579
580 java_library {
581 name: "foo",
582 defaults: ["defaults"],
583 }
584
585 java_library {
586 name: "bar",
587 srcs: ["b.java"],
588 }
589
590 java_library {
591 name: "baz",
592 srcs: ["c.java"],
593 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700594
595 android_test {
596 name: "atestOptimize",
597 defaults: ["defaults"],
598 optimize: {enabled: true},
599 }
600
601 android_test {
602 name: "atestNoOptimize",
603 defaults: ["defaults"],
604 }
605
606 android_test {
607 name: "atestDefault",
608 srcs: ["a.java"],
609 }
Colin Cross89536d42017-07-07 14:35:50 -0700610 `)
611
Colin Cross4c428df2017-09-15 17:36:05 -0700612 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700613 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700614
615 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
616 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
617 }
618
Nan Zhanged19fc32017-10-19 13:06:22 -0700619 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
620 if !strings.Contains(javac.Args["classpath"], barTurbine) {
621 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700622 }
623
Colin Cross1ee23172017-10-18 14:44:18 -0700624 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700625 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
626 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700627 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700628
629 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
630 if atestOptimize.Output == nil {
631 t.Errorf("atestOptimize should optimize APK")
632 }
633
634 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
635 if atestNoOptimize.Output == nil {
636 t.Errorf("atestNoOptimize should not optimize APK")
637 }
638
639 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
640 if atestDefault.Output == nil {
641 t.Errorf("atestDefault should optimize APK")
642 }
Colin Cross89536d42017-07-07 14:35:50 -0700643}
644
Colin Cross0f37af02017-09-27 17:42:05 -0700645func TestResources(t *testing.T) {
646 var table = []struct {
647 name string
648 prop string
649 extra string
650 args string
651 }{
652 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700653 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700654 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800655 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700656 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700657 },
658 {
659 // Test that a module with java_resources includes the files
660 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700661 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
662 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700663 },
664 {
665 // Test that a module with a filegroup in java_resources includes the files with the
666 // path prefix
667 name: "resource filegroup",
668 prop: `java_resources: [":foo-res"]`,
669 extra: `
670 filegroup {
671 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800672 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700673 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700674 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700675 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700676 },
677 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700678 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
679 name: "wildcard dirs",
680 prop: `java_resource_dirs: ["java-res/*"]`,
681 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
682 },
683 {
684 // Test that a module exclude_java_resource_dirs excludes the files
685 name: "wildcard dirs",
686 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
687 args: "-C java-res/a -f java-res/a/a",
688 },
Colin Crosscedd4762018-09-13 11:26:19 -0700689 {
690 // Test wildcards in java_resources
691 name: "wildcard files",
692 prop: `java_resources: ["java-res/**/*"]`,
693 args: "-C . -f java-res/a/a -f java-res/b/b",
694 },
695 {
696 // Test exclude_java_resources with java_resources
697 name: "wildcard files with exclude",
698 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
699 args: "-C . -f java-res/a/a",
700 },
701 {
702 // Test exclude_java_resources with java_resource_dirs
703 name: "resource dirs with exclude files",
704 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
705 args: "-C java-res -f java-res/a/a",
706 },
707 {
708 // Test exclude_java_resource_dirs with java_resource_dirs
709 name: "resource dirs with exclude files",
710 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
711 args: "-C java-res -f java-res/a/a -f java-res/b/b",
712 },
Colin Cross0f37af02017-09-27 17:42:05 -0700713 }
714
715 for _, test := range table {
716 t.Run(test.name, func(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700717 ctx, _ := testJava(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700718 java_library {
719 name: "foo",
720 srcs: [
721 "a.java",
722 "b.java",
723 "c.java",
724 ],
725 `+test.prop+`,
726 }
727 `+test.extra)
728
Colin Cross331a1212018-08-15 20:40:52 -0700729 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700730 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700731
732 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
733 t.Errorf("foo combined jars %v does not contain %q",
734 foo.Inputs.Strings(), fooRes.Output.String())
735 }
736
Colin Crossaf9c55b2017-10-03 14:50:08 -0700737 if fooRes.Args["jarArgs"] != test.args {
738 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700739 fooRes.Args["jarArgs"], test.args)
740 }
741 })
742 }
743}
744
Colin Cross0c4ce212019-05-03 15:28:19 -0700745func TestIncludeSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700746 ctx, _ := testJava(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700747 java_library {
748 name: "foo",
749 srcs: [
750 "a.java",
751 "b.java",
752 "c.java",
753 ],
754 include_srcs: true,
755 }
756
757 java_library {
758 name: "bar",
759 srcs: [
760 "a.java",
761 "b.java",
762 "c.java",
763 ],
764 java_resource_dirs: ["java-res"],
765 include_srcs: true,
766 }
767 `)
768
769 // Test a library with include_srcs: true
770 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
771 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
772
773 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
774 t.Errorf("foo combined jars %v does not contain %q", w, g)
775 }
776
777 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
778 t.Errorf("foo source jar args %q is not %q", w, g)
779 }
780
781 // Test a library with include_srcs: true and resources
782 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
783 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
784 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
785 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
786
787 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
788 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
789 }
790
791 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
792 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
793 }
794
795 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
796 t.Errorf("bar combined jars %v does not contain %q", w, g)
797 }
798
799 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
800 t.Errorf("bar source jar args %q is not %q", w, g)
801 }
802
803 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
804 t.Errorf("bar resource jar args %q is not %q", w, g)
805 }
806}
807
Colin Cross54190b32017-10-09 15:34:10 -0700808func TestGeneratedSources(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700809 ctx, _ := testJava(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700810 java_library {
811 name: "foo",
812 srcs: [
813 "a*.java",
814 ":gen",
815 "b*.java",
816 ],
817 }
818
819 genrule {
820 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800821 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700822 out: ["gen.java"],
823 }
824 `)
825
826 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
827 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
828
Colin Cross15e86d92017-10-20 15:07:08 -0700829 if filepath.Base(genrule.Output.String()) != "gen.java" {
830 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700831 }
832
833 if len(javac.Inputs) != 3 ||
834 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700835 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700836 javac.Inputs[2].String() != "b.java" {
837 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
838 }
839}
840
Nan Zhang61eaedb2017-11-02 13:28:15 -0700841func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700842 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700843 java_library {
844 name: "foo",
845 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900846 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700847 }
848
849 java_library {
850 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800851 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700852 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900853 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700854 }
855
856 java_library {
857 name: "baz",
858 srcs: ["c.java"],
859 libs: ["bar"],
860 sdk_version: "14",
861 }
862 `)
863
864 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
865 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
866 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
867 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
868 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
869
870 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
871 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
872 }
873
874 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
875 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
876 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
877 }
878 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
879 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
880 }
881 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
882 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
883 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100884 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700885 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100886 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700887 }
888}
889
890func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700891 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700892 java_library {
893 name: "bar",
894 srcs: ["a.java","b.java","c.java"],
895 javac_shard_size: 1
896 }
897 `)
898
899 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
900 for i := 0; i < 3; i++ {
901 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
902 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
903 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
904 }
905 }
906}
907
Nan Zhang581fd212018-01-10 16:06:12 -0800908func TestDroiddoc(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700909 ctx, _ := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800910 droiddoc_template {
911 name: "droiddoc-templates-sdk",
912 path: ".",
913 }
Jiyong Park29074592019-07-07 16:27:47 +0900914 filegroup {
915 name: "bar-doc-aidl-srcs",
916 srcs: ["bar-doc/IBar.aidl"],
917 path: "bar-doc",
918 }
Nan Zhang581fd212018-01-10 16:06:12 -0800919 droiddoc {
920 name: "bar-doc",
921 srcs: [
922 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900923 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +0900924 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -0800925 ],
926 exclude_srcs: [
927 "bar-doc/b.java"
928 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800929 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800930 hdf: [
931 "android.whichdoc offline",
932 ],
933 knowntags: [
934 "bar-doc/known_oj_tags.txt",
935 ],
936 proofread_file: "libcore-proofread.txt",
937 todo_file: "libcore-docs-todo.html",
938 args: "-offlinemode -title \"libcore\"",
939 }
940 `)
941
Colin Crossc0806172019-06-14 18:51:47 -0700942 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +0900943 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -0700944 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +0900945 javaSrcs = append(javaSrcs, i.Base())
946 }
Colin Crossc0806172019-06-14 18:51:47 -0700947 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
948 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +0900949 }
950
Colin Crossc0806172019-06-14 18:51:47 -0700951 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
952 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
953 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
954 }
955
956 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
957 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +0900958 }
Nan Zhang581fd212018-01-10 16:06:12 -0800959}
960
Colin Cross54250902017-12-05 09:28:08 -0800961func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700962 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -0800963 java_library {
964 name: "foo",
965 srcs: ["a.java"],
966 }
967
968 java_genrule {
969 name: "jargen",
970 tool_files: ["b.java"],
971 cmd: "$(location b.java) $(in) $(out)",
972 out: ["jargen.jar"],
973 srcs: [":foo"],
974 }
975
976 java_library {
977 name: "bar",
978 static_libs: ["jargen"],
979 srcs: ["c.java"],
980 }
981
982 java_library {
983 name: "baz",
984 libs: ["jargen"],
985 srcs: ["c.java"],
986 }
987 `)
988
989 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
990 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
991 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
992 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
993 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
994
995 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
996 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
997 }
998
999 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1000 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1001 }
1002
1003 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1004 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1005 }
1006
1007 if len(barCombined.Inputs) != 2 ||
1008 barCombined.Inputs[0].String() != bar.Output.String() ||
1009 barCombined.Inputs[1].String() != jargen.Output.String() {
1010 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1011 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1012 }
1013}
1014
Nan Zhang27e284d2018-02-09 21:03:53 +00001015func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001016 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001017 java_library {
1018 name: "foo",
1019 srcs: ["a.java", ":foo-srcs"],
1020 exclude_srcs: ["a.java", ":foo-excludes"],
1021 }
1022
1023 filegroup {
1024 name: "foo-srcs",
1025 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1026 }
1027
1028 filegroup {
1029 name: "foo-excludes",
1030 srcs: ["java-fg/a.java", "java-fg/b.java"],
1031 }
1032 `)
1033
1034 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1035
1036 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1037 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1038 }
1039}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001040
Paul Duffin52d398a2019-06-11 12:31:14 +01001041func TestJavaLibrary(t *testing.T) {
1042 config := testConfig(nil)
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001043 ctx := testContext("", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001044 "libcore/Android.bp": []byte(`
1045 java_library {
1046 name: "core",
1047 sdk_version: "none",
1048 system_modules: "none",
1049 }`),
1050 })
1051 run(t, ctx, config)
1052}
1053
Jiyong Parkc678ad32018-04-10 13:07:10 +09001054func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001055 ctx, _ := testJava(t, `
Jiyong Parkc678ad32018-04-10 13:07:10 +09001056 droiddoc_template {
1057 name: "droiddoc-templates-sdk",
1058 path: ".",
1059 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001060 java_sdk_library {
1061 name: "foo",
1062 srcs: ["a.java", "b.java"],
1063 api_packages: ["foo"],
1064 }
1065 java_sdk_library {
1066 name: "bar",
1067 srcs: ["a.java", "b.java"],
1068 api_packages: ["bar"],
1069 }
1070 java_library {
1071 name: "baz",
1072 srcs: ["c.java"],
1073 libs: ["foo", "bar"],
1074 sdk_version: "system_current",
1075 }
Jiyong Park1be96912018-05-28 18:02:19 +09001076 java_library {
1077 name: "qux",
1078 srcs: ["c.java"],
1079 libs: ["baz"],
1080 sdk_version: "system_current",
1081 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001082 `)
1083
1084 // check the existence of the internal modules
1085 ctx.ModuleForTests("foo", "android_common")
1086 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
1087 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001088 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001089 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
1090 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001091 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jaewoong Jungb9a11512019-01-15 10:47:05 -08001092 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +09001093 ctx.ModuleForTests("foo.api.public.28", "")
1094 ctx.ModuleForTests("foo.api.system.28", "")
1095 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001096
1097 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1098 // tests if baz is actually linked to the stubs lib
1099 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1100 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1101 "foo.stubs.system.jar")
1102 }
1103 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001104 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001105 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001106 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001107 }
1108 // test if baz is not linked to the system variant of foo
1109 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1110 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1111 "foo.stubs.jar")
1112 }
Jiyong Park1be96912018-05-28 18:02:19 +09001113
1114 // test if baz has exported SDK lib names foo and bar to qux
1115 qux := ctx.ModuleForTests("qux", "android_common")
1116 if quxLib, ok := qux.Module().(*Library); ok {
1117 sdkLibs := quxLib.ExportedSdkLibs()
1118 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
1119 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
1120 }
1121 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001122}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001123
1124var compilerFlagsTestCases = []struct {
1125 in string
1126 out bool
1127}{
1128 {
1129 in: "a",
1130 out: false,
1131 },
1132 {
1133 in: "-a",
1134 out: true,
1135 },
1136 {
1137 in: "-no-jdk",
1138 out: false,
1139 },
1140 {
1141 in: "-no-stdlib",
1142 out: false,
1143 },
1144 {
1145 in: "-kotlin-home",
1146 out: false,
1147 },
1148 {
1149 in: "-kotlin-home /some/path",
1150 out: false,
1151 },
1152 {
1153 in: "-include-runtime",
1154 out: false,
1155 },
1156 {
1157 in: "-Xintellij-plugin-root",
1158 out: false,
1159 },
1160}
1161
1162type mockContext struct {
1163 android.ModuleContext
1164 result bool
1165}
1166
1167func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1168 // CheckBadCompilerFlags calls this function when the flag should be rejected
1169 ctx.result = false
1170}
1171
1172func TestCompilerFlags(t *testing.T) {
1173 for _, testCase := range compilerFlagsTestCases {
1174 ctx := &mockContext{result: true}
1175 CheckKotlincFlags(ctx, []string{testCase.in})
1176 if ctx.result != testCase.out {
1177 t.Errorf("incorrect output:")
1178 t.Errorf(" input: %#v", testCase.in)
1179 t.Errorf(" expected: %#v", testCase.out)
1180 t.Errorf(" got: %#v", ctx.result)
1181 }
1182 }
1183}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001184
1185// TODO(jungjw): Consider making this more robust by ignoring path order.
1186func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1187 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1188 flags := strings.Split(variables["javacFlags"], " ")
1189 got := ""
1190 for _, flag := range flags {
1191 keyEnd := strings.Index(flag, "=")
1192 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1193 got = flag[keyEnd+1:]
1194 break
1195 }
1196 }
1197 if expected != got {
1198 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1199 }
1200}
1201
1202func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001203 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001204 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001205 bp := `
1206 java_library {
1207 name: "foo",
1208 srcs: ["a.java"],
1209 java_version: "1.8",
1210 }
1211
1212 java_library {
1213 name: "bar",
1214 srcs: ["b.java"],
1215 sdk_version: "none",
1216 system_modules: "none",
1217 patch_module: "java.base",
1218 java_version: "1.8",
1219 }
1220
1221 java_library {
1222 name: "baz",
1223 srcs: ["c.java"],
1224 patch_module: "java.base",
1225 java_version: "1.8",
1226 }
1227 `
1228 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001229
1230 checkPatchModuleFlag(t, ctx, "foo", "")
1231 checkPatchModuleFlag(t, ctx, "bar", "")
1232 checkPatchModuleFlag(t, ctx, "baz", "")
1233 })
1234
Pete Gillin0c2143e2019-05-02 15:32:11 +01001235 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001236 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001237 bp := `
1238 java_library {
1239 name: "foo",
1240 srcs: ["a.java"],
1241 }
1242
1243 java_library {
1244 name: "bar",
1245 srcs: ["b.java"],
1246 sdk_version: "none",
1247 system_modules: "none",
1248 patch_module: "java.base",
1249 }
1250
1251 java_library {
1252 name: "baz",
1253 srcs: ["c.java"],
1254 patch_module: "java.base",
1255 }
1256 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001257 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001258
1259 checkPatchModuleFlag(t, ctx, "foo", "")
1260 expected := "java.base=.:" + buildDir
1261 checkPatchModuleFlag(t, ctx, "bar", expected)
Insun Kang23433862019-01-24 20:46:28 +09001262 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework"), moduleToPath("updatable_media_stubs")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001263 checkPatchModuleFlag(t, ctx, "baz", expected)
1264 })
1265}