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