blob: 82accd5a5abf21e18f24801caa469fabe21e2cac [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 (
18 "android/soong/android"
Colin Cross0f37af02017-09-27 17:42:05 -070019 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070020 "fmt"
21 "io/ioutil"
22 "os"
23 "path/filepath"
Colin Cross227d4362017-08-30 14:14:52 -070024 "reflect"
Nan Zhang61eaedb2017-11-02 13:28:15 -070025 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070026 "strings"
27 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070028
29 "github.com/google/blueprint/proptools"
Colin Cross72bb3632017-07-13 16:23:21 -070030)
31
32var buildDir string
33
34func setUp() {
35 var err error
36 buildDir, err = ioutil.TempDir("", "soong_java_test")
37 if err != nil {
38 panic(err)
39 }
40}
41
42func tearDown() {
43 os.RemoveAll(buildDir)
44}
45
46func TestMain(m *testing.M) {
47 run := func() int {
48 setUp()
49 defer tearDown()
50
51 return m.Run()
52 }
53
54 os.Exit(run())
55}
Colin Cross527012a2017-11-30 22:56:16 -080056
57func testConfig(env map[string]string) android.Config {
Colin Cross7fdd2b72018-01-02 18:14:25 -080058 if env == nil {
59 env = make(map[string]string)
60 }
61 if env["ANDROID_JAVA8_HOME"] == "" {
62 env["ANDROID_JAVA8_HOME"] = "jdk8"
63 }
Jiyong Park1a5d7b12018-01-15 15:05:10 +090064 config := android.TestArchConfig(buildDir, env)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070065 config.TestProductVariables.DeviceSystemSdkVersions = &[]string{"14", "15"}
Jiyong Park1a5d7b12018-01-15 15:05:10 +090066 return config
Colin Cross527012a2017-11-30 22:56:16 -080067
Colin Cross1369cdb2017-09-29 17:58:17 -070068}
69
Colin Cross527012a2017-11-30 22:56:16 -080070func testContext(config android.Config, bp string,
71 fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070072
Colin Cross4c428df2017-09-15 17:36:05 -070073 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070074 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Crossa97c5d32018-03-28 14:58:31 -070075 ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080076 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Cross9ae1b922018-06-26 17:59:05 -070077 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross2acdae82017-09-15 19:44:24 -070078 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070079 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070080 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070081 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080082 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070083 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070084 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080085 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
86 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Nan Zhangf4936b02018-08-01 15:00:28 -070087 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
Jiyong Parkc678ad32018-04-10 13:07:10 +090088 ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(sdkLibraryFactory))
Jiyong Park58c518b2018-05-12 22:29:12 +090089 ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(prebuiltApisFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070090 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
91 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070092 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090093 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Park58c518b2018-05-12 22:29:12 +090094 ctx.TopDown("prebuilt_apis", prebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +090095 ctx.TopDown("java_sdk_library", sdkLibraryMutator).Parallel()
96 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -080097 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross72bb3632017-07-13 16:23:21 -070098 ctx.Register()
99
Colin Cross4c428df2017-09-15 17:36:05 -0700100 extraModules := []string{
101 "core-oj",
102 "core-libart",
Colin Cross86a60ae2018-05-29 14:44:55 -0700103 "core-lambda-stubs",
Neil Fuller60952592018-08-24 14:45:18 +0100104 "core-simple",
Colin Cross4c428df2017-09-15 17:36:05 -0700105 "framework",
106 "ext",
107 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -0700108 "android_stubs_current",
109 "android_system_stubs_current",
110 "android_test_stubs_current",
Colin Crossf19b9bb2018-03-26 14:42:44 -0700111 "core.current.stubs",
Colin Cross93e85952017-08-15 13:34:18 -0700112 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -0700113 }
Colin Cross72bb3632017-07-13 16:23:21 -0700114
115 for _, extra := range extraModules {
116 bp += fmt.Sprintf(`
117 java_library {
118 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -0700119 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -0700120 no_standard_libs: true,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700121 sdk_version: "core_current",
Colin Cross1369cdb2017-09-29 17:58:17 -0700122 system_modules: "core-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -0700123 }
124 `, extra)
125 }
126
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800127 bp += `
128 android_app {
129 name: "framework-res",
130 no_framework_libs: true,
131 }
132 `
133
Colin Cross527012a2017-11-30 22:56:16 -0800134 systemModules := []string{
135 "core-system-modules",
136 "android_stubs_current_system_modules",
137 "android_system_stubs_current_system_modules",
138 "android_test_stubs_current_system_modules",
139 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700140
Colin Cross527012a2017-11-30 22:56:16 -0800141 for _, extra := range systemModules {
142 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700143 java_system_modules {
144 name: "%s",
145 }
146 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700147 }
148
Colin Cross824bee32017-11-22 17:27:51 -0800149 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900150 "Android.bp": []byte(bp),
151 "a.java": nil,
152 "b.java": nil,
153 "c.java": nil,
154 "b.kt": nil,
155 "a.jar": nil,
156 "b.jar": nil,
157 "java-res/a/a": nil,
158 "java-res/b/b": nil,
159 "java-res2/a": nil,
160 "java-fg/a.java": nil,
161 "java-fg/b.java": nil,
162 "java-fg/c.java": nil,
163 "api/current.txt": nil,
164 "api/removed.txt": nil,
165 "api/system-current.txt": nil,
166 "api/system-removed.txt": nil,
167 "api/test-current.txt": nil,
168 "api/test-removed.txt": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700169
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100170 "prebuilts/sdk/14/public/android.jar": nil,
171 "prebuilts/sdk/14/public/framework.aidl": nil,
172 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700173 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100174 "prebuilts/sdk/current/public/android.jar": nil,
175 "prebuilts/sdk/current/public/framework.aidl": nil,
176 "prebuilts/sdk/current/public/core.jar": nil,
177 "prebuilts/sdk/current/system/android.jar": nil,
178 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900179 "prebuilts/sdk/28/public/api/foo.txt": nil,
180 "prebuilts/sdk/28/system/api/foo.txt": nil,
181 "prebuilts/sdk/28/test/api/foo.txt": nil,
182 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
183 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
184 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
185 "prebuilts/sdk/28/public/api/bar.txt": nil,
186 "prebuilts/sdk/28/system/api/bar.txt": nil,
187 "prebuilts/sdk/28/test/api/bar.txt": nil,
188 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
189 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
190 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700191 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900192 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800193
194 // For framework-res, which is an implicit dependency for framework
195 "AndroidManifest.xml": nil,
196 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800197
198 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800199
Colin Cross66dbc0b2017-12-28 12:23:20 -0800200 "build/make/core/proguard.flags": nil,
201 "build/make/core/proguard_basic_keeps.flags": nil,
202
Colin Cross7fdd2b72018-01-02 18:14:25 -0800203 "jdk8/jre/lib/jce.jar": nil,
204 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700205 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800206
207 "bar-doc/a.java": nil,
208 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900209 "bar-doc/IFoo.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800210 "bar-doc/known_oj_tags.txt": nil,
211 "external/doclava/templates-sdk": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800212 }
213
214 for k, v := range fs {
215 mockFS[k] = v
216 }
217
218 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700219
Colin Cross527012a2017-11-30 22:56:16 -0800220 return ctx
221}
222
223func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800224 t.Helper()
Jiyong Park58c518b2018-05-12 22:29:12 +0900225 _, errs := ctx.ParseFileList(".", []string{"Android.bp", "prebuilts/sdk/Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800226 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700227 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800228 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800229}
230
231func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800232 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800233 config := testConfig(nil)
234 ctx := testContext(config, bp, nil)
235 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700236
237 return ctx
238}
239
Colin Cross2acdae82017-09-15 19:44:24 -0700240func moduleToPath(name string) string {
241 switch {
242 case name == `""`:
243 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700244 case strings.HasSuffix(name, ".jar"):
245 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700246 default:
247 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700248 }
249}
250
Colin Cross72bb3632017-07-13 16:23:21 -0700251func TestSimple(t *testing.T) {
252 ctx := testJava(t, `
253 java_library {
254 name: "foo",
255 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700256 libs: ["bar"],
257 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700258 }
259
260 java_library {
261 name: "bar",
262 srcs: ["b.java"],
263 }
264
265 java_library {
266 name: "baz",
267 srcs: ["c.java"],
268 }
Colin Crossd5934c82017-10-02 13:55:26 -0700269 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700270
Colin Cross4c428df2017-09-15 17:36:05 -0700271 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700272 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700273
274 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
275 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
276 }
277
Colin Cross1ee23172017-10-18 14:44:18 -0700278 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700279 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
280 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700281
Nan Zhanged19fc32017-10-19 13:06:22 -0700282 if !strings.Contains(javac.Args["classpath"], barTurbine) {
283 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700284 }
285
Nan Zhanged19fc32017-10-19 13:06:22 -0700286 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
287 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700288 }
289
290 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
291 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700292 }
293}
294
Colin Crossd5934c82017-10-02 13:55:26 -0700295func TestArchSpecific(t *testing.T) {
296 ctx := testJava(t, `
297 java_library {
298 name: "foo",
299 srcs: ["a.java"],
300 target: {
301 android: {
302 srcs: ["b.java"],
303 },
304 },
305 }
306 `)
307
308 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
309 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
310 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
311 }
312}
313
Colin Cross6b4a32d2017-12-05 13:42:45 -0800314func TestBinary(t *testing.T) {
315 ctx := testJava(t, `
316 java_library_host {
317 name: "foo",
318 srcs: ["a.java"],
319 }
320
321 java_binary_host {
322 name: "bar",
323 srcs: ["b.java"],
324 static_libs: ["foo"],
325 }
326 `)
327
328 buildOS := android.BuildOs.String()
329
330 bar := ctx.ModuleForTests("bar", buildOS+"_common")
331 barJar := bar.Output("bar.jar").Output.String()
332 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
333 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
334
335 // Test that the install binary wrapper depends on the installed jar file
336 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
337 t.Errorf("expected binary wrapper implicits [%q], got %v",
338 barJar, barWrapperDeps)
339 }
340
341}
342
Colin Cross2acdae82017-09-15 19:44:24 -0700343var classpathTestcases = []struct {
344 name string
Colin Cross86a60ae2018-05-29 14:44:55 -0700345 unbundled bool
Colin Cross965714f2017-10-03 20:57:01 -0700346 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700347 host android.OsClass
348 properties string
349 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700350 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700351 classpath []string
352}{
353 {
354 name: "default",
Neil Fuller60952592018-08-24 14:45:18 +0100355 bootclasspath: []string{"core-oj", "core-libart", "core-simple"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700356 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700357 classpath: []string{"ext", "framework", "okhttp"},
358 },
359 {
360 name: "blank sdk version",
361 properties: `sdk_version: "",`,
Neil Fuller60952592018-08-24 14:45:18 +0100362 bootclasspath: []string{"core-oj", "core-libart", "core-simple"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700363 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700364 classpath: []string{"ext", "framework", "okhttp"},
365 },
366 {
Colin Cross4c428df2017-09-15 17:36:05 -0700367
Colin Cross2acdae82017-09-15 19:44:24 -0700368 name: "sdk v14",
369 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700370 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700371 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross86a60ae2018-05-29 14:44:55 -0700372 classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700373 },
374 {
Colin Cross72bb3632017-07-13 16:23:21 -0700375
Colin Cross2acdae82017-09-15 19:44:24 -0700376 name: "current",
377 properties: `sdk_version: "current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700378 bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700379 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700380 },
381 {
Colin Cross72bb3632017-07-13 16:23:21 -0700382
Colin Cross2acdae82017-09-15 19:44:24 -0700383 name: "system_current",
384 properties: `sdk_version: "system_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700385 bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700386 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700387 },
388 {
Colin Cross72bb3632017-07-13 16:23:21 -0700389
Sundong Ahn0926fae2017-10-17 16:34:51 +0900390 name: "system_14",
391 properties: `sdk_version: "system_14",`,
392 bootclasspath: []string{`""`},
393 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross86a60ae2018-05-29 14:44:55 -0700394 classpath: []string{"prebuilts/sdk/14/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Sundong Ahn0926fae2017-10-17 16:34:51 +0900395 },
396 {
397
Colin Cross2acdae82017-09-15 19:44:24 -0700398 name: "test_current",
399 properties: `sdk_version: "test_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700400 bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700401 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700402 },
403 {
Colin Cross72bb3632017-07-13 16:23:21 -0700404
Jiyong Park750e5572018-01-31 00:20:13 +0900405 name: "core_current",
406 properties: `sdk_version: "core_current",`,
Colin Cross86a60ae2018-05-29 14:44:55 -0700407 bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
Jiyong Park750e5572018-01-31 00:20:13 +0900408 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Jiyong Park750e5572018-01-31 00:20:13 +0900409 },
410 {
411
Colin Cross2acdae82017-09-15 19:44:24 -0700412 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700413 properties: `no_standard_libs: true, system_modules: "none"`,
414 system: "none",
415 bootclasspath: []string{`""`},
416 classpath: []string{},
417 },
418 {
419
420 name: "nostdlib system_modules",
421 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
422 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700423 bootclasspath: []string{`""`},
424 classpath: []string{},
425 },
426 {
Colin Cross72bb3632017-07-13 16:23:21 -0700427
Colin Cross7fdd2b72018-01-02 18:14:25 -0800428 name: "host default",
429 moduleType: "java_library_host",
430 properties: ``,
431 host: android.Host,
432 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
433 classpath: []string{},
Colin Cross2acdae82017-09-15 19:44:24 -0700434 },
435 {
436 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700437 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700438 host: android.Host,
439 properties: `no_standard_libs: true`,
440 classpath: []string{},
441 },
Colin Cross965714f2017-10-03 20:57:01 -0700442 {
443
Colin Cross7fdd2b72018-01-02 18:14:25 -0800444 name: "host supported default",
445 host: android.Host,
446 properties: `host_supported: true,`,
447 classpath: []string{},
448 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
Colin Cross965714f2017-10-03 20:57:01 -0700449 },
450 {
451 name: "host supported nostdlib",
452 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700453 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700454 classpath: []string{},
455 },
Colin Cross86a60ae2018-05-29 14:44:55 -0700456 {
457
458 name: "unbundled sdk v14",
459 unbundled: true,
460 properties: `sdk_version: "14",`,
461 bootclasspath: []string{`""`},
462 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
463 classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
464 },
465 {
466
467 name: "unbundled current",
468 unbundled: true,
469 properties: `sdk_version: "current",`,
470 bootclasspath: []string{`""`},
471 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
472 classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
473 },
Colin Cross2acdae82017-09-15 19:44:24 -0700474}
Colin Cross72bb3632017-07-13 16:23:21 -0700475
Colin Cross2acdae82017-09-15 19:44:24 -0700476func TestClasspath(t *testing.T) {
477 for _, testcase := range classpathTestcases {
478 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700479 moduleType := "java_library"
480 if testcase.moduleType != "" {
481 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700482 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700483
484 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700485 name: "foo",
486 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700487 ` + testcase.properties + `
488 }`
489
490 variant := "android_common"
491 if testcase.host == android.Host {
492 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700493 }
Colin Cross72bb3632017-07-13 16:23:21 -0700494
Colin Cross2acdae82017-09-15 19:44:24 -0700495 convertModulesToPaths := func(cp []string) []string {
496 ret := make([]string, len(cp))
497 for i, e := range cp {
498 ret[i] = moduleToPath(e)
499 }
500 return ret
501 }
Colin Cross72bb3632017-07-13 16:23:21 -0700502
Colin Cross2acdae82017-09-15 19:44:24 -0700503 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
504 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700505
Colin Cross2acdae82017-09-15 19:44:24 -0700506 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700507 if bc != "" {
508 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700509 }
Colin Cross72bb3632017-07-13 16:23:21 -0700510
Colin Cross2acdae82017-09-15 19:44:24 -0700511 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700512 if c != "" {
513 c = "-classpath " + c
514 }
515 system := ""
516 if testcase.system == "none" {
517 system = "--system=none"
518 } else if testcase.system != "" {
519 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700520 }
521
Colin Cross1369cdb2017-09-29 17:58:17 -0700522 t.Run("1.8", func(t *testing.T) {
523 // Test default javac 1.8
Colin Cross86a60ae2018-05-29 14:44:55 -0700524 config := testConfig(nil)
525 if testcase.unbundled {
526 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
527 }
528 ctx := testContext(config, bp, nil)
529 run(t, ctx, config)
Colin Cross2acdae82017-09-15 19:44:24 -0700530
Colin Cross1369cdb2017-09-29 17:58:17 -0700531 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
532
533 got := javac.Args["bootClasspath"]
534 if got != bc {
535 t.Errorf("bootclasspath expected %q != got %q", bc, got)
536 }
537
538 got = javac.Args["classpath"]
539 if got != c {
540 t.Errorf("classpath expected %q != got %q", c, got)
541 }
542
543 var deps []string
544 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
545 deps = append(deps, bootclasspath...)
546 }
547 deps = append(deps, classpath...)
548
549 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
550 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
551 }
552 })
553
554 // Test again with javac 1.9
555 t.Run("1.9", func(t *testing.T) {
Colin Cross527012a2017-11-30 22:56:16 -0800556 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
Colin Cross86a60ae2018-05-29 14:44:55 -0700557 if testcase.unbundled {
558 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
559 }
Colin Cross527012a2017-11-30 22:56:16 -0800560 ctx := testContext(config, bp, nil)
561 run(t, ctx, config)
Colin Cross1369cdb2017-09-29 17:58:17 -0700562
563 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
564 got := javac.Args["bootClasspath"]
565 expected := system
566 if testcase.system == "bootclasspath" {
567 expected = bc
568 }
569 if got != expected {
570 t.Errorf("bootclasspath expected %q != got %q", expected, got)
571 }
572 })
Colin Cross2acdae82017-09-15 19:44:24 -0700573 })
Colin Cross72bb3632017-07-13 16:23:21 -0700574 }
575
Colin Cross72bb3632017-07-13 16:23:21 -0700576}
577
578func TestPrebuilts(t *testing.T) {
579 ctx := testJava(t, `
580 java_library {
581 name: "foo",
582 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700583 libs: ["bar"],
584 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700585 }
586
Colin Cross74d73e22017-08-02 11:05:49 -0700587 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700588 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700589 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700590 }
591
Colin Cross74d73e22017-08-02 11:05:49 -0700592 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700593 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700594 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700595 }
596 `)
597
Colin Cross4c428df2017-09-15 17:36:05 -0700598 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700599 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700600 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
601 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700602
Colin Cross37f6d792018-07-12 12:28:41 -0700603 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
604 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700605 }
606
Colin Cross37f6d792018-07-12 12:28:41 -0700607 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
608 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700609 }
610}
611
Colin Cross89536d42017-07-07 14:35:50 -0700612func TestDefaults(t *testing.T) {
613 ctx := testJava(t, `
614 java_defaults {
615 name: "defaults",
616 srcs: ["a.java"],
617 libs: ["bar"],
618 static_libs: ["baz"],
619 }
620
621 java_library {
622 name: "foo",
623 defaults: ["defaults"],
624 }
625
626 java_library {
627 name: "bar",
628 srcs: ["b.java"],
629 }
630
631 java_library {
632 name: "baz",
633 srcs: ["c.java"],
634 }
635 `)
636
Colin Cross4c428df2017-09-15 17:36:05 -0700637 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700638 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700639
640 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
641 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
642 }
643
Nan Zhanged19fc32017-10-19 13:06:22 -0700644 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
645 if !strings.Contains(javac.Args["classpath"], barTurbine) {
646 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700647 }
648
Colin Cross1ee23172017-10-18 14:44:18 -0700649 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700650 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
651 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700652 }
653}
654
Colin Cross0f37af02017-09-27 17:42:05 -0700655func TestResources(t *testing.T) {
656 var table = []struct {
657 name string
658 prop string
659 extra string
660 args string
661 }{
662 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700663 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700664 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800665 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700666 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700667 },
668 {
669 // Test that a module with java_resources includes the files
670 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700671 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
672 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700673 },
674 {
675 // Test that a module with a filegroup in java_resources includes the files with the
676 // path prefix
677 name: "resource filegroup",
678 prop: `java_resources: [":foo-res"]`,
679 extra: `
680 filegroup {
681 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800682 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700683 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700684 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700685 args: "-C java-res -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 "include_srcs: true" includes its source files in the resources jar
689 name: "include sources",
690 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700691 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700692 },
Colin Cross0ead1d72018-04-10 13:07:42 -0700693 {
694 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
695 name: "wildcard dirs",
696 prop: `java_resource_dirs: ["java-res/*"]`,
697 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
698 },
699 {
700 // Test that a module exclude_java_resource_dirs excludes the files
701 name: "wildcard dirs",
702 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
703 args: "-C java-res/a -f java-res/a/a",
704 },
Colin Crosscedd4762018-09-13 11:26:19 -0700705 {
706 // Test wildcards in java_resources
707 name: "wildcard files",
708 prop: `java_resources: ["java-res/**/*"]`,
709 args: "-C . -f java-res/a/a -f java-res/b/b",
710 },
711 {
712 // Test exclude_java_resources with java_resources
713 name: "wildcard files with exclude",
714 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
715 args: "-C . -f java-res/a/a",
716 },
717 {
718 // Test exclude_java_resources with java_resource_dirs
719 name: "resource dirs with exclude files",
720 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
721 args: "-C java-res -f java-res/a/a",
722 },
723 {
724 // Test exclude_java_resource_dirs with java_resource_dirs
725 name: "resource dirs with exclude files",
726 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
727 args: "-C java-res -f java-res/a/a -f java-res/b/b",
728 },
Colin Cross0f37af02017-09-27 17:42:05 -0700729 }
730
731 for _, test := range table {
732 t.Run(test.name, func(t *testing.T) {
733 ctx := testJava(t, `
734 java_library {
735 name: "foo",
736 srcs: [
737 "a.java",
738 "b.java",
739 "c.java",
740 ],
741 `+test.prop+`,
742 }
743 `+test.extra)
744
Colin Cross331a1212018-08-15 20:40:52 -0700745 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700746 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700747
748 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
749 t.Errorf("foo combined jars %v does not contain %q",
750 foo.Inputs.Strings(), fooRes.Output.String())
751 }
752
Colin Crossaf9c55b2017-10-03 14:50:08 -0700753 if fooRes.Args["jarArgs"] != test.args {
754 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700755 fooRes.Args["jarArgs"], test.args)
756 }
757 })
758 }
759}
760
Colin Cross54190b32017-10-09 15:34:10 -0700761func TestGeneratedSources(t *testing.T) {
762 ctx := testJava(t, `
763 java_library {
764 name: "foo",
765 srcs: [
766 "a*.java",
767 ":gen",
768 "b*.java",
769 ],
770 }
771
772 genrule {
773 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800774 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700775 out: ["gen.java"],
776 }
777 `)
778
779 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
780 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
781
Colin Cross15e86d92017-10-20 15:07:08 -0700782 if filepath.Base(genrule.Output.String()) != "gen.java" {
783 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700784 }
785
786 if len(javac.Inputs) != 3 ||
787 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700788 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700789 javac.Inputs[2].String() != "b.java" {
790 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
791 }
792}
793
Colin Cross93e85952017-08-15 13:34:18 -0700794func TestKotlin(t *testing.T) {
795 ctx := testJava(t, `
796 java_library {
797 name: "foo",
Colin Cross9bc43432017-12-15 20:20:39 -0800798 srcs: ["a.java", "b.kt"],
Colin Cross93e85952017-08-15 13:34:18 -0700799 }
Colin Cross715d7112017-10-25 20:32:18 -0700800
801 java_library {
802 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800803 srcs: ["b.kt"],
804 libs: ["foo"],
805 static_libs: ["baz"],
806 }
807
808 java_library {
809 name: "baz",
810 srcs: ["c.java"],
Colin Cross715d7112017-10-25 20:32:18 -0700811 }
Colin Cross93e85952017-08-15 13:34:18 -0700812 `)
813
Colin Cross9bc43432017-12-15 20:20:39 -0800814 fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
815 fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
816 fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700817
Colin Cross9bc43432017-12-15 20:20:39 -0800818 if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
819 fooKotlinc.Inputs[1].String() != "b.kt" {
820 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700821 }
822
Colin Cross9bc43432017-12-15 20:20:39 -0800823 if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
824 t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700825 }
826
Colin Cross9bc43432017-12-15 20:20:39 -0800827 if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
Colin Cross93e85952017-08-15 13:34:18 -0700828 t.Errorf("foo classpath %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800829 fooJavac.Args["classpath"], fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700830 }
831
Colin Cross9bc43432017-12-15 20:20:39 -0800832 if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
Colin Cross93e85952017-08-15 13:34:18 -0700833 t.Errorf("foo jar inputs %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800834 fooJar.Inputs.Strings(), fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700835 }
Colin Cross715d7112017-10-25 20:32:18 -0700836
Colin Cross9bc43432017-12-15 20:20:39 -0800837 fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
838 bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
839 barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
Colin Cross715d7112017-10-25 20:32:18 -0700840
Colin Cross9bc43432017-12-15 20:20:39 -0800841 if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
842 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
843 }
844
845 if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
846 t.Errorf(`expected %q in bar implicits %v`,
847 fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
848 }
849
850 if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
851 t.Errorf(`expected %q in bar implicits %v`,
852 bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
Colin Cross715d7112017-10-25 20:32:18 -0700853 }
Colin Cross93e85952017-08-15 13:34:18 -0700854}
855
Nan Zhang61eaedb2017-11-02 13:28:15 -0700856func TestTurbine(t *testing.T) {
857 ctx := testJava(t, `
858 java_library {
859 name: "foo",
860 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900861 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700862 }
863
864 java_library {
865 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800866 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700867 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900868 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700869 }
870
871 java_library {
872 name: "baz",
873 srcs: ["c.java"],
874 libs: ["bar"],
875 sdk_version: "14",
876 }
877 `)
878
879 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
880 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
881 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
882 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
883 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
884
885 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
886 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
887 }
888
889 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
890 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
891 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
892 }
893 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
894 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
895 }
896 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
897 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
898 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100899 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700900 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100901 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700902 }
903}
904
905func TestSharding(t *testing.T) {
906 ctx := testJava(t, `
907 java_library {
908 name: "bar",
909 srcs: ["a.java","b.java","c.java"],
910 javac_shard_size: 1
911 }
912 `)
913
914 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
915 for i := 0; i < 3; i++ {
916 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
917 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
918 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
919 }
920 }
921}
922
Nan Zhang581fd212018-01-10 16:06:12 -0800923func TestDroiddoc(t *testing.T) {
924 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800925 droiddoc_template {
926 name: "droiddoc-templates-sdk",
927 path: ".",
928 }
Nan Zhang581fd212018-01-10 16:06:12 -0800929 droiddoc {
930 name: "bar-doc",
931 srcs: [
932 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900933 "bar-doc/IFoo.aidl",
Nan Zhang581fd212018-01-10 16:06:12 -0800934 ],
935 exclude_srcs: [
936 "bar-doc/b.java"
937 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800938 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800939 hdf: [
940 "android.whichdoc offline",
941 ],
942 knowntags: [
943 "bar-doc/known_oj_tags.txt",
944 ],
945 proofread_file: "libcore-proofread.txt",
946 todo_file: "libcore-docs-todo.html",
947 args: "-offlinemode -title \"libcore\"",
948 }
949 `)
950
Nan Zhang3bb14202018-02-28 10:14:25 -0800951 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
952 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800953 if stubsJar != barDoc.Output.String() {
954 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
955 }
Jiyong Park1e440682018-05-23 18:42:04 +0900956 inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
957 var javaSrcs []string
958 for _, i := range inputs {
959 javaSrcs = append(javaSrcs, i.Base())
960 }
961 if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
962 t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
963 }
Nan Zhang581fd212018-01-10 16:06:12 -0800964}
965
Colin Cross54250902017-12-05 09:28:08 -0800966func TestJarGenrules(t *testing.T) {
967 ctx := testJava(t, `
968 java_library {
969 name: "foo",
970 srcs: ["a.java"],
971 }
972
973 java_genrule {
974 name: "jargen",
975 tool_files: ["b.java"],
976 cmd: "$(location b.java) $(in) $(out)",
977 out: ["jargen.jar"],
978 srcs: [":foo"],
979 }
980
981 java_library {
982 name: "bar",
983 static_libs: ["jargen"],
984 srcs: ["c.java"],
985 }
986
987 java_library {
988 name: "baz",
989 libs: ["jargen"],
990 srcs: ["c.java"],
991 }
992 `)
993
994 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
995 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
996 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
997 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
998 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
999
1000 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1001 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1002 }
1003
1004 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1005 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1006 }
1007
1008 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1009 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1010 }
1011
1012 if len(barCombined.Inputs) != 2 ||
1013 barCombined.Inputs[0].String() != bar.Output.String() ||
1014 barCombined.Inputs[1].String() != jargen.Output.String() {
1015 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1016 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1017 }
1018}
1019
Nan Zhang27e284d2018-02-09 21:03:53 +00001020func TestExcludeFileGroupInSrcs(t *testing.T) {
1021 ctx := testJava(t, `
1022 java_library {
1023 name: "foo",
1024 srcs: ["a.java", ":foo-srcs"],
1025 exclude_srcs: ["a.java", ":foo-excludes"],
1026 }
1027
1028 filegroup {
1029 name: "foo-srcs",
1030 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1031 }
1032
1033 filegroup {
1034 name: "foo-excludes",
1035 srcs: ["java-fg/a.java", "java-fg/b.java"],
1036 }
1037 `)
1038
1039 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1040
1041 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1042 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1043 }
1044}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001045
1046func TestJavaSdkLibrary(t *testing.T) {
1047 ctx := testJava(t, `
1048 droiddoc_template {
1049 name: "droiddoc-templates-sdk",
1050 path: ".",
1051 }
1052 java_library {
1053 name: "conscrypt",
1054 }
1055 java_library {
1056 name: "bouncycastle",
1057 }
1058 java_sdk_library {
1059 name: "foo",
1060 srcs: ["a.java", "b.java"],
1061 api_packages: ["foo"],
1062 }
1063 java_sdk_library {
1064 name: "bar",
1065 srcs: ["a.java", "b.java"],
1066 api_packages: ["bar"],
1067 }
1068 java_library {
1069 name: "baz",
1070 srcs: ["c.java"],
1071 libs: ["foo", "bar"],
1072 sdk_version: "system_current",
1073 }
Jiyong Park1be96912018-05-28 18:02:19 +09001074 java_library {
1075 name: "qux",
1076 srcs: ["c.java"],
1077 libs: ["baz"],
1078 sdk_version: "system_current",
1079 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001080 `)
1081
1082 // check the existence of the internal modules
1083 ctx.ModuleForTests("foo", "android_common")
1084 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
1085 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001086 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001087 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
1088 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001089 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001090 ctx.ModuleForTests("foo"+sdkImplLibrarySuffix, "android_common")
Jiyong Park5a8d1be2018-04-25 22:57:34 +09001091 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001092 ctx.ModuleForTests("foo.api.public.28", "")
1093 ctx.ModuleForTests("foo.api.system.28", "")
1094 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001095
1096 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1097 // tests if baz is actually linked to the stubs lib
1098 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1099 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1100 "foo.stubs.system.jar")
1101 }
1102 // ... and not to the impl lib
1103 if strings.Contains(bazJavac.Args["classpath"], "foo.impl.jar") {
1104 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1105 "foo.impl.jar")
1106 }
1107 // test if baz is not linked to the system variant of foo
1108 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1109 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1110 "foo.stubs.jar")
1111 }
Jiyong Park1be96912018-05-28 18:02:19 +09001112
1113 // test if baz has exported SDK lib names foo and bar to qux
1114 qux := ctx.ModuleForTests("qux", "android_common")
1115 if quxLib, ok := qux.Module().(*Library); ok {
1116 sdkLibs := quxLib.ExportedSdkLibs()
1117 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
1118 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
1119 }
1120 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001121}