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" |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 21 | |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 22 | "android/soong/bazel" |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 23 | "android/soong/starlark_fmt" |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func init() { |
| 27 | RegisterSingletonType("api_levels", ApiLevelsSingleton) |
| 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 | |
Cole Faust | e5bf3fb | 2022-07-01 19:39:14 +0000 | [diff] [blame] | 57 | func (this ApiLevel) FinalInt() int { |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame^] | 58 | if this.IsInvalid() { |
| 59 | panic(fmt.Errorf("%v is not a recognized api_level\n", this)) |
| 60 | } |
Cole Faust | e5bf3fb | 2022-07-01 19:39:14 +0000 | [diff] [blame] | 61 | if this.IsPreview() { |
| 62 | panic("Requested a final int from a non-final ApiLevel") |
| 63 | } else { |
| 64 | return this.number |
| 65 | } |
| 66 | } |
| 67 | |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 68 | func (this ApiLevel) FinalOrFutureInt() int { |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame^] | 69 | if this.IsInvalid() { |
| 70 | panic(fmt.Errorf("%v is not a recognized api_level\n", this)) |
| 71 | } |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 72 | if this.IsPreview() { |
Dan Albert | 0b176c8 | 2020-07-23 16:43:25 -0700 | [diff] [blame] | 73 | return FutureApiLevelInt |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 74 | } else { |
| 75 | return this.number |
| 76 | } |
| 77 | } |
| 78 | |
Jooyung Han | 11b0fbd | 2021-02-05 02:28:22 +0900 | [diff] [blame] | 79 | // FinalOrPreviewInt distinguishes preview versions from "current" (future). |
| 80 | // This is for "native" stubs and should be in sync with ndkstubgen/getApiLevelsMap(). |
| 81 | // - "current" -> future (10000) |
| 82 | // - preview codenames -> preview base (9000) + index |
| 83 | // - otherwise -> cast to int |
| 84 | func (this ApiLevel) FinalOrPreviewInt() int { |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame^] | 85 | if this.IsInvalid() { |
| 86 | panic(fmt.Errorf("%v is not a recognized api_level\n", this)) |
| 87 | } |
Jooyung Han | 11b0fbd | 2021-02-05 02:28:22 +0900 | [diff] [blame] | 88 | if this.IsCurrent() { |
| 89 | return this.number |
| 90 | } |
| 91 | if this.IsPreview() { |
| 92 | return previewAPILevelBase + this.number |
| 93 | } |
| 94 | return this.number |
| 95 | } |
| 96 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 97 | // Returns the canonical name for this API level. For a finalized API level |
| 98 | // this will be the API number as a string. For a preview API level this |
| 99 | // will be the codename, or "current". |
| 100 | func (this ApiLevel) String() string { |
| 101 | return this.value |
| 102 | } |
| 103 | |
| 104 | // Returns true if this is a non-final API level. |
| 105 | func (this ApiLevel) IsPreview() bool { |
| 106 | return this.isPreview |
| 107 | } |
| 108 | |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame^] | 109 | // Returns true if the raw api level string is invalid |
| 110 | func (this ApiLevel) IsInvalid() bool { |
| 111 | return this.EqualTo(InvalidApiLevel) |
| 112 | } |
| 113 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 114 | // Returns true if this is the unfinalized "current" API level. This means |
| 115 | // different things across Java and native. Java APIs do not use explicit |
| 116 | // codenames, so all non-final codenames are grouped into "current". For native |
| 117 | // explicit codenames are typically used, and current is the union of all |
| 118 | // non-final APIs, including those that may not yet be in any codename. |
| 119 | // |
| 120 | // Note that in a build where the platform is final, "current" will not be a |
| 121 | // preview API level but will instead be canonicalized to the final API level. |
| 122 | func (this ApiLevel) IsCurrent() bool { |
| 123 | return this.value == "current" |
| 124 | } |
| 125 | |
Jooyung Han | ed124c3 | 2021-01-26 11:43:46 +0900 | [diff] [blame] | 126 | func (this ApiLevel) IsNone() bool { |
| 127 | return this.number == -1 |
| 128 | } |
| 129 | |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame^] | 130 | // Returns true if an app is compiling against private apis. |
| 131 | // e.g. if sdk_version = "" in Android.bp, then the ApiLevel of that "sdk" is at PrivateApiLevel. |
| 132 | func (this ApiLevel) IsPrivate() bool { |
| 133 | return this.number == PrivateApiLevel.number |
| 134 | } |
| 135 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 136 | // Returns -1 if the current API level is less than the argument, 0 if they |
| 137 | // are equal, and 1 if it is greater than the argument. |
| 138 | func (this ApiLevel) CompareTo(other ApiLevel) int { |
| 139 | if this.IsPreview() && !other.IsPreview() { |
| 140 | return 1 |
| 141 | } else if !this.IsPreview() && other.IsPreview() { |
| 142 | return -1 |
| 143 | } |
| 144 | |
| 145 | if this.number < other.number { |
| 146 | return -1 |
| 147 | } else if this.number == other.number { |
| 148 | return 0 |
| 149 | } else { |
| 150 | return 1 |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func (this ApiLevel) EqualTo(other ApiLevel) bool { |
| 155 | return this.CompareTo(other) == 0 |
| 156 | } |
| 157 | |
| 158 | func (this ApiLevel) GreaterThan(other ApiLevel) bool { |
| 159 | return this.CompareTo(other) > 0 |
| 160 | } |
| 161 | |
| 162 | func (this ApiLevel) GreaterThanOrEqualTo(other ApiLevel) bool { |
| 163 | return this.CompareTo(other) >= 0 |
| 164 | } |
| 165 | |
| 166 | func (this ApiLevel) LessThan(other ApiLevel) bool { |
| 167 | return this.CompareTo(other) < 0 |
| 168 | } |
| 169 | |
| 170 | func (this ApiLevel) LessThanOrEqualTo(other ApiLevel) bool { |
| 171 | return this.CompareTo(other) <= 0 |
| 172 | } |
| 173 | |
| 174 | func uncheckedFinalApiLevel(num int) ApiLevel { |
| 175 | return ApiLevel{ |
| 176 | value: strconv.Itoa(num), |
| 177 | number: num, |
| 178 | isPreview: false, |
| 179 | } |
| 180 | } |
| 181 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 182 | var NoneApiLevel = ApiLevel{ |
| 183 | value: "(no version)", |
| 184 | // Not 0 because we don't want this to compare equal with the first preview. |
| 185 | number: -1, |
| 186 | isPreview: true, |
| 187 | } |
| 188 | |
Spandan Das | 15da588 | 2023-03-02 23:36:39 +0000 | [diff] [blame^] | 189 | // Sentinel ApiLevel to validate that an apiLevel is either an int or a recognized codename. |
| 190 | var InvalidApiLevel = NewInvalidApiLevel("invalid") |
| 191 | |
| 192 | // Returns an apiLevel object at the same level as InvalidApiLevel. |
| 193 | // The object contains the raw string provied in bp file, and can be used for error handling. |
| 194 | func NewInvalidApiLevel(raw string) ApiLevel { |
| 195 | return ApiLevel{ |
| 196 | value: raw, |
| 197 | number: -2, // One less than NoneApiLevel |
| 198 | isPreview: true, |
| 199 | } |
| 200 | } |
| 201 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 202 | // The first version that introduced 64-bit ABIs. |
| 203 | var FirstLp64Version = uncheckedFinalApiLevel(21) |
| 204 | |
Elliott Hughes | 0e9cdb0 | 2021-05-14 13:07:32 -0700 | [diff] [blame] | 205 | // Android has had various kinds of packed relocations over the years |
| 206 | // (http://b/187907243). |
| 207 | // |
| 208 | // API level 30 is where the now-standard SHT_RELR is available. |
| 209 | var FirstShtRelrVersion = uncheckedFinalApiLevel(30) |
| 210 | |
| 211 | // API level 28 introduced SHT_RELR when it was still Android-only, and used an |
| 212 | // Android-specific relocation. |
| 213 | var FirstAndroidRelrVersion = uncheckedFinalApiLevel(28) |
| 214 | |
| 215 | // API level 23 was when we first had the Chrome relocation packer, which is |
| 216 | // obsolete and has been removed, but lld can now generate compatible packed |
| 217 | // relocations itself. |
| 218 | var FirstPackedRelocationsVersion = uncheckedFinalApiLevel(23) |
| 219 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 220 | // The first API level that does not require NDK code to link |
| 221 | // libandroid_support. |
| 222 | var FirstNonLibAndroidSupportVersion = uncheckedFinalApiLevel(21) |
| 223 | |
Paul Duffin | 004547f | 2021-10-29 13:50:24 +0100 | [diff] [blame] | 224 | // LastWithoutModuleLibCoreSystemModules is the last API level where prebuilts/sdk does not contain |
| 225 | // a core-for-system-modules.jar for the module-lib API scope. |
| 226 | var LastWithoutModuleLibCoreSystemModules = uncheckedFinalApiLevel(31) |
| 227 | |
Vinh Tran | f192474 | 2022-06-24 16:40:11 -0400 | [diff] [blame] | 228 | // ReplaceFinalizedCodenames returns the API level number associated with that API level |
| 229 | // if the `raw` input is the codename of an API level has been finalized. |
| 230 | // If the input is *not* a finalized codename, the input is returned unmodified. |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 231 | func ReplaceFinalizedCodenames(config Config, raw string) string { |
| 232 | num, ok := getFinalCodenamesMap(config)[raw] |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 233 | if !ok { |
| 234 | return raw |
| 235 | } |
| 236 | |
| 237 | return strconv.Itoa(num) |
| 238 | } |
| 239 | |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 240 | // 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] | 241 | // |
| 242 | // `raw` must be non-empty. Passing an empty string results in a panic. |
| 243 | // |
| 244 | // "current" will return CurrentApiLevel, which is the ApiLevel associated with |
| 245 | // an arbitrary future release (often referred to as API level 10000). |
| 246 | // |
| 247 | // Finalized codenames will be interpreted as their final API levels, not the |
| 248 | // preview of the associated releases. R is now API 30, not the R preview. |
| 249 | // |
| 250 | // Future codenames return a preview API level that has no associated integer. |
| 251 | // |
| 252 | // Inputs that are not "current", known previews, or convertible to an integer |
| 253 | // will return an error. |
Colin Cross | 9f720ce | 2020-10-02 10:26:04 -0700 | [diff] [blame] | 254 | func ApiLevelFromUser(ctx PathContext, raw string) (ApiLevel, error) { |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 255 | return ApiLevelFromUserWithConfig(ctx.Config(), raw) |
| 256 | } |
| 257 | |
| 258 | // ApiLevelFromUserWithConfig implements ApiLevelFromUser, see comments for |
| 259 | // ApiLevelFromUser for more details. |
| 260 | func ApiLevelFromUserWithConfig(config Config, raw string) (ApiLevel, error) { |
Alix | fb7f7b9 | 2023-03-02 19:35:02 +0000 | [diff] [blame] | 261 | // This logic is replicated in starlark, if changing logic here update starlark code too |
| 262 | // https://cs.android.com/android/platform/superproject/+/master:build/bazel/rules/common/api.bzl;l=42;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061 |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 263 | if raw == "" { |
| 264 | panic("API level string must be non-empty") |
| 265 | } |
| 266 | |
| 267 | if raw == "current" { |
Dan Albert | 0b176c8 | 2020-07-23 16:43:25 -0700 | [diff] [blame] | 268 | return FutureApiLevel, nil |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 269 | } |
| 270 | |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 271 | for _, preview := range config.PreviewApiLevels() { |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 272 | if raw == preview.String() { |
| 273 | return preview, nil |
| 274 | } |
| 275 | } |
| 276 | |
satayev | 0ee2f91 | 2021-12-01 17:39:48 +0000 | [diff] [blame] | 277 | canonical := ReplaceFinalizedCodenames(config, raw) |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 278 | asInt, err := strconv.Atoi(canonical) |
| 279 | if err != nil { |
| 280 | return NoneApiLevel, fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", canonical) |
| 281 | } |
| 282 | |
| 283 | apiLevel := uncheckedFinalApiLevel(asInt) |
| 284 | return apiLevel, nil |
| 285 | } |
| 286 | |
Paul Duffin | 004547f | 2021-10-29 13:50:24 +0100 | [diff] [blame] | 287 | // ApiLevelForTest returns an ApiLevel constructed from the supplied raw string. |
| 288 | // |
| 289 | // This only supports "current" and numeric levels, code names are not supported. |
| 290 | func ApiLevelForTest(raw string) ApiLevel { |
| 291 | if raw == "" { |
| 292 | panic("API level string must be non-empty") |
| 293 | } |
| 294 | |
| 295 | if raw == "current" { |
| 296 | return FutureApiLevel |
| 297 | } |
| 298 | |
| 299 | asInt, err := strconv.Atoi(raw) |
| 300 | if err != nil { |
| 301 | panic(fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", raw)) |
| 302 | } |
| 303 | |
| 304 | apiLevel := uncheckedFinalApiLevel(asInt) |
| 305 | return apiLevel |
| 306 | } |
| 307 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 308 | // Converts an API level string `raw` into an ApiLevel in the same method as |
| 309 | // `ApiLevelFromUser`, but the input is assumed to have no errors and any errors |
| 310 | // will panic instead of returning an error. |
Colin Cross | 9f720ce | 2020-10-02 10:26:04 -0700 | [diff] [blame] | 311 | func ApiLevelOrPanic(ctx PathContext, raw string) ApiLevel { |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 312 | value, err := ApiLevelFromUser(ctx, raw) |
| 313 | if err != nil { |
| 314 | panic(err.Error()) |
| 315 | } |
| 316 | return value |
| 317 | } |
| 318 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 319 | func ApiLevelsSingleton() Singleton { |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 320 | return &apiLevelsSingleton{} |
| 321 | } |
| 322 | |
| 323 | type apiLevelsSingleton struct{} |
| 324 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 325 | func createApiLevelsJson(ctx SingletonContext, file WritablePath, |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 326 | apiLevelsMap map[string]int) { |
| 327 | |
| 328 | jsonStr, err := json.Marshal(apiLevelsMap) |
| 329 | if err != nil { |
| 330 | ctx.Errorf(err.Error()) |
| 331 | } |
| 332 | |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 333 | WriteFileRule(ctx, file, string(jsonStr)) |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 336 | func GetApiLevelsJson(ctx PathContext) WritablePath { |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 337 | return PathForOutput(ctx, "api_levels.json") |
| 338 | } |
| 339 | |
Alix Espino | 4fd7e74 | 2023-02-24 14:46:43 +0000 | [diff] [blame] | 340 | func getApiLevelsMapReleasedVersions() map[string]int { |
| 341 | return map[string]int{ |
| 342 | "G": 9, |
| 343 | "I": 14, |
| 344 | "J": 16, |
| 345 | "J-MR1": 17, |
| 346 | "J-MR2": 18, |
| 347 | "K": 19, |
| 348 | "L": 21, |
| 349 | "L-MR1": 22, |
| 350 | "M": 23, |
| 351 | "N": 24, |
| 352 | "N-MR1": 25, |
| 353 | "O": 26, |
| 354 | "O-MR1": 27, |
| 355 | "P": 28, |
| 356 | "Q": 29, |
| 357 | "R": 30, |
| 358 | "S": 31, |
| 359 | "S-V2": 32, |
| 360 | "Tiramisu": 33, |
| 361 | } |
| 362 | } |
| 363 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 364 | var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap") |
| 365 | |
| 366 | func getFinalCodenamesMap(config Config) map[string]int { |
Alix | fb7f7b9 | 2023-03-02 19:35:02 +0000 | [diff] [blame] | 367 | // This logic is replicated in starlark, if changing logic here update starlark code too |
| 368 | // https://cs.android.com/android/platform/superproject/+/master:build/bazel/rules/common/api.bzl;l=30;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061 |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 369 | return config.Once(finalCodenamesMapKey, func() interface{} { |
Alix Espino | 4fd7e74 | 2023-02-24 14:46:43 +0000 | [diff] [blame] | 370 | apiLevelsMap := getApiLevelsMapReleasedVersions() |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 371 | |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 372 | // TODO: Differentiate "current" and "future". |
| 373 | // The code base calls it FutureApiLevel, but the spelling is "current", |
| 374 | // and these are really two different things. When defining APIs it |
| 375 | // means the API has not yet been added to a specific release. When |
| 376 | // choosing an API level to build for it means that the future API level |
| 377 | // should be used, except in the case where the build is finalized in |
| 378 | // which case the platform version should be used. This is *weird*, |
| 379 | // because in the circumstance where API foo was added in R and bar was |
| 380 | // added in S, both of these are usable when building for "current" when |
| 381 | // neither R nor S are final, but the S APIs stop being available in a |
| 382 | // final R build. |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 383 | if Bool(config.productVariables.Platform_sdk_final) { |
Dan Albert | 4f378d7 | 2020-07-23 17:32:15 -0700 | [diff] [blame] | 384 | apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt() |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | return apiLevelsMap |
| 388 | }).(map[string]int) |
| 389 | } |
| 390 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 391 | var apiLevelsMapKey = NewOnceKey("ApiLevelsMap") |
| 392 | |
Alix Espino | 4fd7e74 | 2023-02-24 14:46:43 +0000 | [diff] [blame] | 393 | // ApiLevelsMap has entries for preview API levels |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 394 | func GetApiLevelsMap(config Config) map[string]int { |
Alix | fb7f7b9 | 2023-03-02 19:35:02 +0000 | [diff] [blame] | 395 | // This logic is replicated in starlark, if changing logic here update starlark code too |
| 396 | // https://cs.android.com/android/platform/superproject/+/master:build/bazel/rules/common/api.bzl;l=23;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061 |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 397 | return config.Once(apiLevelsMapKey, func() interface{} { |
Alix Espino | 4fd7e74 | 2023-02-24 14:46:43 +0000 | [diff] [blame] | 398 | apiLevelsMap := getApiLevelsMapReleasedVersions() |
Jooyung Han | 424175d | 2020-04-08 09:22:26 +0900 | [diff] [blame] | 399 | for i, codename := range config.PlatformVersionActiveCodenames() { |
Jooyung Han | 11b0fbd | 2021-02-05 02:28:22 +0900 | [diff] [blame] | 400 | apiLevelsMap[codename] = previewAPILevelBase + i |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 401 | } |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 402 | |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 403 | return apiLevelsMap |
| 404 | }).(map[string]int) |
| 405 | } |
| 406 | |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 407 | func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) { |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 408 | apiLevelsMap := GetApiLevelsMap(ctx.Config()) |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 409 | apiLevelsJson := GetApiLevelsJson(ctx) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 410 | createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap) |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 411 | } |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 412 | |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 413 | func StarlarkApiLevelConfigs(config Config) string { |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 414 | return fmt.Sprintf(bazel.GeneratedBazelFileWarning+` |
Alix Espino | 4fd7e74 | 2023-02-24 14:46:43 +0000 | [diff] [blame] | 415 | _api_levels_released_versions = %s |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 416 | |
Alix Espino | 4fd7e74 | 2023-02-24 14:46:43 +0000 | [diff] [blame] | 417 | api_levels_released_versions = _api_levels_released_versions |
| 418 | `, starlark_fmt.PrintStringIntDict(getApiLevelsMapReleasedVersions(), 0), |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 419 | ) |
Sam Delmerico | 7f88956 | 2022-03-25 14:55:40 +0000 | [diff] [blame] | 420 | } |