Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "path/filepath" |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 21 | "reflect" |
Paul Duffin | daaa332 | 2020-05-26 18:13:57 +0100 | [diff] [blame] | 22 | "regexp" |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 23 | "sort" |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 24 | "strconv" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 25 | "strings" |
| 26 | "testing" |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 27 | |
Jeongik Cha | 28df257 | 2019-11-11 10:46:36 +0900 | [diff] [blame] | 28 | "github.com/google/blueprint/proptools" |
| 29 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 30 | "android/soong/android" |
| 31 | "android/soong/cc" |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 32 | "android/soong/dexpreopt" |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 33 | "android/soong/genrule" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | var buildDir string |
| 37 | |
| 38 | func setUp() { |
| 39 | var err error |
| 40 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 41 | if err != nil { |
| 42 | panic(err) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func tearDown() { |
| 47 | os.RemoveAll(buildDir) |
| 48 | } |
| 49 | |
| 50 | func TestMain(m *testing.M) { |
| 51 | run := func() int { |
| 52 | setUp() |
| 53 | defer tearDown() |
| 54 | |
| 55 | return m.Run() |
| 56 | } |
| 57 | |
| 58 | os.Exit(run()) |
| 59 | } |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 60 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 61 | func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config { |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 62 | bp += dexpreopt.BpToolModulesForTest() |
| 63 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 64 | config := TestConfig(buildDir, env, bp, fs) |
| 65 | |
| 66 | // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that |
| 67 | // it doesn't create a real one, which would fail. |
| 68 | _ = dexpreopt.GlobalSoongConfigForTests(config) |
| 69 | |
| 70 | return config |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 73 | func testContext() *android.TestContext { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 74 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 75 | ctx := android.NewTestArchContext() |
Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 76 | RegisterJavaBuildComponents(ctx) |
| 77 | RegisterAppBuildComponents(ctx) |
| 78 | RegisterAARBuildComponents(ctx) |
Paul Duffin | b0f8507 | 2019-12-19 10:28:56 +0000 | [diff] [blame] | 79 | RegisterGenRuleBuildComponents(ctx) |
Paul Duffin | 43dc1cc | 2019-12-19 11:18:54 +0000 | [diff] [blame] | 80 | RegisterSystemModulesBuildComponents(ctx) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 81 | ctx.RegisterModuleType("java_plugin", PluginFactory) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 82 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
| 83 | ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) |
Paul Duffin | 884363e | 2019-12-19 10:21:09 +0000 | [diff] [blame] | 84 | RegisterDocsBuildComponents(ctx) |
| 85 | RegisterStubsBuildComponents(ctx) |
Paul Duffin | 43dc1cc | 2019-12-19 11:18:54 +0000 | [diff] [blame] | 86 | RegisterSdkLibraryBuildComponents(ctx) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 87 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 88 | |
| 89 | RegisterPrebuiltApisBuildComponents(ctx) |
| 90 | |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 91 | ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 92 | ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory)) |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 93 | ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory)) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 94 | |
| 95 | // Register module types and mutators from cc needed for JNI testing |
Paul Duffin | 77980a8 | 2019-12-19 16:01:36 +0000 | [diff] [blame] | 96 | cc.RegisterRequiredBuildComponentsForTest(ctx) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 97 | |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 98 | dexpreopt.RegisterToolModulesForTest(ctx) |
| 99 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 100 | return ctx |
| 101 | } |
| 102 | |
| 103 | func run(t *testing.T, ctx *android.TestContext, config android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 104 | t.Helper() |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 105 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 106 | pathCtx := android.PathContextForTesting(config) |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 107 | dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 108 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 109 | ctx.Register(config) |
Paul Duffin | baccf7e | 2019-06-11 11:58:30 +0100 | [diff] [blame] | 110 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 111 | android.FailIfErrored(t, errs) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 112 | _, errs = ctx.PrepareBuildActions(config) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 113 | android.FailIfErrored(t, errs) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 116 | func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) { |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 117 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 118 | return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil)) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 119 | } |
| 120 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 121 | func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 122 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 123 | ctx := testContext() |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 124 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 125 | pathCtx := android.PathContextForTesting(config) |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 126 | dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 127 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 128 | ctx.Register(config) |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 129 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
| 130 | if len(errs) > 0 { |
| 131 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 132 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 133 | } |
| 134 | _, errs = ctx.PrepareBuildActions(config) |
| 135 | if len(errs) > 0 { |
| 136 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 137 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 141 | |
| 142 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 145 | func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) { |
| 146 | t.Helper() |
| 147 | return testJavaWithConfig(t, testConfig(nil, bp, fs)) |
| 148 | } |
| 149 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 150 | func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 151 | t.Helper() |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 152 | return testJavaWithFS(t, bp, nil) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 153 | } |
| 154 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 155 | func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 156 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 157 | ctx := testContext() |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 158 | run(t, ctx, config) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 159 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 160 | return ctx, config |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 163 | func moduleToPath(name string) string { |
| 164 | switch { |
| 165 | case name == `""`: |
| 166 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 167 | case strings.HasSuffix(name, ".jar"): |
| 168 | return name |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 169 | default: |
| 170 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 174 | func TestJavaLinkType(t *testing.T) { |
| 175 | testJava(t, ` |
| 176 | java_library { |
| 177 | name: "foo", |
| 178 | srcs: ["a.java"], |
| 179 | libs: ["bar"], |
| 180 | static_libs: ["baz"], |
| 181 | } |
| 182 | |
| 183 | java_library { |
| 184 | name: "bar", |
| 185 | sdk_version: "current", |
| 186 | srcs: ["b.java"], |
| 187 | } |
| 188 | |
| 189 | java_library { |
| 190 | name: "baz", |
| 191 | sdk_version: "system_current", |
| 192 | srcs: ["c.java"], |
| 193 | } |
| 194 | `) |
| 195 | |
| 196 | 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.", ` |
| 197 | java_library { |
| 198 | name: "foo", |
| 199 | srcs: ["a.java"], |
| 200 | libs: ["bar"], |
| 201 | sdk_version: "current", |
| 202 | static_libs: ["baz"], |
| 203 | } |
| 204 | |
| 205 | java_library { |
| 206 | name: "bar", |
| 207 | sdk_version: "current", |
| 208 | srcs: ["b.java"], |
| 209 | } |
| 210 | |
| 211 | java_library { |
| 212 | name: "baz", |
| 213 | sdk_version: "system_current", |
| 214 | srcs: ["c.java"], |
| 215 | } |
| 216 | `) |
| 217 | |
| 218 | testJava(t, ` |
| 219 | java_library { |
| 220 | name: "foo", |
| 221 | srcs: ["a.java"], |
| 222 | libs: ["bar"], |
| 223 | sdk_version: "system_current", |
| 224 | static_libs: ["baz"], |
| 225 | } |
| 226 | |
| 227 | java_library { |
| 228 | name: "bar", |
| 229 | sdk_version: "current", |
| 230 | srcs: ["b.java"], |
| 231 | } |
| 232 | |
| 233 | java_library { |
| 234 | name: "baz", |
| 235 | sdk_version: "system_current", |
| 236 | srcs: ["c.java"], |
| 237 | } |
| 238 | `) |
| 239 | |
| 240 | 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.", ` |
| 241 | java_library { |
| 242 | name: "foo", |
| 243 | srcs: ["a.java"], |
| 244 | libs: ["bar"], |
| 245 | sdk_version: "system_current", |
| 246 | static_libs: ["baz"], |
| 247 | } |
| 248 | |
| 249 | java_library { |
| 250 | name: "bar", |
| 251 | sdk_version: "current", |
| 252 | srcs: ["b.java"], |
| 253 | } |
| 254 | |
| 255 | java_library { |
| 256 | name: "baz", |
| 257 | srcs: ["c.java"], |
| 258 | } |
| 259 | `) |
| 260 | } |
| 261 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 262 | func TestSimple(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 263 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 264 | java_library { |
| 265 | name: "foo", |
| 266 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 267 | libs: ["bar"], |
| 268 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | java_library { |
| 272 | name: "bar", |
| 273 | srcs: ["b.java"], |
| 274 | } |
| 275 | |
| 276 | java_library { |
| 277 | name: "baz", |
| 278 | srcs: ["c.java"], |
| 279 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 280 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 281 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 282 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 283 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 284 | |
| 285 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 286 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 287 | } |
| 288 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 289 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 290 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 291 | bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 292 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 293 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 294 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 297 | if !strings.Contains(javac.Args["classpath"], bazTurbine) { |
| 298 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 302 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 306 | func TestExportedPlugins(t *testing.T) { |
| 307 | type Result struct { |
| 308 | library string |
| 309 | processors string |
| 310 | } |
| 311 | var tests = []struct { |
| 312 | name string |
| 313 | extra string |
| 314 | results []Result |
| 315 | }{ |
| 316 | { |
| 317 | name: "Exported plugin is not a direct plugin", |
| 318 | extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`, |
| 319 | results: []Result{{library: "exports", processors: "-proc:none"}}, |
| 320 | }, |
| 321 | { |
| 322 | name: "Exports plugin to dependee", |
| 323 | extra: ` |
| 324 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 325 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 326 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 327 | `, |
| 328 | results: []Result{ |
| 329 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 330 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 331 | }, |
| 332 | }, |
| 333 | { |
| 334 | name: "Exports plugin to android_library", |
| 335 | extra: ` |
| 336 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 337 | android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 338 | android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 339 | `, |
| 340 | results: []Result{ |
| 341 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 342 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 343 | }, |
| 344 | }, |
| 345 | { |
| 346 | name: "Exports plugin is not propagated via transitive deps", |
| 347 | extra: ` |
| 348 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 349 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 350 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]} |
| 351 | `, |
| 352 | results: []Result{ |
| 353 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 354 | {library: "bar", processors: "-proc:none"}, |
| 355 | }, |
| 356 | }, |
| 357 | { |
| 358 | name: "Exports plugin appends to plugins", |
| 359 | extra: ` |
| 360 | java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"} |
| 361 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 362 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]} |
| 363 | `, |
| 364 | results: []Result{ |
| 365 | {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"}, |
| 366 | }, |
| 367 | }, |
| 368 | } |
| 369 | |
| 370 | for _, test := range tests { |
| 371 | t.Run(test.name, func(t *testing.T) { |
| 372 | ctx, _ := testJava(t, ` |
| 373 | java_plugin { |
| 374 | name: "plugin", |
| 375 | processor_class: "com.android.TestPlugin", |
| 376 | } |
| 377 | `+test.extra) |
| 378 | |
| 379 | for _, want := range test.results { |
| 380 | javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac") |
| 381 | if javac.Args["processor"] != want.processors { |
| 382 | t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"]) |
| 383 | } |
| 384 | } |
| 385 | }) |
| 386 | } |
| 387 | } |
| 388 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 389 | func TestSdkVersionByPartition(t *testing.T) { |
| 390 | testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 391 | java_library { |
| 392 | name: "foo", |
| 393 | srcs: ["a.java"], |
| 394 | vendor: true, |
| 395 | } |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 396 | `) |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 397 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 398 | testJava(t, ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 399 | java_library { |
| 400 | name: "bar", |
| 401 | srcs: ["b.java"], |
| 402 | } |
| 403 | `) |
| 404 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 405 | for _, enforce := range []bool{true, false} { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 406 | bp := ` |
| 407 | java_library { |
| 408 | name: "foo", |
| 409 | srcs: ["a.java"], |
| 410 | product_specific: true, |
| 411 | } |
| 412 | ` |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 413 | |
| 414 | config := testConfig(nil, bp, nil) |
| 415 | config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 416 | if enforce { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 417 | testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", config) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 418 | } else { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 419 | testJavaWithConfig(t, config) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 420 | } |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 424 | func TestArchSpecific(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 425 | ctx, _ := testJava(t, ` |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 426 | java_library { |
| 427 | name: "foo", |
| 428 | srcs: ["a.java"], |
| 429 | target: { |
| 430 | android: { |
| 431 | srcs: ["b.java"], |
| 432 | }, |
| 433 | }, |
| 434 | } |
| 435 | `) |
| 436 | |
| 437 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 438 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 439 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 440 | } |
| 441 | } |
| 442 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 443 | func TestBinary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 444 | ctx, _ := testJava(t, ` |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 445 | java_library_host { |
| 446 | name: "foo", |
| 447 | srcs: ["a.java"], |
| 448 | } |
| 449 | |
| 450 | java_binary_host { |
| 451 | name: "bar", |
| 452 | srcs: ["b.java"], |
| 453 | static_libs: ["foo"], |
| 454 | } |
| 455 | `) |
| 456 | |
| 457 | buildOS := android.BuildOs.String() |
| 458 | |
| 459 | bar := ctx.ModuleForTests("bar", buildOS+"_common") |
| 460 | barJar := bar.Output("bar.jar").Output.String() |
| 461 | barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64") |
| 462 | barWrapperDeps := barWrapper.Output("bar").Implicits.Strings() |
| 463 | |
| 464 | // Test that the install binary wrapper depends on the installed jar file |
| 465 | if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar { |
| 466 | t.Errorf("expected binary wrapper implicits [%q], got %v", |
| 467 | barJar, barWrapperDeps) |
| 468 | } |
| 469 | |
| 470 | } |
| 471 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 472 | func TestPrebuilts(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 473 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 474 | java_library { |
| 475 | name: "foo", |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 476 | srcs: ["a.java", ":stubs-source"], |
Paul Duffin | fcfd791 | 2020-01-31 17:54:30 +0000 | [diff] [blame] | 477 | libs: ["bar", "sdklib"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 478 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 481 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 482 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 483 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 486 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 487 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 488 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 489 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 490 | |
| 491 | dex_import { |
| 492 | name: "qux", |
| 493 | jars: ["b.jar"], |
| 494 | } |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 495 | |
| 496 | java_sdk_library_import { |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 497 | name: "sdklib", |
| 498 | public: { |
| 499 | jars: ["c.jar"], |
| 500 | }, |
| 501 | } |
| 502 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 503 | prebuilt_stubs_sources { |
| 504 | name: "stubs-source", |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 505 | srcs: ["stubs/sources"], |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 506 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 507 | |
| 508 | java_test_import { |
| 509 | name: "test", |
| 510 | jars: ["a.jar"], |
| 511 | test_suites: ["cts"], |
| 512 | test_config: "AndroidTest.xml", |
| 513 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 514 | `) |
| 515 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 516 | fooModule := ctx.ModuleForTests("foo", "android_common") |
| 517 | javac := fooModule.Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 518 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 519 | barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output |
| 520 | bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 521 | sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 522 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 523 | fooLibrary := fooModule.Module().(*Library) |
| 524 | assertDeepEquals(t, "foo java sources incorrect", |
| 525 | []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings()) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 526 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 527 | assertDeepEquals(t, "foo java source jars incorrect", |
| 528 | []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"}, |
| 529 | android.NormalizePathsForTesting(fooLibrary.compiledSrcJars)) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 530 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 531 | if !strings.Contains(javac.Args["classpath"], barJar.String()) { |
| 532 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String()) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 535 | if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) { |
| 536 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String()) |
| 537 | } |
| 538 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 539 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() { |
| 540 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String()) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 541 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 542 | |
| 543 | ctx.ModuleForTests("qux", "android_common").Rule("Cp") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 546 | func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) { |
| 547 | if !reflect.DeepEqual(expected, actual) { |
| 548 | t.Errorf("%s: expected %q, found %q", message, expected, actual) |
| 549 | } |
| 550 | } |
| 551 | |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 552 | func TestJavaSdkLibraryImport(t *testing.T) { |
| 553 | ctx, _ := testJava(t, ` |
| 554 | java_library { |
| 555 | name: "foo", |
| 556 | srcs: ["a.java"], |
| 557 | libs: ["sdklib"], |
| 558 | sdk_version: "current", |
| 559 | } |
| 560 | |
| 561 | java_library { |
| 562 | name: "foo.system", |
| 563 | srcs: ["a.java"], |
| 564 | libs: ["sdklib"], |
| 565 | sdk_version: "system_current", |
| 566 | } |
| 567 | |
| 568 | java_library { |
| 569 | name: "foo.test", |
| 570 | srcs: ["a.java"], |
| 571 | libs: ["sdklib"], |
| 572 | sdk_version: "test_current", |
| 573 | } |
| 574 | |
| 575 | java_sdk_library_import { |
| 576 | name: "sdklib", |
| 577 | public: { |
| 578 | jars: ["a.jar"], |
| 579 | }, |
| 580 | system: { |
| 581 | jars: ["b.jar"], |
| 582 | }, |
| 583 | test: { |
| 584 | jars: ["c.jar"], |
Paul Duffin | 0f8faff | 2020-05-20 16:18:00 +0100 | [diff] [blame] | 585 | stub_srcs: ["c.java"], |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 586 | }, |
| 587 | } |
| 588 | `) |
| 589 | |
| 590 | for _, scope := range []string{"", ".system", ".test"} { |
| 591 | fooModule := ctx.ModuleForTests("foo"+scope, "android_common") |
| 592 | javac := fooModule.Rule("javac") |
| 593 | |
| 594 | sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output |
| 595 | if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) { |
| 596 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String()) |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 601 | func TestDefaults(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 602 | ctx, _ := testJava(t, ` |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 603 | java_defaults { |
| 604 | name: "defaults", |
| 605 | srcs: ["a.java"], |
| 606 | libs: ["bar"], |
| 607 | static_libs: ["baz"], |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 608 | optimize: {enabled: false}, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | java_library { |
| 612 | name: "foo", |
| 613 | defaults: ["defaults"], |
| 614 | } |
| 615 | |
| 616 | java_library { |
| 617 | name: "bar", |
| 618 | srcs: ["b.java"], |
| 619 | } |
| 620 | |
| 621 | java_library { |
| 622 | name: "baz", |
| 623 | srcs: ["c.java"], |
| 624 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 625 | |
| 626 | android_test { |
| 627 | name: "atestOptimize", |
| 628 | defaults: ["defaults"], |
| 629 | optimize: {enabled: true}, |
| 630 | } |
| 631 | |
| 632 | android_test { |
| 633 | name: "atestNoOptimize", |
| 634 | defaults: ["defaults"], |
| 635 | } |
| 636 | |
| 637 | android_test { |
| 638 | name: "atestDefault", |
| 639 | srcs: ["a.java"], |
| 640 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 641 | `) |
| 642 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 643 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 644 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 645 | |
| 646 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 647 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 648 | } |
| 649 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 650 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 651 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 652 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 655 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 656 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 657 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 658 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 659 | |
| 660 | atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8") |
| 661 | if atestOptimize.Output == nil { |
| 662 | t.Errorf("atestOptimize should optimize APK") |
| 663 | } |
| 664 | |
| 665 | atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8") |
| 666 | if atestNoOptimize.Output == nil { |
| 667 | t.Errorf("atestNoOptimize should not optimize APK") |
| 668 | } |
| 669 | |
| 670 | atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8") |
| 671 | if atestDefault.Output == nil { |
| 672 | t.Errorf("atestDefault should optimize APK") |
| 673 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 676 | func TestResources(t *testing.T) { |
| 677 | var table = []struct { |
| 678 | name string |
| 679 | prop string |
| 680 | extra string |
| 681 | args string |
| 682 | }{ |
| 683 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 684 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 685 | name: "resource dirs", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 686 | prop: `java_resource_dirs: ["java-res"]`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 687 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 688 | }, |
| 689 | { |
| 690 | // Test that a module with java_resources includes the files |
| 691 | name: "resource files", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 692 | prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`, |
| 693 | args: "-C . -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 694 | }, |
| 695 | { |
| 696 | // Test that a module with a filegroup in java_resources includes the files with the |
| 697 | // path prefix |
| 698 | name: "resource filegroup", |
| 699 | prop: `java_resources: [":foo-res"]`, |
| 700 | extra: ` |
| 701 | filegroup { |
| 702 | name: "foo-res", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 703 | path: "java-res", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 704 | srcs: ["java-res/a/a", "java-res/b/b"], |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 705 | }`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 706 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 707 | }, |
| 708 | { |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 709 | // Test that a module with wildcards in java_resource_dirs has the correct path prefixes |
| 710 | name: "wildcard dirs", |
| 711 | prop: `java_resource_dirs: ["java-res/*"]`, |
| 712 | args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b", |
| 713 | }, |
| 714 | { |
| 715 | // Test that a module exclude_java_resource_dirs excludes the files |
| 716 | name: "wildcard dirs", |
| 717 | prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`, |
| 718 | args: "-C java-res/a -f java-res/a/a", |
| 719 | }, |
Colin Cross | cedd476 | 2018-09-13 11:26:19 -0700 | [diff] [blame] | 720 | { |
| 721 | // Test wildcards in java_resources |
| 722 | name: "wildcard files", |
| 723 | prop: `java_resources: ["java-res/**/*"]`, |
| 724 | args: "-C . -f java-res/a/a -f java-res/b/b", |
| 725 | }, |
| 726 | { |
| 727 | // Test exclude_java_resources with java_resources |
| 728 | name: "wildcard files with exclude", |
| 729 | prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`, |
| 730 | args: "-C . -f java-res/a/a", |
| 731 | }, |
| 732 | { |
| 733 | // Test exclude_java_resources with java_resource_dirs |
| 734 | name: "resource dirs with exclude files", |
| 735 | prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`, |
| 736 | args: "-C java-res -f java-res/a/a", |
| 737 | }, |
| 738 | { |
| 739 | // Test exclude_java_resource_dirs with java_resource_dirs |
| 740 | name: "resource dirs with exclude files", |
| 741 | prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`, |
| 742 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
| 743 | }, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | for _, test := range table { |
| 747 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 748 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 749 | java_library { |
| 750 | name: "foo", |
| 751 | srcs: [ |
| 752 | "a.java", |
| 753 | "b.java", |
| 754 | "c.java", |
| 755 | ], |
| 756 | `+test.prop+`, |
| 757 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 758 | `+test.extra, |
| 759 | map[string][]byte{ |
| 760 | "java-res/a/a": nil, |
| 761 | "java-res/b/b": nil, |
| 762 | "java-res2/a": nil, |
| 763 | }, |
| 764 | ) |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 765 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 766 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 767 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 768 | |
| 769 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 770 | t.Errorf("foo combined jars %v does not contain %q", |
| 771 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 772 | } |
| 773 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 774 | if fooRes.Args["jarArgs"] != test.args { |
| 775 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 776 | fooRes.Args["jarArgs"], test.args) |
| 777 | } |
| 778 | }) |
| 779 | } |
| 780 | } |
| 781 | |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 782 | func TestIncludeSrcs(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 783 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 784 | java_library { |
| 785 | name: "foo", |
| 786 | srcs: [ |
| 787 | "a.java", |
| 788 | "b.java", |
| 789 | "c.java", |
| 790 | ], |
| 791 | include_srcs: true, |
| 792 | } |
| 793 | |
| 794 | java_library { |
| 795 | name: "bar", |
| 796 | srcs: [ |
| 797 | "a.java", |
| 798 | "b.java", |
| 799 | "c.java", |
| 800 | ], |
| 801 | java_resource_dirs: ["java-res"], |
| 802 | include_srcs: true, |
| 803 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 804 | `, map[string][]byte{ |
| 805 | "java-res/a/a": nil, |
| 806 | "java-res/b/b": nil, |
| 807 | "java-res2/a": nil, |
| 808 | }) |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 809 | |
| 810 | // Test a library with include_srcs: true |
| 811 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
| 812 | fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar") |
| 813 | |
| 814 | if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) { |
| 815 | t.Errorf("foo combined jars %v does not contain %q", w, g) |
| 816 | } |
| 817 | |
| 818 | if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 819 | t.Errorf("foo source jar args %q is not %q", w, g) |
| 820 | } |
| 821 | |
| 822 | // Test a library with include_srcs: true and resources |
| 823 | bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar") |
| 824 | barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar") |
| 825 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") |
| 826 | barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar") |
| 827 | |
| 828 | if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 829 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 830 | } |
| 831 | |
| 832 | if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 833 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 834 | } |
| 835 | |
| 836 | if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) { |
| 837 | t.Errorf("bar combined jars %v does not contain %q", w, g) |
| 838 | } |
| 839 | |
| 840 | if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 841 | t.Errorf("bar source jar args %q is not %q", w, g) |
| 842 | } |
| 843 | |
| 844 | if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w { |
| 845 | t.Errorf("bar resource jar args %q is not %q", w, g) |
| 846 | } |
| 847 | } |
| 848 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 849 | func TestGeneratedSources(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 850 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 851 | java_library { |
| 852 | name: "foo", |
| 853 | srcs: [ |
| 854 | "a*.java", |
| 855 | ":gen", |
| 856 | "b*.java", |
| 857 | ], |
| 858 | } |
| 859 | |
| 860 | genrule { |
| 861 | name: "gen", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 862 | tool_files: ["java-res/a"], |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 863 | out: ["gen.java"], |
| 864 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 865 | `, map[string][]byte{ |
| 866 | "a.java": nil, |
| 867 | "b.java": nil, |
| 868 | }) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 869 | |
| 870 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 871 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 872 | |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 873 | if filepath.Base(genrule.Output.String()) != "gen.java" { |
| 874 | t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String()) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | if len(javac.Inputs) != 3 || |
| 878 | javac.Inputs[0].String() != "a.java" || |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 879 | javac.Inputs[1].String() != genrule.Output.String() || |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 880 | javac.Inputs[2].String() != "b.java" { |
| 881 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 882 | } |
| 883 | } |
| 884 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 885 | func TestTurbine(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 886 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 887 | java_library { |
| 888 | name: "foo", |
| 889 | srcs: ["a.java"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 890 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | java_library { |
| 894 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 895 | srcs: ["b.java"], |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 896 | static_libs: ["foo"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 897 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | java_library { |
| 901 | name: "baz", |
| 902 | srcs: ["c.java"], |
| 903 | libs: ["bar"], |
| 904 | sdk_version: "14", |
| 905 | } |
| 906 | `) |
| 907 | |
| 908 | fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine") |
| 909 | barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine") |
| 910 | barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac") |
| 911 | barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine") |
| 912 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 913 | |
| 914 | if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" { |
| 915 | t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs) |
| 916 | } |
| 917 | |
| 918 | fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 919 | if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) { |
| 920 | t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar) |
| 921 | } |
| 922 | if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) { |
| 923 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar) |
| 924 | } |
| 925 | if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar { |
| 926 | t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar) |
| 927 | } |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 928 | if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") { |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 929 | t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"], |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 930 | "prebuilts/sdk/14/public/android.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
| 934 | func TestSharding(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 935 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 936 | java_library { |
| 937 | name: "bar", |
| 938 | srcs: ["a.java","b.java","c.java"], |
| 939 | javac_shard_size: 1 |
| 940 | } |
| 941 | `) |
| 942 | |
| 943 | barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 944 | for i := 0; i < 3; i++ { |
| 945 | barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)) |
| 946 | if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) { |
| 947 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar) |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 952 | func TestDroiddoc(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 953 | ctx, _ := testJavaWithFS(t, ` |
Paul Duffin | 884363e | 2019-12-19 10:21:09 +0000 | [diff] [blame] | 954 | droiddoc_exported_dir { |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 955 | name: "droiddoc-templates-sdk", |
| 956 | path: ".", |
| 957 | } |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 958 | filegroup { |
| 959 | name: "bar-doc-aidl-srcs", |
| 960 | srcs: ["bar-doc/IBar.aidl"], |
| 961 | path: "bar-doc", |
| 962 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 963 | droiddoc { |
| 964 | name: "bar-doc", |
| 965 | srcs: [ |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 966 | "bar-doc/a.java", |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 967 | "bar-doc/IFoo.aidl", |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 968 | ":bar-doc-aidl-srcs", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 969 | ], |
| 970 | exclude_srcs: [ |
| 971 | "bar-doc/b.java" |
| 972 | ], |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 973 | custom_template: "droiddoc-templates-sdk", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 974 | hdf: [ |
| 975 | "android.whichdoc offline", |
| 976 | ], |
| 977 | knowntags: [ |
| 978 | "bar-doc/known_oj_tags.txt", |
| 979 | ], |
| 980 | proofread_file: "libcore-proofread.txt", |
| 981 | todo_file: "libcore-docs-todo.html", |
| 982 | args: "-offlinemode -title \"libcore\"", |
| 983 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 984 | `, |
| 985 | map[string][]byte{ |
| 986 | "bar-doc/a.java": nil, |
| 987 | "bar-doc/b.java": nil, |
| 988 | }) |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 989 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 990 | barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc") |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 991 | var javaSrcs []string |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 992 | for _, i := range barDoc.Inputs { |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 993 | javaSrcs = append(javaSrcs, i.Base()) |
| 994 | } |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 995 | if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" { |
| 996 | t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs) |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 997 | } |
| 998 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 999 | aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl") |
| 1000 | if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) { |
| 1001 | t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g) |
| 1002 | } |
| 1003 | |
| 1004 | if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) { |
| 1005 | t.Errorf("aidl inputs must be %q, but was %q", w, g) |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 1006 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1009 | func TestDroidstubsWithSystemModules(t *testing.T) { |
| 1010 | ctx, _ := testJava(t, ` |
| 1011 | droidstubs { |
| 1012 | name: "stubs-source-system-modules", |
| 1013 | srcs: [ |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 1014 | "bar-doc/a.java", |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1015 | ], |
| 1016 | sdk_version: "none", |
| 1017 | system_modules: "source-system-modules", |
| 1018 | } |
| 1019 | |
| 1020 | java_library { |
| 1021 | name: "source-jar", |
| 1022 | srcs: [ |
| 1023 | "a.java", |
| 1024 | ], |
| 1025 | } |
| 1026 | |
| 1027 | java_system_modules { |
| 1028 | name: "source-system-modules", |
| 1029 | libs: ["source-jar"], |
| 1030 | } |
| 1031 | |
| 1032 | droidstubs { |
| 1033 | name: "stubs-prebuilt-system-modules", |
| 1034 | srcs: [ |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame^] | 1035 | "bar-doc/a.java", |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1036 | ], |
| 1037 | sdk_version: "none", |
| 1038 | system_modules: "prebuilt-system-modules", |
| 1039 | } |
| 1040 | |
| 1041 | java_import { |
| 1042 | name: "prebuilt-jar", |
| 1043 | jars: ["a.jar"], |
| 1044 | } |
| 1045 | |
| 1046 | java_system_modules_import { |
| 1047 | name: "prebuilt-system-modules", |
| 1048 | libs: ["prebuilt-jar"], |
| 1049 | } |
| 1050 | `) |
| 1051 | |
| 1052 | checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar") |
| 1053 | |
| 1054 | checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar") |
| 1055 | } |
| 1056 | |
| 1057 | func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) { |
| 1058 | metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava") |
| 1059 | var systemJars []string |
| 1060 | for _, i := range metalavaRule.Implicits { |
| 1061 | systemJars = append(systemJars, i.Base()) |
| 1062 | } |
Ramy Medhat | c7965cd | 2020-04-30 03:08:37 -0400 | [diff] [blame] | 1063 | if len(systemJars) < 1 || systemJars[0] != systemJar { |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1064 | t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars) |
| 1065 | } |
| 1066 | } |
| 1067 | |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 1068 | func TestJarGenrules(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1069 | ctx, _ := testJava(t, ` |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 1070 | java_library { |
| 1071 | name: "foo", |
| 1072 | srcs: ["a.java"], |
| 1073 | } |
| 1074 | |
| 1075 | java_genrule { |
| 1076 | name: "jargen", |
| 1077 | tool_files: ["b.java"], |
| 1078 | cmd: "$(location b.java) $(in) $(out)", |
| 1079 | out: ["jargen.jar"], |
| 1080 | srcs: [":foo"], |
| 1081 | } |
| 1082 | |
| 1083 | java_library { |
| 1084 | name: "bar", |
| 1085 | static_libs: ["jargen"], |
| 1086 | srcs: ["c.java"], |
| 1087 | } |
| 1088 | |
| 1089 | java_library { |
| 1090 | name: "baz", |
| 1091 | libs: ["jargen"], |
| 1092 | srcs: ["c.java"], |
| 1093 | } |
| 1094 | `) |
| 1095 | |
| 1096 | foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar") |
| 1097 | jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar") |
| 1098 | bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar") |
| 1099 | baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar") |
| 1100 | barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar") |
| 1101 | |
| 1102 | if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() { |
| 1103 | t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings()) |
| 1104 | } |
| 1105 | |
| 1106 | if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) { |
| 1107 | t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String()) |
| 1108 | } |
| 1109 | |
| 1110 | if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) { |
| 1111 | t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String()) |
| 1112 | } |
| 1113 | |
| 1114 | if len(barCombined.Inputs) != 2 || |
| 1115 | barCombined.Inputs[0].String() != bar.Output.String() || |
| 1116 | barCombined.Inputs[1].String() != jargen.Output.String() { |
| 1117 | t.Errorf("bar combined jar inputs %v is not [%q, %q]", |
| 1118 | barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String()) |
| 1119 | } |
| 1120 | } |
| 1121 | |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1122 | func TestExcludeFileGroupInSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1123 | ctx, _ := testJava(t, ` |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1124 | java_library { |
| 1125 | name: "foo", |
| 1126 | srcs: ["a.java", ":foo-srcs"], |
| 1127 | exclude_srcs: ["a.java", ":foo-excludes"], |
| 1128 | } |
| 1129 | |
| 1130 | filegroup { |
| 1131 | name: "foo-srcs", |
| 1132 | srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"], |
| 1133 | } |
| 1134 | |
| 1135 | filegroup { |
| 1136 | name: "foo-excludes", |
| 1137 | srcs: ["java-fg/a.java", "java-fg/b.java"], |
| 1138 | } |
| 1139 | `) |
| 1140 | |
| 1141 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 1142 | |
| 1143 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" { |
| 1144 | t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs) |
| 1145 | } |
| 1146 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1147 | |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1148 | func TestJavaLibrary(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1149 | config := testConfig(nil, "", map[string][]byte{ |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1150 | "libcore/Android.bp": []byte(` |
| 1151 | java_library { |
| 1152 | name: "core", |
| 1153 | sdk_version: "none", |
| 1154 | system_modules: "none", |
| 1155 | }`), |
| 1156 | }) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1157 | ctx := testContext() |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1158 | run(t, ctx, config) |
| 1159 | } |
| 1160 | |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1161 | func TestJavaSdkLibrary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1162 | ctx, _ := testJava(t, ` |
Paul Duffin | 884363e | 2019-12-19 10:21:09 +0000 | [diff] [blame] | 1163 | droiddoc_exported_dir { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1164 | name: "droiddoc-templates-sdk", |
| 1165 | path: ".", |
| 1166 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1167 | java_sdk_library { |
| 1168 | name: "foo", |
| 1169 | srcs: ["a.java", "b.java"], |
| 1170 | api_packages: ["foo"], |
| 1171 | } |
| 1172 | java_sdk_library { |
| 1173 | name: "bar", |
| 1174 | srcs: ["a.java", "b.java"], |
| 1175 | api_packages: ["bar"], |
| 1176 | } |
| 1177 | java_library { |
| 1178 | name: "baz", |
| 1179 | srcs: ["c.java"], |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 1180 | libs: ["foo", "bar.stubs"], |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1181 | sdk_version: "system_current", |
| 1182 | } |
Paul Duffin | dfa131e | 2020-05-15 20:37:11 +0100 | [diff] [blame] | 1183 | java_sdk_library { |
| 1184 | name: "barney", |
| 1185 | srcs: ["c.java"], |
| 1186 | api_only: true, |
| 1187 | } |
| 1188 | java_sdk_library { |
| 1189 | name: "betty", |
| 1190 | srcs: ["c.java"], |
| 1191 | shared_library: false, |
| 1192 | } |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 1193 | java_sdk_library_import { |
| 1194 | name: "quuz", |
| 1195 | public: { |
| 1196 | jars: ["c.jar"], |
| 1197 | }, |
| 1198 | } |
| 1199 | java_sdk_library_import { |
| 1200 | name: "fred", |
| 1201 | public: { |
| 1202 | jars: ["b.jar"], |
| 1203 | }, |
| 1204 | } |
Paul Duffin | dfa131e | 2020-05-15 20:37:11 +0100 | [diff] [blame] | 1205 | java_sdk_library_import { |
| 1206 | name: "wilma", |
| 1207 | public: { |
| 1208 | jars: ["b.jar"], |
| 1209 | }, |
| 1210 | shared_library: false, |
| 1211 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1212 | java_library { |
| 1213 | name: "qux", |
| 1214 | srcs: ["c.java"], |
Paul Duffin | dfa131e | 2020-05-15 20:37:11 +0100 | [diff] [blame] | 1215 | libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"], |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1216 | sdk_version: "system_current", |
| 1217 | } |
Paul Duffin | 726d23c | 2020-01-22 16:30:37 +0000 | [diff] [blame] | 1218 | java_library { |
| 1219 | name: "baz-test", |
| 1220 | srcs: ["c.java"], |
| 1221 | libs: ["foo"], |
| 1222 | sdk_version: "test_current", |
| 1223 | } |
Paul Duffin | a2db18f | 2020-01-22 17:11:15 +0000 | [diff] [blame] | 1224 | java_library { |
| 1225 | name: "baz-29", |
| 1226 | srcs: ["c.java"], |
| 1227 | libs: ["foo"], |
| 1228 | sdk_version: "system_29", |
| 1229 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1230 | `) |
| 1231 | |
| 1232 | // check the existence of the internal modules |
| 1233 | ctx.ModuleForTests("foo", "android_common") |
Paul Duffin | dd9d074 | 2020-05-08 15:52:37 +0100 | [diff] [blame] | 1234 | ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common") |
| 1235 | ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common") |
| 1236 | ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common") |
| 1237 | ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common") |
| 1238 | ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common") |
| 1239 | ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common") |
Jiyong Park | e383388 | 2020-02-17 17:28:10 +0900 | [diff] [blame] | 1240 | ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1241 | ctx.ModuleForTests("foo.api.public.28", "") |
| 1242 | ctx.ModuleForTests("foo.api.system.28", "") |
| 1243 | ctx.ModuleForTests("foo.api.test.28", "") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1244 | |
| 1245 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 1246 | // tests if baz is actually linked to the stubs lib |
| 1247 | if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") { |
| 1248 | t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"], |
| 1249 | "foo.stubs.system.jar") |
| 1250 | } |
| 1251 | // ... and not to the impl lib |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 1252 | if strings.Contains(bazJavac.Args["classpath"], "foo.jar") { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1253 | t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"], |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 1254 | "foo.jar") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1255 | } |
| 1256 | // test if baz is not linked to the system variant of foo |
| 1257 | if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") { |
| 1258 | t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"], |
| 1259 | "foo.stubs.jar") |
| 1260 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1261 | |
Paul Duffin | 726d23c | 2020-01-22 16:30:37 +0000 | [diff] [blame] | 1262 | bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac") |
| 1263 | // tests if baz-test is actually linked to the test stubs lib |
| 1264 | if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") { |
| 1265 | t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"], |
| 1266 | "foo.stubs.test.jar") |
| 1267 | } |
| 1268 | |
Paul Duffin | a2db18f | 2020-01-22 17:11:15 +0000 | [diff] [blame] | 1269 | baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac") |
| 1270 | // tests if baz-29 is actually linked to the system 29 stubs lib |
| 1271 | if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") { |
| 1272 | t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"], |
| 1273 | "prebuilts/sdk/29/system/foo.jar") |
| 1274 | } |
| 1275 | |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1276 | // test if baz has exported SDK lib names foo and bar to qux |
| 1277 | qux := ctx.ModuleForTests("qux", "android_common") |
| 1278 | if quxLib, ok := qux.Module().(*Library); ok { |
| 1279 | sdkLibs := quxLib.ExportedSdkLibs() |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 1280 | sort.Strings(sdkLibs) |
| 1281 | if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) { |
| 1282 | t.Errorf("qux should export %q but exports %q", w, sdkLibs) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1283 | } |
| 1284 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1285 | } |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 1286 | |
Paul Duffin | daaa332 | 2020-05-26 18:13:57 +0100 | [diff] [blame] | 1287 | func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) { |
| 1288 | ctx, _ := testJava(t, ` |
| 1289 | java_sdk_library { |
| 1290 | name: "foo", |
| 1291 | srcs: ["a.java"], |
| 1292 | api_only: true, |
| 1293 | public: { |
| 1294 | enabled: true, |
| 1295 | }, |
| 1296 | } |
| 1297 | |
| 1298 | java_library { |
| 1299 | name: "bar", |
| 1300 | srcs: ["b.java"], |
| 1301 | libs: ["foo"], |
| 1302 | } |
| 1303 | `) |
| 1304 | |
| 1305 | // The bar library should depend on the stubs jar. |
| 1306 | barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac") |
| 1307 | if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) { |
| 1308 | t.Errorf("expected %q, found %#q", expected, actual) |
| 1309 | } |
| 1310 | } |
| 1311 | |
Paul Duffin | 46dc45a | 2020-05-14 15:39:10 +0100 | [diff] [blame] | 1312 | func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) { |
| 1313 | testJava(t, ` |
| 1314 | java_sdk_library { |
| 1315 | name: "foo", |
| 1316 | srcs: ["a.java"], |
| 1317 | api_packages: ["foo"], |
| 1318 | public: { |
| 1319 | enabled: true, |
| 1320 | }, |
| 1321 | } |
| 1322 | |
| 1323 | java_library { |
| 1324 | name: "bar", |
| 1325 | srcs: ["b.java", ":foo{.public.stubs.source}"], |
| 1326 | } |
| 1327 | `) |
| 1328 | } |
| 1329 | |
| 1330 | func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) { |
| 1331 | testJavaError(t, `"foo" does not provide api scope system`, ` |
| 1332 | java_sdk_library { |
| 1333 | name: "foo", |
| 1334 | srcs: ["a.java"], |
| 1335 | api_packages: ["foo"], |
| 1336 | public: { |
| 1337 | enabled: true, |
| 1338 | }, |
| 1339 | } |
| 1340 | |
| 1341 | java_library { |
| 1342 | name: "bar", |
| 1343 | srcs: ["b.java", ":foo{.system.stubs.source}"], |
| 1344 | } |
| 1345 | `) |
| 1346 | } |
| 1347 | |
| 1348 | func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) { |
| 1349 | testJava(t, ` |
| 1350 | java_sdk_library_import { |
| 1351 | name: "foo", |
| 1352 | public: { |
| 1353 | jars: ["a.jar"], |
| 1354 | stub_srcs: ["a.java"], |
| 1355 | current_api: "api/current.txt", |
| 1356 | removed_api: "api/removed.txt", |
| 1357 | }, |
| 1358 | } |
| 1359 | |
| 1360 | java_library { |
| 1361 | name: "bar", |
| 1362 | srcs: [":foo{.public.stubs.source}"], |
| 1363 | java_resources: [ |
| 1364 | ":foo{.public.api.txt}", |
| 1365 | ":foo{.public.removed-api.txt}", |
| 1366 | ], |
| 1367 | } |
| 1368 | `) |
| 1369 | } |
| 1370 | |
| 1371 | func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) { |
| 1372 | bp := ` |
| 1373 | java_sdk_library_import { |
| 1374 | name: "foo", |
| 1375 | public: { |
| 1376 | jars: ["a.jar"], |
| 1377 | }, |
| 1378 | } |
| 1379 | ` |
| 1380 | |
| 1381 | t.Run("stubs.source", func(t *testing.T) { |
| 1382 | testJavaError(t, `stubs.source not available for api scope public`, bp+` |
| 1383 | java_library { |
| 1384 | name: "bar", |
| 1385 | srcs: [":foo{.public.stubs.source}"], |
| 1386 | java_resources: [ |
| 1387 | ":foo{.public.api.txt}", |
| 1388 | ":foo{.public.removed-api.txt}", |
| 1389 | ], |
| 1390 | } |
| 1391 | `) |
| 1392 | }) |
| 1393 | |
| 1394 | t.Run("api.txt", func(t *testing.T) { |
| 1395 | testJavaError(t, `api.txt not available for api scope public`, bp+` |
| 1396 | java_library { |
| 1397 | name: "bar", |
| 1398 | srcs: ["a.java"], |
| 1399 | java_resources: [ |
| 1400 | ":foo{.public.api.txt}", |
| 1401 | ], |
| 1402 | } |
| 1403 | `) |
| 1404 | }) |
| 1405 | |
| 1406 | t.Run("removed-api.txt", func(t *testing.T) { |
| 1407 | testJavaError(t, `removed-api.txt not available for api scope public`, bp+` |
| 1408 | java_library { |
| 1409 | name: "bar", |
| 1410 | srcs: ["a.java"], |
| 1411 | java_resources: [ |
| 1412 | ":foo{.public.removed-api.txt}", |
| 1413 | ], |
| 1414 | } |
| 1415 | `) |
| 1416 | }) |
| 1417 | } |
| 1418 | |
Paul Duffin | 3375e35 | 2020-04-28 10:44:03 +0100 | [diff] [blame] | 1419 | func TestJavaSdkLibrary_InvalidScopes(t *testing.T) { |
| 1420 | testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, ` |
| 1421 | java_sdk_library { |
| 1422 | name: "foo", |
| 1423 | srcs: ["a.java", "b.java"], |
| 1424 | api_packages: ["foo"], |
| 1425 | // Explicitly disable public to test the check that ensures the set of enabled |
| 1426 | // scopes is consistent. |
| 1427 | public: { |
| 1428 | enabled: false, |
| 1429 | }, |
| 1430 | system: { |
| 1431 | enabled: true, |
| 1432 | }, |
| 1433 | } |
| 1434 | `) |
| 1435 | } |
| 1436 | |
Paul Duffin | 87a05a3 | 2020-05-12 11:50:28 +0100 | [diff] [blame] | 1437 | func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) { |
| 1438 | testJava(t, ` |
| 1439 | java_sdk_library { |
| 1440 | name: "foo", |
| 1441 | srcs: ["a.java", "b.java"], |
| 1442 | api_packages: ["foo"], |
| 1443 | system: { |
| 1444 | enabled: true, |
| 1445 | sdk_version: "module_current", |
| 1446 | }, |
| 1447 | } |
| 1448 | `) |
| 1449 | } |
| 1450 | |
Paul Duffin | 803a956 | 2020-05-20 11:52:25 +0100 | [diff] [blame] | 1451 | func TestJavaSdkLibrary_MissingScope(t *testing.T) { |
| 1452 | testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, ` |
| 1453 | java_sdk_library { |
| 1454 | name: "foo", |
| 1455 | srcs: ["a.java"], |
| 1456 | public: { |
| 1457 | enabled: false, |
| 1458 | }, |
| 1459 | } |
| 1460 | |
| 1461 | java_library { |
| 1462 | name: "baz", |
| 1463 | srcs: ["a.java"], |
| 1464 | libs: ["foo"], |
| 1465 | sdk_version: "module_current", |
| 1466 | } |
| 1467 | `) |
| 1468 | } |
| 1469 | |
| 1470 | func TestJavaSdkLibrary_FallbackScope(t *testing.T) { |
| 1471 | testJava(t, ` |
| 1472 | java_sdk_library { |
| 1473 | name: "foo", |
| 1474 | srcs: ["a.java"], |
| 1475 | system: { |
| 1476 | enabled: true, |
| 1477 | }, |
| 1478 | } |
| 1479 | |
| 1480 | java_library { |
| 1481 | name: "baz", |
| 1482 | srcs: ["a.java"], |
| 1483 | libs: ["foo"], |
| 1484 | // foo does not have module-lib scope so it should fallback to system |
| 1485 | sdk_version: "module_current", |
| 1486 | } |
| 1487 | `) |
| 1488 | } |
| 1489 | |
Jiyong Park | 932cdfe | 2020-05-28 00:19:53 +0900 | [diff] [blame] | 1490 | func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) { |
| 1491 | ctx, _ := testJava(t, ` |
| 1492 | java_sdk_library { |
| 1493 | name: "foo", |
| 1494 | srcs: ["a.java"], |
| 1495 | system: { |
| 1496 | enabled: true, |
| 1497 | }, |
| 1498 | default_to_stubs: true, |
| 1499 | } |
| 1500 | |
| 1501 | java_library { |
| 1502 | name: "baz", |
| 1503 | srcs: ["a.java"], |
| 1504 | libs: ["foo"], |
| 1505 | // does not have sdk_version set, should fallback to module, |
| 1506 | // which will then fallback to system because the module scope |
| 1507 | // is not enabled. |
| 1508 | } |
| 1509 | `) |
| 1510 | // The baz library should depend on the system stubs jar. |
| 1511 | bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 1512 | if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) { |
| 1513 | t.Errorf("expected %q, found %#q", expected, actual) |
| 1514 | } |
| 1515 | } |
| 1516 | |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 1517 | var compilerFlagsTestCases = []struct { |
| 1518 | in string |
| 1519 | out bool |
| 1520 | }{ |
| 1521 | { |
| 1522 | in: "a", |
| 1523 | out: false, |
| 1524 | }, |
| 1525 | { |
| 1526 | in: "-a", |
| 1527 | out: true, |
| 1528 | }, |
| 1529 | { |
| 1530 | in: "-no-jdk", |
| 1531 | out: false, |
| 1532 | }, |
| 1533 | { |
| 1534 | in: "-no-stdlib", |
| 1535 | out: false, |
| 1536 | }, |
| 1537 | { |
| 1538 | in: "-kotlin-home", |
| 1539 | out: false, |
| 1540 | }, |
| 1541 | { |
| 1542 | in: "-kotlin-home /some/path", |
| 1543 | out: false, |
| 1544 | }, |
| 1545 | { |
| 1546 | in: "-include-runtime", |
| 1547 | out: false, |
| 1548 | }, |
| 1549 | { |
| 1550 | in: "-Xintellij-plugin-root", |
| 1551 | out: false, |
| 1552 | }, |
| 1553 | } |
| 1554 | |
| 1555 | type mockContext struct { |
| 1556 | android.ModuleContext |
| 1557 | result bool |
| 1558 | } |
| 1559 | |
| 1560 | func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) { |
| 1561 | // CheckBadCompilerFlags calls this function when the flag should be rejected |
| 1562 | ctx.result = false |
| 1563 | } |
| 1564 | |
| 1565 | func TestCompilerFlags(t *testing.T) { |
| 1566 | for _, testCase := range compilerFlagsTestCases { |
| 1567 | ctx := &mockContext{result: true} |
| 1568 | CheckKotlincFlags(ctx, []string{testCase.in}) |
| 1569 | if ctx.result != testCase.out { |
| 1570 | t.Errorf("incorrect output:") |
| 1571 | t.Errorf(" input: %#v", testCase.in) |
| 1572 | t.Errorf(" expected: %#v", testCase.out) |
| 1573 | t.Errorf(" got: %#v", ctx.result) |
| 1574 | } |
| 1575 | } |
| 1576 | } |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1577 | |
| 1578 | // TODO(jungjw): Consider making this more robust by ignoring path order. |
| 1579 | func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) { |
| 1580 | variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests() |
| 1581 | flags := strings.Split(variables["javacFlags"], " ") |
| 1582 | got := "" |
| 1583 | for _, flag := range flags { |
| 1584 | keyEnd := strings.Index(flag, "=") |
| 1585 | if keyEnd > -1 && flag[:keyEnd] == "--patch-module" { |
| 1586 | got = flag[keyEnd+1:] |
| 1587 | break |
| 1588 | } |
| 1589 | } |
| 1590 | if expected != got { |
| 1591 | t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got) |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | func TestPatchModule(t *testing.T) { |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1596 | t.Run("Java language level 8", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1597 | // Test with legacy javac -source 1.8 -target 1.8 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1598 | bp := ` |
| 1599 | java_library { |
| 1600 | name: "foo", |
| 1601 | srcs: ["a.java"], |
| 1602 | java_version: "1.8", |
| 1603 | } |
| 1604 | |
| 1605 | java_library { |
| 1606 | name: "bar", |
| 1607 | srcs: ["b.java"], |
| 1608 | sdk_version: "none", |
| 1609 | system_modules: "none", |
| 1610 | patch_module: "java.base", |
| 1611 | java_version: "1.8", |
| 1612 | } |
| 1613 | |
| 1614 | java_library { |
| 1615 | name: "baz", |
| 1616 | srcs: ["c.java"], |
| 1617 | patch_module: "java.base", |
| 1618 | java_version: "1.8", |
| 1619 | } |
| 1620 | ` |
| 1621 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1622 | |
| 1623 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1624 | checkPatchModuleFlag(t, ctx, "bar", "") |
| 1625 | checkPatchModuleFlag(t, ctx, "baz", "") |
| 1626 | }) |
| 1627 | |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1628 | t.Run("Java language level 9", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1629 | // Test with default javac -source 9 -target 9 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1630 | bp := ` |
| 1631 | java_library { |
| 1632 | name: "foo", |
| 1633 | srcs: ["a.java"], |
| 1634 | } |
| 1635 | |
| 1636 | java_library { |
| 1637 | name: "bar", |
| 1638 | srcs: ["b.java"], |
| 1639 | sdk_version: "none", |
| 1640 | system_modules: "none", |
| 1641 | patch_module: "java.base", |
| 1642 | } |
| 1643 | |
| 1644 | java_library { |
| 1645 | name: "baz", |
| 1646 | srcs: ["c.java"], |
| 1647 | patch_module: "java.base", |
| 1648 | } |
| 1649 | ` |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1650 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1651 | |
| 1652 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1653 | expected := "java.base=.:" + buildDir |
| 1654 | checkPatchModuleFlag(t, ctx, "bar", expected) |
Jiyong Park | 231d056 | 2019-12-18 17:28:17 +0900 | [diff] [blame] | 1655 | expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":") |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1656 | checkPatchModuleFlag(t, ctx, "baz", expected) |
| 1657 | }) |
| 1658 | } |
Paul Duffin | a7b9f42 | 2020-01-10 17:12:18 +0000 | [diff] [blame] | 1659 | |
| 1660 | func TestJavaSystemModules(t *testing.T) { |
| 1661 | ctx, _ := testJava(t, ` |
| 1662 | java_system_modules { |
| 1663 | name: "system-modules", |
| 1664 | libs: ["system-module1", "system-module2"], |
| 1665 | } |
| 1666 | java_library { |
| 1667 | name: "system-module1", |
| 1668 | srcs: ["a.java"], |
| 1669 | sdk_version: "none", |
| 1670 | system_modules: "none", |
| 1671 | } |
| 1672 | java_library { |
| 1673 | name: "system-module2", |
| 1674 | srcs: ["b.java"], |
| 1675 | sdk_version: "none", |
| 1676 | system_modules: "none", |
| 1677 | } |
| 1678 | `) |
| 1679 | |
| 1680 | // check the existence of the module |
| 1681 | systemModules := ctx.ModuleForTests("system-modules", "android_common") |
| 1682 | |
| 1683 | cmd := systemModules.Rule("jarsTosystemModules") |
| 1684 | |
| 1685 | // make sure the command compiles against the supplied modules. |
| 1686 | for _, module := range []string{"system-module1.jar", "system-module2.jar"} { |
| 1687 | if !strings.Contains(cmd.Args["classpath"], module) { |
| 1688 | t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"], |
| 1689 | module) |
| 1690 | } |
| 1691 | } |
| 1692 | } |
Paul Duffin | 90169ba | 2020-01-10 17:16:44 +0000 | [diff] [blame] | 1693 | |
| 1694 | func TestJavaSystemModulesImport(t *testing.T) { |
| 1695 | ctx, _ := testJava(t, ` |
| 1696 | java_system_modules_import { |
| 1697 | name: "system-modules", |
| 1698 | libs: ["system-module1", "system-module2"], |
| 1699 | } |
| 1700 | java_import { |
| 1701 | name: "system-module1", |
| 1702 | jars: ["a.jar"], |
| 1703 | } |
| 1704 | java_import { |
| 1705 | name: "system-module2", |
| 1706 | jars: ["b.jar"], |
| 1707 | } |
| 1708 | `) |
| 1709 | |
| 1710 | // check the existence of the module |
| 1711 | systemModules := ctx.ModuleForTests("system-modules", "android_common") |
| 1712 | |
| 1713 | cmd := systemModules.Rule("jarsTosystemModules") |
| 1714 | |
| 1715 | // make sure the command compiles against the supplied modules. |
| 1716 | for _, module := range []string{"system-module1.jar", "system-module2.jar"} { |
| 1717 | if !strings.Contains(cmd.Args["classpath"], module) { |
| 1718 | t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"], |
| 1719 | module) |
| 1720 | } |
| 1721 | } |
| 1722 | } |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1723 | |
| 1724 | func TestJavaLibraryWithSystemModules(t *testing.T) { |
| 1725 | ctx, _ := testJava(t, ` |
| 1726 | java_library { |
| 1727 | name: "lib-with-source-system-modules", |
| 1728 | srcs: [ |
| 1729 | "a.java", |
| 1730 | ], |
| 1731 | sdk_version: "none", |
| 1732 | system_modules: "source-system-modules", |
| 1733 | } |
| 1734 | |
| 1735 | java_library { |
| 1736 | name: "source-jar", |
| 1737 | srcs: [ |
| 1738 | "a.java", |
| 1739 | ], |
| 1740 | } |
| 1741 | |
| 1742 | java_system_modules { |
| 1743 | name: "source-system-modules", |
| 1744 | libs: ["source-jar"], |
| 1745 | } |
| 1746 | |
| 1747 | java_library { |
| 1748 | name: "lib-with-prebuilt-system-modules", |
| 1749 | srcs: [ |
| 1750 | "a.java", |
| 1751 | ], |
| 1752 | sdk_version: "none", |
| 1753 | system_modules: "prebuilt-system-modules", |
| 1754 | } |
| 1755 | |
| 1756 | java_import { |
| 1757 | name: "prebuilt-jar", |
| 1758 | jars: ["a.jar"], |
| 1759 | } |
| 1760 | |
| 1761 | java_system_modules_import { |
| 1762 | name: "prebuilt-system-modules", |
| 1763 | libs: ["prebuilt-jar"], |
| 1764 | } |
| 1765 | `) |
| 1766 | |
| 1767 | checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar") |
| 1768 | |
| 1769 | checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar") |
| 1770 | } |
| 1771 | |
| 1772 | func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) { |
| 1773 | javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac") |
| 1774 | bootClasspath := javacRule.Args["bootClasspath"] |
| 1775 | if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) { |
| 1776 | t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath) |
| 1777 | } |
| 1778 | } |
Jiyong Park | 19604de | 2020-03-24 16:44:11 +0900 | [diff] [blame] | 1779 | |
| 1780 | func TestAidlExportIncludeDirsFromImports(t *testing.T) { |
| 1781 | ctx, _ := testJava(t, ` |
| 1782 | java_library { |
| 1783 | name: "foo", |
| 1784 | srcs: ["aidl/foo/IFoo.aidl"], |
| 1785 | libs: ["bar"], |
| 1786 | } |
| 1787 | |
| 1788 | java_import { |
| 1789 | name: "bar", |
| 1790 | jars: ["a.jar"], |
| 1791 | aidl: { |
| 1792 | export_include_dirs: ["aidl/bar"], |
| 1793 | }, |
| 1794 | } |
| 1795 | `) |
| 1796 | |
| 1797 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1798 | expectedAidlFlag := "-Iaidl/bar" |
| 1799 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1800 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1801 | } |
| 1802 | } |