blob: c2c7ee265092bb34935c1924be7c385d8db87004 [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"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
27)
28
29var buildDir string
30
31func setUp() {
32 var err error
33 buildDir, err = ioutil.TempDir("", "soong_java_test")
34 if err != nil {
35 panic(err)
36 }
37}
38
39func tearDown() {
40 os.RemoveAll(buildDir)
41}
42
43func TestMain(m *testing.M) {
44 run := func() int {
45 setUp()
46 defer tearDown()
47
48 return m.Run()
49 }
50
51 os.Exit(run())
52}
53
54func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross4c428df2017-09-15 17:36:05 -070055 config := android.TestArchConfig(buildDir)
Colin Cross72bb3632017-07-13 16:23:21 -070056
Colin Cross4c428df2017-09-15 17:36:05 -070057 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070058 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Crossa60ead82017-10-02 18:10:21 -070059 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true)))
Colin Cross2acdae82017-09-15 19:44:24 -070060 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070061 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070062 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross0f37af02017-09-27 17:42:05 -070063 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070064 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
65 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070066 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross72bb3632017-07-13 16:23:21 -070067 ctx.Register()
68
Colin Cross4c428df2017-09-15 17:36:05 -070069 extraModules := []string{
70 "core-oj",
71 "core-libart",
72 "framework",
73 "ext",
74 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -070075 "android_stubs_current",
76 "android_system_stubs_current",
77 "android_test_stubs_current",
Colin Cross4c428df2017-09-15 17:36:05 -070078 }
Colin Cross72bb3632017-07-13 16:23:21 -070079
80 for _, extra := range extraModules {
81 bp += fmt.Sprintf(`
82 java_library {
83 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -070084 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -070085 no_standard_libs: true,
Colin Cross72bb3632017-07-13 16:23:21 -070086 }
87 `, extra)
88 }
89
90 ctx.MockFileSystem(map[string][]byte{
Colin Crossfc3674a2017-09-18 17:41:52 -070091 "Android.bp": []byte(bp),
92 "a.java": nil,
93 "b.java": nil,
94 "c.java": nil,
95 "a.jar": nil,
96 "b.jar": nil,
Colin Cross0f37af02017-09-27 17:42:05 -070097 "res/a": nil,
98 "res/b": nil,
Colin Cross0532fb02017-10-02 16:57:40 -070099 "res2/a": nil,
100
Colin Crossfc3674a2017-09-18 17:41:52 -0700101 "prebuilts/sdk/14/android.jar": nil,
102 "prebuilts/sdk/14/framework.aidl": nil,
Colin Cross72bb3632017-07-13 16:23:21 -0700103 })
104
105 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
106 fail(t, errs)
107 _, errs = ctx.PrepareBuildActions(config)
108 fail(t, errs)
109
110 return ctx
111}
112
Colin Cross2acdae82017-09-15 19:44:24 -0700113func moduleToPath(name string) string {
114 switch {
115 case name == `""`:
116 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700117 case strings.HasSuffix(name, ".jar"):
118 return name
Colin Cross2acdae82017-09-15 19:44:24 -0700119 default:
Colin Cross8649b262017-09-27 18:03:17 -0700120 return filepath.Join(buildDir, ".intermediates", name, "android_common", "classes-compiled.jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700121 }
122}
123
Colin Cross72bb3632017-07-13 16:23:21 -0700124func TestSimple(t *testing.T) {
125 ctx := testJava(t, `
126 java_library {
127 name: "foo",
128 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700129 libs: ["bar"],
130 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700131 }
132
133 java_library {
134 name: "bar",
135 srcs: ["b.java"],
136 }
137
138 java_library {
139 name: "baz",
140 srcs: ["c.java"],
141 }
Colin Crossd5934c82017-10-02 13:55:26 -0700142 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700143
Colin Cross4c428df2017-09-15 17:36:05 -0700144 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
145 combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
Colin Cross72bb3632017-07-13 16:23:21 -0700146
147 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
148 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
149 }
150
Colin Cross8649b262017-09-27 18:03:17 -0700151 bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar")
152 baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700153
Colin Cross72bb3632017-07-13 16:23:21 -0700154 if !strings.Contains(javac.Args["classpath"], bar) {
155 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
156 }
157
Colin Cross0a6e0072017-08-30 14:24:55 -0700158 if !strings.Contains(javac.Args["classpath"], baz) {
159 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], baz)
160 }
161
162 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
163 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700164 }
165}
166
Colin Crossd5934c82017-10-02 13:55:26 -0700167func TestArchSpecific(t *testing.T) {
168 ctx := testJava(t, `
169 java_library {
170 name: "foo",
171 srcs: ["a.java"],
172 target: {
173 android: {
174 srcs: ["b.java"],
175 },
176 },
177 }
178 `)
179
180 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
181 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
182 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
183 }
184}
185
Colin Cross2acdae82017-09-15 19:44:24 -0700186var classpathTestcases = []struct {
187 name string
Colin Cross965714f2017-10-03 20:57:01 -0700188 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700189 host android.OsClass
190 properties string
191 bootclasspath []string
192 classpath []string
193}{
194 {
195 name: "default",
196 bootclasspath: []string{"core-oj", "core-libart"},
197 classpath: []string{"ext", "framework", "okhttp"},
198 },
199 {
200 name: "blank sdk version",
201 properties: `sdk_version: "",`,
202 bootclasspath: []string{"core-oj", "core-libart"},
203 classpath: []string{"ext", "framework", "okhttp"},
204 },
205 {
Colin Cross4c428df2017-09-15 17:36:05 -0700206
Colin Cross2acdae82017-09-15 19:44:24 -0700207 name: "sdk v14",
208 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700209 bootclasspath: []string{`""`},
210 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700211 },
212 {
Colin Cross72bb3632017-07-13 16:23:21 -0700213
Colin Cross2acdae82017-09-15 19:44:24 -0700214 name: "current",
215 properties: `sdk_version: "current",`,
216 bootclasspath: []string{"android_stubs_current"},
217 classpath: []string{},
218 },
219 {
Colin Cross72bb3632017-07-13 16:23:21 -0700220
Colin Cross2acdae82017-09-15 19:44:24 -0700221 name: "system_current",
222 properties: `sdk_version: "system_current",`,
223 bootclasspath: []string{"android_system_stubs_current"},
224 classpath: []string{},
225 },
226 {
Colin Cross72bb3632017-07-13 16:23:21 -0700227
Colin Cross2acdae82017-09-15 19:44:24 -0700228 name: "test_current",
229 properties: `sdk_version: "test_current",`,
230 bootclasspath: []string{"android_test_stubs_current"},
231 classpath: []string{},
232 },
233 {
Colin Cross72bb3632017-07-13 16:23:21 -0700234
Colin Cross2acdae82017-09-15 19:44:24 -0700235 name: "nostdlib",
236 properties: `no_standard_libs: true`,
237 bootclasspath: []string{`""`},
238 classpath: []string{},
239 },
240 {
Colin Cross72bb3632017-07-13 16:23:21 -0700241
Colin Cross2acdae82017-09-15 19:44:24 -0700242 name: "host default",
Colin Cross965714f2017-10-03 20:57:01 -0700243 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700244 properties: ``,
Colin Cross965714f2017-10-03 20:57:01 -0700245 host: android.Host,
Colin Cross2acdae82017-09-15 19:44:24 -0700246 classpath: []string{},
247 },
248 {
249 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700250 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700251 host: android.Host,
252 properties: `no_standard_libs: true`,
253 classpath: []string{},
254 },
Colin Cross965714f2017-10-03 20:57:01 -0700255 {
256
257 name: "host supported default",
258 host: android.Host,
259 properties: `host_supported: true,`,
260 classpath: []string{},
261 },
262 {
263 name: "host supported nostdlib",
264 host: android.Host,
265 properties: `host_supported: true, no_standard_libs: true`,
266 classpath: []string{},
267 },
Colin Cross2acdae82017-09-15 19:44:24 -0700268}
Colin Cross72bb3632017-07-13 16:23:21 -0700269
Colin Cross2acdae82017-09-15 19:44:24 -0700270func TestClasspath(t *testing.T) {
271 for _, testcase := range classpathTestcases {
272 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700273 moduleType := "java_library"
274 if testcase.moduleType != "" {
275 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700276 }
Colin Cross965714f2017-10-03 20:57:01 -0700277 ctx := testJava(t, moduleType+` {
Colin Cross2acdae82017-09-15 19:44:24 -0700278 name: "foo",
279 srcs: ["a.java"],
280 `+testcase.properties+`
281 }
282 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700283
Colin Cross2acdae82017-09-15 19:44:24 -0700284 convertModulesToPaths := func(cp []string) []string {
285 ret := make([]string, len(cp))
286 for i, e := range cp {
287 ret[i] = moduleToPath(e)
288 }
289 return ret
290 }
Colin Cross72bb3632017-07-13 16:23:21 -0700291
Colin Cross2acdae82017-09-15 19:44:24 -0700292 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
293 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700294
Colin Cross2acdae82017-09-15 19:44:24 -0700295 variant := "android_common"
296 if testcase.host == android.Host {
297 variant = android.BuildOs.String() + "_common"
298 }
299 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
300
Colin Cross72bb3632017-07-13 16:23:21 -0700301 got := strings.TrimPrefix(javac.Args["bootClasspath"], "-bootclasspath ")
Colin Cross2acdae82017-09-15 19:44:24 -0700302 bc := strings.Join(bootclasspath, ":")
303 if got != bc {
304 t.Errorf("bootclasspath expected %q != got %q", bc, got)
Colin Cross72bb3632017-07-13 16:23:21 -0700305 }
Colin Cross72bb3632017-07-13 16:23:21 -0700306
Colin Cross2acdae82017-09-15 19:44:24 -0700307 got = strings.TrimPrefix(javac.Args["classpath"], "-classpath ")
308 c := strings.Join(classpath, ":")
309 if got != c {
310 t.Errorf("classpath expected %q != got %q", c, got)
311 }
312
313 var deps []string
314 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
315 deps = append(deps, bootclasspath...)
316 }
317 deps = append(deps, classpath...)
318
319 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
320 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
321 }
322 })
Colin Cross72bb3632017-07-13 16:23:21 -0700323 }
324
Colin Cross72bb3632017-07-13 16:23:21 -0700325}
326
327func TestPrebuilts(t *testing.T) {
328 ctx := testJava(t, `
329 java_library {
330 name: "foo",
331 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700332 libs: ["bar"],
333 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700334 }
335
Colin Cross74d73e22017-08-02 11:05:49 -0700336 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700337 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700338 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700339 }
340
Colin Cross74d73e22017-08-02 11:05:49 -0700341 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700342 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700343 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700344 }
345 `)
346
Colin Cross4c428df2017-09-15 17:36:05 -0700347 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
348 combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
Colin Cross72bb3632017-07-13 16:23:21 -0700349
350 bar := "a.jar"
351 if !strings.Contains(javac.Args["classpath"], bar) {
352 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
353 }
354
Colin Cross0a6e0072017-08-30 14:24:55 -0700355 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
356 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700357 }
358}
359
Colin Cross89536d42017-07-07 14:35:50 -0700360func TestDefaults(t *testing.T) {
361 ctx := testJava(t, `
362 java_defaults {
363 name: "defaults",
364 srcs: ["a.java"],
365 libs: ["bar"],
366 static_libs: ["baz"],
367 }
368
369 java_library {
370 name: "foo",
371 defaults: ["defaults"],
372 }
373
374 java_library {
375 name: "bar",
376 srcs: ["b.java"],
377 }
378
379 java_library {
380 name: "baz",
381 srcs: ["c.java"],
382 }
383 `)
384
Colin Cross4c428df2017-09-15 17:36:05 -0700385 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
386 combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
Colin Cross89536d42017-07-07 14:35:50 -0700387
388 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
389 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
390 }
391
Colin Cross8649b262017-09-27 18:03:17 -0700392 bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar")
Colin Cross89536d42017-07-07 14:35:50 -0700393 if !strings.Contains(javac.Args["classpath"], bar) {
394 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
395 }
396
Colin Cross8649b262017-09-27 18:03:17 -0700397 baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700398 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
399 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700400 }
401}
402
Colin Cross0f37af02017-09-27 17:42:05 -0700403func TestResources(t *testing.T) {
404 var table = []struct {
405 name string
406 prop string
407 extra string
408 args string
409 }{
410 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700411 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700412 name: "resource dirs",
413 prop: `java_resource_dirs: ["res"]`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700414 args: "-C res -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700415 },
416 {
417 // Test that a module with java_resources includes the files
418 name: "resource files",
419 prop: `java_resources: ["res/a", "res/b"]`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700420 args: "-C . -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700421 },
422 {
423 // Test that a module with a filegroup in java_resources includes the files with the
424 // path prefix
425 name: "resource filegroup",
426 prop: `java_resources: [":foo-res"]`,
427 extra: `
428 filegroup {
429 name: "foo-res",
430 path: "res",
431 srcs: ["res/a", "res/b"],
432 }`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700433 args: "-C res -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700434 },
435 {
436 // Test that a module with "include_srcs: true" includes its source files in the resources jar
437 name: "include sources",
438 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700439 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700440 },
441 }
442
443 for _, test := range table {
444 t.Run(test.name, func(t *testing.T) {
445 ctx := testJava(t, `
446 java_library {
447 name: "foo",
448 srcs: [
449 "a.java",
450 "b.java",
451 "c.java",
452 ],
453 `+test.prop+`,
454 }
455 `+test.extra)
456
457 foo := ctx.ModuleForTests("foo", "android_common").Output("classes.jar")
458 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar")
459
460 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
461 t.Errorf("foo combined jars %v does not contain %q",
462 foo.Inputs.Strings(), fooRes.Output.String())
463 }
464
Colin Crossaf9c55b2017-10-03 14:50:08 -0700465 if fooRes.Args["jarArgs"] != test.args {
466 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700467 fooRes.Args["jarArgs"], test.args)
468 }
469 })
470 }
471}
472
Colin Cross0532fb02017-10-02 16:57:40 -0700473func TestExcludeResources(t *testing.T) {
474 ctx := testJava(t, `
475 java_library {
476 name: "foo",
477 srcs: ["a.java"],
478 java_resource_dirs: ["res", "res2"],
479 exclude_java_resource_dirs: ["res2"],
480 }
481
482 java_library {
483 name: "bar",
484 srcs: ["a.java"],
485 java_resources: ["res/*"],
486 exclude_java_resources: ["res/b"],
487 }
488 `)
489
490 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar")
491
Colin Crossaf9c55b2017-10-03 14:50:08 -0700492 expected := "-C res -f res/a -f res/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700493 if fooRes.Args["jarArgs"] != expected {
494 t.Errorf("foo resource jar args %q is not %q",
495 fooRes.Args["jarArgs"], expected)
496
497 }
498
499 barRes := ctx.ModuleForTests("bar", "android_common").Output("res.jar")
500
501 expected = "-C . -f res/a"
502 if barRes.Args["jarArgs"] != expected {
503 t.Errorf("bar resource jar args %q is not %q",
504 barRes.Args["jarArgs"], expected)
505
506 }
507}
508
Colin Cross72bb3632017-07-13 16:23:21 -0700509func fail(t *testing.T, errs []error) {
510 if len(errs) > 0 {
511 for _, err := range errs {
512 t.Error(err)
513 }
514 t.FailNow()
515 }
516}