blob: 82eff1e93386306218104303967e26e6645d4fc6 [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 Cross1369cdb2017-09-29 17:58:17 -070055 return testJavaWithEnv(t, bp, nil)
56}
57
58func testJavaWithEnv(t *testing.T, bp string, env map[string]string) *android.TestContext {
59 config := android.TestArchConfig(buildDir, env)
Colin Cross72bb3632017-07-13 16:23:21 -070060
Colin Cross4c428df2017-09-15 17:36:05 -070061 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070062 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Crossa60ead82017-10-02 18:10:21 -070063 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true)))
Colin Cross2acdae82017-09-15 19:44:24 -070064 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070065 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070066 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070067 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross0f37af02017-09-27 17:42:05 -070068 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070069 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070070 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
71 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070072 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross72bb3632017-07-13 16:23:21 -070073 ctx.Register()
74
Colin Cross4c428df2017-09-15 17:36:05 -070075 extraModules := []string{
76 "core-oj",
77 "core-libart",
78 "framework",
79 "ext",
80 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -070081 "android_stubs_current",
82 "android_system_stubs_current",
83 "android_test_stubs_current",
Colin Cross93e85952017-08-15 13:34:18 -070084 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -070085 }
Colin Cross72bb3632017-07-13 16:23:21 -070086
87 for _, extra := range extraModules {
88 bp += fmt.Sprintf(`
89 java_library {
90 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -070091 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -070092 no_standard_libs: true,
Colin Cross1369cdb2017-09-29 17:58:17 -070093 system_modules: "core-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -070094 }
95 `, extra)
96 }
97
Colin Cross1369cdb2017-09-29 17:58:17 -070098 if config.TargetOpenJDK9() {
99 systemModules := []string{
100 "core-system-modules",
101 "android_stubs_current_system_modules",
102 "android_system_stubs_current_system_modules",
103 "android_test_stubs_current_system_modules",
104 }
105
106 for _, extra := range systemModules {
107 bp += fmt.Sprintf(`
108 java_system_modules {
109 name: "%s",
110 }
111 `, extra)
112 }
113 }
114
Colin Cross72bb3632017-07-13 16:23:21 -0700115 ctx.MockFileSystem(map[string][]byte{
Colin Crossfc3674a2017-09-18 17:41:52 -0700116 "Android.bp": []byte(bp),
117 "a.java": nil,
118 "b.java": nil,
119 "c.java": nil,
Colin Cross93e85952017-08-15 13:34:18 -0700120 "b.kt": nil,
Colin Crossfc3674a2017-09-18 17:41:52 -0700121 "a.jar": nil,
122 "b.jar": nil,
Colin Cross0f37af02017-09-27 17:42:05 -0700123 "res/a": nil,
124 "res/b": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700125 "res2/a": nil,
126
Colin Cross2ebc4762017-10-20 14:00:31 -0700127 "prebuilts/sdk/14/android.jar": nil,
128 "prebuilts/sdk/14/framework.aidl": nil,
129 "prebuilts/sdk/current/android.jar": nil,
130 "prebuilts/sdk/current/framework.aidl": nil,
131 "prebuilts/sdk/system_current/android.jar": nil,
132 "prebuilts/sdk/system_current/framework.aidl": nil,
133 "prebuilts/sdk/test_current/android.jar": nil,
134 "prebuilts/sdk/test_current/framework.aidl": nil,
Colin Cross72bb3632017-07-13 16:23:21 -0700135 })
136
137 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
138 fail(t, errs)
139 _, errs = ctx.PrepareBuildActions(config)
140 fail(t, errs)
141
142 return ctx
143}
144
Colin Cross2acdae82017-09-15 19:44:24 -0700145func moduleToPath(name string) string {
146 switch {
147 case name == `""`:
148 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700149 case strings.HasSuffix(name, ".jar"):
150 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700151 case name == "android_stubs_current" || name == "android_system_stubs_current" ||
152 name == "android_test_stubs_current":
Colin Cross1ee23172017-10-18 14:44:18 -0700153 return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700154 default:
155 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700156 }
157}
158
Colin Cross72bb3632017-07-13 16:23:21 -0700159func TestSimple(t *testing.T) {
160 ctx := testJava(t, `
161 java_library {
162 name: "foo",
163 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700164 libs: ["bar"],
165 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700166 }
167
168 java_library {
169 name: "bar",
170 srcs: ["b.java"],
171 }
172
173 java_library {
174 name: "baz",
175 srcs: ["c.java"],
176 }
Colin Crossd5934c82017-10-02 13:55:26 -0700177 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700178
Colin Cross4c428df2017-09-15 17:36:05 -0700179 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700180 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700181
182 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
183 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
184 }
185
Colin Cross1ee23172017-10-18 14:44:18 -0700186 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700187 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
188 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700189
Nan Zhanged19fc32017-10-19 13:06:22 -0700190 if !strings.Contains(javac.Args["classpath"], barTurbine) {
191 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700192 }
193
Nan Zhanged19fc32017-10-19 13:06:22 -0700194 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
195 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700196 }
197
198 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
199 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700200 }
201}
202
Colin Crossd5934c82017-10-02 13:55:26 -0700203func TestArchSpecific(t *testing.T) {
204 ctx := testJava(t, `
205 java_library {
206 name: "foo",
207 srcs: ["a.java"],
208 target: {
209 android: {
210 srcs: ["b.java"],
211 },
212 },
213 }
214 `)
215
216 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
217 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
218 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
219 }
220}
221
Colin Cross2acdae82017-09-15 19:44:24 -0700222var classpathTestcases = []struct {
223 name string
Colin Cross965714f2017-10-03 20:57:01 -0700224 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700225 host android.OsClass
226 properties string
227 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700228 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700229 classpath []string
230}{
231 {
232 name: "default",
233 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700234 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700235 classpath: []string{"ext", "framework", "okhttp"},
236 },
237 {
238 name: "blank sdk version",
239 properties: `sdk_version: "",`,
240 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700241 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700242 classpath: []string{"ext", "framework", "okhttp"},
243 },
244 {
Colin Cross4c428df2017-09-15 17:36:05 -0700245
Colin Cross2acdae82017-09-15 19:44:24 -0700246 name: "sdk v14",
247 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700248 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700249 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossfc3674a2017-09-18 17:41:52 -0700250 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700251 },
252 {
Colin Cross72bb3632017-07-13 16:23:21 -0700253
Colin Cross2acdae82017-09-15 19:44:24 -0700254 name: "current",
255 properties: `sdk_version: "current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700256 bootclasspath: []string{`""`},
257 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
258 classpath: []string{"prebuilts/sdk/current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700259 },
260 {
Colin Cross72bb3632017-07-13 16:23:21 -0700261
Colin Cross2acdae82017-09-15 19:44:24 -0700262 name: "system_current",
263 properties: `sdk_version: "system_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700264 bootclasspath: []string{`""`},
265 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
266 classpath: []string{"prebuilts/sdk/system_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700267 },
268 {
Colin Cross72bb3632017-07-13 16:23:21 -0700269
Colin Cross2acdae82017-09-15 19:44:24 -0700270 name: "test_current",
271 properties: `sdk_version: "test_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700272 bootclasspath: []string{`""`},
273 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
274 classpath: []string{"prebuilts/sdk/test_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700275 },
276 {
Colin Cross72bb3632017-07-13 16:23:21 -0700277
Colin Cross2acdae82017-09-15 19:44:24 -0700278 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700279 properties: `no_standard_libs: true, system_modules: "none"`,
280 system: "none",
281 bootclasspath: []string{`""`},
282 classpath: []string{},
283 },
284 {
285
286 name: "nostdlib system_modules",
287 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
288 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700289 bootclasspath: []string{`""`},
290 classpath: []string{},
291 },
292 {
Colin Cross72bb3632017-07-13 16:23:21 -0700293
Colin Cross2acdae82017-09-15 19:44:24 -0700294 name: "host default",
Colin Cross965714f2017-10-03 20:57:01 -0700295 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700296 properties: ``,
Colin Cross965714f2017-10-03 20:57:01 -0700297 host: android.Host,
Colin Cross2acdae82017-09-15 19:44:24 -0700298 classpath: []string{},
299 },
300 {
301 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700302 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700303 host: android.Host,
304 properties: `no_standard_libs: true`,
305 classpath: []string{},
306 },
Colin Cross965714f2017-10-03 20:57:01 -0700307 {
308
309 name: "host supported default",
310 host: android.Host,
311 properties: `host_supported: true,`,
312 classpath: []string{},
313 },
314 {
315 name: "host supported nostdlib",
316 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700317 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700318 classpath: []string{},
319 },
Colin Cross2acdae82017-09-15 19:44:24 -0700320}
Colin Cross72bb3632017-07-13 16:23:21 -0700321
Colin Cross2acdae82017-09-15 19:44:24 -0700322func TestClasspath(t *testing.T) {
323 for _, testcase := range classpathTestcases {
324 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700325 moduleType := "java_library"
326 if testcase.moduleType != "" {
327 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700328 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700329
330 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700331 name: "foo",
332 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700333 ` + testcase.properties + `
334 }`
335
336 variant := "android_common"
337 if testcase.host == android.Host {
338 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700339 }
Colin Cross72bb3632017-07-13 16:23:21 -0700340
Colin Cross2acdae82017-09-15 19:44:24 -0700341 convertModulesToPaths := func(cp []string) []string {
342 ret := make([]string, len(cp))
343 for i, e := range cp {
344 ret[i] = moduleToPath(e)
345 }
346 return ret
347 }
Colin Cross72bb3632017-07-13 16:23:21 -0700348
Colin Cross2acdae82017-09-15 19:44:24 -0700349 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
350 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700351
Colin Cross2acdae82017-09-15 19:44:24 -0700352 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700353 if bc != "" {
354 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700355 }
Colin Cross72bb3632017-07-13 16:23:21 -0700356
Colin Cross2acdae82017-09-15 19:44:24 -0700357 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700358 if c != "" {
359 c = "-classpath " + c
360 }
361 system := ""
362 if testcase.system == "none" {
363 system = "--system=none"
364 } else if testcase.system != "" {
365 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700366 }
367
Colin Cross1369cdb2017-09-29 17:58:17 -0700368 t.Run("1.8", func(t *testing.T) {
369 // Test default javac 1.8
370 ctx := testJava(t, bp)
Colin Cross2acdae82017-09-15 19:44:24 -0700371
Colin Cross1369cdb2017-09-29 17:58:17 -0700372 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
373
374 got := javac.Args["bootClasspath"]
375 if got != bc {
376 t.Errorf("bootclasspath expected %q != got %q", bc, got)
377 }
378
379 got = javac.Args["classpath"]
380 if got != c {
381 t.Errorf("classpath expected %q != got %q", c, got)
382 }
383
384 var deps []string
385 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
386 deps = append(deps, bootclasspath...)
387 }
388 deps = append(deps, classpath...)
389
390 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
391 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
392 }
393 })
394
395 // Test again with javac 1.9
396 t.Run("1.9", func(t *testing.T) {
397 ctx := testJavaWithEnv(t, bp, map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
398
399 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
400 got := javac.Args["bootClasspath"]
401 expected := system
402 if testcase.system == "bootclasspath" {
403 expected = bc
404 }
405 if got != expected {
406 t.Errorf("bootclasspath expected %q != got %q", expected, got)
407 }
408 })
Colin Cross2acdae82017-09-15 19:44:24 -0700409 })
Colin Cross72bb3632017-07-13 16:23:21 -0700410 }
411
Colin Cross72bb3632017-07-13 16:23:21 -0700412}
413
414func TestPrebuilts(t *testing.T) {
415 ctx := testJava(t, `
416 java_library {
417 name: "foo",
418 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700419 libs: ["bar"],
420 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700421 }
422
Colin Cross74d73e22017-08-02 11:05:49 -0700423 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700424 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700425 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700426 }
427
Colin Cross74d73e22017-08-02 11:05:49 -0700428 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700429 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700430 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700431 }
432 `)
433
Colin Cross4c428df2017-09-15 17:36:05 -0700434 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700435 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700436
437 bar := "a.jar"
438 if !strings.Contains(javac.Args["classpath"], bar) {
439 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
440 }
441
Colin Cross0a6e0072017-08-30 14:24:55 -0700442 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
443 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700444 }
445}
446
Colin Cross89536d42017-07-07 14:35:50 -0700447func TestDefaults(t *testing.T) {
448 ctx := testJava(t, `
449 java_defaults {
450 name: "defaults",
451 srcs: ["a.java"],
452 libs: ["bar"],
453 static_libs: ["baz"],
454 }
455
456 java_library {
457 name: "foo",
458 defaults: ["defaults"],
459 }
460
461 java_library {
462 name: "bar",
463 srcs: ["b.java"],
464 }
465
466 java_library {
467 name: "baz",
468 srcs: ["c.java"],
469 }
470 `)
471
Colin Cross4c428df2017-09-15 17:36:05 -0700472 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700473 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700474
475 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
476 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
477 }
478
Nan Zhanged19fc32017-10-19 13:06:22 -0700479 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
480 if !strings.Contains(javac.Args["classpath"], barTurbine) {
481 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700482 }
483
Colin Cross1ee23172017-10-18 14:44:18 -0700484 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700485 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
486 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700487 }
488}
489
Colin Cross0f37af02017-09-27 17:42:05 -0700490func TestResources(t *testing.T) {
491 var table = []struct {
492 name string
493 prop string
494 extra string
495 args string
496 }{
497 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700498 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700499 name: "resource dirs",
500 prop: `java_resource_dirs: ["res"]`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700501 args: "-C res -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700502 },
503 {
504 // Test that a module with java_resources includes the files
505 name: "resource files",
506 prop: `java_resources: ["res/a", "res/b"]`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700507 args: "-C . -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700508 },
509 {
510 // Test that a module with a filegroup in java_resources includes the files with the
511 // path prefix
512 name: "resource filegroup",
513 prop: `java_resources: [":foo-res"]`,
514 extra: `
515 filegroup {
516 name: "foo-res",
517 path: "res",
518 srcs: ["res/a", "res/b"],
519 }`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700520 args: "-C res -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700521 },
522 {
523 // Test that a module with "include_srcs: true" includes its source files in the resources jar
524 name: "include sources",
525 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700526 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700527 },
528 }
529
530 for _, test := range table {
531 t.Run(test.name, func(t *testing.T) {
532 ctx := testJava(t, `
533 java_library {
534 name: "foo",
535 srcs: [
536 "a.java",
537 "b.java",
538 "c.java",
539 ],
540 `+test.prop+`,
541 }
542 `+test.extra)
543
Colin Cross1ee23172017-10-18 14:44:18 -0700544 foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
545 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700546
547 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
548 t.Errorf("foo combined jars %v does not contain %q",
549 foo.Inputs.Strings(), fooRes.Output.String())
550 }
551
Colin Crossaf9c55b2017-10-03 14:50:08 -0700552 if fooRes.Args["jarArgs"] != test.args {
553 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700554 fooRes.Args["jarArgs"], test.args)
555 }
556 })
557 }
558}
559
Colin Cross0532fb02017-10-02 16:57:40 -0700560func TestExcludeResources(t *testing.T) {
561 ctx := testJava(t, `
562 java_library {
563 name: "foo",
564 srcs: ["a.java"],
565 java_resource_dirs: ["res", "res2"],
566 exclude_java_resource_dirs: ["res2"],
567 }
568
569 java_library {
570 name: "bar",
571 srcs: ["a.java"],
572 java_resources: ["res/*"],
573 exclude_java_resources: ["res/b"],
574 }
575 `)
576
Colin Cross1ee23172017-10-18 14:44:18 -0700577 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700578
Colin Crossaf9c55b2017-10-03 14:50:08 -0700579 expected := "-C res -f res/a -f res/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700580 if fooRes.Args["jarArgs"] != expected {
581 t.Errorf("foo resource jar args %q is not %q",
582 fooRes.Args["jarArgs"], expected)
583
584 }
585
Colin Cross1ee23172017-10-18 14:44:18 -0700586 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700587
588 expected = "-C . -f res/a"
589 if barRes.Args["jarArgs"] != expected {
590 t.Errorf("bar resource jar args %q is not %q",
591 barRes.Args["jarArgs"], expected)
592
593 }
594}
595
Colin Cross54190b32017-10-09 15:34:10 -0700596func TestGeneratedSources(t *testing.T) {
597 ctx := testJava(t, `
598 java_library {
599 name: "foo",
600 srcs: [
601 "a*.java",
602 ":gen",
603 "b*.java",
604 ],
605 }
606
607 genrule {
608 name: "gen",
609 tool_files: ["res/a"],
610 out: ["gen.java"],
611 }
612 `)
613
614 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
615 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
616
Colin Cross15e86d92017-10-20 15:07:08 -0700617 if filepath.Base(genrule.Output.String()) != "gen.java" {
618 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700619 }
620
621 if len(javac.Inputs) != 3 ||
622 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700623 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700624 javac.Inputs[2].String() != "b.java" {
625 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
626 }
627}
628
Colin Cross93e85952017-08-15 13:34:18 -0700629func TestKotlin(t *testing.T) {
630 ctx := testJava(t, `
631 java_library {
632 name: "foo",
633 srcs: ["a.java", "b.kt"],
634 }
Colin Cross715d7112017-10-25 20:32:18 -0700635
636 java_library {
637 name: "bar",
638 srcs: ["b.kt"],
639 }
Colin Cross93e85952017-08-15 13:34:18 -0700640 `)
641
642 kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
643 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Colin Cross1ee23172017-10-18 14:44:18 -0700644 jar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700645
646 if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" ||
647 kotlinc.Inputs[1].String() != "b.kt" {
648 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs)
649 }
650
651 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
652 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
653 }
654
655 if !strings.Contains(javac.Args["classpath"], kotlinc.Output.String()) {
656 t.Errorf("foo classpath %v does not contain %q",
657 javac.Args["classpath"], kotlinc.Output.String())
658 }
659
660 if !inList(kotlinc.Output.String(), jar.Inputs.Strings()) {
661 t.Errorf("foo jar inputs %v does not contain %q",
662 jar.Inputs.Strings(), kotlinc.Output.String())
663 }
Colin Cross715d7112017-10-25 20:32:18 -0700664
665 kotlinc = ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
666 jar = ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
667
668 if len(kotlinc.Inputs) != 1 || kotlinc.Inputs[0].String() != "b.kt" {
669 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, kotlinc.Inputs)
670 }
Colin Cross93e85952017-08-15 13:34:18 -0700671}
672
Nan Zhang61eaedb2017-11-02 13:28:15 -0700673func TestTurbine(t *testing.T) {
674 ctx := testJava(t, `
675 java_library {
676 name: "foo",
677 srcs: ["a.java"],
678 }
679
680 java_library {
681 name: "bar",
682 srcs: ["b.java"],
683 static_libs: ["foo"],
684 }
685
686 java_library {
687 name: "baz",
688 srcs: ["c.java"],
689 libs: ["bar"],
690 sdk_version: "14",
691 }
692 `)
693
694 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
695 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
696 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
697 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
698 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
699
700 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
701 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
702 }
703
704 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
705 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
706 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
707 }
708 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
709 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
710 }
711 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
712 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
713 }
714 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/android.jar") {
715 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
716 "prebuilts/sdk/14/android.jar")
717 }
718}
719
720func TestSharding(t *testing.T) {
721 ctx := testJava(t, `
722 java_library {
723 name: "bar",
724 srcs: ["a.java","b.java","c.java"],
725 javac_shard_size: 1
726 }
727 `)
728
729 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
730 for i := 0; i < 3; i++ {
731 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
732 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
733 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
734 }
735 }
736}
737
Colin Cross72bb3632017-07-13 16:23:21 -0700738func fail(t *testing.T, errs []error) {
739 if len(errs) > 0 {
740 for _, err := range errs {
741 t.Error(err)
742 }
743 t.FailNow()
744 }
745}