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 15package android 16 17import ( 18 "encoding/json" 19 "fmt" 20 "strconv" 21) 22 23func init() { 24 RegisterSingletonType("api_levels", ApiLevelsSingleton) 25} 26 27const previewAPILevelBase = 9000 28 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. 37type 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 54func (this ApiLevel) FinalOrFutureInt() int { 55 if this.IsPreview() { 56 return FutureApiLevelInt 57 } else { 58 return this.number 59 } 60} 61 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 67func (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 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". 80func (this ApiLevel) String() string { 81 return this.value 82} 83 84// Returns true if this is a non-final API level. 85func (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. 97func (this ApiLevel) IsCurrent() bool { 98 return this.value == "current" 99} 100 101func (this ApiLevel) IsNone() bool { 102 return this.number == -1 103} 104 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. 107func (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 123func (this ApiLevel) EqualTo(other ApiLevel) bool { 124 return this.CompareTo(other) == 0 125} 126 127func (this ApiLevel) GreaterThan(other ApiLevel) bool { 128 return this.CompareTo(other) > 0 129} 130 131func (this ApiLevel) GreaterThanOrEqualTo(other ApiLevel) bool { 132 return this.CompareTo(other) >= 0 133} 134 135func (this ApiLevel) LessThan(other ApiLevel) bool { 136 return this.CompareTo(other) < 0 137} 138 139func (this ApiLevel) LessThanOrEqualTo(other ApiLevel) bool { 140 return this.CompareTo(other) <= 0 141} 142 143func uncheckedFinalApiLevel(num int) ApiLevel { 144 return ApiLevel{ 145 value: strconv.Itoa(num), 146 number: num, 147 isPreview: false, 148 } 149} 150 151var 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. 159var FirstLp64Version = uncheckedFinalApiLevel(21) 160 161// Android has had various kinds of packed relocations over the years 162// (http://b/187907243). 163// 164// API level 30 is where the now-standard SHT_RELR is available. 165var FirstShtRelrVersion = uncheckedFinalApiLevel(30) 166 167// API level 28 introduced SHT_RELR when it was still Android-only, and used an 168// Android-specific relocation. 169var FirstAndroidRelrVersion = uncheckedFinalApiLevel(28) 170 171// API level 23 was when we first had the Chrome relocation packer, which is 172// obsolete and has been removed, but lld can now generate compatible packed 173// relocations itself. 174var FirstPackedRelocationsVersion = uncheckedFinalApiLevel(23) 175 176// The first API level that does not require NDK code to link 177// libandroid_support. 178var FirstNonLibAndroidSupportVersion = uncheckedFinalApiLevel(21) 179 180// If the `raw` input is the codename of an API level has been finalized, this 181// function returns the API level number associated with that API level. If the 182// input is *not* a finalized codename, the input is returned unmodified. 183// 184// For example, at the time of writing, R has been finalized as API level 30, 185// but S is in development so it has no number assigned. For the following 186// inputs: 187// 188// * "30" -> "30" 189// * "R" -> "30" 190// * "S" -> "S" 191func ReplaceFinalizedCodenames(ctx PathContext, raw string) string { 192 num, ok := getFinalCodenamesMap(ctx.Config())[raw] 193 if !ok { 194 return raw 195 } 196 197 return strconv.Itoa(num) 198} 199 200// Converts the given string `raw` to an ApiLevel, possibly returning an error. 201// 202// `raw` must be non-empty. Passing an empty string results in a panic. 203// 204// "current" will return CurrentApiLevel, which is the ApiLevel associated with 205// an arbitrary future release (often referred to as API level 10000). 206// 207// Finalized codenames will be interpreted as their final API levels, not the 208// preview of the associated releases. R is now API 30, not the R preview. 209// 210// Future codenames return a preview API level that has no associated integer. 211// 212// Inputs that are not "current", known previews, or convertible to an integer 213// will return an error. 214func ApiLevelFromUser(ctx PathContext, raw string) (ApiLevel, error) { 215 if raw == "" { 216 panic("API level string must be non-empty") 217 } 218 219 if raw == "current" { 220 return FutureApiLevel, nil 221 } 222 223 for _, preview := range ctx.Config().PreviewApiLevels() { 224 if raw == preview.String() { 225 return preview, nil 226 } 227 } 228 229 canonical := ReplaceFinalizedCodenames(ctx, raw) 230 asInt, err := strconv.Atoi(canonical) 231 if err != nil { 232 return NoneApiLevel, fmt.Errorf("%q could not be parsed as an integer and is not a recognized codename", canonical) 233 } 234 235 apiLevel := uncheckedFinalApiLevel(asInt) 236 return apiLevel, nil 237} 238 239// Converts an API level string `raw` into an ApiLevel in the same method as 240// `ApiLevelFromUser`, but the input is assumed to have no errors and any errors 241// will panic instead of returning an error. 242func ApiLevelOrPanic(ctx PathContext, raw string) ApiLevel { 243 value, err := ApiLevelFromUser(ctx, raw) 244 if err != nil { 245 panic(err.Error()) 246 } 247 return value 248} 249 250func ApiLevelsSingleton() Singleton { 251 return &apiLevelsSingleton{} 252} 253 254type apiLevelsSingleton struct{} 255 256func createApiLevelsJson(ctx SingletonContext, file WritablePath, 257 apiLevelsMap map[string]int) { 258 259 jsonStr, err := json.Marshal(apiLevelsMap) 260 if err != nil { 261 ctx.Errorf(err.Error()) 262 } 263 264 WriteFileRule(ctx, file, string(jsonStr)) 265} 266 267func GetApiLevelsJson(ctx PathContext) WritablePath { 268 return PathForOutput(ctx, "api_levels.json") 269} 270 271var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap") 272 273func getFinalCodenamesMap(config Config) map[string]int { 274 return config.Once(finalCodenamesMapKey, func() interface{} { 275 apiLevelsMap := map[string]int{ 276 "G": 9, 277 "I": 14, 278 "J": 16, 279 "J-MR1": 17, 280 "J-MR2": 18, 281 "K": 19, 282 "L": 21, 283 "L-MR1": 22, 284 "M": 23, 285 "N": 24, 286 "N-MR1": 25, 287 "O": 26, 288 "O-MR1": 27, 289 "P": 28, 290 "Q": 29, 291 "R": 30, 292 "S": 31, 293 "S-V2": 32, 294 } 295 296 // TODO: Differentiate "current" and "future". 297 // The code base calls it FutureApiLevel, but the spelling is "current", 298 // and these are really two different things. When defining APIs it 299 // means the API has not yet been added to a specific release. When 300 // choosing an API level to build for it means that the future API level 301 // should be used, except in the case where the build is finalized in 302 // which case the platform version should be used. This is *weird*, 303 // because in the circumstance where API foo was added in R and bar was 304 // added in S, both of these are usable when building for "current" when 305 // neither R nor S are final, but the S APIs stop being available in a 306 // final R build. 307 if Bool(config.productVariables.Platform_sdk_final) { 308 apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt() 309 } 310 311 return apiLevelsMap 312 }).(map[string]int) 313} 314 315var apiLevelsMapKey = NewOnceKey("ApiLevelsMap") 316 317func getApiLevelsMap(config Config) map[string]int { 318 return config.Once(apiLevelsMapKey, func() interface{} { 319 apiLevelsMap := map[string]int{ 320 "G": 9, 321 "I": 14, 322 "J": 16, 323 "J-MR1": 17, 324 "J-MR2": 18, 325 "K": 19, 326 "L": 21, 327 "L-MR1": 22, 328 "M": 23, 329 "N": 24, 330 "N-MR1": 25, 331 "O": 26, 332 "O-MR1": 27, 333 "P": 28, 334 "Q": 29, 335 "R": 30, 336 "S": 31, 337 "S-V2": 32, 338 } 339 for i, codename := range config.PlatformVersionActiveCodenames() { 340 apiLevelsMap[codename] = previewAPILevelBase + i 341 } 342 343 return apiLevelsMap 344 }).(map[string]int) 345} 346 347func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) { 348 apiLevelsMap := getApiLevelsMap(ctx.Config()) 349 apiLevelsJson := GetApiLevelsJson(ctx) 350 createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap) 351} 352