blob: dbecc70bb3dafb4269d7f4f60f3250fd4e1cb30d [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 {
56 return android.TestArchConfig(buildDir, env)
57
Colin Cross1369cdb2017-09-29 17:58:17 -070058}
59
Colin Cross527012a2017-11-30 22:56:16 -080060func testContext(config android.Config, bp string,
61 fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070062
Colin Cross4c428df2017-09-15 17:36:05 -070063 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070064 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Crossa60ead82017-10-02 18:10:21 -070065 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true)))
Colin Cross2acdae82017-09-15 19:44:24 -070066 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070067 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070068 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070069 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross0f37af02017-09-27 17:42:05 -070070 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070071 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070072 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
73 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070074 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080075 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross72bb3632017-07-13 16:23:21 -070076 ctx.Register()
77
Colin Cross4c428df2017-09-15 17:36:05 -070078 extraModules := []string{
79 "core-oj",
80 "core-libart",
81 "framework",
82 "ext",
83 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -070084 "android_stubs_current",
85 "android_system_stubs_current",
86 "android_test_stubs_current",
Colin Cross93e85952017-08-15 13:34:18 -070087 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -070088 }
Colin Cross72bb3632017-07-13 16:23:21 -070089
90 for _, extra := range extraModules {
91 bp += fmt.Sprintf(`
92 java_library {
93 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -070094 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -070095 no_standard_libs: true,
Colin Cross1369cdb2017-09-29 17:58:17 -070096 system_modules: "core-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -070097 }
98 `, extra)
99 }
100
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800101 bp += `
102 android_app {
103 name: "framework-res",
104 no_framework_libs: true,
105 }
106 `
107
Colin Cross527012a2017-11-30 22:56:16 -0800108 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 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700114
Colin Cross527012a2017-11-30 22:56:16 -0800115 for _, extra := range systemModules {
116 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700117 java_system_modules {
118 name: "%s",
119 }
120 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700121 }
122
Colin Cross824bee32017-11-22 17:27:51 -0800123 mockFS := map[string][]byte{
124 "Android.bp": []byte(bp),
125 "a.java": nil,
126 "b.java": nil,
127 "c.java": nil,
128 "b.kt": nil,
129 "a.jar": nil,
130 "b.jar": nil,
131 "java-res/a": nil,
132 "java-res/b": nil,
133 "java-res2/a": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700134
Colin Cross2ebc4762017-10-20 14:00:31 -0700135 "prebuilts/sdk/14/android.jar": nil,
136 "prebuilts/sdk/14/framework.aidl": nil,
137 "prebuilts/sdk/current/android.jar": nil,
138 "prebuilts/sdk/current/framework.aidl": nil,
139 "prebuilts/sdk/system_current/android.jar": nil,
140 "prebuilts/sdk/system_current/framework.aidl": nil,
Sundong Ahn0926fae2017-10-17 16:34:51 +0900141 "prebuilts/sdk/system_14/android.jar": nil,
142 "prebuilts/sdk/system_14/framework.aidl": nil,
Colin Cross2ebc4762017-10-20 14:00:31 -0700143 "prebuilts/sdk/test_current/android.jar": nil,
144 "prebuilts/sdk/test_current/framework.aidl": nil,
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800145
146 // For framework-res, which is an implicit dependency for framework
147 "AndroidManifest.xml": nil,
148 "build/target/product/security/testkey": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800149 }
150
151 for k, v := range fs {
152 mockFS[k] = v
153 }
154
155 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700156
Colin Cross527012a2017-11-30 22:56:16 -0800157 return ctx
158}
159
160func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Jeff Gastond3e141d2017-08-08 17:46:01 -0700161 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Colin Cross72bb3632017-07-13 16:23:21 -0700162 fail(t, errs)
163 _, errs = ctx.PrepareBuildActions(config)
164 fail(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800165}
166
167func testJava(t *testing.T, bp string) *android.TestContext {
168 config := testConfig(nil)
169 ctx := testContext(config, bp, nil)
170 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700171
172 return ctx
173}
174
Colin Cross2acdae82017-09-15 19:44:24 -0700175func moduleToPath(name string) string {
176 switch {
177 case name == `""`:
178 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700179 case strings.HasSuffix(name, ".jar"):
180 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700181 case name == "android_stubs_current" || name == "android_system_stubs_current" ||
182 name == "android_test_stubs_current":
Colin Cross1ee23172017-10-18 14:44:18 -0700183 return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700184 default:
185 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700186 }
187}
188
Colin Cross72bb3632017-07-13 16:23:21 -0700189func TestSimple(t *testing.T) {
190 ctx := testJava(t, `
191 java_library {
192 name: "foo",
193 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700194 libs: ["bar"],
195 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700196 }
197
198 java_library {
199 name: "bar",
200 srcs: ["b.java"],
201 }
202
203 java_library {
204 name: "baz",
205 srcs: ["c.java"],
206 }
Colin Crossd5934c82017-10-02 13:55:26 -0700207 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700208
Colin Cross4c428df2017-09-15 17:36:05 -0700209 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700210 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700211
212 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
213 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
214 }
215
Colin Cross1ee23172017-10-18 14:44:18 -0700216 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700217 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
218 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700219
Nan Zhanged19fc32017-10-19 13:06:22 -0700220 if !strings.Contains(javac.Args["classpath"], barTurbine) {
221 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700222 }
223
Nan Zhanged19fc32017-10-19 13:06:22 -0700224 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
225 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700226 }
227
228 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
229 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700230 }
231}
232
Colin Crossd5934c82017-10-02 13:55:26 -0700233func TestArchSpecific(t *testing.T) {
234 ctx := testJava(t, `
235 java_library {
236 name: "foo",
237 srcs: ["a.java"],
238 target: {
239 android: {
240 srcs: ["b.java"],
241 },
242 },
243 }
244 `)
245
246 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
247 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
248 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
249 }
250}
251
Colin Cross2acdae82017-09-15 19:44:24 -0700252var classpathTestcases = []struct {
253 name string
Colin Cross965714f2017-10-03 20:57:01 -0700254 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700255 host android.OsClass
256 properties string
257 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700258 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700259 classpath []string
260}{
261 {
262 name: "default",
263 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700264 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700265 classpath: []string{"ext", "framework", "okhttp"},
266 },
267 {
268 name: "blank sdk version",
269 properties: `sdk_version: "",`,
270 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700271 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700272 classpath: []string{"ext", "framework", "okhttp"},
273 },
274 {
Colin Cross4c428df2017-09-15 17:36:05 -0700275
Colin Cross2acdae82017-09-15 19:44:24 -0700276 name: "sdk v14",
277 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700278 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700279 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossfc3674a2017-09-18 17:41:52 -0700280 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700281 },
282 {
Colin Cross72bb3632017-07-13 16:23:21 -0700283
Colin Cross2acdae82017-09-15 19:44:24 -0700284 name: "current",
285 properties: `sdk_version: "current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700286 bootclasspath: []string{`""`},
287 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
288 classpath: []string{"prebuilts/sdk/current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700289 },
290 {
Colin Cross72bb3632017-07-13 16:23:21 -0700291
Colin Cross2acdae82017-09-15 19:44:24 -0700292 name: "system_current",
293 properties: `sdk_version: "system_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700294 bootclasspath: []string{`""`},
295 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
296 classpath: []string{"prebuilts/sdk/system_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700297 },
298 {
Colin Cross72bb3632017-07-13 16:23:21 -0700299
Sundong Ahn0926fae2017-10-17 16:34:51 +0900300 name: "system_14",
301 properties: `sdk_version: "system_14",`,
302 bootclasspath: []string{`""`},
303 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
304 classpath: []string{"prebuilts/sdk/system_14/android.jar"},
305 },
306 {
307
Colin Cross2acdae82017-09-15 19:44:24 -0700308 name: "test_current",
309 properties: `sdk_version: "test_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700310 bootclasspath: []string{`""`},
311 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
312 classpath: []string{"prebuilts/sdk/test_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700313 },
314 {
Colin Cross72bb3632017-07-13 16:23:21 -0700315
Colin Cross2acdae82017-09-15 19:44:24 -0700316 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700317 properties: `no_standard_libs: true, system_modules: "none"`,
318 system: "none",
319 bootclasspath: []string{`""`},
320 classpath: []string{},
321 },
322 {
323
324 name: "nostdlib system_modules",
325 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
326 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700327 bootclasspath: []string{`""`},
328 classpath: []string{},
329 },
330 {
Colin Cross72bb3632017-07-13 16:23:21 -0700331
Colin Cross2acdae82017-09-15 19:44:24 -0700332 name: "host default",
Colin Cross965714f2017-10-03 20:57:01 -0700333 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700334 properties: ``,
Colin Cross965714f2017-10-03 20:57:01 -0700335 host: android.Host,
Colin Cross2acdae82017-09-15 19:44:24 -0700336 classpath: []string{},
337 },
338 {
339 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700340 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700341 host: android.Host,
342 properties: `no_standard_libs: true`,
343 classpath: []string{},
344 },
Colin Cross965714f2017-10-03 20:57:01 -0700345 {
346
347 name: "host supported default",
348 host: android.Host,
349 properties: `host_supported: true,`,
350 classpath: []string{},
351 },
352 {
353 name: "host supported nostdlib",
354 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700355 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700356 classpath: []string{},
357 },
Colin Cross2acdae82017-09-15 19:44:24 -0700358}
Colin Cross72bb3632017-07-13 16:23:21 -0700359
Colin Cross2acdae82017-09-15 19:44:24 -0700360func TestClasspath(t *testing.T) {
361 for _, testcase := range classpathTestcases {
362 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700363 moduleType := "java_library"
364 if testcase.moduleType != "" {
365 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700366 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700367
368 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700369 name: "foo",
370 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700371 ` + testcase.properties + `
372 }`
373
374 variant := "android_common"
375 if testcase.host == android.Host {
376 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700377 }
Colin Cross72bb3632017-07-13 16:23:21 -0700378
Colin Cross2acdae82017-09-15 19:44:24 -0700379 convertModulesToPaths := func(cp []string) []string {
380 ret := make([]string, len(cp))
381 for i, e := range cp {
382 ret[i] = moduleToPath(e)
383 }
384 return ret
385 }
Colin Cross72bb3632017-07-13 16:23:21 -0700386
Colin Cross2acdae82017-09-15 19:44:24 -0700387 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
388 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700389
Colin Cross2acdae82017-09-15 19:44:24 -0700390 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700391 if bc != "" {
392 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700393 }
Colin Cross72bb3632017-07-13 16:23:21 -0700394
Colin Cross2acdae82017-09-15 19:44:24 -0700395 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700396 if c != "" {
397 c = "-classpath " + c
398 }
399 system := ""
400 if testcase.system == "none" {
401 system = "--system=none"
402 } else if testcase.system != "" {
403 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700404 }
405
Colin Cross1369cdb2017-09-29 17:58:17 -0700406 t.Run("1.8", func(t *testing.T) {
407 // Test default javac 1.8
408 ctx := testJava(t, bp)
Colin Cross2acdae82017-09-15 19:44:24 -0700409
Colin Cross1369cdb2017-09-29 17:58:17 -0700410 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
411
412 got := javac.Args["bootClasspath"]
413 if got != bc {
414 t.Errorf("bootclasspath expected %q != got %q", bc, got)
415 }
416
417 got = javac.Args["classpath"]
418 if got != c {
419 t.Errorf("classpath expected %q != got %q", c, got)
420 }
421
422 var deps []string
423 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
424 deps = append(deps, bootclasspath...)
425 }
426 deps = append(deps, classpath...)
427
428 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
429 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
430 }
431 })
432
433 // Test again with javac 1.9
434 t.Run("1.9", func(t *testing.T) {
Colin Cross527012a2017-11-30 22:56:16 -0800435 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
436 ctx := testContext(config, bp, nil)
437 run(t, ctx, config)
Colin Cross1369cdb2017-09-29 17:58:17 -0700438
439 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
440 got := javac.Args["bootClasspath"]
441 expected := system
442 if testcase.system == "bootclasspath" {
443 expected = bc
444 }
445 if got != expected {
446 t.Errorf("bootclasspath expected %q != got %q", expected, got)
447 }
448 })
Colin Cross2acdae82017-09-15 19:44:24 -0700449 })
Colin Cross72bb3632017-07-13 16:23:21 -0700450 }
451
Colin Cross72bb3632017-07-13 16:23:21 -0700452}
453
454func TestPrebuilts(t *testing.T) {
455 ctx := testJava(t, `
456 java_library {
457 name: "foo",
458 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700459 libs: ["bar"],
460 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700461 }
462
Colin Cross74d73e22017-08-02 11:05:49 -0700463 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700464 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700465 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700466 }
467
Colin Cross74d73e22017-08-02 11:05:49 -0700468 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700469 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700470 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700471 }
472 `)
473
Colin Cross4c428df2017-09-15 17:36:05 -0700474 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700475 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700476
477 bar := "a.jar"
478 if !strings.Contains(javac.Args["classpath"], bar) {
479 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
480 }
481
Colin Cross0a6e0072017-08-30 14:24:55 -0700482 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
483 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700484 }
485}
486
Colin Cross89536d42017-07-07 14:35:50 -0700487func TestDefaults(t *testing.T) {
488 ctx := testJava(t, `
489 java_defaults {
490 name: "defaults",
491 srcs: ["a.java"],
492 libs: ["bar"],
493 static_libs: ["baz"],
494 }
495
496 java_library {
497 name: "foo",
498 defaults: ["defaults"],
499 }
500
501 java_library {
502 name: "bar",
503 srcs: ["b.java"],
504 }
505
506 java_library {
507 name: "baz",
508 srcs: ["c.java"],
509 }
510 `)
511
Colin Cross4c428df2017-09-15 17:36:05 -0700512 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700513 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700514
515 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
516 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
517 }
518
Nan Zhanged19fc32017-10-19 13:06:22 -0700519 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
520 if !strings.Contains(javac.Args["classpath"], barTurbine) {
521 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700522 }
523
Colin Cross1ee23172017-10-18 14:44:18 -0700524 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700525 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
526 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700527 }
528}
529
Colin Cross0f37af02017-09-27 17:42:05 -0700530func TestResources(t *testing.T) {
531 var table = []struct {
532 name string
533 prop string
534 extra string
535 args string
536 }{
537 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700538 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700539 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800540 prop: `java_resource_dirs: ["java-res"]`,
541 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700542 },
543 {
544 // Test that a module with java_resources includes the files
545 name: "resource files",
Colin Cross824bee32017-11-22 17:27:51 -0800546 prop: `java_resources: ["java-res/a", "java-res/b"]`,
547 args: "-C . -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700548 },
549 {
550 // Test that a module with a filegroup in java_resources includes the files with the
551 // path prefix
552 name: "resource filegroup",
553 prop: `java_resources: [":foo-res"]`,
554 extra: `
555 filegroup {
556 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800557 path: "java-res",
558 srcs: ["java-res/a", "java-res/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700559 }`,
Colin Cross824bee32017-11-22 17:27:51 -0800560 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700561 },
562 {
563 // Test that a module with "include_srcs: true" includes its source files in the resources jar
564 name: "include sources",
565 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700566 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700567 },
568 }
569
570 for _, test := range table {
571 t.Run(test.name, func(t *testing.T) {
572 ctx := testJava(t, `
573 java_library {
574 name: "foo",
575 srcs: [
576 "a.java",
577 "b.java",
578 "c.java",
579 ],
580 `+test.prop+`,
581 }
582 `+test.extra)
583
Colin Cross1ee23172017-10-18 14:44:18 -0700584 foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
585 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700586
587 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
588 t.Errorf("foo combined jars %v does not contain %q",
589 foo.Inputs.Strings(), fooRes.Output.String())
590 }
591
Colin Crossaf9c55b2017-10-03 14:50:08 -0700592 if fooRes.Args["jarArgs"] != test.args {
593 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700594 fooRes.Args["jarArgs"], test.args)
595 }
596 })
597 }
598}
599
Colin Cross0532fb02017-10-02 16:57:40 -0700600func TestExcludeResources(t *testing.T) {
601 ctx := testJava(t, `
602 java_library {
603 name: "foo",
604 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800605 java_resource_dirs: ["java-res", "java-res2"],
606 exclude_java_resource_dirs: ["java-res2"],
Colin Cross0532fb02017-10-02 16:57:40 -0700607 }
608
609 java_library {
610 name: "bar",
611 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800612 java_resources: ["java-res/*"],
613 exclude_java_resources: ["java-res/b"],
Colin Cross0532fb02017-10-02 16:57:40 -0700614 }
615 `)
616
Colin Cross1ee23172017-10-18 14:44:18 -0700617 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700618
Colin Cross824bee32017-11-22 17:27:51 -0800619 expected := "-C java-res -f java-res/a -f java-res/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700620 if fooRes.Args["jarArgs"] != expected {
621 t.Errorf("foo resource jar args %q is not %q",
622 fooRes.Args["jarArgs"], expected)
623
624 }
625
Colin Cross1ee23172017-10-18 14:44:18 -0700626 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700627
Colin Cross824bee32017-11-22 17:27:51 -0800628 expected = "-C . -f java-res/a"
Colin Cross0532fb02017-10-02 16:57:40 -0700629 if barRes.Args["jarArgs"] != expected {
630 t.Errorf("bar resource jar args %q is not %q",
631 barRes.Args["jarArgs"], expected)
632
633 }
634}
635
Colin Cross54190b32017-10-09 15:34:10 -0700636func TestGeneratedSources(t *testing.T) {
637 ctx := testJava(t, `
638 java_library {
639 name: "foo",
640 srcs: [
641 "a*.java",
642 ":gen",
643 "b*.java",
644 ],
645 }
646
647 genrule {
648 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800649 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700650 out: ["gen.java"],
651 }
652 `)
653
654 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
655 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
656
Colin Cross15e86d92017-10-20 15:07:08 -0700657 if filepath.Base(genrule.Output.String()) != "gen.java" {
658 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700659 }
660
661 if len(javac.Inputs) != 3 ||
662 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700663 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700664 javac.Inputs[2].String() != "b.java" {
665 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
666 }
667}
668
Colin Cross93e85952017-08-15 13:34:18 -0700669func TestKotlin(t *testing.T) {
670 ctx := testJava(t, `
671 java_library {
672 name: "foo",
673 srcs: ["a.java", "b.kt"],
674 }
Colin Cross715d7112017-10-25 20:32:18 -0700675
676 java_library {
677 name: "bar",
678 srcs: ["b.kt"],
679 }
Colin Cross93e85952017-08-15 13:34:18 -0700680 `)
681
682 kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
683 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Colin Cross1ee23172017-10-18 14:44:18 -0700684 jar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700685
686 if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" ||
687 kotlinc.Inputs[1].String() != "b.kt" {
688 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs)
689 }
690
691 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
692 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
693 }
694
695 if !strings.Contains(javac.Args["classpath"], kotlinc.Output.String()) {
696 t.Errorf("foo classpath %v does not contain %q",
697 javac.Args["classpath"], kotlinc.Output.String())
698 }
699
700 if !inList(kotlinc.Output.String(), jar.Inputs.Strings()) {
701 t.Errorf("foo jar inputs %v does not contain %q",
702 jar.Inputs.Strings(), kotlinc.Output.String())
703 }
Colin Cross715d7112017-10-25 20:32:18 -0700704
705 kotlinc = ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
706 jar = ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
707
708 if len(kotlinc.Inputs) != 1 || kotlinc.Inputs[0].String() != "b.kt" {
709 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, kotlinc.Inputs)
710 }
Colin Cross93e85952017-08-15 13:34:18 -0700711}
712
Nan Zhang61eaedb2017-11-02 13:28:15 -0700713func TestTurbine(t *testing.T) {
714 ctx := testJava(t, `
715 java_library {
716 name: "foo",
717 srcs: ["a.java"],
718 }
719
720 java_library {
721 name: "bar",
722 srcs: ["b.java"],
723 static_libs: ["foo"],
724 }
725
726 java_library {
727 name: "baz",
728 srcs: ["c.java"],
729 libs: ["bar"],
730 sdk_version: "14",
731 }
732 `)
733
734 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
735 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
736 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
737 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
738 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
739
740 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
741 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
742 }
743
744 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
745 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
746 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
747 }
748 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
749 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
750 }
751 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
752 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
753 }
754 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/android.jar") {
755 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
756 "prebuilts/sdk/14/android.jar")
757 }
758}
759
760func TestSharding(t *testing.T) {
761 ctx := testJava(t, `
762 java_library {
763 name: "bar",
764 srcs: ["a.java","b.java","c.java"],
765 javac_shard_size: 1
766 }
767 `)
768
769 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
770 for i := 0; i < 3; i++ {
771 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
772 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
773 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
774 }
775 }
776}
777
Colin Cross72bb3632017-07-13 16:23:21 -0700778func fail(t *testing.T, errs []error) {
779 if len(errs) > 0 {
780 for _, err := range errs {
781 t.Error(err)
782 }
783 t.FailNow()
784 }
785}