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