blob: aa55aa149b41edde52c14a90d820040e9f4a9ba9 [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
Dan Albert1a246272020-07-06 14:49:35 -0700195// If the `raw` input is the codename of an API level has been finalized, this
196// function returns the API level number associated with that API level. If the
197// input is *not* a finalized codename, the input is returned unmodified.
198//
199// For example, at the time of writing, R has been finalized as API level 30,
200// but S is in development so it has no number assigned. For the following
201// inputs:
202//
203// * "30" -> "30"
204// * "R" -> "30"
205// * "S" -> "S"
satayev0ee2f912021-12-01 17:39:48 +0000206func ReplaceFinalizedCodenames(config Config, raw string) string {
207 num, ok := getFinalCodenamesMap(config)[raw]
Dan Albert1a246272020-07-06 14:49:35 -0700208 if !ok {
209 return raw
210 }
211
212 return strconv.Itoa(num)
213}
214
satayev0ee2f912021-12-01 17:39:48 +0000215// ApiLevelFromUser converts the given string `raw` to an ApiLevel, possibly returning an error.
Dan Albert1a246272020-07-06 14:49:35 -0700216//
217// `raw` must be non-empty. Passing an empty string results in a panic.
218//
219// "current" will return CurrentApiLevel, which is the ApiLevel associated with
220// an arbitrary future release (often referred to as API level 10000).
221//
222// Finalized codenames will be interpreted as their final API levels, not the
223// preview of the associated releases. R is now API 30, not the R preview.
224//
225// Future codenames return a preview API level that has no associated integer.
226//
227// Inputs that are not "current", known previews, or convertible to an integer
228// will return an error.
Colin Cross9f720ce2020-10-02 10:26:04 -0700229func ApiLevelFromUser(ctx PathContext, raw string) (ApiLevel, error) {
satayev0ee2f912021-12-01 17:39:48 +0000230 return ApiLevelFromUserWithConfig(ctx.Config(), raw)
231}
232
233// ApiLevelFromUserWithConfig implements ApiLevelFromUser, see comments for
234// ApiLevelFromUser for more details.
235func ApiLevelFromUserWithConfig(config Config, raw string) (ApiLevel, error) {
Dan Albert1a246272020-07-06 14:49:35 -0700236 if raw == "" {
237 panic("API level string must be non-empty")
238 }
239
240 if raw == "current" {
Dan Albert0b176c82020-07-23 16:43:25 -0700241 return FutureApiLevel, nil
Dan Albert1a246272020-07-06 14:49:35 -0700242 }
243
satayev0ee2f912021-12-01 17:39:48 +0000244 for _, preview := range config.PreviewApiLevels() {
Dan Albert1a246272020-07-06 14:49:35 -0700245 if raw == preview.String() {
246 return preview, nil
247 }
248 }
249
satayev0ee2f912021-12-01 17:39:48 +0000250 canonical := ReplaceFinalizedCodenames(config, raw)
Dan Albert1a246272020-07-06 14:49:35 -0700251 asInt, err := strconv.Atoi(canonical)
252 if err != nil {
253 return NoneApiLevel, fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", canonical)
254 }
255
256 apiLevel := uncheckedFinalApiLevel(asInt)
257 return apiLevel, nil
258}
259
Paul Duffin004547f2021-10-29 13:50:24 +0100260// ApiLevelForTest returns an ApiLevel constructed from the supplied raw string.
261//
262// This only supports "current" and numeric levels, code names are not supported.
263func ApiLevelForTest(raw string) ApiLevel {
264 if raw == "" {
265 panic("API level string must be non-empty")
266 }
267
268 if raw == "current" {
269 return FutureApiLevel
270 }
271
272 asInt, err := strconv.Atoi(raw)
273 if err != nil {
274 panic(fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", raw))
275 }
276
277 apiLevel := uncheckedFinalApiLevel(asInt)
278 return apiLevel
279}
280
Dan Albert1a246272020-07-06 14:49:35 -0700281// Converts an API level string `raw` into an ApiLevel in the same method as
282// `ApiLevelFromUser`, but the input is assumed to have no errors and any errors
283// will panic instead of returning an error.
Colin Cross9f720ce2020-10-02 10:26:04 -0700284func ApiLevelOrPanic(ctx PathContext, raw string) ApiLevel {
Dan Albert1a246272020-07-06 14:49:35 -0700285 value, err := ApiLevelFromUser(ctx, raw)
286 if err != nil {
287 panic(err.Error())
288 }
289 return value
290}
291
Colin Cross0875c522017-11-28 17:34:01 -0800292func ApiLevelsSingleton() Singleton {
Dan Albert30c9d6e2017-03-28 14:54:55 -0700293 return &apiLevelsSingleton{}
294}
295
296type apiLevelsSingleton struct{}
297
Colin Cross0875c522017-11-28 17:34:01 -0800298func createApiLevelsJson(ctx SingletonContext, file WritablePath,
Dan Albert30c9d6e2017-03-28 14:54:55 -0700299 apiLevelsMap map[string]int) {
300
301 jsonStr, err := json.Marshal(apiLevelsMap)
302 if err != nil {
303 ctx.Errorf(err.Error())
304 }
305
Colin Crosscf371cc2020-11-13 11:48:42 -0800306 WriteFileRule(ctx, file, string(jsonStr))
Dan Albert30c9d6e2017-03-28 14:54:55 -0700307}
308
Colin Cross0875c522017-11-28 17:34:01 -0800309func GetApiLevelsJson(ctx PathContext) WritablePath {
Dan Albert30c9d6e2017-03-28 14:54:55 -0700310 return PathForOutput(ctx, "api_levels.json")
311}
312
Dan Albert1a246272020-07-06 14:49:35 -0700313var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap")
314
315func getFinalCodenamesMap(config Config) map[string]int {
316 return config.Once(finalCodenamesMapKey, func() interface{} {
317 apiLevelsMap := map[string]int{
318 "G": 9,
319 "I": 14,
320 "J": 16,
321 "J-MR1": 17,
322 "J-MR2": 18,
323 "K": 19,
324 "L": 21,
325 "L-MR1": 22,
326 "M": 23,
327 "N": 24,
328 "N-MR1": 25,
329 "O": 26,
330 "O-MR1": 27,
331 "P": 28,
332 "Q": 29,
Dan Albertdbc008f2020-09-16 11:35:00 -0700333 "R": 30,
Jeff Sharkey74120912021-05-27 09:17:56 -0600334 "S": 31,
Michael Wright08dd45a2021-10-28 16:45:21 +0100335 "S-V2": 32,
Dan Albert1a246272020-07-06 14:49:35 -0700336 }
337
Dan Albertc8060532020-07-22 22:32:17 -0700338 // TODO: Differentiate "current" and "future".
339 // The code base calls it FutureApiLevel, but the spelling is "current",
340 // and these are really two different things. When defining APIs it
341 // means the API has not yet been added to a specific release. When
342 // choosing an API level to build for it means that the future API level
343 // should be used, except in the case where the build is finalized in
344 // which case the platform version should be used. This is *weird*,
345 // because in the circumstance where API foo was added in R and bar was
346 // added in S, both of these are usable when building for "current" when
347 // neither R nor S are final, but the S APIs stop being available in a
348 // final R build.
Dan Albert1a246272020-07-06 14:49:35 -0700349 if Bool(config.productVariables.Platform_sdk_final) {
Dan Albert4f378d72020-07-23 17:32:15 -0700350 apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt()
Dan Albert1a246272020-07-06 14:49:35 -0700351 }
352
353 return apiLevelsMap
354 }).(map[string]int)
355}
356
Colin Cross571cccf2019-02-04 11:22:08 -0800357var apiLevelsMapKey = NewOnceKey("ApiLevelsMap")
358
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000359func GetApiLevelsMap(config Config) map[string]int {
Colin Cross571cccf2019-02-04 11:22:08 -0800360 return config.Once(apiLevelsMapKey, func() interface{} {
Dan Albert6bc5b832018-05-03 15:42:34 -0700361 apiLevelsMap := map[string]int{
362 "G": 9,
363 "I": 14,
364 "J": 16,
365 "J-MR1": 17,
366 "J-MR2": 18,
367 "K": 19,
368 "L": 21,
369 "L-MR1": 22,
370 "M": 23,
371 "N": 24,
372 "N-MR1": 25,
373 "O": 26,
374 "O-MR1": 27,
375 "P": 28,
Ian Pedowitz851de712019-05-11 17:02:50 +0000376 "Q": 29,
Svet Ganov3b0b84b2020-04-29 17:14:15 -0700377 "R": 30,
Jeff Sharkey74120912021-05-27 09:17:56 -0600378 "S": 31,
Michael Wright08dd45a2021-10-28 16:45:21 +0100379 "S-V2": 32,
Dan Albert6bc5b832018-05-03 15:42:34 -0700380 }
Jooyung Han424175d2020-04-08 09:22:26 +0900381 for i, codename := range config.PlatformVersionActiveCodenames() {
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900382 apiLevelsMap[codename] = previewAPILevelBase + i
Dan Albert6bc5b832018-05-03 15:42:34 -0700383 }
Dan Albert30c9d6e2017-03-28 14:54:55 -0700384
Dan Albert6bc5b832018-05-03 15:42:34 -0700385 return apiLevelsMap
386 }).(map[string]int)
387}
388
Dan Albert6bc5b832018-05-03 15:42:34 -0700389func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) {
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000390 apiLevelsMap := GetApiLevelsMap(ctx.Config())
Dan Albert30c9d6e2017-03-28 14:54:55 -0700391 apiLevelsJson := GetApiLevelsJson(ctx)
Colin Cross0875c522017-11-28 17:34:01 -0800392 createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap)
Dan Albert30c9d6e2017-03-28 14:54:55 -0700393}
Yu Liufc603162022-03-01 15:44:08 -0800394
395func printApiLevelsStarlarkDict(config Config) string {
396 apiLevelsMap := GetApiLevelsMap(config)
397 valDict := make(map[string]string, len(apiLevelsMap))
398 for k, v := range apiLevelsMap {
399 valDict[k] = strconv.Itoa(v)
400 }
401 return starlark_fmt.PrintDict(valDict, 0)
402}
403
404func StarlarkApiLevelConfigs(config Config) string {
Sam Delmerico7f889562022-03-25 14:55:40 +0000405 return fmt.Sprintf(bazel.GeneratedBazelFileWarning+`
Yu Liufc603162022-03-01 15:44:08 -0800406_api_levels = %s
407
408api_levels = _api_levels
409`, printApiLevelsStarlarkDict(config),
410 )
Sam Delmerico7f889562022-03-25 14:55:40 +0000411}