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