Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -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 android |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
Jooyung Han | 29e91d2 | 2020-04-02 01:41:41 +0900 | [diff] [blame] | 19 | "fmt" |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 20 | "strconv" |
Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 21 | "strings" |
Yu Liu | 619b6ef | 2025-03-05 18:35:17 +0000 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint/gobtools" |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func init() { |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 27 | RegisterParallelSingletonType("api_levels", ApiLevelsSingleton) |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Jooyung Han | 11b0fbd | 2021-02-05 02:28:22 +0900 | [diff] [blame] | 30 | const previewAPILevelBase = 9000 |
| 31 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 32 | // An API level, which may be a finalized (numbered) API, a preview (codenamed) |
| 33 | // API, or the future API level (10000). Can be parsed from a string with |
| 34 | // ApiLevelFromUser or ApiLevelOrPanic. |
| 35 | // |
| 36 | // The different *types* of API levels are handled separately. Currently only |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 37 | // Java has these, and they're managed with the SdkKind enum of the SdkSpec. A |
| 38 | // future cleanup should be to migrate SdkSpec to using ApiLevel instead of its |
| 39 | // SdkVersion int, and to move SdkSpec into this package. |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 40 | type ApiLevel struct { |
| 41 | // The string representation of the API level. |
| 42 | value string |
| 43 | |
| 44 | // A number associated with the API level. The exact value depends on |
| 45 | // whether this API level is a preview or final API. |
| 46 | // |
| 47 | // For final API levels, this is the assigned version number. |
| 48 | // |
| 49 | // For preview API levels, this value has no meaning except to index known |
| 50 | // previews to determine ordering. |
| 51 | number int |
| 52 | |
| 53 | // Identifies this API level as either a preview or final API level. |
| 54 | isPreview bool |
| 55 | } |
| 56 | |
Yu Liu | 619b6ef | 2025-03-05 18:35:17 +0000 | [diff] [blame] | 57 | type apiLevelGob struct { |
| 58 | Value string |
| 59 | Number int |
| 60 | IsPreview bool |
| 61 | } |
| 62 | |
| 63 | func (a *ApiLevel) ToGob() *apiLevelGob { |
| 64 | return &apiLevelGob{ |
| 65 | Value: a.value, |
| 66 | Number: a.number, |
| 67 | IsPreview: a.isPreview, |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func (a *ApiLevel) FromGob(data *apiLevelGob) { |
| 72 | a.value = data.Value |
| 73 | a.number = data.Number |
| 74 | a.isPreview = data.IsPreview |
| 75 | } |
| 76 | |
| 77 | func (a ApiLevel) GobEncode() ([]byte, error) { |
| 78 | return gobtools.CustomGobEncode[apiLevelGob](&a) |
| 79 | } |
| 80 | |
| 81 | func (a *ApiLevel) GobDecode(data []byte) error { |
| 82 | return gobtools.CustomGobDecode[apiLevelGob](data, a) |
| 83 | } |
| 84 | |
Cole Faust | e5bf3fb | 2022-07-01 19:39:14 +0000 | [diff] [blame] | 85 | func (this ApiLevel) FinalInt() int { |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame] | 86 | if this.IsInvalid() { |
| 87 | panic(fmt.Errorf("%v is not a recognized api_level\n", this)) |
| 88 | } |
Cole Faust | e5bf3fb | 2022-07-01 19:39:14 +0000 | [diff] [blame] | 89 | if this.IsPreview() { |
| 90 | panic("Requested a final int from a non-final ApiLevel") |
| 91 | } else { |
| 92 | return this.number |
| 93 | } |
| 94 | } |
| 95 | |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 96 | func (this ApiLevel) FinalOrFutureInt() int { |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame] | 97 | if this.IsInvalid() { |
| 98 | panic(fmt.Errorf("%v is not a recognized api_level\n", this)) |
| 99 | } |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 100 | if this.IsPreview() { |
Dan Albert | 0b176c8 | 2020-07-23 16:43:25 -0700 | [diff] [blame] | 101 | return FutureApiLevelInt |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 102 | } else { |
| 103 | return this.number |
| 104 | } |
| 105 | } |
| 106 | |
Jooyung Han | 11b0fbd | 2021-02-05 02:28:22 +0900 | [diff] [blame] | 107 | // FinalOrPreviewInt distinguishes preview versions from "current" (future). |
| 108 | // This is for "native" stubs and should be in sync with ndkstubgen/getApiLevelsMap(). |
| 109 | // - "current" -> future (10000) |
| 110 | // - preview codenames -> preview base (9000) + index |
| 111 | // - otherwise -> cast to int |
| 112 | func (this ApiLevel) FinalOrPreviewInt() int { |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame] | 113 | if this.IsInvalid() { |
| 114 | panic(fmt.Errorf("%v is not a recognized api_level\n", this)) |
| 115 | } |
Jooyung Han | 11b0fbd | 2021-02-05 02:28:22 +0900 | [diff] [blame] | 116 | if this.IsCurrent() { |
| 117 | return this.number |
| 118 | } |
| 119 | if this.IsPreview() { |
| 120 | return previewAPILevelBase + this.number |
| 121 | } |
| 122 | return this.number |
| 123 | } |
| 124 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 125 | // Returns the canonical name for this API level. For a finalized API level |
| 126 | // this will be the API number as a string. For a preview API level this |
| 127 | // will be the codename, or "current". |
| 128 | func (this ApiLevel) String() string { |
| 129 | return this.value |
| 130 | } |
| 131 | |
| 132 | // Returns true if this is a non-final API level. |
| 133 | func (this ApiLevel) IsPreview() bool { |
| 134 | return this.isPreview |
| 135 | } |
| 136 | |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame] | 137 | // Returns true if the raw api level string is invalid |
| 138 | func (this ApiLevel) IsInvalid() bool { |
| 139 | return this.EqualTo(InvalidApiLevel) |
| 140 | } |
| 141 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 142 | // Returns true if this is the unfinalized "current" API level. This means |
| 143 | // different things across Java and native. Java APIs do not use explicit |
| 144 | // codenames, so all non-final codenames are grouped into "current". For native |
| 145 | // explicit codenames are typically used, and current is the union of all |
| 146 | // non-final APIs, including those that may not yet be in any codename. |
| 147 | // |
| 148 | // Note that in a build where the platform is final, "current" will not be a |
| 149 | // preview API level but will instead be canonicalized to the final API level. |
| 150 | func (this ApiLevel) IsCurrent() bool { |
| 151 | return this.value == "current" |
| 152 | } |
| 153 | |
Jooyung Han | ed124c3 | 2021-01-26 11:43:46 +0900 | [diff] [blame] | 154 | func (this ApiLevel) IsNone() bool { |
| 155 | return this.number == -1 |
| 156 | } |
| 157 | |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame] | 158 | // Returns true if an app is compiling against private apis. |
| 159 | // e.g. if sdk_version = "" in Android.bp, then the ApiLevel of that "sdk" is at PrivateApiLevel. |
| 160 | func (this ApiLevel) IsPrivate() bool { |
| 161 | return this.number == PrivateApiLevel.number |
| 162 | } |
| 163 | |
Spandan Das | dd7057c | 2023-01-05 01:03:47 +0000 | [diff] [blame] | 164 | // EffectiveVersion converts an ApiLevel into the concrete ApiLevel that the module should use. For |
| 165 | // modules targeting an unreleased SDK (meaning it does not yet have a number) it returns |
| 166 | // FutureApiLevel(10000). |
| 167 | func (l ApiLevel) EffectiveVersion(ctx EarlyModuleContext) (ApiLevel, error) { |
| 168 | if l.EqualTo(InvalidApiLevel) { |
| 169 | return l, fmt.Errorf("invalid version in sdk_version %q", l.value) |
| 170 | } |
| 171 | if !l.IsPreview() { |
| 172 | return l, nil |
| 173 | } |
| 174 | ret := ctx.Config().DefaultAppTargetSdk(ctx) |
| 175 | if ret.IsPreview() { |
| 176 | return FutureApiLevel, nil |
| 177 | } |
| 178 | return ret, nil |
| 179 | } |
| 180 | |
| 181 | // EffectiveVersionString converts an SdkSpec into the concrete version string that the module |
| 182 | // should use. For modules targeting an unreleased SDK (meaning it does not yet have a number) |
| 183 | // it returns the codename (P, Q, R, etc.) |
| 184 | func (l ApiLevel) EffectiveVersionString(ctx EarlyModuleContext) (string, error) { |
| 185 | if l.EqualTo(InvalidApiLevel) { |
| 186 | return l.value, fmt.Errorf("invalid version in sdk_version %q", l.value) |
| 187 | } |
| 188 | if !l.IsPreview() { |
| 189 | return l.String(), nil |
| 190 | } |
| 191 | // Determine the default sdk |
| 192 | ret := ctx.Config().DefaultAppTargetSdk(ctx) |
| 193 | if !ret.IsPreview() { |
| 194 | // If the default sdk has been finalized, return that |
| 195 | return ret.String(), nil |
| 196 | } |
| 197 | // There can be more than one active in-development sdks |
| 198 | // If an app is targeting an active sdk, but not the default one, return the requested active sdk. |
| 199 | // e.g. |
| 200 | // SETUP |
| 201 | // In-development: UpsideDownCake, VanillaIceCream |
| 202 | // Default: VanillaIceCream |
| 203 | // Android.bp |
| 204 | // min_sdk_version: `UpsideDownCake` |
| 205 | // RETURN |
| 206 | // UpsideDownCake and not VanillaIceCream |
| 207 | for _, preview := range ctx.Config().PreviewApiLevels() { |
| 208 | if l.String() == preview.String() { |
| 209 | return preview.String(), nil |
| 210 | } |
| 211 | } |
| 212 | // Otherwise return the default one |
| 213 | return ret.String(), nil |
| 214 | } |
| 215 | |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 216 | // Specified returns true if the module is targeting a recognzized api_level. |
| 217 | // It returns false if either |
| 218 | // 1. min_sdk_version is not an int or a recognized codename |
| 219 | // 2. both min_sdk_version and sdk_version are empty. In this case, MinSdkVersion() defaults to SdkSpecPrivate.ApiLevel |
| 220 | func (this ApiLevel) Specified() bool { |
| 221 | return !this.IsInvalid() && !this.IsPrivate() |
| 222 | } |
| 223 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 224 | // Returns -1 if the current API level is less than the argument, 0 if they |
| 225 | // are equal, and 1 if it is greater than the argument. |
| 226 | func (this ApiLevel) CompareTo(other ApiLevel) int { |
| 227 | if this.IsPreview() && !other.IsPreview() { |
| 228 | return 1 |
| 229 | } else if !this.IsPreview() && other.IsPreview() { |
| 230 | return -1 |
| 231 | } |
| 232 | |
| 233 | if this.number < other.number { |
| 234 | return -1 |
| 235 | } else if this.number == other.number { |
| 236 | return 0 |
| 237 | } else { |
| 238 | return 1 |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | func (this ApiLevel) EqualTo(other ApiLevel) bool { |
| 243 | return this.CompareTo(other) == 0 |
| 244 | } |
| 245 | |
| 246 | func (this ApiLevel) GreaterThan(other ApiLevel) bool { |
| 247 | return this.CompareTo(other) > 0 |
| 248 | } |
| 249 | |
| 250 | func (this ApiLevel) GreaterThanOrEqualTo(other ApiLevel) bool { |
| 251 | return this.CompareTo(other) >= 0 |
| 252 | } |
| 253 | |
| 254 | func (this ApiLevel) LessThan(other ApiLevel) bool { |
| 255 | return this.CompareTo(other) < 0 |
| 256 | } |
| 257 | |
| 258 | func (this ApiLevel) LessThanOrEqualTo(other ApiLevel) bool { |
| 259 | return this.CompareTo(other) <= 0 |
| 260 | } |
| 261 | |
| 262 | func uncheckedFinalApiLevel(num int) ApiLevel { |
| 263 | return ApiLevel{ |
| 264 | value: strconv.Itoa(num), |
| 265 | number: num, |
| 266 | isPreview: false, |
| 267 | } |
| 268 | } |
| 269 | |
Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 270 | func uncheckedFinalIncrementalApiLevel(num int, increment int) ApiLevel { |
| 271 | return ApiLevel{ |
| 272 | value: strconv.Itoa(num) + "." + strconv.Itoa(increment), |
| 273 | number: num, |
| 274 | isPreview: false, |
| 275 | } |
| 276 | } |
| 277 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 278 | var NoneApiLevel = ApiLevel{ |
| 279 | value: "(no version)", |
| 280 | // Not 0 because we don't want this to compare equal with the first preview. |
| 281 | number: -1, |
| 282 | isPreview: true, |
| 283 | } |
| 284 | |
Yu Liu | df0b839 | 2025-02-12 18:27:03 +0000 | [diff] [blame] | 285 | // A special ApiLevel that all modules should at least support. |
| 286 | var MinApiLevel = ApiLevel{number: 1} |
| 287 | |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame] | 288 | // Sentinel ApiLevel to validate that an apiLevel is either an int or a recognized codename. |
| 289 | var InvalidApiLevel = NewInvalidApiLevel("invalid") |
| 290 | |
| 291 | // Returns an apiLevel object at the same level as InvalidApiLevel. |
| 292 | // The object contains the raw string provied in bp file, and can be used for error handling. |
| 293 | func NewInvalidApiLevel(raw string) ApiLevel { |
| 294 | return ApiLevel{ |
| 295 | value: raw, |
| 296 | number: -2, // One less than NoneApiLevel |
| 297 | isPreview: true, |
| 298 | } |
| 299 | } |
| 300 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 301 | // The first version that introduced 64-bit ABIs. |
| 302 | var FirstLp64Version = uncheckedFinalApiLevel(21) |
| 303 | |
Elliott Hughes | 0e9cdb0 | 2021-05-14 13:07:32 -0700 | [diff] [blame] | 304 | // Android has had various kinds of packed relocations over the years |
| 305 | // (http://b/187907243). |
| 306 | // |
| 307 | // API level 30 is where the now-standard SHT_RELR is available. |
| 308 | var FirstShtRelrVersion = uncheckedFinalApiLevel(30) |
| 309 | |
| 310 | // API level 28 introduced SHT_RELR when it was still Android-only, and used an |
| 311 | // Android-specific relocation. |
| 312 | var FirstAndroidRelrVersion = uncheckedFinalApiLevel(28) |
| 313 | |
| 314 | // API level 23 was when we first had the Chrome relocation packer, which is |
| 315 | // obsolete and has been removed, but lld can now generate compatible packed |
| 316 | // relocations itself. |
| 317 | var FirstPackedRelocationsVersion = uncheckedFinalApiLevel(23) |
| 318 | |
Paul Duffin | 004547f | 2021-10-29 13:50:24 +0100 | [diff] [blame] | 319 | // LastWithoutModuleLibCoreSystemModules is the last API level where prebuilts/sdk does not contain |
| 320 | // a core-for-system-modules.jar for the module-lib API scope. |
| 321 | var LastWithoutModuleLibCoreSystemModules = uncheckedFinalApiLevel(31) |
| 322 | |
Spandan Das | 38c64f6 | 2024-02-12 15:00:15 +0000 | [diff] [blame] | 323 | var ApiLevelR = uncheckedFinalApiLevel(30) |
| 324 | |
Spandan Das | a5e26d3 | 2024-03-06 14:04:36 +0000 | [diff] [blame] | 325 | var ApiLevelUpsideDownCake = uncheckedFinalApiLevel(34) |
| 326 | |
Jihoon Kang | 98aa8fa | 2024-06-07 11:06:57 +0000 | [diff] [blame] | 327 | var ApiLevelVanillaIceCream = uncheckedFinalApiLevel(35) |
| 328 | |
Vinh Tran | f192474 | 2022-06-24 16:40:11 -0400 | [diff] [blame] | 329 | // ReplaceFinalizedCodenames returns the API level number associated with that API level |
| 330 | // if the `raw` input is the codename of an API level has been finalized. |
| 331 | // If the input is *not* a finalized codename, the input is returned unmodified. |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 332 | func ReplaceFinalizedCodenames(config Config, raw string) (string, error) { |
| 333 | finalCodenamesMap, err := getFinalCodenamesMap(config) |
| 334 | if err != nil { |
| 335 | return raw, err |
| 336 | } |
| 337 | num, ok := finalCodenamesMap[raw] |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 338 | if !ok { |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 339 | return raw, nil |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 342 | return strconv.Itoa(num), nil |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 345 | // ApiLevelFrom converts the given string `raw` to an ApiLevel. |
| 346 | // If `raw` is invalid (empty string, unrecognized codename etc.) it returns an invalid ApiLevel |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 347 | func ApiLevelFrom(ctx ConfigContext, raw string) ApiLevel { |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 348 | ret, err := ApiLevelFromUser(ctx, raw) |
| 349 | if err != nil { |
| 350 | return NewInvalidApiLevel(raw) |
| 351 | } |
| 352 | return ret |
| 353 | } |
| 354 | |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 355 | // ApiLevelFromUser converts the given string `raw` to an ApiLevel, possibly returning an error. |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 356 | // |
| 357 | // `raw` must be non-empty. Passing an empty string results in a panic. |
| 358 | // |
| 359 | // "current" will return CurrentApiLevel, which is the ApiLevel associated with |
| 360 | // an arbitrary future release (often referred to as API level 10000). |
| 361 | // |
| 362 | // Finalized codenames will be interpreted as their final API levels, not the |
| 363 | // preview of the associated releases. R is now API 30, not the R preview. |
| 364 | // |
| 365 | // Future codenames return a preview API level that has no associated integer. |
| 366 | // |
| 367 | // Inputs that are not "current", known previews, or convertible to an integer |
| 368 | // will return an error. |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 369 | func ApiLevelFromUser(ctx ConfigContext, raw string) (ApiLevel, error) { |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 370 | return ApiLevelFromUserWithConfig(ctx.Config(), raw) |
| 371 | } |
| 372 | |
| 373 | // ApiLevelFromUserWithConfig implements ApiLevelFromUser, see comments for |
| 374 | // ApiLevelFromUser for more details. |
| 375 | func ApiLevelFromUserWithConfig(config Config, raw string) (ApiLevel, error) { |
Alix | fb7f7b9 | 2023-03-02 19:35:02 +0000 | [diff] [blame] | 376 | // This logic is replicated in starlark, if changing logic here update starlark code too |
Elliott Hughes | 1036316 | 2024-01-09 22:02:03 +0000 | [diff] [blame] | 377 | // https://cs.android.com/android/platform/superproject/+/main:build/bazel/rules/common/api.bzl;l=42;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061 |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 378 | if raw == "" { |
| 379 | panic("API level string must be non-empty") |
| 380 | } |
| 381 | |
| 382 | if raw == "current" { |
Dan Albert | 0b176c8 | 2020-07-23 16:43:25 -0700 | [diff] [blame] | 383 | return FutureApiLevel, nil |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 384 | } |
| 385 | |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 386 | for _, preview := range config.PreviewApiLevels() { |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 387 | if raw == preview.String() { |
| 388 | return preview, nil |
| 389 | } |
| 390 | } |
| 391 | |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 392 | apiLevelsReleasedVersions, err := getApiLevelsMapReleasedVersions() |
| 393 | if err != nil { |
| 394 | return NoneApiLevel, err |
| 395 | } |
| 396 | canonical, ok := apiLevelsReleasedVersions[raw] |
Alix | fb50251 | 2023-03-06 21:04:30 +0000 | [diff] [blame] | 397 | if !ok { |
| 398 | asInt, err := strconv.Atoi(raw) |
| 399 | if err != nil { |
| 400 | return NoneApiLevel, fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", raw) |
| 401 | } |
| 402 | return uncheckedFinalApiLevel(asInt), nil |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Alix | fb50251 | 2023-03-06 21:04:30 +0000 | [diff] [blame] | 405 | return uncheckedFinalApiLevel(canonical), nil |
| 406 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Paul Duffin | 004547f | 2021-10-29 13:50:24 +0100 | [diff] [blame] | 409 | // ApiLevelForTest returns an ApiLevel constructed from the supplied raw string. |
| 410 | // |
| 411 | // This only supports "current" and numeric levels, code names are not supported. |
| 412 | func ApiLevelForTest(raw string) ApiLevel { |
| 413 | if raw == "" { |
| 414 | panic("API level string must be non-empty") |
| 415 | } |
| 416 | |
| 417 | if raw == "current" { |
| 418 | return FutureApiLevel |
| 419 | } |
| 420 | |
Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 421 | if strings.Contains(raw, ".") { |
| 422 | // Check prebuilt incremental API format MM.m for major (API level) and minor (incremental) revisions |
| 423 | parts := strings.Split(raw, ".") |
| 424 | if len(parts) != 2 { |
| 425 | panic(fmt.Errorf("Found unexpected version '%s' for incremental API - expect MM.m format for incremental API with both major (MM) an minor (m) revision.", raw)) |
| 426 | } |
| 427 | sdk, sdk_err := strconv.Atoi(parts[0]) |
| 428 | qpr, qpr_err := strconv.Atoi(parts[1]) |
| 429 | if sdk_err != nil || qpr_err != nil { |
| 430 | panic(fmt.Errorf("Unable to read version number for incremental api '%s'", raw)) |
| 431 | } |
| 432 | |
| 433 | apiLevel := uncheckedFinalIncrementalApiLevel(sdk, qpr) |
| 434 | return apiLevel |
| 435 | } |
| 436 | |
Paul Duffin | 004547f | 2021-10-29 13:50:24 +0100 | [diff] [blame] | 437 | asInt, err := strconv.Atoi(raw) |
| 438 | if err != nil { |
| 439 | panic(fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", raw)) |
| 440 | } |
| 441 | |
| 442 | apiLevel := uncheckedFinalApiLevel(asInt) |
| 443 | return apiLevel |
| 444 | } |
| 445 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 446 | // Converts an API level string `raw` into an ApiLevel in the same method as |
| 447 | // `ApiLevelFromUser`, but the input is assumed to have no errors and any errors |
| 448 | // will panic instead of returning an error. |
Colin Cross | 1cea530 | 2024-12-03 16:40:08 -0800 | [diff] [blame] | 449 | func ApiLevelOrPanic(ctx ConfigContext, raw string) ApiLevel { |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 450 | value, err := ApiLevelFromUser(ctx, raw) |
| 451 | if err != nil { |
| 452 | panic(err.Error()) |
| 453 | } |
| 454 | return value |
| 455 | } |
| 456 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 457 | func ApiLevelsSingleton() Singleton { |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 458 | return &apiLevelsSingleton{} |
| 459 | } |
| 460 | |
| 461 | type apiLevelsSingleton struct{} |
| 462 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 463 | func createApiLevelsJson(ctx SingletonContext, file WritablePath, |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 464 | apiLevelsMap map[string]int) { |
| 465 | |
| 466 | jsonStr, err := json.Marshal(apiLevelsMap) |
| 467 | if err != nil { |
| 468 | ctx.Errorf(err.Error()) |
| 469 | } |
| 470 | |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 471 | WriteFileRule(ctx, file, string(jsonStr)) |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 474 | func GetApiLevelsJson(ctx PathContext) WritablePath { |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 475 | return PathForOutput(ctx, "api_levels.json") |
| 476 | } |
| 477 | |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 478 | func getApiLevelsMapReleasedVersions() (map[string]int, error) { |
Cole Faust | 37bfb07 | 2024-03-06 15:50:53 -0800 | [diff] [blame] | 479 | return map[string]int{ |
Michael Wright | c91d0cd | 2024-04-23 22:50:41 +0000 | [diff] [blame] | 480 | "G": 9, |
| 481 | "I": 14, |
| 482 | "J": 16, |
| 483 | "J-MR1": 17, |
| 484 | "J-MR2": 18, |
| 485 | "K": 19, |
| 486 | "L": 21, |
| 487 | "L-MR1": 22, |
| 488 | "M": 23, |
| 489 | "N": 24, |
| 490 | "N-MR1": 25, |
| 491 | "O": 26, |
| 492 | "O-MR1": 27, |
| 493 | "P": 28, |
| 494 | "Q": 29, |
| 495 | "R": 30, |
| 496 | "S": 31, |
| 497 | "S-V2": 32, |
| 498 | "Tiramisu": 33, |
| 499 | "UpsideDownCake": 34, |
| 500 | "VanillaIceCream": 35, |
Linus Tufvesson | 367dac6 | 2025-02-18 06:35:50 -0800 | [diff] [blame] | 501 | "Baklava": 36, |
Cole Faust | 37bfb07 | 2024-03-06 15:50:53 -0800 | [diff] [blame] | 502 | }, nil |
Alix Espino | 4fd7e74 | 2023-02-24 14:46:43 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 505 | var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap") |
| 506 | |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 507 | func getFinalCodenamesMap(config Config) (map[string]int, error) { |
| 508 | type resultStruct struct { |
| 509 | result map[string]int |
| 510 | err error |
| 511 | } |
Alix | fb7f7b9 | 2023-03-02 19:35:02 +0000 | [diff] [blame] | 512 | // This logic is replicated in starlark, if changing logic here update starlark code too |
Elliott Hughes | 1036316 | 2024-01-09 22:02:03 +0000 | [diff] [blame] | 513 | // https://cs.android.com/android/platform/superproject/+/main:build/bazel/rules/common/api.bzl;l=30;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061 |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 514 | result := config.Once(finalCodenamesMapKey, func() interface{} { |
| 515 | apiLevelsMap, err := getApiLevelsMapReleasedVersions() |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 516 | |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 517 | // TODO: Differentiate "current" and "future". |
| 518 | // The code base calls it FutureApiLevel, but the spelling is "current", |
| 519 | // and these are really two different things. When defining APIs it |
| 520 | // means the API has not yet been added to a specific release. When |
| 521 | // choosing an API level to build for it means that the future API level |
| 522 | // should be used, except in the case where the build is finalized in |
| 523 | // which case the platform version should be used. This is *weird*, |
| 524 | // because in the circumstance where API foo was added in R and bar was |
| 525 | // added in S, both of these are usable when building for "current" when |
| 526 | // neither R nor S are final, but the S APIs stop being available in a |
| 527 | // final R build. |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 528 | if err == nil && Bool(config.productVariables.Platform_sdk_final) { |
Dan Albert | 4f378d7 | 2020-07-23 17:32:15 -0700 | [diff] [blame] | 529 | apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt() |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 532 | return resultStruct{apiLevelsMap, err} |
| 533 | }).(resultStruct) |
| 534 | return result.result, result.err |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 537 | var apiLevelsMapKey = NewOnceKey("ApiLevelsMap") |
| 538 | |
Alix Espino | 4fd7e74 | 2023-02-24 14:46:43 +0000 | [diff] [blame] | 539 | // ApiLevelsMap has entries for preview API levels |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 540 | func GetApiLevelsMap(config Config) (map[string]int, error) { |
| 541 | type resultStruct struct { |
| 542 | result map[string]int |
| 543 | err error |
| 544 | } |
Alix | fb7f7b9 | 2023-03-02 19:35:02 +0000 | [diff] [blame] | 545 | // This logic is replicated in starlark, if changing logic here update starlark code too |
Elliott Hughes | 1036316 | 2024-01-09 22:02:03 +0000 | [diff] [blame] | 546 | // https://cs.android.com/android/platform/superproject/+/main:build/bazel/rules/common/api.bzl;l=23;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061 |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 547 | result := config.Once(apiLevelsMapKey, func() interface{} { |
| 548 | apiLevelsMap, err := getApiLevelsMapReleasedVersions() |
| 549 | if err == nil { |
| 550 | for i, codename := range config.PlatformVersionAllPreviewCodenames() { |
| 551 | apiLevelsMap[codename] = previewAPILevelBase + i |
| 552 | } |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 553 | } |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 554 | |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 555 | return resultStruct{apiLevelsMap, err} |
| 556 | }).(resultStruct) |
| 557 | return result.result, result.err |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 560 | func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) { |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 561 | apiLevelsMap, err := GetApiLevelsMap(ctx.Config()) |
| 562 | if err != nil { |
| 563 | ctx.Errorf("%s\n", err) |
| 564 | return |
| 565 | } |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 566 | apiLevelsJson := GetApiLevelsJson(ctx) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 567 | createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap) |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 568 | } |