blob: adc5ad18122d771be5d3969e7f24750b8ca822da [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 (
Colin Cross72bb3632017-07-13 16:23:21 -070018 "io/ioutil"
19 "os"
20 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070021 "reflect"
Nan Zhang61eaedb2017-11-02 13:28:15 -070022 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070023 "strings"
24 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070025
Jeongik Cha28df2572019-11-11 10:46:36 +090026 "github.com/google/blueprint/proptools"
27
Colin Crossa4f08812018-10-02 22:03:40 -070028 "android/soong/android"
29 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080030 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070031 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070032)
33
34var buildDir string
35
36func setUp() {
37 var err error
38 buildDir, err = ioutil.TempDir("", "soong_java_test")
39 if err != nil {
40 panic(err)
41 }
42}
43
44func tearDown() {
45 os.RemoveAll(buildDir)
46}
47
48func TestMain(m *testing.M) {
49 run := func() int {
50 setUp()
51 defer tearDown()
52
53 return m.Run()
54 }
55
56 os.Exit(run())
57}
Colin Cross527012a2017-11-30 22:56:16 -080058
Colin Cross98be1bb2019-12-13 20:41:13 -080059func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
60 return TestConfig(buildDir, env, bp, fs)
Colin Cross1369cdb2017-09-29 17:58:17 -070061}
62
Colin Cross98be1bb2019-12-13 20:41:13 -080063func testContext() *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070064
Colin Cross4c428df2017-09-15 17:36:05 -070065 ctx := android.NewTestArchContext()
Paul Duffinf9b1da02019-12-18 19:51:55 +000066 RegisterJavaBuildComponents(ctx)
67 RegisterAppBuildComponents(ctx)
68 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000069 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000070 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080071 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080072 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
73 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000074 RegisterDocsBuildComponents(ctx)
75 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000076 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080077 ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory)
Colin Cross5ea9bcc2017-07-27 15:41:32 -070078 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
79 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070080 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090081 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Inseob Kimc0907f12019-02-08 21:00:45 +090082 ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +090083 })
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070084 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080085 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070086 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070087
88 // Register module types and mutators from cc needed for JNI testing
Colin Cross4b49b762019-11-22 15:25:03 -080089 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
90 ctx.RegisterModuleType("cc_object", cc.ObjectFactory)
91 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
92 ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory)
93 ctx.RegisterModuleType("ndk_prebuilt_shared_stl", cc.NdkPrebuiltSharedStlFactory)
Colin Crossa4f08812018-10-02 22:03:40 -070094 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
95 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
96 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
97 })
98
Colin Cross527012a2017-11-30 22:56:16 -080099 return ctx
100}
101
102func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800103 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800104
Colin Cross98be1bb2019-12-13 20:41:13 -0800105 pathCtx := android.PathContextForTesting(config)
Colin Crossc28bb0b2019-02-25 14:20:47 -0800106 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
107
Colin Cross98be1bb2019-12-13 20:41:13 -0800108 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100109 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800110 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700111 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800112 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800113}
114
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900115func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900116 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800117 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900118}
119
Colin Cross98be1bb2019-12-13 20:41:13 -0800120func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900121 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800122 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900123
Colin Cross98be1bb2019-12-13 20:41:13 -0800124 pathCtx := android.PathContextForTesting(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900125 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
126
Colin Cross98be1bb2019-12-13 20:41:13 -0800127 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900128 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
129 if len(errs) > 0 {
130 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900131 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900132 }
133 _, errs = ctx.PrepareBuildActions(config)
134 if len(errs) > 0 {
135 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900136 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900137 }
138
139 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900140
141 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900142}
143
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700144func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800145 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800146 return testJavaWithConfig(t, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900147}
148
Colin Cross98be1bb2019-12-13 20:41:13 -0800149func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900150 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800151 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800152 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700153
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700154 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700155}
156
Colin Cross2acdae82017-09-15 19:44:24 -0700157func moduleToPath(name string) string {
158 switch {
159 case name == `""`:
160 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700161 case strings.HasSuffix(name, ".jar"):
162 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700163 default:
164 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700165 }
166}
167
Jeongik Chae403e9e2019-12-07 00:16:24 +0900168func TestJavaLinkType(t *testing.T) {
169 testJava(t, `
170 java_library {
171 name: "foo",
172 srcs: ["a.java"],
173 libs: ["bar"],
174 static_libs: ["baz"],
175 }
176
177 java_library {
178 name: "bar",
179 sdk_version: "current",
180 srcs: ["b.java"],
181 }
182
183 java_library {
184 name: "baz",
185 sdk_version: "system_current",
186 srcs: ["c.java"],
187 }
188 `)
189
190 testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", `
191 java_library {
192 name: "foo",
193 srcs: ["a.java"],
194 libs: ["bar"],
195 sdk_version: "current",
196 static_libs: ["baz"],
197 }
198
199 java_library {
200 name: "bar",
201 sdk_version: "current",
202 srcs: ["b.java"],
203 }
204
205 java_library {
206 name: "baz",
207 sdk_version: "system_current",
208 srcs: ["c.java"],
209 }
210 `)
211
212 testJava(t, `
213 java_library {
214 name: "foo",
215 srcs: ["a.java"],
216 libs: ["bar"],
217 sdk_version: "system_current",
218 static_libs: ["baz"],
219 }
220
221 java_library {
222 name: "bar",
223 sdk_version: "current",
224 srcs: ["b.java"],
225 }
226
227 java_library {
228 name: "baz",
229 sdk_version: "system_current",
230 srcs: ["c.java"],
231 }
232 `)
233
234 testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", `
235 java_library {
236 name: "foo",
237 srcs: ["a.java"],
238 libs: ["bar"],
239 sdk_version: "system_current",
240 static_libs: ["baz"],
241 }
242
243 java_library {
244 name: "bar",
245 sdk_version: "current",
246 srcs: ["b.java"],
247 }
248
249 java_library {
250 name: "baz",
251 srcs: ["c.java"],
252 }
253 `)
254}
255
Colin Cross72bb3632017-07-13 16:23:21 -0700256func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700257 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700258 java_library {
259 name: "foo",
260 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700261 libs: ["bar"],
262 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700263 }
264
265 java_library {
266 name: "bar",
267 srcs: ["b.java"],
268 }
269
270 java_library {
271 name: "baz",
272 srcs: ["c.java"],
273 }
Colin Crossd5934c82017-10-02 13:55:26 -0700274 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700275
Colin Cross4c428df2017-09-15 17:36:05 -0700276 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700277 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700278
279 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
280 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
281 }
282
Colin Cross1ee23172017-10-18 14:44:18 -0700283 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700284 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
285 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700286
Nan Zhanged19fc32017-10-19 13:06:22 -0700287 if !strings.Contains(javac.Args["classpath"], barTurbine) {
288 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700289 }
290
Nan Zhanged19fc32017-10-19 13:06:22 -0700291 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
292 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700293 }
294
295 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
296 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700297 }
298}
299
Artur Satayev9cf46692019-11-26 18:08:34 +0000300func TestExportedPlugins(t *testing.T) {
301 type Result struct {
302 library string
303 processors string
304 }
305 var tests = []struct {
306 name string
307 extra string
308 results []Result
309 }{
310 {
311 name: "Exported plugin is not a direct plugin",
312 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
313 results: []Result{{library: "exports", processors: "-proc:none"}},
314 },
315 {
316 name: "Exports plugin to dependee",
317 extra: `
318 java_library{name: "exports", exported_plugins: ["plugin"]}
319 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
320 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
321 `,
322 results: []Result{
323 {library: "foo", processors: "-processor com.android.TestPlugin"},
324 {library: "bar", processors: "-processor com.android.TestPlugin"},
325 },
326 },
327 {
328 name: "Exports plugin to android_library",
329 extra: `
330 java_library{name: "exports", exported_plugins: ["plugin"]}
331 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
332 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
333 `,
334 results: []Result{
335 {library: "foo", processors: "-processor com.android.TestPlugin"},
336 {library: "bar", processors: "-processor com.android.TestPlugin"},
337 },
338 },
339 {
340 name: "Exports plugin is not propagated via transitive deps",
341 extra: `
342 java_library{name: "exports", exported_plugins: ["plugin"]}
343 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
344 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
345 `,
346 results: []Result{
347 {library: "foo", processors: "-processor com.android.TestPlugin"},
348 {library: "bar", processors: "-proc:none"},
349 },
350 },
351 {
352 name: "Exports plugin appends to plugins",
353 extra: `
354 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
355 java_library{name: "exports", exported_plugins: ["plugin"]}
356 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
357 `,
358 results: []Result{
359 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
360 },
361 },
362 }
363
364 for _, test := range tests {
365 t.Run(test.name, func(t *testing.T) {
366 ctx, _ := testJava(t, `
367 java_plugin {
368 name: "plugin",
369 processor_class: "com.android.TestPlugin",
370 }
371 `+test.extra)
372
373 for _, want := range test.results {
374 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
375 if javac.Args["processor"] != want.processors {
376 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
377 }
378 }
379 })
380 }
381}
382
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900383func TestSdkVersionByPartition(t *testing.T) {
384 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900385 java_library {
386 name: "foo",
387 srcs: ["a.java"],
388 vendor: true,
389 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900390 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900391
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900392 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900393 java_library {
394 name: "bar",
395 srcs: ["b.java"],
396 }
397 `)
398
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900399 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900400 bp := `
401 java_library {
402 name: "foo",
403 srcs: ["a.java"],
404 product_specific: true,
405 }
406 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800407
408 config := testConfig(nil, bp, nil)
409 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900410 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800411 testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900412 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800413 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900414 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900415 }
416}
417
Colin Crossd5934c82017-10-02 13:55:26 -0700418func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700419 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700420 java_library {
421 name: "foo",
422 srcs: ["a.java"],
423 target: {
424 android: {
425 srcs: ["b.java"],
426 },
427 },
428 }
429 `)
430
431 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
432 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
433 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
434 }
435}
436
Colin Cross6b4a32d2017-12-05 13:42:45 -0800437func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700438 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800439 java_library_host {
440 name: "foo",
441 srcs: ["a.java"],
442 }
443
444 java_binary_host {
445 name: "bar",
446 srcs: ["b.java"],
447 static_libs: ["foo"],
448 }
449 `)
450
451 buildOS := android.BuildOs.String()
452
453 bar := ctx.ModuleForTests("bar", buildOS+"_common")
454 barJar := bar.Output("bar.jar").Output.String()
455 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
456 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
457
458 // Test that the install binary wrapper depends on the installed jar file
459 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
460 t.Errorf("expected binary wrapper implicits [%q], got %v",
461 barJar, barWrapperDeps)
462 }
463
464}
465
Colin Cross72bb3632017-07-13 16:23:21 -0700466func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700467 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700468 java_library {
469 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000470 srcs: ["a.java", ":stubs-source"],
Colin Cross79c7c262019-04-17 11:11:46 -0700471 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700472 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700473 }
474
Colin Cross74d73e22017-08-02 11:05:49 -0700475 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700476 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700477 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700478 }
479
Colin Cross74d73e22017-08-02 11:05:49 -0700480 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700481 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700482 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700483 }
Colin Cross42be7612019-02-21 18:12:14 -0800484
485 dex_import {
486 name: "qux",
487 jars: ["b.jar"],
488 }
Colin Cross79c7c262019-04-17 11:11:46 -0700489
490 java_sdk_library_import {
491 name: "sdklib",
492 jars: ["b.jar"],
493 }
Paul Duffin91547182019-11-12 19:39:36 +0000494
495 prebuilt_stubs_sources {
496 name: "stubs-source",
497 srcs: ["stubs/sources/**/*.java"],
498 }
Colin Cross72bb3632017-07-13 16:23:21 -0700499 `)
500
Colin Cross4c428df2017-09-15 17:36:05 -0700501 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700502 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700503 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
504 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700505 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700506
Paul Duffin91547182019-11-12 19:39:36 +0000507 inputs := []string{}
508 for _, p := range javac.BuildParams.Inputs {
509 inputs = append(inputs, p.String())
510 }
511
512 expected := []string{
513 "a.java",
514 "stubs/sources/foo/Foo.java",
515 }
516 if !reflect.DeepEqual(expected, inputs) {
517 t.Errorf("foo inputs incorrect: expected %q, found %q", expected, inputs)
518 }
519
Colin Cross37f6d792018-07-12 12:28:41 -0700520 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
521 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700522 }
523
Colin Cross79c7c262019-04-17 11:11:46 -0700524 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
525 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
526 }
527
Colin Cross37f6d792018-07-12 12:28:41 -0700528 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
529 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700530 }
Colin Cross42be7612019-02-21 18:12:14 -0800531
532 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700533}
534
Colin Cross89536d42017-07-07 14:35:50 -0700535func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700536 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700537 java_defaults {
538 name: "defaults",
539 srcs: ["a.java"],
540 libs: ["bar"],
541 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700542 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700543 }
544
545 java_library {
546 name: "foo",
547 defaults: ["defaults"],
548 }
549
550 java_library {
551 name: "bar",
552 srcs: ["b.java"],
553 }
554
555 java_library {
556 name: "baz",
557 srcs: ["c.java"],
558 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700559
560 android_test {
561 name: "atestOptimize",
562 defaults: ["defaults"],
563 optimize: {enabled: true},
564 }
565
566 android_test {
567 name: "atestNoOptimize",
568 defaults: ["defaults"],
569 }
570
571 android_test {
572 name: "atestDefault",
573 srcs: ["a.java"],
574 }
Colin Cross89536d42017-07-07 14:35:50 -0700575 `)
576
Colin Cross4c428df2017-09-15 17:36:05 -0700577 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700578 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700579
580 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
581 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
582 }
583
Nan Zhanged19fc32017-10-19 13:06:22 -0700584 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
585 if !strings.Contains(javac.Args["classpath"], barTurbine) {
586 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700587 }
588
Colin Cross1ee23172017-10-18 14:44:18 -0700589 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700590 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
591 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700592 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700593
594 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
595 if atestOptimize.Output == nil {
596 t.Errorf("atestOptimize should optimize APK")
597 }
598
599 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
600 if atestNoOptimize.Output == nil {
601 t.Errorf("atestNoOptimize should not optimize APK")
602 }
603
604 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
605 if atestDefault.Output == nil {
606 t.Errorf("atestDefault should optimize APK")
607 }
Colin Cross89536d42017-07-07 14:35:50 -0700608}
609
Colin Cross0f37af02017-09-27 17:42:05 -0700610func TestResources(t *testing.T) {
611 var table = []struct {
612 name string
613 prop string
614 extra string
615 args string
616 }{
617 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700618 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700619 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800620 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700621 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700622 },
623 {
624 // Test that a module with java_resources includes the files
625 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700626 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
627 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700628 },
629 {
630 // Test that a module with a filegroup in java_resources includes the files with the
631 // path prefix
632 name: "resource filegroup",
633 prop: `java_resources: [":foo-res"]`,
634 extra: `
635 filegroup {
636 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800637 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700638 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700639 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700640 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700641 },
642 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700643 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
644 name: "wildcard dirs",
645 prop: `java_resource_dirs: ["java-res/*"]`,
646 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
647 },
648 {
649 // Test that a module exclude_java_resource_dirs excludes the files
650 name: "wildcard dirs",
651 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
652 args: "-C java-res/a -f java-res/a/a",
653 },
Colin Crosscedd4762018-09-13 11:26:19 -0700654 {
655 // Test wildcards in java_resources
656 name: "wildcard files",
657 prop: `java_resources: ["java-res/**/*"]`,
658 args: "-C . -f java-res/a/a -f java-res/b/b",
659 },
660 {
661 // Test exclude_java_resources with java_resources
662 name: "wildcard files with exclude",
663 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
664 args: "-C . -f java-res/a/a",
665 },
666 {
667 // Test exclude_java_resources with java_resource_dirs
668 name: "resource dirs with exclude files",
669 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
670 args: "-C java-res -f java-res/a/a",
671 },
672 {
673 // Test exclude_java_resource_dirs with java_resource_dirs
674 name: "resource dirs with exclude files",
675 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
676 args: "-C java-res -f java-res/a/a -f java-res/b/b",
677 },
Colin Cross0f37af02017-09-27 17:42:05 -0700678 }
679
680 for _, test := range table {
681 t.Run(test.name, func(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700682 ctx, _ := testJava(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700683 java_library {
684 name: "foo",
685 srcs: [
686 "a.java",
687 "b.java",
688 "c.java",
689 ],
690 `+test.prop+`,
691 }
692 `+test.extra)
693
Colin Cross331a1212018-08-15 20:40:52 -0700694 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700695 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700696
697 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
698 t.Errorf("foo combined jars %v does not contain %q",
699 foo.Inputs.Strings(), fooRes.Output.String())
700 }
701
Colin Crossaf9c55b2017-10-03 14:50:08 -0700702 if fooRes.Args["jarArgs"] != test.args {
703 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700704 fooRes.Args["jarArgs"], test.args)
705 }
706 })
707 }
708}
709
Colin Cross0c4ce212019-05-03 15:28:19 -0700710func TestIncludeSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700711 ctx, _ := testJava(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700712 java_library {
713 name: "foo",
714 srcs: [
715 "a.java",
716 "b.java",
717 "c.java",
718 ],
719 include_srcs: true,
720 }
721
722 java_library {
723 name: "bar",
724 srcs: [
725 "a.java",
726 "b.java",
727 "c.java",
728 ],
729 java_resource_dirs: ["java-res"],
730 include_srcs: true,
731 }
732 `)
733
734 // Test a library with include_srcs: true
735 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
736 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
737
738 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
739 t.Errorf("foo combined jars %v does not contain %q", w, g)
740 }
741
742 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
743 t.Errorf("foo source jar args %q is not %q", w, g)
744 }
745
746 // Test a library with include_srcs: true and resources
747 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
748 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
749 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
750 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
751
752 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
753 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
754 }
755
756 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
757 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
758 }
759
760 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
761 t.Errorf("bar combined jars %v does not contain %q", w, g)
762 }
763
764 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
765 t.Errorf("bar source jar args %q is not %q", w, g)
766 }
767
768 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
769 t.Errorf("bar resource jar args %q is not %q", w, g)
770 }
771}
772
Colin Cross54190b32017-10-09 15:34:10 -0700773func TestGeneratedSources(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700774 ctx, _ := testJava(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700775 java_library {
776 name: "foo",
777 srcs: [
778 "a*.java",
779 ":gen",
780 "b*.java",
781 ],
782 }
783
784 genrule {
785 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800786 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700787 out: ["gen.java"],
788 }
789 `)
790
791 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
792 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
793
Colin Cross15e86d92017-10-20 15:07:08 -0700794 if filepath.Base(genrule.Output.String()) != "gen.java" {
795 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700796 }
797
798 if len(javac.Inputs) != 3 ||
799 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700800 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700801 javac.Inputs[2].String() != "b.java" {
802 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
803 }
804}
805
Nan Zhang61eaedb2017-11-02 13:28:15 -0700806func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700807 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700808 java_library {
809 name: "foo",
810 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900811 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700812 }
813
814 java_library {
815 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800816 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700817 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900818 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700819 }
820
821 java_library {
822 name: "baz",
823 srcs: ["c.java"],
824 libs: ["bar"],
825 sdk_version: "14",
826 }
827 `)
828
829 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
830 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
831 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
832 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
833 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
834
835 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
836 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
837 }
838
839 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
840 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
841 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
842 }
843 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
844 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
845 }
846 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
847 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
848 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100849 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700850 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100851 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700852 }
853}
854
855func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700856 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700857 java_library {
858 name: "bar",
859 srcs: ["a.java","b.java","c.java"],
860 javac_shard_size: 1
861 }
862 `)
863
864 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
865 for i := 0; i < 3; i++ {
866 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
867 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
868 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
869 }
870 }
871}
872
Nan Zhang581fd212018-01-10 16:06:12 -0800873func TestDroiddoc(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700874 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +0000875 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -0800876 name: "droiddoc-templates-sdk",
877 path: ".",
878 }
Jiyong Park29074592019-07-07 16:27:47 +0900879 filegroup {
880 name: "bar-doc-aidl-srcs",
881 srcs: ["bar-doc/IBar.aidl"],
882 path: "bar-doc",
883 }
Nan Zhang581fd212018-01-10 16:06:12 -0800884 droiddoc {
885 name: "bar-doc",
886 srcs: [
887 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900888 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +0900889 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -0800890 ],
891 exclude_srcs: [
892 "bar-doc/b.java"
893 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800894 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800895 hdf: [
896 "android.whichdoc offline",
897 ],
898 knowntags: [
899 "bar-doc/known_oj_tags.txt",
900 ],
901 proofread_file: "libcore-proofread.txt",
902 todo_file: "libcore-docs-todo.html",
903 args: "-offlinemode -title \"libcore\"",
904 }
905 `)
906
Colin Crossc0806172019-06-14 18:51:47 -0700907 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +0900908 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -0700909 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +0900910 javaSrcs = append(javaSrcs, i.Base())
911 }
Colin Crossc0806172019-06-14 18:51:47 -0700912 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
913 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +0900914 }
915
Colin Crossc0806172019-06-14 18:51:47 -0700916 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
917 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
918 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
919 }
920
921 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
922 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +0900923 }
Nan Zhang581fd212018-01-10 16:06:12 -0800924}
925
Colin Cross54250902017-12-05 09:28:08 -0800926func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700927 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -0800928 java_library {
929 name: "foo",
930 srcs: ["a.java"],
931 }
932
933 java_genrule {
934 name: "jargen",
935 tool_files: ["b.java"],
936 cmd: "$(location b.java) $(in) $(out)",
937 out: ["jargen.jar"],
938 srcs: [":foo"],
939 }
940
941 java_library {
942 name: "bar",
943 static_libs: ["jargen"],
944 srcs: ["c.java"],
945 }
946
947 java_library {
948 name: "baz",
949 libs: ["jargen"],
950 srcs: ["c.java"],
951 }
952 `)
953
954 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
955 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
956 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
957 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
958 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
959
960 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
961 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
962 }
963
964 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
965 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
966 }
967
968 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
969 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
970 }
971
972 if len(barCombined.Inputs) != 2 ||
973 barCombined.Inputs[0].String() != bar.Output.String() ||
974 barCombined.Inputs[1].String() != jargen.Output.String() {
975 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
976 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
977 }
978}
979
Nan Zhang27e284d2018-02-09 21:03:53 +0000980func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700981 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +0000982 java_library {
983 name: "foo",
984 srcs: ["a.java", ":foo-srcs"],
985 exclude_srcs: ["a.java", ":foo-excludes"],
986 }
987
988 filegroup {
989 name: "foo-srcs",
990 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
991 }
992
993 filegroup {
994 name: "foo-excludes",
995 srcs: ["java-fg/a.java", "java-fg/b.java"],
996 }
997 `)
998
999 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1000
1001 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1002 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1003 }
1004}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001005
Paul Duffin52d398a2019-06-11 12:31:14 +01001006func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001007 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001008 "libcore/Android.bp": []byte(`
1009 java_library {
1010 name: "core",
1011 sdk_version: "none",
1012 system_modules: "none",
1013 }`),
1014 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001015 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001016 run(t, ctx, config)
1017}
1018
Jiyong Parkc678ad32018-04-10 13:07:10 +09001019func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001020 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001021 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001022 name: "droiddoc-templates-sdk",
1023 path: ".",
1024 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001025 java_sdk_library {
1026 name: "foo",
1027 srcs: ["a.java", "b.java"],
1028 api_packages: ["foo"],
1029 }
1030 java_sdk_library {
1031 name: "bar",
1032 srcs: ["a.java", "b.java"],
1033 api_packages: ["bar"],
1034 }
1035 java_library {
1036 name: "baz",
1037 srcs: ["c.java"],
1038 libs: ["foo", "bar"],
1039 sdk_version: "system_current",
1040 }
Jiyong Park1be96912018-05-28 18:02:19 +09001041 java_library {
1042 name: "qux",
1043 srcs: ["c.java"],
1044 libs: ["baz"],
1045 sdk_version: "system_current",
1046 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001047 `)
1048
1049 // check the existence of the internal modules
1050 ctx.ModuleForTests("foo", "android_common")
1051 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
1052 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001053 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001054 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
1055 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001056 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Colin Cross7113d202019-11-20 16:39:12 -08001057 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +09001058 ctx.ModuleForTests("foo.api.public.28", "")
1059 ctx.ModuleForTests("foo.api.system.28", "")
1060 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001061
1062 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1063 // tests if baz is actually linked to the stubs lib
1064 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1065 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1066 "foo.stubs.system.jar")
1067 }
1068 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001069 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001070 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001071 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001072 }
1073 // test if baz is not linked to the system variant of foo
1074 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1075 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1076 "foo.stubs.jar")
1077 }
Jiyong Park1be96912018-05-28 18:02:19 +09001078
1079 // test if baz has exported SDK lib names foo and bar to qux
1080 qux := ctx.ModuleForTests("qux", "android_common")
1081 if quxLib, ok := qux.Module().(*Library); ok {
1082 sdkLibs := quxLib.ExportedSdkLibs()
1083 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
1084 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
1085 }
1086 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001087}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001088
1089var compilerFlagsTestCases = []struct {
1090 in string
1091 out bool
1092}{
1093 {
1094 in: "a",
1095 out: false,
1096 },
1097 {
1098 in: "-a",
1099 out: true,
1100 },
1101 {
1102 in: "-no-jdk",
1103 out: false,
1104 },
1105 {
1106 in: "-no-stdlib",
1107 out: false,
1108 },
1109 {
1110 in: "-kotlin-home",
1111 out: false,
1112 },
1113 {
1114 in: "-kotlin-home /some/path",
1115 out: false,
1116 },
1117 {
1118 in: "-include-runtime",
1119 out: false,
1120 },
1121 {
1122 in: "-Xintellij-plugin-root",
1123 out: false,
1124 },
1125}
1126
1127type mockContext struct {
1128 android.ModuleContext
1129 result bool
1130}
1131
1132func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1133 // CheckBadCompilerFlags calls this function when the flag should be rejected
1134 ctx.result = false
1135}
1136
1137func TestCompilerFlags(t *testing.T) {
1138 for _, testCase := range compilerFlagsTestCases {
1139 ctx := &mockContext{result: true}
1140 CheckKotlincFlags(ctx, []string{testCase.in})
1141 if ctx.result != testCase.out {
1142 t.Errorf("incorrect output:")
1143 t.Errorf(" input: %#v", testCase.in)
1144 t.Errorf(" expected: %#v", testCase.out)
1145 t.Errorf(" got: %#v", ctx.result)
1146 }
1147 }
1148}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001149
1150// TODO(jungjw): Consider making this more robust by ignoring path order.
1151func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1152 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1153 flags := strings.Split(variables["javacFlags"], " ")
1154 got := ""
1155 for _, flag := range flags {
1156 keyEnd := strings.Index(flag, "=")
1157 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1158 got = flag[keyEnd+1:]
1159 break
1160 }
1161 }
1162 if expected != got {
1163 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1164 }
1165}
1166
1167func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001168 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001169 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001170 bp := `
1171 java_library {
1172 name: "foo",
1173 srcs: ["a.java"],
1174 java_version: "1.8",
1175 }
1176
1177 java_library {
1178 name: "bar",
1179 srcs: ["b.java"],
1180 sdk_version: "none",
1181 system_modules: "none",
1182 patch_module: "java.base",
1183 java_version: "1.8",
1184 }
1185
1186 java_library {
1187 name: "baz",
1188 srcs: ["c.java"],
1189 patch_module: "java.base",
1190 java_version: "1.8",
1191 }
1192 `
1193 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001194
1195 checkPatchModuleFlag(t, ctx, "foo", "")
1196 checkPatchModuleFlag(t, ctx, "bar", "")
1197 checkPatchModuleFlag(t, ctx, "baz", "")
1198 })
1199
Pete Gillin0c2143e2019-05-02 15:32:11 +01001200 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001201 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001202 bp := `
1203 java_library {
1204 name: "foo",
1205 srcs: ["a.java"],
1206 }
1207
1208 java_library {
1209 name: "bar",
1210 srcs: ["b.java"],
1211 sdk_version: "none",
1212 system_modules: "none",
1213 patch_module: "java.base",
1214 }
1215
1216 java_library {
1217 name: "baz",
1218 srcs: ["c.java"],
1219 patch_module: "java.base",
1220 }
1221 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001222 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001223
1224 checkPatchModuleFlag(t, ctx, "foo", "")
1225 expected := "java.base=.:" + buildDir
1226 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09001227 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001228 checkPatchModuleFlag(t, ctx, "baz", expected)
1229 })
1230}