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