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