blob: 9438ce297bdd3b689b18249facd34eb5a3b66eaa [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"
Nan Zhang61eaedb2017-11-02 13:28:15 -070021 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070022 "strings"
23 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070024
Colin Crossa4f08812018-10-02 22:03:40 -070025 "android/soong/android"
26 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080027 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070028 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070029)
30
31var buildDir string
32
33func setUp() {
34 var err error
35 buildDir, err = ioutil.TempDir("", "soong_java_test")
36 if err != nil {
37 panic(err)
38 }
39}
40
41func tearDown() {
42 os.RemoveAll(buildDir)
43}
44
45func TestMain(m *testing.M) {
46 run := func() int {
47 setUp()
48 defer tearDown()
49
50 return m.Run()
51 }
52
53 os.Exit(run())
54}
Colin Cross527012a2017-11-30 22:56:16 -080055
56func testConfig(env map[string]string) android.Config {
Colin Crosse4759b92019-02-15 10:37:39 -080057 return TestConfig(buildDir, env)
Colin Cross1369cdb2017-09-29 17:58:17 -070058}
59
Colin Cross527012a2017-11-30 22:56:16 -080060func testContext(config android.Config, bp string,
61 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))
Colin Crossa97c5d32018-03-28 14:58:31 -070066 ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070067 ctx.RegisterModuleType("android_test", android.ModuleFactoryAdaptor(AndroidTestFactory))
Colin Cross638149e2019-01-05 22:12:12 -080068 ctx.RegisterModuleType("android_test_helper_app", android.ModuleFactoryAdaptor(AndroidTestHelperAppFactory))
69 ctx.RegisterModuleType("java_binary", android.ModuleFactoryAdaptor(BinaryFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080070 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Cross3d7c9822019-03-01 13:46:24 -080071 ctx.RegisterModuleType("java_device_for_host", android.ModuleFactoryAdaptor(DeviceForHostFactory))
72 ctx.RegisterModuleType("java_host_for_device", android.ModuleFactoryAdaptor(HostForDeviceFactory))
Colin Cross9ae1b922018-06-26 17:59:05 -070073 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross2acdae82017-09-15 19:44:24 -070074 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross638149e2019-01-05 22:12:12 -080075 ctx.RegisterModuleType("java_test", android.ModuleFactoryAdaptor(TestFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070076 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross3d7c9822019-03-01 13:46:24 -080077 ctx.RegisterModuleType("java_import_host", android.ModuleFactoryAdaptor(ImportFactoryHost))
Colin Cross89536d42017-07-07 14:35:50 -070078 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070079 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080080 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Colin Crossbe9cdb82019-01-21 21:37:16 -080081 ctx.RegisterModuleType("java_plugin", android.ModuleFactoryAdaptor(PluginFactory))
Colin Cross42be7612019-02-21 18:12:14 -080082 ctx.RegisterModuleType("dex_import", android.ModuleFactoryAdaptor(DexImportFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070083 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070084 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080085 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
86 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Nan Zhangf4936b02018-08-01 15:00:28 -070087 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
Inseob Kimc0907f12019-02-08 21:00:45 +090088 ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(SdkLibraryFactory))
Colin Cross79c7c262019-04-17 11:11:46 -070089 ctx.RegisterModuleType("java_sdk_library_import", android.ModuleFactoryAdaptor(sdkLibraryImportFactory))
Jaewoong Jung525443a2019-02-28 15:35:54 -080090 ctx.RegisterModuleType("override_android_app", android.ModuleFactoryAdaptor(OverrideAndroidAppModuleFactory))
Inseob Kimc0907f12019-02-08 21:00:45 +090091 ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(PrebuiltApisFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070092 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
93 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070094 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jaewoong Jung525443a2019-02-28 15:35:54 -080095 ctx.PreArchMutators(android.RegisterOverridePreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090096 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Inseob Kimc0907f12019-02-08 21:00:45 +090097 ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +090098 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -080099 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross98fd5742019-01-09 23:04:25 -0800100 ctx.RegisterPreSingletonType("sdk", android.SingletonFactoryAdaptor(sdkSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700101
102 // Register module types and mutators from cc needed for JNI testing
103 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
104 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
105 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Colin Cross47fa9d32019-03-26 10:51:39 -0700106 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700107 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
108 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
109 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
110 })
111
Colin Crosse4759b92019-02-15 10:37:39 -0800112 bp += GatherRequiredDepsForTest()
Colin Cross1369cdb2017-09-29 17:58:17 -0700113
Colin Cross824bee32017-11-22 17:27:51 -0800114 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900115 "Android.bp": []byte(bp),
116 "a.java": nil,
117 "b.java": nil,
118 "c.java": nil,
119 "b.kt": nil,
120 "a.jar": nil,
121 "b.jar": nil,
122 "java-res/a/a": nil,
123 "java-res/b/b": nil,
124 "java-res2/a": nil,
125 "java-fg/a.java": nil,
126 "java-fg/b.java": nil,
127 "java-fg/c.java": nil,
128 "api/current.txt": nil,
129 "api/removed.txt": nil,
130 "api/system-current.txt": nil,
131 "api/system-removed.txt": nil,
132 "api/test-current.txt": nil,
133 "api/test-removed.txt": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700134
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100135 "prebuilts/sdk/14/public/android.jar": nil,
136 "prebuilts/sdk/14/public/framework.aidl": nil,
137 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross98fd5742019-01-09 23:04:25 -0800138 "prebuilts/sdk/17/public/android.jar": nil,
139 "prebuilts/sdk/17/public/framework.aidl": nil,
140 "prebuilts/sdk/17/system/android.jar": nil,
Colin Crossff0daf42019-04-02 16:10:56 -0700141 "prebuilts/sdk/25/public/android.jar": nil,
142 "prebuilts/sdk/25/public/framework.aidl": nil,
143 "prebuilts/sdk/25/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700144 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100145 "prebuilts/sdk/current/public/android.jar": nil,
146 "prebuilts/sdk/current/public/framework.aidl": nil,
147 "prebuilts/sdk/current/public/core.jar": nil,
148 "prebuilts/sdk/current/system/android.jar": nil,
149 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900150 "prebuilts/sdk/28/public/api/foo.txt": nil,
151 "prebuilts/sdk/28/system/api/foo.txt": nil,
152 "prebuilts/sdk/28/test/api/foo.txt": nil,
153 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
154 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
155 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
156 "prebuilts/sdk/28/public/api/bar.txt": nil,
157 "prebuilts/sdk/28/system/api/bar.txt": nil,
158 "prebuilts/sdk/28/test/api/bar.txt": nil,
159 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
160 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
161 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700162 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900163 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800164
165 // For framework-res, which is an implicit dependency for framework
Dan Willemsen412160e2019-04-09 21:36:26 -0700166 "AndroidManifest.xml": nil,
167 "build/make/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800168
169 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800170
Colin Cross66dbc0b2017-12-28 12:23:20 -0800171 "build/make/core/proguard.flags": nil,
172 "build/make/core/proguard_basic_keeps.flags": nil,
173
Colin Cross7fdd2b72018-01-02 18:14:25 -0800174 "jdk8/jre/lib/jce.jar": nil,
175 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700176 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800177
178 "bar-doc/a.java": nil,
179 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900180 "bar-doc/IFoo.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800181 "bar-doc/known_oj_tags.txt": nil,
182 "external/doclava/templates-sdk": nil,
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800183
184 "cert/new_cert.x509.pem": nil,
185 "cert/new_cert.pk8": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800186 }
187
188 for k, v := range fs {
189 mockFS[k] = v
190 }
191
192 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700193
Colin Cross527012a2017-11-30 22:56:16 -0800194 return ctx
195}
196
197func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800198 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800199
200 pathCtx := android.PathContextForTesting(config, nil)
201 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
202
203 ctx.Register()
Jiyong Park58c518b2018-05-12 22:29:12 +0900204 _, errs := ctx.ParseFileList(".", []string{"Android.bp", "prebuilts/sdk/Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800205 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700206 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800207 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800208}
209
210func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800211 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800212 config := testConfig(nil)
213 ctx := testContext(config, bp, nil)
214 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700215
216 return ctx
217}
218
Colin Cross2acdae82017-09-15 19:44:24 -0700219func moduleToPath(name string) string {
220 switch {
221 case name == `""`:
222 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700223 case strings.HasSuffix(name, ".jar"):
224 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700225 default:
226 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700227 }
228}
229
Colin Cross72bb3632017-07-13 16:23:21 -0700230func TestSimple(t *testing.T) {
231 ctx := testJava(t, `
232 java_library {
233 name: "foo",
234 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700235 libs: ["bar"],
236 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700237 }
238
239 java_library {
240 name: "bar",
241 srcs: ["b.java"],
242 }
243
244 java_library {
245 name: "baz",
246 srcs: ["c.java"],
247 }
Colin Crossd5934c82017-10-02 13:55:26 -0700248 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700249
Colin Cross4c428df2017-09-15 17:36:05 -0700250 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700251 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700252
253 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
254 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
255 }
256
Colin Cross1ee23172017-10-18 14:44:18 -0700257 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700258 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
259 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700260
Nan Zhanged19fc32017-10-19 13:06:22 -0700261 if !strings.Contains(javac.Args["classpath"], barTurbine) {
262 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700263 }
264
Nan Zhanged19fc32017-10-19 13:06:22 -0700265 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
266 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700267 }
268
269 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
270 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700271 }
272}
273
Colin Crossd5934c82017-10-02 13:55:26 -0700274func TestArchSpecific(t *testing.T) {
275 ctx := testJava(t, `
276 java_library {
277 name: "foo",
278 srcs: ["a.java"],
279 target: {
280 android: {
281 srcs: ["b.java"],
282 },
283 },
284 }
285 `)
286
287 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
288 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
289 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
290 }
291}
292
Colin Cross6b4a32d2017-12-05 13:42:45 -0800293func TestBinary(t *testing.T) {
294 ctx := testJava(t, `
295 java_library_host {
296 name: "foo",
297 srcs: ["a.java"],
298 }
299
300 java_binary_host {
301 name: "bar",
302 srcs: ["b.java"],
303 static_libs: ["foo"],
304 }
305 `)
306
307 buildOS := android.BuildOs.String()
308
309 bar := ctx.ModuleForTests("bar", buildOS+"_common")
310 barJar := bar.Output("bar.jar").Output.String()
311 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
312 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
313
314 // Test that the install binary wrapper depends on the installed jar file
315 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
316 t.Errorf("expected binary wrapper implicits [%q], got %v",
317 barJar, barWrapperDeps)
318 }
319
320}
321
Colin Cross72bb3632017-07-13 16:23:21 -0700322func TestPrebuilts(t *testing.T) {
323 ctx := testJava(t, `
324 java_library {
325 name: "foo",
326 srcs: ["a.java"],
Colin Cross79c7c262019-04-17 11:11:46 -0700327 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700328 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700329 }
330
Colin Cross74d73e22017-08-02 11:05:49 -0700331 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700332 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700333 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700334 }
335
Colin Cross74d73e22017-08-02 11:05:49 -0700336 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700337 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700338 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700339 }
Colin Cross42be7612019-02-21 18:12:14 -0800340
341 dex_import {
342 name: "qux",
343 jars: ["b.jar"],
344 }
Colin Cross79c7c262019-04-17 11:11:46 -0700345
346 java_sdk_library_import {
347 name: "sdklib",
348 jars: ["b.jar"],
349 }
Colin Cross72bb3632017-07-13 16:23:21 -0700350 `)
351
Colin Cross4c428df2017-09-15 17:36:05 -0700352 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700353 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700354 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
355 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700356 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700357
Colin Cross37f6d792018-07-12 12:28:41 -0700358 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
359 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700360 }
361
Colin Cross79c7c262019-04-17 11:11:46 -0700362 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
363 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
364 }
365
Colin Cross37f6d792018-07-12 12:28:41 -0700366 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
367 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700368 }
Colin Cross42be7612019-02-21 18:12:14 -0800369
370 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700371}
372
Colin Cross89536d42017-07-07 14:35:50 -0700373func TestDefaults(t *testing.T) {
374 ctx := testJava(t, `
375 java_defaults {
376 name: "defaults",
377 srcs: ["a.java"],
378 libs: ["bar"],
379 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700380 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700381 }
382
383 java_library {
384 name: "foo",
385 defaults: ["defaults"],
386 }
387
388 java_library {
389 name: "bar",
390 srcs: ["b.java"],
391 }
392
393 java_library {
394 name: "baz",
395 srcs: ["c.java"],
396 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700397
398 android_test {
399 name: "atestOptimize",
400 defaults: ["defaults"],
401 optimize: {enabled: true},
402 }
403
404 android_test {
405 name: "atestNoOptimize",
406 defaults: ["defaults"],
407 }
408
409 android_test {
410 name: "atestDefault",
411 srcs: ["a.java"],
412 }
Colin Cross89536d42017-07-07 14:35:50 -0700413 `)
414
Colin Cross4c428df2017-09-15 17:36:05 -0700415 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700416 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700417
418 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
419 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
420 }
421
Nan Zhanged19fc32017-10-19 13:06:22 -0700422 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
423 if !strings.Contains(javac.Args["classpath"], barTurbine) {
424 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700425 }
426
Colin Cross1ee23172017-10-18 14:44:18 -0700427 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700428 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
429 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700430 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700431
432 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
433 if atestOptimize.Output == nil {
434 t.Errorf("atestOptimize should optimize APK")
435 }
436
437 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
438 if atestNoOptimize.Output == nil {
439 t.Errorf("atestNoOptimize should not optimize APK")
440 }
441
442 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
443 if atestDefault.Output == nil {
444 t.Errorf("atestDefault should optimize APK")
445 }
Colin Cross89536d42017-07-07 14:35:50 -0700446}
447
Colin Cross0f37af02017-09-27 17:42:05 -0700448func TestResources(t *testing.T) {
449 var table = []struct {
450 name string
451 prop string
452 extra string
453 args string
454 }{
455 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700456 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700457 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800458 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700459 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700460 },
461 {
462 // Test that a module with java_resources includes the files
463 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700464 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
465 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700466 },
467 {
468 // Test that a module with a filegroup in java_resources includes the files with the
469 // path prefix
470 name: "resource filegroup",
471 prop: `java_resources: [":foo-res"]`,
472 extra: `
473 filegroup {
474 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800475 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700476 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700477 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700478 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700479 },
480 {
481 // Test that a module with "include_srcs: true" includes its source files in the resources jar
482 name: "include sources",
483 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700484 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700485 },
Colin Cross0ead1d72018-04-10 13:07:42 -0700486 {
487 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
488 name: "wildcard dirs",
489 prop: `java_resource_dirs: ["java-res/*"]`,
490 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
491 },
492 {
493 // Test that a module exclude_java_resource_dirs excludes the files
494 name: "wildcard dirs",
495 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
496 args: "-C java-res/a -f java-res/a/a",
497 },
Colin Crosscedd4762018-09-13 11:26:19 -0700498 {
499 // Test wildcards in java_resources
500 name: "wildcard files",
501 prop: `java_resources: ["java-res/**/*"]`,
502 args: "-C . -f java-res/a/a -f java-res/b/b",
503 },
504 {
505 // Test exclude_java_resources with java_resources
506 name: "wildcard files with exclude",
507 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
508 args: "-C . -f java-res/a/a",
509 },
510 {
511 // Test exclude_java_resources with java_resource_dirs
512 name: "resource dirs with exclude files",
513 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
514 args: "-C java-res -f java-res/a/a",
515 },
516 {
517 // Test exclude_java_resource_dirs with java_resource_dirs
518 name: "resource dirs with exclude files",
519 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
520 args: "-C java-res -f java-res/a/a -f java-res/b/b",
521 },
Colin Cross0f37af02017-09-27 17:42:05 -0700522 }
523
524 for _, test := range table {
525 t.Run(test.name, func(t *testing.T) {
526 ctx := testJava(t, `
527 java_library {
528 name: "foo",
529 srcs: [
530 "a.java",
531 "b.java",
532 "c.java",
533 ],
534 `+test.prop+`,
535 }
536 `+test.extra)
537
Colin Cross331a1212018-08-15 20:40:52 -0700538 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700539 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700540
541 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
542 t.Errorf("foo combined jars %v does not contain %q",
543 foo.Inputs.Strings(), fooRes.Output.String())
544 }
545
Colin Crossaf9c55b2017-10-03 14:50:08 -0700546 if fooRes.Args["jarArgs"] != test.args {
547 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700548 fooRes.Args["jarArgs"], test.args)
549 }
550 })
551 }
552}
553
Colin Cross54190b32017-10-09 15:34:10 -0700554func TestGeneratedSources(t *testing.T) {
555 ctx := testJava(t, `
556 java_library {
557 name: "foo",
558 srcs: [
559 "a*.java",
560 ":gen",
561 "b*.java",
562 ],
563 }
564
565 genrule {
566 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800567 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700568 out: ["gen.java"],
569 }
570 `)
571
572 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
573 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
574
Colin Cross15e86d92017-10-20 15:07:08 -0700575 if filepath.Base(genrule.Output.String()) != "gen.java" {
576 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700577 }
578
579 if len(javac.Inputs) != 3 ||
580 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700581 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700582 javac.Inputs[2].String() != "b.java" {
583 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
584 }
585}
586
Nan Zhang61eaedb2017-11-02 13:28:15 -0700587func TestTurbine(t *testing.T) {
588 ctx := testJava(t, `
589 java_library {
590 name: "foo",
591 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900592 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700593 }
594
595 java_library {
596 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800597 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700598 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900599 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700600 }
601
602 java_library {
603 name: "baz",
604 srcs: ["c.java"],
605 libs: ["bar"],
606 sdk_version: "14",
607 }
608 `)
609
610 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
611 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
612 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
613 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
614 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
615
616 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
617 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
618 }
619
620 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
621 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
622 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
623 }
624 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
625 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
626 }
627 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
628 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
629 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100630 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700631 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100632 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700633 }
634}
635
636func TestSharding(t *testing.T) {
637 ctx := testJava(t, `
638 java_library {
639 name: "bar",
640 srcs: ["a.java","b.java","c.java"],
641 javac_shard_size: 1
642 }
643 `)
644
645 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
646 for i := 0; i < 3; i++ {
647 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
648 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
649 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
650 }
651 }
652}
653
Nan Zhang581fd212018-01-10 16:06:12 -0800654func TestDroiddoc(t *testing.T) {
655 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800656 droiddoc_template {
657 name: "droiddoc-templates-sdk",
658 path: ".",
659 }
Nan Zhang581fd212018-01-10 16:06:12 -0800660 droiddoc {
661 name: "bar-doc",
662 srcs: [
663 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900664 "bar-doc/IFoo.aidl",
Nan Zhang581fd212018-01-10 16:06:12 -0800665 ],
666 exclude_srcs: [
667 "bar-doc/b.java"
668 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800669 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800670 hdf: [
671 "android.whichdoc offline",
672 ],
673 knowntags: [
674 "bar-doc/known_oj_tags.txt",
675 ],
676 proofread_file: "libcore-proofread.txt",
677 todo_file: "libcore-docs-todo.html",
678 args: "-offlinemode -title \"libcore\"",
679 }
680 `)
681
Nan Zhang3bb14202018-02-28 10:14:25 -0800682 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
683 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800684 if stubsJar != barDoc.Output.String() {
685 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
686 }
Jiyong Park1e440682018-05-23 18:42:04 +0900687 inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
688 var javaSrcs []string
689 for _, i := range inputs {
690 javaSrcs = append(javaSrcs, i.Base())
691 }
692 if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
693 t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
694 }
Nan Zhang581fd212018-01-10 16:06:12 -0800695}
696
Colin Cross54250902017-12-05 09:28:08 -0800697func TestJarGenrules(t *testing.T) {
698 ctx := testJava(t, `
699 java_library {
700 name: "foo",
701 srcs: ["a.java"],
702 }
703
704 java_genrule {
705 name: "jargen",
706 tool_files: ["b.java"],
707 cmd: "$(location b.java) $(in) $(out)",
708 out: ["jargen.jar"],
709 srcs: [":foo"],
710 }
711
712 java_library {
713 name: "bar",
714 static_libs: ["jargen"],
715 srcs: ["c.java"],
716 }
717
718 java_library {
719 name: "baz",
720 libs: ["jargen"],
721 srcs: ["c.java"],
722 }
723 `)
724
725 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
726 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
727 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
728 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
729 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
730
731 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
732 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
733 }
734
735 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
736 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
737 }
738
739 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
740 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
741 }
742
743 if len(barCombined.Inputs) != 2 ||
744 barCombined.Inputs[0].String() != bar.Output.String() ||
745 barCombined.Inputs[1].String() != jargen.Output.String() {
746 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
747 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
748 }
749}
750
Nan Zhang27e284d2018-02-09 21:03:53 +0000751func TestExcludeFileGroupInSrcs(t *testing.T) {
752 ctx := testJava(t, `
753 java_library {
754 name: "foo",
755 srcs: ["a.java", ":foo-srcs"],
756 exclude_srcs: ["a.java", ":foo-excludes"],
757 }
758
759 filegroup {
760 name: "foo-srcs",
761 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
762 }
763
764 filegroup {
765 name: "foo-excludes",
766 srcs: ["java-fg/a.java", "java-fg/b.java"],
767 }
768 `)
769
770 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
771
772 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
773 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
774 }
775}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900776
777func TestJavaSdkLibrary(t *testing.T) {
778 ctx := testJava(t, `
779 droiddoc_template {
780 name: "droiddoc-templates-sdk",
781 path: ".",
782 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900783 java_sdk_library {
784 name: "foo",
785 srcs: ["a.java", "b.java"],
786 api_packages: ["foo"],
787 }
788 java_sdk_library {
789 name: "bar",
790 srcs: ["a.java", "b.java"],
791 api_packages: ["bar"],
792 }
793 java_library {
794 name: "baz",
795 srcs: ["c.java"],
796 libs: ["foo", "bar"],
797 sdk_version: "system_current",
798 }
Jiyong Park1be96912018-05-28 18:02:19 +0900799 java_library {
800 name: "qux",
801 srcs: ["c.java"],
802 libs: ["baz"],
803 sdk_version: "system_current",
804 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900805 `)
806
807 // check the existence of the internal modules
808 ctx.ModuleForTests("foo", "android_common")
809 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
810 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900811 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900812 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
813 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900814 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800815 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +0900816 ctx.ModuleForTests("foo.api.public.28", "")
817 ctx.ModuleForTests("foo.api.system.28", "")
818 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900819
820 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
821 // tests if baz is actually linked to the stubs lib
822 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
823 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
824 "foo.stubs.system.jar")
825 }
826 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +0900827 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900828 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +0900829 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900830 }
831 // test if baz is not linked to the system variant of foo
832 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
833 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
834 "foo.stubs.jar")
835 }
Jiyong Park1be96912018-05-28 18:02:19 +0900836
837 // test if baz has exported SDK lib names foo and bar to qux
838 qux := ctx.ModuleForTests("qux", "android_common")
839 if quxLib, ok := qux.Module().(*Library); ok {
840 sdkLibs := quxLib.ExportedSdkLibs()
841 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
842 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
843 }
844 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900845}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200846
847var compilerFlagsTestCases = []struct {
848 in string
849 out bool
850}{
851 {
852 in: "a",
853 out: false,
854 },
855 {
856 in: "-a",
857 out: true,
858 },
859 {
860 in: "-no-jdk",
861 out: false,
862 },
863 {
864 in: "-no-stdlib",
865 out: false,
866 },
867 {
868 in: "-kotlin-home",
869 out: false,
870 },
871 {
872 in: "-kotlin-home /some/path",
873 out: false,
874 },
875 {
876 in: "-include-runtime",
877 out: false,
878 },
879 {
880 in: "-Xintellij-plugin-root",
881 out: false,
882 },
883}
884
885type mockContext struct {
886 android.ModuleContext
887 result bool
888}
889
890func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
891 // CheckBadCompilerFlags calls this function when the flag should be rejected
892 ctx.result = false
893}
894
895func TestCompilerFlags(t *testing.T) {
896 for _, testCase := range compilerFlagsTestCases {
897 ctx := &mockContext{result: true}
898 CheckKotlincFlags(ctx, []string{testCase.in})
899 if ctx.result != testCase.out {
900 t.Errorf("incorrect output:")
901 t.Errorf(" input: %#v", testCase.in)
902 t.Errorf(" expected: %#v", testCase.out)
903 t.Errorf(" got: %#v", ctx.result)
904 }
905 }
906}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800907
908// TODO(jungjw): Consider making this more robust by ignoring path order.
909func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
910 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
911 flags := strings.Split(variables["javacFlags"], " ")
912 got := ""
913 for _, flag := range flags {
914 keyEnd := strings.Index(flag, "=")
915 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
916 got = flag[keyEnd+1:]
917 break
918 }
919 }
920 if expected != got {
921 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
922 }
923}
924
925func TestPatchModule(t *testing.T) {
926 bp := `
927 java_library {
928 name: "foo",
929 srcs: ["a.java"],
930 }
931
932 java_library {
933 name: "bar",
934 srcs: ["b.java"],
935 no_standard_libs: true,
936 system_modules: "none",
937 patch_module: "java.base",
938 }
939
940 java_library {
941 name: "baz",
942 srcs: ["c.java"],
943 patch_module: "java.base",
944 }
945 `
946
947 t.Run("1.8", func(t *testing.T) {
948 // Test default javac 1.8
949 ctx := testJava(t, bp)
950
951 checkPatchModuleFlag(t, ctx, "foo", "")
952 checkPatchModuleFlag(t, ctx, "bar", "")
953 checkPatchModuleFlag(t, ctx, "baz", "")
954 })
955
956 t.Run("1.9", func(t *testing.T) {
957 // Test again with javac 1.9
958 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
959 ctx := testContext(config, bp, nil)
960 run(t, ctx, config)
961
962 checkPatchModuleFlag(t, ctx, "foo", "")
963 expected := "java.base=.:" + buildDir
964 checkPatchModuleFlag(t, ctx, "bar", expected)
965 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
966 checkPatchModuleFlag(t, ctx, "baz", expected)
967 })
968}