Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
Jooyung Han | 29e91d2 | 2020-04-02 01:41:41 +0900 | [diff] [blame] | 19 | "fmt" |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 20 | "strconv" |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | func init() { |
| 24 | RegisterSingletonType("api_levels", ApiLevelsSingleton) |
| 25 | } |
| 26 | |
Jooyung Han | 11b0fbd | 2021-02-05 02:28:22 +0900 | [diff] [blame^] | 27 | const previewAPILevelBase = 9000 |
| 28 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 29 | // An API level, which may be a finalized (numbered) API, a preview (codenamed) |
| 30 | // API, or the future API level (10000). Can be parsed from a string with |
| 31 | // ApiLevelFromUser or ApiLevelOrPanic. |
| 32 | // |
| 33 | // The different *types* of API levels are handled separately. Currently only |
| 34 | // Java has these, and they're managed with the sdkKind enum of the sdkSpec. A |
| 35 | // future cleanup should be to migrate sdkSpec to using ApiLevel instead of its |
| 36 | // sdkVersion int, and to move sdkSpec into this package. |
| 37 | type ApiLevel struct { |
| 38 | // The string representation of the API level. |
| 39 | value string |
| 40 | |
| 41 | // A number associated with the API level. The exact value depends on |
| 42 | // whether this API level is a preview or final API. |
| 43 | // |
| 44 | // For final API levels, this is the assigned version number. |
| 45 | // |
| 46 | // For preview API levels, this value has no meaning except to index known |
| 47 | // previews to determine ordering. |
| 48 | number int |
| 49 | |
| 50 | // Identifies this API level as either a preview or final API level. |
| 51 | isPreview bool |
| 52 | } |
| 53 | |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 54 | func (this ApiLevel) FinalOrFutureInt() int { |
| 55 | if this.IsPreview() { |
Dan Albert | 0b176c8 | 2020-07-23 16:43:25 -0700 | [diff] [blame] | 56 | return FutureApiLevelInt |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 57 | } else { |
| 58 | return this.number |
| 59 | } |
| 60 | } |
| 61 | |
Jooyung Han | 11b0fbd | 2021-02-05 02:28:22 +0900 | [diff] [blame^] | 62 | // FinalOrPreviewInt distinguishes preview versions from "current" (future). |
| 63 | // This is for "native" stubs and should be in sync with ndkstubgen/getApiLevelsMap(). |
| 64 | // - "current" -> future (10000) |
| 65 | // - preview codenames -> preview base (9000) + index |
| 66 | // - otherwise -> cast to int |
| 67 | func (this ApiLevel) FinalOrPreviewInt() int { |
| 68 | if this.IsCurrent() { |
| 69 | return this.number |
| 70 | } |
| 71 | if this.IsPreview() { |
| 72 | return previewAPILevelBase + this.number |
| 73 | } |
| 74 | return this.number |
| 75 | } |
| 76 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 77 | // Returns the canonical name for this API level. For a finalized API level |
| 78 | // this will be the API number as a string. For a preview API level this |
| 79 | // will be the codename, or "current". |
| 80 | func (this ApiLevel) String() string { |
| 81 | return this.value |
| 82 | } |
| 83 | |
| 84 | // Returns true if this is a non-final API level. |
| 85 | func (this ApiLevel) IsPreview() bool { |
| 86 | return this.isPreview |
| 87 | } |
| 88 | |
| 89 | // Returns true if this is the unfinalized "current" API level. This means |
| 90 | // different things across Java and native. Java APIs do not use explicit |
| 91 | // codenames, so all non-final codenames are grouped into "current". For native |
| 92 | // explicit codenames are typically used, and current is the union of all |
| 93 | // non-final APIs, including those that may not yet be in any codename. |
| 94 | // |
| 95 | // Note that in a build where the platform is final, "current" will not be a |
| 96 | // preview API level but will instead be canonicalized to the final API level. |
| 97 | func (this ApiLevel) IsCurrent() bool { |
| 98 | return this.value == "current" |
| 99 | } |
| 100 | |
Jooyung Han | ed124c3 | 2021-01-26 11:43:46 +0900 | [diff] [blame] | 101 | func (this ApiLevel) IsNone() bool { |
| 102 | return this.number == -1 |
| 103 | } |
| 104 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 105 | // Returns -1 if the current API level is less than the argument, 0 if they |
| 106 | // are equal, and 1 if it is greater than the argument. |
| 107 | func (this ApiLevel) CompareTo(other ApiLevel) int { |
| 108 | if this.IsPreview() && !other.IsPreview() { |
| 109 | return 1 |
| 110 | } else if !this.IsPreview() && other.IsPreview() { |
| 111 | return -1 |
| 112 | } |
| 113 | |
| 114 | if this.number < other.number { |
| 115 | return -1 |
| 116 | } else if this.number == other.number { |
| 117 | return 0 |
| 118 | } else { |
| 119 | return 1 |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func (this ApiLevel) EqualTo(other ApiLevel) bool { |
| 124 | return this.CompareTo(other) == 0 |
| 125 | } |
| 126 | |
| 127 | func (this ApiLevel) GreaterThan(other ApiLevel) bool { |
| 128 | return this.CompareTo(other) > 0 |
| 129 | } |
| 130 | |
| 131 | func (this ApiLevel) GreaterThanOrEqualTo(other ApiLevel) bool { |
| 132 | return this.CompareTo(other) >= 0 |
| 133 | } |
| 134 | |
| 135 | func (this ApiLevel) LessThan(other ApiLevel) bool { |
| 136 | return this.CompareTo(other) < 0 |
| 137 | } |
| 138 | |
| 139 | func (this ApiLevel) LessThanOrEqualTo(other ApiLevel) bool { |
| 140 | return this.CompareTo(other) <= 0 |
| 141 | } |
| 142 | |
| 143 | func uncheckedFinalApiLevel(num int) ApiLevel { |
| 144 | return ApiLevel{ |
| 145 | value: strconv.Itoa(num), |
| 146 | number: num, |
| 147 | isPreview: false, |
| 148 | } |
| 149 | } |
| 150 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 151 | var NoneApiLevel = ApiLevel{ |
| 152 | value: "(no version)", |
| 153 | // Not 0 because we don't want this to compare equal with the first preview. |
| 154 | number: -1, |
| 155 | isPreview: true, |
| 156 | } |
| 157 | |
| 158 | // The first version that introduced 64-bit ABIs. |
| 159 | var FirstLp64Version = uncheckedFinalApiLevel(21) |
| 160 | |
| 161 | // The first API level that does not require NDK code to link |
| 162 | // libandroid_support. |
| 163 | var FirstNonLibAndroidSupportVersion = uncheckedFinalApiLevel(21) |
| 164 | |
| 165 | // If the `raw` input is the codename of an API level has been finalized, this |
| 166 | // function returns the API level number associated with that API level. If the |
| 167 | // input is *not* a finalized codename, the input is returned unmodified. |
| 168 | // |
| 169 | // For example, at the time of writing, R has been finalized as API level 30, |
| 170 | // but S is in development so it has no number assigned. For the following |
| 171 | // inputs: |
| 172 | // |
| 173 | // * "30" -> "30" |
| 174 | // * "R" -> "30" |
| 175 | // * "S" -> "S" |
Colin Cross | 9f720ce | 2020-10-02 10:26:04 -0700 | [diff] [blame] | 176 | func ReplaceFinalizedCodenames(ctx PathContext, raw string) string { |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 177 | num, ok := getFinalCodenamesMap(ctx.Config())[raw] |
| 178 | if !ok { |
| 179 | return raw |
| 180 | } |
| 181 | |
| 182 | return strconv.Itoa(num) |
| 183 | } |
| 184 | |
| 185 | // Converts the given string `raw` to an ApiLevel, possibly returning an error. |
| 186 | // |
| 187 | // `raw` must be non-empty. Passing an empty string results in a panic. |
| 188 | // |
| 189 | // "current" will return CurrentApiLevel, which is the ApiLevel associated with |
| 190 | // an arbitrary future release (often referred to as API level 10000). |
| 191 | // |
| 192 | // Finalized codenames will be interpreted as their final API levels, not the |
| 193 | // preview of the associated releases. R is now API 30, not the R preview. |
| 194 | // |
| 195 | // Future codenames return a preview API level that has no associated integer. |
| 196 | // |
| 197 | // Inputs that are not "current", known previews, or convertible to an integer |
| 198 | // will return an error. |
Colin Cross | 9f720ce | 2020-10-02 10:26:04 -0700 | [diff] [blame] | 199 | func ApiLevelFromUser(ctx PathContext, raw string) (ApiLevel, error) { |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 200 | if raw == "" { |
| 201 | panic("API level string must be non-empty") |
| 202 | } |
| 203 | |
| 204 | if raw == "current" { |
Dan Albert | 0b176c8 | 2020-07-23 16:43:25 -0700 | [diff] [blame] | 205 | return FutureApiLevel, nil |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | for _, preview := range ctx.Config().PreviewApiLevels() { |
| 209 | if raw == preview.String() { |
| 210 | return preview, nil |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | canonical := ReplaceFinalizedCodenames(ctx, raw) |
| 215 | asInt, err := strconv.Atoi(canonical) |
| 216 | if err != nil { |
| 217 | return NoneApiLevel, fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", canonical) |
| 218 | } |
| 219 | |
| 220 | apiLevel := uncheckedFinalApiLevel(asInt) |
| 221 | return apiLevel, nil |
| 222 | } |
| 223 | |
| 224 | // Converts an API level string `raw` into an ApiLevel in the same method as |
| 225 | // `ApiLevelFromUser`, but the input is assumed to have no errors and any errors |
| 226 | // will panic instead of returning an error. |
Colin Cross | 9f720ce | 2020-10-02 10:26:04 -0700 | [diff] [blame] | 227 | func ApiLevelOrPanic(ctx PathContext, raw string) ApiLevel { |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 228 | value, err := ApiLevelFromUser(ctx, raw) |
| 229 | if err != nil { |
| 230 | panic(err.Error()) |
| 231 | } |
| 232 | return value |
| 233 | } |
| 234 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 235 | func ApiLevelsSingleton() Singleton { |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 236 | return &apiLevelsSingleton{} |
| 237 | } |
| 238 | |
| 239 | type apiLevelsSingleton struct{} |
| 240 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 241 | func createApiLevelsJson(ctx SingletonContext, file WritablePath, |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 242 | apiLevelsMap map[string]int) { |
| 243 | |
| 244 | jsonStr, err := json.Marshal(apiLevelsMap) |
| 245 | if err != nil { |
| 246 | ctx.Errorf(err.Error()) |
| 247 | } |
| 248 | |
Colin Cross | cf371cc | 2020-11-13 11:48:42 -0800 | [diff] [blame] | 249 | WriteFileRule(ctx, file, string(jsonStr)) |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 252 | func GetApiLevelsJson(ctx PathContext) WritablePath { |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 253 | return PathForOutput(ctx, "api_levels.json") |
| 254 | } |
| 255 | |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 256 | var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap") |
| 257 | |
| 258 | func getFinalCodenamesMap(config Config) map[string]int { |
| 259 | return config.Once(finalCodenamesMapKey, func() interface{} { |
| 260 | apiLevelsMap := map[string]int{ |
| 261 | "G": 9, |
| 262 | "I": 14, |
| 263 | "J": 16, |
| 264 | "J-MR1": 17, |
| 265 | "J-MR2": 18, |
| 266 | "K": 19, |
| 267 | "L": 21, |
| 268 | "L-MR1": 22, |
| 269 | "M": 23, |
| 270 | "N": 24, |
| 271 | "N-MR1": 25, |
| 272 | "O": 26, |
| 273 | "O-MR1": 27, |
| 274 | "P": 28, |
| 275 | "Q": 29, |
Dan Albert | dbc008f | 2020-09-16 11:35:00 -0700 | [diff] [blame] | 276 | "R": 30, |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 279 | // TODO: Differentiate "current" and "future". |
| 280 | // The code base calls it FutureApiLevel, but the spelling is "current", |
| 281 | // and these are really two different things. When defining APIs it |
| 282 | // means the API has not yet been added to a specific release. When |
| 283 | // choosing an API level to build for it means that the future API level |
| 284 | // should be used, except in the case where the build is finalized in |
| 285 | // which case the platform version should be used. This is *weird*, |
| 286 | // because in the circumstance where API foo was added in R and bar was |
| 287 | // added in S, both of these are usable when building for "current" when |
| 288 | // neither R nor S are final, but the S APIs stop being available in a |
| 289 | // final R build. |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 290 | if Bool(config.productVariables.Platform_sdk_final) { |
Dan Albert | 4f378d7 | 2020-07-23 17:32:15 -0700 | [diff] [blame] | 291 | apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt() |
Dan Albert | 1a24627 | 2020-07-06 14:49:35 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | return apiLevelsMap |
| 295 | }).(map[string]int) |
| 296 | } |
| 297 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 298 | var apiLevelsMapKey = NewOnceKey("ApiLevelsMap") |
| 299 | |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 300 | func getApiLevelsMap(config Config) map[string]int { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 301 | return config.Once(apiLevelsMapKey, func() interface{} { |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 302 | apiLevelsMap := map[string]int{ |
| 303 | "G": 9, |
| 304 | "I": 14, |
| 305 | "J": 16, |
| 306 | "J-MR1": 17, |
| 307 | "J-MR2": 18, |
| 308 | "K": 19, |
| 309 | "L": 21, |
| 310 | "L-MR1": 22, |
| 311 | "M": 23, |
| 312 | "N": 24, |
| 313 | "N-MR1": 25, |
| 314 | "O": 26, |
| 315 | "O-MR1": 27, |
| 316 | "P": 28, |
Ian Pedowitz | 851de71 | 2019-05-11 17:02:50 +0000 | [diff] [blame] | 317 | "Q": 29, |
Svet Ganov | 3b0b84b | 2020-04-29 17:14:15 -0700 | [diff] [blame] | 318 | "R": 30, |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 319 | } |
Jooyung Han | 424175d | 2020-04-08 09:22:26 +0900 | [diff] [blame] | 320 | for i, codename := range config.PlatformVersionActiveCodenames() { |
Jooyung Han | 11b0fbd | 2021-02-05 02:28:22 +0900 | [diff] [blame^] | 321 | apiLevelsMap[codename] = previewAPILevelBase + i |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 322 | } |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 323 | |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 324 | return apiLevelsMap |
| 325 | }).(map[string]int) |
| 326 | } |
| 327 | |
Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 328 | func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) { |
| 329 | apiLevelsMap := getApiLevelsMap(ctx.Config()) |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 330 | apiLevelsJson := GetApiLevelsJson(ctx) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 331 | createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap) |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 332 | } |