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 | "fmt" |
| 19 | "path/filepath" |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 20 | "sort" |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 21 | "strconv" |
| 22 | "strings" |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 23 | |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 24 | "android/soong/android" |
| 25 | "android/soong/java/config" |
| 26 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint/pathtools" |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 30 | func init() { |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 31 | android.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory) |
| 32 | android.RegisterSingletonType("sdk", sdkSingletonFactory) |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 33 | android.RegisterMakeVarsProvider(pctx, sdkMakeVars) |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 36 | var sdkVersionsKey = android.NewOnceKey("sdkVersionsKey") |
| 37 | var sdkFrameworkAidlPathKey = android.NewOnceKey("sdkFrameworkAidlPathKey") |
Anton Hansson | 3f07ab2 | 2020-04-09 13:29:59 +0100 | [diff] [blame] | 38 | var nonUpdatableFrameworkAidlPathKey = android.NewOnceKey("nonUpdatableFrameworkAidlPathKey") |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 39 | var apiFingerprintPathKey = android.NewOnceKey("apiFingerprintPathKey") |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 40 | |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 41 | type sdkContext interface { |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 42 | // sdkVersion returns sdkSpec that corresponds to the sdk_version property of the current module |
| 43 | sdkVersion() sdkSpec |
Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 44 | // systemModules returns the system_modules property of the current module, or an empty string if it is not set. |
| 45 | systemModules() string |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 46 | // minSdkVersion returns sdkSpec that corresponds to the min_sdk_version property of the current module, |
| 47 | // or from sdk_version if it is not set. |
| 48 | minSdkVersion() sdkSpec |
| 49 | // targetSdkVersion returns the sdkSpec that corresponds to the target_sdk_version property of the current module, |
| 50 | // or from sdk_version if it is not set. |
| 51 | targetSdkVersion() sdkSpec |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Nikita Ioffe | 1f4f345 | 2020-03-02 16:58:11 +0000 | [diff] [blame] | 54 | func UseApiFingerprint(ctx android.BaseModuleContext) bool { |
| 55 | if ctx.Config().UnbundledBuild() && |
Baligh Uddin | f620137 | 2020-01-24 23:15:44 +0000 | [diff] [blame] | 56 | !ctx.Config().UnbundledBuildUsePrebuiltSdks() && |
| 57 | ctx.Config().IsEnvTrue("UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT") { |
| 58 | return true |
| 59 | } |
| 60 | return false |
| 61 | } |
| 62 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 63 | // sdkKind represents a particular category of an SDK spec like public, system, test, etc. |
| 64 | type sdkKind int |
| 65 | |
| 66 | const ( |
| 67 | sdkInvalid sdkKind = iota |
| 68 | sdkNone |
| 69 | sdkCore |
| 70 | sdkCorePlatform |
| 71 | sdkPublic |
| 72 | sdkSystem |
| 73 | sdkTest |
Jiyong Park | 50146e9 | 2020-01-30 18:00:15 +0900 | [diff] [blame] | 74 | sdkModule |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 75 | sdkSystemServer |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 76 | sdkPrivate |
| 77 | ) |
| 78 | |
| 79 | // String returns the string representation of this sdkKind |
| 80 | func (k sdkKind) String() string { |
| 81 | switch k { |
| 82 | case sdkPrivate: |
| 83 | return "private" |
| 84 | case sdkNone: |
| 85 | return "none" |
| 86 | case sdkPublic: |
| 87 | return "public" |
| 88 | case sdkSystem: |
| 89 | return "system" |
| 90 | case sdkTest: |
| 91 | return "test" |
| 92 | case sdkCore: |
| 93 | return "core" |
| 94 | case sdkCorePlatform: |
| 95 | return "core_platform" |
Jiyong Park | 50146e9 | 2020-01-30 18:00:15 +0900 | [diff] [blame] | 96 | case sdkModule: |
| 97 | return "module" |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 98 | case sdkSystemServer: |
| 99 | return "system_server" |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 100 | default: |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 101 | return "invalid" |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 105 | // sdkVersion represents a specific version number of an SDK spec of a particular kind |
| 106 | type sdkVersion int |
| 107 | |
| 108 | const ( |
| 109 | // special version number for a not-yet-frozen SDK |
| 110 | sdkVersionCurrent sdkVersion = sdkVersion(android.FutureApiLevel) |
| 111 | // special version number to be used for SDK specs where version number doesn't |
| 112 | // make sense, e.g. "none", "", etc. |
| 113 | sdkVersionNone sdkVersion = sdkVersion(0) |
| 114 | ) |
| 115 | |
| 116 | // isCurrent checks if the sdkVersion refers to the not-yet-published version of an sdkKind |
| 117 | func (v sdkVersion) isCurrent() bool { |
| 118 | return v == sdkVersionCurrent |
| 119 | } |
| 120 | |
| 121 | // isNumbered checks if the sdkVersion refers to the published (a.k.a numbered) version of an sdkKind |
| 122 | func (v sdkVersion) isNumbered() bool { |
| 123 | return !v.isCurrent() && v != sdkVersionNone |
| 124 | } |
| 125 | |
| 126 | // String returns the string representation of this sdkVersion. |
| 127 | func (v sdkVersion) String() string { |
| 128 | if v.isCurrent() { |
| 129 | return "current" |
| 130 | } else if v.isNumbered() { |
| 131 | return strconv.Itoa(int(v)) |
| 132 | } |
| 133 | return "(no version)" |
| 134 | } |
| 135 | |
| 136 | // asNumberString directly converts the numeric value of this sdk version as a string. |
| 137 | // When isNumbered() is true, this method is the same as String(). However, for sdkVersionCurrent |
| 138 | // and sdkVersionNone, this returns 10000 and 0 while String() returns "current" and "(no version"), |
| 139 | // respectively. |
| 140 | func (v sdkVersion) asNumberString() string { |
| 141 | return strconv.Itoa(int(v)) |
| 142 | } |
| 143 | |
| 144 | // sdkSpec represents the kind and the version of an SDK for a module to build against |
| 145 | type sdkSpec struct { |
| 146 | kind sdkKind |
| 147 | version sdkVersion |
| 148 | raw string |
| 149 | } |
| 150 | |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 151 | func (s sdkSpec) String() string { |
| 152 | return fmt.Sprintf("%s_%s", s.kind, s.version) |
| 153 | } |
| 154 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 155 | // valid checks if this sdkSpec is well-formed. Note however that true doesn't mean that the |
| 156 | // specified SDK actually exists. |
| 157 | func (s sdkSpec) valid() bool { |
| 158 | return s.kind != sdkInvalid |
| 159 | } |
| 160 | |
| 161 | // specified checks if this sdkSpec is well-formed and is not "". |
| 162 | func (s sdkSpec) specified() bool { |
| 163 | return s.valid() && s.kind != sdkPrivate |
| 164 | } |
| 165 | |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 166 | // whether the API surface is managed and versioned, i.e. has .txt file that |
| 167 | // get frozen on SDK freeze and changes get reviewed by API council. |
| 168 | func (s sdkSpec) stable() bool { |
| 169 | if !s.specified() { |
| 170 | return false |
| 171 | } |
| 172 | switch s.kind { |
Artur Satayev | 8cf899a | 2020-04-15 17:29:42 +0100 | [diff] [blame] | 173 | case sdkNone: |
| 174 | // there is nothing to manage and version in this case; de facto stable API. |
| 175 | return true |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 176 | case sdkCore, sdkPublic, sdkSystem, sdkModule, sdkSystemServer: |
| 177 | return true |
Artur Satayev | 8cf899a | 2020-04-15 17:29:42 +0100 | [diff] [blame] | 178 | case sdkCorePlatform, sdkTest, sdkPrivate: |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 179 | return false |
| 180 | default: |
| 181 | panic(fmt.Errorf("unknown sdkKind=%v", s.kind)) |
| 182 | } |
| 183 | return false |
| 184 | } |
| 185 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 186 | // prebuiltSdkAvailableForUnbundledBuilt tells whether this sdkSpec can have a prebuilt SDK |
| 187 | // that can be used for unbundled builds. |
| 188 | func (s sdkSpec) prebuiltSdkAvailableForUnbundledBuild() bool { |
| 189 | // "", "none", and "core_platform" are not available for unbundled build |
| 190 | // as we don't/can't have prebuilt stub for the versions |
| 191 | return s.kind != sdkPrivate && s.kind != sdkNone && s.kind != sdkCorePlatform |
| 192 | } |
| 193 | |
| 194 | // forPdkBuild converts this sdkSpec into another sdkSpec that is for the PDK builds. |
| 195 | func (s sdkSpec) forPdkBuild(ctx android.EarlyModuleContext) sdkSpec { |
| 196 | // For PDK builds, use the latest SDK version instead of "current" or "" |
| 197 | if s.kind == sdkPrivate || s.kind == sdkPublic { |
| 198 | kind := s.kind |
| 199 | if kind == sdkPrivate { |
| 200 | // We don't have prebuilt SDK for private APIs, so use the public SDK |
| 201 | // instead. This looks odd, but that's how it has been done. |
| 202 | // TODO(b/148271073): investigate the need for this. |
| 203 | kind = sdkPublic |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 204 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 205 | version := sdkVersion(LatestSdkVersionInt(ctx)) |
| 206 | return sdkSpec{kind, version, s.raw} |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 207 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 208 | return s |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 211 | // usePrebuilt determines whether prebuilt SDK should be used for this sdkSpec with the given context. |
| 212 | func (s sdkSpec) usePrebuilt(ctx android.EarlyModuleContext) bool { |
| 213 | if s.version.isCurrent() { |
| 214 | // "current" can be built from source and be from prebuilt SDK |
| 215 | return ctx.Config().UnbundledBuildUsePrebuiltSdks() |
| 216 | } else if s.version.isNumbered() { |
| 217 | // sanity check |
| 218 | if s.kind != sdkPublic && s.kind != sdkSystem && s.kind != sdkTest { |
| 219 | panic(fmt.Errorf("prebuilt SDK is not not available for sdkKind=%q", s.kind)) |
| 220 | return false |
| 221 | } |
| 222 | // numbered SDKs are always from prebuilt |
| 223 | return true |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 224 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 225 | // "", "none", "core_platform" fall here |
| 226 | return false |
| 227 | } |
| 228 | |
| 229 | // effectiveVersion converts an sdkSpec into the concrete sdkVersion that the module |
| 230 | // should use. For modules targeting an unreleased SDK (meaning it does not yet have a number) |
| 231 | // it returns android.FutureApiLevel(10000). |
| 232 | func (s sdkSpec) effectiveVersion(ctx android.EarlyModuleContext) (sdkVersion, error) { |
| 233 | if !s.valid() { |
| 234 | return s.version, fmt.Errorf("invalid sdk version %q", s.raw) |
| 235 | } |
| 236 | if ctx.Config().IsPdkBuild() { |
| 237 | s = s.forPdkBuild(ctx) |
| 238 | } |
| 239 | if s.version.isNumbered() { |
| 240 | return s.version, nil |
| 241 | } |
| 242 | return sdkVersion(ctx.Config().DefaultAppTargetSdkInt()), nil |
| 243 | } |
| 244 | |
| 245 | // effectiveVersionString converts an sdkSpec into the concrete version string that the module |
| 246 | // should use. For modules targeting an unreleased SDK (meaning it does not yet have a number) |
| 247 | // it returns the codename (P, Q, R, etc.) |
| 248 | func (s sdkSpec) effectiveVersionString(ctx android.EarlyModuleContext) (string, error) { |
| 249 | ver, err := s.effectiveVersion(ctx) |
| 250 | if err == nil && int(ver) == ctx.Config().DefaultAppTargetSdkInt() { |
| 251 | return ctx.Config().DefaultAppTargetSdk(), nil |
| 252 | } |
| 253 | return ver.String(), err |
| 254 | } |
| 255 | |
Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 256 | func (s sdkSpec) defaultJavaLanguageVersion(ctx android.EarlyModuleContext) javaVersion { |
| 257 | sdk, err := s.effectiveVersion(ctx) |
| 258 | if err != nil { |
| 259 | ctx.PropertyErrorf("sdk_version", "%s", err) |
| 260 | } |
| 261 | if sdk <= 23 { |
| 262 | return JAVA_VERSION_7 |
| 263 | } else if sdk <= 29 { |
| 264 | return JAVA_VERSION_8 |
| 265 | } else { |
| 266 | return JAVA_VERSION_9 |
| 267 | } |
| 268 | } |
| 269 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 270 | func sdkSpecFrom(str string) sdkSpec { |
| 271 | switch str { |
| 272 | // special cases first |
| 273 | case "": |
| 274 | return sdkSpec{sdkPrivate, sdkVersionNone, str} |
| 275 | case "none": |
| 276 | return sdkSpec{sdkNone, sdkVersionNone, str} |
| 277 | case "core_platform": |
| 278 | return sdkSpec{sdkCorePlatform, sdkVersionNone, str} |
| 279 | default: |
| 280 | // the syntax is [kind_]version |
| 281 | sep := strings.LastIndex(str, "_") |
| 282 | |
| 283 | var kindString string |
| 284 | if sep == 0 { |
| 285 | return sdkSpec{sdkInvalid, sdkVersionNone, str} |
| 286 | } else if sep == -1 { |
| 287 | kindString = "" |
| 288 | } else { |
| 289 | kindString = str[0:sep] |
| 290 | } |
| 291 | versionString := str[sep+1 : len(str)] |
| 292 | |
| 293 | var kind sdkKind |
| 294 | switch kindString { |
| 295 | case "": |
| 296 | kind = sdkPublic |
| 297 | case "core": |
| 298 | kind = sdkCore |
| 299 | case "system": |
| 300 | kind = sdkSystem |
| 301 | case "test": |
| 302 | kind = sdkTest |
Jiyong Park | 50146e9 | 2020-01-30 18:00:15 +0900 | [diff] [blame] | 303 | case "module": |
| 304 | kind = sdkModule |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 305 | case "system_server": |
| 306 | kind = sdkSystemServer |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 307 | default: |
| 308 | return sdkSpec{sdkInvalid, sdkVersionNone, str} |
| 309 | } |
| 310 | |
| 311 | var version sdkVersion |
| 312 | if versionString == "current" { |
| 313 | version = sdkVersionCurrent |
| 314 | } else if i, err := strconv.Atoi(versionString); err == nil { |
| 315 | version = sdkVersion(i) |
| 316 | } else { |
| 317 | return sdkSpec{sdkInvalid, sdkVersionNone, str} |
| 318 | } |
| 319 | |
| 320 | return sdkSpec{kind, version, str} |
| 321 | } |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Jeongik Cha | 7c70831 | 2020-01-28 13:52:36 +0900 | [diff] [blame] | 324 | func (s sdkSpec) validateSystemSdk(ctx android.EarlyModuleContext) bool { |
| 325 | // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module) |
| 326 | // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29, |
| 327 | // sdk_version of the modules in vendor/product that use system sdk must be either system_28, system_29 or system_current |
| 328 | if s.kind != sdkSystem || !s.version.isNumbered() { |
| 329 | return true |
| 330 | } |
| 331 | allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions() |
| 332 | if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) { |
| 333 | systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions() |
| 334 | if len(systemSdkVersions) > 0 { |
| 335 | allowedVersions = systemSdkVersions |
| 336 | } |
| 337 | } |
| 338 | if len(allowedVersions) > 0 && !android.InList(s.version.String(), allowedVersions) { |
| 339 | ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q", |
| 340 | s.raw, allowedVersions) |
| 341 | return false |
| 342 | } |
| 343 | return true |
| 344 | } |
| 345 | |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 346 | func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext sdkContext) sdkDep { |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 347 | sdkVersion := sdkContext.sdkVersion() |
| 348 | if !sdkVersion.valid() { |
| 349 | ctx.PropertyErrorf("sdk_version", "invalid version %q", sdkVersion.raw) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 350 | return sdkDep{} |
| 351 | } |
| 352 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 353 | if ctx.Config().IsPdkBuild() { |
| 354 | sdkVersion = sdkVersion.forPdkBuild(ctx) |
| 355 | } |
Jeongik Cha | 7c70831 | 2020-01-28 13:52:36 +0900 | [diff] [blame] | 356 | if !sdkVersion.validateSystemSdk(ctx) { |
| 357 | return sdkDep{} |
| 358 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 359 | |
| 360 | if sdkVersion.usePrebuilt(ctx) { |
| 361 | dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), sdkVersion.kind.String()) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 362 | jar := filepath.Join(dir, "android.jar") |
| 363 | // There's no aidl for other SDKs yet. |
| 364 | // TODO(77525052): Add aidl files for other SDKs too. |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 365 | public_dir := filepath.Join("prebuilts", "sdk", sdkVersion.version.String(), "public") |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 366 | aidl := filepath.Join(public_dir, "framework.aidl") |
| 367 | jarPath := android.ExistentPathForSource(ctx, jar) |
| 368 | aidlPath := android.ExistentPathForSource(ctx, aidl) |
| 369 | lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath) |
| 370 | |
| 371 | if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() { |
| 372 | return sdkDep{ |
| 373 | invalidVersion: true, |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 374 | bootclasspath: []string{fmt.Sprintf("sdk_%s_%s_android", sdkVersion.kind, sdkVersion.version.String())}, |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | if !jarPath.Valid() { |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 379 | ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, jar) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 380 | return sdkDep{} |
| 381 | } |
| 382 | |
| 383 | if !aidlPath.Valid() { |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 384 | ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", sdkVersion.raw, aidl) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 385 | return sdkDep{} |
| 386 | } |
| 387 | |
Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 388 | var systemModules string |
| 389 | if sdkVersion.defaultJavaLanguageVersion(ctx).usesJavaModules() { |
| 390 | systemModules = "sdk_public_" + sdkVersion.version.String() + "_system_modules" |
| 391 | } |
| 392 | |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 393 | return sdkDep{ |
Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 394 | useFiles: true, |
| 395 | jars: android.Paths{jarPath.Path(), lambdaStubsPath}, |
| 396 | aidl: android.OptionalPathForPath(aidlPath.Path()), |
| 397 | systemModules: systemModules, |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 401 | toModule := func(modules []string, res string, aidl android.Path) sdkDep { |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 402 | return sdkDep{ |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 403 | useModule: true, |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 404 | bootclasspath: append(modules, config.DefaultLambdaStubsLibrary), |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 405 | systemModules: "core-current-stubs-system-modules", |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 406 | java9Classpath: modules, |
| 407 | frameworkResModule: res, |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 408 | aidl: android.OptionalPathForPath(aidl), |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 409 | } |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 412 | switch sdkVersion.kind { |
| 413 | case sdkPrivate: |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 414 | return sdkDep{ |
Pete Gillin | e3d44b2 | 2020-06-29 11:28:51 +0100 | [diff] [blame] | 415 | useModule: true, |
| 416 | systemModules: config.LegacyCorePlatformSystemModules, |
| 417 | bootclasspath: config.LegacyCorePlatformBootclasspathLibraries, |
| 418 | classpath: config.FrameworkLibraries, |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 419 | frameworkResModule: "framework-res", |
| 420 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 421 | case sdkNone: |
Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 422 | systemModules := sdkContext.systemModules() |
| 423 | if systemModules == "" { |
| 424 | ctx.PropertyErrorf("sdk_version", |
| 425 | `system_modules is required to be set to a non-empty value when sdk_version is "none", did you mean sdk_version: "core_platform"?`) |
| 426 | } else if systemModules == "none" { |
Colin Cross | 6d8d8c6 | 2019-10-28 15:10:03 -0700 | [diff] [blame] | 427 | return sdkDep{ |
| 428 | noStandardLibs: true, |
| 429 | } |
Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 432 | return sdkDep{ |
Colin Cross | 6d8d8c6 | 2019-10-28 15:10:03 -0700 | [diff] [blame] | 433 | useModule: true, |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 434 | noStandardLibs: true, |
Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 435 | systemModules: systemModules, |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 436 | bootclasspath: []string{systemModules}, |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 437 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 438 | case sdkCorePlatform: |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 439 | return sdkDep{ |
Pete Gillin | 7b0bdce | 2020-07-01 13:05:32 +0100 | [diff] [blame] | 440 | useModule: true, |
| 441 | systemModules: config.LegacyCorePlatformSystemModules, |
| 442 | bootclasspath: config.LegacyCorePlatformBootclasspathLibraries, |
| 443 | noFrameworksLibs: true, |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 444 | } |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 445 | case sdkPublic: |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 446 | return toModule([]string{"android_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx)) |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 447 | case sdkSystem: |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 448 | return toModule([]string{"android_system_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx)) |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 449 | case sdkTest: |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 450 | return toModule([]string{"android_test_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx)) |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 451 | case sdkCore: |
Pete Gillin | 880f964 | 2020-07-01 13:17:16 +0100 | [diff] [blame^] | 452 | return sdkDep{ |
| 453 | useModule: true, |
| 454 | bootclasspath: []string{"core.current.stubs", config.DefaultLambdaStubsLibrary}, |
| 455 | systemModules: "core-current-stubs-system-modules", |
| 456 | noFrameworksLibs: true, |
| 457 | } |
Jiyong Park | 50146e9 | 2020-01-30 18:00:15 +0900 | [diff] [blame] | 458 | case sdkModule: |
| 459 | // TODO(146757305): provide .apk and .aidl that have more APIs for modules |
Anton Hansson | 3f07ab2 | 2020-04-09 13:29:59 +0100 | [diff] [blame] | 460 | return toModule([]string{"android_module_lib_stubs_current"}, "framework-res", nonUpdatableFrameworkAidlPath(ctx)) |
Jiyong Park | aae9bd1 | 2020-02-12 04:36:43 +0900 | [diff] [blame] | 461 | case sdkSystemServer: |
| 462 | // TODO(146757305): provide .apk and .aidl that have more APIs for modules |
Anton Hansson | ba6ab2e | 2020-03-19 15:23:38 +0000 | [diff] [blame] | 463 | return toModule([]string{"android_system_server_stubs_current"}, "framework-res", sdkFrameworkAidlPath(ctx)) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 464 | default: |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 465 | panic(fmt.Errorf("invalid sdk %q", sdkVersion.raw)) |
Colin Cross | fb6d781 | 2019-01-09 22:17:55 -0800 | [diff] [blame] | 466 | } |
| 467 | } |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 468 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 469 | func sdkPreSingletonFactory() android.Singleton { |
| 470 | return sdkPreSingleton{} |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 473 | type sdkPreSingleton struct{} |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 474 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 475 | func (sdkPreSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 476 | sdkJars, err := ctx.GlobWithDeps("prebuilts/sdk/*/public/android.jar", nil) |
| 477 | if err != nil { |
| 478 | ctx.Errorf("failed to glob prebuilts/sdk/*/public/android.jar: %s", err.Error()) |
| 479 | } |
| 480 | |
| 481 | var sdkVersions []int |
| 482 | for _, sdkJar := range sdkJars { |
| 483 | dir := filepath.Base(filepath.Dir(filepath.Dir(sdkJar))) |
| 484 | v, err := strconv.Atoi(dir) |
| 485 | if scerr, ok := err.(*strconv.NumError); ok && scerr.Err == strconv.ErrSyntax { |
| 486 | continue |
| 487 | } else if err != nil { |
| 488 | ctx.Errorf("invalid sdk jar %q, %s, %v", sdkJar, err.Error()) |
| 489 | } |
| 490 | sdkVersions = append(sdkVersions, v) |
| 491 | } |
| 492 | |
| 493 | sort.Ints(sdkVersions) |
| 494 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 495 | ctx.Config().Once(sdkVersionsKey, func() interface{} { return sdkVersions }) |
| 496 | } |
| 497 | |
Jiyong Park | 6a927c4 | 2020-01-21 02:03:43 +0900 | [diff] [blame] | 498 | func LatestSdkVersionInt(ctx android.EarlyModuleContext) int { |
| 499 | sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int) |
| 500 | latestSdkVersion := 0 |
| 501 | if len(sdkVersions) > 0 { |
| 502 | latestSdkVersion = sdkVersions[len(sdkVersions)-1] |
| 503 | } |
| 504 | return latestSdkVersion |
| 505 | } |
| 506 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 507 | func sdkSingletonFactory() android.Singleton { |
| 508 | return sdkSingleton{} |
| 509 | } |
| 510 | |
| 511 | type sdkSingleton struct{} |
| 512 | |
| 513 | func (sdkSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 514 | if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() { |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 515 | return |
| 516 | } |
| 517 | |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 518 | createSdkFrameworkAidl(ctx) |
Anton Hansson | 3f07ab2 | 2020-04-09 13:29:59 +0100 | [diff] [blame] | 519 | createNonUpdatableFrameworkAidl(ctx) |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 520 | createAPIFingerprint(ctx) |
| 521 | } |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 522 | |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 523 | // Create framework.aidl by extracting anything that implements android.os.Parcelable from the SDK stubs modules. |
| 524 | func createSdkFrameworkAidl(ctx android.SingletonContext) { |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 525 | stubsModules := []string{ |
| 526 | "android_stubs_current", |
| 527 | "android_test_stubs_current", |
| 528 | "android_system_stubs_current", |
| 529 | } |
| 530 | |
Anton Hansson | 3f07ab2 | 2020-04-09 13:29:59 +0100 | [diff] [blame] | 531 | combinedAidl := sdkFrameworkAidlPath(ctx) |
| 532 | tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp") |
| 533 | |
| 534 | rule := createFrameworkAidl(stubsModules, tempPath, ctx) |
| 535 | |
| 536 | commitChangeForRestat(rule, tempPath, combinedAidl) |
| 537 | |
| 538 | rule.Build(pctx, ctx, "framework_aidl", "generate framework.aidl") |
| 539 | } |
| 540 | |
| 541 | // Creates a version of framework.aidl for the non-updatable part of the platform. |
| 542 | func createNonUpdatableFrameworkAidl(ctx android.SingletonContext) { |
| 543 | stubsModules := []string{"android_module_lib_stubs_current"} |
| 544 | |
| 545 | combinedAidl := nonUpdatableFrameworkAidlPath(ctx) |
| 546 | tempPath := combinedAidl.ReplaceExtension(ctx, "aidl.tmp") |
| 547 | |
| 548 | rule := createFrameworkAidl(stubsModules, tempPath, ctx) |
| 549 | |
| 550 | commitChangeForRestat(rule, tempPath, combinedAidl) |
| 551 | |
| 552 | rule.Build(pctx, ctx, "framework_non_updatable_aidl", "generate framework_non_updatable.aidl") |
| 553 | } |
| 554 | |
| 555 | func createFrameworkAidl(stubsModules []string, path android.OutputPath, ctx android.SingletonContext) *android.RuleBuilder { |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 556 | stubsJars := make([]android.Paths, len(stubsModules)) |
| 557 | |
| 558 | ctx.VisitAllModules(func(module android.Module) { |
| 559 | // Collect dex jar paths for the modules listed above. |
| 560 | if j, ok := module.(Dependency); ok { |
| 561 | name := ctx.ModuleName(module) |
| 562 | if i := android.IndexList(name, stubsModules); i != -1 { |
| 563 | stubsJars[i] = j.HeaderJars() |
| 564 | } |
| 565 | } |
| 566 | }) |
| 567 | |
| 568 | var missingDeps []string |
| 569 | |
| 570 | for i := range stubsJars { |
| 571 | if stubsJars[i] == nil { |
| 572 | if ctx.Config().AllowMissingDependencies() { |
| 573 | missingDeps = append(missingDeps, stubsModules[i]) |
| 574 | } else { |
Anton Hansson | 3f07ab2 | 2020-04-09 13:29:59 +0100 | [diff] [blame] | 575 | ctx.Errorf("failed to find dex jar path for module %q", stubsModules[i]) |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | rule := android.NewRuleBuilder() |
| 581 | rule.MissingDeps(missingDeps) |
| 582 | |
| 583 | var aidls android.Paths |
| 584 | for _, jars := range stubsJars { |
| 585 | for _, jar := range jars { |
| 586 | aidl := android.PathForOutput(ctx, "aidl", pathtools.ReplaceExtension(jar.Base(), "aidl")) |
| 587 | |
| 588 | rule.Command(). |
| 589 | Text("rm -f").Output(aidl) |
| 590 | rule.Command(). |
Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 591 | BuiltTool(ctx, "sdkparcelables"). |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 592 | Input(jar). |
| 593 | Output(aidl) |
| 594 | |
| 595 | aidls = append(aidls, aidl) |
| 596 | } |
| 597 | } |
| 598 | |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 599 | rule.Command(). |
Anton Hansson | 3f07ab2 | 2020-04-09 13:29:59 +0100 | [diff] [blame] | 600 | Text("rm -f").Output(path) |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 601 | rule.Command(). |
| 602 | Text("cat"). |
| 603 | Inputs(aidls). |
| 604 | Text("| sort -u >"). |
Anton Hansson | 3f07ab2 | 2020-04-09 13:29:59 +0100 | [diff] [blame] | 605 | Output(path) |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 606 | |
Anton Hansson | 3f07ab2 | 2020-04-09 13:29:59 +0100 | [diff] [blame] | 607 | return rule |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | func sdkFrameworkAidlPath(ctx android.PathContext) android.OutputPath { |
| 611 | return ctx.Config().Once(sdkFrameworkAidlPathKey, func() interface{} { |
| 612 | return android.PathForOutput(ctx, "framework.aidl") |
| 613 | }).(android.OutputPath) |
| 614 | } |
| 615 | |
Anton Hansson | 3f07ab2 | 2020-04-09 13:29:59 +0100 | [diff] [blame] | 616 | func nonUpdatableFrameworkAidlPath(ctx android.PathContext) android.OutputPath { |
| 617 | return ctx.Config().Once(nonUpdatableFrameworkAidlPathKey, func() interface{} { |
| 618 | return android.PathForOutput(ctx, "framework_non_updatable.aidl") |
| 619 | }).(android.OutputPath) |
| 620 | } |
| 621 | |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 622 | // Create api_fingerprint.txt |
| 623 | func createAPIFingerprint(ctx android.SingletonContext) { |
Jiyong Park | 71b519d | 2019-04-18 17:25:49 +0900 | [diff] [blame] | 624 | out := ApiFingerprintPath(ctx) |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 625 | |
| 626 | rule := android.NewRuleBuilder() |
| 627 | |
| 628 | rule.Command(). |
| 629 | Text("rm -f").Output(out) |
| 630 | cmd := rule.Command() |
| 631 | |
| 632 | if ctx.Config().PlatformSdkCodename() == "REL" { |
| 633 | cmd.Text("echo REL >").Output(out) |
| 634 | } else if ctx.Config().IsPdkBuild() { |
| 635 | // TODO: get this from the PDK artifacts? |
| 636 | cmd.Text("echo PDK >").Output(out) |
| 637 | } else if !ctx.Config().UnbundledBuildUsePrebuiltSdks() { |
| 638 | in, err := ctx.GlobWithDeps("frameworks/base/api/*current.txt", nil) |
| 639 | if err != nil { |
| 640 | ctx.Errorf("error globbing API files: %s", err) |
| 641 | } |
| 642 | |
| 643 | cmd.Text("cat"). |
| 644 | Inputs(android.PathsForSource(ctx, in)). |
Elliott Hughes | 34b49d1 | 2019-09-06 14:42:24 -0700 | [diff] [blame] | 645 | Text("| md5sum | cut -d' ' -f1 >"). |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 646 | Output(out) |
| 647 | } else { |
| 648 | // Unbundled build |
| 649 | // TODO: use a prebuilt api_fingerprint.txt from prebuilts/sdk/current.txt once we have one |
| 650 | cmd.Text("echo"). |
| 651 | Flag(ctx.Config().PlatformPreviewSdkVersion()). |
| 652 | Text(">"). |
| 653 | Output(out) |
| 654 | } |
| 655 | |
| 656 | rule.Build(pctx, ctx, "api_fingerprint", "generate api_fingerprint.txt") |
| 657 | } |
| 658 | |
Jiyong Park | 71b519d | 2019-04-18 17:25:49 +0900 | [diff] [blame] | 659 | func ApiFingerprintPath(ctx android.PathContext) android.OutputPath { |
Colin Cross | 1093287 | 2019-04-18 14:27:12 -0700 | [diff] [blame] | 660 | return ctx.Config().Once(apiFingerprintPathKey, func() interface{} { |
| 661 | return android.PathForOutput(ctx, "api_fingerprint.txt") |
| 662 | }).(android.OutputPath) |
| 663 | } |
| 664 | |
| 665 | func sdkMakeVars(ctx android.MakeVarsContext) { |
| 666 | if ctx.Config().UnbundledBuildUsePrebuiltSdks() || ctx.Config().IsPdkBuild() { |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 667 | return |
| 668 | } |
| 669 | |
| 670 | ctx.Strict("FRAMEWORK_AIDL", sdkFrameworkAidlPath(ctx).String()) |
Jiyong Park | 71b519d | 2019-04-18 17:25:49 +0900 | [diff] [blame] | 671 | ctx.Strict("API_FINGERPRINT", ApiFingerprintPath(ctx).String()) |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 672 | } |