Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 1 | // Copyright 2021 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 android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strconv" |
| 20 | "strings" |
| 21 | ) |
| 22 | |
| 23 | type SdkContext interface { |
| 24 | // SdkVersion returns SdkSpec that corresponds to the sdk_version property of the current module |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 25 | SdkVersion(ctx EarlyModuleContext) SdkSpec |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 26 | // SystemModules returns the system_modules property of the current module, or an empty string if it is not set. |
| 27 | SystemModules() string |
| 28 | // MinSdkVersion returns SdkSpec that corresponds to the min_sdk_version property of the current module, |
| 29 | // or from sdk_version if it is not set. |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 30 | MinSdkVersion(ctx EarlyModuleContext) SdkSpec |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 31 | // ReplaceMaxSdkVersionPlaceholder returns SdkSpec to replace the maxSdkVersion property of permission and |
| 32 | // uses-permission tags if it is set. |
| 33 | ReplaceMaxSdkVersionPlaceholder(ctx EarlyModuleContext) SdkSpec |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 34 | // TargetSdkVersion returns the SdkSpec that corresponds to the target_sdk_version property of the current module, |
| 35 | // or from sdk_version if it is not set. |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 36 | TargetSdkVersion(ctx EarlyModuleContext) SdkSpec |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // SdkKind represents a particular category of an SDK spec like public, system, test, etc. |
| 40 | type SdkKind int |
| 41 | |
| 42 | const ( |
| 43 | SdkInvalid SdkKind = iota |
| 44 | SdkNone |
| 45 | SdkCore |
| 46 | SdkCorePlatform |
Spandan Das | 0b555e3 | 2022-11-28 18:48:51 +0000 | [diff] [blame] | 47 | SdkIntraCore // API surface provided by one core module to another |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 48 | SdkPublic |
| 49 | SdkSystem |
| 50 | SdkTest |
| 51 | SdkModule |
| 52 | SdkSystemServer |
| 53 | SdkPrivate |
Spandan Das | 4ac2aed | 2022-12-28 01:54:29 +0000 | [diff] [blame] | 54 | SdkToolchain // API surface provided by ART to compile other API domains |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 55 | ) |
| 56 | |
| 57 | // String returns the string representation of this SdkKind |
| 58 | func (k SdkKind) String() string { |
| 59 | switch k { |
| 60 | case SdkPrivate: |
| 61 | return "private" |
| 62 | case SdkNone: |
| 63 | return "none" |
| 64 | case SdkPublic: |
| 65 | return "public" |
| 66 | case SdkSystem: |
| 67 | return "system" |
| 68 | case SdkTest: |
| 69 | return "test" |
| 70 | case SdkCore: |
| 71 | return "core" |
| 72 | case SdkCorePlatform: |
| 73 | return "core_platform" |
Spandan Das | 0b555e3 | 2022-11-28 18:48:51 +0000 | [diff] [blame] | 74 | case SdkIntraCore: |
| 75 | return "intracore" |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 76 | case SdkModule: |
| 77 | return "module-lib" |
| 78 | case SdkSystemServer: |
| 79 | return "system-server" |
Spandan Das | 4ac2aed | 2022-12-28 01:54:29 +0000 | [diff] [blame] | 80 | case SdkToolchain: |
| 81 | return "toolchain" |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 82 | default: |
| 83 | return "invalid" |
| 84 | } |
| 85 | } |
| 86 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 87 | // SdkSpec represents the kind and the version of an SDK for a module to build against |
| 88 | type SdkSpec struct { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 89 | Kind SdkKind |
| 90 | ApiLevel ApiLevel |
| 91 | Raw string |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func (s SdkSpec) String() string { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 95 | return fmt.Sprintf("%s_%s", s.Kind, s.ApiLevel) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Valid checks if this SdkSpec is well-formed. Note however that true doesn't mean that the |
| 99 | // specified SDK actually exists. |
| 100 | func (s SdkSpec) Valid() bool { |
| 101 | return s.Kind != SdkInvalid |
| 102 | } |
| 103 | |
| 104 | // Specified checks if this SdkSpec is well-formed and is not "". |
| 105 | func (s SdkSpec) Specified() bool { |
| 106 | return s.Valid() && s.Kind != SdkPrivate |
| 107 | } |
| 108 | |
| 109 | // whether the API surface is managed and versioned, i.e. has .txt file that |
| 110 | // get frozen on SDK freeze and changes get reviewed by API council. |
| 111 | func (s SdkSpec) Stable() bool { |
| 112 | if !s.Specified() { |
| 113 | return false |
| 114 | } |
| 115 | switch s.Kind { |
| 116 | case SdkNone: |
| 117 | // there is nothing to manage and version in this case; de facto stable API. |
| 118 | return true |
| 119 | case SdkCore, SdkPublic, SdkSystem, SdkModule, SdkSystemServer: |
| 120 | return true |
| 121 | case SdkCorePlatform, SdkTest, SdkPrivate: |
| 122 | return false |
| 123 | default: |
| 124 | panic(fmt.Errorf("unknown SdkKind=%v", s.Kind)) |
| 125 | } |
| 126 | return false |
| 127 | } |
| 128 | |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 129 | // PrebuiltSdkAvailableForUnbundledBuild tells whether this SdkSpec can have a prebuilt SDK |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 130 | // that can be used for unbundled builds. |
| 131 | func (s SdkSpec) PrebuiltSdkAvailableForUnbundledBuild() bool { |
| 132 | // "", "none", and "core_platform" are not available for unbundled build |
| 133 | // as we don't/can't have prebuilt stub for the versions |
| 134 | return s.Kind != SdkPrivate && s.Kind != SdkNone && s.Kind != SdkCorePlatform |
| 135 | } |
| 136 | |
| 137 | func (s SdkSpec) ForVendorPartition(ctx EarlyModuleContext) SdkSpec { |
| 138 | // If BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES has a numeric value, |
| 139 | // use it instead of "current" for the vendor partition. |
| 140 | currentSdkVersion := ctx.DeviceConfig().CurrentApiLevelForVendorModules() |
| 141 | if currentSdkVersion == "current" { |
| 142 | return s |
| 143 | } |
| 144 | |
| 145 | if s.Kind == SdkPublic || s.Kind == SdkSystem { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 146 | if s.ApiLevel.IsCurrent() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 147 | if i, err := strconv.Atoi(currentSdkVersion); err == nil { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 148 | apiLevel := uncheckedFinalApiLevel(i) |
| 149 | return SdkSpec{s.Kind, apiLevel, s.Raw} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 150 | } |
| 151 | panic(fmt.Errorf("BOARD_CURRENT_API_LEVEL_FOR_VENDOR_MODULES must be either \"current\" or a number, but was %q", currentSdkVersion)) |
| 152 | } |
| 153 | } |
| 154 | return s |
| 155 | } |
| 156 | |
| 157 | // UsePrebuilt determines whether prebuilt SDK should be used for this SdkSpec with the given context. |
| 158 | func (s SdkSpec) UsePrebuilt(ctx EarlyModuleContext) bool { |
Jiyong Park | c702204 | 2021-04-15 16:53:05 +0900 | [diff] [blame] | 159 | switch s { |
| 160 | case SdkSpecNone, SdkSpecCorePlatform, SdkSpecPrivate: |
| 161 | return false |
| 162 | } |
| 163 | |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 164 | if s.ApiLevel.IsCurrent() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 165 | // "current" can be built from source and be from prebuilt SDK |
| 166 | return ctx.Config().AlwaysUsePrebuiltSdks() |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 167 | } else if !s.ApiLevel.IsPreview() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 168 | // validation check |
Paul Duffin | 12e311d | 2021-10-28 17:42:16 +0100 | [diff] [blame] | 169 | if s.Kind != SdkPublic && s.Kind != SdkSystem && s.Kind != SdkTest && s.Kind != SdkModule && s.Kind != SdkSystemServer { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 170 | panic(fmt.Errorf("prebuilt SDK is not not available for SdkKind=%q", s.Kind)) |
| 171 | return false |
| 172 | } |
| 173 | // numbered SDKs are always from prebuilt |
| 174 | return true |
| 175 | } |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 176 | return false |
| 177 | } |
| 178 | |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 179 | // EffectiveVersion converts an SdkSpec into the concrete ApiLevel that the module should use. For |
| 180 | // modules targeting an unreleased SDK (meaning it does not yet have a number) it returns |
| 181 | // FutureApiLevel(10000). |
| 182 | func (s SdkSpec) EffectiveVersion(ctx EarlyModuleContext) (ApiLevel, error) { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 183 | if !s.Valid() { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 184 | return s.ApiLevel, fmt.Errorf("invalid sdk version %q", s.Raw) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if ctx.DeviceSpecific() || ctx.SocSpecific() { |
| 188 | s = s.ForVendorPartition(ctx) |
| 189 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 190 | if !s.ApiLevel.IsPreview() { |
| 191 | return s.ApiLevel, nil |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 192 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 193 | ret := ctx.Config().DefaultAppTargetSdk(ctx) |
| 194 | if ret.IsPreview() { |
| 195 | return FutureApiLevel, nil |
| 196 | } |
| 197 | return ret, nil |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // EffectiveVersionString converts an SdkSpec into the concrete version string that the module |
| 201 | // should use. For modules targeting an unreleased SDK (meaning it does not yet have a number) |
| 202 | // it returns the codename (P, Q, R, etc.) |
| 203 | func (s SdkSpec) EffectiveVersionString(ctx EarlyModuleContext) (string, error) { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 204 | if !s.Valid() { |
| 205 | return s.ApiLevel.String(), fmt.Errorf("invalid sdk version %q", s.Raw) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 206 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 207 | |
| 208 | if ctx.DeviceSpecific() || ctx.SocSpecific() { |
| 209 | s = s.ForVendorPartition(ctx) |
| 210 | } |
| 211 | if !s.ApiLevel.IsPreview() { |
| 212 | return s.ApiLevel.String(), nil |
| 213 | } |
Spandan Das | ffb31af | 2023-03-01 19:46:18 +0000 | [diff] [blame] | 214 | // Determine the default sdk |
| 215 | ret := ctx.Config().DefaultAppTargetSdk(ctx) |
| 216 | if !ret.IsPreview() { |
| 217 | // If the default sdk has been finalized, return that |
| 218 | return ret.String(), nil |
| 219 | } |
| 220 | // There can be more than one active in-development sdks |
| 221 | // If an app is targeting an active sdk, but not the default one, return the requested active sdk. |
| 222 | // e.g. |
| 223 | // SETUP |
| 224 | // In-development: UpsideDownCake, VanillaIceCream |
| 225 | // Default: VanillaIceCream |
| 226 | // Android.bp |
| 227 | // min_sdk_version: `UpsideDownCake` |
| 228 | // RETURN |
| 229 | // UpsideDownCake and not VanillaIceCream |
| 230 | for _, preview := range ctx.Config().PreviewApiLevels() { |
| 231 | if s.ApiLevel.String() == preview.String() { |
| 232 | return preview.String(), nil |
| 233 | } |
| 234 | } |
| 235 | // Otherwise return the default one |
| 236 | return ret.String(), nil |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 237 | } |
| 238 | |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 239 | var ( |
Jiyong Park | c702204 | 2021-04-15 16:53:05 +0900 | [diff] [blame] | 240 | SdkSpecNone = SdkSpec{SdkNone, NoneApiLevel, "(no version)"} |
| 241 | SdkSpecPrivate = SdkSpec{SdkPrivate, FutureApiLevel, ""} |
| 242 | SdkSpecCorePlatform = SdkSpec{SdkCorePlatform, FutureApiLevel, "core_platform"} |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 243 | ) |
| 244 | |
| 245 | func SdkSpecFrom(ctx EarlyModuleContext, str string) SdkSpec { |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 246 | return SdkSpecFromWithConfig(ctx.Config(), str) |
| 247 | } |
| 248 | |
| 249 | func SdkSpecFromWithConfig(config Config, str string) SdkSpec { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 250 | switch str { |
| 251 | // special cases first |
| 252 | case "": |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 253 | return SdkSpecPrivate |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 254 | case "none": |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 255 | return SdkSpecNone |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 256 | case "core_platform": |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 257 | return SdkSpecCorePlatform |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 258 | default: |
| 259 | // the syntax is [kind_]version |
| 260 | sep := strings.LastIndex(str, "_") |
| 261 | |
| 262 | var kindString string |
| 263 | if sep == 0 { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 264 | return SdkSpec{SdkInvalid, NoneApiLevel, str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 265 | } else if sep == -1 { |
| 266 | kindString = "" |
| 267 | } else { |
| 268 | kindString = str[0:sep] |
| 269 | } |
| 270 | versionString := str[sep+1 : len(str)] |
| 271 | |
| 272 | var kind SdkKind |
| 273 | switch kindString { |
| 274 | case "": |
| 275 | kind = SdkPublic |
| 276 | case "core": |
| 277 | kind = SdkCore |
| 278 | case "system": |
| 279 | kind = SdkSystem |
| 280 | case "test": |
| 281 | kind = SdkTest |
| 282 | case "module": |
| 283 | kind = SdkModule |
| 284 | case "system_server": |
| 285 | kind = SdkSystemServer |
| 286 | default: |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 287 | return SdkSpec{SdkInvalid, NoneApiLevel, str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 288 | } |
| 289 | |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 290 | apiLevel, err := ApiLevelFromUserWithConfig(config, versionString) |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 291 | if err != nil { |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 292 | return SdkSpec{SdkInvalid, apiLevel, str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 293 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 294 | return SdkSpec{kind, apiLevel, str} |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | func (s SdkSpec) ValidateSystemSdk(ctx EarlyModuleContext) bool { |
| 299 | // Ensures that the specified system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor/product Java module) |
| 300 | // Assuming that BOARD_SYSTEMSDK_VERSIONS := 28 29, |
| 301 | // sdk_version of the modules in vendor/product that use system sdk must be either system_28, system_29 or system_current |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 302 | if s.Kind != SdkSystem || s.ApiLevel.IsPreview() { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 303 | return true |
| 304 | } |
| 305 | allowedVersions := ctx.DeviceConfig().PlatformSystemSdkVersions() |
| 306 | if ctx.DeviceSpecific() || ctx.SocSpecific() || (ctx.ProductSpecific() && ctx.Config().EnforceProductPartitionInterface()) { |
| 307 | systemSdkVersions := ctx.DeviceConfig().SystemSdkVersions() |
| 308 | if len(systemSdkVersions) > 0 { |
| 309 | allowedVersions = systemSdkVersions |
| 310 | } |
| 311 | } |
Jiyong Park | 54105c4 | 2021-03-31 18:17:53 +0900 | [diff] [blame] | 312 | if len(allowedVersions) > 0 && !InList(s.ApiLevel.String(), allowedVersions) { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 313 | ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q", |
| 314 | s.Raw, allowedVersions) |
| 315 | return false |
| 316 | } |
| 317 | return true |
| 318 | } |