blob: f0cb6f8bda09b8cec6f40a6b2c1f0e3b55238491 [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
Colin Crossa4f08812018-10-02 22:03:40 -070026 "android/soong/android"
27 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080028 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070030)
31
32var buildDir string
33
34func setUp() {
35 var err error
36 buildDir, err = ioutil.TempDir("", "soong_java_test")
37 if err != nil {
38 panic(err)
39 }
40}
41
42func tearDown() {
43 os.RemoveAll(buildDir)
44}
45
46func TestMain(m *testing.M) {
47 run := func() int {
48 setUp()
49 defer tearDown()
50
51 return m.Run()
52 }
53
54 os.Exit(run())
55}
Colin Cross527012a2017-11-30 22:56:16 -080056
57func testConfig(env map[string]string) android.Config {
Colin Crossb1974532019-02-15 10:37:39 -080058 return TestConfig(buildDir, env)
Colin Cross1369cdb2017-09-29 17:58:17 -070059}
60
Jaewoong Jungf9a04432019-07-17 11:15:09 -070061func testContext(bp string, fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070062
Colin Cross4c428df2017-09-15 17:36:05 -070063 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070064 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Jaewoong Jung2ad817c2019-01-18 14:27:16 -080065 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(AndroidAppCertificateFactory))
Jaewoong Jungccbb3932019-04-15 09:48:31 -070066 ctx.RegisterModuleType("android_app_import", android.ModuleFactoryAdaptor(AndroidAppImportFactory))
Colin Crossa97c5d32018-03-28 14:58:31 -070067 ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070068 ctx.RegisterModuleType("android_test", android.ModuleFactoryAdaptor(AndroidTestFactory))
Colin Cross638149e2019-01-05 22:12:12 -080069 ctx.RegisterModuleType("android_test_helper_app", android.ModuleFactoryAdaptor(AndroidTestHelperAppFactory))
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -070070 ctx.RegisterModuleType("android_test_import", android.ModuleFactoryAdaptor(AndroidTestImportFactory))
Colin Cross638149e2019-01-05 22:12:12 -080071 ctx.RegisterModuleType("java_binary", android.ModuleFactoryAdaptor(BinaryFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080072 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Cross3d7c9822019-03-01 13:46:24 -080073 ctx.RegisterModuleType("java_device_for_host", android.ModuleFactoryAdaptor(DeviceForHostFactory))
74 ctx.RegisterModuleType("java_host_for_device", android.ModuleFactoryAdaptor(HostForDeviceFactory))
Colin Cross9ae1b922018-06-26 17:59:05 -070075 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross2acdae82017-09-15 19:44:24 -070076 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross638149e2019-01-05 22:12:12 -080077 ctx.RegisterModuleType("java_test", android.ModuleFactoryAdaptor(TestFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070078 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross3d7c9822019-03-01 13:46:24 -080079 ctx.RegisterModuleType("java_import_host", android.ModuleFactoryAdaptor(ImportFactoryHost))
Colin Cross89536d42017-07-07 14:35:50 -070080 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070081 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080082 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Colin Crossbe9cdb82019-01-21 21:37:16 -080083 ctx.RegisterModuleType("java_plugin", android.ModuleFactoryAdaptor(PluginFactory))
Colin Cross42be7612019-02-21 18:12:14 -080084 ctx.RegisterModuleType("dex_import", android.ModuleFactoryAdaptor(DexImportFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070085 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070086 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080087 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
88 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Nan Zhangf4936b02018-08-01 15:00:28 -070089 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
Inseob Kimc0907f12019-02-08 21:00:45 +090090 ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(SdkLibraryFactory))
Colin Cross79c7c262019-04-17 11:11:46 -070091 ctx.RegisterModuleType("java_sdk_library_import", android.ModuleFactoryAdaptor(sdkLibraryImportFactory))
Jaewoong Jung525443a2019-02-28 15:35:54 -080092 ctx.RegisterModuleType("override_android_app", android.ModuleFactoryAdaptor(OverrideAndroidAppModuleFactory))
Inseob Kimc0907f12019-02-08 21:00:45 +090093 ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(PrebuiltApisFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070094 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
95 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070096 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090097 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Inseob Kimc0907f12019-02-08 21:00:45 +090098 ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +090099 })
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700100 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800101 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -0700102 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700103
104 // Register module types and mutators from cc needed for JNI testing
105 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
106 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
107 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Colin Cross47fa9d32019-03-26 10:51:39 -0700108 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jaewoong Jung710756a2019-06-04 11:53:47 -0700109 ctx.RegisterModuleType("ndk_prebuilt_shared_stl", android.ModuleFactoryAdaptor(cc.NdkPrebuiltSharedStlFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700110 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
111 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
112 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
113 })
114
Colin Crossb1974532019-02-15 10:37:39 -0800115 bp += GatherRequiredDepsForTest()
Colin Cross1369cdb2017-09-29 17:58:17 -0700116
Colin Cross824bee32017-11-22 17:27:51 -0800117 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900118 "Android.bp": []byte(bp),
119 "a.java": nil,
120 "b.java": nil,
121 "c.java": nil,
122 "b.kt": nil,
123 "a.jar": nil,
124 "b.jar": nil,
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700125 "APP_NOTICE": nil,
126 "GENRULE_NOTICE": nil,
127 "LIB_NOTICE": nil,
128 "TOOL_NOTICE": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900129 "java-res/a/a": nil,
130 "java-res/b/b": nil,
131 "java-res2/a": nil,
132 "java-fg/a.java": nil,
133 "java-fg/b.java": nil,
134 "java-fg/c.java": nil,
135 "api/current.txt": nil,
136 "api/removed.txt": nil,
137 "api/system-current.txt": nil,
138 "api/system-removed.txt": nil,
139 "api/test-current.txt": nil,
140 "api/test-removed.txt": nil,
Colin Cross3047fa22019-04-18 10:56:44 -0700141 "framework/aidl/a.aidl": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700142
Jaewoong Jung710756a2019-06-04 11:53:47 -0700143 "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so": nil,
144
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100145 "prebuilts/sdk/14/public/android.jar": nil,
146 "prebuilts/sdk/14/public/framework.aidl": nil,
147 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross98fd5742019-01-09 23:04:25 -0800148 "prebuilts/sdk/17/public/android.jar": nil,
149 "prebuilts/sdk/17/public/framework.aidl": nil,
150 "prebuilts/sdk/17/system/android.jar": nil,
Colin Crossff0daf42019-04-02 16:10:56 -0700151 "prebuilts/sdk/25/public/android.jar": nil,
152 "prebuilts/sdk/25/public/framework.aidl": nil,
153 "prebuilts/sdk/25/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700154 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100155 "prebuilts/sdk/current/public/android.jar": nil,
156 "prebuilts/sdk/current/public/framework.aidl": nil,
157 "prebuilts/sdk/current/public/core.jar": nil,
158 "prebuilts/sdk/current/system/android.jar": nil,
159 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900160 "prebuilts/sdk/28/public/api/foo.txt": nil,
161 "prebuilts/sdk/28/system/api/foo.txt": nil,
162 "prebuilts/sdk/28/test/api/foo.txt": nil,
163 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
164 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
165 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
166 "prebuilts/sdk/28/public/api/bar.txt": nil,
167 "prebuilts/sdk/28/system/api/bar.txt": nil,
168 "prebuilts/sdk/28/test/api/bar.txt": nil,
169 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
170 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
171 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700172 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900173 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800174
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700175 "prebuilts/apk/app.apk": nil,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -0700176 "prebuilts/apk/app_arm.apk": nil,
177 "prebuilts/apk/app_arm64.apk": nil,
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700178 "prebuilts/apk/app_xhdpi.apk": nil,
179 "prebuilts/apk/app_xxhdpi.apk": nil,
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700180
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800181 // For framework-res, which is an implicit dependency for framework
Dan Willemsen412160e2019-04-09 21:36:26 -0700182 "AndroidManifest.xml": nil,
183 "build/make/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800184
185 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800186
Colin Cross50ddcc42019-05-16 12:28:22 -0700187 "build/make/core/verify_uses_libraries.sh": nil,
188
Colin Cross66dbc0b2017-12-28 12:23:20 -0800189 "build/make/core/proguard.flags": nil,
190 "build/make/core/proguard_basic_keeps.flags": nil,
191
Colin Cross7fdd2b72018-01-02 18:14:25 -0800192 "jdk8/jre/lib/jce.jar": nil,
193 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700194 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800195
196 "bar-doc/a.java": nil,
197 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900198 "bar-doc/IFoo.aidl": nil,
Jiyong Park29074592019-07-07 16:27:47 +0900199 "bar-doc/IBar.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800200 "bar-doc/known_oj_tags.txt": nil,
201 "external/doclava/templates-sdk": nil,
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800202
203 "cert/new_cert.x509.pem": nil,
204 "cert/new_cert.pk8": nil,
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -0700205
206 "testdata/data": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800207 }
208
209 for k, v := range fs {
210 mockFS[k] = v
211 }
212
213 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700214
Colin Cross527012a2017-11-30 22:56:16 -0800215 return ctx
216}
217
218func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800219 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800220
221 pathCtx := android.PathContextForTesting(config, nil)
222 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
223
224 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100225 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800226 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700227 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800228 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800229}
230
Jeongik Cha538c0d02019-07-11 15:54:27 +0900231func testJavaError(t *testing.T, pattern string, bp string) {
232 t.Helper()
233 config := testConfig(nil)
234 ctx := testContext(bp, nil)
235
236 pathCtx := android.PathContextForTesting(config, nil)
237 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
238
239 ctx.Register()
240 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
241 if len(errs) > 0 {
242 android.FailIfNoMatchingErrors(t, pattern, errs)
243 return
244 }
245 _, errs = ctx.PrepareBuildActions(config)
246 if len(errs) > 0 {
247 android.FailIfNoMatchingErrors(t, pattern, errs)
248 return
249 }
250
251 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
252}
253
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700254func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800255 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800256 config := testConfig(nil)
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700257 ctx := testContext(bp, nil)
Colin Cross527012a2017-11-30 22:56:16 -0800258 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700259
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700260 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700261}
262
Colin Cross2acdae82017-09-15 19:44:24 -0700263func moduleToPath(name string) string {
264 switch {
265 case name == `""`:
266 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700267 case strings.HasSuffix(name, ".jar"):
268 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700269 default:
270 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700271 }
272}
273
Colin Cross72bb3632017-07-13 16:23:21 -0700274func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700275 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700276 java_library {
277 name: "foo",
278 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700279 libs: ["bar"],
280 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700281 }
282
283 java_library {
284 name: "bar",
285 srcs: ["b.java"],
286 }
287
288 java_library {
289 name: "baz",
290 srcs: ["c.java"],
291 }
Colin Crossd5934c82017-10-02 13:55:26 -0700292 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700293
Colin Cross4c428df2017-09-15 17:36:05 -0700294 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700295 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700296
297 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
298 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
299 }
300
Colin Cross1ee23172017-10-18 14:44:18 -0700301 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700302 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
303 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700304
Nan Zhanged19fc32017-10-19 13:06:22 -0700305 if !strings.Contains(javac.Args["classpath"], barTurbine) {
306 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700307 }
308
Nan Zhanged19fc32017-10-19 13:06:22 -0700309 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
310 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700311 }
312
313 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
314 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700315 }
316}
317
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900318func TestSdkVersion(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700319 ctx, _ := testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900320 java_library {
321 name: "foo",
322 srcs: ["a.java"],
323 vendor: true,
324 }
325
326 java_library {
327 name: "bar",
328 srcs: ["b.java"],
329 }
330 `)
331
332 foo := ctx.ModuleForTests("foo", "android_common").Module().(*Library)
333 bar := ctx.ModuleForTests("bar", "android_common").Module().(*Library)
334
335 if foo.sdkVersion() != "system_current" {
336 t.Errorf("If sdk version of vendor module is empty, it must change to system_current.")
337 }
338
339 if bar.sdkVersion() != "" {
340 t.Errorf("If sdk version of non-vendor module is empty, it keeps empty.")
341 }
342}
343
Colin Crossd5934c82017-10-02 13:55:26 -0700344func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700345 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700346 java_library {
347 name: "foo",
348 srcs: ["a.java"],
349 target: {
350 android: {
351 srcs: ["b.java"],
352 },
353 },
354 }
355 `)
356
357 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
358 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
359 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
360 }
361}
362
Colin Cross6b4a32d2017-12-05 13:42:45 -0800363func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700364 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800365 java_library_host {
366 name: "foo",
367 srcs: ["a.java"],
368 }
369
370 java_binary_host {
371 name: "bar",
372 srcs: ["b.java"],
373 static_libs: ["foo"],
374 }
375 `)
376
377 buildOS := android.BuildOs.String()
378
379 bar := ctx.ModuleForTests("bar", buildOS+"_common")
380 barJar := bar.Output("bar.jar").Output.String()
381 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
382 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
383
384 // Test that the install binary wrapper depends on the installed jar file
385 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
386 t.Errorf("expected binary wrapper implicits [%q], got %v",
387 barJar, barWrapperDeps)
388 }
389
390}
391
Colin Cross72bb3632017-07-13 16:23:21 -0700392func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700393 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700394 java_library {
395 name: "foo",
396 srcs: ["a.java"],
Colin Cross79c7c262019-04-17 11:11:46 -0700397 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700398 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700399 }
400
Colin Cross74d73e22017-08-02 11:05:49 -0700401 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700402 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700403 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700404 }
405
Colin Cross74d73e22017-08-02 11:05:49 -0700406 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700407 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700408 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700409 }
Colin Cross42be7612019-02-21 18:12:14 -0800410
411 dex_import {
412 name: "qux",
413 jars: ["b.jar"],
414 }
Colin Cross79c7c262019-04-17 11:11:46 -0700415
416 java_sdk_library_import {
417 name: "sdklib",
418 jars: ["b.jar"],
419 }
Colin Cross72bb3632017-07-13 16:23:21 -0700420 `)
421
Colin Cross4c428df2017-09-15 17:36:05 -0700422 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700423 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700424 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
425 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700426 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700427
Colin Cross37f6d792018-07-12 12:28:41 -0700428 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
429 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700430 }
431
Colin Cross79c7c262019-04-17 11:11:46 -0700432 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
433 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
434 }
435
Colin Cross37f6d792018-07-12 12:28:41 -0700436 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
437 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700438 }
Colin Cross42be7612019-02-21 18:12:14 -0800439
440 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700441}
442
Colin Cross89536d42017-07-07 14:35:50 -0700443func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700444 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700445 java_defaults {
446 name: "defaults",
447 srcs: ["a.java"],
448 libs: ["bar"],
449 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700450 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700451 }
452
453 java_library {
454 name: "foo",
455 defaults: ["defaults"],
456 }
457
458 java_library {
459 name: "bar",
460 srcs: ["b.java"],
461 }
462
463 java_library {
464 name: "baz",
465 srcs: ["c.java"],
466 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700467
468 android_test {
469 name: "atestOptimize",
470 defaults: ["defaults"],
471 optimize: {enabled: true},
472 }
473
474 android_test {
475 name: "atestNoOptimize",
476 defaults: ["defaults"],
477 }
478
479 android_test {
480 name: "atestDefault",
481 srcs: ["a.java"],
482 }
Colin Cross89536d42017-07-07 14:35:50 -0700483 `)
484
Colin Cross4c428df2017-09-15 17:36:05 -0700485 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700486 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700487
488 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
489 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
490 }
491
Nan Zhanged19fc32017-10-19 13:06:22 -0700492 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
493 if !strings.Contains(javac.Args["classpath"], barTurbine) {
494 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700495 }
496
Colin Cross1ee23172017-10-18 14:44:18 -0700497 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700498 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
499 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700500 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700501
502 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
503 if atestOptimize.Output == nil {
504 t.Errorf("atestOptimize should optimize APK")
505 }
506
507 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
508 if atestNoOptimize.Output == nil {
509 t.Errorf("atestNoOptimize should not optimize APK")
510 }
511
512 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
513 if atestDefault.Output == nil {
514 t.Errorf("atestDefault should optimize APK")
515 }
Colin Cross89536d42017-07-07 14:35:50 -0700516}
517
Colin Cross0f37af02017-09-27 17:42:05 -0700518func TestResources(t *testing.T) {
519 var table = []struct {
520 name string
521 prop string
522 extra string
523 args string
524 }{
525 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700526 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700527 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800528 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700529 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700530 },
531 {
532 // Test that a module with java_resources includes the files
533 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700534 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
535 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700536 },
537 {
538 // Test that a module with a filegroup in java_resources includes the files with the
539 // path prefix
540 name: "resource filegroup",
541 prop: `java_resources: [":foo-res"]`,
542 extra: `
543 filegroup {
544 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800545 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700546 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700547 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700548 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700549 },
550 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700551 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
552 name: "wildcard dirs",
553 prop: `java_resource_dirs: ["java-res/*"]`,
554 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
555 },
556 {
557 // Test that a module exclude_java_resource_dirs excludes the files
558 name: "wildcard dirs",
559 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
560 args: "-C java-res/a -f java-res/a/a",
561 },
Colin Crosscedd4762018-09-13 11:26:19 -0700562 {
563 // Test wildcards in java_resources
564 name: "wildcard files",
565 prop: `java_resources: ["java-res/**/*"]`,
566 args: "-C . -f java-res/a/a -f java-res/b/b",
567 },
568 {
569 // Test exclude_java_resources with java_resources
570 name: "wildcard files with exclude",
571 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
572 args: "-C . -f java-res/a/a",
573 },
574 {
575 // Test exclude_java_resources with java_resource_dirs
576 name: "resource dirs with exclude files",
577 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
578 args: "-C java-res -f java-res/a/a",
579 },
580 {
581 // Test exclude_java_resource_dirs with java_resource_dirs
582 name: "resource dirs with exclude files",
583 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
584 args: "-C java-res -f java-res/a/a -f java-res/b/b",
585 },
Colin Cross0f37af02017-09-27 17:42:05 -0700586 }
587
588 for _, test := range table {
589 t.Run(test.name, func(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700590 ctx, _ := testJava(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700591 java_library {
592 name: "foo",
593 srcs: [
594 "a.java",
595 "b.java",
596 "c.java",
597 ],
598 `+test.prop+`,
599 }
600 `+test.extra)
601
Colin Cross331a1212018-08-15 20:40:52 -0700602 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700603 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700604
605 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
606 t.Errorf("foo combined jars %v does not contain %q",
607 foo.Inputs.Strings(), fooRes.Output.String())
608 }
609
Colin Crossaf9c55b2017-10-03 14:50:08 -0700610 if fooRes.Args["jarArgs"] != test.args {
611 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700612 fooRes.Args["jarArgs"], test.args)
613 }
614 })
615 }
616}
617
Colin Cross0c4ce212019-05-03 15:28:19 -0700618func TestIncludeSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700619 ctx, _ := testJava(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700620 java_library {
621 name: "foo",
622 srcs: [
623 "a.java",
624 "b.java",
625 "c.java",
626 ],
627 include_srcs: true,
628 }
629
630 java_library {
631 name: "bar",
632 srcs: [
633 "a.java",
634 "b.java",
635 "c.java",
636 ],
637 java_resource_dirs: ["java-res"],
638 include_srcs: true,
639 }
640 `)
641
642 // Test a library with include_srcs: true
643 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
644 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
645
646 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
647 t.Errorf("foo combined jars %v does not contain %q", w, g)
648 }
649
650 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
651 t.Errorf("foo source jar args %q is not %q", w, g)
652 }
653
654 // Test a library with include_srcs: true and resources
655 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
656 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
657 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
658 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
659
660 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
661 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
662 }
663
664 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
665 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
666 }
667
668 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
669 t.Errorf("bar combined jars %v does not contain %q", w, g)
670 }
671
672 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
673 t.Errorf("bar source jar args %q is not %q", w, g)
674 }
675
676 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
677 t.Errorf("bar resource jar args %q is not %q", w, g)
678 }
679}
680
Colin Cross54190b32017-10-09 15:34:10 -0700681func TestGeneratedSources(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700682 ctx, _ := testJava(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700683 java_library {
684 name: "foo",
685 srcs: [
686 "a*.java",
687 ":gen",
688 "b*.java",
689 ],
690 }
691
692 genrule {
693 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800694 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700695 out: ["gen.java"],
696 }
697 `)
698
699 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
700 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
701
Colin Cross15e86d92017-10-20 15:07:08 -0700702 if filepath.Base(genrule.Output.String()) != "gen.java" {
703 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700704 }
705
706 if len(javac.Inputs) != 3 ||
707 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700708 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700709 javac.Inputs[2].String() != "b.java" {
710 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
711 }
712}
713
Nan Zhang61eaedb2017-11-02 13:28:15 -0700714func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700715 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700716 java_library {
717 name: "foo",
718 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900719 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700720 }
721
722 java_library {
723 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800724 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700725 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900726 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700727 }
728
729 java_library {
730 name: "baz",
731 srcs: ["c.java"],
732 libs: ["bar"],
733 sdk_version: "14",
734 }
735 `)
736
737 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
738 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
739 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
740 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
741 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
742
743 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
744 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
745 }
746
747 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
748 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
749 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
750 }
751 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
752 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
753 }
754 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
755 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
756 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100757 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700758 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100759 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700760 }
761}
762
763func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700764 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700765 java_library {
766 name: "bar",
767 srcs: ["a.java","b.java","c.java"],
768 javac_shard_size: 1
769 }
770 `)
771
772 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
773 for i := 0; i < 3; i++ {
774 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
775 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
776 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
777 }
778 }
779}
780
Nan Zhang581fd212018-01-10 16:06:12 -0800781func TestDroiddoc(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700782 ctx, _ := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800783 droiddoc_template {
784 name: "droiddoc-templates-sdk",
785 path: ".",
786 }
Jiyong Park29074592019-07-07 16:27:47 +0900787 filegroup {
788 name: "bar-doc-aidl-srcs",
789 srcs: ["bar-doc/IBar.aidl"],
790 path: "bar-doc",
791 }
Nan Zhang581fd212018-01-10 16:06:12 -0800792 droiddoc {
793 name: "bar-doc",
794 srcs: [
795 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900796 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +0900797 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -0800798 ],
799 exclude_srcs: [
800 "bar-doc/b.java"
801 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800802 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800803 hdf: [
804 "android.whichdoc offline",
805 ],
806 knowntags: [
807 "bar-doc/known_oj_tags.txt",
808 ],
809 proofread_file: "libcore-proofread.txt",
810 todo_file: "libcore-docs-todo.html",
811 args: "-offlinemode -title \"libcore\"",
812 }
813 `)
814
Colin Crossc0806172019-06-14 18:51:47 -0700815 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +0900816 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -0700817 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +0900818 javaSrcs = append(javaSrcs, i.Base())
819 }
Colin Crossc0806172019-06-14 18:51:47 -0700820 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
821 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +0900822 }
823
Colin Crossc0806172019-06-14 18:51:47 -0700824 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
825 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
826 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
827 }
828
829 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
830 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +0900831 }
Nan Zhang581fd212018-01-10 16:06:12 -0800832}
833
Colin Cross54250902017-12-05 09:28:08 -0800834func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700835 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -0800836 java_library {
837 name: "foo",
838 srcs: ["a.java"],
839 }
840
841 java_genrule {
842 name: "jargen",
843 tool_files: ["b.java"],
844 cmd: "$(location b.java) $(in) $(out)",
845 out: ["jargen.jar"],
846 srcs: [":foo"],
847 }
848
849 java_library {
850 name: "bar",
851 static_libs: ["jargen"],
852 srcs: ["c.java"],
853 }
854
855 java_library {
856 name: "baz",
857 libs: ["jargen"],
858 srcs: ["c.java"],
859 }
860 `)
861
862 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
863 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
864 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
865 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
866 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
867
868 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
869 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
870 }
871
872 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
873 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
874 }
875
876 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
877 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
878 }
879
880 if len(barCombined.Inputs) != 2 ||
881 barCombined.Inputs[0].String() != bar.Output.String() ||
882 barCombined.Inputs[1].String() != jargen.Output.String() {
883 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
884 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
885 }
886}
887
Nan Zhang27e284d2018-02-09 21:03:53 +0000888func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700889 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +0000890 java_library {
891 name: "foo",
892 srcs: ["a.java", ":foo-srcs"],
893 exclude_srcs: ["a.java", ":foo-excludes"],
894 }
895
896 filegroup {
897 name: "foo-srcs",
898 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
899 }
900
901 filegroup {
902 name: "foo-excludes",
903 srcs: ["java-fg/a.java", "java-fg/b.java"],
904 }
905 `)
906
907 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
908
909 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
910 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
911 }
912}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900913
Paul Duffin52d398a2019-06-11 12:31:14 +0100914func TestJavaLibrary(t *testing.T) {
915 config := testConfig(nil)
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700916 ctx := testContext("", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +0100917 "libcore/Android.bp": []byte(`
918 java_library {
919 name: "core",
920 sdk_version: "none",
921 system_modules: "none",
922 }`),
923 })
924 run(t, ctx, config)
925}
926
Jiyong Parkc678ad32018-04-10 13:07:10 +0900927func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700928 ctx, _ := testJava(t, `
Jiyong Parkc678ad32018-04-10 13:07:10 +0900929 droiddoc_template {
930 name: "droiddoc-templates-sdk",
931 path: ".",
932 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900933 java_sdk_library {
934 name: "foo",
935 srcs: ["a.java", "b.java"],
936 api_packages: ["foo"],
937 }
938 java_sdk_library {
939 name: "bar",
940 srcs: ["a.java", "b.java"],
941 api_packages: ["bar"],
942 }
943 java_library {
944 name: "baz",
945 srcs: ["c.java"],
946 libs: ["foo", "bar"],
947 sdk_version: "system_current",
948 }
Jiyong Park1be96912018-05-28 18:02:19 +0900949 java_library {
950 name: "qux",
951 srcs: ["c.java"],
952 libs: ["baz"],
953 sdk_version: "system_current",
954 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900955 `)
956
957 // check the existence of the internal modules
958 ctx.ModuleForTests("foo", "android_common")
959 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
960 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900961 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900962 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
963 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900964 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800965 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +0900966 ctx.ModuleForTests("foo.api.public.28", "")
967 ctx.ModuleForTests("foo.api.system.28", "")
968 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900969
970 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
971 // tests if baz is actually linked to the stubs lib
972 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
973 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
974 "foo.stubs.system.jar")
975 }
976 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +0900977 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900978 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +0900979 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900980 }
981 // test if baz is not linked to the system variant of foo
982 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
983 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
984 "foo.stubs.jar")
985 }
Jiyong Park1be96912018-05-28 18:02:19 +0900986
987 // test if baz has exported SDK lib names foo and bar to qux
988 qux := ctx.ModuleForTests("qux", "android_common")
989 if quxLib, ok := qux.Module().(*Library); ok {
990 sdkLibs := quxLib.ExportedSdkLibs()
991 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
992 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
993 }
994 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900995}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200996
997var compilerFlagsTestCases = []struct {
998 in string
999 out bool
1000}{
1001 {
1002 in: "a",
1003 out: false,
1004 },
1005 {
1006 in: "-a",
1007 out: true,
1008 },
1009 {
1010 in: "-no-jdk",
1011 out: false,
1012 },
1013 {
1014 in: "-no-stdlib",
1015 out: false,
1016 },
1017 {
1018 in: "-kotlin-home",
1019 out: false,
1020 },
1021 {
1022 in: "-kotlin-home /some/path",
1023 out: false,
1024 },
1025 {
1026 in: "-include-runtime",
1027 out: false,
1028 },
1029 {
1030 in: "-Xintellij-plugin-root",
1031 out: false,
1032 },
1033}
1034
1035type mockContext struct {
1036 android.ModuleContext
1037 result bool
1038}
1039
1040func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1041 // CheckBadCompilerFlags calls this function when the flag should be rejected
1042 ctx.result = false
1043}
1044
1045func TestCompilerFlags(t *testing.T) {
1046 for _, testCase := range compilerFlagsTestCases {
1047 ctx := &mockContext{result: true}
1048 CheckKotlincFlags(ctx, []string{testCase.in})
1049 if ctx.result != testCase.out {
1050 t.Errorf("incorrect output:")
1051 t.Errorf(" input: %#v", testCase.in)
1052 t.Errorf(" expected: %#v", testCase.out)
1053 t.Errorf(" got: %#v", ctx.result)
1054 }
1055 }
1056}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001057
1058// TODO(jungjw): Consider making this more robust by ignoring path order.
1059func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1060 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1061 flags := strings.Split(variables["javacFlags"], " ")
1062 got := ""
1063 for _, flag := range flags {
1064 keyEnd := strings.Index(flag, "=")
1065 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1066 got = flag[keyEnd+1:]
1067 break
1068 }
1069 }
1070 if expected != got {
1071 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1072 }
1073}
1074
1075func TestPatchModule(t *testing.T) {
1076 bp := `
1077 java_library {
1078 name: "foo",
1079 srcs: ["a.java"],
1080 }
1081
1082 java_library {
1083 name: "bar",
1084 srcs: ["b.java"],
Paul Duffina3d09862019-06-11 13:40:47 +01001085 sdk_version: "none",
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001086 system_modules: "none",
1087 patch_module: "java.base",
1088 }
1089
1090 java_library {
1091 name: "baz",
1092 srcs: ["c.java"],
1093 patch_module: "java.base",
1094 }
1095 `
1096
Pete Gillin0c2143e2019-05-02 15:32:11 +01001097 t.Run("Java language level 8", func(t *testing.T) {
1098 // Test default javac -source 1.8 -target 1.8
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001099 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001100
1101 checkPatchModuleFlag(t, ctx, "foo", "")
1102 checkPatchModuleFlag(t, ctx, "bar", "")
1103 checkPatchModuleFlag(t, ctx, "baz", "")
1104 })
1105
Pete Gillin0c2143e2019-05-02 15:32:11 +01001106 t.Run("Java language level 9", func(t *testing.T) {
1107 // Test again with javac -source 9 -target 9
1108 config := testConfig(map[string]string{"EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9": "true"})
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001109 ctx := testContext(bp, nil)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001110 run(t, ctx, config)
1111
1112 checkPatchModuleFlag(t, ctx, "foo", "")
1113 expected := "java.base=.:" + buildDir
1114 checkPatchModuleFlag(t, ctx, "bar", expected)
Insun Kang23433862019-01-24 20:46:28 +09001115 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework"), moduleToPath("updatable_media_stubs")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001116 checkPatchModuleFlag(t, ctx, "baz", expected)
1117 })
1118}