blob: 5b364279d2da163a83e9d6a9a8ead582b00a4011 [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 Cross72bb3632017-07-13 16:23:21 -070054func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross824bee32017-11-22 17:27:51 -080055 return testJavaWithEnvFs(t, bp, nil, nil)
Colin Cross1369cdb2017-09-29 17:58:17 -070056}
57
Colin Cross824bee32017-11-22 17:27:51 -080058func testJavaWithEnvFs(t *testing.T, bp string,
59 env map[string]string, fs map[string][]byte) *android.TestContext {
Colin Cross1369cdb2017-09-29 17:58:17 -070060 config := android.TestArchConfig(buildDir, env)
Colin Cross72bb3632017-07-13 16:23:21 -070061
Colin Cross4c428df2017-09-15 17:36:05 -070062 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070063 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Crossa60ead82017-10-02 18:10:21 -070064 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true)))
Colin Cross2acdae82017-09-15 19:44:24 -070065 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070066 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070067 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070068 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross0f37af02017-09-27 17:42:05 -070069 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070070 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070071 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
72 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070073 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080074 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross72bb3632017-07-13 16:23:21 -070075 ctx.Register()
76
Colin Cross4c428df2017-09-15 17:36:05 -070077 extraModules := []string{
78 "core-oj",
79 "core-libart",
80 "framework",
81 "ext",
82 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -070083 "android_stubs_current",
84 "android_system_stubs_current",
85 "android_test_stubs_current",
Colin Cross93e85952017-08-15 13:34:18 -070086 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -070087 }
Colin Cross72bb3632017-07-13 16:23:21 -070088
89 for _, extra := range extraModules {
90 bp += fmt.Sprintf(`
91 java_library {
92 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -070093 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -070094 no_standard_libs: true,
Colin Cross1369cdb2017-09-29 17:58:17 -070095 system_modules: "core-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -070096 }
97 `, extra)
98 }
99
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800100 bp += `
101 android_app {
102 name: "framework-res",
103 no_framework_libs: true,
104 }
105 `
106
Colin Cross1369cdb2017-09-29 17:58:17 -0700107 if config.TargetOpenJDK9() {
108 systemModules := []string{
109 "core-system-modules",
110 "android_stubs_current_system_modules",
111 "android_system_stubs_current_system_modules",
112 "android_test_stubs_current_system_modules",
113 }
114
115 for _, extra := range systemModules {
116 bp += fmt.Sprintf(`
117 java_system_modules {
118 name: "%s",
119 }
120 `, extra)
121 }
122 }
123
Colin Cross824bee32017-11-22 17:27:51 -0800124 mockFS := map[string][]byte{
125 "Android.bp": []byte(bp),
126 "a.java": nil,
127 "b.java": nil,
128 "c.java": nil,
129 "b.kt": nil,
130 "a.jar": nil,
131 "b.jar": nil,
132 "java-res/a": nil,
133 "java-res/b": nil,
134 "java-res2/a": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700135
Colin Cross2ebc4762017-10-20 14:00:31 -0700136 "prebuilts/sdk/14/android.jar": nil,
137 "prebuilts/sdk/14/framework.aidl": nil,
138 "prebuilts/sdk/current/android.jar": nil,
139 "prebuilts/sdk/current/framework.aidl": nil,
140 "prebuilts/sdk/system_current/android.jar": nil,
141 "prebuilts/sdk/system_current/framework.aidl": nil,
Sundong Ahn0926fae2017-10-17 16:34:51 +0900142 "prebuilts/sdk/system_14/android.jar": nil,
143 "prebuilts/sdk/system_14/framework.aidl": nil,
Colin Cross2ebc4762017-10-20 14:00:31 -0700144 "prebuilts/sdk/test_current/android.jar": nil,
145 "prebuilts/sdk/test_current/framework.aidl": nil,
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800146
147 // For framework-res, which is an implicit dependency for framework
148 "AndroidManifest.xml": nil,
149 "build/target/product/security/testkey": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800150 }
151
152 for k, v := range fs {
153 mockFS[k] = v
154 }
155
156 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700157
Jeff Gastond3e141d2017-08-08 17:46:01 -0700158 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Colin Cross72bb3632017-07-13 16:23:21 -0700159 fail(t, errs)
160 _, errs = ctx.PrepareBuildActions(config)
161 fail(t, errs)
162
163 return ctx
164}
165
Colin Cross2acdae82017-09-15 19:44:24 -0700166func moduleToPath(name string) string {
167 switch {
168 case name == `""`:
169 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700170 case strings.HasSuffix(name, ".jar"):
171 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700172 case name == "android_stubs_current" || name == "android_system_stubs_current" ||
173 name == "android_test_stubs_current":
Colin Cross1ee23172017-10-18 14:44:18 -0700174 return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700175 default:
176 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700177 }
178}
179
Colin Cross72bb3632017-07-13 16:23:21 -0700180func TestSimple(t *testing.T) {
181 ctx := testJava(t, `
182 java_library {
183 name: "foo",
184 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700185 libs: ["bar"],
186 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700187 }
188
189 java_library {
190 name: "bar",
191 srcs: ["b.java"],
192 }
193
194 java_library {
195 name: "baz",
196 srcs: ["c.java"],
197 }
Colin Crossd5934c82017-10-02 13:55:26 -0700198 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700199
Colin Cross4c428df2017-09-15 17:36:05 -0700200 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700201 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700202
203 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
204 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
205 }
206
Colin Cross1ee23172017-10-18 14:44:18 -0700207 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700208 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
209 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700210
Nan Zhanged19fc32017-10-19 13:06:22 -0700211 if !strings.Contains(javac.Args["classpath"], barTurbine) {
212 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700213 }
214
Nan Zhanged19fc32017-10-19 13:06:22 -0700215 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
216 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700217 }
218
219 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
220 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700221 }
222}
223
Colin Crossd5934c82017-10-02 13:55:26 -0700224func TestArchSpecific(t *testing.T) {
225 ctx := testJava(t, `
226 java_library {
227 name: "foo",
228 srcs: ["a.java"],
229 target: {
230 android: {
231 srcs: ["b.java"],
232 },
233 },
234 }
235 `)
236
237 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
238 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
239 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
240 }
241}
242
Colin Cross2acdae82017-09-15 19:44:24 -0700243var classpathTestcases = []struct {
244 name string
Colin Cross965714f2017-10-03 20:57:01 -0700245 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700246 host android.OsClass
247 properties string
248 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700249 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700250 classpath []string
251}{
252 {
253 name: "default",
254 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700255 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700256 classpath: []string{"ext", "framework", "okhttp"},
257 },
258 {
259 name: "blank sdk version",
260 properties: `sdk_version: "",`,
261 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700262 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700263 classpath: []string{"ext", "framework", "okhttp"},
264 },
265 {
Colin Cross4c428df2017-09-15 17:36:05 -0700266
Colin Cross2acdae82017-09-15 19:44:24 -0700267 name: "sdk v14",
268 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700269 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700270 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossfc3674a2017-09-18 17:41:52 -0700271 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700272 },
273 {
Colin Cross72bb3632017-07-13 16:23:21 -0700274
Colin Cross2acdae82017-09-15 19:44:24 -0700275 name: "current",
276 properties: `sdk_version: "current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700277 bootclasspath: []string{`""`},
278 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
279 classpath: []string{"prebuilts/sdk/current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700280 },
281 {
Colin Cross72bb3632017-07-13 16:23:21 -0700282
Colin Cross2acdae82017-09-15 19:44:24 -0700283 name: "system_current",
284 properties: `sdk_version: "system_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700285 bootclasspath: []string{`""`},
286 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
287 classpath: []string{"prebuilts/sdk/system_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700288 },
289 {
Colin Cross72bb3632017-07-13 16:23:21 -0700290
Sundong Ahn0926fae2017-10-17 16:34:51 +0900291 name: "system_14",
292 properties: `sdk_version: "system_14",`,
293 bootclasspath: []string{`""`},
294 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
295 classpath: []string{"prebuilts/sdk/system_14/android.jar"},
296 },
297 {
298
Colin Cross2acdae82017-09-15 19:44:24 -0700299 name: "test_current",
300 properties: `sdk_version: "test_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700301 bootclasspath: []string{`""`},
302 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
303 classpath: []string{"prebuilts/sdk/test_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700304 },
305 {
Colin Cross72bb3632017-07-13 16:23:21 -0700306
Colin Cross2acdae82017-09-15 19:44:24 -0700307 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700308 properties: `no_standard_libs: true, system_modules: "none"`,
309 system: "none",
310 bootclasspath: []string{`""`},
311 classpath: []string{},
312 },
313 {
314
315 name: "nostdlib system_modules",
316 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
317 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700318 bootclasspath: []string{`""`},
319 classpath: []string{},
320 },
321 {
Colin Cross72bb3632017-07-13 16:23:21 -0700322
Colin Cross2acdae82017-09-15 19:44:24 -0700323 name: "host default",
Colin Cross965714f2017-10-03 20:57:01 -0700324 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700325 properties: ``,
Colin Cross965714f2017-10-03 20:57:01 -0700326 host: android.Host,
Colin Cross2acdae82017-09-15 19:44:24 -0700327 classpath: []string{},
328 },
329 {
330 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700331 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700332 host: android.Host,
333 properties: `no_standard_libs: true`,
334 classpath: []string{},
335 },
Colin Cross965714f2017-10-03 20:57:01 -0700336 {
337
338 name: "host supported default",
339 host: android.Host,
340 properties: `host_supported: true,`,
341 classpath: []string{},
342 },
343 {
344 name: "host supported nostdlib",
345 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700346 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700347 classpath: []string{},
348 },
Colin Cross2acdae82017-09-15 19:44:24 -0700349}
Colin Cross72bb3632017-07-13 16:23:21 -0700350
Colin Cross2acdae82017-09-15 19:44:24 -0700351func TestClasspath(t *testing.T) {
352 for _, testcase := range classpathTestcases {
353 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700354 moduleType := "java_library"
355 if testcase.moduleType != "" {
356 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700357 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700358
359 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700360 name: "foo",
361 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700362 ` + testcase.properties + `
363 }`
364
365 variant := "android_common"
366 if testcase.host == android.Host {
367 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700368 }
Colin Cross72bb3632017-07-13 16:23:21 -0700369
Colin Cross2acdae82017-09-15 19:44:24 -0700370 convertModulesToPaths := func(cp []string) []string {
371 ret := make([]string, len(cp))
372 for i, e := range cp {
373 ret[i] = moduleToPath(e)
374 }
375 return ret
376 }
Colin Cross72bb3632017-07-13 16:23:21 -0700377
Colin Cross2acdae82017-09-15 19:44:24 -0700378 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
379 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700380
Colin Cross2acdae82017-09-15 19:44:24 -0700381 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700382 if bc != "" {
383 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700384 }
Colin Cross72bb3632017-07-13 16:23:21 -0700385
Colin Cross2acdae82017-09-15 19:44:24 -0700386 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700387 if c != "" {
388 c = "-classpath " + c
389 }
390 system := ""
391 if testcase.system == "none" {
392 system = "--system=none"
393 } else if testcase.system != "" {
394 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700395 }
396
Colin Cross1369cdb2017-09-29 17:58:17 -0700397 t.Run("1.8", func(t *testing.T) {
398 // Test default javac 1.8
399 ctx := testJava(t, bp)
Colin Cross2acdae82017-09-15 19:44:24 -0700400
Colin Cross1369cdb2017-09-29 17:58:17 -0700401 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
402
403 got := javac.Args["bootClasspath"]
404 if got != bc {
405 t.Errorf("bootclasspath expected %q != got %q", bc, got)
406 }
407
408 got = javac.Args["classpath"]
409 if got != c {
410 t.Errorf("classpath expected %q != got %q", c, got)
411 }
412
413 var deps []string
414 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
415 deps = append(deps, bootclasspath...)
416 }
417 deps = append(deps, classpath...)
418
419 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
420 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
421 }
422 })
423
424 // Test again with javac 1.9
425 t.Run("1.9", func(t *testing.T) {
Colin Cross824bee32017-11-22 17:27:51 -0800426 ctx := testJavaWithEnvFs(t, bp, map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"}, nil)
Colin Cross1369cdb2017-09-29 17:58:17 -0700427
428 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
429 got := javac.Args["bootClasspath"]
430 expected := system
431 if testcase.system == "bootclasspath" {
432 expected = bc
433 }
434 if got != expected {
435 t.Errorf("bootclasspath expected %q != got %q", expected, got)
436 }
437 })
Colin Cross2acdae82017-09-15 19:44:24 -0700438 })
Colin Cross72bb3632017-07-13 16:23:21 -0700439 }
440
Colin Cross72bb3632017-07-13 16:23:21 -0700441}
442
443func TestPrebuilts(t *testing.T) {
444 ctx := testJava(t, `
445 java_library {
446 name: "foo",
447 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700448 libs: ["bar"],
449 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700450 }
451
Colin Cross74d73e22017-08-02 11:05:49 -0700452 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700453 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700454 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700455 }
456
Colin Cross74d73e22017-08-02 11:05:49 -0700457 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700458 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700459 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700460 }
461 `)
462
Colin Cross4c428df2017-09-15 17:36:05 -0700463 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700464 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700465
466 bar := "a.jar"
467 if !strings.Contains(javac.Args["classpath"], bar) {
468 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
469 }
470
Colin Cross0a6e0072017-08-30 14:24:55 -0700471 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
472 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700473 }
474}
475
Colin Cross89536d42017-07-07 14:35:50 -0700476func TestDefaults(t *testing.T) {
477 ctx := testJava(t, `
478 java_defaults {
479 name: "defaults",
480 srcs: ["a.java"],
481 libs: ["bar"],
482 static_libs: ["baz"],
483 }
484
485 java_library {
486 name: "foo",
487 defaults: ["defaults"],
488 }
489
490 java_library {
491 name: "bar",
492 srcs: ["b.java"],
493 }
494
495 java_library {
496 name: "baz",
497 srcs: ["c.java"],
498 }
499 `)
500
Colin Cross4c428df2017-09-15 17:36:05 -0700501 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700502 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700503
504 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
505 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
506 }
507
Nan Zhanged19fc32017-10-19 13:06:22 -0700508 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
509 if !strings.Contains(javac.Args["classpath"], barTurbine) {
510 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700511 }
512
Colin Cross1ee23172017-10-18 14:44:18 -0700513 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700514 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
515 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700516 }
517}
518
Colin Cross0f37af02017-09-27 17:42:05 -0700519func TestResources(t *testing.T) {
520 var table = []struct {
521 name string
522 prop string
523 extra string
524 args string
525 }{
526 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700527 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700528 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800529 prop: `java_resource_dirs: ["java-res"]`,
530 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700531 },
532 {
533 // Test that a module with java_resources includes the files
534 name: "resource files",
Colin Cross824bee32017-11-22 17:27:51 -0800535 prop: `java_resources: ["java-res/a", "java-res/b"]`,
536 args: "-C . -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700537 },
538 {
539 // Test that a module with a filegroup in java_resources includes the files with the
540 // path prefix
541 name: "resource filegroup",
542 prop: `java_resources: [":foo-res"]`,
543 extra: `
544 filegroup {
545 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800546 path: "java-res",
547 srcs: ["java-res/a", "java-res/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700548 }`,
Colin Cross824bee32017-11-22 17:27:51 -0800549 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700550 },
551 {
552 // Test that a module with "include_srcs: true" includes its source files in the resources jar
553 name: "include sources",
554 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700555 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700556 },
557 }
558
559 for _, test := range table {
560 t.Run(test.name, func(t *testing.T) {
561 ctx := testJava(t, `
562 java_library {
563 name: "foo",
564 srcs: [
565 "a.java",
566 "b.java",
567 "c.java",
568 ],
569 `+test.prop+`,
570 }
571 `+test.extra)
572
Colin Cross1ee23172017-10-18 14:44:18 -0700573 foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
574 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700575
576 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
577 t.Errorf("foo combined jars %v does not contain %q",
578 foo.Inputs.Strings(), fooRes.Output.String())
579 }
580
Colin Crossaf9c55b2017-10-03 14:50:08 -0700581 if fooRes.Args["jarArgs"] != test.args {
582 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700583 fooRes.Args["jarArgs"], test.args)
584 }
585 })
586 }
587}
588
Colin Cross0532fb02017-10-02 16:57:40 -0700589func TestExcludeResources(t *testing.T) {
590 ctx := testJava(t, `
591 java_library {
592 name: "foo",
593 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800594 java_resource_dirs: ["java-res", "java-res2"],
595 exclude_java_resource_dirs: ["java-res2"],
Colin Cross0532fb02017-10-02 16:57:40 -0700596 }
597
598 java_library {
599 name: "bar",
600 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800601 java_resources: ["java-res/*"],
602 exclude_java_resources: ["java-res/b"],
Colin Cross0532fb02017-10-02 16:57:40 -0700603 }
604 `)
605
Colin Cross1ee23172017-10-18 14:44:18 -0700606 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700607
Colin Cross824bee32017-11-22 17:27:51 -0800608 expected := "-C java-res -f java-res/a -f java-res/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700609 if fooRes.Args["jarArgs"] != expected {
610 t.Errorf("foo resource jar args %q is not %q",
611 fooRes.Args["jarArgs"], expected)
612
613 }
614
Colin Cross1ee23172017-10-18 14:44:18 -0700615 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700616
Colin Cross824bee32017-11-22 17:27:51 -0800617 expected = "-C . -f java-res/a"
Colin Cross0532fb02017-10-02 16:57:40 -0700618 if barRes.Args["jarArgs"] != expected {
619 t.Errorf("bar resource jar args %q is not %q",
620 barRes.Args["jarArgs"], expected)
621
622 }
623}
624
Colin Cross54190b32017-10-09 15:34:10 -0700625func TestGeneratedSources(t *testing.T) {
626 ctx := testJava(t, `
627 java_library {
628 name: "foo",
629 srcs: [
630 "a*.java",
631 ":gen",
632 "b*.java",
633 ],
634 }
635
636 genrule {
637 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800638 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700639 out: ["gen.java"],
640 }
641 `)
642
643 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
644 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
645
Colin Cross15e86d92017-10-20 15:07:08 -0700646 if filepath.Base(genrule.Output.String()) != "gen.java" {
647 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700648 }
649
650 if len(javac.Inputs) != 3 ||
651 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700652 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700653 javac.Inputs[2].String() != "b.java" {
654 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
655 }
656}
657
Colin Cross93e85952017-08-15 13:34:18 -0700658func TestKotlin(t *testing.T) {
659 ctx := testJava(t, `
660 java_library {
661 name: "foo",
662 srcs: ["a.java", "b.kt"],
663 }
Colin Cross715d7112017-10-25 20:32:18 -0700664
665 java_library {
666 name: "bar",
667 srcs: ["b.kt"],
668 }
Colin Cross93e85952017-08-15 13:34:18 -0700669 `)
670
671 kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
672 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Colin Cross1ee23172017-10-18 14:44:18 -0700673 jar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700674
675 if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" ||
676 kotlinc.Inputs[1].String() != "b.kt" {
677 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs)
678 }
679
680 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
681 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
682 }
683
684 if !strings.Contains(javac.Args["classpath"], kotlinc.Output.String()) {
685 t.Errorf("foo classpath %v does not contain %q",
686 javac.Args["classpath"], kotlinc.Output.String())
687 }
688
689 if !inList(kotlinc.Output.String(), jar.Inputs.Strings()) {
690 t.Errorf("foo jar inputs %v does not contain %q",
691 jar.Inputs.Strings(), kotlinc.Output.String())
692 }
Colin Cross715d7112017-10-25 20:32:18 -0700693
694 kotlinc = ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
695 jar = ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
696
697 if len(kotlinc.Inputs) != 1 || kotlinc.Inputs[0].String() != "b.kt" {
698 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, kotlinc.Inputs)
699 }
Colin Cross93e85952017-08-15 13:34:18 -0700700}
701
Nan Zhang61eaedb2017-11-02 13:28:15 -0700702func TestTurbine(t *testing.T) {
703 ctx := testJava(t, `
704 java_library {
705 name: "foo",
706 srcs: ["a.java"],
707 }
708
709 java_library {
710 name: "bar",
711 srcs: ["b.java"],
712 static_libs: ["foo"],
713 }
714
715 java_library {
716 name: "baz",
717 srcs: ["c.java"],
718 libs: ["bar"],
719 sdk_version: "14",
720 }
721 `)
722
723 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
724 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
725 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
726 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
727 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
728
729 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
730 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
731 }
732
733 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
734 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
735 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
736 }
737 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
738 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
739 }
740 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
741 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
742 }
743 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/android.jar") {
744 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
745 "prebuilts/sdk/14/android.jar")
746 }
747}
748
749func TestSharding(t *testing.T) {
750 ctx := testJava(t, `
751 java_library {
752 name: "bar",
753 srcs: ["a.java","b.java","c.java"],
754 javac_shard_size: 1
755 }
756 `)
757
758 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
759 for i := 0; i < 3; i++ {
760 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
761 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
762 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
763 }
764 }
765}
766
Colin Cross72bb3632017-07-13 16:23:21 -0700767func fail(t *testing.T, errs []error) {
768 if len(errs) > 0 {
769 for _, err := range errs {
770 t.Error(err)
771 }
772 t.FailNow()
773 }
774}