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