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