blob: 143e82cd020bcb6d0d99a2813666e8378c910881 [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 Cross72bb3632017-07-13 16:23:21 -070074 ctx.Register()
75
Colin Cross4c428df2017-09-15 17:36:05 -070076 extraModules := []string{
77 "core-oj",
78 "core-libart",
79 "framework",
80 "ext",
81 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -070082 "android_stubs_current",
83 "android_system_stubs_current",
84 "android_test_stubs_current",
Colin Cross93e85952017-08-15 13:34:18 -070085 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -070086 }
Colin Cross72bb3632017-07-13 16:23:21 -070087
88 for _, extra := range extraModules {
89 bp += fmt.Sprintf(`
90 java_library {
91 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -070092 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -070093 no_standard_libs: true,
Colin Cross1369cdb2017-09-29 17:58:17 -070094 system_modules: "core-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -070095 }
96 `, extra)
97 }
98
Colin Cross1369cdb2017-09-29 17:58:17 -070099 if config.TargetOpenJDK9() {
100 systemModules := []string{
101 "core-system-modules",
102 "android_stubs_current_system_modules",
103 "android_system_stubs_current_system_modules",
104 "android_test_stubs_current_system_modules",
105 }
106
107 for _, extra := range systemModules {
108 bp += fmt.Sprintf(`
109 java_system_modules {
110 name: "%s",
111 }
112 `, extra)
113 }
114 }
115
Colin Cross824bee32017-11-22 17:27:51 -0800116 mockFS := map[string][]byte{
117 "Android.bp": []byte(bp),
118 "a.java": nil,
119 "b.java": nil,
120 "c.java": nil,
121 "b.kt": nil,
122 "a.jar": nil,
123 "b.jar": nil,
124 "java-res/a": nil,
125 "java-res/b": nil,
126 "java-res2/a": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700127
Colin Cross2ebc4762017-10-20 14:00:31 -0700128 "prebuilts/sdk/14/android.jar": nil,
129 "prebuilts/sdk/14/framework.aidl": nil,
130 "prebuilts/sdk/current/android.jar": nil,
131 "prebuilts/sdk/current/framework.aidl": nil,
132 "prebuilts/sdk/system_current/android.jar": nil,
133 "prebuilts/sdk/system_current/framework.aidl": nil,
134 "prebuilts/sdk/test_current/android.jar": nil,
135 "prebuilts/sdk/test_current/framework.aidl": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800136 }
137
138 for k, v := range fs {
139 mockFS[k] = v
140 }
141
142 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700143
Jeff Gastond3e141d2017-08-08 17:46:01 -0700144 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Colin Cross72bb3632017-07-13 16:23:21 -0700145 fail(t, errs)
146 _, errs = ctx.PrepareBuildActions(config)
147 fail(t, errs)
148
149 return ctx
150}
151
Colin Cross2acdae82017-09-15 19:44:24 -0700152func moduleToPath(name string) string {
153 switch {
154 case name == `""`:
155 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700156 case strings.HasSuffix(name, ".jar"):
157 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700158 case name == "android_stubs_current" || name == "android_system_stubs_current" ||
159 name == "android_test_stubs_current":
Colin Cross1ee23172017-10-18 14:44:18 -0700160 return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700161 default:
162 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700163 }
164}
165
Colin Cross72bb3632017-07-13 16:23:21 -0700166func TestSimple(t *testing.T) {
167 ctx := testJava(t, `
168 java_library {
169 name: "foo",
170 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700171 libs: ["bar"],
172 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700173 }
174
175 java_library {
176 name: "bar",
177 srcs: ["b.java"],
178 }
179
180 java_library {
181 name: "baz",
182 srcs: ["c.java"],
183 }
Colin Crossd5934c82017-10-02 13:55:26 -0700184 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700185
Colin Cross4c428df2017-09-15 17:36:05 -0700186 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700187 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700188
189 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
190 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
191 }
192
Colin Cross1ee23172017-10-18 14:44:18 -0700193 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700194 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
195 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700196
Nan Zhanged19fc32017-10-19 13:06:22 -0700197 if !strings.Contains(javac.Args["classpath"], barTurbine) {
198 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700199 }
200
Nan Zhanged19fc32017-10-19 13:06:22 -0700201 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
202 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700203 }
204
205 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
206 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700207 }
208}
209
Colin Crossd5934c82017-10-02 13:55:26 -0700210func TestArchSpecific(t *testing.T) {
211 ctx := testJava(t, `
212 java_library {
213 name: "foo",
214 srcs: ["a.java"],
215 target: {
216 android: {
217 srcs: ["b.java"],
218 },
219 },
220 }
221 `)
222
223 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
224 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
225 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
226 }
227}
228
Colin Cross2acdae82017-09-15 19:44:24 -0700229var classpathTestcases = []struct {
230 name string
Colin Cross965714f2017-10-03 20:57:01 -0700231 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700232 host android.OsClass
233 properties string
234 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700235 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700236 classpath []string
237}{
238 {
239 name: "default",
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 {
245 name: "blank sdk version",
246 properties: `sdk_version: "",`,
247 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700248 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700249 classpath: []string{"ext", "framework", "okhttp"},
250 },
251 {
Colin Cross4c428df2017-09-15 17:36:05 -0700252
Colin Cross2acdae82017-09-15 19:44:24 -0700253 name: "sdk v14",
254 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700255 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700256 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossfc3674a2017-09-18 17:41:52 -0700257 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700258 },
259 {
Colin Cross72bb3632017-07-13 16:23:21 -0700260
Colin Cross2acdae82017-09-15 19:44:24 -0700261 name: "current",
262 properties: `sdk_version: "current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700263 bootclasspath: []string{`""`},
264 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
265 classpath: []string{"prebuilts/sdk/current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700266 },
267 {
Colin Cross72bb3632017-07-13 16:23:21 -0700268
Colin Cross2acdae82017-09-15 19:44:24 -0700269 name: "system_current",
270 properties: `sdk_version: "system_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700271 bootclasspath: []string{`""`},
272 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
273 classpath: []string{"prebuilts/sdk/system_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700274 },
275 {
Colin Cross72bb3632017-07-13 16:23:21 -0700276
Colin Cross2acdae82017-09-15 19:44:24 -0700277 name: "test_current",
278 properties: `sdk_version: "test_current",`,
Colin Cross2ebc4762017-10-20 14:00:31 -0700279 bootclasspath: []string{`""`},
280 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
281 classpath: []string{"prebuilts/sdk/test_current/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700282 },
283 {
Colin Cross72bb3632017-07-13 16:23:21 -0700284
Colin Cross2acdae82017-09-15 19:44:24 -0700285 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700286 properties: `no_standard_libs: true, system_modules: "none"`,
287 system: "none",
288 bootclasspath: []string{`""`},
289 classpath: []string{},
290 },
291 {
292
293 name: "nostdlib system_modules",
294 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
295 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700296 bootclasspath: []string{`""`},
297 classpath: []string{},
298 },
299 {
Colin Cross72bb3632017-07-13 16:23:21 -0700300
Colin Cross2acdae82017-09-15 19:44:24 -0700301 name: "host default",
Colin Cross965714f2017-10-03 20:57:01 -0700302 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700303 properties: ``,
Colin Cross965714f2017-10-03 20:57:01 -0700304 host: android.Host,
Colin Cross2acdae82017-09-15 19:44:24 -0700305 classpath: []string{},
306 },
307 {
308 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700309 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700310 host: android.Host,
311 properties: `no_standard_libs: true`,
312 classpath: []string{},
313 },
Colin Cross965714f2017-10-03 20:57:01 -0700314 {
315
316 name: "host supported default",
317 host: android.Host,
318 properties: `host_supported: true,`,
319 classpath: []string{},
320 },
321 {
322 name: "host supported nostdlib",
323 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700324 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700325 classpath: []string{},
326 },
Colin Cross2acdae82017-09-15 19:44:24 -0700327}
Colin Cross72bb3632017-07-13 16:23:21 -0700328
Colin Cross2acdae82017-09-15 19:44:24 -0700329func TestClasspath(t *testing.T) {
330 for _, testcase := range classpathTestcases {
331 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700332 moduleType := "java_library"
333 if testcase.moduleType != "" {
334 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700335 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700336
337 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700338 name: "foo",
339 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700340 ` + testcase.properties + `
341 }`
342
343 variant := "android_common"
344 if testcase.host == android.Host {
345 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700346 }
Colin Cross72bb3632017-07-13 16:23:21 -0700347
Colin Cross2acdae82017-09-15 19:44:24 -0700348 convertModulesToPaths := func(cp []string) []string {
349 ret := make([]string, len(cp))
350 for i, e := range cp {
351 ret[i] = moduleToPath(e)
352 }
353 return ret
354 }
Colin Cross72bb3632017-07-13 16:23:21 -0700355
Colin Cross2acdae82017-09-15 19:44:24 -0700356 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
357 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700358
Colin Cross2acdae82017-09-15 19:44:24 -0700359 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700360 if bc != "" {
361 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700362 }
Colin Cross72bb3632017-07-13 16:23:21 -0700363
Colin Cross2acdae82017-09-15 19:44:24 -0700364 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700365 if c != "" {
366 c = "-classpath " + c
367 }
368 system := ""
369 if testcase.system == "none" {
370 system = "--system=none"
371 } else if testcase.system != "" {
372 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700373 }
374
Colin Cross1369cdb2017-09-29 17:58:17 -0700375 t.Run("1.8", func(t *testing.T) {
376 // Test default javac 1.8
377 ctx := testJava(t, bp)
Colin Cross2acdae82017-09-15 19:44:24 -0700378
Colin Cross1369cdb2017-09-29 17:58:17 -0700379 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
380
381 got := javac.Args["bootClasspath"]
382 if got != bc {
383 t.Errorf("bootclasspath expected %q != got %q", bc, got)
384 }
385
386 got = javac.Args["classpath"]
387 if got != c {
388 t.Errorf("classpath expected %q != got %q", c, got)
389 }
390
391 var deps []string
392 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
393 deps = append(deps, bootclasspath...)
394 }
395 deps = append(deps, classpath...)
396
397 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
398 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
399 }
400 })
401
402 // Test again with javac 1.9
403 t.Run("1.9", func(t *testing.T) {
Colin Cross824bee32017-11-22 17:27:51 -0800404 ctx := testJavaWithEnvFs(t, bp, map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"}, nil)
Colin Cross1369cdb2017-09-29 17:58:17 -0700405
406 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
407 got := javac.Args["bootClasspath"]
408 expected := system
409 if testcase.system == "bootclasspath" {
410 expected = bc
411 }
412 if got != expected {
413 t.Errorf("bootclasspath expected %q != got %q", expected, got)
414 }
415 })
Colin Cross2acdae82017-09-15 19:44:24 -0700416 })
Colin Cross72bb3632017-07-13 16:23:21 -0700417 }
418
Colin Cross72bb3632017-07-13 16:23:21 -0700419}
420
421func TestPrebuilts(t *testing.T) {
422 ctx := testJava(t, `
423 java_library {
424 name: "foo",
425 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700426 libs: ["bar"],
427 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700428 }
429
Colin Cross74d73e22017-08-02 11:05:49 -0700430 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700431 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700432 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700433 }
434
Colin Cross74d73e22017-08-02 11:05:49 -0700435 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700436 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700437 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700438 }
439 `)
440
Colin Cross4c428df2017-09-15 17:36:05 -0700441 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700442 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700443
444 bar := "a.jar"
445 if !strings.Contains(javac.Args["classpath"], bar) {
446 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
447 }
448
Colin Cross0a6e0072017-08-30 14:24:55 -0700449 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
450 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700451 }
452}
453
Colin Cross89536d42017-07-07 14:35:50 -0700454func TestDefaults(t *testing.T) {
455 ctx := testJava(t, `
456 java_defaults {
457 name: "defaults",
458 srcs: ["a.java"],
459 libs: ["bar"],
460 static_libs: ["baz"],
461 }
462
463 java_library {
464 name: "foo",
465 defaults: ["defaults"],
466 }
467
468 java_library {
469 name: "bar",
470 srcs: ["b.java"],
471 }
472
473 java_library {
474 name: "baz",
475 srcs: ["c.java"],
476 }
477 `)
478
Colin Cross4c428df2017-09-15 17:36:05 -0700479 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700480 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700481
482 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
483 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
484 }
485
Nan Zhanged19fc32017-10-19 13:06:22 -0700486 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
487 if !strings.Contains(javac.Args["classpath"], barTurbine) {
488 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700489 }
490
Colin Cross1ee23172017-10-18 14:44:18 -0700491 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700492 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
493 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700494 }
495}
496
Colin Cross0f37af02017-09-27 17:42:05 -0700497func TestResources(t *testing.T) {
498 var table = []struct {
499 name string
500 prop string
501 extra string
502 args string
503 }{
504 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700505 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700506 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800507 prop: `java_resource_dirs: ["java-res"]`,
508 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700509 },
510 {
511 // Test that a module with java_resources includes the files
512 name: "resource files",
Colin Cross824bee32017-11-22 17:27:51 -0800513 prop: `java_resources: ["java-res/a", "java-res/b"]`,
514 args: "-C . -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700515 },
516 {
517 // Test that a module with a filegroup in java_resources includes the files with the
518 // path prefix
519 name: "resource filegroup",
520 prop: `java_resources: [":foo-res"]`,
521 extra: `
522 filegroup {
523 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800524 path: "java-res",
525 srcs: ["java-res/a", "java-res/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700526 }`,
Colin Cross824bee32017-11-22 17:27:51 -0800527 args: "-C java-res -f java-res/a -f java-res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700528 },
529 {
530 // Test that a module with "include_srcs: true" includes its source files in the resources jar
531 name: "include sources",
532 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700533 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700534 },
535 }
536
537 for _, test := range table {
538 t.Run(test.name, func(t *testing.T) {
539 ctx := testJava(t, `
540 java_library {
541 name: "foo",
542 srcs: [
543 "a.java",
544 "b.java",
545 "c.java",
546 ],
547 `+test.prop+`,
548 }
549 `+test.extra)
550
Colin Cross1ee23172017-10-18 14:44:18 -0700551 foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
552 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700553
554 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
555 t.Errorf("foo combined jars %v does not contain %q",
556 foo.Inputs.Strings(), fooRes.Output.String())
557 }
558
Colin Crossaf9c55b2017-10-03 14:50:08 -0700559 if fooRes.Args["jarArgs"] != test.args {
560 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700561 fooRes.Args["jarArgs"], test.args)
562 }
563 })
564 }
565}
566
Colin Cross0532fb02017-10-02 16:57:40 -0700567func TestExcludeResources(t *testing.T) {
568 ctx := testJava(t, `
569 java_library {
570 name: "foo",
571 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800572 java_resource_dirs: ["java-res", "java-res2"],
573 exclude_java_resource_dirs: ["java-res2"],
Colin Cross0532fb02017-10-02 16:57:40 -0700574 }
575
576 java_library {
577 name: "bar",
578 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800579 java_resources: ["java-res/*"],
580 exclude_java_resources: ["java-res/b"],
Colin Cross0532fb02017-10-02 16:57:40 -0700581 }
582 `)
583
Colin Cross1ee23172017-10-18 14:44:18 -0700584 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700585
Colin Cross824bee32017-11-22 17:27:51 -0800586 expected := "-C java-res -f java-res/a -f java-res/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700587 if fooRes.Args["jarArgs"] != expected {
588 t.Errorf("foo resource jar args %q is not %q",
589 fooRes.Args["jarArgs"], expected)
590
591 }
592
Colin Cross1ee23172017-10-18 14:44:18 -0700593 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700594
Colin Cross824bee32017-11-22 17:27:51 -0800595 expected = "-C . -f java-res/a"
Colin Cross0532fb02017-10-02 16:57:40 -0700596 if barRes.Args["jarArgs"] != expected {
597 t.Errorf("bar resource jar args %q is not %q",
598 barRes.Args["jarArgs"], expected)
599
600 }
601}
602
Colin Cross54190b32017-10-09 15:34:10 -0700603func TestGeneratedSources(t *testing.T) {
604 ctx := testJava(t, `
605 java_library {
606 name: "foo",
607 srcs: [
608 "a*.java",
609 ":gen",
610 "b*.java",
611 ],
612 }
613
614 genrule {
615 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800616 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700617 out: ["gen.java"],
618 }
619 `)
620
621 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
622 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
623
Colin Cross15e86d92017-10-20 15:07:08 -0700624 if filepath.Base(genrule.Output.String()) != "gen.java" {
625 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700626 }
627
628 if len(javac.Inputs) != 3 ||
629 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700630 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700631 javac.Inputs[2].String() != "b.java" {
632 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
633 }
634}
635
Colin Cross93e85952017-08-15 13:34:18 -0700636func TestKotlin(t *testing.T) {
637 ctx := testJava(t, `
638 java_library {
639 name: "foo",
640 srcs: ["a.java", "b.kt"],
641 }
Colin Cross715d7112017-10-25 20:32:18 -0700642
643 java_library {
644 name: "bar",
645 srcs: ["b.kt"],
646 }
Colin Cross93e85952017-08-15 13:34:18 -0700647 `)
648
649 kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
650 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Colin Cross1ee23172017-10-18 14:44:18 -0700651 jar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700652
653 if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" ||
654 kotlinc.Inputs[1].String() != "b.kt" {
655 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs)
656 }
657
658 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
659 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
660 }
661
662 if !strings.Contains(javac.Args["classpath"], kotlinc.Output.String()) {
663 t.Errorf("foo classpath %v does not contain %q",
664 javac.Args["classpath"], kotlinc.Output.String())
665 }
666
667 if !inList(kotlinc.Output.String(), jar.Inputs.Strings()) {
668 t.Errorf("foo jar inputs %v does not contain %q",
669 jar.Inputs.Strings(), kotlinc.Output.String())
670 }
Colin Cross715d7112017-10-25 20:32:18 -0700671
672 kotlinc = ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
673 jar = ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
674
675 if len(kotlinc.Inputs) != 1 || kotlinc.Inputs[0].String() != "b.kt" {
676 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, kotlinc.Inputs)
677 }
Colin Cross93e85952017-08-15 13:34:18 -0700678}
679
Nan Zhang61eaedb2017-11-02 13:28:15 -0700680func TestTurbine(t *testing.T) {
681 ctx := testJava(t, `
682 java_library {
683 name: "foo",
684 srcs: ["a.java"],
685 }
686
687 java_library {
688 name: "bar",
689 srcs: ["b.java"],
690 static_libs: ["foo"],
691 }
692
693 java_library {
694 name: "baz",
695 srcs: ["c.java"],
696 libs: ["bar"],
697 sdk_version: "14",
698 }
699 `)
700
701 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
702 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
703 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
704 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
705 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
706
707 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
708 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
709 }
710
711 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
712 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
713 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
714 }
715 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
716 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
717 }
718 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
719 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
720 }
721 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/android.jar") {
722 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
723 "prebuilts/sdk/14/android.jar")
724 }
725}
726
727func TestSharding(t *testing.T) {
728 ctx := testJava(t, `
729 java_library {
730 name: "bar",
731 srcs: ["a.java","b.java","c.java"],
732 javac_shard_size: 1
733 }
734 `)
735
736 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
737 for i := 0; i < 3; i++ {
738 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
739 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
740 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
741 }
742 }
743}
744
Colin Cross72bb3632017-07-13 16:23:21 -0700745func fail(t *testing.T, errs []error) {
746 if len(errs) > 0 {
747 for _, err := range errs {
748 t.Error(err)
749 }
750 t.FailNow()
751 }
752}