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