blob: 6d0c3891f8903d3a00dc27152b14e0f4346c01b0 [file] [log] [blame]
Dan Albert30c9d6e2017-03-28 14:54:55 -07001// 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
15package android
16
17import (
18 "encoding/json"
Jooyung Han29e91d22020-04-02 01:41:41 +090019 "fmt"
Dan Albert6bc5b832018-05-03 15:42:34 -070020 "strconv"
Todd Lee2ec7e1c2023-08-25 18:02:13 +000021 "strings"
Dan Albert30c9d6e2017-03-28 14:54:55 -070022)
23
24func init() {
LaMont Jones0c10e4d2023-05-16 00:58:37 +000025 RegisterParallelSingletonType("api_levels", ApiLevelsSingleton)
Dan Albert30c9d6e2017-03-28 14:54:55 -070026}
27
Jooyung Han11b0fbd2021-02-05 02:28:22 +090028const previewAPILevelBase = 9000
29
Dan Albert1a246272020-07-06 14:49:35 -070030// An API level, which may be a finalized (numbered) API, a preview (codenamed)
31// API, or the future API level (10000). Can be parsed from a string with
32// ApiLevelFromUser or ApiLevelOrPanic.
33//
34// The different *types* of API levels are handled separately. Currently only
Jiyong Parkf1691d22021-03-29 20:11:58 +090035// Java has these, and they're managed with the SdkKind enum of the SdkSpec. A
36// future cleanup should be to migrate SdkSpec to using ApiLevel instead of its
37// SdkVersion int, and to move SdkSpec into this package.
Dan Albert1a246272020-07-06 14:49:35 -070038type ApiLevel struct {
39 // The string representation of the API level.
40 value string
41
42 // A number associated with the API level. The exact value depends on
43 // whether this API level is a preview or final API.
44 //
45 // For final API levels, this is the assigned version number.
46 //
47 // For preview API levels, this value has no meaning except to index known
48 // previews to determine ordering.
49 number int
50
51 // Identifies this API level as either a preview or final API level.
52 isPreview bool
53}
54
Cole Fauste5bf3fb2022-07-01 19:39:14 +000055func (this ApiLevel) FinalInt() int {
Spandan Das15da5882023-03-02 23:36:39 +000056 if this.IsInvalid() {
57 panic(fmt.Errorf("%v is not a recognized api_level\n", this))
58 }
Cole Fauste5bf3fb2022-07-01 19:39:14 +000059 if this.IsPreview() {
60 panic("Requested a final int from a non-final ApiLevel")
61 } else {
62 return this.number
63 }
64}
65
Dan Albertc8060532020-07-22 22:32:17 -070066func (this ApiLevel) FinalOrFutureInt() int {
Spandan Das15da5882023-03-02 23:36:39 +000067 if this.IsInvalid() {
68 panic(fmt.Errorf("%v is not a recognized api_level\n", this))
69 }
Dan Albertc8060532020-07-22 22:32:17 -070070 if this.IsPreview() {
Dan Albert0b176c82020-07-23 16:43:25 -070071 return FutureApiLevelInt
Dan Albertc8060532020-07-22 22:32:17 -070072 } else {
73 return this.number
74 }
75}
76
Jooyung Han11b0fbd2021-02-05 02:28:22 +090077// FinalOrPreviewInt distinguishes preview versions from "current" (future).
78// This is for "native" stubs and should be in sync with ndkstubgen/getApiLevelsMap().
79// - "current" -> future (10000)
80// - preview codenames -> preview base (9000) + index
81// - otherwise -> cast to int
82func (this ApiLevel) FinalOrPreviewInt() int {
Spandan Das15da5882023-03-02 23:36:39 +000083 if this.IsInvalid() {
84 panic(fmt.Errorf("%v is not a recognized api_level\n", this))
85 }
Jooyung Han11b0fbd2021-02-05 02:28:22 +090086 if this.IsCurrent() {
87 return this.number
88 }
89 if this.IsPreview() {
90 return previewAPILevelBase + this.number
91 }
92 return this.number
93}
94
Dan Albert1a246272020-07-06 14:49:35 -070095// Returns the canonical name for this API level. For a finalized API level
96// this will be the API number as a string. For a preview API level this
97// will be the codename, or "current".
98func (this ApiLevel) String() string {
99 return this.value
100}
101
102// Returns true if this is a non-final API level.
103func (this ApiLevel) IsPreview() bool {
104 return this.isPreview
105}
106
Spandan Das15da5882023-03-02 23:36:39 +0000107// Returns true if the raw api level string is invalid
108func (this ApiLevel) IsInvalid() bool {
109 return this.EqualTo(InvalidApiLevel)
110}
111
Dan Albert1a246272020-07-06 14:49:35 -0700112// Returns true if this is the unfinalized "current" API level. This means
113// different things across Java and native. Java APIs do not use explicit
114// codenames, so all non-final codenames are grouped into "current". For native
115// explicit codenames are typically used, and current is the union of all
116// non-final APIs, including those that may not yet be in any codename.
117//
118// Note that in a build where the platform is final, "current" will not be a
119// preview API level but will instead be canonicalized to the final API level.
120func (this ApiLevel) IsCurrent() bool {
121 return this.value == "current"
122}
123
Jooyung Haned124c32021-01-26 11:43:46 +0900124func (this ApiLevel) IsNone() bool {
125 return this.number == -1
126}
127
Spandan Das15da5882023-03-02 23:36:39 +0000128// Returns true if an app is compiling against private apis.
129// e.g. if sdk_version = "" in Android.bp, then the ApiLevel of that "sdk" is at PrivateApiLevel.
130func (this ApiLevel) IsPrivate() bool {
131 return this.number == PrivateApiLevel.number
132}
133
Spandan Dasdd7057c2023-01-05 01:03:47 +0000134// EffectiveVersion converts an ApiLevel into the concrete ApiLevel that the module should use. For
135// modules targeting an unreleased SDK (meaning it does not yet have a number) it returns
136// FutureApiLevel(10000).
137func (l ApiLevel) EffectiveVersion(ctx EarlyModuleContext) (ApiLevel, error) {
138 if l.EqualTo(InvalidApiLevel) {
139 return l, fmt.Errorf("invalid version in sdk_version %q", l.value)
140 }
141 if !l.IsPreview() {
142 return l, nil
143 }
144 ret := ctx.Config().DefaultAppTargetSdk(ctx)
145 if ret.IsPreview() {
146 return FutureApiLevel, nil
147 }
148 return ret, nil
149}
150
151// EffectiveVersionString converts an SdkSpec into the concrete version string that the module
152// should use. For modules targeting an unreleased SDK (meaning it does not yet have a number)
153// it returns the codename (P, Q, R, etc.)
154func (l ApiLevel) EffectiveVersionString(ctx EarlyModuleContext) (string, error) {
155 if l.EqualTo(InvalidApiLevel) {
156 return l.value, fmt.Errorf("invalid version in sdk_version %q", l.value)
157 }
158 if !l.IsPreview() {
159 return l.String(), nil
160 }
161 // Determine the default sdk
162 ret := ctx.Config().DefaultAppTargetSdk(ctx)
163 if !ret.IsPreview() {
164 // If the default sdk has been finalized, return that
165 return ret.String(), nil
166 }
167 // There can be more than one active in-development sdks
168 // If an app is targeting an active sdk, but not the default one, return the requested active sdk.
169 // e.g.
170 // SETUP
171 // In-development: UpsideDownCake, VanillaIceCream
172 // Default: VanillaIceCream
173 // Android.bp
174 // min_sdk_version: `UpsideDownCake`
175 // RETURN
176 // UpsideDownCake and not VanillaIceCream
177 for _, preview := range ctx.Config().PreviewApiLevels() {
178 if l.String() == preview.String() {
179 return preview.String(), nil
180 }
181 }
182 // Otherwise return the default one
183 return ret.String(), nil
184}
185
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000186// Specified returns true if the module is targeting a recognzized api_level.
187// It returns false if either
188// 1. min_sdk_version is not an int or a recognized codename
189// 2. both min_sdk_version and sdk_version are empty. In this case, MinSdkVersion() defaults to SdkSpecPrivate.ApiLevel
190func (this ApiLevel) Specified() bool {
191 return !this.IsInvalid() && !this.IsPrivate()
192}
193
Dan Albert1a246272020-07-06 14:49:35 -0700194// Returns -1 if the current API level is less than the argument, 0 if they
195// are equal, and 1 if it is greater than the argument.
196func (this ApiLevel) CompareTo(other ApiLevel) int {
197 if this.IsPreview() && !other.IsPreview() {
198 return 1
199 } else if !this.IsPreview() && other.IsPreview() {
200 return -1
201 }
202
203 if this.number < other.number {
204 return -1
205 } else if this.number == other.number {
206 return 0
207 } else {
208 return 1
209 }
210}
211
212func (this ApiLevel) EqualTo(other ApiLevel) bool {
213 return this.CompareTo(other) == 0
214}
215
216func (this ApiLevel) GreaterThan(other ApiLevel) bool {
217 return this.CompareTo(other) > 0
218}
219
220func (this ApiLevel) GreaterThanOrEqualTo(other ApiLevel) bool {
221 return this.CompareTo(other) >= 0
222}
223
224func (this ApiLevel) LessThan(other ApiLevel) bool {
225 return this.CompareTo(other) < 0
226}
227
228func (this ApiLevel) LessThanOrEqualTo(other ApiLevel) bool {
229 return this.CompareTo(other) <= 0
230}
231
232func uncheckedFinalApiLevel(num int) ApiLevel {
233 return ApiLevel{
234 value: strconv.Itoa(num),
235 number: num,
236 isPreview: false,
237 }
238}
239
Todd Lee2ec7e1c2023-08-25 18:02:13 +0000240func uncheckedFinalIncrementalApiLevel(num int, increment int) ApiLevel {
241 return ApiLevel{
242 value: strconv.Itoa(num) + "." + strconv.Itoa(increment),
243 number: num,
244 isPreview: false,
245 }
246}
247
Dan Albert1a246272020-07-06 14:49:35 -0700248var NoneApiLevel = ApiLevel{
249 value: "(no version)",
250 // Not 0 because we don't want this to compare equal with the first preview.
251 number: -1,
252 isPreview: true,
253}
254
Yu Liudf0b8392025-02-12 18:27:03 +0000255// A special ApiLevel that all modules should at least support.
256var MinApiLevel = ApiLevel{number: 1}
257
Spandan Das15da5882023-03-02 23:36:39 +0000258// Sentinel ApiLevel to validate that an apiLevel is either an int or a recognized codename.
259var InvalidApiLevel = NewInvalidApiLevel("invalid")
260
261// Returns an apiLevel object at the same level as InvalidApiLevel.
262// The object contains the raw string provied in bp file, and can be used for error handling.
263func NewInvalidApiLevel(raw string) ApiLevel {
264 return ApiLevel{
265 value: raw,
266 number: -2, // One less than NoneApiLevel
267 isPreview: true,
268 }
269}
270
Dan Albert1a246272020-07-06 14:49:35 -0700271// The first version that introduced 64-bit ABIs.
272var FirstLp64Version = uncheckedFinalApiLevel(21)
273
Elliott Hughes0e9cdb02021-05-14 13:07:32 -0700274// Android has had various kinds of packed relocations over the years
275// (http://b/187907243).
276//
277// API level 30 is where the now-standard SHT_RELR is available.
278var FirstShtRelrVersion = uncheckedFinalApiLevel(30)
279
280// API level 28 introduced SHT_RELR when it was still Android-only, and used an
281// Android-specific relocation.
282var FirstAndroidRelrVersion = uncheckedFinalApiLevel(28)
283
284// API level 23 was when we first had the Chrome relocation packer, which is
285// obsolete and has been removed, but lld can now generate compatible packed
286// relocations itself.
287var FirstPackedRelocationsVersion = uncheckedFinalApiLevel(23)
288
Paul Duffin004547f2021-10-29 13:50:24 +0100289// LastWithoutModuleLibCoreSystemModules is the last API level where prebuilts/sdk does not contain
290// a core-for-system-modules.jar for the module-lib API scope.
291var LastWithoutModuleLibCoreSystemModules = uncheckedFinalApiLevel(31)
292
Spandan Das38c64f62024-02-12 15:00:15 +0000293var ApiLevelR = uncheckedFinalApiLevel(30)
294
Spandan Dasa5e26d32024-03-06 14:04:36 +0000295var ApiLevelUpsideDownCake = uncheckedFinalApiLevel(34)
296
Jihoon Kang98aa8fa2024-06-07 11:06:57 +0000297var ApiLevelVanillaIceCream = uncheckedFinalApiLevel(35)
298
Vinh Tranf1924742022-06-24 16:40:11 -0400299// ReplaceFinalizedCodenames returns the API level number associated with that API level
300// if the `raw` input is the codename of an API level has been finalized.
301// If the input is *not* a finalized codename, the input is returned unmodified.
Cole Faust34867402023-04-28 12:32:27 -0700302func ReplaceFinalizedCodenames(config Config, raw string) (string, error) {
303 finalCodenamesMap, err := getFinalCodenamesMap(config)
304 if err != nil {
305 return raw, err
306 }
307 num, ok := finalCodenamesMap[raw]
Dan Albert1a246272020-07-06 14:49:35 -0700308 if !ok {
Cole Faust34867402023-04-28 12:32:27 -0700309 return raw, nil
Dan Albert1a246272020-07-06 14:49:35 -0700310 }
311
Cole Faust34867402023-04-28 12:32:27 -0700312 return strconv.Itoa(num), nil
Dan Albert1a246272020-07-06 14:49:35 -0700313}
314
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000315// ApiLevelFrom converts the given string `raw` to an ApiLevel.
316// If `raw` is invalid (empty string, unrecognized codename etc.) it returns an invalid ApiLevel
Colin Cross1cea5302024-12-03 16:40:08 -0800317func ApiLevelFrom(ctx ConfigContext, raw string) ApiLevel {
Spandan Das8c9ae7e2023-03-03 21:20:36 +0000318 ret, err := ApiLevelFromUser(ctx, raw)
319 if err != nil {
320 return NewInvalidApiLevel(raw)
321 }
322 return ret
323}
324
satayev0ee2f912021-12-01 17:39:48 +0000325// ApiLevelFromUser converts the given string `raw` to an ApiLevel, possibly returning an error.
Dan Albert1a246272020-07-06 14:49:35 -0700326//
327// `raw` must be non-empty. Passing an empty string results in a panic.
328//
329// "current" will return CurrentApiLevel, which is the ApiLevel associated with
330// an arbitrary future release (often referred to as API level 10000).
331//
332// Finalized codenames will be interpreted as their final API levels, not the
333// preview of the associated releases. R is now API 30, not the R preview.
334//
335// Future codenames return a preview API level that has no associated integer.
336//
337// Inputs that are not "current", known previews, or convertible to an integer
338// will return an error.
Colin Cross1cea5302024-12-03 16:40:08 -0800339func ApiLevelFromUser(ctx ConfigContext, raw string) (ApiLevel, error) {
satayev0ee2f912021-12-01 17:39:48 +0000340 return ApiLevelFromUserWithConfig(ctx.Config(), raw)
341}
342
343// ApiLevelFromUserWithConfig implements ApiLevelFromUser, see comments for
344// ApiLevelFromUser for more details.
345func ApiLevelFromUserWithConfig(config Config, raw string) (ApiLevel, error) {
Alixfb7f7b92023-03-02 19:35:02 +0000346 // This logic is replicated in starlark, if changing logic here update starlark code too
Elliott Hughes10363162024-01-09 22:02:03 +0000347 // https://cs.android.com/android/platform/superproject/+/main:build/bazel/rules/common/api.bzl;l=42;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061
Dan Albert1a246272020-07-06 14:49:35 -0700348 if raw == "" {
349 panic("API level string must be non-empty")
350 }
351
352 if raw == "current" {
Dan Albert0b176c82020-07-23 16:43:25 -0700353 return FutureApiLevel, nil
Dan Albert1a246272020-07-06 14:49:35 -0700354 }
355
satayev0ee2f912021-12-01 17:39:48 +0000356 for _, preview := range config.PreviewApiLevels() {
Dan Albert1a246272020-07-06 14:49:35 -0700357 if raw == preview.String() {
358 return preview, nil
359 }
360 }
361
Cole Faust34867402023-04-28 12:32:27 -0700362 apiLevelsReleasedVersions, err := getApiLevelsMapReleasedVersions()
363 if err != nil {
364 return NoneApiLevel, err
365 }
366 canonical, ok := apiLevelsReleasedVersions[raw]
Alixfb502512023-03-06 21:04:30 +0000367 if !ok {
368 asInt, err := strconv.Atoi(raw)
369 if err != nil {
370 return NoneApiLevel, fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", raw)
371 }
372 return uncheckedFinalApiLevel(asInt), nil
Dan Albert1a246272020-07-06 14:49:35 -0700373 }
374
Alixfb502512023-03-06 21:04:30 +0000375 return uncheckedFinalApiLevel(canonical), nil
376
Dan Albert1a246272020-07-06 14:49:35 -0700377}
378
Paul Duffin004547f2021-10-29 13:50:24 +0100379// ApiLevelForTest returns an ApiLevel constructed from the supplied raw string.
380//
381// This only supports "current" and numeric levels, code names are not supported.
382func ApiLevelForTest(raw string) ApiLevel {
383 if raw == "" {
384 panic("API level string must be non-empty")
385 }
386
387 if raw == "current" {
388 return FutureApiLevel
389 }
390
Todd Lee2ec7e1c2023-08-25 18:02:13 +0000391 if strings.Contains(raw, ".") {
392 // Check prebuilt incremental API format MM.m for major (API level) and minor (incremental) revisions
393 parts := strings.Split(raw, ".")
394 if len(parts) != 2 {
395 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))
396 }
397 sdk, sdk_err := strconv.Atoi(parts[0])
398 qpr, qpr_err := strconv.Atoi(parts[1])
399 if sdk_err != nil || qpr_err != nil {
400 panic(fmt.Errorf("Unable to read version number for incremental api '%s'", raw))
401 }
402
403 apiLevel := uncheckedFinalIncrementalApiLevel(sdk, qpr)
404 return apiLevel
405 }
406
Paul Duffin004547f2021-10-29 13:50:24 +0100407 asInt, err := strconv.Atoi(raw)
408 if err != nil {
409 panic(fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", raw))
410 }
411
412 apiLevel := uncheckedFinalApiLevel(asInt)
413 return apiLevel
414}
415
Dan Albert1a246272020-07-06 14:49:35 -0700416// Converts an API level string `raw` into an ApiLevel in the same method as
417// `ApiLevelFromUser`, but the input is assumed to have no errors and any errors
418// will panic instead of returning an error.
Colin Cross1cea5302024-12-03 16:40:08 -0800419func ApiLevelOrPanic(ctx ConfigContext, raw string) ApiLevel {
Dan Albert1a246272020-07-06 14:49:35 -0700420 value, err := ApiLevelFromUser(ctx, raw)
421 if err != nil {
422 panic(err.Error())
423 }
424 return value
425}
426
Colin Cross0875c522017-11-28 17:34:01 -0800427func ApiLevelsSingleton() Singleton {
Dan Albert30c9d6e2017-03-28 14:54:55 -0700428 return &apiLevelsSingleton{}
429}
430
431type apiLevelsSingleton struct{}
432
Colin Cross0875c522017-11-28 17:34:01 -0800433func createApiLevelsJson(ctx SingletonContext, file WritablePath,
Dan Albert30c9d6e2017-03-28 14:54:55 -0700434 apiLevelsMap map[string]int) {
435
436 jsonStr, err := json.Marshal(apiLevelsMap)
437 if err != nil {
438 ctx.Errorf(err.Error())
439 }
440
Colin Crosscf371cc2020-11-13 11:48:42 -0800441 WriteFileRule(ctx, file, string(jsonStr))
Dan Albert30c9d6e2017-03-28 14:54:55 -0700442}
443
Colin Cross0875c522017-11-28 17:34:01 -0800444func GetApiLevelsJson(ctx PathContext) WritablePath {
Dan Albert30c9d6e2017-03-28 14:54:55 -0700445 return PathForOutput(ctx, "api_levels.json")
446}
447
Cole Faust34867402023-04-28 12:32:27 -0700448func getApiLevelsMapReleasedVersions() (map[string]int, error) {
Cole Faust37bfb072024-03-06 15:50:53 -0800449 return map[string]int{
Michael Wrightc91d0cd2024-04-23 22:50:41 +0000450 "G": 9,
451 "I": 14,
452 "J": 16,
453 "J-MR1": 17,
454 "J-MR2": 18,
455 "K": 19,
456 "L": 21,
457 "L-MR1": 22,
458 "M": 23,
459 "N": 24,
460 "N-MR1": 25,
461 "O": 26,
462 "O-MR1": 27,
463 "P": 28,
464 "Q": 29,
465 "R": 30,
466 "S": 31,
467 "S-V2": 32,
468 "Tiramisu": 33,
469 "UpsideDownCake": 34,
470 "VanillaIceCream": 35,
Cole Faust37bfb072024-03-06 15:50:53 -0800471 }, nil
Alix Espino4fd7e742023-02-24 14:46:43 +0000472}
473
Dan Albert1a246272020-07-06 14:49:35 -0700474var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap")
475
Cole Faust34867402023-04-28 12:32:27 -0700476func getFinalCodenamesMap(config Config) (map[string]int, error) {
477 type resultStruct struct {
478 result map[string]int
479 err error
480 }
Alixfb7f7b92023-03-02 19:35:02 +0000481 // This logic is replicated in starlark, if changing logic here update starlark code too
Elliott Hughes10363162024-01-09 22:02:03 +0000482 // https://cs.android.com/android/platform/superproject/+/main:build/bazel/rules/common/api.bzl;l=30;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061
Cole Faust34867402023-04-28 12:32:27 -0700483 result := config.Once(finalCodenamesMapKey, func() interface{} {
484 apiLevelsMap, err := getApiLevelsMapReleasedVersions()
Dan Albert1a246272020-07-06 14:49:35 -0700485
Dan Albertc8060532020-07-22 22:32:17 -0700486 // TODO: Differentiate "current" and "future".
487 // The code base calls it FutureApiLevel, but the spelling is "current",
488 // and these are really two different things. When defining APIs it
489 // means the API has not yet been added to a specific release. When
490 // choosing an API level to build for it means that the future API level
491 // should be used, except in the case where the build is finalized in
492 // which case the platform version should be used. This is *weird*,
493 // because in the circumstance where API foo was added in R and bar was
494 // added in S, both of these are usable when building for "current" when
495 // neither R nor S are final, but the S APIs stop being available in a
496 // final R build.
Cole Faust34867402023-04-28 12:32:27 -0700497 if err == nil && Bool(config.productVariables.Platform_sdk_final) {
Dan Albert4f378d72020-07-23 17:32:15 -0700498 apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt()
Dan Albert1a246272020-07-06 14:49:35 -0700499 }
500
Cole Faust34867402023-04-28 12:32:27 -0700501 return resultStruct{apiLevelsMap, err}
502 }).(resultStruct)
503 return result.result, result.err
Dan Albert1a246272020-07-06 14:49:35 -0700504}
505
Colin Cross571cccf2019-02-04 11:22:08 -0800506var apiLevelsMapKey = NewOnceKey("ApiLevelsMap")
507
Alix Espino4fd7e742023-02-24 14:46:43 +0000508// ApiLevelsMap has entries for preview API levels
Cole Faust34867402023-04-28 12:32:27 -0700509func GetApiLevelsMap(config Config) (map[string]int, error) {
510 type resultStruct struct {
511 result map[string]int
512 err error
513 }
Alixfb7f7b92023-03-02 19:35:02 +0000514 // This logic is replicated in starlark, if changing logic here update starlark code too
Elliott Hughes10363162024-01-09 22:02:03 +0000515 // https://cs.android.com/android/platform/superproject/+/main:build/bazel/rules/common/api.bzl;l=23;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061
Cole Faust34867402023-04-28 12:32:27 -0700516 result := config.Once(apiLevelsMapKey, func() interface{} {
517 apiLevelsMap, err := getApiLevelsMapReleasedVersions()
518 if err == nil {
519 for i, codename := range config.PlatformVersionAllPreviewCodenames() {
520 apiLevelsMap[codename] = previewAPILevelBase + i
521 }
Dan Albert6bc5b832018-05-03 15:42:34 -0700522 }
Dan Albert30c9d6e2017-03-28 14:54:55 -0700523
Cole Faust34867402023-04-28 12:32:27 -0700524 return resultStruct{apiLevelsMap, err}
525 }).(resultStruct)
526 return result.result, result.err
Dan Albert6bc5b832018-05-03 15:42:34 -0700527}
528
Dan Albert6bc5b832018-05-03 15:42:34 -0700529func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) {
Cole Faust34867402023-04-28 12:32:27 -0700530 apiLevelsMap, err := GetApiLevelsMap(ctx.Config())
531 if err != nil {
532 ctx.Errorf("%s\n", err)
533 return
534 }
Dan Albert30c9d6e2017-03-28 14:54:55 -0700535 apiLevelsJson := GetApiLevelsJson(ctx)
Colin Cross0875c522017-11-28 17:34:01 -0800536 createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap)
Dan Albert30c9d6e2017-03-28 14:54:55 -0700537}