blob: 81638940c403d6ab1b8193b65560c61dbcfc0976 [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
Dan Albertc8060532020-07-22 22:32:17 -070057func (this ApiLevel) FinalOrFutureInt() int {
58 if this.IsPreview() {
Dan Albert0b176c82020-07-23 16:43:25 -070059 return FutureApiLevelInt
Dan Albertc8060532020-07-22 22:32:17 -070060 } else {
61 return this.number
62 }
63}
64
Jooyung Han11b0fbd2021-02-05 02:28:22 +090065// FinalOrPreviewInt distinguishes preview versions from "current" (future).
66// This is for "native" stubs and should be in sync with ndkstubgen/getApiLevelsMap().
67// - "current" -> future (10000)
68// - preview codenames -> preview base (9000) + index
69// - otherwise -> cast to int
70func (this ApiLevel) FinalOrPreviewInt() int {
71 if this.IsCurrent() {
72 return this.number
73 }
74 if this.IsPreview() {
75 return previewAPILevelBase + this.number
76 }
77 return this.number
78}
79
Dan Albert1a246272020-07-06 14:49:35 -070080// Returns the canonical name for this API level. For a finalized API level
81// this will be the API number as a string. For a preview API level this
82// will be the codename, or "current".
83func (this ApiLevel) String() string {
84 return this.value
85}
86
87// Returns true if this is a non-final API level.
88func (this ApiLevel) IsPreview() bool {
89 return this.isPreview
90}
91
92// Returns true if this is the unfinalized "current" API level. This means
93// different things across Java and native. Java APIs do not use explicit
94// codenames, so all non-final codenames are grouped into "current". For native
95// explicit codenames are typically used, and current is the union of all
96// non-final APIs, including those that may not yet be in any codename.
97//
98// Note that in a build where the platform is final, "current" will not be a
99// preview API level but will instead be canonicalized to the final API level.
100func (this ApiLevel) IsCurrent() bool {
101 return this.value == "current"
102}
103
Jooyung Haned124c32021-01-26 11:43:46 +0900104func (this ApiLevel) IsNone() bool {
105 return this.number == -1
106}
107
Dan Albert1a246272020-07-06 14:49:35 -0700108// Returns -1 if the current API level is less than the argument, 0 if they
109// are equal, and 1 if it is greater than the argument.
110func (this ApiLevel) CompareTo(other ApiLevel) int {
111 if this.IsPreview() && !other.IsPreview() {
112 return 1
113 } else if !this.IsPreview() && other.IsPreview() {
114 return -1
115 }
116
117 if this.number < other.number {
118 return -1
119 } else if this.number == other.number {
120 return 0
121 } else {
122 return 1
123 }
124}
125
126func (this ApiLevel) EqualTo(other ApiLevel) bool {
127 return this.CompareTo(other) == 0
128}
129
130func (this ApiLevel) GreaterThan(other ApiLevel) bool {
131 return this.CompareTo(other) > 0
132}
133
134func (this ApiLevel) GreaterThanOrEqualTo(other ApiLevel) bool {
135 return this.CompareTo(other) >= 0
136}
137
138func (this ApiLevel) LessThan(other ApiLevel) bool {
139 return this.CompareTo(other) < 0
140}
141
142func (this ApiLevel) LessThanOrEqualTo(other ApiLevel) bool {
143 return this.CompareTo(other) <= 0
144}
145
146func uncheckedFinalApiLevel(num int) ApiLevel {
147 return ApiLevel{
148 value: strconv.Itoa(num),
149 number: num,
150 isPreview: false,
151 }
152}
153
Dan Albert1a246272020-07-06 14:49:35 -0700154var NoneApiLevel = ApiLevel{
155 value: "(no version)",
156 // Not 0 because we don't want this to compare equal with the first preview.
157 number: -1,
158 isPreview: true,
159}
160
161// The first version that introduced 64-bit ABIs.
162var FirstLp64Version = uncheckedFinalApiLevel(21)
163
Elliott Hughes0e9cdb02021-05-14 13:07:32 -0700164// Android has had various kinds of packed relocations over the years
165// (http://b/187907243).
166//
167// API level 30 is where the now-standard SHT_RELR is available.
168var FirstShtRelrVersion = uncheckedFinalApiLevel(30)
169
170// API level 28 introduced SHT_RELR when it was still Android-only, and used an
171// Android-specific relocation.
172var FirstAndroidRelrVersion = uncheckedFinalApiLevel(28)
173
174// API level 23 was when we first had the Chrome relocation packer, which is
175// obsolete and has been removed, but lld can now generate compatible packed
176// relocations itself.
177var FirstPackedRelocationsVersion = uncheckedFinalApiLevel(23)
178
Dan Albert1a246272020-07-06 14:49:35 -0700179// The first API level that does not require NDK code to link
180// libandroid_support.
181var FirstNonLibAndroidSupportVersion = uncheckedFinalApiLevel(21)
182
Paul Duffin004547f2021-10-29 13:50:24 +0100183// LastWithoutModuleLibCoreSystemModules is the last API level where prebuilts/sdk does not contain
184// a core-for-system-modules.jar for the module-lib API scope.
185var LastWithoutModuleLibCoreSystemModules = uncheckedFinalApiLevel(31)
186
Dan Albert1a246272020-07-06 14:49:35 -0700187// If the `raw` input is the codename of an API level has been finalized, this
188// function returns the API level number associated with that API level. If the
189// input is *not* a finalized codename, the input is returned unmodified.
190//
191// For example, at the time of writing, R has been finalized as API level 30,
192// but S is in development so it has no number assigned. For the following
193// inputs:
194//
195// * "30" -> "30"
196// * "R" -> "30"
197// * "S" -> "S"
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
Dan Albert1a246272020-07-06 14:49:35 -0700305var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap")
306
307func getFinalCodenamesMap(config Config) map[string]int {
308 return config.Once(finalCodenamesMapKey, func() interface{} {
309 apiLevelsMap := map[string]int{
310 "G": 9,
311 "I": 14,
312 "J": 16,
313 "J-MR1": 17,
314 "J-MR2": 18,
315 "K": 19,
316 "L": 21,
317 "L-MR1": 22,
318 "M": 23,
319 "N": 24,
320 "N-MR1": 25,
321 "O": 26,
322 "O-MR1": 27,
323 "P": 28,
324 "Q": 29,
Dan Albertdbc008f2020-09-16 11:35:00 -0700325 "R": 30,
Jeff Sharkey74120912021-05-27 09:17:56 -0600326 "S": 31,
Michael Wright08dd45a2021-10-28 16:45:21 +0100327 "S-V2": 32,
Dan Albert1a246272020-07-06 14:49:35 -0700328 }
329
Dan Albertc8060532020-07-22 22:32:17 -0700330 // TODO: Differentiate "current" and "future".
331 // The code base calls it FutureApiLevel, but the spelling is "current",
332 // and these are really two different things. When defining APIs it
333 // means the API has not yet been added to a specific release. When
334 // choosing an API level to build for it means that the future API level
335 // should be used, except in the case where the build is finalized in
336 // which case the platform version should be used. This is *weird*,
337 // because in the circumstance where API foo was added in R and bar was
338 // added in S, both of these are usable when building for "current" when
339 // neither R nor S are final, but the S APIs stop being available in a
340 // final R build.
Dan Albert1a246272020-07-06 14:49:35 -0700341 if Bool(config.productVariables.Platform_sdk_final) {
Dan Albert4f378d72020-07-23 17:32:15 -0700342 apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt()
Dan Albert1a246272020-07-06 14:49:35 -0700343 }
344
345 return apiLevelsMap
346 }).(map[string]int)
347}
348
Colin Cross571cccf2019-02-04 11:22:08 -0800349var apiLevelsMapKey = NewOnceKey("ApiLevelsMap")
350
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000351func GetApiLevelsMap(config Config) map[string]int {
Colin Cross571cccf2019-02-04 11:22:08 -0800352 return config.Once(apiLevelsMapKey, func() interface{} {
Dan Albert6bc5b832018-05-03 15:42:34 -0700353 apiLevelsMap := map[string]int{
354 "G": 9,
355 "I": 14,
356 "J": 16,
357 "J-MR1": 17,
358 "J-MR2": 18,
359 "K": 19,
360 "L": 21,
361 "L-MR1": 22,
362 "M": 23,
363 "N": 24,
364 "N-MR1": 25,
365 "O": 26,
366 "O-MR1": 27,
367 "P": 28,
Ian Pedowitz851de712019-05-11 17:02:50 +0000368 "Q": 29,
Svet Ganov3b0b84b2020-04-29 17:14:15 -0700369 "R": 30,
Jeff Sharkey74120912021-05-27 09:17:56 -0600370 "S": 31,
Michael Wright08dd45a2021-10-28 16:45:21 +0100371 "S-V2": 32,
Dan Albert6bc5b832018-05-03 15:42:34 -0700372 }
Jooyung Han424175d2020-04-08 09:22:26 +0900373 for i, codename := range config.PlatformVersionActiveCodenames() {
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900374 apiLevelsMap[codename] = previewAPILevelBase + i
Dan Albert6bc5b832018-05-03 15:42:34 -0700375 }
Dan Albert30c9d6e2017-03-28 14:54:55 -0700376
Dan Albert6bc5b832018-05-03 15:42:34 -0700377 return apiLevelsMap
378 }).(map[string]int)
379}
380
Dan Albert6bc5b832018-05-03 15:42:34 -0700381func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) {
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000382 apiLevelsMap := GetApiLevelsMap(ctx.Config())
Dan Albert30c9d6e2017-03-28 14:54:55 -0700383 apiLevelsJson := GetApiLevelsJson(ctx)
Colin Cross0875c522017-11-28 17:34:01 -0800384 createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap)
Dan Albert30c9d6e2017-03-28 14:54:55 -0700385}
Yu Liufc603162022-03-01 15:44:08 -0800386
387func printApiLevelsStarlarkDict(config Config) string {
388 apiLevelsMap := GetApiLevelsMap(config)
389 valDict := make(map[string]string, len(apiLevelsMap))
390 for k, v := range apiLevelsMap {
391 valDict[k] = strconv.Itoa(v)
392 }
393 return starlark_fmt.PrintDict(valDict, 0)
394}
395
396func StarlarkApiLevelConfigs(config Config) string {
Sam Delmerico7f889562022-03-25 14:55:40 +0000397 return fmt.Sprintf(bazel.GeneratedBazelFileWarning+`
Yu Liufc603162022-03-01 15:44:08 -0800398_api_levels = %s
399
400api_levels = _api_levels
401`, printApiLevelsStarlarkDict(config),
402 )
Sam Delmerico7f889562022-03-25 14:55:40 +0000403}