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 ( |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 18 | "fmt" |
| 19 | "io/ioutil" |
| 20 | "os" |
| 21 | "path/filepath" |
Colin Cross | 227d436 | 2017-08-30 14:14:52 -0700 | [diff] [blame] | 22 | "reflect" |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 23 | "strconv" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 24 | "strings" |
| 25 | "testing" |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 26 | |
| 27 | "github.com/google/blueprint/proptools" |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 28 | |
| 29 | "android/soong/android" |
| 30 | "android/soong/cc" |
| 31 | "android/soong/genrule" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | var buildDir string |
| 35 | |
| 36 | func setUp() { |
| 37 | var err error |
| 38 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 39 | if err != nil { |
| 40 | panic(err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func tearDown() { |
| 45 | os.RemoveAll(buildDir) |
| 46 | } |
| 47 | |
| 48 | func TestMain(m *testing.M) { |
| 49 | run := func() int { |
| 50 | setUp() |
| 51 | defer tearDown() |
| 52 | |
| 53 | return m.Run() |
| 54 | } |
| 55 | |
| 56 | os.Exit(run()) |
| 57 | } |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 58 | |
| 59 | func testConfig(env map[string]string) android.Config { |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 60 | if env == nil { |
| 61 | env = make(map[string]string) |
| 62 | } |
| 63 | if env["ANDROID_JAVA8_HOME"] == "" { |
| 64 | env["ANDROID_JAVA8_HOME"] = "jdk8" |
| 65 | } |
Jiyong Park | 1a5d7b1 | 2018-01-15 15:05:10 +0900 | [diff] [blame] | 66 | config := android.TestArchConfig(buildDir, env) |
Dan Willemsen | 674dc7f | 2018-03-12 18:06:05 -0700 | [diff] [blame] | 67 | config.TestProductVariables.DeviceSystemSdkVersions = &[]string{"14", "15"} |
Jiyong Park | 1a5d7b1 | 2018-01-15 15:05:10 +0900 | [diff] [blame] | 68 | return config |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 69 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 72 | func testContext(config android.Config, bp string, |
| 73 | fs map[string][]byte) *android.TestContext { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 74 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 75 | ctx := android.NewTestArchContext() |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 76 | ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory)) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 77 | ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory)) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 78 | ctx.RegisterModuleType("android_test", android.ModuleFactoryAdaptor(AndroidTestFactory)) |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame^] | 79 | ctx.RegisterModuleType("android_test_helper_app", android.ModuleFactoryAdaptor(AndroidTestHelperAppFactory)) |
| 80 | ctx.RegisterModuleType("java_binary", android.ModuleFactoryAdaptor(BinaryFactory)) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 81 | ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory)) |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 82 | ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory)) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 83 | ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory)) |
Colin Cross | 638149e | 2019-01-05 22:12:12 -0800 | [diff] [blame^] | 84 | ctx.RegisterModuleType("java_test", android.ModuleFactoryAdaptor(TestFactory)) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 85 | ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory)) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 86 | ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory)) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 87 | ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory)) |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 88 | ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory)) |
Pirama Arumuga Nainar | 955dc49 | 2018-04-17 14:58:42 -0700 | [diff] [blame] | 89 | ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory)) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 90 | ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory)) |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 91 | ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory)) |
| 92 | ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory)) |
Nan Zhang | f4936b0 | 2018-08-01 15:00:28 -0700 | [diff] [blame] | 93 | ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory)) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 94 | ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(sdkLibraryFactory)) |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 95 | ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(prebuiltApisFactory)) |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 96 | ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) |
| 97 | ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 98 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 99 | ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) { |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 100 | ctx.TopDown("prebuilt_apis", prebuiltApisMutator).Parallel() |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 101 | ctx.TopDown("java_sdk_library", sdkLibraryMutator).Parallel() |
| 102 | }) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 103 | ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory)) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 104 | |
| 105 | // Register module types and mutators from cc needed for JNI testing |
| 106 | ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory)) |
| 107 | ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory)) |
| 108 | ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory)) |
| 109 | ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 110 | ctx.BottomUp("link", cc.LinkageMutator).Parallel() |
| 111 | ctx.BottomUp("begin", cc.BeginMutator).Parallel() |
| 112 | }) |
| 113 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 114 | ctx.Register() |
| 115 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 116 | extraModules := []string{ |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 117 | "core-lambda-stubs", |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 118 | "framework", |
| 119 | "ext", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 120 | "android_stubs_current", |
| 121 | "android_system_stubs_current", |
| 122 | "android_test_stubs_current", |
Colin Cross | f19b9bb | 2018-03-26 14:42:44 -0700 | [diff] [blame] | 123 | "core.current.stubs", |
Neil Fuller | bf210f7 | 2018-10-09 16:19:19 +0100 | [diff] [blame] | 124 | "core.platform.api.stubs", |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 125 | "kotlin-stdlib", |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 126 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 127 | |
| 128 | for _, extra := range extraModules { |
| 129 | bp += fmt.Sprintf(` |
| 130 | java_library { |
| 131 | name: "%s", |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 132 | srcs: ["a.java"], |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 133 | no_standard_libs: true, |
Colin Cross | f19b9bb | 2018-03-26 14:42:44 -0700 | [diff] [blame] | 134 | sdk_version: "core_current", |
Neil Fuller | bf210f7 | 2018-10-09 16:19:19 +0100 | [diff] [blame] | 135 | system_modules: "core-platform-api-stubs-system-modules", |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 136 | } |
| 137 | `, extra) |
| 138 | } |
| 139 | |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 140 | bp += ` |
| 141 | android_app { |
| 142 | name: "framework-res", |
| 143 | no_framework_libs: true, |
| 144 | } |
| 145 | ` |
| 146 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 147 | systemModules := []string{ |
| 148 | "core-system-modules", |
Neil Fuller | bf210f7 | 2018-10-09 16:19:19 +0100 | [diff] [blame] | 149 | "core-platform-api-stubs-system-modules", |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 150 | "android_stubs_current_system_modules", |
| 151 | "android_system_stubs_current_system_modules", |
| 152 | "android_test_stubs_current_system_modules", |
| 153 | } |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 154 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 155 | for _, extra := range systemModules { |
| 156 | bp += fmt.Sprintf(` |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 157 | java_system_modules { |
| 158 | name: "%s", |
| 159 | } |
| 160 | `, extra) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 163 | mockFS := map[string][]byte{ |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 164 | "Android.bp": []byte(bp), |
| 165 | "a.java": nil, |
| 166 | "b.java": nil, |
| 167 | "c.java": nil, |
| 168 | "b.kt": nil, |
| 169 | "a.jar": nil, |
| 170 | "b.jar": nil, |
| 171 | "java-res/a/a": nil, |
| 172 | "java-res/b/b": nil, |
| 173 | "java-res2/a": nil, |
| 174 | "java-fg/a.java": nil, |
| 175 | "java-fg/b.java": nil, |
| 176 | "java-fg/c.java": nil, |
| 177 | "api/current.txt": nil, |
| 178 | "api/removed.txt": nil, |
| 179 | "api/system-current.txt": nil, |
| 180 | "api/system-removed.txt": nil, |
| 181 | "api/test-current.txt": nil, |
| 182 | "api/test-removed.txt": nil, |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 183 | |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 184 | "prebuilts/sdk/14/public/android.jar": nil, |
| 185 | "prebuilts/sdk/14/public/framework.aidl": nil, |
| 186 | "prebuilts/sdk/14/system/android.jar": nil, |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 187 | "prebuilts/sdk/current/core/android.jar": nil, |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 188 | "prebuilts/sdk/current/public/android.jar": nil, |
| 189 | "prebuilts/sdk/current/public/framework.aidl": nil, |
| 190 | "prebuilts/sdk/current/public/core.jar": nil, |
| 191 | "prebuilts/sdk/current/system/android.jar": nil, |
| 192 | "prebuilts/sdk/current/test/android.jar": nil, |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 193 | "prebuilts/sdk/28/public/api/foo.txt": nil, |
| 194 | "prebuilts/sdk/28/system/api/foo.txt": nil, |
| 195 | "prebuilts/sdk/28/test/api/foo.txt": nil, |
| 196 | "prebuilts/sdk/28/public/api/foo-removed.txt": nil, |
| 197 | "prebuilts/sdk/28/system/api/foo-removed.txt": nil, |
| 198 | "prebuilts/sdk/28/test/api/foo-removed.txt": nil, |
| 199 | "prebuilts/sdk/28/public/api/bar.txt": nil, |
| 200 | "prebuilts/sdk/28/system/api/bar.txt": nil, |
| 201 | "prebuilts/sdk/28/test/api/bar.txt": nil, |
| 202 | "prebuilts/sdk/28/public/api/bar-removed.txt": nil, |
| 203 | "prebuilts/sdk/28/system/api/bar-removed.txt": nil, |
| 204 | "prebuilts/sdk/28/test/api/bar-removed.txt": nil, |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 205 | "prebuilts/sdk/tools/core-lambda-stubs.jar": nil, |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 206 | "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`), |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 207 | |
| 208 | // For framework-res, which is an implicit dependency for framework |
| 209 | "AndroidManifest.xml": nil, |
| 210 | "build/target/product/security/testkey": nil, |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 211 | |
| 212 | "build/soong/scripts/jar-wrapper.sh": nil, |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 213 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 214 | "build/make/core/proguard.flags": nil, |
| 215 | "build/make/core/proguard_basic_keeps.flags": nil, |
| 216 | |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 217 | "jdk8/jre/lib/jce.jar": nil, |
| 218 | "jdk8/jre/lib/rt.jar": nil, |
Nan Zhang | 61819ce | 2018-05-04 18:49:16 -0700 | [diff] [blame] | 219 | "jdk8/lib/tools.jar": nil, |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 220 | |
| 221 | "bar-doc/a.java": nil, |
| 222 | "bar-doc/b.java": nil, |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 223 | "bar-doc/IFoo.aidl": nil, |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 224 | "bar-doc/known_oj_tags.txt": nil, |
| 225 | "external/doclava/templates-sdk": nil, |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | for k, v := range fs { |
| 229 | mockFS[k] = v |
| 230 | } |
| 231 | |
| 232 | ctx.MockFileSystem(mockFS) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 233 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 234 | return ctx |
| 235 | } |
| 236 | |
| 237 | func run(t *testing.T, ctx *android.TestContext, config android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 238 | t.Helper() |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 239 | _, errs := ctx.ParseFileList(".", []string{"Android.bp", "prebuilts/sdk/Android.bp"}) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 240 | android.FailIfErrored(t, errs) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 241 | _, errs = ctx.PrepareBuildActions(config) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 242 | android.FailIfErrored(t, errs) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | func testJava(t *testing.T, bp string) *android.TestContext { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 246 | t.Helper() |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 247 | config := testConfig(nil) |
| 248 | ctx := testContext(config, bp, nil) |
| 249 | run(t, ctx, config) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 250 | |
| 251 | return ctx |
| 252 | } |
| 253 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 254 | func moduleToPath(name string) string { |
| 255 | switch { |
| 256 | case name == `""`: |
| 257 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 258 | case strings.HasSuffix(name, ".jar"): |
| 259 | return name |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 260 | default: |
| 261 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 265 | func TestSimple(t *testing.T) { |
| 266 | ctx := testJava(t, ` |
| 267 | java_library { |
| 268 | name: "foo", |
| 269 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 270 | libs: ["bar"], |
| 271 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | java_library { |
| 275 | name: "bar", |
| 276 | srcs: ["b.java"], |
| 277 | } |
| 278 | |
| 279 | java_library { |
| 280 | name: "baz", |
| 281 | srcs: ["c.java"], |
| 282 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 283 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 284 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 285 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 286 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 287 | |
| 288 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 289 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 290 | } |
| 291 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 292 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 293 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 294 | bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 295 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 296 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 297 | 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] | 298 | } |
| 299 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 300 | if !strings.Contains(javac.Args["classpath"], bazTurbine) { |
| 301 | 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] | 302 | } |
| 303 | |
| 304 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 305 | 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] | 306 | } |
| 307 | } |
| 308 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 309 | func TestArchSpecific(t *testing.T) { |
| 310 | ctx := testJava(t, ` |
| 311 | java_library { |
| 312 | name: "foo", |
| 313 | srcs: ["a.java"], |
| 314 | target: { |
| 315 | android: { |
| 316 | srcs: ["b.java"], |
| 317 | }, |
| 318 | }, |
| 319 | } |
| 320 | `) |
| 321 | |
| 322 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 323 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 324 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 325 | } |
| 326 | } |
| 327 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 328 | func TestBinary(t *testing.T) { |
| 329 | ctx := testJava(t, ` |
| 330 | java_library_host { |
| 331 | name: "foo", |
| 332 | srcs: ["a.java"], |
| 333 | } |
| 334 | |
| 335 | java_binary_host { |
| 336 | name: "bar", |
| 337 | srcs: ["b.java"], |
| 338 | static_libs: ["foo"], |
| 339 | } |
| 340 | `) |
| 341 | |
| 342 | buildOS := android.BuildOs.String() |
| 343 | |
| 344 | bar := ctx.ModuleForTests("bar", buildOS+"_common") |
| 345 | barJar := bar.Output("bar.jar").Output.String() |
| 346 | barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64") |
| 347 | barWrapperDeps := barWrapper.Output("bar").Implicits.Strings() |
| 348 | |
| 349 | // Test that the install binary wrapper depends on the installed jar file |
| 350 | if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar { |
| 351 | t.Errorf("expected binary wrapper implicits [%q], got %v", |
| 352 | barJar, barWrapperDeps) |
| 353 | } |
| 354 | |
| 355 | } |
| 356 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 357 | var classpathTestcases = []struct { |
| 358 | name string |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 359 | unbundled bool |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 360 | moduleType string |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 361 | host android.OsClass |
| 362 | properties string |
| 363 | bootclasspath []string |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 364 | system string |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 365 | classpath []string |
| 366 | }{ |
| 367 | { |
| 368 | name: "default", |
Neil Fuller | bf210f7 | 2018-10-09 16:19:19 +0100 | [diff] [blame] | 369 | bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"}, |
| 370 | system: "core-platform-api-stubs-system-modules", |
Neil Fuller | 1fee9f3 | 2018-10-04 11:45:49 +0100 | [diff] [blame] | 371 | classpath: []string{"ext", "framework"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 372 | }, |
| 373 | { |
| 374 | name: "blank sdk version", |
| 375 | properties: `sdk_version: "",`, |
Neil Fuller | bf210f7 | 2018-10-09 16:19:19 +0100 | [diff] [blame] | 376 | bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"}, |
| 377 | system: "core-platform-api-stubs-system-modules", |
Neil Fuller | 1fee9f3 | 2018-10-04 11:45:49 +0100 | [diff] [blame] | 378 | classpath: []string{"ext", "framework"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 379 | }, |
| 380 | { |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 381 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 382 | name: "sdk v14", |
| 383 | properties: `sdk_version: "14",`, |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 384 | bootclasspath: []string{`""`}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 385 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 386 | classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 387 | }, |
| 388 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 389 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 390 | name: "current", |
| 391 | properties: `sdk_version: "current",`, |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 392 | bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"}, |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 393 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 394 | }, |
| 395 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 396 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 397 | name: "system_current", |
| 398 | properties: `sdk_version: "system_current",`, |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 399 | bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"}, |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 400 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 401 | }, |
| 402 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 403 | |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 404 | name: "system_14", |
| 405 | properties: `sdk_version: "system_14",`, |
| 406 | bootclasspath: []string{`""`}, |
| 407 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 408 | classpath: []string{"prebuilts/sdk/14/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"}, |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 409 | }, |
| 410 | { |
| 411 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 412 | name: "test_current", |
| 413 | properties: `sdk_version: "test_current",`, |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 414 | bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"}, |
Colin Cross | 2ebc476 | 2017-10-20 14:00:31 -0700 | [diff] [blame] | 415 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 416 | }, |
| 417 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 418 | |
Jiyong Park | 750e557 | 2018-01-31 00:20:13 +0900 | [diff] [blame] | 419 | name: "core_current", |
| 420 | properties: `sdk_version: "core_current",`, |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 421 | bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"}, |
Jiyong Park | 750e557 | 2018-01-31 00:20:13 +0900 | [diff] [blame] | 422 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Jiyong Park | 750e557 | 2018-01-31 00:20:13 +0900 | [diff] [blame] | 423 | }, |
| 424 | { |
| 425 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 426 | name: "nostdlib", |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 427 | properties: `no_standard_libs: true, system_modules: "none"`, |
| 428 | system: "none", |
| 429 | bootclasspath: []string{`""`}, |
| 430 | classpath: []string{}, |
| 431 | }, |
| 432 | { |
| 433 | |
| 434 | name: "nostdlib system_modules", |
Neil Fuller | bf210f7 | 2018-10-09 16:19:19 +0100 | [diff] [blame] | 435 | properties: `no_standard_libs: true, system_modules: "core-platform-api-stubs-system-modules"`, |
| 436 | system: "core-platform-api-stubs-system-modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 437 | bootclasspath: []string{`""`}, |
| 438 | classpath: []string{}, |
| 439 | }, |
| 440 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 441 | |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 442 | name: "host default", |
| 443 | moduleType: "java_library_host", |
| 444 | properties: ``, |
| 445 | host: android.Host, |
| 446 | bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"}, |
| 447 | classpath: []string{}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 448 | }, |
| 449 | { |
| 450 | name: "host nostdlib", |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 451 | moduleType: "java_library_host", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 452 | host: android.Host, |
| 453 | properties: `no_standard_libs: true`, |
| 454 | classpath: []string{}, |
| 455 | }, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 456 | { |
| 457 | |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 458 | name: "host supported default", |
| 459 | host: android.Host, |
| 460 | properties: `host_supported: true,`, |
| 461 | classpath: []string{}, |
| 462 | bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"}, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 463 | }, |
| 464 | { |
| 465 | name: "host supported nostdlib", |
| 466 | host: android.Host, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 467 | properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 468 | classpath: []string{}, |
| 469 | }, |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 470 | { |
| 471 | |
| 472 | name: "unbundled sdk v14", |
| 473 | unbundled: true, |
| 474 | properties: `sdk_version: "14",`, |
| 475 | bootclasspath: []string{`""`}, |
| 476 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 477 | classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"}, |
| 478 | }, |
| 479 | { |
| 480 | |
| 481 | name: "unbundled current", |
| 482 | unbundled: true, |
| 483 | properties: `sdk_version: "current",`, |
| 484 | bootclasspath: []string{`""`}, |
| 485 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 486 | classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"}, |
| 487 | }, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 488 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 489 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 490 | func TestClasspath(t *testing.T) { |
| 491 | for _, testcase := range classpathTestcases { |
| 492 | t.Run(testcase.name, func(t *testing.T) { |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 493 | moduleType := "java_library" |
| 494 | if testcase.moduleType != "" { |
| 495 | moduleType = testcase.moduleType |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 496 | } |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 497 | |
| 498 | bp := moduleType + ` { |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 499 | name: "foo", |
| 500 | srcs: ["a.java"], |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 501 | ` + testcase.properties + ` |
| 502 | }` |
| 503 | |
| 504 | variant := "android_common" |
| 505 | if testcase.host == android.Host { |
| 506 | variant = android.BuildOs.String() + "_common" |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 507 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 508 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 509 | convertModulesToPaths := func(cp []string) []string { |
| 510 | ret := make([]string, len(cp)) |
| 511 | for i, e := range cp { |
| 512 | ret[i] = moduleToPath(e) |
| 513 | } |
| 514 | return ret |
| 515 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 516 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 517 | bootclasspath := convertModulesToPaths(testcase.bootclasspath) |
| 518 | classpath := convertModulesToPaths(testcase.classpath) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 519 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 520 | bc := strings.Join(bootclasspath, ":") |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 521 | if bc != "" { |
| 522 | bc = "-bootclasspath " + bc |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 523 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 524 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 525 | c := strings.Join(classpath, ":") |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 526 | if c != "" { |
| 527 | c = "-classpath " + c |
| 528 | } |
| 529 | system := "" |
| 530 | if testcase.system == "none" { |
| 531 | system = "--system=none" |
| 532 | } else if testcase.system != "" { |
| 533 | system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/" |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 536 | t.Run("1.8", func(t *testing.T) { |
| 537 | // Test default javac 1.8 |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 538 | config := testConfig(nil) |
| 539 | if testcase.unbundled { |
| 540 | config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true) |
| 541 | } |
| 542 | ctx := testContext(config, bp, nil) |
| 543 | run(t, ctx, config) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 544 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 545 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 546 | |
| 547 | got := javac.Args["bootClasspath"] |
| 548 | if got != bc { |
| 549 | t.Errorf("bootclasspath expected %q != got %q", bc, got) |
| 550 | } |
| 551 | |
| 552 | got = javac.Args["classpath"] |
| 553 | if got != c { |
| 554 | t.Errorf("classpath expected %q != got %q", c, got) |
| 555 | } |
| 556 | |
| 557 | var deps []string |
| 558 | if len(bootclasspath) > 0 && bootclasspath[0] != `""` { |
| 559 | deps = append(deps, bootclasspath...) |
| 560 | } |
| 561 | deps = append(deps, classpath...) |
| 562 | |
| 563 | if !reflect.DeepEqual(javac.Implicits.Strings(), deps) { |
| 564 | t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings()) |
| 565 | } |
| 566 | }) |
| 567 | |
| 568 | // Test again with javac 1.9 |
| 569 | t.Run("1.9", func(t *testing.T) { |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 570 | config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"}) |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 571 | if testcase.unbundled { |
| 572 | config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true) |
| 573 | } |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 574 | ctx := testContext(config, bp, nil) |
| 575 | run(t, ctx, config) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 576 | |
| 577 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 578 | got := javac.Args["bootClasspath"] |
| 579 | expected := system |
| 580 | if testcase.system == "bootclasspath" { |
| 581 | expected = bc |
| 582 | } |
| 583 | if got != expected { |
| 584 | t.Errorf("bootclasspath expected %q != got %q", expected, got) |
| 585 | } |
| 586 | }) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 587 | }) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | func TestPrebuilts(t *testing.T) { |
| 593 | ctx := testJava(t, ` |
| 594 | java_library { |
| 595 | name: "foo", |
| 596 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 597 | libs: ["bar"], |
| 598 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 601 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 602 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 603 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 606 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 607 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 608 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 609 | } |
| 610 | `) |
| 611 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 612 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 613 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 614 | barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output |
| 615 | bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 616 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 617 | if !strings.Contains(javac.Args["classpath"], barJar.String()) { |
| 618 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String()) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 621 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() { |
| 622 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String()) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 626 | func TestDefaults(t *testing.T) { |
| 627 | ctx := testJava(t, ` |
| 628 | java_defaults { |
| 629 | name: "defaults", |
| 630 | srcs: ["a.java"], |
| 631 | libs: ["bar"], |
| 632 | static_libs: ["baz"], |
| 633 | } |
| 634 | |
| 635 | java_library { |
| 636 | name: "foo", |
| 637 | defaults: ["defaults"], |
| 638 | } |
| 639 | |
| 640 | java_library { |
| 641 | name: "bar", |
| 642 | srcs: ["b.java"], |
| 643 | } |
| 644 | |
| 645 | java_library { |
| 646 | name: "baz", |
| 647 | srcs: ["c.java"], |
| 648 | } |
| 649 | `) |
| 650 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 651 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 652 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 653 | |
| 654 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 655 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 656 | } |
| 657 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 658 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 659 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 660 | 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] | 661 | } |
| 662 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 663 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 664 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 665 | 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] | 666 | } |
| 667 | } |
| 668 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 669 | func TestResources(t *testing.T) { |
| 670 | var table = []struct { |
| 671 | name string |
| 672 | prop string |
| 673 | extra string |
| 674 | args string |
| 675 | }{ |
| 676 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 677 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 678 | name: "resource dirs", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 679 | prop: `java_resource_dirs: ["java-res"]`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 680 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 681 | }, |
| 682 | { |
| 683 | // Test that a module with java_resources includes the files |
| 684 | name: "resource files", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 685 | prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`, |
| 686 | args: "-C . -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 687 | }, |
| 688 | { |
| 689 | // Test that a module with a filegroup in java_resources includes the files with the |
| 690 | // path prefix |
| 691 | name: "resource filegroup", |
| 692 | prop: `java_resources: [":foo-res"]`, |
| 693 | extra: ` |
| 694 | filegroup { |
| 695 | name: "foo-res", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 696 | path: "java-res", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 697 | srcs: ["java-res/a/a", "java-res/b/b"], |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 698 | }`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 699 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 700 | }, |
| 701 | { |
| 702 | // Test that a module with "include_srcs: true" includes its source files in the resources jar |
| 703 | name: "include sources", |
| 704 | prop: `include_srcs: true`, |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 705 | args: "-C . -f a.java -f b.java -f c.java", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 706 | }, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 707 | { |
| 708 | // Test that a module with wildcards in java_resource_dirs has the correct path prefixes |
| 709 | name: "wildcard dirs", |
| 710 | prop: `java_resource_dirs: ["java-res/*"]`, |
| 711 | args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b", |
| 712 | }, |
| 713 | { |
| 714 | // Test that a module exclude_java_resource_dirs excludes the files |
| 715 | name: "wildcard dirs", |
| 716 | prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`, |
| 717 | args: "-C java-res/a -f java-res/a/a", |
| 718 | }, |
Colin Cross | cedd476 | 2018-09-13 11:26:19 -0700 | [diff] [blame] | 719 | { |
| 720 | // Test wildcards in java_resources |
| 721 | name: "wildcard files", |
| 722 | prop: `java_resources: ["java-res/**/*"]`, |
| 723 | args: "-C . -f java-res/a/a -f java-res/b/b", |
| 724 | }, |
| 725 | { |
| 726 | // Test exclude_java_resources with java_resources |
| 727 | name: "wildcard files with exclude", |
| 728 | prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`, |
| 729 | args: "-C . -f java-res/a/a", |
| 730 | }, |
| 731 | { |
| 732 | // Test exclude_java_resources with java_resource_dirs |
| 733 | name: "resource dirs with exclude files", |
| 734 | prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`, |
| 735 | args: "-C java-res -f java-res/a/a", |
| 736 | }, |
| 737 | { |
| 738 | // Test exclude_java_resource_dirs with java_resource_dirs |
| 739 | name: "resource dirs with exclude files", |
| 740 | prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`, |
| 741 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
| 742 | }, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | for _, test := range table { |
| 746 | t.Run(test.name, func(t *testing.T) { |
| 747 | ctx := testJava(t, ` |
| 748 | java_library { |
| 749 | name: "foo", |
| 750 | srcs: [ |
| 751 | "a.java", |
| 752 | "b.java", |
| 753 | "c.java", |
| 754 | ], |
| 755 | `+test.prop+`, |
| 756 | } |
| 757 | `+test.extra) |
| 758 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 759 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 760 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 761 | |
| 762 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 763 | t.Errorf("foo combined jars %v does not contain %q", |
| 764 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 765 | } |
| 766 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 767 | if fooRes.Args["jarArgs"] != test.args { |
| 768 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 769 | fooRes.Args["jarArgs"], test.args) |
| 770 | } |
| 771 | }) |
| 772 | } |
| 773 | } |
| 774 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 775 | func TestGeneratedSources(t *testing.T) { |
| 776 | ctx := testJava(t, ` |
| 777 | java_library { |
| 778 | name: "foo", |
| 779 | srcs: [ |
| 780 | "a*.java", |
| 781 | ":gen", |
| 782 | "b*.java", |
| 783 | ], |
| 784 | } |
| 785 | |
| 786 | genrule { |
| 787 | name: "gen", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 788 | tool_files: ["java-res/a"], |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 789 | out: ["gen.java"], |
| 790 | } |
| 791 | `) |
| 792 | |
| 793 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 794 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 795 | |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 796 | if filepath.Base(genrule.Output.String()) != "gen.java" { |
| 797 | 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] | 798 | } |
| 799 | |
| 800 | if len(javac.Inputs) != 3 || |
| 801 | javac.Inputs[0].String() != "a.java" || |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 802 | javac.Inputs[1].String() != genrule.Output.String() || |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 803 | javac.Inputs[2].String() != "b.java" { |
| 804 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 805 | } |
| 806 | } |
| 807 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 808 | func TestKotlin(t *testing.T) { |
| 809 | ctx := testJava(t, ` |
| 810 | java_library { |
| 811 | name: "foo", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 812 | srcs: ["a.java", "b.kt"], |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 813 | } |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 814 | |
| 815 | java_library { |
| 816 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 817 | srcs: ["b.kt"], |
| 818 | libs: ["foo"], |
| 819 | static_libs: ["baz"], |
| 820 | } |
| 821 | |
| 822 | java_library { |
| 823 | name: "baz", |
| 824 | srcs: ["c.java"], |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 825 | } |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 826 | `) |
| 827 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 828 | fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") |
| 829 | fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 830 | fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 831 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 832 | if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" || |
| 833 | fooKotlinc.Inputs[1].String() != "b.kt" { |
| 834 | t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs) |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 837 | if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" { |
| 838 | t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs) |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 841 | if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) { |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 842 | t.Errorf("foo classpath %v does not contain %q", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 843 | fooJavac.Args["classpath"], fooKotlinc.Output.String()) |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 846 | if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) { |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 847 | t.Errorf("foo jar inputs %v does not contain %q", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 848 | fooJar.Inputs.Strings(), fooKotlinc.Output.String()) |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 849 | } |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 850 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 851 | fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar") |
| 852 | bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar") |
| 853 | barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc") |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 854 | |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 855 | if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" { |
| 856 | t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs) |
| 857 | } |
| 858 | |
| 859 | if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { |
| 860 | t.Errorf(`expected %q in bar implicits %v`, |
| 861 | fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) |
| 862 | } |
| 863 | |
| 864 | if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { |
| 865 | t.Errorf(`expected %q in bar implicits %v`, |
| 866 | bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) |
Colin Cross | 715d711 | 2017-10-25 20:32:18 -0700 | [diff] [blame] | 867 | } |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 870 | func TestTurbine(t *testing.T) { |
| 871 | ctx := testJava(t, ` |
| 872 | java_library { |
| 873 | name: "foo", |
| 874 | srcs: ["a.java"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 875 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | java_library { |
| 879 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 880 | srcs: ["b.java"], |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 881 | static_libs: ["foo"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 882 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | java_library { |
| 886 | name: "baz", |
| 887 | srcs: ["c.java"], |
| 888 | libs: ["bar"], |
| 889 | sdk_version: "14", |
| 890 | } |
| 891 | `) |
| 892 | |
| 893 | fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine") |
| 894 | barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine") |
| 895 | barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac") |
| 896 | barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine") |
| 897 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 898 | |
| 899 | if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" { |
| 900 | t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs) |
| 901 | } |
| 902 | |
| 903 | fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 904 | if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) { |
| 905 | t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar) |
| 906 | } |
| 907 | if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) { |
| 908 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar) |
| 909 | } |
| 910 | if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar { |
| 911 | t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar) |
| 912 | } |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 913 | if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") { |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 914 | t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"], |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 915 | "prebuilts/sdk/14/public/android.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
| 919 | func TestSharding(t *testing.T) { |
| 920 | ctx := testJava(t, ` |
| 921 | java_library { |
| 922 | name: "bar", |
| 923 | srcs: ["a.java","b.java","c.java"], |
| 924 | javac_shard_size: 1 |
| 925 | } |
| 926 | `) |
| 927 | |
| 928 | barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 929 | for i := 0; i < 3; i++ { |
| 930 | barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)) |
| 931 | if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) { |
| 932 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar) |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 937 | func TestDroiddoc(t *testing.T) { |
| 938 | ctx := testJava(t, ` |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 939 | droiddoc_template { |
| 940 | name: "droiddoc-templates-sdk", |
| 941 | path: ".", |
| 942 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 943 | droiddoc { |
| 944 | name: "bar-doc", |
| 945 | srcs: [ |
| 946 | "bar-doc/*.java", |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 947 | "bar-doc/IFoo.aidl", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 948 | ], |
| 949 | exclude_srcs: [ |
| 950 | "bar-doc/b.java" |
| 951 | ], |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 952 | custom_template: "droiddoc-templates-sdk", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 953 | hdf: [ |
| 954 | "android.whichdoc offline", |
| 955 | ], |
| 956 | knowntags: [ |
| 957 | "bar-doc/known_oj_tags.txt", |
| 958 | ], |
| 959 | proofread_file: "libcore-proofread.txt", |
| 960 | todo_file: "libcore-docs-todo.html", |
| 961 | args: "-offlinemode -title \"libcore\"", |
| 962 | } |
| 963 | `) |
| 964 | |
Nan Zhang | 3bb1420 | 2018-02-28 10:14:25 -0800 | [diff] [blame] | 965 | stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar") |
| 966 | barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar") |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 967 | if stubsJar != barDoc.Output.String() { |
| 968 | t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String()) |
| 969 | } |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 970 | inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs |
| 971 | var javaSrcs []string |
| 972 | for _, i := range inputs { |
| 973 | javaSrcs = append(javaSrcs, i.Base()) |
| 974 | } |
| 975 | if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" { |
| 976 | t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs) |
| 977 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 978 | } |
| 979 | |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 980 | func TestJarGenrules(t *testing.T) { |
| 981 | ctx := testJava(t, ` |
| 982 | java_library { |
| 983 | name: "foo", |
| 984 | srcs: ["a.java"], |
| 985 | } |
| 986 | |
| 987 | java_genrule { |
| 988 | name: "jargen", |
| 989 | tool_files: ["b.java"], |
| 990 | cmd: "$(location b.java) $(in) $(out)", |
| 991 | out: ["jargen.jar"], |
| 992 | srcs: [":foo"], |
| 993 | } |
| 994 | |
| 995 | java_library { |
| 996 | name: "bar", |
| 997 | static_libs: ["jargen"], |
| 998 | srcs: ["c.java"], |
| 999 | } |
| 1000 | |
| 1001 | java_library { |
| 1002 | name: "baz", |
| 1003 | libs: ["jargen"], |
| 1004 | srcs: ["c.java"], |
| 1005 | } |
| 1006 | `) |
| 1007 | |
| 1008 | foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar") |
| 1009 | jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar") |
| 1010 | bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar") |
| 1011 | baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar") |
| 1012 | barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar") |
| 1013 | |
| 1014 | if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() { |
| 1015 | t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings()) |
| 1016 | } |
| 1017 | |
| 1018 | if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) { |
| 1019 | t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String()) |
| 1020 | } |
| 1021 | |
| 1022 | if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) { |
| 1023 | t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String()) |
| 1024 | } |
| 1025 | |
| 1026 | if len(barCombined.Inputs) != 2 || |
| 1027 | barCombined.Inputs[0].String() != bar.Output.String() || |
| 1028 | barCombined.Inputs[1].String() != jargen.Output.String() { |
| 1029 | t.Errorf("bar combined jar inputs %v is not [%q, %q]", |
| 1030 | barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String()) |
| 1031 | } |
| 1032 | } |
| 1033 | |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1034 | func TestExcludeFileGroupInSrcs(t *testing.T) { |
| 1035 | ctx := testJava(t, ` |
| 1036 | java_library { |
| 1037 | name: "foo", |
| 1038 | srcs: ["a.java", ":foo-srcs"], |
| 1039 | exclude_srcs: ["a.java", ":foo-excludes"], |
| 1040 | } |
| 1041 | |
| 1042 | filegroup { |
| 1043 | name: "foo-srcs", |
| 1044 | srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"], |
| 1045 | } |
| 1046 | |
| 1047 | filegroup { |
| 1048 | name: "foo-excludes", |
| 1049 | srcs: ["java-fg/a.java", "java-fg/b.java"], |
| 1050 | } |
| 1051 | `) |
| 1052 | |
| 1053 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 1054 | |
| 1055 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" { |
| 1056 | t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs) |
| 1057 | } |
| 1058 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1059 | |
| 1060 | func TestJavaSdkLibrary(t *testing.T) { |
| 1061 | ctx := testJava(t, ` |
| 1062 | droiddoc_template { |
| 1063 | name: "droiddoc-templates-sdk", |
| 1064 | path: ".", |
| 1065 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1066 | java_sdk_library { |
| 1067 | name: "foo", |
| 1068 | srcs: ["a.java", "b.java"], |
| 1069 | api_packages: ["foo"], |
| 1070 | } |
| 1071 | java_sdk_library { |
| 1072 | name: "bar", |
| 1073 | srcs: ["a.java", "b.java"], |
| 1074 | api_packages: ["bar"], |
| 1075 | } |
| 1076 | java_library { |
| 1077 | name: "baz", |
| 1078 | srcs: ["c.java"], |
| 1079 | libs: ["foo", "bar"], |
| 1080 | sdk_version: "system_current", |
| 1081 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1082 | java_library { |
| 1083 | name: "qux", |
| 1084 | srcs: ["c.java"], |
| 1085 | libs: ["baz"], |
| 1086 | sdk_version: "system_current", |
| 1087 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1088 | `) |
| 1089 | |
| 1090 | // check the existence of the internal modules |
| 1091 | ctx.ModuleForTests("foo", "android_common") |
| 1092 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common") |
| 1093 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1094 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1095 | ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common") |
| 1096 | ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1097 | ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1098 | ctx.ModuleForTests("foo"+sdkImplLibrarySuffix, "android_common") |
Jiyong Park | 5a8d1be | 2018-04-25 22:57:34 +0900 | [diff] [blame] | 1099 | ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1100 | ctx.ModuleForTests("foo.api.public.28", "") |
| 1101 | ctx.ModuleForTests("foo.api.system.28", "") |
| 1102 | ctx.ModuleForTests("foo.api.test.28", "") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1103 | |
| 1104 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 1105 | // tests if baz is actually linked to the stubs lib |
| 1106 | if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") { |
| 1107 | t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"], |
| 1108 | "foo.stubs.system.jar") |
| 1109 | } |
| 1110 | // ... and not to the impl lib |
| 1111 | if strings.Contains(bazJavac.Args["classpath"], "foo.impl.jar") { |
| 1112 | t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"], |
| 1113 | "foo.impl.jar") |
| 1114 | } |
| 1115 | // test if baz is not linked to the system variant of foo |
| 1116 | if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") { |
| 1117 | t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"], |
| 1118 | "foo.stubs.jar") |
| 1119 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1120 | |
| 1121 | // test if baz has exported SDK lib names foo and bar to qux |
| 1122 | qux := ctx.ModuleForTests("qux", "android_common") |
| 1123 | if quxLib, ok := qux.Module().(*Library); ok { |
| 1124 | sdkLibs := quxLib.ExportedSdkLibs() |
| 1125 | if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) { |
| 1126 | t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs) |
| 1127 | } |
| 1128 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1129 | } |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 1130 | |
| 1131 | var compilerFlagsTestCases = []struct { |
| 1132 | in string |
| 1133 | out bool |
| 1134 | }{ |
| 1135 | { |
| 1136 | in: "a", |
| 1137 | out: false, |
| 1138 | }, |
| 1139 | { |
| 1140 | in: "-a", |
| 1141 | out: true, |
| 1142 | }, |
| 1143 | { |
| 1144 | in: "-no-jdk", |
| 1145 | out: false, |
| 1146 | }, |
| 1147 | { |
| 1148 | in: "-no-stdlib", |
| 1149 | out: false, |
| 1150 | }, |
| 1151 | { |
| 1152 | in: "-kotlin-home", |
| 1153 | out: false, |
| 1154 | }, |
| 1155 | { |
| 1156 | in: "-kotlin-home /some/path", |
| 1157 | out: false, |
| 1158 | }, |
| 1159 | { |
| 1160 | in: "-include-runtime", |
| 1161 | out: false, |
| 1162 | }, |
| 1163 | { |
| 1164 | in: "-Xintellij-plugin-root", |
| 1165 | out: false, |
| 1166 | }, |
| 1167 | } |
| 1168 | |
| 1169 | type mockContext struct { |
| 1170 | android.ModuleContext |
| 1171 | result bool |
| 1172 | } |
| 1173 | |
| 1174 | func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) { |
| 1175 | // CheckBadCompilerFlags calls this function when the flag should be rejected |
| 1176 | ctx.result = false |
| 1177 | } |
| 1178 | |
| 1179 | func TestCompilerFlags(t *testing.T) { |
| 1180 | for _, testCase := range compilerFlagsTestCases { |
| 1181 | ctx := &mockContext{result: true} |
| 1182 | CheckKotlincFlags(ctx, []string{testCase.in}) |
| 1183 | if ctx.result != testCase.out { |
| 1184 | t.Errorf("incorrect output:") |
| 1185 | t.Errorf(" input: %#v", testCase.in) |
| 1186 | t.Errorf(" expected: %#v", testCase.out) |
| 1187 | t.Errorf(" got: %#v", ctx.result) |
| 1188 | } |
| 1189 | } |
| 1190 | } |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1191 | |
| 1192 | // TODO(jungjw): Consider making this more robust by ignoring path order. |
| 1193 | func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) { |
| 1194 | variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests() |
| 1195 | flags := strings.Split(variables["javacFlags"], " ") |
| 1196 | got := "" |
| 1197 | for _, flag := range flags { |
| 1198 | keyEnd := strings.Index(flag, "=") |
| 1199 | if keyEnd > -1 && flag[:keyEnd] == "--patch-module" { |
| 1200 | got = flag[keyEnd+1:] |
| 1201 | break |
| 1202 | } |
| 1203 | } |
| 1204 | if expected != got { |
| 1205 | t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got) |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | func TestPatchModule(t *testing.T) { |
| 1210 | bp := ` |
| 1211 | java_library { |
| 1212 | name: "foo", |
| 1213 | srcs: ["a.java"], |
| 1214 | } |
| 1215 | |
| 1216 | java_library { |
| 1217 | name: "bar", |
| 1218 | srcs: ["b.java"], |
| 1219 | no_standard_libs: true, |
| 1220 | system_modules: "none", |
| 1221 | patch_module: "java.base", |
| 1222 | } |
| 1223 | |
| 1224 | java_library { |
| 1225 | name: "baz", |
| 1226 | srcs: ["c.java"], |
| 1227 | patch_module: "java.base", |
| 1228 | } |
| 1229 | ` |
| 1230 | |
| 1231 | t.Run("1.8", func(t *testing.T) { |
| 1232 | // Test default javac 1.8 |
| 1233 | ctx := testJava(t, bp) |
| 1234 | |
| 1235 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1236 | checkPatchModuleFlag(t, ctx, "bar", "") |
| 1237 | checkPatchModuleFlag(t, ctx, "baz", "") |
| 1238 | }) |
| 1239 | |
| 1240 | t.Run("1.9", func(t *testing.T) { |
| 1241 | // Test again with javac 1.9 |
| 1242 | config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"}) |
| 1243 | ctx := testContext(config, bp, nil) |
| 1244 | run(t, ctx, config) |
| 1245 | |
| 1246 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1247 | expected := "java.base=.:" + buildDir |
| 1248 | checkPatchModuleFlag(t, ctx, "bar", expected) |
| 1249 | expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":") |
| 1250 | checkPatchModuleFlag(t, ctx, "baz", expected) |
| 1251 | }) |
| 1252 | } |