blob: 2a54f699c94666ee9dc55a09497d6bf107cc010a [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 Cross6b4a32d2017-12-05 13:42:45 -080079 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Cross9ae1b922018-06-26 17:59:05 -070080 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross2acdae82017-09-15 19:44:24 -070081 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070082 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070083 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070084 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080085 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070086 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070087 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080088 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
89 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Nan Zhangf4936b02018-08-01 15:00:28 -070090 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
Jiyong Parkc678ad32018-04-10 13:07:10 +090091 ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(sdkLibraryFactory))
Jiyong Park58c518b2018-05-12 22:29:12 +090092 ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(prebuiltApisFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070093 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
94 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070095 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090096 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Park58c518b2018-05-12 22:29:12 +090097 ctx.TopDown("prebuilt_apis", prebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +090098 ctx.TopDown("java_sdk_library", sdkLibraryMutator).Parallel()
99 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800100 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
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))
106 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
107 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
108 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
109 })
110
Colin Cross72bb3632017-07-13 16:23:21 -0700111 ctx.Register()
112
Colin Cross4c428df2017-09-15 17:36:05 -0700113 extraModules := []string{
Colin Cross86a60ae2018-05-29 14:44:55 -0700114 "core-lambda-stubs",
Colin Cross4c428df2017-09-15 17:36:05 -0700115 "framework",
116 "ext",
Colin Cross2acdae82017-09-15 19:44:24 -0700117 "android_stubs_current",
118 "android_system_stubs_current",
119 "android_test_stubs_current",
Colin Crossf19b9bb2018-03-26 14:42:44 -0700120 "core.current.stubs",
Neil Fullerbf210f72018-10-09 16:19:19 +0100121 "core.platform.api.stubs",
Colin Cross93e85952017-08-15 13:34:18 -0700122 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -0700123 }
Colin Cross72bb3632017-07-13 16:23:21 -0700124
125 for _, extra := range extraModules {
126 bp += fmt.Sprintf(`
127 java_library {
128 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -0700129 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -0700130 no_standard_libs: true,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700131 sdk_version: "core_current",
Neil Fullerbf210f72018-10-09 16:19:19 +0100132 system_modules: "core-platform-api-stubs-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -0700133 }
134 `, extra)
135 }
136
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800137 bp += `
138 android_app {
139 name: "framework-res",
140 no_framework_libs: true,
141 }
142 `
143
Colin Cross527012a2017-11-30 22:56:16 -0800144 systemModules := []string{
145 "core-system-modules",
Neil Fullerbf210f72018-10-09 16:19:19 +0100146 "core-platform-api-stubs-system-modules",
Colin Cross527012a2017-11-30 22:56:16 -0800147 "android_stubs_current_system_modules",
148 "android_system_stubs_current_system_modules",
149 "android_test_stubs_current_system_modules",
150 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700151
Colin Cross527012a2017-11-30 22:56:16 -0800152 for _, extra := range systemModules {
153 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700154 java_system_modules {
155 name: "%s",
156 }
157 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700158 }
159
Colin Cross824bee32017-11-22 17:27:51 -0800160 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900161 "Android.bp": []byte(bp),
162 "a.java": nil,
163 "b.java": nil,
164 "c.java": nil,
165 "b.kt": nil,
166 "a.jar": nil,
167 "b.jar": nil,
168 "java-res/a/a": nil,
169 "java-res/b/b": nil,
170 "java-res2/a": nil,
171 "java-fg/a.java": nil,
172 "java-fg/b.java": nil,
173 "java-fg/c.java": nil,
174 "api/current.txt": nil,
175 "api/removed.txt": nil,
176 "api/system-current.txt": nil,
177 "api/system-removed.txt": nil,
178 "api/test-current.txt": nil,
179 "api/test-removed.txt": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700180
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100181 "prebuilts/sdk/14/public/android.jar": nil,
182 "prebuilts/sdk/14/public/framework.aidl": nil,
183 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700184 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100185 "prebuilts/sdk/current/public/android.jar": nil,
186 "prebuilts/sdk/current/public/framework.aidl": nil,
187 "prebuilts/sdk/current/public/core.jar": nil,
188 "prebuilts/sdk/current/system/android.jar": nil,
189 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900190 "prebuilts/sdk/28/public/api/foo.txt": nil,
191 "prebuilts/sdk/28/system/api/foo.txt": nil,
192 "prebuilts/sdk/28/test/api/foo.txt": nil,
193 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
194 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
195 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
196 "prebuilts/sdk/28/public/api/bar.txt": nil,
197 "prebuilts/sdk/28/system/api/bar.txt": nil,
198 "prebuilts/sdk/28/test/api/bar.txt": nil,
199 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
200 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
201 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700202 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900203 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800204
205 // For framework-res, which is an implicit dependency for framework
206 "AndroidManifest.xml": nil,
207 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800208
209 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800210
Colin Cross66dbc0b2017-12-28 12:23:20 -0800211 "build/make/core/proguard.flags": nil,
212 "build/make/core/proguard_basic_keeps.flags": nil,
213
Colin Cross7fdd2b72018-01-02 18:14:25 -0800214 "jdk8/jre/lib/jce.jar": nil,
215 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700216 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800217
218 "bar-doc/a.java": nil,
219 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900220 "bar-doc/IFoo.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800221 "bar-doc/known_oj_tags.txt": nil,
222 "external/doclava/templates-sdk": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800223 }
224
225 for k, v := range fs {
226 mockFS[k] = v
227 }
228
229 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700230
Colin Cross527012a2017-11-30 22:56:16 -0800231 return ctx
232}
233
234func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800235 t.Helper()
Jiyong Park58c518b2018-05-12 22:29:12 +0900236 _, errs := ctx.ParseFileList(".", []string{"Android.bp", "prebuilts/sdk/Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800237 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700238 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800239 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800240}
241
242func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800243 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800244 config := testConfig(nil)
245 ctx := testContext(config, bp, nil)
246 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700247
248 return ctx
249}
250
Colin Cross2acdae82017-09-15 19:44:24 -0700251func moduleToPath(name string) string {
252 switch {
253 case name == `""`:
254 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700255 case strings.HasSuffix(name, ".jar"):
256 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700257 default:
258 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700259 }
260}
261
Colin Cross72bb3632017-07-13 16:23:21 -0700262func TestSimple(t *testing.T) {
263 ctx := testJava(t, `
264 java_library {
265 name: "foo",
266 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700267 libs: ["bar"],
268 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700269 }
270
271 java_library {
272 name: "bar",
273 srcs: ["b.java"],
274 }
275
276 java_library {
277 name: "baz",
278 srcs: ["c.java"],
279 }
Colin Crossd5934c82017-10-02 13:55:26 -0700280 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700281
Colin Cross4c428df2017-09-15 17:36:05 -0700282 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700283 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700284
285 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
286 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
287 }
288
Colin Cross1ee23172017-10-18 14:44:18 -0700289 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700290 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
291 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700292
Nan Zhanged19fc32017-10-19 13:06:22 -0700293 if !strings.Contains(javac.Args["classpath"], barTurbine) {
294 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700295 }
296
Nan Zhanged19fc32017-10-19 13:06:22 -0700297 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
298 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700299 }
300
301 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
302 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700303 }
304}
305
Colin Crossd5934c82017-10-02 13:55:26 -0700306func TestArchSpecific(t *testing.T) {
307 ctx := testJava(t, `
308 java_library {
309 name: "foo",
310 srcs: ["a.java"],
311 target: {
312 android: {
313 srcs: ["b.java"],
314 },
315 },
316 }
317 `)
318
319 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
320 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
321 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
322 }
323}
324
Colin Cross6b4a32d2017-12-05 13:42:45 -0800325func TestBinary(t *testing.T) {
326 ctx := testJava(t, `
327 java_library_host {
328 name: "foo",
329 srcs: ["a.java"],
330 }
331
332 java_binary_host {
333 name: "bar",
334 srcs: ["b.java"],
335 static_libs: ["foo"],
336 }
337 `)
338
339 buildOS := android.BuildOs.String()
340
341 bar := ctx.ModuleForTests("bar", buildOS+"_common")
342 barJar := bar.Output("bar.jar").Output.String()
343 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
344 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
345
346 // Test that the install binary wrapper depends on the installed jar file
347 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
348 t.Errorf("expected binary wrapper implicits [%q], got %v",
349 barJar, barWrapperDeps)
350 }
351
352}
353
Colin Cross2acdae82017-09-15 19:44:24 -0700354var classpathTestcases = []struct {
355 name string
Colin Cross86a60ae2018-05-29 14:44:55 -0700356 unbundled bool
Colin Cross965714f2017-10-03 20:57:01 -0700357 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700358 host android.OsClass
359 properties string
360 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700361 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700362 classpath []string
363}{
364 {
365 name: "default",
Neil Fullerbf210f72018-10-09 16:19:19 +0100366 bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
367 system: "core-platform-api-stubs-system-modules",
Neil Fuller1fee9f32018-10-04 11:45:49 +0100368 classpath: []string{"ext", "framework"},
Colin Cross2acdae82017-09-15 19:44:24 -0700369 },
370 {
371 name: "blank sdk version",
372 properties: `sdk_version: "",`,
Neil Fullerbf210f72018-10-09 16:19:19 +0100373 bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
374 system: "core-platform-api-stubs-system-modules",
Neil Fuller1fee9f32018-10-04 11:45:49 +0100375 classpath: []string{"ext", "framework"},
Colin Cross2acdae82017-09-15 19:44:24 -0700376 },
377 {
Colin Cross4c428df2017-09-15 17:36:05 -0700378
Colin Cross2acdae82017-09-15 19:44:24 -0700379 name: "sdk v14",
380 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700381 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700382 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross86a60ae2018-05-29 14:44:55 -0700383 classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700384 },
385 {
Colin Cross72bb3632017-07-13 16:23:21 -0700386
Colin Cross2acdae82017-09-15 19:44:24 -0700387 name: "current",
388 properties: `sdk_version: "current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700389 bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700390 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700391 },
392 {
Colin Cross72bb3632017-07-13 16:23:21 -0700393
Colin Cross2acdae82017-09-15 19:44:24 -0700394 name: "system_current",
395 properties: `sdk_version: "system_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700396 bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700397 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700398 },
399 {
Colin Cross72bb3632017-07-13 16:23:21 -0700400
Sundong Ahn0926fae2017-10-17 16:34:51 +0900401 name: "system_14",
402 properties: `sdk_version: "system_14",`,
403 bootclasspath: []string{`""`},
404 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross86a60ae2018-05-29 14:44:55 -0700405 classpath: []string{"prebuilts/sdk/14/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Sundong Ahn0926fae2017-10-17 16:34:51 +0900406 },
407 {
408
Colin Cross2acdae82017-09-15 19:44:24 -0700409 name: "test_current",
410 properties: `sdk_version: "test_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700411 bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700412 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700413 },
414 {
Colin Cross72bb3632017-07-13 16:23:21 -0700415
Jiyong Park750e5572018-01-31 00:20:13 +0900416 name: "core_current",
417 properties: `sdk_version: "core_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700418 bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
Jiyong Park750e5572018-01-31 00:20:13 +0900419 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Jiyong Park750e5572018-01-31 00:20:13 +0900420 },
421 {
422
Colin Cross2acdae82017-09-15 19:44:24 -0700423 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700424 properties: `no_standard_libs: true, system_modules: "none"`,
425 system: "none",
426 bootclasspath: []string{`""`},
427 classpath: []string{},
428 },
429 {
430
431 name: "nostdlib system_modules",
Neil Fullerbf210f72018-10-09 16:19:19 +0100432 properties: `no_standard_libs: true, system_modules: "core-platform-api-stubs-system-modules"`,
433 system: "core-platform-api-stubs-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700434 bootclasspath: []string{`""`},
435 classpath: []string{},
436 },
437 {
Colin Cross72bb3632017-07-13 16:23:21 -0700438
Colin Cross7fdd2b72018-01-02 18:14:25 -0800439 name: "host default",
440 moduleType: "java_library_host",
441 properties: ``,
442 host: android.Host,
443 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
444 classpath: []string{},
Colin Cross2acdae82017-09-15 19:44:24 -0700445 },
446 {
447 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700448 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700449 host: android.Host,
450 properties: `no_standard_libs: true`,
451 classpath: []string{},
452 },
Colin Cross965714f2017-10-03 20:57:01 -0700453 {
454
Colin Cross7fdd2b72018-01-02 18:14:25 -0800455 name: "host supported default",
456 host: android.Host,
457 properties: `host_supported: true,`,
458 classpath: []string{},
459 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
Colin Cross965714f2017-10-03 20:57:01 -0700460 },
461 {
462 name: "host supported nostdlib",
463 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700464 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700465 classpath: []string{},
466 },
Colin Cross86a60ae2018-05-29 14:44:55 -0700467 {
468
469 name: "unbundled sdk v14",
470 unbundled: true,
471 properties: `sdk_version: "14",`,
472 bootclasspath: []string{`""`},
473 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
474 classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
475 },
476 {
477
478 name: "unbundled current",
479 unbundled: true,
480 properties: `sdk_version: "current",`,
481 bootclasspath: []string{`""`},
482 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
483 classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
484 },
Colin Cross2acdae82017-09-15 19:44:24 -0700485}
Colin Cross72bb3632017-07-13 16:23:21 -0700486
Colin Cross2acdae82017-09-15 19:44:24 -0700487func TestClasspath(t *testing.T) {
488 for _, testcase := range classpathTestcases {
489 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700490 moduleType := "java_library"
491 if testcase.moduleType != "" {
492 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700493 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700494
495 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700496 name: "foo",
497 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700498 ` + testcase.properties + `
499 }`
500
501 variant := "android_common"
502 if testcase.host == android.Host {
503 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700504 }
Colin Cross72bb3632017-07-13 16:23:21 -0700505
Colin Cross2acdae82017-09-15 19:44:24 -0700506 convertModulesToPaths := func(cp []string) []string {
507 ret := make([]string, len(cp))
508 for i, e := range cp {
509 ret[i] = moduleToPath(e)
510 }
511 return ret
512 }
Colin Cross72bb3632017-07-13 16:23:21 -0700513
Colin Cross2acdae82017-09-15 19:44:24 -0700514 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
515 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700516
Colin Cross2acdae82017-09-15 19:44:24 -0700517 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700518 if bc != "" {
519 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700520 }
Colin Cross72bb3632017-07-13 16:23:21 -0700521
Colin Cross2acdae82017-09-15 19:44:24 -0700522 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700523 if c != "" {
524 c = "-classpath " + c
525 }
526 system := ""
527 if testcase.system == "none" {
528 system = "--system=none"
529 } else if testcase.system != "" {
530 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700531 }
532
Colin Cross1369cdb2017-09-29 17:58:17 -0700533 t.Run("1.8", func(t *testing.T) {
534 // Test default javac 1.8
Colin Cross86a60ae2018-05-29 14:44:55 -0700535 config := testConfig(nil)
536 if testcase.unbundled {
537 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
538 }
539 ctx := testContext(config, bp, nil)
540 run(t, ctx, config)
Colin Cross2acdae82017-09-15 19:44:24 -0700541
Colin Cross1369cdb2017-09-29 17:58:17 -0700542 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
543
544 got := javac.Args["bootClasspath"]
545 if got != bc {
546 t.Errorf("bootclasspath expected %q != got %q", bc, got)
547 }
548
549 got = javac.Args["classpath"]
550 if got != c {
551 t.Errorf("classpath expected %q != got %q", c, got)
552 }
553
554 var deps []string
555 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
556 deps = append(deps, bootclasspath...)
557 }
558 deps = append(deps, classpath...)
559
560 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
561 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
562 }
563 })
564
565 // Test again with javac 1.9
566 t.Run("1.9", func(t *testing.T) {
Colin Cross527012a2017-11-30 22:56:16 -0800567 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
Colin Cross86a60ae2018-05-29 14:44:55 -0700568 if testcase.unbundled {
569 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
570 }
Colin Cross527012a2017-11-30 22:56:16 -0800571 ctx := testContext(config, bp, nil)
572 run(t, ctx, config)
Colin Cross1369cdb2017-09-29 17:58:17 -0700573
574 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
575 got := javac.Args["bootClasspath"]
576 expected := system
577 if testcase.system == "bootclasspath" {
578 expected = bc
579 }
580 if got != expected {
581 t.Errorf("bootclasspath expected %q != got %q", expected, got)
582 }
583 })
Colin Cross2acdae82017-09-15 19:44:24 -0700584 })
Colin Cross72bb3632017-07-13 16:23:21 -0700585 }
586
Colin Cross72bb3632017-07-13 16:23:21 -0700587}
588
589func TestPrebuilts(t *testing.T) {
590 ctx := testJava(t, `
591 java_library {
592 name: "foo",
593 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700594 libs: ["bar"],
595 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700596 }
597
Colin Cross74d73e22017-08-02 11:05:49 -0700598 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700599 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700600 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700601 }
602
Colin Cross74d73e22017-08-02 11:05:49 -0700603 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700604 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700605 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700606 }
607 `)
608
Colin Cross4c428df2017-09-15 17:36:05 -0700609 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700610 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700611 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
612 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700613
Colin Cross37f6d792018-07-12 12:28:41 -0700614 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
615 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700616 }
617
Colin Cross37f6d792018-07-12 12:28:41 -0700618 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
619 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700620 }
621}
622
Colin Cross89536d42017-07-07 14:35:50 -0700623func TestDefaults(t *testing.T) {
624 ctx := testJava(t, `
625 java_defaults {
626 name: "defaults",
627 srcs: ["a.java"],
628 libs: ["bar"],
629 static_libs: ["baz"],
630 }
631
632 java_library {
633 name: "foo",
634 defaults: ["defaults"],
635 }
636
637 java_library {
638 name: "bar",
639 srcs: ["b.java"],
640 }
641
642 java_library {
643 name: "baz",
644 srcs: ["c.java"],
645 }
646 `)
647
Colin Cross4c428df2017-09-15 17:36:05 -0700648 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700649 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700650
651 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
652 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
653 }
654
Nan Zhanged19fc32017-10-19 13:06:22 -0700655 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
656 if !strings.Contains(javac.Args["classpath"], barTurbine) {
657 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700658 }
659
Colin Cross1ee23172017-10-18 14:44:18 -0700660 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700661 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
662 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700663 }
664}
665
Colin Cross0f37af02017-09-27 17:42:05 -0700666func TestResources(t *testing.T) {
667 var table = []struct {
668 name string
669 prop string
670 extra string
671 args string
672 }{
673 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700674 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700675 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800676 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700677 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700678 },
679 {
680 // Test that a module with java_resources includes the files
681 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700682 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
683 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700684 },
685 {
686 // Test that a module with a filegroup in java_resources includes the files with the
687 // path prefix
688 name: "resource filegroup",
689 prop: `java_resources: [":foo-res"]`,
690 extra: `
691 filegroup {
692 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800693 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700694 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700695 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700696 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700697 },
698 {
699 // Test that a module with "include_srcs: true" includes its source files in the resources jar
700 name: "include sources",
701 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700702 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700703 },
Colin Cross0ead1d72018-04-10 13:07:42 -0700704 {
705 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
706 name: "wildcard dirs",
707 prop: `java_resource_dirs: ["java-res/*"]`,
708 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
709 },
710 {
711 // Test that a module exclude_java_resource_dirs excludes the files
712 name: "wildcard dirs",
713 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
714 args: "-C java-res/a -f java-res/a/a",
715 },
Colin Crosscedd4762018-09-13 11:26:19 -0700716 {
717 // Test wildcards in java_resources
718 name: "wildcard files",
719 prop: `java_resources: ["java-res/**/*"]`,
720 args: "-C . -f java-res/a/a -f java-res/b/b",
721 },
722 {
723 // Test exclude_java_resources with java_resources
724 name: "wildcard files with exclude",
725 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
726 args: "-C . -f java-res/a/a",
727 },
728 {
729 // Test exclude_java_resources with java_resource_dirs
730 name: "resource dirs with exclude files",
731 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
732 args: "-C java-res -f java-res/a/a",
733 },
734 {
735 // Test exclude_java_resource_dirs with java_resource_dirs
736 name: "resource dirs with exclude files",
737 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
738 args: "-C java-res -f java-res/a/a -f java-res/b/b",
739 },
Colin Cross0f37af02017-09-27 17:42:05 -0700740 }
741
742 for _, test := range table {
743 t.Run(test.name, func(t *testing.T) {
744 ctx := testJava(t, `
745 java_library {
746 name: "foo",
747 srcs: [
748 "a.java",
749 "b.java",
750 "c.java",
751 ],
752 `+test.prop+`,
753 }
754 `+test.extra)
755
Colin Cross331a1212018-08-15 20:40:52 -0700756 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700757 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700758
759 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
760 t.Errorf("foo combined jars %v does not contain %q",
761 foo.Inputs.Strings(), fooRes.Output.String())
762 }
763
Colin Crossaf9c55b2017-10-03 14:50:08 -0700764 if fooRes.Args["jarArgs"] != test.args {
765 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700766 fooRes.Args["jarArgs"], test.args)
767 }
768 })
769 }
770}
771
Colin Cross54190b32017-10-09 15:34:10 -0700772func TestGeneratedSources(t *testing.T) {
773 ctx := testJava(t, `
774 java_library {
775 name: "foo",
776 srcs: [
777 "a*.java",
778 ":gen",
779 "b*.java",
780 ],
781 }
782
783 genrule {
784 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800785 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700786 out: ["gen.java"],
787 }
788 `)
789
790 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
791 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
792
Colin Cross15e86d92017-10-20 15:07:08 -0700793 if filepath.Base(genrule.Output.String()) != "gen.java" {
794 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700795 }
796
797 if len(javac.Inputs) != 3 ||
798 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700799 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700800 javac.Inputs[2].String() != "b.java" {
801 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
802 }
803}
804
Colin Cross93e85952017-08-15 13:34:18 -0700805func TestKotlin(t *testing.T) {
806 ctx := testJava(t, `
807 java_library {
808 name: "foo",
Colin Cross9bc43432017-12-15 20:20:39 -0800809 srcs: ["a.java", "b.kt"],
Colin Cross93e85952017-08-15 13:34:18 -0700810 }
Colin Cross715d7112017-10-25 20:32:18 -0700811
812 java_library {
813 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800814 srcs: ["b.kt"],
815 libs: ["foo"],
816 static_libs: ["baz"],
817 }
818
819 java_library {
820 name: "baz",
821 srcs: ["c.java"],
Colin Cross715d7112017-10-25 20:32:18 -0700822 }
Colin Cross93e85952017-08-15 13:34:18 -0700823 `)
824
Colin Cross9bc43432017-12-15 20:20:39 -0800825 fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
826 fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
827 fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700828
Colin Cross9bc43432017-12-15 20:20:39 -0800829 if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
830 fooKotlinc.Inputs[1].String() != "b.kt" {
831 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700832 }
833
Colin Cross9bc43432017-12-15 20:20:39 -0800834 if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
835 t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700836 }
837
Colin Cross9bc43432017-12-15 20:20:39 -0800838 if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
Colin Cross93e85952017-08-15 13:34:18 -0700839 t.Errorf("foo classpath %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800840 fooJavac.Args["classpath"], fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700841 }
842
Colin Cross9bc43432017-12-15 20:20:39 -0800843 if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
Colin Cross93e85952017-08-15 13:34:18 -0700844 t.Errorf("foo jar inputs %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800845 fooJar.Inputs.Strings(), fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700846 }
Colin Cross715d7112017-10-25 20:32:18 -0700847
Colin Cross9bc43432017-12-15 20:20:39 -0800848 fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
849 bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
850 barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
Colin Cross715d7112017-10-25 20:32:18 -0700851
Colin Cross9bc43432017-12-15 20:20:39 -0800852 if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
853 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
854 }
855
856 if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
857 t.Errorf(`expected %q in bar implicits %v`,
858 fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
859 }
860
861 if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
862 t.Errorf(`expected %q in bar implicits %v`,
863 bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
Colin Cross715d7112017-10-25 20:32:18 -0700864 }
Colin Cross93e85952017-08-15 13:34:18 -0700865}
866
Nan Zhang61eaedb2017-11-02 13:28:15 -0700867func TestTurbine(t *testing.T) {
868 ctx := testJava(t, `
869 java_library {
870 name: "foo",
871 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900872 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700873 }
874
875 java_library {
876 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800877 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700878 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900879 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700880 }
881
882 java_library {
883 name: "baz",
884 srcs: ["c.java"],
885 libs: ["bar"],
886 sdk_version: "14",
887 }
888 `)
889
890 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
891 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
892 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
893 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
894 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
895
896 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
897 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
898 }
899
900 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
901 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
902 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
903 }
904 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
905 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
906 }
907 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
908 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
909 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100910 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700911 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100912 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700913 }
914}
915
916func TestSharding(t *testing.T) {
917 ctx := testJava(t, `
918 java_library {
919 name: "bar",
920 srcs: ["a.java","b.java","c.java"],
921 javac_shard_size: 1
922 }
923 `)
924
925 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
926 for i := 0; i < 3; i++ {
927 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
928 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
929 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
930 }
931 }
932}
933
Nan Zhang581fd212018-01-10 16:06:12 -0800934func TestDroiddoc(t *testing.T) {
935 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800936 droiddoc_template {
937 name: "droiddoc-templates-sdk",
938 path: ".",
939 }
Nan Zhang581fd212018-01-10 16:06:12 -0800940 droiddoc {
941 name: "bar-doc",
942 srcs: [
943 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900944 "bar-doc/IFoo.aidl",
Nan Zhang581fd212018-01-10 16:06:12 -0800945 ],
946 exclude_srcs: [
947 "bar-doc/b.java"
948 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800949 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800950 hdf: [
951 "android.whichdoc offline",
952 ],
953 knowntags: [
954 "bar-doc/known_oj_tags.txt",
955 ],
956 proofread_file: "libcore-proofread.txt",
957 todo_file: "libcore-docs-todo.html",
958 args: "-offlinemode -title \"libcore\"",
959 }
960 `)
961
Nan Zhang3bb14202018-02-28 10:14:25 -0800962 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
963 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800964 if stubsJar != barDoc.Output.String() {
965 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
966 }
Jiyong Park1e440682018-05-23 18:42:04 +0900967 inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
968 var javaSrcs []string
969 for _, i := range inputs {
970 javaSrcs = append(javaSrcs, i.Base())
971 }
972 if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
973 t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
974 }
Nan Zhang581fd212018-01-10 16:06:12 -0800975}
976
Colin Cross54250902017-12-05 09:28:08 -0800977func TestJarGenrules(t *testing.T) {
978 ctx := testJava(t, `
979 java_library {
980 name: "foo",
981 srcs: ["a.java"],
982 }
983
984 java_genrule {
985 name: "jargen",
986 tool_files: ["b.java"],
987 cmd: "$(location b.java) $(in) $(out)",
988 out: ["jargen.jar"],
989 srcs: [":foo"],
990 }
991
992 java_library {
993 name: "bar",
994 static_libs: ["jargen"],
995 srcs: ["c.java"],
996 }
997
998 java_library {
999 name: "baz",
1000 libs: ["jargen"],
1001 srcs: ["c.java"],
1002 }
1003 `)
1004
1005 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1006 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1007 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1008 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1009 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1010
1011 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1012 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1013 }
1014
1015 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1016 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1017 }
1018
1019 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1020 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1021 }
1022
1023 if len(barCombined.Inputs) != 2 ||
1024 barCombined.Inputs[0].String() != bar.Output.String() ||
1025 barCombined.Inputs[1].String() != jargen.Output.String() {
1026 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1027 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1028 }
1029}
1030
Nan Zhang27e284d2018-02-09 21:03:53 +00001031func TestExcludeFileGroupInSrcs(t *testing.T) {
1032 ctx := testJava(t, `
1033 java_library {
1034 name: "foo",
1035 srcs: ["a.java", ":foo-srcs"],
1036 exclude_srcs: ["a.java", ":foo-excludes"],
1037 }
1038
1039 filegroup {
1040 name: "foo-srcs",
1041 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1042 }
1043
1044 filegroup {
1045 name: "foo-excludes",
1046 srcs: ["java-fg/a.java", "java-fg/b.java"],
1047 }
1048 `)
1049
1050 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1051
1052 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1053 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1054 }
1055}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001056
1057func TestJavaSdkLibrary(t *testing.T) {
1058 ctx := testJava(t, `
1059 droiddoc_template {
1060 name: "droiddoc-templates-sdk",
1061 path: ".",
1062 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001063 java_sdk_library {
1064 name: "foo",
1065 srcs: ["a.java", "b.java"],
1066 api_packages: ["foo"],
1067 }
1068 java_sdk_library {
1069 name: "bar",
1070 srcs: ["a.java", "b.java"],
1071 api_packages: ["bar"],
1072 }
1073 java_library {
1074 name: "baz",
1075 srcs: ["c.java"],
1076 libs: ["foo", "bar"],
1077 sdk_version: "system_current",
1078 }
Jiyong Park1be96912018-05-28 18:02:19 +09001079 java_library {
1080 name: "qux",
1081 srcs: ["c.java"],
1082 libs: ["baz"],
1083 sdk_version: "system_current",
1084 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001085 `)
1086
1087 // check the existence of the internal modules
1088 ctx.ModuleForTests("foo", "android_common")
1089 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
1090 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001091 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001092 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
1093 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001094 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001095 ctx.ModuleForTests("foo"+sdkImplLibrarySuffix, "android_common")
Jiyong Park5a8d1be2018-04-25 22:57:34 +09001096 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001097 ctx.ModuleForTests("foo.api.public.28", "")
1098 ctx.ModuleForTests("foo.api.system.28", "")
1099 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001100
1101 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1102 // tests if baz is actually linked to the stubs lib
1103 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1104 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1105 "foo.stubs.system.jar")
1106 }
1107 // ... and not to the impl lib
1108 if strings.Contains(bazJavac.Args["classpath"], "foo.impl.jar") {
1109 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1110 "foo.impl.jar")
1111 }
1112 // test if baz is not linked to the system variant of foo
1113 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1114 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1115 "foo.stubs.jar")
1116 }
Jiyong Park1be96912018-05-28 18:02:19 +09001117
1118 // test if baz has exported SDK lib names foo and bar to qux
1119 qux := ctx.ModuleForTests("qux", "android_common")
1120 if quxLib, ok := qux.Module().(*Library); ok {
1121 sdkLibs := quxLib.ExportedSdkLibs()
1122 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
1123 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
1124 }
1125 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001126}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001127
1128var compilerFlagsTestCases = []struct {
1129 in string
1130 out bool
1131}{
1132 {
1133 in: "a",
1134 out: false,
1135 },
1136 {
1137 in: "-a",
1138 out: true,
1139 },
1140 {
1141 in: "-no-jdk",
1142 out: false,
1143 },
1144 {
1145 in: "-no-stdlib",
1146 out: false,
1147 },
1148 {
1149 in: "-kotlin-home",
1150 out: false,
1151 },
1152 {
1153 in: "-kotlin-home /some/path",
1154 out: false,
1155 },
1156 {
1157 in: "-include-runtime",
1158 out: false,
1159 },
1160 {
1161 in: "-Xintellij-plugin-root",
1162 out: false,
1163 },
1164}
1165
1166type mockContext struct {
1167 android.ModuleContext
1168 result bool
1169}
1170
1171func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1172 // CheckBadCompilerFlags calls this function when the flag should be rejected
1173 ctx.result = false
1174}
1175
1176func TestCompilerFlags(t *testing.T) {
1177 for _, testCase := range compilerFlagsTestCases {
1178 ctx := &mockContext{result: true}
1179 CheckKotlincFlags(ctx, []string{testCase.in})
1180 if ctx.result != testCase.out {
1181 t.Errorf("incorrect output:")
1182 t.Errorf(" input: %#v", testCase.in)
1183 t.Errorf(" expected: %#v", testCase.out)
1184 t.Errorf(" got: %#v", ctx.result)
1185 }
1186 }
1187}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001188
1189// TODO(jungjw): Consider making this more robust by ignoring path order.
1190func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1191 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1192 flags := strings.Split(variables["javacFlags"], " ")
1193 got := ""
1194 for _, flag := range flags {
1195 keyEnd := strings.Index(flag, "=")
1196 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1197 got = flag[keyEnd+1:]
1198 break
1199 }
1200 }
1201 if expected != got {
1202 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1203 }
1204}
1205
1206func TestPatchModule(t *testing.T) {
1207 bp := `
1208 java_library {
1209 name: "foo",
1210 srcs: ["a.java"],
1211 }
1212
1213 java_library {
1214 name: "bar",
1215 srcs: ["b.java"],
1216 no_standard_libs: true,
1217 system_modules: "none",
1218 patch_module: "java.base",
1219 }
1220
1221 java_library {
1222 name: "baz",
1223 srcs: ["c.java"],
1224 patch_module: "java.base",
1225 }
1226 `
1227
1228 t.Run("1.8", func(t *testing.T) {
1229 // Test default javac 1.8
1230 ctx := testJava(t, bp)
1231
1232 checkPatchModuleFlag(t, ctx, "foo", "")
1233 checkPatchModuleFlag(t, ctx, "bar", "")
1234 checkPatchModuleFlag(t, ctx, "baz", "")
1235 })
1236
1237 t.Run("1.9", func(t *testing.T) {
1238 // Test again with javac 1.9
1239 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
1240 ctx := testContext(config, bp, nil)
1241 run(t, ctx, config)
1242
1243 checkPatchModuleFlag(t, ctx, "foo", "")
1244 expected := "java.base=.:" + buildDir
1245 checkPatchModuleFlag(t, ctx, "bar", expected)
1246 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
1247 checkPatchModuleFlag(t, ctx, "baz", expected)
1248 })
1249}