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" |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 22 | "strconv" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 23 | "strings" |
| 24 | "testing" |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 25 | |
Jeongik Cha | 28df257 | 2019-11-11 10:46:36 +0900 | [diff] [blame] | 26 | "github.com/google/blueprint/proptools" |
| 27 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 28 | "android/soong/android" |
| 29 | "android/soong/cc" |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 30 | "android/soong/dexpreopt" |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 31 | "android/soong/genrule" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | var buildDir string |
| 35 | |
| 36 | func setUp() { |
| 37 | var err error |
| 38 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 39 | if err != nil { |
| 40 | panic(err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func tearDown() { |
| 45 | os.RemoveAll(buildDir) |
| 46 | } |
| 47 | |
| 48 | func TestMain(m *testing.M) { |
| 49 | run := func() int { |
| 50 | setUp() |
| 51 | defer tearDown() |
| 52 | |
| 53 | return m.Run() |
| 54 | } |
| 55 | |
| 56 | os.Exit(run()) |
| 57 | } |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 58 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 59 | func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config { |
| 60 | return TestConfig(buildDir, env, bp, fs) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 63 | func testContext() *android.TestContext { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 64 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 65 | ctx := android.NewTestArchContext() |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 66 | ctx.RegisterModuleType("android_app", AndroidAppFactory) |
| 67 | ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory) |
| 68 | ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory) |
| 69 | ctx.RegisterModuleType("android_library", AndroidLibraryFactory) |
| 70 | ctx.RegisterModuleType("android_test", AndroidTestFactory) |
| 71 | ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory) |
| 72 | ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory) |
| 73 | ctx.RegisterModuleType("java_binary", BinaryFactory) |
| 74 | ctx.RegisterModuleType("java_binary_host", BinaryHostFactory) |
| 75 | ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory) |
| 76 | ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory) |
| 77 | ctx.RegisterModuleType("java_library", LibraryFactory) |
| 78 | ctx.RegisterModuleType("java_library_host", LibraryHostFactory) |
| 79 | ctx.RegisterModuleType("java_test", TestFactory) |
| 80 | ctx.RegisterModuleType("java_import", ImportFactory) |
| 81 | ctx.RegisterModuleType("java_import_host", ImportFactoryHost) |
Paul Duffin | 4735766 | 2019-12-05 14:07:14 +0000 | [diff] [blame] | 82 | ctx.RegisterModuleType("java_defaults", DefaultsFactory) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 83 | ctx.RegisterModuleType("java_system_modules", SystemModulesFactory) |
| 84 | ctx.RegisterModuleType("java_genrule", genRuleFactory) |
| 85 | ctx.RegisterModuleType("java_plugin", PluginFactory) |
| 86 | ctx.RegisterModuleType("dex_import", DexImportFactory) |
| 87 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
| 88 | ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) |
| 89 | ctx.RegisterModuleType("droiddoc", DroiddocFactory) |
| 90 | ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory) |
| 91 | ctx.RegisterModuleType("droiddoc_template", ExportedDroiddocDirFactory) |
| 92 | ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory) |
| 93 | ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory) |
| 94 | ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory) |
| 95 | ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory) |
| 96 | ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory) |
| 97 | ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory) |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 98 | ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) |
| 99 | ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 100 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 101 | ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) { |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 102 | ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel() |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 103 | }) |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 104 | ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 105 | ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory)) |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 106 | ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory)) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 107 | |
| 108 | // Register module types and mutators from cc needed for JNI testing |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 109 | ctx.RegisterModuleType("cc_library", cc.LibraryFactory) |
| 110 | ctx.RegisterModuleType("cc_object", cc.ObjectFactory) |
| 111 | ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory) |
| 112 | ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory) |
| 113 | ctx.RegisterModuleType("ndk_prebuilt_shared_stl", cc.NdkPrebuiltSharedStlFactory) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 114 | ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 115 | ctx.BottomUp("link", cc.LinkageMutator).Parallel() |
| 116 | ctx.BottomUp("begin", cc.BeginMutator).Parallel() |
| 117 | }) |
| 118 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 119 | return ctx |
| 120 | } |
| 121 | |
| 122 | func run(t *testing.T, ctx *android.TestContext, config android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 123 | t.Helper() |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 124 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 125 | pathCtx := android.PathContextForTesting(config) |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 126 | setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
| 127 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 128 | ctx.Register(config) |
Paul Duffin | baccf7e | 2019-06-11 11:58:30 +0100 | [diff] [blame] | 129 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 130 | android.FailIfErrored(t, errs) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 131 | _, errs = ctx.PrepareBuildActions(config) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 132 | android.FailIfErrored(t, errs) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 135 | 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] | 136 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 137 | return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil)) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 138 | } |
| 139 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 140 | 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] | 141 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 142 | ctx := testContext() |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 143 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 144 | pathCtx := android.PathContextForTesting(config) |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 145 | setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
| 146 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 147 | ctx.Register(config) |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 148 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
| 149 | if len(errs) > 0 { |
| 150 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 151 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 152 | } |
| 153 | _, errs = ctx.PrepareBuildActions(config) |
| 154 | if len(errs) > 0 { |
| 155 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 156 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 160 | |
| 161 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 162 | } |
| 163 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 164 | func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 165 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 166 | return testJavaWithConfig(t, testConfig(nil, bp, nil)) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 167 | } |
| 168 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 169 | func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 170 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 171 | ctx := testContext() |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 172 | run(t, ctx, config) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 173 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 174 | return ctx, config |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 177 | func moduleToPath(name string) string { |
| 178 | switch { |
| 179 | case name == `""`: |
| 180 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 181 | case strings.HasSuffix(name, ".jar"): |
| 182 | return name |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 183 | default: |
| 184 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 188 | func TestJavaLinkType(t *testing.T) { |
| 189 | testJava(t, ` |
| 190 | java_library { |
| 191 | name: "foo", |
| 192 | srcs: ["a.java"], |
| 193 | libs: ["bar"], |
| 194 | static_libs: ["baz"], |
| 195 | } |
| 196 | |
| 197 | java_library { |
| 198 | name: "bar", |
| 199 | sdk_version: "current", |
| 200 | srcs: ["b.java"], |
| 201 | } |
| 202 | |
| 203 | java_library { |
| 204 | name: "baz", |
| 205 | sdk_version: "system_current", |
| 206 | srcs: ["c.java"], |
| 207 | } |
| 208 | `) |
| 209 | |
| 210 | 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.", ` |
| 211 | java_library { |
| 212 | name: "foo", |
| 213 | srcs: ["a.java"], |
| 214 | libs: ["bar"], |
| 215 | sdk_version: "current", |
| 216 | static_libs: ["baz"], |
| 217 | } |
| 218 | |
| 219 | java_library { |
| 220 | name: "bar", |
| 221 | sdk_version: "current", |
| 222 | srcs: ["b.java"], |
| 223 | } |
| 224 | |
| 225 | java_library { |
| 226 | name: "baz", |
| 227 | sdk_version: "system_current", |
| 228 | srcs: ["c.java"], |
| 229 | } |
| 230 | `) |
| 231 | |
| 232 | testJava(t, ` |
| 233 | java_library { |
| 234 | name: "foo", |
| 235 | srcs: ["a.java"], |
| 236 | libs: ["bar"], |
| 237 | sdk_version: "system_current", |
| 238 | static_libs: ["baz"], |
| 239 | } |
| 240 | |
| 241 | java_library { |
| 242 | name: "bar", |
| 243 | sdk_version: "current", |
| 244 | srcs: ["b.java"], |
| 245 | } |
| 246 | |
| 247 | java_library { |
| 248 | name: "baz", |
| 249 | sdk_version: "system_current", |
| 250 | srcs: ["c.java"], |
| 251 | } |
| 252 | `) |
| 253 | |
| 254 | 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.", ` |
| 255 | java_library { |
| 256 | name: "foo", |
| 257 | srcs: ["a.java"], |
| 258 | libs: ["bar"], |
| 259 | sdk_version: "system_current", |
| 260 | static_libs: ["baz"], |
| 261 | } |
| 262 | |
| 263 | java_library { |
| 264 | name: "bar", |
| 265 | sdk_version: "current", |
| 266 | srcs: ["b.java"], |
| 267 | } |
| 268 | |
| 269 | java_library { |
| 270 | name: "baz", |
| 271 | srcs: ["c.java"], |
| 272 | } |
| 273 | `) |
| 274 | } |
| 275 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 276 | func TestSimple(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 277 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 278 | java_library { |
| 279 | name: "foo", |
| 280 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 281 | libs: ["bar"], |
| 282 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | java_library { |
| 286 | name: "bar", |
| 287 | srcs: ["b.java"], |
| 288 | } |
| 289 | |
| 290 | java_library { |
| 291 | name: "baz", |
| 292 | srcs: ["c.java"], |
| 293 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 294 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 295 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 296 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 297 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 298 | |
| 299 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 300 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 301 | } |
| 302 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 303 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 304 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 305 | bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 306 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 307 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 308 | 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] | 309 | } |
| 310 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 311 | if !strings.Contains(javac.Args["classpath"], bazTurbine) { |
| 312 | 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] | 313 | } |
| 314 | |
| 315 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 316 | 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] | 317 | } |
| 318 | } |
| 319 | |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 320 | func TestExportedPlugins(t *testing.T) { |
| 321 | type Result struct { |
| 322 | library string |
| 323 | processors string |
| 324 | } |
| 325 | var tests = []struct { |
| 326 | name string |
| 327 | extra string |
| 328 | results []Result |
| 329 | }{ |
| 330 | { |
| 331 | name: "Exported plugin is not a direct plugin", |
| 332 | extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`, |
| 333 | results: []Result{{library: "exports", processors: "-proc:none"}}, |
| 334 | }, |
| 335 | { |
| 336 | name: "Exports plugin to dependee", |
| 337 | extra: ` |
| 338 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 339 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 340 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 341 | `, |
| 342 | results: []Result{ |
| 343 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 344 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 345 | }, |
| 346 | }, |
| 347 | { |
| 348 | name: "Exports plugin to android_library", |
| 349 | extra: ` |
| 350 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 351 | android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 352 | android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 353 | `, |
| 354 | results: []Result{ |
| 355 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 356 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 357 | }, |
| 358 | }, |
| 359 | { |
| 360 | name: "Exports plugin is not propagated via transitive deps", |
| 361 | extra: ` |
| 362 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 363 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 364 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]} |
| 365 | `, |
| 366 | results: []Result{ |
| 367 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 368 | {library: "bar", processors: "-proc:none"}, |
| 369 | }, |
| 370 | }, |
| 371 | { |
| 372 | name: "Exports plugin appends to plugins", |
| 373 | extra: ` |
| 374 | java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"} |
| 375 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 376 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]} |
| 377 | `, |
| 378 | results: []Result{ |
| 379 | {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"}, |
| 380 | }, |
| 381 | }, |
| 382 | } |
| 383 | |
| 384 | for _, test := range tests { |
| 385 | t.Run(test.name, func(t *testing.T) { |
| 386 | ctx, _ := testJava(t, ` |
| 387 | java_plugin { |
| 388 | name: "plugin", |
| 389 | processor_class: "com.android.TestPlugin", |
| 390 | } |
| 391 | `+test.extra) |
| 392 | |
| 393 | for _, want := range test.results { |
| 394 | javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac") |
| 395 | if javac.Args["processor"] != want.processors { |
| 396 | t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"]) |
| 397 | } |
| 398 | } |
| 399 | }) |
| 400 | } |
| 401 | } |
| 402 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 403 | func TestSdkVersionByPartition(t *testing.T) { |
| 404 | 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] | 405 | java_library { |
| 406 | name: "foo", |
| 407 | srcs: ["a.java"], |
| 408 | vendor: true, |
| 409 | } |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 410 | `) |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 411 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 412 | testJava(t, ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 413 | java_library { |
| 414 | name: "bar", |
| 415 | srcs: ["b.java"], |
| 416 | } |
| 417 | `) |
| 418 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 419 | for _, enforce := range []bool{true, false} { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 420 | bp := ` |
| 421 | java_library { |
| 422 | name: "foo", |
| 423 | srcs: ["a.java"], |
| 424 | product_specific: true, |
| 425 | } |
| 426 | ` |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 427 | |
| 428 | config := testConfig(nil, bp, nil) |
| 429 | config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 430 | if enforce { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 431 | 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] | 432 | } else { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 433 | testJavaWithConfig(t, config) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 434 | } |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 438 | func TestArchSpecific(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 439 | ctx, _ := testJava(t, ` |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 440 | java_library { |
| 441 | name: "foo", |
| 442 | srcs: ["a.java"], |
| 443 | target: { |
| 444 | android: { |
| 445 | srcs: ["b.java"], |
| 446 | }, |
| 447 | }, |
| 448 | } |
| 449 | `) |
| 450 | |
| 451 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 452 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 453 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 454 | } |
| 455 | } |
| 456 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 457 | func TestBinary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 458 | ctx, _ := testJava(t, ` |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 459 | java_library_host { |
| 460 | name: "foo", |
| 461 | srcs: ["a.java"], |
| 462 | } |
| 463 | |
| 464 | java_binary_host { |
| 465 | name: "bar", |
| 466 | srcs: ["b.java"], |
| 467 | static_libs: ["foo"], |
| 468 | } |
| 469 | `) |
| 470 | |
| 471 | buildOS := android.BuildOs.String() |
| 472 | |
| 473 | bar := ctx.ModuleForTests("bar", buildOS+"_common") |
| 474 | barJar := bar.Output("bar.jar").Output.String() |
| 475 | barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64") |
| 476 | barWrapperDeps := barWrapper.Output("bar").Implicits.Strings() |
| 477 | |
| 478 | // Test that the install binary wrapper depends on the installed jar file |
| 479 | if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar { |
| 480 | t.Errorf("expected binary wrapper implicits [%q], got %v", |
| 481 | barJar, barWrapperDeps) |
| 482 | } |
| 483 | |
| 484 | } |
| 485 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 486 | func TestPrebuilts(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 487 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 488 | java_library { |
| 489 | name: "foo", |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 490 | srcs: ["a.java", ":stubs-source"], |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 491 | libs: ["bar", "sdklib"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 492 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 495 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 496 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 497 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 500 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 501 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 502 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 503 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 504 | |
| 505 | dex_import { |
| 506 | name: "qux", |
| 507 | jars: ["b.jar"], |
| 508 | } |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 509 | |
| 510 | java_sdk_library_import { |
| 511 | name: "sdklib", |
| 512 | jars: ["b.jar"], |
| 513 | } |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 514 | |
| 515 | prebuilt_stubs_sources { |
| 516 | name: "stubs-source", |
| 517 | srcs: ["stubs/sources/**/*.java"], |
| 518 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 519 | `) |
| 520 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 521 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 522 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 523 | barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output |
| 524 | bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 525 | sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 526 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 527 | inputs := []string{} |
| 528 | for _, p := range javac.BuildParams.Inputs { |
| 529 | inputs = append(inputs, p.String()) |
| 530 | } |
| 531 | |
| 532 | expected := []string{ |
| 533 | "a.java", |
| 534 | "stubs/sources/foo/Foo.java", |
| 535 | } |
| 536 | if !reflect.DeepEqual(expected, inputs) { |
| 537 | t.Errorf("foo inputs incorrect: expected %q, found %q", expected, inputs) |
| 538 | } |
| 539 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 540 | if !strings.Contains(javac.Args["classpath"], barJar.String()) { |
| 541 | 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] | 542 | } |
| 543 | |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 544 | if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) { |
| 545 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String()) |
| 546 | } |
| 547 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 548 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() { |
| 549 | 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] | 550 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 551 | |
| 552 | ctx.ModuleForTests("qux", "android_common").Rule("Cp") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 555 | func TestDefaults(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 556 | ctx, _ := testJava(t, ` |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 557 | java_defaults { |
| 558 | name: "defaults", |
| 559 | srcs: ["a.java"], |
| 560 | libs: ["bar"], |
| 561 | static_libs: ["baz"], |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 562 | optimize: {enabled: false}, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | java_library { |
| 566 | name: "foo", |
| 567 | defaults: ["defaults"], |
| 568 | } |
| 569 | |
| 570 | java_library { |
| 571 | name: "bar", |
| 572 | srcs: ["b.java"], |
| 573 | } |
| 574 | |
| 575 | java_library { |
| 576 | name: "baz", |
| 577 | srcs: ["c.java"], |
| 578 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 579 | |
| 580 | android_test { |
| 581 | name: "atestOptimize", |
| 582 | defaults: ["defaults"], |
| 583 | optimize: {enabled: true}, |
| 584 | } |
| 585 | |
| 586 | android_test { |
| 587 | name: "atestNoOptimize", |
| 588 | defaults: ["defaults"], |
| 589 | } |
| 590 | |
| 591 | android_test { |
| 592 | name: "atestDefault", |
| 593 | srcs: ["a.java"], |
| 594 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 595 | `) |
| 596 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 597 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 598 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 599 | |
| 600 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 601 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 602 | } |
| 603 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 604 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 605 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 606 | 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] | 607 | } |
| 608 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 609 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 610 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 611 | 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] | 612 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 613 | |
| 614 | atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8") |
| 615 | if atestOptimize.Output == nil { |
| 616 | t.Errorf("atestOptimize should optimize APK") |
| 617 | } |
| 618 | |
| 619 | atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8") |
| 620 | if atestNoOptimize.Output == nil { |
| 621 | t.Errorf("atestNoOptimize should not optimize APK") |
| 622 | } |
| 623 | |
| 624 | atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8") |
| 625 | if atestDefault.Output == nil { |
| 626 | t.Errorf("atestDefault should optimize APK") |
| 627 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 630 | func TestResources(t *testing.T) { |
| 631 | var table = []struct { |
| 632 | name string |
| 633 | prop string |
| 634 | extra string |
| 635 | args string |
| 636 | }{ |
| 637 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 638 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 639 | name: "resource dirs", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 640 | prop: `java_resource_dirs: ["java-res"]`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 641 | 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] | 642 | }, |
| 643 | { |
| 644 | // Test that a module with java_resources includes the files |
| 645 | name: "resource files", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 646 | prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`, |
| 647 | args: "-C . -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 648 | }, |
| 649 | { |
| 650 | // Test that a module with a filegroup in java_resources includes the files with the |
| 651 | // path prefix |
| 652 | name: "resource filegroup", |
| 653 | prop: `java_resources: [":foo-res"]`, |
| 654 | extra: ` |
| 655 | filegroup { |
| 656 | name: "foo-res", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 657 | path: "java-res", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 658 | srcs: ["java-res/a/a", "java-res/b/b"], |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 659 | }`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 660 | 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] | 661 | }, |
| 662 | { |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 663 | // Test that a module with wildcards in java_resource_dirs has the correct path prefixes |
| 664 | name: "wildcard dirs", |
| 665 | prop: `java_resource_dirs: ["java-res/*"]`, |
| 666 | args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b", |
| 667 | }, |
| 668 | { |
| 669 | // Test that a module exclude_java_resource_dirs excludes the files |
| 670 | name: "wildcard dirs", |
| 671 | prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`, |
| 672 | args: "-C java-res/a -f java-res/a/a", |
| 673 | }, |
Colin Cross | cedd476 | 2018-09-13 11:26:19 -0700 | [diff] [blame] | 674 | { |
| 675 | // Test wildcards in java_resources |
| 676 | name: "wildcard files", |
| 677 | prop: `java_resources: ["java-res/**/*"]`, |
| 678 | args: "-C . -f java-res/a/a -f java-res/b/b", |
| 679 | }, |
| 680 | { |
| 681 | // Test exclude_java_resources with java_resources |
| 682 | name: "wildcard files with exclude", |
| 683 | prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`, |
| 684 | args: "-C . -f java-res/a/a", |
| 685 | }, |
| 686 | { |
| 687 | // Test exclude_java_resources with java_resource_dirs |
| 688 | name: "resource dirs with exclude files", |
| 689 | prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`, |
| 690 | args: "-C java-res -f java-res/a/a", |
| 691 | }, |
| 692 | { |
| 693 | // Test exclude_java_resource_dirs with java_resource_dirs |
| 694 | name: "resource dirs with exclude files", |
| 695 | prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`, |
| 696 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
| 697 | }, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | for _, test := range table { |
| 701 | t.Run(test.name, func(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 702 | ctx, _ := testJava(t, ` |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 703 | java_library { |
| 704 | name: "foo", |
| 705 | srcs: [ |
| 706 | "a.java", |
| 707 | "b.java", |
| 708 | "c.java", |
| 709 | ], |
| 710 | `+test.prop+`, |
| 711 | } |
| 712 | `+test.extra) |
| 713 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 714 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 715 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 716 | |
| 717 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 718 | t.Errorf("foo combined jars %v does not contain %q", |
| 719 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 720 | } |
| 721 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 722 | if fooRes.Args["jarArgs"] != test.args { |
| 723 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 724 | fooRes.Args["jarArgs"], test.args) |
| 725 | } |
| 726 | }) |
| 727 | } |
| 728 | } |
| 729 | |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 730 | func TestIncludeSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 731 | ctx, _ := testJava(t, ` |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 732 | java_library { |
| 733 | name: "foo", |
| 734 | srcs: [ |
| 735 | "a.java", |
| 736 | "b.java", |
| 737 | "c.java", |
| 738 | ], |
| 739 | include_srcs: true, |
| 740 | } |
| 741 | |
| 742 | java_library { |
| 743 | name: "bar", |
| 744 | srcs: [ |
| 745 | "a.java", |
| 746 | "b.java", |
| 747 | "c.java", |
| 748 | ], |
| 749 | java_resource_dirs: ["java-res"], |
| 750 | include_srcs: true, |
| 751 | } |
| 752 | `) |
| 753 | |
| 754 | // Test a library with include_srcs: true |
| 755 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
| 756 | fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar") |
| 757 | |
| 758 | if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) { |
| 759 | t.Errorf("foo combined jars %v does not contain %q", w, g) |
| 760 | } |
| 761 | |
| 762 | if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 763 | t.Errorf("foo source jar args %q is not %q", w, g) |
| 764 | } |
| 765 | |
| 766 | // Test a library with include_srcs: true and resources |
| 767 | bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar") |
| 768 | barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar") |
| 769 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") |
| 770 | barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar") |
| 771 | |
| 772 | if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 773 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 774 | } |
| 775 | |
| 776 | if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 777 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 778 | } |
| 779 | |
| 780 | if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) { |
| 781 | t.Errorf("bar combined jars %v does not contain %q", w, g) |
| 782 | } |
| 783 | |
| 784 | if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 785 | t.Errorf("bar source jar args %q is not %q", w, g) |
| 786 | } |
| 787 | |
| 788 | if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w { |
| 789 | t.Errorf("bar resource jar args %q is not %q", w, g) |
| 790 | } |
| 791 | } |
| 792 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 793 | func TestGeneratedSources(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 794 | ctx, _ := testJava(t, ` |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 795 | java_library { |
| 796 | name: "foo", |
| 797 | srcs: [ |
| 798 | "a*.java", |
| 799 | ":gen", |
| 800 | "b*.java", |
| 801 | ], |
| 802 | } |
| 803 | |
| 804 | genrule { |
| 805 | name: "gen", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 806 | tool_files: ["java-res/a"], |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 807 | out: ["gen.java"], |
| 808 | } |
| 809 | `) |
| 810 | |
| 811 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 812 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 813 | |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 814 | if filepath.Base(genrule.Output.String()) != "gen.java" { |
| 815 | 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] | 816 | } |
| 817 | |
| 818 | if len(javac.Inputs) != 3 || |
| 819 | javac.Inputs[0].String() != "a.java" || |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 820 | javac.Inputs[1].String() != genrule.Output.String() || |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 821 | javac.Inputs[2].String() != "b.java" { |
| 822 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 823 | } |
| 824 | } |
| 825 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 826 | func TestTurbine(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 827 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 828 | java_library { |
| 829 | name: "foo", |
| 830 | srcs: ["a.java"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 831 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | java_library { |
| 835 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 836 | srcs: ["b.java"], |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 837 | static_libs: ["foo"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 838 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | java_library { |
| 842 | name: "baz", |
| 843 | srcs: ["c.java"], |
| 844 | libs: ["bar"], |
| 845 | sdk_version: "14", |
| 846 | } |
| 847 | `) |
| 848 | |
| 849 | fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine") |
| 850 | barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine") |
| 851 | barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac") |
| 852 | barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine") |
| 853 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 854 | |
| 855 | if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" { |
| 856 | t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs) |
| 857 | } |
| 858 | |
| 859 | fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 860 | if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) { |
| 861 | t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar) |
| 862 | } |
| 863 | if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) { |
| 864 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar) |
| 865 | } |
| 866 | if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar { |
| 867 | t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar) |
| 868 | } |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 869 | if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") { |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 870 | 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] | 871 | "prebuilts/sdk/14/public/android.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
| 875 | func TestSharding(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 876 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 877 | java_library { |
| 878 | name: "bar", |
| 879 | srcs: ["a.java","b.java","c.java"], |
| 880 | javac_shard_size: 1 |
| 881 | } |
| 882 | `) |
| 883 | |
| 884 | barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 885 | for i := 0; i < 3; i++ { |
| 886 | barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)) |
| 887 | if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) { |
| 888 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar) |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 893 | func TestDroiddoc(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 894 | ctx, _ := testJava(t, ` |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 895 | droiddoc_template { |
| 896 | name: "droiddoc-templates-sdk", |
| 897 | path: ".", |
| 898 | } |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 899 | filegroup { |
| 900 | name: "bar-doc-aidl-srcs", |
| 901 | srcs: ["bar-doc/IBar.aidl"], |
| 902 | path: "bar-doc", |
| 903 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 904 | droiddoc { |
| 905 | name: "bar-doc", |
| 906 | srcs: [ |
| 907 | "bar-doc/*.java", |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 908 | "bar-doc/IFoo.aidl", |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 909 | ":bar-doc-aidl-srcs", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 910 | ], |
| 911 | exclude_srcs: [ |
| 912 | "bar-doc/b.java" |
| 913 | ], |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 914 | custom_template: "droiddoc-templates-sdk", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 915 | hdf: [ |
| 916 | "android.whichdoc offline", |
| 917 | ], |
| 918 | knowntags: [ |
| 919 | "bar-doc/known_oj_tags.txt", |
| 920 | ], |
| 921 | proofread_file: "libcore-proofread.txt", |
| 922 | todo_file: "libcore-docs-todo.html", |
| 923 | args: "-offlinemode -title \"libcore\"", |
| 924 | } |
| 925 | `) |
| 926 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 927 | barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc") |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 928 | var javaSrcs []string |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 929 | for _, i := range barDoc.Inputs { |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 930 | javaSrcs = append(javaSrcs, i.Base()) |
| 931 | } |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 932 | if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" { |
| 933 | 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] | 934 | } |
| 935 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 936 | aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl") |
| 937 | if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) { |
| 938 | t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g) |
| 939 | } |
| 940 | |
| 941 | if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) { |
| 942 | t.Errorf("aidl inputs must be %q, but was %q", w, g) |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 943 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 944 | } |
| 945 | |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 946 | func TestJarGenrules(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 947 | ctx, _ := testJava(t, ` |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 948 | java_library { |
| 949 | name: "foo", |
| 950 | srcs: ["a.java"], |
| 951 | } |
| 952 | |
| 953 | java_genrule { |
| 954 | name: "jargen", |
| 955 | tool_files: ["b.java"], |
| 956 | cmd: "$(location b.java) $(in) $(out)", |
| 957 | out: ["jargen.jar"], |
| 958 | srcs: [":foo"], |
| 959 | } |
| 960 | |
| 961 | java_library { |
| 962 | name: "bar", |
| 963 | static_libs: ["jargen"], |
| 964 | srcs: ["c.java"], |
| 965 | } |
| 966 | |
| 967 | java_library { |
| 968 | name: "baz", |
| 969 | libs: ["jargen"], |
| 970 | srcs: ["c.java"], |
| 971 | } |
| 972 | `) |
| 973 | |
| 974 | foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar") |
| 975 | jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar") |
| 976 | bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar") |
| 977 | baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar") |
| 978 | barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar") |
| 979 | |
| 980 | if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() { |
| 981 | t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings()) |
| 982 | } |
| 983 | |
| 984 | if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) { |
| 985 | t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String()) |
| 986 | } |
| 987 | |
| 988 | if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) { |
| 989 | t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String()) |
| 990 | } |
| 991 | |
| 992 | if len(barCombined.Inputs) != 2 || |
| 993 | barCombined.Inputs[0].String() != bar.Output.String() || |
| 994 | barCombined.Inputs[1].String() != jargen.Output.String() { |
| 995 | t.Errorf("bar combined jar inputs %v is not [%q, %q]", |
| 996 | barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String()) |
| 997 | } |
| 998 | } |
| 999 | |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1000 | func TestExcludeFileGroupInSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1001 | ctx, _ := testJava(t, ` |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1002 | java_library { |
| 1003 | name: "foo", |
| 1004 | srcs: ["a.java", ":foo-srcs"], |
| 1005 | exclude_srcs: ["a.java", ":foo-excludes"], |
| 1006 | } |
| 1007 | |
| 1008 | filegroup { |
| 1009 | name: "foo-srcs", |
| 1010 | srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"], |
| 1011 | } |
| 1012 | |
| 1013 | filegroup { |
| 1014 | name: "foo-excludes", |
| 1015 | srcs: ["java-fg/a.java", "java-fg/b.java"], |
| 1016 | } |
| 1017 | `) |
| 1018 | |
| 1019 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 1020 | |
| 1021 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" { |
| 1022 | t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs) |
| 1023 | } |
| 1024 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1025 | |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1026 | func TestJavaLibrary(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 1027 | config := testConfig(nil, "", map[string][]byte{ |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1028 | "libcore/Android.bp": []byte(` |
| 1029 | java_library { |
| 1030 | name: "core", |
| 1031 | sdk_version: "none", |
| 1032 | system_modules: "none", |
| 1033 | }`), |
| 1034 | }) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame^] | 1035 | ctx := testContext() |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1036 | run(t, ctx, config) |
| 1037 | } |
| 1038 | |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1039 | func TestJavaSdkLibrary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1040 | ctx, _ := testJava(t, ` |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1041 | droiddoc_template { |
| 1042 | name: "droiddoc-templates-sdk", |
| 1043 | path: ".", |
| 1044 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1045 | java_sdk_library { |
| 1046 | name: "foo", |
| 1047 | srcs: ["a.java", "b.java"], |
| 1048 | api_packages: ["foo"], |
| 1049 | } |
| 1050 | java_sdk_library { |
| 1051 | name: "bar", |
| 1052 | srcs: ["a.java", "b.java"], |
| 1053 | api_packages: ["bar"], |
| 1054 | } |
| 1055 | java_library { |
| 1056 | name: "baz", |
| 1057 | srcs: ["c.java"], |
| 1058 | libs: ["foo", "bar"], |
| 1059 | sdk_version: "system_current", |
| 1060 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1061 | java_library { |
| 1062 | name: "qux", |
| 1063 | srcs: ["c.java"], |
| 1064 | libs: ["baz"], |
| 1065 | sdk_version: "system_current", |
| 1066 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1067 | `) |
| 1068 | |
| 1069 | // check the existence of the internal modules |
| 1070 | ctx.ModuleForTests("foo", "android_common") |
| 1071 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common") |
| 1072 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1073 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1074 | ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common") |
| 1075 | ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1076 | ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common") |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 1077 | ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1078 | ctx.ModuleForTests("foo.api.public.28", "") |
| 1079 | ctx.ModuleForTests("foo.api.system.28", "") |
| 1080 | ctx.ModuleForTests("foo.api.test.28", "") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1081 | |
| 1082 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 1083 | // tests if baz is actually linked to the stubs lib |
| 1084 | if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") { |
| 1085 | t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"], |
| 1086 | "foo.stubs.system.jar") |
| 1087 | } |
| 1088 | // ... and not to the impl lib |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 1089 | if strings.Contains(bazJavac.Args["classpath"], "foo.jar") { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1090 | 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] | 1091 | "foo.jar") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1092 | } |
| 1093 | // test if baz is not linked to the system variant of foo |
| 1094 | if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") { |
| 1095 | t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"], |
| 1096 | "foo.stubs.jar") |
| 1097 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1098 | |
| 1099 | // test if baz has exported SDK lib names foo and bar to qux |
| 1100 | qux := ctx.ModuleForTests("qux", "android_common") |
| 1101 | if quxLib, ok := qux.Module().(*Library); ok { |
| 1102 | sdkLibs := quxLib.ExportedSdkLibs() |
| 1103 | if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) { |
| 1104 | t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs) |
| 1105 | } |
| 1106 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1107 | } |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 1108 | |
| 1109 | var compilerFlagsTestCases = []struct { |
| 1110 | in string |
| 1111 | out bool |
| 1112 | }{ |
| 1113 | { |
| 1114 | in: "a", |
| 1115 | out: false, |
| 1116 | }, |
| 1117 | { |
| 1118 | in: "-a", |
| 1119 | out: true, |
| 1120 | }, |
| 1121 | { |
| 1122 | in: "-no-jdk", |
| 1123 | out: false, |
| 1124 | }, |
| 1125 | { |
| 1126 | in: "-no-stdlib", |
| 1127 | out: false, |
| 1128 | }, |
| 1129 | { |
| 1130 | in: "-kotlin-home", |
| 1131 | out: false, |
| 1132 | }, |
| 1133 | { |
| 1134 | in: "-kotlin-home /some/path", |
| 1135 | out: false, |
| 1136 | }, |
| 1137 | { |
| 1138 | in: "-include-runtime", |
| 1139 | out: false, |
| 1140 | }, |
| 1141 | { |
| 1142 | in: "-Xintellij-plugin-root", |
| 1143 | out: false, |
| 1144 | }, |
| 1145 | } |
| 1146 | |
| 1147 | type mockContext struct { |
| 1148 | android.ModuleContext |
| 1149 | result bool |
| 1150 | } |
| 1151 | |
| 1152 | func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) { |
| 1153 | // CheckBadCompilerFlags calls this function when the flag should be rejected |
| 1154 | ctx.result = false |
| 1155 | } |
| 1156 | |
| 1157 | func TestCompilerFlags(t *testing.T) { |
| 1158 | for _, testCase := range compilerFlagsTestCases { |
| 1159 | ctx := &mockContext{result: true} |
| 1160 | CheckKotlincFlags(ctx, []string{testCase.in}) |
| 1161 | if ctx.result != testCase.out { |
| 1162 | t.Errorf("incorrect output:") |
| 1163 | t.Errorf(" input: %#v", testCase.in) |
| 1164 | t.Errorf(" expected: %#v", testCase.out) |
| 1165 | t.Errorf(" got: %#v", ctx.result) |
| 1166 | } |
| 1167 | } |
| 1168 | } |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1169 | |
| 1170 | // TODO(jungjw): Consider making this more robust by ignoring path order. |
| 1171 | func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) { |
| 1172 | variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests() |
| 1173 | flags := strings.Split(variables["javacFlags"], " ") |
| 1174 | got := "" |
| 1175 | for _, flag := range flags { |
| 1176 | keyEnd := strings.Index(flag, "=") |
| 1177 | if keyEnd > -1 && flag[:keyEnd] == "--patch-module" { |
| 1178 | got = flag[keyEnd+1:] |
| 1179 | break |
| 1180 | } |
| 1181 | } |
| 1182 | if expected != got { |
| 1183 | t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got) |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | func TestPatchModule(t *testing.T) { |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1188 | t.Run("Java language level 8", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1189 | // Test with legacy javac -source 1.8 -target 1.8 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1190 | bp := ` |
| 1191 | java_library { |
| 1192 | name: "foo", |
| 1193 | srcs: ["a.java"], |
| 1194 | java_version: "1.8", |
| 1195 | } |
| 1196 | |
| 1197 | java_library { |
| 1198 | name: "bar", |
| 1199 | srcs: ["b.java"], |
| 1200 | sdk_version: "none", |
| 1201 | system_modules: "none", |
| 1202 | patch_module: "java.base", |
| 1203 | java_version: "1.8", |
| 1204 | } |
| 1205 | |
| 1206 | java_library { |
| 1207 | name: "baz", |
| 1208 | srcs: ["c.java"], |
| 1209 | patch_module: "java.base", |
| 1210 | java_version: "1.8", |
| 1211 | } |
| 1212 | ` |
| 1213 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1214 | |
| 1215 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1216 | checkPatchModuleFlag(t, ctx, "bar", "") |
| 1217 | checkPatchModuleFlag(t, ctx, "baz", "") |
| 1218 | }) |
| 1219 | |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1220 | t.Run("Java language level 9", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1221 | // Test with default javac -source 9 -target 9 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1222 | bp := ` |
| 1223 | java_library { |
| 1224 | name: "foo", |
| 1225 | srcs: ["a.java"], |
| 1226 | } |
| 1227 | |
| 1228 | java_library { |
| 1229 | name: "bar", |
| 1230 | srcs: ["b.java"], |
| 1231 | sdk_version: "none", |
| 1232 | system_modules: "none", |
| 1233 | patch_module: "java.base", |
| 1234 | } |
| 1235 | |
| 1236 | java_library { |
| 1237 | name: "baz", |
| 1238 | srcs: ["c.java"], |
| 1239 | patch_module: "java.base", |
| 1240 | } |
| 1241 | ` |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1242 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1243 | |
| 1244 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1245 | expected := "java.base=.:" + buildDir |
| 1246 | checkPatchModuleFlag(t, ctx, "bar", expected) |
Insun Kang | 2343386 | 2019-01-24 20:46:28 +0900 | [diff] [blame] | 1247 | expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework"), moduleToPath("updatable_media_stubs")}, ":") |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1248 | checkPatchModuleFlag(t, ctx, "baz", expected) |
| 1249 | }) |
| 1250 | } |