blob: 27a3b7fd83f16f4574958f809c954954c8d60748 [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
22 "android/soong/starlark_fmt"
Dan Albert30c9d6e2017-03-28 14:54:55 -070023)
24
25func init() {
26 RegisterSingletonType("api_levels", ApiLevelsSingleton)
27}
28
Jooyung Han11b0fbd2021-02-05 02:28:22 +090029const previewAPILevelBase = 9000
30
Dan Albert1a246272020-07-06 14:49:35 -070031// 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 Parkf1691d22021-03-29 20:11:58 +090036// 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 Albert1a246272020-07-06 14:49:35 -070039type 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
Dan Albertc8060532020-07-22 22:32:17 -070056func (this ApiLevel) FinalOrFutureInt() int {
57 if this.IsPreview() {
Dan Albert0b176c82020-07-23 16:43:25 -070058 return FutureApiLevelInt
Dan Albertc8060532020-07-22 22:32:17 -070059 } else {
60 return this.number
61 }
62}
63
Jooyung Han11b0fbd2021-02-05 02:28:22 +090064// FinalOrPreviewInt distinguishes preview versions from "current" (future).
65// This is for "native" stubs and should be in sync with ndkstubgen/getApiLevelsMap().
66// - "current" -> future (10000)
67// - preview codenames -> preview base (9000) + index
68// - otherwise -> cast to int
69func (this ApiLevel) FinalOrPreviewInt() int {
70 if this.IsCurrent() {
71 return this.number
72 }
73 if this.IsPreview() {
74 return previewAPILevelBase + this.number
75 }
76 return this.number
77}
78
Dan Albert1a246272020-07-06 14:49:35 -070079// Returns the canonical name for this API level. For a finalized API level
80// this will be the API number as a string. For a preview API level this
81// will be the codename, or "current".
82func (this ApiLevel) String() string {
83 return this.value
84}
85
86// Returns true if this is a non-final API level.
87func (this ApiLevel) IsPreview() bool {
88 return this.isPreview
89}
90
91// Returns true if this is the unfinalized "current" API level. This means
92// different things across Java and native. Java APIs do not use explicit
93// codenames, so all non-final codenames are grouped into "current". For native
94// explicit codenames are typically used, and current is the union of all
95// non-final APIs, including those that may not yet be in any codename.
96//
97// Note that in a build where the platform is final, "current" will not be a
98// preview API level but will instead be canonicalized to the final API level.
99func (this ApiLevel) IsCurrent() bool {
100 return this.value == "current"
101}
102
Jooyung Haned124c32021-01-26 11:43:46 +0900103func (this ApiLevel) IsNone() bool {
104 return this.number == -1
105}
106
Dan Albert1a246272020-07-06 14:49:35 -0700107// Returns -1 if the current API level is less than the argument, 0 if they
108// are equal, and 1 if it is greater than the argument.
109func (this ApiLevel) CompareTo(other ApiLevel) int {
110 if this.IsPreview() && !other.IsPreview() {
111 return 1
112 } else if !this.IsPreview() && other.IsPreview() {
113 return -1
114 }
115
116 if this.number < other.number {
117 return -1
118 } else if this.number == other.number {
119 return 0
120 } else {
121 return 1
122 }
123}
124
125func (this ApiLevel) EqualTo(other ApiLevel) bool {
126 return this.CompareTo(other) == 0
127}
128
129func (this ApiLevel) GreaterThan(other ApiLevel) bool {
130 return this.CompareTo(other) > 0
131}
132
133func (this ApiLevel) GreaterThanOrEqualTo(other ApiLevel) bool {
134 return this.CompareTo(other) >= 0
135}
136
137func (this ApiLevel) LessThan(other ApiLevel) bool {
138 return this.CompareTo(other) < 0
139}
140
141func (this ApiLevel) LessThanOrEqualTo(other ApiLevel) bool {
142 return this.CompareTo(other) <= 0
143}
144
145func uncheckedFinalApiLevel(num int) ApiLevel {
146 return ApiLevel{
147 value: strconv.Itoa(num),
148 number: num,
149 isPreview: false,
150 }
151}
152
Dan Albert1a246272020-07-06 14:49:35 -0700153var NoneApiLevel = ApiLevel{
154 value: "(no version)",
155 // Not 0 because we don't want this to compare equal with the first preview.
156 number: -1,
157 isPreview: true,
158}
159
160// The first version that introduced 64-bit ABIs.
161var FirstLp64Version = uncheckedFinalApiLevel(21)
162
Elliott Hughes0e9cdb02021-05-14 13:07:32 -0700163// Android has had various kinds of packed relocations over the years
164// (http://b/187907243).
165//
166// API level 30 is where the now-standard SHT_RELR is available.
167var FirstShtRelrVersion = uncheckedFinalApiLevel(30)
168
169// API level 28 introduced SHT_RELR when it was still Android-only, and used an
170// Android-specific relocation.
171var FirstAndroidRelrVersion = uncheckedFinalApiLevel(28)
172
173// API level 23 was when we first had the Chrome relocation packer, which is
174// obsolete and has been removed, but lld can now generate compatible packed
175// relocations itself.
176var FirstPackedRelocationsVersion = uncheckedFinalApiLevel(23)
177
Dan Albert1a246272020-07-06 14:49:35 -0700178// The first API level that does not require NDK code to link
179// libandroid_support.
180var FirstNonLibAndroidSupportVersion = uncheckedFinalApiLevel(21)
181
Paul Duffin004547f2021-10-29 13:50:24 +0100182// LastWithoutModuleLibCoreSystemModules is the last API level where prebuilts/sdk does not contain
183// a core-for-system-modules.jar for the module-lib API scope.
184var LastWithoutModuleLibCoreSystemModules = uncheckedFinalApiLevel(31)
185
Dan Albert1a246272020-07-06 14:49:35 -0700186// If the `raw` input is the codename of an API level has been finalized, this
187// function returns the API level number associated with that API level. If the
188// input is *not* a finalized codename, the input is returned unmodified.
189//
190// For example, at the time of writing, R has been finalized as API level 30,
191// but S is in development so it has no number assigned. For the following
192// inputs:
193//
194// * "30" -> "30"
195// * "R" -> "30"
196// * "S" -> "S"
satayev0ee2f912021-12-01 17:39:48 +0000197func ReplaceFinalizedCodenames(config Config, raw string) string {
198 num, ok := getFinalCodenamesMap(config)[raw]
Dan Albert1a246272020-07-06 14:49:35 -0700199 if !ok {
200 return raw
201 }
202
203 return strconv.Itoa(num)
204}
205
satayev0ee2f912021-12-01 17:39:48 +0000206// ApiLevelFromUser converts the given string `raw` to an ApiLevel, possibly returning an error.
Dan Albert1a246272020-07-06 14:49:35 -0700207//
208// `raw` must be non-empty. Passing an empty string results in a panic.
209//
210// "current" will return CurrentApiLevel, which is the ApiLevel associated with
211// an arbitrary future release (often referred to as API level 10000).
212//
213// Finalized codenames will be interpreted as their final API levels, not the
214// preview of the associated releases. R is now API 30, not the R preview.
215//
216// Future codenames return a preview API level that has no associated integer.
217//
218// Inputs that are not "current", known previews, or convertible to an integer
219// will return an error.
Colin Cross9f720ce2020-10-02 10:26:04 -0700220func ApiLevelFromUser(ctx PathContext, raw string) (ApiLevel, error) {
satayev0ee2f912021-12-01 17:39:48 +0000221 return ApiLevelFromUserWithConfig(ctx.Config(), raw)
222}
223
224// ApiLevelFromUserWithConfig implements ApiLevelFromUser, see comments for
225// ApiLevelFromUser for more details.
226func ApiLevelFromUserWithConfig(config Config, raw string) (ApiLevel, error) {
Dan Albert1a246272020-07-06 14:49:35 -0700227 if raw == "" {
228 panic("API level string must be non-empty")
229 }
230
231 if raw == "current" {
Dan Albert0b176c82020-07-23 16:43:25 -0700232 return FutureApiLevel, nil
Dan Albert1a246272020-07-06 14:49:35 -0700233 }
234
satayev0ee2f912021-12-01 17:39:48 +0000235 for _, preview := range config.PreviewApiLevels() {
Dan Albert1a246272020-07-06 14:49:35 -0700236 if raw == preview.String() {
237 return preview, nil
238 }
239 }
240
satayev0ee2f912021-12-01 17:39:48 +0000241 canonical := ReplaceFinalizedCodenames(config, raw)
Dan Albert1a246272020-07-06 14:49:35 -0700242 asInt, err := strconv.Atoi(canonical)
243 if err != nil {
244 return NoneApiLevel, fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", canonical)
245 }
246
247 apiLevel := uncheckedFinalApiLevel(asInt)
248 return apiLevel, nil
249}
250
Paul Duffin004547f2021-10-29 13:50:24 +0100251// ApiLevelForTest returns an ApiLevel constructed from the supplied raw string.
252//
253// This only supports "current" and numeric levels, code names are not supported.
254func ApiLevelForTest(raw string) ApiLevel {
255 if raw == "" {
256 panic("API level string must be non-empty")
257 }
258
259 if raw == "current" {
260 return FutureApiLevel
261 }
262
263 asInt, err := strconv.Atoi(raw)
264 if err != nil {
265 panic(fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", raw))
266 }
267
268 apiLevel := uncheckedFinalApiLevel(asInt)
269 return apiLevel
270}
271
Dan Albert1a246272020-07-06 14:49:35 -0700272// Converts an API level string `raw` into an ApiLevel in the same method as
273// `ApiLevelFromUser`, but the input is assumed to have no errors and any errors
274// will panic instead of returning an error.
Colin Cross9f720ce2020-10-02 10:26:04 -0700275func ApiLevelOrPanic(ctx PathContext, raw string) ApiLevel {
Dan Albert1a246272020-07-06 14:49:35 -0700276 value, err := ApiLevelFromUser(ctx, raw)
277 if err != nil {
278 panic(err.Error())
279 }
280 return value
281}
282
Colin Cross0875c522017-11-28 17:34:01 -0800283func ApiLevelsSingleton() Singleton {
Dan Albert30c9d6e2017-03-28 14:54:55 -0700284 return &apiLevelsSingleton{}
285}
286
287type apiLevelsSingleton struct{}
288
Colin Cross0875c522017-11-28 17:34:01 -0800289func createApiLevelsJson(ctx SingletonContext, file WritablePath,
Dan Albert30c9d6e2017-03-28 14:54:55 -0700290 apiLevelsMap map[string]int) {
291
292 jsonStr, err := json.Marshal(apiLevelsMap)
293 if err != nil {
294 ctx.Errorf(err.Error())
295 }
296
Colin Crosscf371cc2020-11-13 11:48:42 -0800297 WriteFileRule(ctx, file, string(jsonStr))
Dan Albert30c9d6e2017-03-28 14:54:55 -0700298}
299
Colin Cross0875c522017-11-28 17:34:01 -0800300func GetApiLevelsJson(ctx PathContext) WritablePath {
Dan Albert30c9d6e2017-03-28 14:54:55 -0700301 return PathForOutput(ctx, "api_levels.json")
302}
303
Dan Albert1a246272020-07-06 14:49:35 -0700304var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap")
305
306func getFinalCodenamesMap(config Config) map[string]int {
307 return config.Once(finalCodenamesMapKey, func() interface{} {
308 apiLevelsMap := map[string]int{
309 "G": 9,
310 "I": 14,
311 "J": 16,
312 "J-MR1": 17,
313 "J-MR2": 18,
314 "K": 19,
315 "L": 21,
316 "L-MR1": 22,
317 "M": 23,
318 "N": 24,
319 "N-MR1": 25,
320 "O": 26,
321 "O-MR1": 27,
322 "P": 28,
323 "Q": 29,
Dan Albertdbc008f2020-09-16 11:35:00 -0700324 "R": 30,
Jeff Sharkey74120912021-05-27 09:17:56 -0600325 "S": 31,
Michael Wright08dd45a2021-10-28 16:45:21 +0100326 "S-V2": 32,
Dan Albert1a246272020-07-06 14:49:35 -0700327 }
328
Dan Albertc8060532020-07-22 22:32:17 -0700329 // TODO: Differentiate "current" and "future".
330 // The code base calls it FutureApiLevel, but the spelling is "current",
331 // and these are really two different things. When defining APIs it
332 // means the API has not yet been added to a specific release. When
333 // choosing an API level to build for it means that the future API level
334 // should be used, except in the case where the build is finalized in
335 // which case the platform version should be used. This is *weird*,
336 // because in the circumstance where API foo was added in R and bar was
337 // added in S, both of these are usable when building for "current" when
338 // neither R nor S are final, but the S APIs stop being available in a
339 // final R build.
Dan Albert1a246272020-07-06 14:49:35 -0700340 if Bool(config.productVariables.Platform_sdk_final) {
Dan Albert4f378d72020-07-23 17:32:15 -0700341 apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt()
Dan Albert1a246272020-07-06 14:49:35 -0700342 }
343
344 return apiLevelsMap
345 }).(map[string]int)
346}
347
Colin Cross571cccf2019-02-04 11:22:08 -0800348var apiLevelsMapKey = NewOnceKey("ApiLevelsMap")
349
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000350func GetApiLevelsMap(config Config) map[string]int {
Colin Cross571cccf2019-02-04 11:22:08 -0800351 return config.Once(apiLevelsMapKey, func() interface{} {
Dan Albert6bc5b832018-05-03 15:42:34 -0700352 apiLevelsMap := map[string]int{
353 "G": 9,
354 "I": 14,
355 "J": 16,
356 "J-MR1": 17,
357 "J-MR2": 18,
358 "K": 19,
359 "L": 21,
360 "L-MR1": 22,
361 "M": 23,
362 "N": 24,
363 "N-MR1": 25,
364 "O": 26,
365 "O-MR1": 27,
366 "P": 28,
Ian Pedowitz851de712019-05-11 17:02:50 +0000367 "Q": 29,
Svet Ganov3b0b84b2020-04-29 17:14:15 -0700368 "R": 30,
Jeff Sharkey74120912021-05-27 09:17:56 -0600369 "S": 31,
Michael Wright08dd45a2021-10-28 16:45:21 +0100370 "S-V2": 32,
Dan Albert6bc5b832018-05-03 15:42:34 -0700371 }
Jooyung Han424175d2020-04-08 09:22:26 +0900372 for i, codename := range config.PlatformVersionActiveCodenames() {
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900373 apiLevelsMap[codename] = previewAPILevelBase + i
Dan Albert6bc5b832018-05-03 15:42:34 -0700374 }
Dan Albert30c9d6e2017-03-28 14:54:55 -0700375
Dan Albert6bc5b832018-05-03 15:42:34 -0700376 return apiLevelsMap
377 }).(map[string]int)
378}
379
Dan Albert6bc5b832018-05-03 15:42:34 -0700380func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) {
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000381 apiLevelsMap := GetApiLevelsMap(ctx.Config())
Dan Albert30c9d6e2017-03-28 14:54:55 -0700382 apiLevelsJson := GetApiLevelsJson(ctx)
Colin Cross0875c522017-11-28 17:34:01 -0800383 createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap)
Dan Albert30c9d6e2017-03-28 14:54:55 -0700384}
Yu Liufc603162022-03-01 15:44:08 -0800385
386func printApiLevelsStarlarkDict(config Config) string {
387 apiLevelsMap := GetApiLevelsMap(config)
388 valDict := make(map[string]string, len(apiLevelsMap))
389 for k, v := range apiLevelsMap {
390 valDict[k] = strconv.Itoa(v)
391 }
392 return starlark_fmt.PrintDict(valDict, 0)
393}
394
395func StarlarkApiLevelConfigs(config Config) string {
396 return fmt.Sprintf(`# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
397_api_levels = %s
398
399api_levels = _api_levels
400`, printApiLevelsStarlarkDict(config),
401 )
402}