blob: c4cf9059de8f87d642ef0e6f3452de19361ce1cc [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 "fmt"
19 "io/ioutil"
20 "os"
21 "path/filepath"
Colin Cross227d4362017-08-30 14:14:52 -070022 "reflect"
Nan Zhang61eaedb2017-11-02 13:28:15 -070023 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070024 "strings"
25 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070026
27 "github.com/google/blueprint/proptools"
Colin Crossa4f08812018-10-02 22:03:40 -070028
29 "android/soong/android"
30 "android/soong/cc"
31 "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 Cross7fdd2b72018-01-02 18:14:25 -080060 if env == nil {
61 env = make(map[string]string)
62 }
63 if env["ANDROID_JAVA8_HOME"] == "" {
64 env["ANDROID_JAVA8_HOME"] = "jdk8"
65 }
Jiyong Park1a5d7b12018-01-15 15:05:10 +090066 config := android.TestArchConfig(buildDir, env)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070067 config.TestProductVariables.DeviceSystemSdkVersions = &[]string{"14", "15"}
Jiyong Park1a5d7b12018-01-15 15:05:10 +090068 return config
Colin Cross527012a2017-11-30 22:56:16 -080069
Colin Cross1369cdb2017-09-29 17:58:17 -070070}
71
Colin Cross527012a2017-11-30 22:56:16 -080072func testContext(config android.Config, bp string,
73 fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070074
Colin Cross4c428df2017-09-15 17:36:05 -070075 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070076 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Crossa97c5d32018-03-28 14:58:31 -070077 ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070078 ctx.RegisterModuleType("android_test", android.ModuleFactoryAdaptor(AndroidTestFactory))
Colin Cross638149e2019-01-05 22:12:12 -080079 ctx.RegisterModuleType("android_test_helper_app", android.ModuleFactoryAdaptor(AndroidTestHelperAppFactory))
80 ctx.RegisterModuleType("java_binary", android.ModuleFactoryAdaptor(BinaryFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080081 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Cross9ae1b922018-06-26 17:59:05 -070082 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross2acdae82017-09-15 19:44:24 -070083 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross638149e2019-01-05 22:12:12 -080084 ctx.RegisterModuleType("java_test", android.ModuleFactoryAdaptor(TestFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070085 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070086 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070087 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080088 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070089 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070090 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080091 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
92 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Nan Zhangf4936b02018-08-01 15:00:28 -070093 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
Jiyong Parkc678ad32018-04-10 13:07:10 +090094 ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(sdkLibraryFactory))
Jiyong Park58c518b2018-05-12 22:29:12 +090095 ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(prebuiltApisFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070096 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
97 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070098 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090099 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Park58c518b2018-05-12 22:29:12 +0900100 ctx.TopDown("prebuilt_apis", prebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900101 ctx.TopDown("java_sdk_library", sdkLibraryMutator).Parallel()
102 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800103 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700104
105 // Register module types and mutators from cc needed for JNI testing
106 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
107 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
108 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
109 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
110 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
111 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
112 })
113
Colin Cross72bb3632017-07-13 16:23:21 -0700114 ctx.Register()
115
Colin Cross4c428df2017-09-15 17:36:05 -0700116 extraModules := []string{
Colin Cross86a60ae2018-05-29 14:44:55 -0700117 "core-lambda-stubs",
Colin Cross4c428df2017-09-15 17:36:05 -0700118 "framework",
119 "ext",
Colin Cross2acdae82017-09-15 19:44:24 -0700120 "android_stubs_current",
121 "android_system_stubs_current",
122 "android_test_stubs_current",
Colin Crossf19b9bb2018-03-26 14:42:44 -0700123 "core.current.stubs",
Neil Fullerbf210f72018-10-09 16:19:19 +0100124 "core.platform.api.stubs",
Colin Cross93e85952017-08-15 13:34:18 -0700125 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -0700126 }
Colin Cross72bb3632017-07-13 16:23:21 -0700127
128 for _, extra := range extraModules {
129 bp += fmt.Sprintf(`
130 java_library {
131 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -0700132 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -0700133 no_standard_libs: true,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700134 sdk_version: "core_current",
Neil Fullerbf210f72018-10-09 16:19:19 +0100135 system_modules: "core-platform-api-stubs-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -0700136 }
137 `, extra)
138 }
139
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800140 bp += `
141 android_app {
142 name: "framework-res",
143 no_framework_libs: true,
144 }
145 `
146
Colin Cross527012a2017-11-30 22:56:16 -0800147 systemModules := []string{
148 "core-system-modules",
Neil Fullerbf210f72018-10-09 16:19:19 +0100149 "core-platform-api-stubs-system-modules",
Colin Cross527012a2017-11-30 22:56:16 -0800150 "android_stubs_current_system_modules",
151 "android_system_stubs_current_system_modules",
152 "android_test_stubs_current_system_modules",
153 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700154
Colin Cross527012a2017-11-30 22:56:16 -0800155 for _, extra := range systemModules {
156 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700157 java_system_modules {
158 name: "%s",
159 }
160 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700161 }
162
Colin Cross824bee32017-11-22 17:27:51 -0800163 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900164 "Android.bp": []byte(bp),
165 "a.java": nil,
166 "b.java": nil,
167 "c.java": nil,
168 "b.kt": nil,
169 "a.jar": nil,
170 "b.jar": nil,
171 "java-res/a/a": nil,
172 "java-res/b/b": nil,
173 "java-res2/a": nil,
174 "java-fg/a.java": nil,
175 "java-fg/b.java": nil,
176 "java-fg/c.java": nil,
177 "api/current.txt": nil,
178 "api/removed.txt": nil,
179 "api/system-current.txt": nil,
180 "api/system-removed.txt": nil,
181 "api/test-current.txt": nil,
182 "api/test-removed.txt": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700183
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100184 "prebuilts/sdk/14/public/android.jar": nil,
185 "prebuilts/sdk/14/public/framework.aidl": nil,
186 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700187 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100188 "prebuilts/sdk/current/public/android.jar": nil,
189 "prebuilts/sdk/current/public/framework.aidl": nil,
190 "prebuilts/sdk/current/public/core.jar": nil,
191 "prebuilts/sdk/current/system/android.jar": nil,
192 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900193 "prebuilts/sdk/28/public/api/foo.txt": nil,
194 "prebuilts/sdk/28/system/api/foo.txt": nil,
195 "prebuilts/sdk/28/test/api/foo.txt": nil,
196 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
197 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
198 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
199 "prebuilts/sdk/28/public/api/bar.txt": nil,
200 "prebuilts/sdk/28/system/api/bar.txt": nil,
201 "prebuilts/sdk/28/test/api/bar.txt": nil,
202 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
203 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
204 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700205 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900206 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800207
208 // For framework-res, which is an implicit dependency for framework
209 "AndroidManifest.xml": nil,
210 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800211
212 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800213
Colin Cross66dbc0b2017-12-28 12:23:20 -0800214 "build/make/core/proguard.flags": nil,
215 "build/make/core/proguard_basic_keeps.flags": nil,
216
Colin Cross7fdd2b72018-01-02 18:14:25 -0800217 "jdk8/jre/lib/jce.jar": nil,
218 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700219 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800220
221 "bar-doc/a.java": nil,
222 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900223 "bar-doc/IFoo.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800224 "bar-doc/known_oj_tags.txt": nil,
225 "external/doclava/templates-sdk": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800226 }
227
228 for k, v := range fs {
229 mockFS[k] = v
230 }
231
232 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700233
Colin Cross527012a2017-11-30 22:56:16 -0800234 return ctx
235}
236
237func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800238 t.Helper()
Jiyong Park58c518b2018-05-12 22:29:12 +0900239 _, errs := ctx.ParseFileList(".", []string{"Android.bp", "prebuilts/sdk/Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800240 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700241 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800242 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800243}
244
245func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800246 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800247 config := testConfig(nil)
248 ctx := testContext(config, bp, nil)
249 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700250
251 return ctx
252}
253
Colin Cross2acdae82017-09-15 19:44:24 -0700254func moduleToPath(name string) string {
255 switch {
256 case name == `""`:
257 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700258 case strings.HasSuffix(name, ".jar"):
259 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700260 default:
261 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700262 }
263}
264
Colin Cross72bb3632017-07-13 16:23:21 -0700265func TestSimple(t *testing.T) {
266 ctx := testJava(t, `
267 java_library {
268 name: "foo",
269 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700270 libs: ["bar"],
271 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700272 }
273
274 java_library {
275 name: "bar",
276 srcs: ["b.java"],
277 }
278
279 java_library {
280 name: "baz",
281 srcs: ["c.java"],
282 }
Colin Crossd5934c82017-10-02 13:55:26 -0700283 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700284
Colin Cross4c428df2017-09-15 17:36:05 -0700285 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700286 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700287
288 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
289 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
290 }
291
Colin Cross1ee23172017-10-18 14:44:18 -0700292 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700293 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
294 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700295
Nan Zhanged19fc32017-10-19 13:06:22 -0700296 if !strings.Contains(javac.Args["classpath"], barTurbine) {
297 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700298 }
299
Nan Zhanged19fc32017-10-19 13:06:22 -0700300 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
301 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700302 }
303
304 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
305 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700306 }
307}
308
Colin Crossd5934c82017-10-02 13:55:26 -0700309func TestArchSpecific(t *testing.T) {
310 ctx := testJava(t, `
311 java_library {
312 name: "foo",
313 srcs: ["a.java"],
314 target: {
315 android: {
316 srcs: ["b.java"],
317 },
318 },
319 }
320 `)
321
322 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
323 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
324 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
325 }
326}
327
Colin Cross6b4a32d2017-12-05 13:42:45 -0800328func TestBinary(t *testing.T) {
329 ctx := testJava(t, `
330 java_library_host {
331 name: "foo",
332 srcs: ["a.java"],
333 }
334
335 java_binary_host {
336 name: "bar",
337 srcs: ["b.java"],
338 static_libs: ["foo"],
339 }
340 `)
341
342 buildOS := android.BuildOs.String()
343
344 bar := ctx.ModuleForTests("bar", buildOS+"_common")
345 barJar := bar.Output("bar.jar").Output.String()
346 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
347 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
348
349 // Test that the install binary wrapper depends on the installed jar file
350 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
351 t.Errorf("expected binary wrapper implicits [%q], got %v",
352 barJar, barWrapperDeps)
353 }
354
355}
356
Colin Cross2acdae82017-09-15 19:44:24 -0700357var classpathTestcases = []struct {
358 name string
Colin Cross86a60ae2018-05-29 14:44:55 -0700359 unbundled bool
Colin Cross965714f2017-10-03 20:57:01 -0700360 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700361 host android.OsClass
362 properties string
363 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700364 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700365 classpath []string
366}{
367 {
368 name: "default",
Neil Fullerbf210f72018-10-09 16:19:19 +0100369 bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
370 system: "core-platform-api-stubs-system-modules",
Neil Fuller1fee9f32018-10-04 11:45:49 +0100371 classpath: []string{"ext", "framework"},
Colin Cross2acdae82017-09-15 19:44:24 -0700372 },
373 {
374 name: "blank sdk version",
375 properties: `sdk_version: "",`,
Neil Fullerbf210f72018-10-09 16:19:19 +0100376 bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
377 system: "core-platform-api-stubs-system-modules",
Neil Fuller1fee9f32018-10-04 11:45:49 +0100378 classpath: []string{"ext", "framework"},
Colin Cross2acdae82017-09-15 19:44:24 -0700379 },
380 {
Colin Cross4c428df2017-09-15 17:36:05 -0700381
Colin Cross2acdae82017-09-15 19:44:24 -0700382 name: "sdk v14",
383 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700384 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700385 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross86a60ae2018-05-29 14:44:55 -0700386 classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700387 },
388 {
Colin Cross72bb3632017-07-13 16:23:21 -0700389
Colin Cross2acdae82017-09-15 19:44:24 -0700390 name: "current",
391 properties: `sdk_version: "current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700392 bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700393 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700394 },
395 {
Colin Cross72bb3632017-07-13 16:23:21 -0700396
Colin Cross2acdae82017-09-15 19:44:24 -0700397 name: "system_current",
398 properties: `sdk_version: "system_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700399 bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700400 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700401 },
402 {
Colin Cross72bb3632017-07-13 16:23:21 -0700403
Sundong Ahn0926fae2017-10-17 16:34:51 +0900404 name: "system_14",
405 properties: `sdk_version: "system_14",`,
406 bootclasspath: []string{`""`},
407 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross86a60ae2018-05-29 14:44:55 -0700408 classpath: []string{"prebuilts/sdk/14/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Sundong Ahn0926fae2017-10-17 16:34:51 +0900409 },
410 {
411
Colin Cross2acdae82017-09-15 19:44:24 -0700412 name: "test_current",
413 properties: `sdk_version: "test_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700414 bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700415 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700416 },
417 {
Colin Cross72bb3632017-07-13 16:23:21 -0700418
Jiyong Park750e5572018-01-31 00:20:13 +0900419 name: "core_current",
420 properties: `sdk_version: "core_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700421 bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
Jiyong Park750e5572018-01-31 00:20:13 +0900422 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Jiyong Park750e5572018-01-31 00:20:13 +0900423 },
424 {
425
Colin Cross2acdae82017-09-15 19:44:24 -0700426 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700427 properties: `no_standard_libs: true, system_modules: "none"`,
428 system: "none",
429 bootclasspath: []string{`""`},
430 classpath: []string{},
431 },
432 {
433
434 name: "nostdlib system_modules",
Neil Fullerbf210f72018-10-09 16:19:19 +0100435 properties: `no_standard_libs: true, system_modules: "core-platform-api-stubs-system-modules"`,
436 system: "core-platform-api-stubs-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700437 bootclasspath: []string{`""`},
438 classpath: []string{},
439 },
440 {
Colin Cross72bb3632017-07-13 16:23:21 -0700441
Colin Cross7fdd2b72018-01-02 18:14:25 -0800442 name: "host default",
443 moduleType: "java_library_host",
444 properties: ``,
445 host: android.Host,
446 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
447 classpath: []string{},
Colin Cross2acdae82017-09-15 19:44:24 -0700448 },
449 {
450 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700451 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700452 host: android.Host,
453 properties: `no_standard_libs: true`,
454 classpath: []string{},
455 },
Colin Cross965714f2017-10-03 20:57:01 -0700456 {
457
Colin Cross7fdd2b72018-01-02 18:14:25 -0800458 name: "host supported default",
459 host: android.Host,
460 properties: `host_supported: true,`,
461 classpath: []string{},
462 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
Colin Cross965714f2017-10-03 20:57:01 -0700463 },
464 {
465 name: "host supported nostdlib",
466 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700467 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700468 classpath: []string{},
469 },
Colin Cross86a60ae2018-05-29 14:44:55 -0700470 {
471
472 name: "unbundled sdk v14",
473 unbundled: true,
474 properties: `sdk_version: "14",`,
475 bootclasspath: []string{`""`},
476 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
477 classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
478 },
479 {
480
481 name: "unbundled current",
482 unbundled: true,
483 properties: `sdk_version: "current",`,
484 bootclasspath: []string{`""`},
485 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
486 classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
487 },
Colin Cross2acdae82017-09-15 19:44:24 -0700488}
Colin Cross72bb3632017-07-13 16:23:21 -0700489
Colin Cross2acdae82017-09-15 19:44:24 -0700490func TestClasspath(t *testing.T) {
491 for _, testcase := range classpathTestcases {
492 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700493 moduleType := "java_library"
494 if testcase.moduleType != "" {
495 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700496 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700497
498 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700499 name: "foo",
500 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700501 ` + testcase.properties + `
502 }`
503
504 variant := "android_common"
505 if testcase.host == android.Host {
506 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700507 }
Colin Cross72bb3632017-07-13 16:23:21 -0700508
Colin Cross2acdae82017-09-15 19:44:24 -0700509 convertModulesToPaths := func(cp []string) []string {
510 ret := make([]string, len(cp))
511 for i, e := range cp {
512 ret[i] = moduleToPath(e)
513 }
514 return ret
515 }
Colin Cross72bb3632017-07-13 16:23:21 -0700516
Colin Cross2acdae82017-09-15 19:44:24 -0700517 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
518 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700519
Colin Cross2acdae82017-09-15 19:44:24 -0700520 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700521 if bc != "" {
522 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700523 }
Colin Cross72bb3632017-07-13 16:23:21 -0700524
Colin Cross2acdae82017-09-15 19:44:24 -0700525 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700526 if c != "" {
527 c = "-classpath " + c
528 }
529 system := ""
530 if testcase.system == "none" {
531 system = "--system=none"
532 } else if testcase.system != "" {
533 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700534 }
535
Colin Cross1369cdb2017-09-29 17:58:17 -0700536 t.Run("1.8", func(t *testing.T) {
537 // Test default javac 1.8
Colin Cross86a60ae2018-05-29 14:44:55 -0700538 config := testConfig(nil)
539 if testcase.unbundled {
540 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
541 }
542 ctx := testContext(config, bp, nil)
543 run(t, ctx, config)
Colin Cross2acdae82017-09-15 19:44:24 -0700544
Colin Cross1369cdb2017-09-29 17:58:17 -0700545 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
546
547 got := javac.Args["bootClasspath"]
548 if got != bc {
549 t.Errorf("bootclasspath expected %q != got %q", bc, got)
550 }
551
552 got = javac.Args["classpath"]
553 if got != c {
554 t.Errorf("classpath expected %q != got %q", c, got)
555 }
556
557 var deps []string
558 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
559 deps = append(deps, bootclasspath...)
560 }
561 deps = append(deps, classpath...)
562
563 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
564 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
565 }
566 })
567
568 // Test again with javac 1.9
569 t.Run("1.9", func(t *testing.T) {
Colin Cross527012a2017-11-30 22:56:16 -0800570 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
Colin Cross86a60ae2018-05-29 14:44:55 -0700571 if testcase.unbundled {
572 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
573 }
Colin Cross527012a2017-11-30 22:56:16 -0800574 ctx := testContext(config, bp, nil)
575 run(t, ctx, config)
Colin Cross1369cdb2017-09-29 17:58:17 -0700576
577 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
578 got := javac.Args["bootClasspath"]
579 expected := system
580 if testcase.system == "bootclasspath" {
581 expected = bc
582 }
583 if got != expected {
584 t.Errorf("bootclasspath expected %q != got %q", expected, got)
585 }
586 })
Colin Cross2acdae82017-09-15 19:44:24 -0700587 })
Colin Cross72bb3632017-07-13 16:23:21 -0700588 }
589
Colin Cross72bb3632017-07-13 16:23:21 -0700590}
591
592func TestPrebuilts(t *testing.T) {
593 ctx := testJava(t, `
594 java_library {
595 name: "foo",
596 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700597 libs: ["bar"],
598 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700599 }
600
Colin Cross74d73e22017-08-02 11:05:49 -0700601 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700602 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700603 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700604 }
605
Colin Cross74d73e22017-08-02 11:05:49 -0700606 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700607 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700608 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700609 }
610 `)
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 Cross37f6d792018-07-12 12:28:41 -0700614 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
615 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700616
Colin Cross37f6d792018-07-12 12:28:41 -0700617 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
618 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700619 }
620
Colin Cross37f6d792018-07-12 12:28:41 -0700621 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
622 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700623 }
624}
625
Colin Cross89536d42017-07-07 14:35:50 -0700626func TestDefaults(t *testing.T) {
627 ctx := testJava(t, `
628 java_defaults {
629 name: "defaults",
630 srcs: ["a.java"],
631 libs: ["bar"],
632 static_libs: ["baz"],
633 }
634
635 java_library {
636 name: "foo",
637 defaults: ["defaults"],
638 }
639
640 java_library {
641 name: "bar",
642 srcs: ["b.java"],
643 }
644
645 java_library {
646 name: "baz",
647 srcs: ["c.java"],
648 }
649 `)
650
Colin Cross4c428df2017-09-15 17:36:05 -0700651 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700652 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700653
654 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
655 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
656 }
657
Nan Zhanged19fc32017-10-19 13:06:22 -0700658 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
659 if !strings.Contains(javac.Args["classpath"], barTurbine) {
660 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700661 }
662
Colin Cross1ee23172017-10-18 14:44:18 -0700663 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700664 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
665 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700666 }
667}
668
Colin Cross0f37af02017-09-27 17:42:05 -0700669func TestResources(t *testing.T) {
670 var table = []struct {
671 name string
672 prop string
673 extra string
674 args string
675 }{
676 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700677 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700678 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800679 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700680 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700681 },
682 {
683 // Test that a module with java_resources includes the files
684 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700685 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
686 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700687 },
688 {
689 // Test that a module with a filegroup in java_resources includes the files with the
690 // path prefix
691 name: "resource filegroup",
692 prop: `java_resources: [":foo-res"]`,
693 extra: `
694 filegroup {
695 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800696 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700697 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700698 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700699 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700700 },
701 {
702 // Test that a module with "include_srcs: true" includes its source files in the resources jar
703 name: "include sources",
704 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700705 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700706 },
Colin Cross0ead1d72018-04-10 13:07:42 -0700707 {
708 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
709 name: "wildcard dirs",
710 prop: `java_resource_dirs: ["java-res/*"]`,
711 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
712 },
713 {
714 // Test that a module exclude_java_resource_dirs excludes the files
715 name: "wildcard dirs",
716 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
717 args: "-C java-res/a -f java-res/a/a",
718 },
Colin Crosscedd4762018-09-13 11:26:19 -0700719 {
720 // Test wildcards in java_resources
721 name: "wildcard files",
722 prop: `java_resources: ["java-res/**/*"]`,
723 args: "-C . -f java-res/a/a -f java-res/b/b",
724 },
725 {
726 // Test exclude_java_resources with java_resources
727 name: "wildcard files with exclude",
728 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
729 args: "-C . -f java-res/a/a",
730 },
731 {
732 // Test exclude_java_resources with java_resource_dirs
733 name: "resource dirs with exclude files",
734 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
735 args: "-C java-res -f java-res/a/a",
736 },
737 {
738 // Test exclude_java_resource_dirs with java_resource_dirs
739 name: "resource dirs with exclude files",
740 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
741 args: "-C java-res -f java-res/a/a -f java-res/b/b",
742 },
Colin Cross0f37af02017-09-27 17:42:05 -0700743 }
744
745 for _, test := range table {
746 t.Run(test.name, func(t *testing.T) {
747 ctx := testJava(t, `
748 java_library {
749 name: "foo",
750 srcs: [
751 "a.java",
752 "b.java",
753 "c.java",
754 ],
755 `+test.prop+`,
756 }
757 `+test.extra)
758
Colin Cross331a1212018-08-15 20:40:52 -0700759 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700760 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700761
762 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
763 t.Errorf("foo combined jars %v does not contain %q",
764 foo.Inputs.Strings(), fooRes.Output.String())
765 }
766
Colin Crossaf9c55b2017-10-03 14:50:08 -0700767 if fooRes.Args["jarArgs"] != test.args {
768 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700769 fooRes.Args["jarArgs"], test.args)
770 }
771 })
772 }
773}
774
Colin Cross54190b32017-10-09 15:34:10 -0700775func TestGeneratedSources(t *testing.T) {
776 ctx := testJava(t, `
777 java_library {
778 name: "foo",
779 srcs: [
780 "a*.java",
781 ":gen",
782 "b*.java",
783 ],
784 }
785
786 genrule {
787 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800788 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700789 out: ["gen.java"],
790 }
791 `)
792
793 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
794 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
795
Colin Cross15e86d92017-10-20 15:07:08 -0700796 if filepath.Base(genrule.Output.String()) != "gen.java" {
797 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700798 }
799
800 if len(javac.Inputs) != 3 ||
801 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700802 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700803 javac.Inputs[2].String() != "b.java" {
804 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
805 }
806}
807
Colin Cross93e85952017-08-15 13:34:18 -0700808func TestKotlin(t *testing.T) {
809 ctx := testJava(t, `
810 java_library {
811 name: "foo",
Colin Cross9bc43432017-12-15 20:20:39 -0800812 srcs: ["a.java", "b.kt"],
Colin Cross93e85952017-08-15 13:34:18 -0700813 }
Colin Cross715d7112017-10-25 20:32:18 -0700814
815 java_library {
816 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800817 srcs: ["b.kt"],
818 libs: ["foo"],
819 static_libs: ["baz"],
820 }
821
822 java_library {
823 name: "baz",
824 srcs: ["c.java"],
Colin Cross715d7112017-10-25 20:32:18 -0700825 }
Colin Cross93e85952017-08-15 13:34:18 -0700826 `)
827
Colin Cross9bc43432017-12-15 20:20:39 -0800828 fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
829 fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
830 fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700831
Colin Cross9bc43432017-12-15 20:20:39 -0800832 if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
833 fooKotlinc.Inputs[1].String() != "b.kt" {
834 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700835 }
836
Colin Cross9bc43432017-12-15 20:20:39 -0800837 if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
838 t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700839 }
840
Colin Cross9bc43432017-12-15 20:20:39 -0800841 if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
Colin Cross93e85952017-08-15 13:34:18 -0700842 t.Errorf("foo classpath %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800843 fooJavac.Args["classpath"], fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700844 }
845
Colin Cross9bc43432017-12-15 20:20:39 -0800846 if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
Colin Cross93e85952017-08-15 13:34:18 -0700847 t.Errorf("foo jar inputs %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800848 fooJar.Inputs.Strings(), fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700849 }
Colin Cross715d7112017-10-25 20:32:18 -0700850
Colin Cross9bc43432017-12-15 20:20:39 -0800851 fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
852 bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
853 barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
Colin Cross715d7112017-10-25 20:32:18 -0700854
Colin Cross9bc43432017-12-15 20:20:39 -0800855 if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
856 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
857 }
858
859 if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
860 t.Errorf(`expected %q in bar implicits %v`,
861 fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
862 }
863
864 if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
865 t.Errorf(`expected %q in bar implicits %v`,
866 bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
Colin Cross715d7112017-10-25 20:32:18 -0700867 }
Colin Cross93e85952017-08-15 13:34:18 -0700868}
869
Nan Zhang61eaedb2017-11-02 13:28:15 -0700870func TestTurbine(t *testing.T) {
871 ctx := testJava(t, `
872 java_library {
873 name: "foo",
874 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900875 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700876 }
877
878 java_library {
879 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800880 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700881 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900882 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700883 }
884
885 java_library {
886 name: "baz",
887 srcs: ["c.java"],
888 libs: ["bar"],
889 sdk_version: "14",
890 }
891 `)
892
893 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
894 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
895 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
896 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
897 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
898
899 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
900 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
901 }
902
903 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
904 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
905 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
906 }
907 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
908 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
909 }
910 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
911 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
912 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100913 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700914 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100915 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700916 }
917}
918
919func TestSharding(t *testing.T) {
920 ctx := testJava(t, `
921 java_library {
922 name: "bar",
923 srcs: ["a.java","b.java","c.java"],
924 javac_shard_size: 1
925 }
926 `)
927
928 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
929 for i := 0; i < 3; i++ {
930 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
931 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
932 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
933 }
934 }
935}
936
Nan Zhang581fd212018-01-10 16:06:12 -0800937func TestDroiddoc(t *testing.T) {
938 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800939 droiddoc_template {
940 name: "droiddoc-templates-sdk",
941 path: ".",
942 }
Nan Zhang581fd212018-01-10 16:06:12 -0800943 droiddoc {
944 name: "bar-doc",
945 srcs: [
946 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900947 "bar-doc/IFoo.aidl",
Nan Zhang581fd212018-01-10 16:06:12 -0800948 ],
949 exclude_srcs: [
950 "bar-doc/b.java"
951 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800952 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800953 hdf: [
954 "android.whichdoc offline",
955 ],
956 knowntags: [
957 "bar-doc/known_oj_tags.txt",
958 ],
959 proofread_file: "libcore-proofread.txt",
960 todo_file: "libcore-docs-todo.html",
961 args: "-offlinemode -title \"libcore\"",
962 }
963 `)
964
Nan Zhang3bb14202018-02-28 10:14:25 -0800965 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
966 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800967 if stubsJar != barDoc.Output.String() {
968 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
969 }
Jiyong Park1e440682018-05-23 18:42:04 +0900970 inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
971 var javaSrcs []string
972 for _, i := range inputs {
973 javaSrcs = append(javaSrcs, i.Base())
974 }
975 if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
976 t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
977 }
Nan Zhang581fd212018-01-10 16:06:12 -0800978}
979
Colin Cross54250902017-12-05 09:28:08 -0800980func TestJarGenrules(t *testing.T) {
981 ctx := testJava(t, `
982 java_library {
983 name: "foo",
984 srcs: ["a.java"],
985 }
986
987 java_genrule {
988 name: "jargen",
989 tool_files: ["b.java"],
990 cmd: "$(location b.java) $(in) $(out)",
991 out: ["jargen.jar"],
992 srcs: [":foo"],
993 }
994
995 java_library {
996 name: "bar",
997 static_libs: ["jargen"],
998 srcs: ["c.java"],
999 }
1000
1001 java_library {
1002 name: "baz",
1003 libs: ["jargen"],
1004 srcs: ["c.java"],
1005 }
1006 `)
1007
1008 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1009 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1010 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1011 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1012 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1013
1014 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1015 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1016 }
1017
1018 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1019 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1020 }
1021
1022 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1023 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1024 }
1025
1026 if len(barCombined.Inputs) != 2 ||
1027 barCombined.Inputs[0].String() != bar.Output.String() ||
1028 barCombined.Inputs[1].String() != jargen.Output.String() {
1029 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1030 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1031 }
1032}
1033
Nan Zhang27e284d2018-02-09 21:03:53 +00001034func TestExcludeFileGroupInSrcs(t *testing.T) {
1035 ctx := testJava(t, `
1036 java_library {
1037 name: "foo",
1038 srcs: ["a.java", ":foo-srcs"],
1039 exclude_srcs: ["a.java", ":foo-excludes"],
1040 }
1041
1042 filegroup {
1043 name: "foo-srcs",
1044 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1045 }
1046
1047 filegroup {
1048 name: "foo-excludes",
1049 srcs: ["java-fg/a.java", "java-fg/b.java"],
1050 }
1051 `)
1052
1053 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1054
1055 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1056 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1057 }
1058}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001059
1060func TestJavaSdkLibrary(t *testing.T) {
1061 ctx := testJava(t, `
1062 droiddoc_template {
1063 name: "droiddoc-templates-sdk",
1064 path: ".",
1065 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001066 java_sdk_library {
1067 name: "foo",
1068 srcs: ["a.java", "b.java"],
1069 api_packages: ["foo"],
1070 }
1071 java_sdk_library {
1072 name: "bar",
1073 srcs: ["a.java", "b.java"],
1074 api_packages: ["bar"],
1075 }
1076 java_library {
1077 name: "baz",
1078 srcs: ["c.java"],
1079 libs: ["foo", "bar"],
1080 sdk_version: "system_current",
1081 }
Jiyong Park1be96912018-05-28 18:02:19 +09001082 java_library {
1083 name: "qux",
1084 srcs: ["c.java"],
1085 libs: ["baz"],
1086 sdk_version: "system_current",
1087 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001088 `)
1089
1090 // check the existence of the internal modules
1091 ctx.ModuleForTests("foo", "android_common")
1092 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
1093 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001094 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001095 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
1096 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001097 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001098 ctx.ModuleForTests("foo"+sdkImplLibrarySuffix, "android_common")
Jiyong Park5a8d1be2018-04-25 22:57:34 +09001099 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001100 ctx.ModuleForTests("foo.api.public.28", "")
1101 ctx.ModuleForTests("foo.api.system.28", "")
1102 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001103
1104 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1105 // tests if baz is actually linked to the stubs lib
1106 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1107 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1108 "foo.stubs.system.jar")
1109 }
1110 // ... and not to the impl lib
1111 if strings.Contains(bazJavac.Args["classpath"], "foo.impl.jar") {
1112 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1113 "foo.impl.jar")
1114 }
1115 // test if baz is not linked to the system variant of foo
1116 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1117 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1118 "foo.stubs.jar")
1119 }
Jiyong Park1be96912018-05-28 18:02:19 +09001120
1121 // test if baz has exported SDK lib names foo and bar to qux
1122 qux := ctx.ModuleForTests("qux", "android_common")
1123 if quxLib, ok := qux.Module().(*Library); ok {
1124 sdkLibs := quxLib.ExportedSdkLibs()
1125 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
1126 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
1127 }
1128 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001129}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001130
1131var compilerFlagsTestCases = []struct {
1132 in string
1133 out bool
1134}{
1135 {
1136 in: "a",
1137 out: false,
1138 },
1139 {
1140 in: "-a",
1141 out: true,
1142 },
1143 {
1144 in: "-no-jdk",
1145 out: false,
1146 },
1147 {
1148 in: "-no-stdlib",
1149 out: false,
1150 },
1151 {
1152 in: "-kotlin-home",
1153 out: false,
1154 },
1155 {
1156 in: "-kotlin-home /some/path",
1157 out: false,
1158 },
1159 {
1160 in: "-include-runtime",
1161 out: false,
1162 },
1163 {
1164 in: "-Xintellij-plugin-root",
1165 out: false,
1166 },
1167}
1168
1169type mockContext struct {
1170 android.ModuleContext
1171 result bool
1172}
1173
1174func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1175 // CheckBadCompilerFlags calls this function when the flag should be rejected
1176 ctx.result = false
1177}
1178
1179func TestCompilerFlags(t *testing.T) {
1180 for _, testCase := range compilerFlagsTestCases {
1181 ctx := &mockContext{result: true}
1182 CheckKotlincFlags(ctx, []string{testCase.in})
1183 if ctx.result != testCase.out {
1184 t.Errorf("incorrect output:")
1185 t.Errorf(" input: %#v", testCase.in)
1186 t.Errorf(" expected: %#v", testCase.out)
1187 t.Errorf(" got: %#v", ctx.result)
1188 }
1189 }
1190}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001191
1192// TODO(jungjw): Consider making this more robust by ignoring path order.
1193func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1194 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1195 flags := strings.Split(variables["javacFlags"], " ")
1196 got := ""
1197 for _, flag := range flags {
1198 keyEnd := strings.Index(flag, "=")
1199 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1200 got = flag[keyEnd+1:]
1201 break
1202 }
1203 }
1204 if expected != got {
1205 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1206 }
1207}
1208
1209func TestPatchModule(t *testing.T) {
1210 bp := `
1211 java_library {
1212 name: "foo",
1213 srcs: ["a.java"],
1214 }
1215
1216 java_library {
1217 name: "bar",
1218 srcs: ["b.java"],
1219 no_standard_libs: true,
1220 system_modules: "none",
1221 patch_module: "java.base",
1222 }
1223
1224 java_library {
1225 name: "baz",
1226 srcs: ["c.java"],
1227 patch_module: "java.base",
1228 }
1229 `
1230
1231 t.Run("1.8", func(t *testing.T) {
1232 // Test default javac 1.8
1233 ctx := testJava(t, bp)
1234
1235 checkPatchModuleFlag(t, ctx, "foo", "")
1236 checkPatchModuleFlag(t, ctx, "bar", "")
1237 checkPatchModuleFlag(t, ctx, "baz", "")
1238 })
1239
1240 t.Run("1.9", func(t *testing.T) {
1241 // Test again with javac 1.9
1242 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
1243 ctx := testContext(config, bp, nil)
1244 run(t, ctx, config)
1245
1246 checkPatchModuleFlag(t, ctx, "foo", "")
1247 expected := "java.base=.:" + buildDir
1248 checkPatchModuleFlag(t, ctx, "bar", expected)
1249 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
1250 checkPatchModuleFlag(t, ctx, "baz", expected)
1251 })
1252}