blob: 6d7552ff73a6b294c431763be3d15b7e4dd35ae2 [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"
Yu Liufc603162022-03-01 15:44:08 -080021
Sam Delmerico7f889562022-03-25 14:55:40 +000022 "android/soong/bazel"
Yu Liufc603162022-03-01 15:44:08 -080023 "android/soong/starlark_fmt"
Dan Albert30c9d6e2017-03-28 14:54:55 -070024)
25
26func init() {
27 RegisterSingletonType("api_levels", ApiLevelsSingleton)
28}
29
Jooyung Han11b0fbd2021-02-05 02:28:22 +090030const previewAPILevelBase = 9000
31
Dan Albert1a246272020-07-06 14:49:35 -070032// 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 Parkf1691d22021-03-29 20:11:58 +090037// 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 Albert1a246272020-07-06 14:49:35 -070040type 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 Fauste5bf3fb2022-07-01 19:39:14 +000057func (this ApiLevel) FinalInt() int {
58 if this.IsPreview() {
59 panic("Requested a final int from a non-final ApiLevel")
60 } else {
61 return this.number
62 }
63}
64
Dan Albertc8060532020-07-22 22:32:17 -070065func (this ApiLevel) FinalOrFutureInt() int {
66 if this.IsPreview() {
Dan Albert0b176c82020-07-23 16:43:25 -070067 return FutureApiLevelInt
Dan Albertc8060532020-07-22 22:32:17 -070068 } else {
69 return this.number
70 }
71}
72
Jooyung Han11b0fbd2021-02-05 02:28:22 +090073// FinalOrPreviewInt distinguishes preview versions from "current" (future).
74// This is for "native" stubs and should be in sync with ndkstubgen/getApiLevelsMap().
75// - "current" -> future (10000)
76// - preview codenames -> preview base (9000) + index
77// - otherwise -> cast to int
78func (this ApiLevel) FinalOrPreviewInt() int {
79 if this.IsCurrent() {
80 return this.number
81 }
82 if this.IsPreview() {
83 return previewAPILevelBase + this.number
84 }
85 return this.number
86}
87
Dan Albert1a246272020-07-06 14:49:35 -070088// Returns the canonical name for this API level. For a finalized API level
89// this will be the API number as a string. For a preview API level this
90// will be the codename, or "current".
91func (this ApiLevel) String() string {
92 return this.value
93}
94
95// Returns true if this is a non-final API level.
96func (this ApiLevel) IsPreview() bool {
97 return this.isPreview
98}
99
100// Returns true if this is the unfinalized "current" API level. This means
101// different things across Java and native. Java APIs do not use explicit
102// codenames, so all non-final codenames are grouped into "current". For native
103// explicit codenames are typically used, and current is the union of all
104// non-final APIs, including those that may not yet be in any codename.
105//
106// Note that in a build where the platform is final, "current" will not be a
107// preview API level but will instead be canonicalized to the final API level.
108func (this ApiLevel) IsCurrent() bool {
109 return this.value == "current"
110}
111
Jooyung Haned124c32021-01-26 11:43:46 +0900112func (this ApiLevel) IsNone() bool {
113 return this.number == -1
114}
115
Dan Albert1a246272020-07-06 14:49:35 -0700116// Returns -1 if the current API level is less than the argument, 0 if they
117// are equal, and 1 if it is greater than the argument.
118func (this ApiLevel) CompareTo(other ApiLevel) int {
119 if this.IsPreview() && !other.IsPreview() {
120 return 1
121 } else if !this.IsPreview() && other.IsPreview() {
122 return -1
123 }
124
125 if this.number < other.number {
126 return -1
127 } else if this.number == other.number {
128 return 0
129 } else {
130 return 1
131 }
132}
133
134func (this ApiLevel) EqualTo(other ApiLevel) bool {
135 return this.CompareTo(other) == 0
136}
137
138func (this ApiLevel) GreaterThan(other ApiLevel) bool {
139 return this.CompareTo(other) > 0
140}
141
142func (this ApiLevel) GreaterThanOrEqualTo(other ApiLevel) bool {
143 return this.CompareTo(other) >= 0
144}
145
146func (this ApiLevel) LessThan(other ApiLevel) bool {
147 return this.CompareTo(other) < 0
148}
149
150func (this ApiLevel) LessThanOrEqualTo(other ApiLevel) bool {
151 return this.CompareTo(other) <= 0
152}
153
154func uncheckedFinalApiLevel(num int) ApiLevel {
155 return ApiLevel{
156 value: strconv.Itoa(num),
157 number: num,
158 isPreview: false,
159 }
160}
161
Dan Albert1a246272020-07-06 14:49:35 -0700162var NoneApiLevel = ApiLevel{
163 value: "(no version)",
164 // Not 0 because we don't want this to compare equal with the first preview.
165 number: -1,
166 isPreview: true,
167}
168
169// The first version that introduced 64-bit ABIs.
170var FirstLp64Version = uncheckedFinalApiLevel(21)
171
Elliott Hughes0e9cdb02021-05-14 13:07:32 -0700172// Android has had various kinds of packed relocations over the years
173// (http://b/187907243).
174//
175// API level 30 is where the now-standard SHT_RELR is available.
176var FirstShtRelrVersion = uncheckedFinalApiLevel(30)
177
178// API level 28 introduced SHT_RELR when it was still Android-only, and used an
179// Android-specific relocation.
180var FirstAndroidRelrVersion = uncheckedFinalApiLevel(28)
181
182// API level 23 was when we first had the Chrome relocation packer, which is
183// obsolete and has been removed, but lld can now generate compatible packed
184// relocations itself.
185var FirstPackedRelocationsVersion = uncheckedFinalApiLevel(23)
186
Dan Albert1a246272020-07-06 14:49:35 -0700187// The first API level that does not require NDK code to link
188// libandroid_support.
189var FirstNonLibAndroidSupportVersion = uncheckedFinalApiLevel(21)
190
Paul Duffin004547f2021-10-29 13:50:24 +0100191// LastWithoutModuleLibCoreSystemModules is the last API level where prebuilts/sdk does not contain
192// a core-for-system-modules.jar for the module-lib API scope.
193var LastWithoutModuleLibCoreSystemModules = uncheckedFinalApiLevel(31)
194
Vinh Tranf1924742022-06-24 16:40:11 -0400195// ReplaceFinalizedCodenames returns the API level number associated with that API level
196// if the `raw` input is the codename of an API level has been finalized.
197// If the input is *not* a finalized codename, the input is returned unmodified.
satayev0ee2f912021-12-01 17:39:48 +0000198func ReplaceFinalizedCodenames(config Config, raw string) string {
199 num, ok := getFinalCodenamesMap(config)[raw]
Dan Albert1a246272020-07-06 14:49:35 -0700200 if !ok {
201 return raw
202 }
203
204 return strconv.Itoa(num)
205}
206
satayev0ee2f912021-12-01 17:39:48 +0000207// ApiLevelFromUser converts the given string `raw` to an ApiLevel, possibly returning an error.
Dan Albert1a246272020-07-06 14:49:35 -0700208//
209// `raw` must be non-empty. Passing an empty string results in a panic.
210//
211// "current" will return CurrentApiLevel, which is the ApiLevel associated with
212// an arbitrary future release (often referred to as API level 10000).
213//
214// Finalized codenames will be interpreted as their final API levels, not the
215// preview of the associated releases. R is now API 30, not the R preview.
216//
217// Future codenames return a preview API level that has no associated integer.
218//
219// Inputs that are not "current", known previews, or convertible to an integer
220// will return an error.
Colin Cross9f720ce2020-10-02 10:26:04 -0700221func ApiLevelFromUser(ctx PathContext, raw string) (ApiLevel, error) {
satayev0ee2f912021-12-01 17:39:48 +0000222 return ApiLevelFromUserWithConfig(ctx.Config(), raw)
223}
224
225// ApiLevelFromUserWithConfig implements ApiLevelFromUser, see comments for
226// ApiLevelFromUser for more details.
227func ApiLevelFromUserWithConfig(config Config, raw string) (ApiLevel, error) {
Dan Albert1a246272020-07-06 14:49:35 -0700228 if raw == "" {
229 panic("API level string must be non-empty")
230 }
231
232 if raw == "current" {
Dan Albert0b176c82020-07-23 16:43:25 -0700233 return FutureApiLevel, nil
Dan Albert1a246272020-07-06 14:49:35 -0700234 }
235
satayev0ee2f912021-12-01 17:39:48 +0000236 for _, preview := range config.PreviewApiLevels() {
Dan Albert1a246272020-07-06 14:49:35 -0700237 if raw == preview.String() {
238 return preview, nil
239 }
240 }
241
satayev0ee2f912021-12-01 17:39:48 +0000242 canonical := ReplaceFinalizedCodenames(config, raw)
Dan Albert1a246272020-07-06 14:49:35 -0700243 asInt, err := strconv.Atoi(canonical)
244 if err != nil {
245 return NoneApiLevel, fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", canonical)
246 }
247
248 apiLevel := uncheckedFinalApiLevel(asInt)
249 return apiLevel, nil
250}
251
Paul Duffin004547f2021-10-29 13:50:24 +0100252// ApiLevelForTest returns an ApiLevel constructed from the supplied raw string.
253//
254// This only supports "current" and numeric levels, code names are not supported.
255func ApiLevelForTest(raw string) ApiLevel {
256 if raw == "" {
257 panic("API level string must be non-empty")
258 }
259
260 if raw == "current" {
261 return FutureApiLevel
262 }
263
264 asInt, err := strconv.Atoi(raw)
265 if err != nil {
266 panic(fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", raw))
267 }
268
269 apiLevel := uncheckedFinalApiLevel(asInt)
270 return apiLevel
271}
272
Dan Albert1a246272020-07-06 14:49:35 -0700273// Converts an API level string `raw` into an ApiLevel in the same method as
274// `ApiLevelFromUser`, but the input is assumed to have no errors and any errors
275// will panic instead of returning an error.
Colin Cross9f720ce2020-10-02 10:26:04 -0700276func ApiLevelOrPanic(ctx PathContext, raw string) ApiLevel {
Dan Albert1a246272020-07-06 14:49:35 -0700277 value, err := ApiLevelFromUser(ctx, raw)
278 if err != nil {
279 panic(err.Error())
280 }
281 return value
282}
283
Colin Cross0875c522017-11-28 17:34:01 -0800284func ApiLevelsSingleton() Singleton {
Dan Albert30c9d6e2017-03-28 14:54:55 -0700285 return &apiLevelsSingleton{}
286}
287
288type apiLevelsSingleton struct{}
289
Colin Cross0875c522017-11-28 17:34:01 -0800290func createApiLevelsJson(ctx SingletonContext, file WritablePath,
Dan Albert30c9d6e2017-03-28 14:54:55 -0700291 apiLevelsMap map[string]int) {
292
293 jsonStr, err := json.Marshal(apiLevelsMap)
294 if err != nil {
295 ctx.Errorf(err.Error())
296 }
297
Colin Crosscf371cc2020-11-13 11:48:42 -0800298 WriteFileRule(ctx, file, string(jsonStr))
Dan Albert30c9d6e2017-03-28 14:54:55 -0700299}
300
Colin Cross0875c522017-11-28 17:34:01 -0800301func GetApiLevelsJson(ctx PathContext) WritablePath {
Dan Albert30c9d6e2017-03-28 14:54:55 -0700302 return PathForOutput(ctx, "api_levels.json")
303}
304
Alix Espino4fd7e742023-02-24 14:46:43 +0000305func getApiLevelsMapReleasedVersions() map[string]int {
306 return map[string]int{
307 "G": 9,
308 "I": 14,
309 "J": 16,
310 "J-MR1": 17,
311 "J-MR2": 18,
312 "K": 19,
313 "L": 21,
314 "L-MR1": 22,
315 "M": 23,
316 "N": 24,
317 "N-MR1": 25,
318 "O": 26,
319 "O-MR1": 27,
320 "P": 28,
321 "Q": 29,
322 "R": 30,
323 "S": 31,
324 "S-V2": 32,
325 "Tiramisu": 33,
326 }
327}
328
Dan Albert1a246272020-07-06 14:49:35 -0700329var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap")
330
331func getFinalCodenamesMap(config Config) map[string]int {
332 return config.Once(finalCodenamesMapKey, func() interface{} {
Alix Espino4fd7e742023-02-24 14:46:43 +0000333 apiLevelsMap := getApiLevelsMapReleasedVersions()
Dan Albert1a246272020-07-06 14:49:35 -0700334
Dan Albertc8060532020-07-22 22:32:17 -0700335 // TODO: Differentiate "current" and "future".
336 // The code base calls it FutureApiLevel, but the spelling is "current",
337 // and these are really two different things. When defining APIs it
338 // means the API has not yet been added to a specific release. When
339 // choosing an API level to build for it means that the future API level
340 // should be used, except in the case where the build is finalized in
341 // which case the platform version should be used. This is *weird*,
342 // because in the circumstance where API foo was added in R and bar was
343 // added in S, both of these are usable when building for "current" when
344 // neither R nor S are final, but the S APIs stop being available in a
345 // final R build.
Dan Albert1a246272020-07-06 14:49:35 -0700346 if Bool(config.productVariables.Platform_sdk_final) {
Dan Albert4f378d72020-07-23 17:32:15 -0700347 apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt()
Dan Albert1a246272020-07-06 14:49:35 -0700348 }
349
350 return apiLevelsMap
351 }).(map[string]int)
352}
353
Colin Cross571cccf2019-02-04 11:22:08 -0800354var apiLevelsMapKey = NewOnceKey("ApiLevelsMap")
355
Alix Espino4fd7e742023-02-24 14:46:43 +0000356// ApiLevelsMap has entries for preview API levels
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000357func GetApiLevelsMap(config Config) map[string]int {
Colin Cross571cccf2019-02-04 11:22:08 -0800358 return config.Once(apiLevelsMapKey, func() interface{} {
Alix Espino4fd7e742023-02-24 14:46:43 +0000359 apiLevelsMap := getApiLevelsMapReleasedVersions()
Jooyung Han424175d2020-04-08 09:22:26 +0900360 for i, codename := range config.PlatformVersionActiveCodenames() {
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900361 apiLevelsMap[codename] = previewAPILevelBase + i
Dan Albert6bc5b832018-05-03 15:42:34 -0700362 }
Dan Albert30c9d6e2017-03-28 14:54:55 -0700363
Dan Albert6bc5b832018-05-03 15:42:34 -0700364 return apiLevelsMap
365 }).(map[string]int)
366}
367
Dan Albert6bc5b832018-05-03 15:42:34 -0700368func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) {
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000369 apiLevelsMap := GetApiLevelsMap(ctx.Config())
Dan Albert30c9d6e2017-03-28 14:54:55 -0700370 apiLevelsJson := GetApiLevelsJson(ctx)
Colin Cross0875c522017-11-28 17:34:01 -0800371 createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap)
Dan Albert30c9d6e2017-03-28 14:54:55 -0700372}
Yu Liufc603162022-03-01 15:44:08 -0800373
Yu Liufc603162022-03-01 15:44:08 -0800374func StarlarkApiLevelConfigs(config Config) string {
Sam Delmerico7f889562022-03-25 14:55:40 +0000375 return fmt.Sprintf(bazel.GeneratedBazelFileWarning+`
Alix Espino4fd7e742023-02-24 14:46:43 +0000376_api_levels_released_versions = %s
Yu Liufc603162022-03-01 15:44:08 -0800377
Alix Espino4fd7e742023-02-24 14:46:43 +0000378api_levels_released_versions = _api_levels_released_versions
379`, starlark_fmt.PrintStringIntDict(getApiLevelsMapReleasedVersions(), 0),
Yu Liufc603162022-03-01 15:44:08 -0800380 )
Sam Delmerico7f889562022-03-25 14:55:40 +0000381}