Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 1 | // Copyright 2019 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" |
| 19 | "path/filepath" |
| 20 | "reflect" |
| 21 | "strings" |
| 22 | "testing" |
| 23 | |
| 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
| 27 | var classpathTestcases = []struct { |
| 28 | name string |
| 29 | unbundled bool |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 30 | pdk bool |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 31 | moduleType string |
| 32 | host android.OsClass |
| 33 | properties string |
| 34 | bootclasspath []string |
| 35 | system string |
| 36 | classpath []string |
| 37 | }{ |
| 38 | { |
| 39 | name: "default", |
| 40 | bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"}, |
| 41 | system: "core-platform-api-stubs-system-modules", |
| 42 | classpath: []string{"ext", "framework"}, |
| 43 | }, |
| 44 | { |
| 45 | name: "blank sdk version", |
| 46 | properties: `sdk_version: "",`, |
| 47 | bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"}, |
| 48 | system: "core-platform-api-stubs-system-modules", |
| 49 | classpath: []string{"ext", "framework"}, |
| 50 | }, |
| 51 | { |
| 52 | |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 53 | name: "sdk v25", |
| 54 | properties: `sdk_version: "25",`, |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 55 | bootclasspath: []string{`""`}, |
| 56 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 57 | classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"}, |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 58 | }, |
| 59 | { |
| 60 | |
| 61 | name: "current", |
| 62 | properties: `sdk_version: "current",`, |
| 63 | bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"}, |
| 64 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 65 | }, |
| 66 | { |
| 67 | |
| 68 | name: "system_current", |
| 69 | properties: `sdk_version: "system_current",`, |
| 70 | bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"}, |
| 71 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 72 | }, |
| 73 | { |
| 74 | |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 75 | name: "system_25", |
| 76 | properties: `sdk_version: "system_25",`, |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 77 | bootclasspath: []string{`""`}, |
| 78 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 79 | classpath: []string{"prebuilts/sdk/25/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"}, |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 80 | }, |
| 81 | { |
| 82 | |
| 83 | name: "test_current", |
| 84 | properties: `sdk_version: "test_current",`, |
| 85 | bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"}, |
| 86 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 87 | }, |
| 88 | { |
| 89 | |
| 90 | name: "core_current", |
| 91 | properties: `sdk_version: "core_current",`, |
| 92 | bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"}, |
| 93 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 94 | }, |
| 95 | { |
| 96 | |
| 97 | name: "nostdlib", |
| 98 | properties: `no_standard_libs: true, system_modules: "none"`, |
| 99 | system: "none", |
| 100 | bootclasspath: []string{`""`}, |
| 101 | classpath: []string{}, |
| 102 | }, |
| 103 | { |
| 104 | |
| 105 | name: "nostdlib system_modules", |
| 106 | properties: `no_standard_libs: true, system_modules: "core-platform-api-stubs-system-modules"`, |
| 107 | system: "core-platform-api-stubs-system-modules", |
| 108 | bootclasspath: []string{`""`}, |
| 109 | classpath: []string{}, |
| 110 | }, |
| 111 | { |
| 112 | |
| 113 | name: "host default", |
| 114 | moduleType: "java_library_host", |
| 115 | properties: ``, |
| 116 | host: android.Host, |
| 117 | bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"}, |
| 118 | classpath: []string{}, |
| 119 | }, |
| 120 | { |
| 121 | name: "host nostdlib", |
| 122 | moduleType: "java_library_host", |
| 123 | host: android.Host, |
| 124 | properties: `no_standard_libs: true`, |
| 125 | classpath: []string{}, |
| 126 | }, |
| 127 | { |
| 128 | |
| 129 | name: "host supported default", |
| 130 | host: android.Host, |
| 131 | properties: `host_supported: true,`, |
| 132 | classpath: []string{}, |
| 133 | bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"}, |
| 134 | }, |
| 135 | { |
| 136 | name: "host supported nostdlib", |
| 137 | host: android.Host, |
| 138 | properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`, |
| 139 | classpath: []string{}, |
| 140 | }, |
| 141 | { |
| 142 | |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 143 | name: "unbundled sdk v25", |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 144 | unbundled: true, |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 145 | properties: `sdk_version: "25",`, |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 146 | bootclasspath: []string{`""`}, |
| 147 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 148 | classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"}, |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 149 | }, |
| 150 | { |
| 151 | |
| 152 | name: "unbundled current", |
| 153 | unbundled: true, |
| 154 | properties: `sdk_version: "current",`, |
| 155 | bootclasspath: []string{`""`}, |
| 156 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
| 157 | classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"}, |
| 158 | }, |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 159 | |
| 160 | { |
| 161 | name: "pdk default", |
| 162 | pdk: true, |
| 163 | bootclasspath: []string{`""`}, |
| 164 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 165 | classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"}, |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 166 | }, |
| 167 | { |
| 168 | name: "pdk current", |
| 169 | pdk: true, |
| 170 | properties: `sdk_version: "current",`, |
| 171 | bootclasspath: []string{`""`}, |
| 172 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 173 | classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"}, |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 174 | }, |
| 175 | { |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 176 | name: "pdk 25", |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 177 | pdk: true, |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 178 | properties: `sdk_version: "25",`, |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 179 | bootclasspath: []string{`""`}, |
| 180 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 181 | classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"}, |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 182 | }, |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | func TestClasspath(t *testing.T) { |
| 186 | for _, testcase := range classpathTestcases { |
| 187 | t.Run(testcase.name, func(t *testing.T) { |
| 188 | moduleType := "java_library" |
| 189 | if testcase.moduleType != "" { |
| 190 | moduleType = testcase.moduleType |
| 191 | } |
| 192 | |
| 193 | bp := moduleType + ` { |
| 194 | name: "foo", |
| 195 | srcs: ["a.java"], |
| 196 | ` + testcase.properties + ` |
| 197 | }` |
| 198 | |
| 199 | variant := "android_common" |
| 200 | if testcase.host == android.Host { |
| 201 | variant = android.BuildOs.String() + "_common" |
| 202 | } |
| 203 | |
| 204 | convertModulesToPaths := func(cp []string) []string { |
| 205 | ret := make([]string, len(cp)) |
| 206 | for i, e := range cp { |
| 207 | ret[i] = moduleToPath(e) |
| 208 | } |
| 209 | return ret |
| 210 | } |
| 211 | |
| 212 | bootclasspath := convertModulesToPaths(testcase.bootclasspath) |
| 213 | classpath := convertModulesToPaths(testcase.classpath) |
| 214 | |
| 215 | bc := strings.Join(bootclasspath, ":") |
| 216 | if bc != "" { |
| 217 | bc = "-bootclasspath " + bc |
| 218 | } |
| 219 | |
| 220 | c := strings.Join(classpath, ":") |
| 221 | if c != "" { |
| 222 | c = "-classpath " + c |
| 223 | } |
| 224 | system := "" |
| 225 | if testcase.system == "none" { |
| 226 | system = "--system=none" |
| 227 | } else if testcase.system != "" { |
| 228 | system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/" |
| 229 | } |
| 230 | |
| 231 | t.Run("1.8", func(t *testing.T) { |
| 232 | // Test default javac 1.8 |
| 233 | config := testConfig(nil) |
| 234 | if testcase.unbundled { |
| 235 | config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true) |
| 236 | } |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 237 | if testcase.pdk { |
| 238 | config.TestProductVariables.Pdk = proptools.BoolPtr(true) |
| 239 | } |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 240 | ctx := testContext(config, bp, nil) |
| 241 | run(t, ctx, config) |
| 242 | |
| 243 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 244 | |
| 245 | got := javac.Args["bootClasspath"] |
| 246 | if got != bc { |
| 247 | t.Errorf("bootclasspath expected %q != got %q", bc, got) |
| 248 | } |
| 249 | |
| 250 | got = javac.Args["classpath"] |
| 251 | if got != c { |
| 252 | t.Errorf("classpath expected %q != got %q", c, got) |
| 253 | } |
| 254 | |
| 255 | var deps []string |
| 256 | if len(bootclasspath) > 0 && bootclasspath[0] != `""` { |
| 257 | deps = append(deps, bootclasspath...) |
| 258 | } |
| 259 | deps = append(deps, classpath...) |
| 260 | |
| 261 | if !reflect.DeepEqual(javac.Implicits.Strings(), deps) { |
| 262 | t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings()) |
| 263 | } |
| 264 | }) |
| 265 | |
| 266 | // Test again with javac 1.9 |
| 267 | t.Run("1.9", func(t *testing.T) { |
| 268 | config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"}) |
| 269 | if testcase.unbundled { |
| 270 | config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true) |
| 271 | } |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 272 | if testcase.pdk { |
| 273 | config.TestProductVariables.Pdk = proptools.BoolPtr(true) |
| 274 | } |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 275 | ctx := testContext(config, bp, nil) |
| 276 | run(t, ctx, config) |
| 277 | |
| 278 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 279 | got := javac.Args["bootClasspath"] |
| 280 | expected := system |
| 281 | if testcase.system == "bootclasspath" { |
| 282 | expected = bc |
| 283 | } |
| 284 | if got != expected { |
| 285 | t.Errorf("bootclasspath expected %q != got %q", expected, got) |
| 286 | } |
| 287 | }) |
Colin Cross | ff0daf4 | 2019-04-02 16:10:56 -0700 | [diff] [blame^] | 288 | |
| 289 | // Test again with PLATFORM_VERSION_CODENAME=REL |
| 290 | t.Run("REL", func(t *testing.T) { |
| 291 | config := testConfig(nil) |
| 292 | config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL") |
| 293 | config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true) |
| 294 | |
| 295 | if testcase.unbundled { |
| 296 | config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true) |
| 297 | } |
| 298 | if testcase.pdk { |
| 299 | config.TestProductVariables.Pdk = proptools.BoolPtr(true) |
| 300 | } |
| 301 | ctx := testContext(config, bp, nil) |
| 302 | run(t, ctx, config) |
| 303 | |
| 304 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 305 | |
| 306 | got := javac.Args["bootClasspath"] |
| 307 | if got != bc { |
| 308 | t.Errorf("bootclasspath expected %q != got %q", bc, got) |
| 309 | } |
| 310 | |
| 311 | got = javac.Args["classpath"] |
| 312 | if got != c { |
| 313 | t.Errorf("classpath expected %q != got %q", c, got) |
| 314 | } |
| 315 | |
| 316 | var deps []string |
| 317 | if len(bootclasspath) > 0 && bootclasspath[0] != `""` { |
| 318 | deps = append(deps, bootclasspath...) |
| 319 | } |
| 320 | deps = append(deps, classpath...) |
| 321 | |
| 322 | if !reflect.DeepEqual(javac.Implicits.Strings(), deps) { |
| 323 | t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings()) |
| 324 | } |
| 325 | }) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 326 | }) |
| 327 | } |
| 328 | |
| 329 | } |