blob: 97683404ee7864d2446b9909baa16b125da2304a [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"
Dan Albert30c9d6e2017-03-28 14:54:55 -070021)
22
23func init() {
24 RegisterSingletonType("api_levels", ApiLevelsSingleton)
25}
26
Dan Albert1a246272020-07-06 14:49:35 -070027// An API level, which may be a finalized (numbered) API, a preview (codenamed)
28// API, or the future API level (10000). Can be parsed from a string with
29// ApiLevelFromUser or ApiLevelOrPanic.
30//
31// The different *types* of API levels are handled separately. Currently only
32// Java has these, and they're managed with the sdkKind enum of the sdkSpec. A
33// future cleanup should be to migrate sdkSpec to using ApiLevel instead of its
34// sdkVersion int, and to move sdkSpec into this package.
35type ApiLevel struct {
36 // The string representation of the API level.
37 value string
38
39 // A number associated with the API level. The exact value depends on
40 // whether this API level is a preview or final API.
41 //
42 // For final API levels, this is the assigned version number.
43 //
44 // For preview API levels, this value has no meaning except to index known
45 // previews to determine ordering.
46 number int
47
48 // Identifies this API level as either a preview or final API level.
49 isPreview bool
50}
51
Dan Albertc8060532020-07-22 22:32:17 -070052func (this ApiLevel) FinalOrFutureInt() int {
53 if this.IsPreview() {
Dan Albert0b176c82020-07-23 16:43:25 -070054 return FutureApiLevelInt
Dan Albertc8060532020-07-22 22:32:17 -070055 } else {
56 return this.number
57 }
58}
59
Dan Albert1a246272020-07-06 14:49:35 -070060// Returns the canonical name for this API level. For a finalized API level
61// this will be the API number as a string. For a preview API level this
62// will be the codename, or "current".
63func (this ApiLevel) String() string {
64 return this.value
65}
66
67// Returns true if this is a non-final API level.
68func (this ApiLevel) IsPreview() bool {
69 return this.isPreview
70}
71
72// Returns true if this is the unfinalized "current" API level. This means
73// different things across Java and native. Java APIs do not use explicit
74// codenames, so all non-final codenames are grouped into "current". For native
75// explicit codenames are typically used, and current is the union of all
76// non-final APIs, including those that may not yet be in any codename.
77//
78// Note that in a build where the platform is final, "current" will not be a
79// preview API level but will instead be canonicalized to the final API level.
80func (this ApiLevel) IsCurrent() bool {
81 return this.value == "current"
82}
83
84// Returns -1 if the current API level is less than the argument, 0 if they
85// are equal, and 1 if it is greater than the argument.
86func (this ApiLevel) CompareTo(other ApiLevel) int {
87 if this.IsPreview() && !other.IsPreview() {
88 return 1
89 } else if !this.IsPreview() && other.IsPreview() {
90 return -1
91 }
92
93 if this.number < other.number {
94 return -1
95 } else if this.number == other.number {
96 return 0
97 } else {
98 return 1
99 }
100}
101
102func (this ApiLevel) EqualTo(other ApiLevel) bool {
103 return this.CompareTo(other) == 0
104}
105
106func (this ApiLevel) GreaterThan(other ApiLevel) bool {
107 return this.CompareTo(other) > 0
108}
109
110func (this ApiLevel) GreaterThanOrEqualTo(other ApiLevel) bool {
111 return this.CompareTo(other) >= 0
112}
113
114func (this ApiLevel) LessThan(other ApiLevel) bool {
115 return this.CompareTo(other) < 0
116}
117
118func (this ApiLevel) LessThanOrEqualTo(other ApiLevel) bool {
119 return this.CompareTo(other) <= 0
120}
121
122func uncheckedFinalApiLevel(num int) ApiLevel {
123 return ApiLevel{
124 value: strconv.Itoa(num),
125 number: num,
126 isPreview: false,
127 }
128}
129
Dan Albert1a246272020-07-06 14:49:35 -0700130var NoneApiLevel = ApiLevel{
131 value: "(no version)",
132 // Not 0 because we don't want this to compare equal with the first preview.
133 number: -1,
134 isPreview: true,
135}
136
137// The first version that introduced 64-bit ABIs.
138var FirstLp64Version = uncheckedFinalApiLevel(21)
139
140// The first API level that does not require NDK code to link
141// libandroid_support.
142var FirstNonLibAndroidSupportVersion = uncheckedFinalApiLevel(21)
143
144// If the `raw` input is the codename of an API level has been finalized, this
145// function returns the API level number associated with that API level. If the
146// input is *not* a finalized codename, the input is returned unmodified.
147//
148// For example, at the time of writing, R has been finalized as API level 30,
149// but S is in development so it has no number assigned. For the following
150// inputs:
151//
152// * "30" -> "30"
153// * "R" -> "30"
154// * "S" -> "S"
155func ReplaceFinalizedCodenames(ctx EarlyModuleContext, raw string) string {
156 num, ok := getFinalCodenamesMap(ctx.Config())[raw]
157 if !ok {
158 return raw
159 }
160
161 return strconv.Itoa(num)
162}
163
164// Converts the given string `raw` to an ApiLevel, possibly returning an error.
165//
166// `raw` must be non-empty. Passing an empty string results in a panic.
167//
168// "current" will return CurrentApiLevel, which is the ApiLevel associated with
169// an arbitrary future release (often referred to as API level 10000).
170//
171// Finalized codenames will be interpreted as their final API levels, not the
172// preview of the associated releases. R is now API 30, not the R preview.
173//
174// Future codenames return a preview API level that has no associated integer.
175//
176// Inputs that are not "current", known previews, or convertible to an integer
177// will return an error.
178func ApiLevelFromUser(ctx EarlyModuleContext, raw string) (ApiLevel, error) {
179 if raw == "" {
180 panic("API level string must be non-empty")
181 }
182
183 if raw == "current" {
Dan Albert0b176c82020-07-23 16:43:25 -0700184 return FutureApiLevel, nil
Dan Albert1a246272020-07-06 14:49:35 -0700185 }
186
187 for _, preview := range ctx.Config().PreviewApiLevels() {
188 if raw == preview.String() {
189 return preview, nil
190 }
191 }
192
193 canonical := ReplaceFinalizedCodenames(ctx, raw)
194 asInt, err := strconv.Atoi(canonical)
195 if err != nil {
196 return NoneApiLevel, fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", canonical)
197 }
198
199 apiLevel := uncheckedFinalApiLevel(asInt)
200 return apiLevel, nil
201}
202
203// Converts an API level string `raw` into an ApiLevel in the same method as
204// `ApiLevelFromUser`, but the input is assumed to have no errors and any errors
205// will panic instead of returning an error.
206func ApiLevelOrPanic(ctx EarlyModuleContext, raw string) ApiLevel {
207 value, err := ApiLevelFromUser(ctx, raw)
208 if err != nil {
209 panic(err.Error())
210 }
211 return value
212}
213
Colin Cross0875c522017-11-28 17:34:01 -0800214func ApiLevelsSingleton() Singleton {
Dan Albert30c9d6e2017-03-28 14:54:55 -0700215 return &apiLevelsSingleton{}
216}
217
218type apiLevelsSingleton struct{}
219
Colin Cross0875c522017-11-28 17:34:01 -0800220func createApiLevelsJson(ctx SingletonContext, file WritablePath,
Dan Albert30c9d6e2017-03-28 14:54:55 -0700221 apiLevelsMap map[string]int) {
222
223 jsonStr, err := json.Marshal(apiLevelsMap)
224 if err != nil {
225 ctx.Errorf(err.Error())
226 }
227
Colin Cross0875c522017-11-28 17:34:01 -0800228 ctx.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700229 Rule: WriteFile,
Colin Cross0875c522017-11-28 17:34:01 -0800230 Description: "generate " + file.Base(),
231 Output: file,
Dan Albert30c9d6e2017-03-28 14:54:55 -0700232 Args: map[string]string{
233 "content": string(jsonStr[:]),
234 },
235 })
236}
237
Colin Cross0875c522017-11-28 17:34:01 -0800238func GetApiLevelsJson(ctx PathContext) WritablePath {
Dan Albert30c9d6e2017-03-28 14:54:55 -0700239 return PathForOutput(ctx, "api_levels.json")
240}
241
Dan Albert1a246272020-07-06 14:49:35 -0700242var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap")
243
244func getFinalCodenamesMap(config Config) map[string]int {
245 return config.Once(finalCodenamesMapKey, func() interface{} {
246 apiLevelsMap := map[string]int{
247 "G": 9,
248 "I": 14,
249 "J": 16,
250 "J-MR1": 17,
251 "J-MR2": 18,
252 "K": 19,
253 "L": 21,
254 "L-MR1": 22,
255 "M": 23,
256 "N": 24,
257 "N-MR1": 25,
258 "O": 26,
259 "O-MR1": 27,
260 "P": 28,
261 "Q": 29,
Dan Albertdbc008f2020-09-16 11:35:00 -0700262 "R": 30,
Dan Albert1a246272020-07-06 14:49:35 -0700263 }
264
Dan Albertc8060532020-07-22 22:32:17 -0700265 // TODO: Differentiate "current" and "future".
266 // The code base calls it FutureApiLevel, but the spelling is "current",
267 // and these are really two different things. When defining APIs it
268 // means the API has not yet been added to a specific release. When
269 // choosing an API level to build for it means that the future API level
270 // should be used, except in the case where the build is finalized in
271 // which case the platform version should be used. This is *weird*,
272 // because in the circumstance where API foo was added in R and bar was
273 // added in S, both of these are usable when building for "current" when
274 // neither R nor S are final, but the S APIs stop being available in a
275 // final R build.
Dan Albert1a246272020-07-06 14:49:35 -0700276 if Bool(config.productVariables.Platform_sdk_final) {
Dan Albert4f378d72020-07-23 17:32:15 -0700277 apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt()
Dan Albert1a246272020-07-06 14:49:35 -0700278 }
279
280 return apiLevelsMap
281 }).(map[string]int)
282}
283
Colin Cross571cccf2019-02-04 11:22:08 -0800284var apiLevelsMapKey = NewOnceKey("ApiLevelsMap")
285
Dan Albert6bc5b832018-05-03 15:42:34 -0700286func getApiLevelsMap(config Config) map[string]int {
Colin Cross571cccf2019-02-04 11:22:08 -0800287 return config.Once(apiLevelsMapKey, func() interface{} {
Dan Albert6bc5b832018-05-03 15:42:34 -0700288 baseApiLevel := 9000
289 apiLevelsMap := map[string]int{
290 "G": 9,
291 "I": 14,
292 "J": 16,
293 "J-MR1": 17,
294 "J-MR2": 18,
295 "K": 19,
296 "L": 21,
297 "L-MR1": 22,
298 "M": 23,
299 "N": 24,
300 "N-MR1": 25,
301 "O": 26,
302 "O-MR1": 27,
303 "P": 28,
Ian Pedowitz851de712019-05-11 17:02:50 +0000304 "Q": 29,
Svet Ganov3b0b84b2020-04-29 17:14:15 -0700305 "R": 30,
Dan Albert6bc5b832018-05-03 15:42:34 -0700306 }
Jooyung Han424175d2020-04-08 09:22:26 +0900307 for i, codename := range config.PlatformVersionActiveCodenames() {
Dan Albert6bc5b832018-05-03 15:42:34 -0700308 apiLevelsMap[codename] = baseApiLevel + i
309 }
Dan Albert30c9d6e2017-03-28 14:54:55 -0700310
Dan Albert6bc5b832018-05-03 15:42:34 -0700311 return apiLevelsMap
312 }).(map[string]int)
313}
314
Dan Albert6bc5b832018-05-03 15:42:34 -0700315func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) {
316 apiLevelsMap := getApiLevelsMap(ctx.Config())
Dan Albert30c9d6e2017-03-28 14:54:55 -0700317 apiLevelsJson := GetApiLevelsJson(ctx)
Colin Cross0875c522017-11-28 17:34:01 -0800318 createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap)
Dan Albert30c9d6e2017-03-28 14:54:55 -0700319}