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