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