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