blob: 19c5f21bce12ecab9c0df79fdd4447a9db8ba8d0 [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"
28)
29
30var buildDir string
31
32func setUp() {
33 var err error
34 buildDir, err = ioutil.TempDir("", "soong_java_test")
35 if err != nil {
36 panic(err)
37 }
38}
39
40func tearDown() {
41 os.RemoveAll(buildDir)
42}
43
44func TestMain(m *testing.M) {
45 run := func() int {
46 setUp()
47 defer tearDown()
48
49 return m.Run()
50 }
51
52 os.Exit(run())
53}
Colin Cross527012a2017-11-30 22:56:16 -080054
55func testConfig(env map[string]string) android.Config {
Colin Cross7fdd2b72018-01-02 18:14:25 -080056 if env == nil {
57 env = make(map[string]string)
58 }
59 if env["ANDROID_JAVA8_HOME"] == "" {
60 env["ANDROID_JAVA8_HOME"] = "jdk8"
61 }
Jiyong Park1a5d7b12018-01-15 15:05:10 +090062 config := android.TestArchConfig(buildDir, env)
63 config.ProductVariables.DeviceSystemSdkVersions = &[]string{"14", "15"}
64 return config
Colin Cross527012a2017-11-30 22:56:16 -080065
Colin Cross1369cdb2017-09-29 17:58:17 -070066}
67
Colin Cross527012a2017-11-30 22:56:16 -080068func testContext(config android.Config, bp string,
69 fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070070
Colin Cross4c428df2017-09-15 17:36:05 -070071 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070072 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080073 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Crossa60ead82017-10-02 18:10:21 -070074 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true)))
Colin Cross2acdae82017-09-15 19:44:24 -070075 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070076 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070077 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070078 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080079 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Colin Cross0f37af02017-09-27 17:42:05 -070080 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070081 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070082 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
83 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070084 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080085 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross72bb3632017-07-13 16:23:21 -070086 ctx.Register()
87
Colin Cross4c428df2017-09-15 17:36:05 -070088 extraModules := []string{
89 "core-oj",
90 "core-libart",
91 "framework",
92 "ext",
93 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -070094 "android_stubs_current",
95 "android_system_stubs_current",
96 "android_test_stubs_current",
Colin Cross93e85952017-08-15 13:34:18 -070097 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -070098 }
Colin Cross72bb3632017-07-13 16:23:21 -070099
100 for _, extra := range extraModules {
101 bp += fmt.Sprintf(`
102 java_library {
103 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -0700104 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -0700105 no_standard_libs: true,
Colin Cross1369cdb2017-09-29 17:58:17 -0700106 system_modules: "core-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -0700107 }
108 `, extra)
109 }
110
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800111 bp += `
112 android_app {
113 name: "framework-res",
114 no_framework_libs: true,
115 }
116 `
117
Colin Cross527012a2017-11-30 22:56:16 -0800118 systemModules := []string{
119 "core-system-modules",
120 "android_stubs_current_system_modules",
121 "android_system_stubs_current_system_modules",
122 "android_test_stubs_current_system_modules",
123 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700124
Colin Cross527012a2017-11-30 22:56:16 -0800125 for _, extra := range systemModules {
126 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700127 java_system_modules {
128 name: "%s",
129 }
130 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700131 }
132
Colin Cross824bee32017-11-22 17:27:51 -0800133 mockFS := map[string][]byte{
Nan Zhang27e284d2018-02-09 21:03:53 +0000134 "Android.bp": []byte(bp),
135 "a.java": nil,
136 "b.java": nil,
137 "c.java": nil,
138 "b.kt": nil,
139 "a.jar": nil,
140 "b.jar": nil,
141 "java-res/a": nil,
142 "java-res/b": nil,
143 "java-res2/a": nil,
144 "java-fg/a.java": nil,
145 "java-fg/b.java": nil,
146 "java-fg/c.java": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700147
Colin Cross2ebc4762017-10-20 14:00:31 -0700148 "prebuilts/sdk/14/android.jar": nil,
149 "prebuilts/sdk/14/framework.aidl": nil,
150 "prebuilts/sdk/current/android.jar": nil,
151 "prebuilts/sdk/current/framework.aidl": nil,
Jiyong Park750e5572018-01-31 00:20:13 +0900152 "prebuilts/sdk/current/core.jar": nil,
Colin Cross2ebc4762017-10-20 14:00:31 -0700153 "prebuilts/sdk/system_current/android.jar": nil,
154 "prebuilts/sdk/system_current/framework.aidl": nil,
Sundong Ahn0926fae2017-10-17 16:34:51 +0900155 "prebuilts/sdk/system_14/android.jar": nil,
156 "prebuilts/sdk/system_14/framework.aidl": nil,
Colin Cross2ebc4762017-10-20 14:00:31 -0700157 "prebuilts/sdk/test_current/android.jar": nil,
158 "prebuilts/sdk/test_current/framework.aidl": nil,
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800159
160 // For framework-res, which is an implicit dependency for framework
161 "AndroidManifest.xml": nil,
162 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800163
164 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800165
Colin Cross66dbc0b2017-12-28 12:23:20 -0800166 "build/make/core/proguard.flags": nil,
167 "build/make/core/proguard_basic_keeps.flags": nil,
168
Colin Cross7fdd2b72018-01-02 18:14:25 -0800169 "jdk8/jre/lib/jce.jar": nil,
170 "jdk8/jre/lib/rt.jar": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800171 }
172
173 for k, v := range fs {
174 mockFS[k] = v
175 }
176
177 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700178
Colin Cross527012a2017-11-30 22:56:16 -0800179 return ctx
180}
181
182func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800183 t.Helper()
Jeff Gastond3e141d2017-08-08 17:46:01 -0700184 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Colin Cross72bb3632017-07-13 16:23:21 -0700185 fail(t, errs)
186 _, errs = ctx.PrepareBuildActions(config)
187 fail(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800188}
189
190func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800191 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800192 config := testConfig(nil)
193 ctx := testContext(config, bp, nil)
194 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700195
196 return ctx
197}
198
Colin Cross2acdae82017-09-15 19:44:24 -0700199func moduleToPath(name string) string {
200 switch {
201 case name == `""`:
202 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700203 case strings.HasSuffix(name, ".jar"):
204 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700205 case name == "android_stubs_current" || name == "android_system_stubs_current" ||
206 name == "android_test_stubs_current":
Colin Cross1ee23172017-10-18 14:44:18 -0700207 return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700208 default:
209 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700210 }
211}
212
Colin Cross72bb3632017-07-13 16:23:21 -0700213func TestSimple(t *testing.T) {
214 ctx := testJava(t, `
215 java_library {
216 name: "foo",
217 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700218 libs: ["bar"],
219 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700220 }
221
222 java_library {
223 name: "bar",
224 srcs: ["b.java"],
225 }
226
227 java_library {
228 name: "baz",
229 srcs: ["c.java"],
230 }
Colin Crossd5934c82017-10-02 13:55:26 -0700231 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700232
Colin Cross4c428df2017-09-15 17:36:05 -0700233 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700234 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700235
236 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
237 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
238 }
239
Colin Cross1ee23172017-10-18 14:44:18 -0700240 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700241 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
242 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700243
Nan Zhanged19fc32017-10-19 13:06:22 -0700244 if !strings.Contains(javac.Args["classpath"], barTurbine) {
245 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700246 }
247
Nan Zhanged19fc32017-10-19 13:06:22 -0700248 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
249 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700250 }
251
252 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
253 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700254 }
255}
256
Colin Crossd5934c82017-10-02 13:55:26 -0700257func TestArchSpecific(t *testing.T) {
258 ctx := testJava(t, `
259 java_library {
260 name: "foo",
261 srcs: ["a.java"],
262 target: {
263 android: {
264 srcs: ["b.java"],
265 },
266 },
267 }
268 `)
269
270 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
271 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
272 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
273 }
274}
275
Colin Cross6b4a32d2017-12-05 13:42:45 -0800276func TestBinary(t *testing.T) {
277 ctx := testJava(t, `
278 java_library_host {
279 name: "foo",
280 srcs: ["a.java"],
281 }
282
283 java_binary_host {
284 name: "bar",
285 srcs: ["b.java"],
286 static_libs: ["foo"],
287 }
288 `)
289
290 buildOS := android.BuildOs.String()
291
292 bar := ctx.ModuleForTests("bar", buildOS+"_common")
293 barJar := bar.Output("bar.jar").Output.String()
294 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
295 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
296
297 // Test that the install binary wrapper depends on the installed jar file
298 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
299 t.Errorf("expected binary wrapper implicits [%q], got %v",
300 barJar, barWrapperDeps)
301 }
302
303}
304
Colin Cross2acdae82017-09-15 19:44:24 -0700305var classpathTestcases = []struct {
306 name string
Colin Cross965714f2017-10-03 20:57:01 -0700307 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700308 host android.OsClass
309 properties string
310 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700311 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700312 classpath []string
313}{
314 {
315 name: "default",
316 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700317 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700318 classpath: []string{"ext", "framework", "okhttp"},
319 },
320 {
321 name: "blank sdk version",
322 properties: `sdk_version: "",`,
323 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700324 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700325 classpath: []string{"ext", "framework", "okhttp"},
326 },
327 {
Colin Cross4c428df2017-09-15 17:36:05 -0700328
Colin Cross2acdae82017-09-15 19:44:24 -0700329 name: "sdk v14",
330 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700331 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700332 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossfc3674a2017-09-18 17:41:52 -0700333 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700334 },
335 {
Colin Cross72bb3632017-07-13 16:23:21 -0700336
Colin Cross2acdae82017-09-15 19:44:24 -0700337 name: "current",
338 properties: `sdk_version: "current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700339 bootclasspath: []string{`""`},
340 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
341 classpath: []string{"prebuilts/sdk/current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700342 },
343 {
Colin Cross72bb3632017-07-13 16:23:21 -0700344
Colin Cross2acdae82017-09-15 19:44:24 -0700345 name: "system_current",
346 properties: `sdk_version: "system_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700347 bootclasspath: []string{`""`},
348 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
349 classpath: []string{"prebuilts/sdk/system_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700350 },
351 {
Colin Cross72bb3632017-07-13 16:23:21 -0700352
Sundong Ahn0926fae2017-10-17 16:34:51 +0900353 name: "system_14",
354 properties: `sdk_version: "system_14",`,
355 bootclasspath: []string{`""`},
356 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
357 classpath: []string{"prebuilts/sdk/system_14/android.jar"},
358 },
359 {
360
Colin Cross2acdae82017-09-15 19:44:24 -0700361 name: "test_current",
362 properties: `sdk_version: "test_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700363 bootclasspath: []string{`""`},
364 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
365 classpath: []string{"prebuilts/sdk/test_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700366 },
367 {
Colin Cross72bb3632017-07-13 16:23:21 -0700368
Jiyong Park750e5572018-01-31 00:20:13 +0900369 name: "core_current",
370 properties: `sdk_version: "core_current",`,
371 bootclasspath: []string{`""`},
372 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
373 classpath: []string{"prebuilts/sdk/current/core.jar"},
374 },
375 {
376
Colin Cross2acdae82017-09-15 19:44:24 -0700377 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700378 properties: `no_standard_libs: true, system_modules: "none"`,
379 system: "none",
380 bootclasspath: []string{`""`},
381 classpath: []string{},
382 },
383 {
384
385 name: "nostdlib system_modules",
386 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
387 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700388 bootclasspath: []string{`""`},
389 classpath: []string{},
390 },
391 {
Colin Cross72bb3632017-07-13 16:23:21 -0700392
Colin Cross7fdd2b72018-01-02 18:14:25 -0800393 name: "host default",
394 moduleType: "java_library_host",
395 properties: ``,
396 host: android.Host,
397 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
398 classpath: []string{},
Colin Cross2acdae82017-09-15 19:44:24 -0700399 },
400 {
401 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700402 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700403 host: android.Host,
404 properties: `no_standard_libs: true`,
405 classpath: []string{},
406 },
Colin Cross965714f2017-10-03 20:57:01 -0700407 {
408
Colin Cross7fdd2b72018-01-02 18:14:25 -0800409 name: "host supported default",
410 host: android.Host,
411 properties: `host_supported: true,`,
412 classpath: []string{},
413 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
Colin Cross965714f2017-10-03 20:57:01 -0700414 },
415 {
416 name: "host supported nostdlib",
417 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700418 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700419 classpath: []string{},
420 },
Colin Cross2acdae82017-09-15 19:44:24 -0700421}
Colin Cross72bb3632017-07-13 16:23:21 -0700422
Colin Cross2acdae82017-09-15 19:44:24 -0700423func TestClasspath(t *testing.T) {
424 for _, testcase := range classpathTestcases {
425 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700426 moduleType := "java_library"
427 if testcase.moduleType != "" {
428 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700429 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700430
431 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700432 name: "foo",
433 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700434 ` + testcase.properties + `
435 }`
436
437 variant := "android_common"
438 if testcase.host == android.Host {
439 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700440 }
Colin Cross72bb3632017-07-13 16:23:21 -0700441
Colin Cross2acdae82017-09-15 19:44:24 -0700442 convertModulesToPaths := func(cp []string) []string {
443 ret := make([]string, len(cp))
444 for i, e := range cp {
445 ret[i] = moduleToPath(e)
446 }
447 return ret
448 }
Colin Cross72bb3632017-07-13 16:23:21 -0700449
Colin Cross2acdae82017-09-15 19:44:24 -0700450 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
451 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700452
Colin Cross2acdae82017-09-15 19:44:24 -0700453 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700454 if bc != "" {
455 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700456 }
Colin Cross72bb3632017-07-13 16:23:21 -0700457
Colin Cross2acdae82017-09-15 19:44:24 -0700458 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700459 if c != "" {
460 c = "-classpath " + c
461 }
462 system := ""
463 if testcase.system == "none" {
464 system = "--system=none"
465 } else if testcase.system != "" {
466 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700467 }
468
Colin Cross1369cdb2017-09-29 17:58:17 -0700469 t.Run("1.8", func(t *testing.T) {
470 // Test default javac 1.8
471 ctx := testJava(t, bp)
Colin Cross2acdae82017-09-15 19:44:24 -0700472
Colin Cross1369cdb2017-09-29 17:58:17 -0700473 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
474
475 got := javac.Args["bootClasspath"]
476 if got != bc {
477 t.Errorf("bootclasspath expected %q != got %q", bc, got)
478 }
479
480 got = javac.Args["classpath"]
481 if got != c {
482 t.Errorf("classpath expected %q != got %q", c, got)
483 }
484
485 var deps []string
486 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
487 deps = append(deps, bootclasspath...)
488 }
489 deps = append(deps, classpath...)
490
491 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
492 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
493 }
494 })
495
496 // Test again with javac 1.9
497 t.Run("1.9", func(t *testing.T) {
Colin Cross527012a2017-11-30 22:56:16 -0800498 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
499 ctx := testContext(config, bp, nil)
500 run(t, ctx, config)
Colin Cross1369cdb2017-09-29 17:58:17 -0700501
502 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
503 got := javac.Args["bootClasspath"]
504 expected := system
505 if testcase.system == "bootclasspath" {
506 expected = bc
507 }
508 if got != expected {
509 t.Errorf("bootclasspath expected %q != got %q", expected, got)
510 }
511 })
Colin Cross2acdae82017-09-15 19:44:24 -0700512 })
Colin Cross72bb3632017-07-13 16:23:21 -0700513 }
514
Colin Cross72bb3632017-07-13 16:23:21 -0700515}
516
517func TestPrebuilts(t *testing.T) {
518 ctx := testJava(t, `
519 java_library {
520 name: "foo",
521 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700522 libs: ["bar"],
523 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700524 }
525
Colin Cross74d73e22017-08-02 11:05:49 -0700526 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700527 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700528 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700529 }
530
Colin Cross74d73e22017-08-02 11:05:49 -0700531 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700532 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700533 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700534 }
535 `)
536
Colin Cross4c428df2017-09-15 17:36:05 -0700537 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700538 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700539
540 bar := "a.jar"
541 if !strings.Contains(javac.Args["classpath"], bar) {
542 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
543 }
544
Colin Cross0a6e0072017-08-30 14:24:55 -0700545 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
546 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700547 }
548}
549
Colin Cross89536d42017-07-07 14:35:50 -0700550func TestDefaults(t *testing.T) {
551 ctx := testJava(t, `
552 java_defaults {
553 name: "defaults",
554 srcs: ["a.java"],
555 libs: ["bar"],
556 static_libs: ["baz"],
557 }
558
559 java_library {
560 name: "foo",
561 defaults: ["defaults"],
562 }
563
564 java_library {
565 name: "bar",
566 srcs: ["b.java"],
567 }
568
569 java_library {
570 name: "baz",
571 srcs: ["c.java"],
572 }
573 `)
574
Colin Cross4c428df2017-09-15 17:36:05 -0700575 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700576 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700577
578 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
579 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
580 }
581
Nan Zhanged19fc32017-10-19 13:06:22 -0700582 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
583 if !strings.Contains(javac.Args["classpath"], barTurbine) {
584 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700585 }
586
Colin Cross1ee23172017-10-18 14:44:18 -0700587 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700588 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
589 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700590 }
591}
592
Colin Cross0f37af02017-09-27 17:42:05 -0700593func TestResources(t *testing.T) {
594 var table = []struct {
595 name string
596 prop string
597 extra string
598 args string
599 }{
600 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700601 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700602 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800603 prop: `java_resource_dirs: ["java-res"]`,
604 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700605 },
606 {
607 // Test that a module with java_resources includes the files
608 name: "resource files",
Colin Cross824bee32017-11-22 17:27:51 -0800609 prop: `java_resources: ["java-res/a", "java-res/b"]`,
610 args: "-C . -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700611 },
612 {
613 // Test that a module with a filegroup in java_resources includes the files with the
614 // path prefix
615 name: "resource filegroup",
616 prop: `java_resources: [":foo-res"]`,
617 extra: `
618 filegroup {
619 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800620 path: "java-res",
621 srcs: ["java-res/a", "java-res/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700622 }`,
Colin Cross824bee32017-11-22 17:27:51 -0800623 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700624 },
625 {
626 // Test that a module with "include_srcs: true" includes its source files in the resources jar
627 name: "include sources",
628 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700629 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700630 },
631 }
632
633 for _, test := range table {
634 t.Run(test.name, func(t *testing.T) {
635 ctx := testJava(t, `
636 java_library {
637 name: "foo",
638 srcs: [
639 "a.java",
640 "b.java",
641 "c.java",
642 ],
643 `+test.prop+`,
644 }
645 `+test.extra)
646
Colin Cross1ee23172017-10-18 14:44:18 -0700647 foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
648 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700649
650 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
651 t.Errorf("foo combined jars %v does not contain %q",
652 foo.Inputs.Strings(), fooRes.Output.String())
653 }
654
Colin Crossaf9c55b2017-10-03 14:50:08 -0700655 if fooRes.Args["jarArgs"] != test.args {
656 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700657 fooRes.Args["jarArgs"], test.args)
658 }
659 })
660 }
661}
662
Colin Cross0532fb02017-10-02 16:57:40 -0700663func TestExcludeResources(t *testing.T) {
664 ctx := testJava(t, `
665 java_library {
666 name: "foo",
667 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800668 java_resource_dirs: ["java-res", "java-res2"],
669 exclude_java_resource_dirs: ["java-res2"],
Colin Cross0532fb02017-10-02 16:57:40 -0700670 }
671
672 java_library {
673 name: "bar",
674 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800675 java_resources: ["java-res/*"],
676 exclude_java_resources: ["java-res/b"],
Colin Cross0532fb02017-10-02 16:57:40 -0700677 }
678 `)
679
Colin Cross1ee23172017-10-18 14:44:18 -0700680 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700681
Colin Cross824bee32017-11-22 17:27:51 -0800682 expected := "-C java-res -f java-res/a -f java-res/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700683 if fooRes.Args["jarArgs"] != expected {
684 t.Errorf("foo resource jar args %q is not %q",
685 fooRes.Args["jarArgs"], expected)
686
687 }
688
Colin Cross1ee23172017-10-18 14:44:18 -0700689 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700690
Colin Cross824bee32017-11-22 17:27:51 -0800691 expected = "-C . -f java-res/a"
Colin Cross0532fb02017-10-02 16:57:40 -0700692 if barRes.Args["jarArgs"] != expected {
693 t.Errorf("bar resource jar args %q is not %q",
694 barRes.Args["jarArgs"], expected)
695
696 }
697}
698
Colin Cross54190b32017-10-09 15:34:10 -0700699func TestGeneratedSources(t *testing.T) {
700 ctx := testJava(t, `
701 java_library {
702 name: "foo",
703 srcs: [
704 "a*.java",
705 ":gen",
706 "b*.java",
707 ],
708 }
709
710 genrule {
711 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800712 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700713 out: ["gen.java"],
714 }
715 `)
716
717 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
718 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
719
Colin Cross15e86d92017-10-20 15:07:08 -0700720 if filepath.Base(genrule.Output.String()) != "gen.java" {
721 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700722 }
723
724 if len(javac.Inputs) != 3 ||
725 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700726 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700727 javac.Inputs[2].String() != "b.java" {
728 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
729 }
730}
731
Colin Cross93e85952017-08-15 13:34:18 -0700732func TestKotlin(t *testing.T) {
733 ctx := testJava(t, `
734 java_library {
735 name: "foo",
Colin Cross9bc43432017-12-15 20:20:39 -0800736 srcs: ["a.java", "b.kt"],
Colin Cross93e85952017-08-15 13:34:18 -0700737 }
Colin Cross715d7112017-10-25 20:32:18 -0700738
739 java_library {
740 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800741 srcs: ["b.kt"],
742 libs: ["foo"],
743 static_libs: ["baz"],
744 }
745
746 java_library {
747 name: "baz",
748 srcs: ["c.java"],
Colin Cross715d7112017-10-25 20:32:18 -0700749 }
Colin Cross93e85952017-08-15 13:34:18 -0700750 `)
751
Colin Cross9bc43432017-12-15 20:20:39 -0800752 fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
753 fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
754 fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700755
Colin Cross9bc43432017-12-15 20:20:39 -0800756 if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
757 fooKotlinc.Inputs[1].String() != "b.kt" {
758 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700759 }
760
Colin Cross9bc43432017-12-15 20:20:39 -0800761 if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
762 t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700763 }
764
Colin Cross9bc43432017-12-15 20:20:39 -0800765 if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
Colin Cross93e85952017-08-15 13:34:18 -0700766 t.Errorf("foo classpath %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800767 fooJavac.Args["classpath"], fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700768 }
769
Colin Cross9bc43432017-12-15 20:20:39 -0800770 if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
Colin Cross93e85952017-08-15 13:34:18 -0700771 t.Errorf("foo jar inputs %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800772 fooJar.Inputs.Strings(), fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700773 }
Colin Cross715d7112017-10-25 20:32:18 -0700774
Colin Cross9bc43432017-12-15 20:20:39 -0800775 fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
776 bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
777 barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
Colin Cross715d7112017-10-25 20:32:18 -0700778
Colin Cross9bc43432017-12-15 20:20:39 -0800779 if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
780 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
781 }
782
783 if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
784 t.Errorf(`expected %q in bar implicits %v`,
785 fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
786 }
787
788 if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
789 t.Errorf(`expected %q in bar implicits %v`,
790 bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
Colin Cross715d7112017-10-25 20:32:18 -0700791 }
Colin Cross93e85952017-08-15 13:34:18 -0700792}
793
Nan Zhang61eaedb2017-11-02 13:28:15 -0700794func TestTurbine(t *testing.T) {
795 ctx := testJava(t, `
796 java_library {
797 name: "foo",
798 srcs: ["a.java"],
799 }
800
801 java_library {
802 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800803 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700804 static_libs: ["foo"],
805 }
806
807 java_library {
808 name: "baz",
809 srcs: ["c.java"],
810 libs: ["bar"],
811 sdk_version: "14",
812 }
813 `)
814
815 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
816 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
817 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
818 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
819 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
820
821 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
822 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
823 }
824
825 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
826 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
827 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
828 }
829 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
830 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
831 }
832 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
833 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
834 }
835 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/android.jar") {
836 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
837 "prebuilts/sdk/14/android.jar")
838 }
839}
840
841func TestSharding(t *testing.T) {
842 ctx := testJava(t, `
843 java_library {
844 name: "bar",
845 srcs: ["a.java","b.java","c.java"],
846 javac_shard_size: 1
847 }
848 `)
849
850 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
851 for i := 0; i < 3; i++ {
852 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
853 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
854 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
855 }
856 }
857}
858
Colin Cross54250902017-12-05 09:28:08 -0800859func TestJarGenrules(t *testing.T) {
860 ctx := testJava(t, `
861 java_library {
862 name: "foo",
863 srcs: ["a.java"],
864 }
865
866 java_genrule {
867 name: "jargen",
868 tool_files: ["b.java"],
869 cmd: "$(location b.java) $(in) $(out)",
870 out: ["jargen.jar"],
871 srcs: [":foo"],
872 }
873
874 java_library {
875 name: "bar",
876 static_libs: ["jargen"],
877 srcs: ["c.java"],
878 }
879
880 java_library {
881 name: "baz",
882 libs: ["jargen"],
883 srcs: ["c.java"],
884 }
885 `)
886
887 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
888 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
889 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
890 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
891 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
892
893 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
894 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
895 }
896
897 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
898 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
899 }
900
901 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
902 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
903 }
904
905 if len(barCombined.Inputs) != 2 ||
906 barCombined.Inputs[0].String() != bar.Output.String() ||
907 barCombined.Inputs[1].String() != jargen.Output.String() {
908 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
909 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
910 }
911}
912
Nan Zhang27e284d2018-02-09 21:03:53 +0000913func TestExcludeFileGroupInSrcs(t *testing.T) {
914 ctx := testJava(t, `
915 java_library {
916 name: "foo",
917 srcs: ["a.java", ":foo-srcs"],
918 exclude_srcs: ["a.java", ":foo-excludes"],
919 }
920
921 filegroup {
922 name: "foo-srcs",
923 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
924 }
925
926 filegroup {
927 name: "foo-excludes",
928 srcs: ["java-fg/a.java", "java-fg/b.java"],
929 }
930 `)
931
932 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
933
934 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
935 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
936 }
937}
938
Colin Cross72bb3632017-07-13 16:23:21 -0700939func fail(t *testing.T, errs []error) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800940 t.Helper()
Colin Cross72bb3632017-07-13 16:23:21 -0700941 if len(errs) > 0 {
942 for _, err := range errs {
943 t.Error(err)
944 }
945 t.FailNow()
946 }
947}