blob: 1bfd24bbceb642049ab064d3e0099ad8e482620b [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{
114 "core-oj",
115 "core-libart",
Colin Cross86a60ae2018-05-29 14:44:55 -0700116 "core-lambda-stubs",
Neil Fuller60952592018-08-24 14:45:18 +0100117 "core-simple",
Colin Cross4c428df2017-09-15 17:36:05 -0700118 "framework",
119 "ext",
120 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -0700121 "android_stubs_current",
122 "android_system_stubs_current",
123 "android_test_stubs_current",
Colin Crossf19b9bb2018-03-26 14:42:44 -0700124 "core.current.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",
Colin Cross1369cdb2017-09-29 17:58:17 -0700135 system_modules: "core-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",
149 "android_stubs_current_system_modules",
150 "android_system_stubs_current_system_modules",
151 "android_test_stubs_current_system_modules",
152 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700153
Colin Cross527012a2017-11-30 22:56:16 -0800154 for _, extra := range systemModules {
155 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700156 java_system_modules {
157 name: "%s",
158 }
159 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700160 }
161
Colin Cross824bee32017-11-22 17:27:51 -0800162 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900163 "Android.bp": []byte(bp),
164 "a.java": nil,
165 "b.java": nil,
166 "c.java": nil,
167 "b.kt": nil,
168 "a.jar": nil,
169 "b.jar": nil,
170 "java-res/a/a": nil,
171 "java-res/b/b": nil,
172 "java-res2/a": nil,
173 "java-fg/a.java": nil,
174 "java-fg/b.java": nil,
175 "java-fg/c.java": nil,
176 "api/current.txt": nil,
177 "api/removed.txt": nil,
178 "api/system-current.txt": nil,
179 "api/system-removed.txt": nil,
180 "api/test-current.txt": nil,
181 "api/test-removed.txt": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700182
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100183 "prebuilts/sdk/14/public/android.jar": nil,
184 "prebuilts/sdk/14/public/framework.aidl": nil,
185 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700186 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100187 "prebuilts/sdk/current/public/android.jar": nil,
188 "prebuilts/sdk/current/public/framework.aidl": nil,
189 "prebuilts/sdk/current/public/core.jar": nil,
190 "prebuilts/sdk/current/system/android.jar": nil,
191 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900192 "prebuilts/sdk/28/public/api/foo.txt": nil,
193 "prebuilts/sdk/28/system/api/foo.txt": nil,
194 "prebuilts/sdk/28/test/api/foo.txt": nil,
195 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
196 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
197 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
198 "prebuilts/sdk/28/public/api/bar.txt": nil,
199 "prebuilts/sdk/28/system/api/bar.txt": nil,
200 "prebuilts/sdk/28/test/api/bar.txt": nil,
201 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
202 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
203 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700204 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900205 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800206
207 // For framework-res, which is an implicit dependency for framework
208 "AndroidManifest.xml": nil,
209 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800210
211 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800212
Colin Cross66dbc0b2017-12-28 12:23:20 -0800213 "build/make/core/proguard.flags": nil,
214 "build/make/core/proguard_basic_keeps.flags": nil,
215
Colin Cross7fdd2b72018-01-02 18:14:25 -0800216 "jdk8/jre/lib/jce.jar": nil,
217 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700218 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800219
220 "bar-doc/a.java": nil,
221 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900222 "bar-doc/IFoo.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800223 "bar-doc/known_oj_tags.txt": nil,
224 "external/doclava/templates-sdk": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800225 }
226
227 for k, v := range fs {
228 mockFS[k] = v
229 }
230
231 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700232
Colin Cross527012a2017-11-30 22:56:16 -0800233 return ctx
234}
235
236func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800237 t.Helper()
Jiyong Park58c518b2018-05-12 22:29:12 +0900238 _, errs := ctx.ParseFileList(".", []string{"Android.bp", "prebuilts/sdk/Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800239 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700240 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800241 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800242}
243
244func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800245 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800246 config := testConfig(nil)
247 ctx := testContext(config, bp, nil)
248 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700249
250 return ctx
251}
252
Colin Cross2acdae82017-09-15 19:44:24 -0700253func moduleToPath(name string) string {
254 switch {
255 case name == `""`:
256 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700257 case strings.HasSuffix(name, ".jar"):
258 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700259 default:
260 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700261 }
262}
263
Colin Cross72bb3632017-07-13 16:23:21 -0700264func TestSimple(t *testing.T) {
265 ctx := testJava(t, `
266 java_library {
267 name: "foo",
268 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700269 libs: ["bar"],
270 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700271 }
272
273 java_library {
274 name: "bar",
275 srcs: ["b.java"],
276 }
277
278 java_library {
279 name: "baz",
280 srcs: ["c.java"],
281 }
Colin Crossd5934c82017-10-02 13:55:26 -0700282 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700283
Colin Cross4c428df2017-09-15 17:36:05 -0700284 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700285 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700286
287 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
288 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
289 }
290
Colin Cross1ee23172017-10-18 14:44:18 -0700291 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700292 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
293 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700294
Nan Zhanged19fc32017-10-19 13:06:22 -0700295 if !strings.Contains(javac.Args["classpath"], barTurbine) {
296 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700297 }
298
Nan Zhanged19fc32017-10-19 13:06:22 -0700299 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
300 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700301 }
302
303 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
304 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700305 }
306}
307
Colin Crossd5934c82017-10-02 13:55:26 -0700308func TestArchSpecific(t *testing.T) {
309 ctx := testJava(t, `
310 java_library {
311 name: "foo",
312 srcs: ["a.java"],
313 target: {
314 android: {
315 srcs: ["b.java"],
316 },
317 },
318 }
319 `)
320
321 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
322 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
323 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
324 }
325}
326
Colin Cross6b4a32d2017-12-05 13:42:45 -0800327func TestBinary(t *testing.T) {
328 ctx := testJava(t, `
329 java_library_host {
330 name: "foo",
331 srcs: ["a.java"],
332 }
333
334 java_binary_host {
335 name: "bar",
336 srcs: ["b.java"],
337 static_libs: ["foo"],
338 }
339 `)
340
341 buildOS := android.BuildOs.String()
342
343 bar := ctx.ModuleForTests("bar", buildOS+"_common")
344 barJar := bar.Output("bar.jar").Output.String()
345 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
346 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
347
348 // Test that the install binary wrapper depends on the installed jar file
349 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
350 t.Errorf("expected binary wrapper implicits [%q], got %v",
351 barJar, barWrapperDeps)
352 }
353
354}
355
Colin Cross2acdae82017-09-15 19:44:24 -0700356var classpathTestcases = []struct {
357 name string
Colin Cross86a60ae2018-05-29 14:44:55 -0700358 unbundled bool
Colin Cross965714f2017-10-03 20:57:01 -0700359 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700360 host android.OsClass
361 properties string
362 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700363 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700364 classpath []string
365}{
366 {
367 name: "default",
Neil Fuller60952592018-08-24 14:45:18 +0100368 bootclasspath: []string{"core-oj", "core-libart", "core-simple"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700369 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700370 classpath: []string{"ext", "framework", "okhttp"},
371 },
372 {
373 name: "blank sdk version",
374 properties: `sdk_version: "",`,
Neil Fuller60952592018-08-24 14:45:18 +0100375 bootclasspath: []string{"core-oj", "core-libart", "core-simple"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700376 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700377 classpath: []string{"ext", "framework", "okhttp"},
378 },
379 {
Colin Cross4c428df2017-09-15 17:36:05 -0700380
Colin Cross2acdae82017-09-15 19:44:24 -0700381 name: "sdk v14",
382 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700383 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700384 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross86a60ae2018-05-29 14:44:55 -0700385 classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700386 },
387 {
Colin Cross72bb3632017-07-13 16:23:21 -0700388
Colin Cross2acdae82017-09-15 19:44:24 -0700389 name: "current",
390 properties: `sdk_version: "current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700391 bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700392 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700393 },
394 {
Colin Cross72bb3632017-07-13 16:23:21 -0700395
Colin Cross2acdae82017-09-15 19:44:24 -0700396 name: "system_current",
397 properties: `sdk_version: "system_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700398 bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700399 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700400 },
401 {
Colin Cross72bb3632017-07-13 16:23:21 -0700402
Sundong Ahn0926fae2017-10-17 16:34:51 +0900403 name: "system_14",
404 properties: `sdk_version: "system_14",`,
405 bootclasspath: []string{`""`},
406 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross86a60ae2018-05-29 14:44:55 -0700407 classpath: []string{"prebuilts/sdk/14/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Sundong Ahn0926fae2017-10-17 16:34:51 +0900408 },
409 {
410
Colin Cross2acdae82017-09-15 19:44:24 -0700411 name: "test_current",
412 properties: `sdk_version: "test_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700413 bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700414 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700415 },
416 {
Colin Cross72bb3632017-07-13 16:23:21 -0700417
Jiyong Park750e5572018-01-31 00:20:13 +0900418 name: "core_current",
419 properties: `sdk_version: "core_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700420 bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
Jiyong Park750e5572018-01-31 00:20:13 +0900421 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Jiyong Park750e5572018-01-31 00:20:13 +0900422 },
423 {
424
Colin Cross2acdae82017-09-15 19:44:24 -0700425 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700426 properties: `no_standard_libs: true, system_modules: "none"`,
427 system: "none",
428 bootclasspath: []string{`""`},
429 classpath: []string{},
430 },
431 {
432
433 name: "nostdlib system_modules",
434 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
435 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700436 bootclasspath: []string{`""`},
437 classpath: []string{},
438 },
439 {
Colin Cross72bb3632017-07-13 16:23:21 -0700440
Colin Cross7fdd2b72018-01-02 18:14:25 -0800441 name: "host default",
442 moduleType: "java_library_host",
443 properties: ``,
444 host: android.Host,
445 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
446 classpath: []string{},
Colin Cross2acdae82017-09-15 19:44:24 -0700447 },
448 {
449 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700450 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700451 host: android.Host,
452 properties: `no_standard_libs: true`,
453 classpath: []string{},
454 },
Colin Cross965714f2017-10-03 20:57:01 -0700455 {
456
Colin Cross7fdd2b72018-01-02 18:14:25 -0800457 name: "host supported default",
458 host: android.Host,
459 properties: `host_supported: true,`,
460 classpath: []string{},
461 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
Colin Cross965714f2017-10-03 20:57:01 -0700462 },
463 {
464 name: "host supported nostdlib",
465 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700466 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700467 classpath: []string{},
468 },
Colin Cross86a60ae2018-05-29 14:44:55 -0700469 {
470
471 name: "unbundled sdk v14",
472 unbundled: true,
473 properties: `sdk_version: "14",`,
474 bootclasspath: []string{`""`},
475 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
476 classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
477 },
478 {
479
480 name: "unbundled current",
481 unbundled: true,
482 properties: `sdk_version: "current",`,
483 bootclasspath: []string{`""`},
484 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
485 classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
486 },
Colin Cross2acdae82017-09-15 19:44:24 -0700487}
Colin Cross72bb3632017-07-13 16:23:21 -0700488
Colin Cross2acdae82017-09-15 19:44:24 -0700489func TestClasspath(t *testing.T) {
490 for _, testcase := range classpathTestcases {
491 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700492 moduleType := "java_library"
493 if testcase.moduleType != "" {
494 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700495 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700496
497 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700498 name: "foo",
499 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700500 ` + testcase.properties + `
501 }`
502
503 variant := "android_common"
504 if testcase.host == android.Host {
505 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700506 }
Colin Cross72bb3632017-07-13 16:23:21 -0700507
Colin Cross2acdae82017-09-15 19:44:24 -0700508 convertModulesToPaths := func(cp []string) []string {
509 ret := make([]string, len(cp))
510 for i, e := range cp {
511 ret[i] = moduleToPath(e)
512 }
513 return ret
514 }
Colin Cross72bb3632017-07-13 16:23:21 -0700515
Colin Cross2acdae82017-09-15 19:44:24 -0700516 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
517 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700518
Colin Cross2acdae82017-09-15 19:44:24 -0700519 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700520 if bc != "" {
521 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700522 }
Colin Cross72bb3632017-07-13 16:23:21 -0700523
Colin Cross2acdae82017-09-15 19:44:24 -0700524 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700525 if c != "" {
526 c = "-classpath " + c
527 }
528 system := ""
529 if testcase.system == "none" {
530 system = "--system=none"
531 } else if testcase.system != "" {
532 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700533 }
534
Colin Cross1369cdb2017-09-29 17:58:17 -0700535 t.Run("1.8", func(t *testing.T) {
536 // Test default javac 1.8
Colin Cross86a60ae2018-05-29 14:44:55 -0700537 config := testConfig(nil)
538 if testcase.unbundled {
539 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
540 }
541 ctx := testContext(config, bp, nil)
542 run(t, ctx, config)
Colin Cross2acdae82017-09-15 19:44:24 -0700543
Colin Cross1369cdb2017-09-29 17:58:17 -0700544 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
545
546 got := javac.Args["bootClasspath"]
547 if got != bc {
548 t.Errorf("bootclasspath expected %q != got %q", bc, got)
549 }
550
551 got = javac.Args["classpath"]
552 if got != c {
553 t.Errorf("classpath expected %q != got %q", c, got)
554 }
555
556 var deps []string
557 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
558 deps = append(deps, bootclasspath...)
559 }
560 deps = append(deps, classpath...)
561
562 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
563 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
564 }
565 })
566
567 // Test again with javac 1.9
568 t.Run("1.9", func(t *testing.T) {
Colin Cross527012a2017-11-30 22:56:16 -0800569 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
Colin Cross86a60ae2018-05-29 14:44:55 -0700570 if testcase.unbundled {
571 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
572 }
Colin Cross527012a2017-11-30 22:56:16 -0800573 ctx := testContext(config, bp, nil)
574 run(t, ctx, config)
Colin Cross1369cdb2017-09-29 17:58:17 -0700575
576 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
577 got := javac.Args["bootClasspath"]
578 expected := system
579 if testcase.system == "bootclasspath" {
580 expected = bc
581 }
582 if got != expected {
583 t.Errorf("bootclasspath expected %q != got %q", expected, got)
584 }
585 })
Colin Cross2acdae82017-09-15 19:44:24 -0700586 })
Colin Cross72bb3632017-07-13 16:23:21 -0700587 }
588
Colin Cross72bb3632017-07-13 16:23:21 -0700589}
590
591func TestPrebuilts(t *testing.T) {
592 ctx := testJava(t, `
593 java_library {
594 name: "foo",
595 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700596 libs: ["bar"],
597 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700598 }
599
Colin Cross74d73e22017-08-02 11:05:49 -0700600 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700601 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700602 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700603 }
604
Colin Cross74d73e22017-08-02 11:05:49 -0700605 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700606 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700607 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700608 }
609 `)
610
Colin Cross4c428df2017-09-15 17:36:05 -0700611 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700612 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700613 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
614 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700615
Colin Cross37f6d792018-07-12 12:28:41 -0700616 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
617 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700618 }
619
Colin Cross37f6d792018-07-12 12:28:41 -0700620 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
621 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700622 }
623}
624
Colin Cross89536d42017-07-07 14:35:50 -0700625func TestDefaults(t *testing.T) {
626 ctx := testJava(t, `
627 java_defaults {
628 name: "defaults",
629 srcs: ["a.java"],
630 libs: ["bar"],
631 static_libs: ["baz"],
632 }
633
634 java_library {
635 name: "foo",
636 defaults: ["defaults"],
637 }
638
639 java_library {
640 name: "bar",
641 srcs: ["b.java"],
642 }
643
644 java_library {
645 name: "baz",
646 srcs: ["c.java"],
647 }
648 `)
649
Colin Cross4c428df2017-09-15 17:36:05 -0700650 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700651 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700652
653 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
654 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
655 }
656
Nan Zhanged19fc32017-10-19 13:06:22 -0700657 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
658 if !strings.Contains(javac.Args["classpath"], barTurbine) {
659 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700660 }
661
Colin Cross1ee23172017-10-18 14:44:18 -0700662 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700663 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
664 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700665 }
666}
667
Colin Cross0f37af02017-09-27 17:42:05 -0700668func TestResources(t *testing.T) {
669 var table = []struct {
670 name string
671 prop string
672 extra string
673 args string
674 }{
675 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700676 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700677 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800678 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700679 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700680 },
681 {
682 // Test that a module with java_resources includes the files
683 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700684 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
685 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700686 },
687 {
688 // Test that a module with a filegroup in java_resources includes the files with the
689 // path prefix
690 name: "resource filegroup",
691 prop: `java_resources: [":foo-res"]`,
692 extra: `
693 filegroup {
694 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800695 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700696 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700697 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700698 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700699 },
700 {
701 // Test that a module with "include_srcs: true" includes its source files in the resources jar
702 name: "include sources",
703 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700704 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700705 },
Colin Cross0ead1d72018-04-10 13:07:42 -0700706 {
707 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
708 name: "wildcard dirs",
709 prop: `java_resource_dirs: ["java-res/*"]`,
710 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
711 },
712 {
713 // Test that a module exclude_java_resource_dirs excludes the files
714 name: "wildcard dirs",
715 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
716 args: "-C java-res/a -f java-res/a/a",
717 },
Colin Crosscedd4762018-09-13 11:26:19 -0700718 {
719 // Test wildcards in java_resources
720 name: "wildcard files",
721 prop: `java_resources: ["java-res/**/*"]`,
722 args: "-C . -f java-res/a/a -f java-res/b/b",
723 },
724 {
725 // Test exclude_java_resources with java_resources
726 name: "wildcard files with exclude",
727 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
728 args: "-C . -f java-res/a/a",
729 },
730 {
731 // Test exclude_java_resources with java_resource_dirs
732 name: "resource dirs with exclude files",
733 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
734 args: "-C java-res -f java-res/a/a",
735 },
736 {
737 // Test exclude_java_resource_dirs with java_resource_dirs
738 name: "resource dirs with exclude files",
739 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
740 args: "-C java-res -f java-res/a/a -f java-res/b/b",
741 },
Colin Cross0f37af02017-09-27 17:42:05 -0700742 }
743
744 for _, test := range table {
745 t.Run(test.name, func(t *testing.T) {
746 ctx := testJava(t, `
747 java_library {
748 name: "foo",
749 srcs: [
750 "a.java",
751 "b.java",
752 "c.java",
753 ],
754 `+test.prop+`,
755 }
756 `+test.extra)
757
Colin Cross331a1212018-08-15 20:40:52 -0700758 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700759 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700760
761 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
762 t.Errorf("foo combined jars %v does not contain %q",
763 foo.Inputs.Strings(), fooRes.Output.String())
764 }
765
Colin Crossaf9c55b2017-10-03 14:50:08 -0700766 if fooRes.Args["jarArgs"] != test.args {
767 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700768 fooRes.Args["jarArgs"], test.args)
769 }
770 })
771 }
772}
773
Colin Cross54190b32017-10-09 15:34:10 -0700774func TestGeneratedSources(t *testing.T) {
775 ctx := testJava(t, `
776 java_library {
777 name: "foo",
778 srcs: [
779 "a*.java",
780 ":gen",
781 "b*.java",
782 ],
783 }
784
785 genrule {
786 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800787 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700788 out: ["gen.java"],
789 }
790 `)
791
792 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
793 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
794
Colin Cross15e86d92017-10-20 15:07:08 -0700795 if filepath.Base(genrule.Output.String()) != "gen.java" {
796 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700797 }
798
799 if len(javac.Inputs) != 3 ||
800 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700801 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700802 javac.Inputs[2].String() != "b.java" {
803 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
804 }
805}
806
Colin Cross93e85952017-08-15 13:34:18 -0700807func TestKotlin(t *testing.T) {
808 ctx := testJava(t, `
809 java_library {
810 name: "foo",
Colin Cross9bc43432017-12-15 20:20:39 -0800811 srcs: ["a.java", "b.kt"],
Colin Cross93e85952017-08-15 13:34:18 -0700812 }
Colin Cross715d7112017-10-25 20:32:18 -0700813
814 java_library {
815 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800816 srcs: ["b.kt"],
817 libs: ["foo"],
818 static_libs: ["baz"],
819 }
820
821 java_library {
822 name: "baz",
823 srcs: ["c.java"],
Colin Cross715d7112017-10-25 20:32:18 -0700824 }
Colin Cross93e85952017-08-15 13:34:18 -0700825 `)
826
Colin Cross9bc43432017-12-15 20:20:39 -0800827 fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
828 fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
829 fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700830
Colin Cross9bc43432017-12-15 20:20:39 -0800831 if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
832 fooKotlinc.Inputs[1].String() != "b.kt" {
833 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700834 }
835
Colin Cross9bc43432017-12-15 20:20:39 -0800836 if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
837 t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700838 }
839
Colin Cross9bc43432017-12-15 20:20:39 -0800840 if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
Colin Cross93e85952017-08-15 13:34:18 -0700841 t.Errorf("foo classpath %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800842 fooJavac.Args["classpath"], fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700843 }
844
Colin Cross9bc43432017-12-15 20:20:39 -0800845 if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
Colin Cross93e85952017-08-15 13:34:18 -0700846 t.Errorf("foo jar inputs %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800847 fooJar.Inputs.Strings(), fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700848 }
Colin Cross715d7112017-10-25 20:32:18 -0700849
Colin Cross9bc43432017-12-15 20:20:39 -0800850 fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
851 bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
852 barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
Colin Cross715d7112017-10-25 20:32:18 -0700853
Colin Cross9bc43432017-12-15 20:20:39 -0800854 if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
855 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
856 }
857
858 if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
859 t.Errorf(`expected %q in bar implicits %v`,
860 fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
861 }
862
863 if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
864 t.Errorf(`expected %q in bar implicits %v`,
865 bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
Colin Cross715d7112017-10-25 20:32:18 -0700866 }
Colin Cross93e85952017-08-15 13:34:18 -0700867}
868
Nan Zhang61eaedb2017-11-02 13:28:15 -0700869func TestTurbine(t *testing.T) {
870 ctx := testJava(t, `
871 java_library {
872 name: "foo",
873 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900874 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700875 }
876
877 java_library {
878 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800879 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700880 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900881 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700882 }
883
884 java_library {
885 name: "baz",
886 srcs: ["c.java"],
887 libs: ["bar"],
888 sdk_version: "14",
889 }
890 `)
891
892 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
893 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
894 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
895 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
896 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
897
898 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
899 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
900 }
901
902 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
903 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
904 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
905 }
906 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
907 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
908 }
909 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
910 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
911 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100912 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700913 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100914 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700915 }
916}
917
918func TestSharding(t *testing.T) {
919 ctx := testJava(t, `
920 java_library {
921 name: "bar",
922 srcs: ["a.java","b.java","c.java"],
923 javac_shard_size: 1
924 }
925 `)
926
927 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
928 for i := 0; i < 3; i++ {
929 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
930 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
931 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
932 }
933 }
934}
935
Nan Zhang581fd212018-01-10 16:06:12 -0800936func TestDroiddoc(t *testing.T) {
937 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800938 droiddoc_template {
939 name: "droiddoc-templates-sdk",
940 path: ".",
941 }
Nan Zhang581fd212018-01-10 16:06:12 -0800942 droiddoc {
943 name: "bar-doc",
944 srcs: [
945 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900946 "bar-doc/IFoo.aidl",
Nan Zhang581fd212018-01-10 16:06:12 -0800947 ],
948 exclude_srcs: [
949 "bar-doc/b.java"
950 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800951 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800952 hdf: [
953 "android.whichdoc offline",
954 ],
955 knowntags: [
956 "bar-doc/known_oj_tags.txt",
957 ],
958 proofread_file: "libcore-proofread.txt",
959 todo_file: "libcore-docs-todo.html",
960 args: "-offlinemode -title \"libcore\"",
961 }
962 `)
963
Nan Zhang3bb14202018-02-28 10:14:25 -0800964 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
965 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800966 if stubsJar != barDoc.Output.String() {
967 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
968 }
Jiyong Park1e440682018-05-23 18:42:04 +0900969 inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
970 var javaSrcs []string
971 for _, i := range inputs {
972 javaSrcs = append(javaSrcs, i.Base())
973 }
974 if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
975 t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
976 }
Nan Zhang581fd212018-01-10 16:06:12 -0800977}
978
Colin Cross54250902017-12-05 09:28:08 -0800979func TestJarGenrules(t *testing.T) {
980 ctx := testJava(t, `
981 java_library {
982 name: "foo",
983 srcs: ["a.java"],
984 }
985
986 java_genrule {
987 name: "jargen",
988 tool_files: ["b.java"],
989 cmd: "$(location b.java) $(in) $(out)",
990 out: ["jargen.jar"],
991 srcs: [":foo"],
992 }
993
994 java_library {
995 name: "bar",
996 static_libs: ["jargen"],
997 srcs: ["c.java"],
998 }
999
1000 java_library {
1001 name: "baz",
1002 libs: ["jargen"],
1003 srcs: ["c.java"],
1004 }
1005 `)
1006
1007 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1008 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1009 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1010 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1011 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1012
1013 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1014 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1015 }
1016
1017 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1018 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1019 }
1020
1021 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1022 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1023 }
1024
1025 if len(barCombined.Inputs) != 2 ||
1026 barCombined.Inputs[0].String() != bar.Output.String() ||
1027 barCombined.Inputs[1].String() != jargen.Output.String() {
1028 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1029 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1030 }
1031}
1032
Nan Zhang27e284d2018-02-09 21:03:53 +00001033func TestExcludeFileGroupInSrcs(t *testing.T) {
1034 ctx := testJava(t, `
1035 java_library {
1036 name: "foo",
1037 srcs: ["a.java", ":foo-srcs"],
1038 exclude_srcs: ["a.java", ":foo-excludes"],
1039 }
1040
1041 filegroup {
1042 name: "foo-srcs",
1043 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1044 }
1045
1046 filegroup {
1047 name: "foo-excludes",
1048 srcs: ["java-fg/a.java", "java-fg/b.java"],
1049 }
1050 `)
1051
1052 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1053
1054 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1055 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1056 }
1057}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001058
1059func TestJavaSdkLibrary(t *testing.T) {
1060 ctx := testJava(t, `
1061 droiddoc_template {
1062 name: "droiddoc-templates-sdk",
1063 path: ".",
1064 }
1065 java_library {
1066 name: "conscrypt",
1067 }
1068 java_library {
1069 name: "bouncycastle",
1070 }
1071 java_sdk_library {
1072 name: "foo",
1073 srcs: ["a.java", "b.java"],
1074 api_packages: ["foo"],
1075 }
1076 java_sdk_library {
1077 name: "bar",
1078 srcs: ["a.java", "b.java"],
1079 api_packages: ["bar"],
1080 }
1081 java_library {
1082 name: "baz",
1083 srcs: ["c.java"],
1084 libs: ["foo", "bar"],
1085 sdk_version: "system_current",
1086 }
Jiyong Park1be96912018-05-28 18:02:19 +09001087 java_library {
1088 name: "qux",
1089 srcs: ["c.java"],
1090 libs: ["baz"],
1091 sdk_version: "system_current",
1092 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001093 `)
1094
1095 // check the existence of the internal modules
1096 ctx.ModuleForTests("foo", "android_common")
1097 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
1098 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001099 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001100 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
1101 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001102 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001103 ctx.ModuleForTests("foo"+sdkImplLibrarySuffix, "android_common")
Jiyong Park5a8d1be2018-04-25 22:57:34 +09001104 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001105 ctx.ModuleForTests("foo.api.public.28", "")
1106 ctx.ModuleForTests("foo.api.system.28", "")
1107 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001108
1109 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1110 // tests if baz is actually linked to the stubs lib
1111 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1112 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1113 "foo.stubs.system.jar")
1114 }
1115 // ... and not to the impl lib
1116 if strings.Contains(bazJavac.Args["classpath"], "foo.impl.jar") {
1117 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1118 "foo.impl.jar")
1119 }
1120 // test if baz is not linked to the system variant of foo
1121 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1122 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1123 "foo.stubs.jar")
1124 }
Jiyong Park1be96912018-05-28 18:02:19 +09001125
1126 // test if baz has exported SDK lib names foo and bar to qux
1127 qux := ctx.ModuleForTests("qux", "android_common")
1128 if quxLib, ok := qux.Module().(*Library); ok {
1129 sdkLibs := quxLib.ExportedSdkLibs()
1130 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
1131 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
1132 }
1133 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001134}