blob: 3b73fddd522aff83f9d23ad5a850241913e0976d [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 Crossf506d872017-07-19 15:53:04 -070059 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
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 }
142 `)
143
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 Cross2acdae82017-09-15 19:44:24 -0700167var classpathTestcases = []struct {
168 name string
169 host android.OsClass
170 properties string
171 bootclasspath []string
172 classpath []string
173}{
174 {
175 name: "default",
176 bootclasspath: []string{"core-oj", "core-libart"},
177 classpath: []string{"ext", "framework", "okhttp"},
178 },
179 {
180 name: "blank sdk version",
181 properties: `sdk_version: "",`,
182 bootclasspath: []string{"core-oj", "core-libart"},
183 classpath: []string{"ext", "framework", "okhttp"},
184 },
185 {
Colin Cross4c428df2017-09-15 17:36:05 -0700186
Colin Cross2acdae82017-09-15 19:44:24 -0700187 name: "sdk v14",
188 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700189 bootclasspath: []string{`""`},
190 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700191 },
192 {
Colin Cross72bb3632017-07-13 16:23:21 -0700193
Colin Cross2acdae82017-09-15 19:44:24 -0700194 name: "current",
195 properties: `sdk_version: "current",`,
196 bootclasspath: []string{"android_stubs_current"},
197 classpath: []string{},
198 },
199 {
Colin Cross72bb3632017-07-13 16:23:21 -0700200
Colin Cross2acdae82017-09-15 19:44:24 -0700201 name: "system_current",
202 properties: `sdk_version: "system_current",`,
203 bootclasspath: []string{"android_system_stubs_current"},
204 classpath: []string{},
205 },
206 {
Colin Cross72bb3632017-07-13 16:23:21 -0700207
Colin Cross2acdae82017-09-15 19:44:24 -0700208 name: "test_current",
209 properties: `sdk_version: "test_current",`,
210 bootclasspath: []string{"android_test_stubs_current"},
211 classpath: []string{},
212 },
213 {
Colin Cross72bb3632017-07-13 16:23:21 -0700214
Colin Cross2acdae82017-09-15 19:44:24 -0700215 name: "nostdlib",
216 properties: `no_standard_libs: true`,
217 bootclasspath: []string{`""`},
218 classpath: []string{},
219 },
220 {
Colin Cross72bb3632017-07-13 16:23:21 -0700221
Colin Cross2acdae82017-09-15 19:44:24 -0700222 name: "host default",
223 host: android.Host,
224 properties: ``,
225 classpath: []string{},
226 },
227 {
228 name: "host nostdlib",
229 host: android.Host,
230 properties: `no_standard_libs: true`,
231 classpath: []string{},
232 },
233}
Colin Cross72bb3632017-07-13 16:23:21 -0700234
Colin Cross2acdae82017-09-15 19:44:24 -0700235func TestClasspath(t *testing.T) {
236 for _, testcase := range classpathTestcases {
237 t.Run(testcase.name, func(t *testing.T) {
238 hostExtra := ""
239 if testcase.host == android.Host {
240 hostExtra = "_host"
241 }
242 ctx := testJava(t, `
243 java_library`+hostExtra+` {
244 name: "foo",
245 srcs: ["a.java"],
246 `+testcase.properties+`
247 }
248 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700249
Colin Cross2acdae82017-09-15 19:44:24 -0700250 convertModulesToPaths := func(cp []string) []string {
251 ret := make([]string, len(cp))
252 for i, e := range cp {
253 ret[i] = moduleToPath(e)
254 }
255 return ret
256 }
Colin Cross72bb3632017-07-13 16:23:21 -0700257
Colin Cross2acdae82017-09-15 19:44:24 -0700258 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
259 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700260
Colin Cross2acdae82017-09-15 19:44:24 -0700261 variant := "android_common"
262 if testcase.host == android.Host {
263 variant = android.BuildOs.String() + "_common"
264 }
265 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
266
Colin Cross72bb3632017-07-13 16:23:21 -0700267 got := strings.TrimPrefix(javac.Args["bootClasspath"], "-bootclasspath ")
Colin Cross2acdae82017-09-15 19:44:24 -0700268 bc := strings.Join(bootclasspath, ":")
269 if got != bc {
270 t.Errorf("bootclasspath expected %q != got %q", bc, got)
Colin Cross72bb3632017-07-13 16:23:21 -0700271 }
Colin Cross72bb3632017-07-13 16:23:21 -0700272
Colin Cross2acdae82017-09-15 19:44:24 -0700273 got = strings.TrimPrefix(javac.Args["classpath"], "-classpath ")
274 c := strings.Join(classpath, ":")
275 if got != c {
276 t.Errorf("classpath expected %q != got %q", c, got)
277 }
278
279 var deps []string
280 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
281 deps = append(deps, bootclasspath...)
282 }
283 deps = append(deps, classpath...)
284
285 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
286 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
287 }
288 })
Colin Cross72bb3632017-07-13 16:23:21 -0700289 }
290
Colin Cross72bb3632017-07-13 16:23:21 -0700291}
292
293func TestPrebuilts(t *testing.T) {
294 ctx := testJava(t, `
295 java_library {
296 name: "foo",
297 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700298 libs: ["bar"],
299 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700300 }
301
Colin Cross74d73e22017-08-02 11:05:49 -0700302 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700303 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700304 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700305 }
306
Colin Cross74d73e22017-08-02 11:05:49 -0700307 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700308 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700309 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700310 }
311 `)
312
Colin Cross4c428df2017-09-15 17:36:05 -0700313 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
314 combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
Colin Cross72bb3632017-07-13 16:23:21 -0700315
316 bar := "a.jar"
317 if !strings.Contains(javac.Args["classpath"], bar) {
318 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
319 }
320
Colin Cross0a6e0072017-08-30 14:24:55 -0700321 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
322 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700323 }
324}
325
Colin Cross89536d42017-07-07 14:35:50 -0700326func TestDefaults(t *testing.T) {
327 ctx := testJava(t, `
328 java_defaults {
329 name: "defaults",
330 srcs: ["a.java"],
331 libs: ["bar"],
332 static_libs: ["baz"],
333 }
334
335 java_library {
336 name: "foo",
337 defaults: ["defaults"],
338 }
339
340 java_library {
341 name: "bar",
342 srcs: ["b.java"],
343 }
344
345 java_library {
346 name: "baz",
347 srcs: ["c.java"],
348 }
349 `)
350
Colin Cross4c428df2017-09-15 17:36:05 -0700351 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
352 combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
Colin Cross89536d42017-07-07 14:35:50 -0700353
354 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
355 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
356 }
357
Colin Cross8649b262017-09-27 18:03:17 -0700358 bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar")
Colin Cross89536d42017-07-07 14:35:50 -0700359 if !strings.Contains(javac.Args["classpath"], bar) {
360 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
361 }
362
Colin Cross8649b262017-09-27 18:03:17 -0700363 baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700364 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
365 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700366 }
367}
368
Colin Cross0f37af02017-09-27 17:42:05 -0700369func TestResources(t *testing.T) {
370 var table = []struct {
371 name string
372 prop string
373 extra string
374 args string
375 }{
376 {
377 // Test that a module with java_resource_dirs includes a file list file
378 name: "resource dirs",
379 prop: `java_resource_dirs: ["res"]`,
380 args: "-C res -l ",
381 },
382 {
383 // Test that a module with java_resources includes the files
384 name: "resource files",
385 prop: `java_resources: ["res/a", "res/b"]`,
386 args: "-C . -f res/a -C . -f res/b",
387 },
388 {
389 // Test that a module with a filegroup in java_resources includes the files with the
390 // path prefix
391 name: "resource filegroup",
392 prop: `java_resources: [":foo-res"]`,
393 extra: `
394 filegroup {
395 name: "foo-res",
396 path: "res",
397 srcs: ["res/a", "res/b"],
398 }`,
399 args: "-C res -f res/a -C res -f res/b",
400 },
401 {
402 // Test that a module with "include_srcs: true" includes its source files in the resources jar
403 name: "include sources",
404 prop: `include_srcs: true`,
405 args: "-C . -f a.java -C . -f b.java -C . -f c.java",
406 },
407 }
408
409 for _, test := range table {
410 t.Run(test.name, func(t *testing.T) {
411 ctx := testJava(t, `
412 java_library {
413 name: "foo",
414 srcs: [
415 "a.java",
416 "b.java",
417 "c.java",
418 ],
419 `+test.prop+`,
420 }
421 `+test.extra)
422
423 foo := ctx.ModuleForTests("foo", "android_common").Output("classes.jar")
424 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar")
425
426 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
427 t.Errorf("foo combined jars %v does not contain %q",
428 foo.Inputs.Strings(), fooRes.Output.String())
429 }
430
431 if !strings.Contains(fooRes.Args["jarArgs"], test.args) {
432 t.Errorf("foo resource jar args %q does not contain %q",
433 fooRes.Args["jarArgs"], test.args)
434 }
435 })
436 }
437}
438
Colin Cross0532fb02017-10-02 16:57:40 -0700439func TestExcludeResources(t *testing.T) {
440 ctx := testJava(t, `
441 java_library {
442 name: "foo",
443 srcs: ["a.java"],
444 java_resource_dirs: ["res", "res2"],
445 exclude_java_resource_dirs: ["res2"],
446 }
447
448 java_library {
449 name: "bar",
450 srcs: ["a.java"],
451 java_resources: ["res/*"],
452 exclude_java_resources: ["res/b"],
453 }
454 `)
455
456 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar")
457
458 expected := "-C res -l " + fooRes.Implicits[0].String()
459 if fooRes.Args["jarArgs"] != expected {
460 t.Errorf("foo resource jar args %q is not %q",
461 fooRes.Args["jarArgs"], expected)
462
463 }
464
465 barRes := ctx.ModuleForTests("bar", "android_common").Output("res.jar")
466
467 expected = "-C . -f res/a"
468 if barRes.Args["jarArgs"] != expected {
469 t.Errorf("bar resource jar args %q is not %q",
470 barRes.Args["jarArgs"], expected)
471
472 }
473}
474
Colin Cross72bb3632017-07-13 16:23:21 -0700475func fail(t *testing.T, errs []error) {
476 if len(errs) > 0 {
477 for _, err := range errs {
478 t.Error(err)
479 }
480 t.FailNow()
481 }
482}