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 ( |
| 18 | "android/soong/android" |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 19 | "android/soong/genrule" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 20 | "fmt" |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
Colin Cross | 227d436 | 2017-08-30 14:14:52 -0700 | [diff] [blame] | 24 | "reflect" |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 25 | "strconv" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 26 | "strings" |
| 27 | "testing" |
| 28 | ) |
| 29 | |
| 30 | var buildDir string |
| 31 | |
| 32 | func setUp() { |
| 33 | var err error |
| 34 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 35 | if err != nil { |
| 36 | panic(err) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func tearDown() { |
| 41 | os.RemoveAll(buildDir) |
| 42 | } |
| 43 | |
| 44 | func TestMain(m *testing.M) { |
| 45 | run := func() int { |
| 46 | setUp() |
| 47 | defer tearDown() |
| 48 | |
| 49 | return m.Run() |
| 50 | } |
| 51 | |
| 52 | os.Exit(run()) |
| 53 | } |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 54 | |
| 55 | func testConfig(env map[string]string) android.Config { |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 56 | if env == nil { |
| 57 | env = make(map[string]string) |
| 58 | } |
| 59 | if env["ANDROID_JAVA8_HOME"] == "" { |
| 60 | env["ANDROID_JAVA8_HOME"] = "jdk8" |
| 61 | } |
Jiyong Park | 1a5d7b1 | 2018-01-15 15:05:10 +0900 | [diff] [blame] | 62 | config := android.TestArchConfig(buildDir, env) |
| 63 | config.ProductVariables.DeviceSystemSdkVersions = &[]string{"14", "15"} |
| 64 | return config |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 65 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 68 | func testContext(config android.Config, bp string, |
| 69 | fs map[string][]byte) *android.TestContext { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 70 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 71 | ctx := android.NewTestArchContext() |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 72 | ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory)) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 73 | ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory)) |
Colin Cross | a60ead8 | 2017-10-02 18:10:21 -0700 | [diff] [blame] | 74 | ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true))) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 75 | ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory)) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 76 | ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory)) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 77 | ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory)) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 78 | ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory)) |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 79 | ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory)) |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 80 | ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory)) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 81 | ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory)) |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 82 | ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) |
| 83 | ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 84 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 85 | ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory)) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 86 | ctx.Register() |
| 87 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 88 | extraModules := []string{ |
| 89 | "core-oj", |
| 90 | "core-libart", |
| 91 | "framework", |
| 92 | "ext", |
| 93 | "okhttp", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 94 | "android_stubs_current", |
| 95 | "android_system_stubs_current", |
| 96 | "android_test_stubs_current", |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 97 | "kotlin-stdlib", |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 98 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 99 | |
| 100 | for _, extra := range extraModules { |
| 101 | bp += fmt.Sprintf(` |
| 102 | java_library { |
| 103 | name: "%s", |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 104 | srcs: ["a.java"], |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 105 | no_standard_libs: true, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 106 | system_modules: "core-system-modules", |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 107 | } |
| 108 | `, extra) |
| 109 | } |
| 110 | |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 111 | bp += ` |
| 112 | android_app { |
| 113 | name: "framework-res", |
| 114 | no_framework_libs: true, |
| 115 | } |
| 116 | ` |
| 117 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 118 | systemModules := []string{ |
| 119 | "core-system-modules", |
| 120 | "android_stubs_current_system_modules", |
| 121 | "android_system_stubs_current_system_modules", |
| 122 | "android_test_stubs_current_system_modules", |
| 123 | } |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 124 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 125 | for _, extra := range systemModules { |
| 126 | bp += fmt.Sprintf(` |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 127 | java_system_modules { |
| 128 | name: "%s", |
| 129 | } |
| 130 | `, extra) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 133 | mockFS := map[string][]byte{ |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame^] | 134 | "Android.bp": []byte(bp), |
| 135 | "a.java": nil, |
| 136 | "b.java": nil, |
| 137 | "c.java": nil, |
| 138 | "b.kt": nil, |
| 139 | "a.jar": nil, |
| 140 | "b.jar": nil, |
| 141 | "java-res/a": nil, |
| 142 | "java-res/b": nil, |
| 143 | "java-res2/a": nil, |
| 144 | "java-fg/a.java": nil, |
| 145 | "java-fg/b.java": nil, |
| 146 | "java-fg/c.java": nil, |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 147 | |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 148 | "prebuilts/sdk/14/android.jar": nil, |
| 149 | "prebuilts/sdk/14/framework.aidl": nil, |
| 150 | "prebuilts/sdk/current/android.jar": nil, |
| 151 | "prebuilts/sdk/current/framework.aidl": nil, |
Jiyong Park | 750e557 | 2018-01-31 00:20:13 +0900 | [diff] [blame] | 152 | "prebuilts/sdk/current/core.jar": nil, |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 153 | "prebuilts/sdk/system_current/android.jar": nil, |
| 154 | "prebuilts/sdk/system_current/framework.aidl": nil, |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 155 | "prebuilts/sdk/system_14/android.jar": nil, |
| 156 | "prebuilts/sdk/system_14/framework.aidl": nil, |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 157 | "prebuilts/sdk/test_current/android.jar": nil, |
| 158 | "prebuilts/sdk/test_current/framework.aidl": nil, |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 159 | |
| 160 | // For framework-res, which is an implicit dependency for framework |
| 161 | "AndroidManifest.xml": nil, |
| 162 | "build/target/product/security/testkey": nil, |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 163 | |
| 164 | "build/soong/scripts/jar-wrapper.sh": nil, |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 165 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 166 | "build/make/core/proguard.flags": nil, |
| 167 | "build/make/core/proguard_basic_keeps.flags": nil, |
| 168 | |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 169 | "jdk8/jre/lib/jce.jar": nil, |
| 170 | "jdk8/jre/lib/rt.jar": nil, |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | for k, v := range fs { |
| 174 | mockFS[k] = v |
| 175 | } |
| 176 | |
| 177 | ctx.MockFileSystem(mockFS) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 178 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 179 | return ctx |
| 180 | } |
| 181 | |
| 182 | func run(t *testing.T, ctx *android.TestContext, config android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 183 | t.Helper() |
Jeff Gaston | d3e141d | 2017-08-08 17:46:01 -0700 | [diff] [blame] | 184 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 185 | fail(t, errs) |
| 186 | _, errs = ctx.PrepareBuildActions(config) |
| 187 | fail(t, errs) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | func testJava(t *testing.T, bp string) *android.TestContext { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 191 | t.Helper() |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 192 | config := testConfig(nil) |
| 193 | ctx := testContext(config, bp, nil) |
| 194 | run(t, ctx, config) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 195 | |
| 196 | return ctx |
| 197 | } |
| 198 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 199 | func moduleToPath(name string) string { |
| 200 | switch { |
| 201 | case name == `""`: |
| 202 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 203 | case strings.HasSuffix(name, ".jar"): |
| 204 | return name |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 205 | case name == "android_stubs_current" || name == "android_system_stubs_current" || |
| 206 | name == "android_test_stubs_current": |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 207 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 208 | default: |
| 209 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 213 | func TestSimple(t *testing.T) { |
| 214 | ctx := testJava(t, ` |
| 215 | java_library { |
| 216 | name: "foo", |
| 217 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 218 | libs: ["bar"], |
| 219 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | java_library { |
| 223 | name: "bar", |
| 224 | srcs: ["b.java"], |
| 225 | } |
| 226 | |
| 227 | java_library { |
| 228 | name: "baz", |
| 229 | srcs: ["c.java"], |
| 230 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 231 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 232 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 233 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 234 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 235 | |
| 236 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 237 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 238 | } |
| 239 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 240 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 241 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 242 | bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 243 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 244 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 245 | 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] | 246 | } |
| 247 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 248 | if !strings.Contains(javac.Args["classpath"], bazTurbine) { |
| 249 | 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] | 250 | } |
| 251 | |
| 252 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 253 | 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] | 254 | } |
| 255 | } |
| 256 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 257 | func TestArchSpecific(t *testing.T) { |
| 258 | ctx := testJava(t, ` |
| 259 | java_library { |
| 260 | name: "foo", |
| 261 | srcs: ["a.java"], |
| 262 | target: { |
| 263 | android: { |
| 264 | srcs: ["b.java"], |
| 265 | }, |
| 266 | }, |
| 267 | } |
| 268 | `) |
| 269 | |
| 270 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 271 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 272 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 273 | } |
| 274 | } |
| 275 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 276 | func TestBinary(t *testing.T) { |
| 277 | ctx := testJava(t, ` |
| 278 | java_library_host { |
| 279 | name: "foo", |
| 280 | srcs: ["a.java"], |
| 281 | } |
| 282 | |
| 283 | java_binary_host { |
| 284 | name: "bar", |
| 285 | srcs: ["b.java"], |
| 286 | static_libs: ["foo"], |
| 287 | } |
| 288 | `) |
| 289 | |
| 290 | buildOS := android.BuildOs.String() |
| 291 | |
| 292 | bar := ctx.ModuleForTests("bar", buildOS+"_common") |
| 293 | barJar := bar.Output("bar.jar").Output.String() |
| 294 | barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64") |
| 295 | barWrapperDeps := barWrapper.Output("bar").Implicits.Strings() |
| 296 | |
| 297 | // Test that the install binary wrapper depends on the installed jar file |
| 298 | if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar { |
| 299 | t.Errorf("expected binary wrapper implicits [%q], got %v", |
| 300 | barJar, barWrapperDeps) |
| 301 | } |
| 302 | |
| 303 | } |
| 304 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 305 | var classpathTestcases = []struct { |
| 306 | name string |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 307 | moduleType string |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 308 | host android.OsClass |
| 309 | properties string |
| 310 | bootclasspath []string |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 311 | system string |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 312 | classpath []string |
| 313 | }{ |
| 314 | { |
| 315 | name: "default", |
| 316 | bootclasspath: []string{"core-oj", "core-libart"}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 317 | system: "core-system-modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 318 | classpath: []string{"ext", "framework", "okhttp"}, |
| 319 | }, |
| 320 | { |
| 321 | name: "blank sdk version", |
| 322 | properties: `sdk_version: "",`, |
| 323 | bootclasspath: []string{"core-oj", "core-libart"}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 324 | system: "core-system-modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 325 | classpath: []string{"ext", "framework", "okhttp"}, |
| 326 | }, |
| 327 | { |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 328 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 329 | name: "sdk v14", |
| 330 | properties: `sdk_version: "14",`, |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 331 | bootclasspath: []string{`""`}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 332 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 333 | classpath: []string{"prebuilts/sdk/14/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 334 | }, |
| 335 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 336 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 337 | name: "current", |
| 338 | properties: `sdk_version: "current",`, |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 339 | bootclasspath: []string{`""`}, |
| 340 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 341 | classpath: []string{"prebuilts/sdk/current/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 342 | }, |
| 343 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 344 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 345 | name: "system_current", |
| 346 | properties: `sdk_version: "system_current",`, |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 347 | bootclasspath: []string{`""`}, |
| 348 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 349 | classpath: []string{"prebuilts/sdk/system_current/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 350 | }, |
| 351 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 352 | |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 353 | name: "system_14", |
| 354 | properties: `sdk_version: "system_14",`, |
| 355 | bootclasspath: []string{`""`}, |
| 356 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 357 | classpath: []string{"prebuilts/sdk/system_14/android.jar"}, |
| 358 | }, |
| 359 | { |
| 360 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 361 | name: "test_current", |
| 362 | properties: `sdk_version: "test_current",`, |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 363 | bootclasspath: []string{`""`}, |
| 364 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 365 | classpath: []string{"prebuilts/sdk/test_current/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 366 | }, |
| 367 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 368 | |
Jiyong Park | 750e557 | 2018-01-31 00:20:13 +0900 | [diff] [blame] | 369 | name: "core_current", |
| 370 | properties: `sdk_version: "core_current",`, |
| 371 | bootclasspath: []string{`""`}, |
| 372 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 373 | classpath: []string{"prebuilts/sdk/current/core.jar"}, |
| 374 | }, |
| 375 | { |
| 376 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 377 | name: "nostdlib", |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 378 | properties: `no_standard_libs: true, system_modules: "none"`, |
| 379 | system: "none", |
| 380 | bootclasspath: []string{`""`}, |
| 381 | classpath: []string{}, |
| 382 | }, |
| 383 | { |
| 384 | |
| 385 | name: "nostdlib system_modules", |
| 386 | properties: `no_standard_libs: true, system_modules: "core-system-modules"`, |
| 387 | system: "core-system-modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 388 | bootclasspath: []string{`""`}, |
| 389 | classpath: []string{}, |
| 390 | }, |
| 391 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 392 | |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 393 | name: "host default", |
| 394 | moduleType: "java_library_host", |
| 395 | properties: ``, |
| 396 | host: android.Host, |
| 397 | bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"}, |
| 398 | classpath: []string{}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 399 | }, |
| 400 | { |
| 401 | name: "host nostdlib", |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 402 | moduleType: "java_library_host", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 403 | host: android.Host, |
| 404 | properties: `no_standard_libs: true`, |
| 405 | classpath: []string{}, |
| 406 | }, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 407 | { |
| 408 | |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 409 | name: "host supported default", |
| 410 | host: android.Host, |
| 411 | properties: `host_supported: true,`, |
| 412 | classpath: []string{}, |
| 413 | bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"}, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 414 | }, |
| 415 | { |
| 416 | name: "host supported nostdlib", |
| 417 | host: android.Host, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 418 | properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 419 | classpath: []string{}, |
| 420 | }, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 421 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 422 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 423 | func TestClasspath(t *testing.T) { |
| 424 | for _, testcase := range classpathTestcases { |
| 425 | t.Run(testcase.name, func(t *testing.T) { |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 426 | moduleType := "java_library" |
| 427 | if testcase.moduleType != "" { |
| 428 | moduleType = testcase.moduleType |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 429 | } |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 430 | |
| 431 | bp := moduleType + ` { |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 432 | name: "foo", |
| 433 | srcs: ["a.java"], |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 434 | ` + testcase.properties + ` |
| 435 | }` |
| 436 | |
| 437 | variant := "android_common" |
| 438 | if testcase.host == android.Host { |
| 439 | variant = android.BuildOs.String() + "_common" |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 440 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 441 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 442 | convertModulesToPaths := func(cp []string) []string { |
| 443 | ret := make([]string, len(cp)) |
| 444 | for i, e := range cp { |
| 445 | ret[i] = moduleToPath(e) |
| 446 | } |
| 447 | return ret |
| 448 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 449 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 450 | bootclasspath := convertModulesToPaths(testcase.bootclasspath) |
| 451 | classpath := convertModulesToPaths(testcase.classpath) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 452 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 453 | bc := strings.Join(bootclasspath, ":") |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 454 | if bc != "" { |
| 455 | bc = "-bootclasspath " + bc |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 456 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 457 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 458 | c := strings.Join(classpath, ":") |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 459 | if c != "" { |
| 460 | c = "-classpath " + c |
| 461 | } |
| 462 | system := "" |
| 463 | if testcase.system == "none" { |
| 464 | system = "--system=none" |
| 465 | } else if testcase.system != "" { |
| 466 | system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/" |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 469 | t.Run("1.8", func(t *testing.T) { |
| 470 | // Test default javac 1.8 |
| 471 | ctx := testJava(t, bp) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 472 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 473 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 474 | |
| 475 | got := javac.Args["bootClasspath"] |
| 476 | if got != bc { |
| 477 | t.Errorf("bootclasspath expected %q != got %q", bc, got) |
| 478 | } |
| 479 | |
| 480 | got = javac.Args["classpath"] |
| 481 | if got != c { |
| 482 | t.Errorf("classpath expected %q != got %q", c, got) |
| 483 | } |
| 484 | |
| 485 | var deps []string |
| 486 | if len(bootclasspath) > 0 && bootclasspath[0] != `""` { |
| 487 | deps = append(deps, bootclasspath...) |
| 488 | } |
| 489 | deps = append(deps, classpath...) |
| 490 | |
| 491 | if !reflect.DeepEqual(javac.Implicits.Strings(), deps) { |
| 492 | t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings()) |
| 493 | } |
| 494 | }) |
| 495 | |
| 496 | // Test again with javac 1.9 |
| 497 | t.Run("1.9", func(t *testing.T) { |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 498 | config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"}) |
| 499 | ctx := testContext(config, bp, nil) |
| 500 | run(t, ctx, config) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 501 | |
| 502 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 503 | got := javac.Args["bootClasspath"] |
| 504 | expected := system |
| 505 | if testcase.system == "bootclasspath" { |
| 506 | expected = bc |
| 507 | } |
| 508 | if got != expected { |
| 509 | t.Errorf("bootclasspath expected %q != got %q", expected, got) |
| 510 | } |
| 511 | }) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 512 | }) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | func TestPrebuilts(t *testing.T) { |
| 518 | ctx := testJava(t, ` |
| 519 | java_library { |
| 520 | name: "foo", |
| 521 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 522 | libs: ["bar"], |
| 523 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 526 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 527 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 528 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 531 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 532 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 533 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 534 | } |
| 535 | `) |
| 536 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 537 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 538 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 539 | |
| 540 | bar := "a.jar" |
| 541 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 542 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 543 | } |
| 544 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 545 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" { |
| 546 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 550 | func TestDefaults(t *testing.T) { |
| 551 | ctx := testJava(t, ` |
| 552 | java_defaults { |
| 553 | name: "defaults", |
| 554 | srcs: ["a.java"], |
| 555 | libs: ["bar"], |
| 556 | static_libs: ["baz"], |
| 557 | } |
| 558 | |
| 559 | java_library { |
| 560 | name: "foo", |
| 561 | defaults: ["defaults"], |
| 562 | } |
| 563 | |
| 564 | java_library { |
| 565 | name: "bar", |
| 566 | srcs: ["b.java"], |
| 567 | } |
| 568 | |
| 569 | java_library { |
| 570 | name: "baz", |
| 571 | srcs: ["c.java"], |
| 572 | } |
| 573 | `) |
| 574 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 575 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 576 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 577 | |
| 578 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 579 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 580 | } |
| 581 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 582 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 583 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 584 | 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] | 585 | } |
| 586 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 587 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 588 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 589 | 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] | 590 | } |
| 591 | } |
| 592 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 593 | func TestResources(t *testing.T) { |
| 594 | var table = []struct { |
| 595 | name string |
| 596 | prop string |
| 597 | extra string |
| 598 | args string |
| 599 | }{ |
| 600 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 601 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 602 | name: "resource dirs", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 603 | prop: `java_resource_dirs: ["java-res"]`, |
| 604 | args: "-C java-res -f java-res/a -f java-res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 605 | }, |
| 606 | { |
| 607 | // Test that a module with java_resources includes the files |
| 608 | name: "resource files", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 609 | prop: `java_resources: ["java-res/a", "java-res/b"]`, |
| 610 | args: "-C . -f java-res/a -f java-res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 611 | }, |
| 612 | { |
| 613 | // Test that a module with a filegroup in java_resources includes the files with the |
| 614 | // path prefix |
| 615 | name: "resource filegroup", |
| 616 | prop: `java_resources: [":foo-res"]`, |
| 617 | extra: ` |
| 618 | filegroup { |
| 619 | name: "foo-res", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 620 | path: "java-res", |
| 621 | srcs: ["java-res/a", "java-res/b"], |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 622 | }`, |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 623 | args: "-C java-res -f java-res/a -f java-res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 624 | }, |
| 625 | { |
| 626 | // Test that a module with "include_srcs: true" includes its source files in the resources jar |
| 627 | name: "include sources", |
| 628 | prop: `include_srcs: true`, |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 629 | args: "-C . -f a.java -f b.java -f c.java", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 630 | }, |
| 631 | } |
| 632 | |
| 633 | for _, test := range table { |
| 634 | t.Run(test.name, func(t *testing.T) { |
| 635 | ctx := testJava(t, ` |
| 636 | java_library { |
| 637 | name: "foo", |
| 638 | srcs: [ |
| 639 | "a.java", |
| 640 | "b.java", |
| 641 | "c.java", |
| 642 | ], |
| 643 | `+test.prop+`, |
| 644 | } |
| 645 | `+test.extra) |
| 646 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 647 | foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") |
| 648 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 649 | |
| 650 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 651 | t.Errorf("foo combined jars %v does not contain %q", |
| 652 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 653 | } |
| 654 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 655 | if fooRes.Args["jarArgs"] != test.args { |
| 656 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 657 | fooRes.Args["jarArgs"], test.args) |
| 658 | } |
| 659 | }) |
| 660 | } |
| 661 | } |
| 662 | |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 663 | func TestExcludeResources(t *testing.T) { |
| 664 | ctx := testJava(t, ` |
| 665 | java_library { |
| 666 | name: "foo", |
| 667 | srcs: ["a.java"], |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 668 | java_resource_dirs: ["java-res", "java-res2"], |
| 669 | exclude_java_resource_dirs: ["java-res2"], |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | java_library { |
| 673 | name: "bar", |
| 674 | srcs: ["a.java"], |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 675 | java_resources: ["java-res/*"], |
| 676 | exclude_java_resources: ["java-res/b"], |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 677 | } |
| 678 | `) |
| 679 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 680 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 681 | |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 682 | expected := "-C java-res -f java-res/a -f java-res/b" |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 683 | if fooRes.Args["jarArgs"] != expected { |
| 684 | t.Errorf("foo resource jar args %q is not %q", |
| 685 | fooRes.Args["jarArgs"], expected) |
| 686 | |
| 687 | } |
| 688 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 689 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 690 | |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 691 | expected = "-C . -f java-res/a" |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 692 | if barRes.Args["jarArgs"] != expected { |
| 693 | t.Errorf("bar resource jar args %q is not %q", |
| 694 | barRes.Args["jarArgs"], expected) |
| 695 | |
| 696 | } |
| 697 | } |
| 698 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 699 | func TestGeneratedSources(t *testing.T) { |
| 700 | ctx := testJava(t, ` |
| 701 | java_library { |
| 702 | name: "foo", |
| 703 | srcs: [ |
| 704 | "a*.java", |
| 705 | ":gen", |
| 706 | "b*.java", |
| 707 | ], |
| 708 | } |
| 709 | |
| 710 | genrule { |
| 711 | name: "gen", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 712 | tool_files: ["java-res/a"], |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 713 | out: ["gen.java"], |
| 714 | } |
| 715 | `) |
| 716 | |
| 717 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 718 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 719 | |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 720 | if filepath.Base(genrule.Output.String()) != "gen.java" { |
| 721 | 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] | 722 | } |
| 723 | |
| 724 | if len(javac.Inputs) != 3 || |
| 725 | javac.Inputs[0].String() != "a.java" || |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 726 | javac.Inputs[1].String() != genrule.Output.String() || |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 727 | javac.Inputs[2].String() != "b.java" { |
| 728 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 729 | } |
| 730 | } |
| 731 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 732 | func TestKotlin(t *testing.T) { |
| 733 | ctx := testJava(t, ` |
| 734 | java_library { |
| 735 | name: "foo", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 736 | srcs: ["a.java", "b.kt"], |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 737 | } |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 738 | |
| 739 | java_library { |
| 740 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 741 | srcs: ["b.kt"], |
| 742 | libs: ["foo"], |
| 743 | static_libs: ["baz"], |
| 744 | } |
| 745 | |
| 746 | java_library { |
| 747 | name: "baz", |
| 748 | srcs: ["c.java"], |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 749 | } |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 750 | `) |
| 751 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 752 | fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") |
| 753 | fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 754 | fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 755 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 756 | if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" || |
| 757 | fooKotlinc.Inputs[1].String() != "b.kt" { |
| 758 | t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs) |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 761 | if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" { |
| 762 | t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs) |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 765 | if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) { |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 766 | t.Errorf("foo classpath %v does not contain %q", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 767 | fooJavac.Args["classpath"], fooKotlinc.Output.String()) |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 770 | if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) { |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 771 | t.Errorf("foo jar inputs %v does not contain %q", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 772 | fooJar.Inputs.Strings(), fooKotlinc.Output.String()) |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 773 | } |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 774 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 775 | fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar") |
| 776 | bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar") |
| 777 | barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc") |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 778 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 779 | if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" { |
| 780 | t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs) |
| 781 | } |
| 782 | |
| 783 | if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { |
| 784 | t.Errorf(`expected %q in bar implicits %v`, |
| 785 | fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) |
| 786 | } |
| 787 | |
| 788 | if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { |
| 789 | t.Errorf(`expected %q in bar implicits %v`, |
| 790 | bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 791 | } |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 794 | func TestTurbine(t *testing.T) { |
| 795 | ctx := testJava(t, ` |
| 796 | java_library { |
| 797 | name: "foo", |
| 798 | srcs: ["a.java"], |
| 799 | } |
| 800 | |
| 801 | java_library { |
| 802 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 803 | srcs: ["b.java"], |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 804 | static_libs: ["foo"], |
| 805 | } |
| 806 | |
| 807 | java_library { |
| 808 | name: "baz", |
| 809 | srcs: ["c.java"], |
| 810 | libs: ["bar"], |
| 811 | sdk_version: "14", |
| 812 | } |
| 813 | `) |
| 814 | |
| 815 | fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine") |
| 816 | barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine") |
| 817 | barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac") |
| 818 | barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine") |
| 819 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 820 | |
| 821 | if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" { |
| 822 | t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs) |
| 823 | } |
| 824 | |
| 825 | fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 826 | if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) { |
| 827 | t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar) |
| 828 | } |
| 829 | if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) { |
| 830 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar) |
| 831 | } |
| 832 | if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar { |
| 833 | t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar) |
| 834 | } |
| 835 | if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/android.jar") { |
| 836 | t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"], |
| 837 | "prebuilts/sdk/14/android.jar") |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | func TestSharding(t *testing.T) { |
| 842 | ctx := testJava(t, ` |
| 843 | java_library { |
| 844 | name: "bar", |
| 845 | srcs: ["a.java","b.java","c.java"], |
| 846 | javac_shard_size: 1 |
| 847 | } |
| 848 | `) |
| 849 | |
| 850 | barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 851 | for i := 0; i < 3; i++ { |
| 852 | barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)) |
| 853 | if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) { |
| 854 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar) |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 859 | func TestJarGenrules(t *testing.T) { |
| 860 | ctx := testJava(t, ` |
| 861 | java_library { |
| 862 | name: "foo", |
| 863 | srcs: ["a.java"], |
| 864 | } |
| 865 | |
| 866 | java_genrule { |
| 867 | name: "jargen", |
| 868 | tool_files: ["b.java"], |
| 869 | cmd: "$(location b.java) $(in) $(out)", |
| 870 | out: ["jargen.jar"], |
| 871 | srcs: [":foo"], |
| 872 | } |
| 873 | |
| 874 | java_library { |
| 875 | name: "bar", |
| 876 | static_libs: ["jargen"], |
| 877 | srcs: ["c.java"], |
| 878 | } |
| 879 | |
| 880 | java_library { |
| 881 | name: "baz", |
| 882 | libs: ["jargen"], |
| 883 | srcs: ["c.java"], |
| 884 | } |
| 885 | `) |
| 886 | |
| 887 | foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar") |
| 888 | jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar") |
| 889 | bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar") |
| 890 | baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar") |
| 891 | barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar") |
| 892 | |
| 893 | if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() { |
| 894 | t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings()) |
| 895 | } |
| 896 | |
| 897 | if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) { |
| 898 | t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String()) |
| 899 | } |
| 900 | |
| 901 | if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) { |
| 902 | t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String()) |
| 903 | } |
| 904 | |
| 905 | if len(barCombined.Inputs) != 2 || |
| 906 | barCombined.Inputs[0].String() != bar.Output.String() || |
| 907 | barCombined.Inputs[1].String() != jargen.Output.String() { |
| 908 | t.Errorf("bar combined jar inputs %v is not [%q, %q]", |
| 909 | barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String()) |
| 910 | } |
| 911 | } |
| 912 | |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame^] | 913 | func TestExcludeFileGroupInSrcs(t *testing.T) { |
| 914 | ctx := testJava(t, ` |
| 915 | java_library { |
| 916 | name: "foo", |
| 917 | srcs: ["a.java", ":foo-srcs"], |
| 918 | exclude_srcs: ["a.java", ":foo-excludes"], |
| 919 | } |
| 920 | |
| 921 | filegroup { |
| 922 | name: "foo-srcs", |
| 923 | srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"], |
| 924 | } |
| 925 | |
| 926 | filegroup { |
| 927 | name: "foo-excludes", |
| 928 | srcs: ["java-fg/a.java", "java-fg/b.java"], |
| 929 | } |
| 930 | `) |
| 931 | |
| 932 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 933 | |
| 934 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" { |
| 935 | t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs) |
| 936 | } |
| 937 | } |
| 938 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 939 | func fail(t *testing.T, errs []error) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 940 | t.Helper() |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 941 | if len(errs) > 0 { |
| 942 | for _, err := range errs { |
| 943 | t.Error(err) |
| 944 | } |
| 945 | t.FailNow() |
| 946 | } |
| 947 | } |