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