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