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