| 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 |  | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 27 | func ApiLevelsSingleton() Singleton { | 
| Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 28 | return &apiLevelsSingleton{} | 
|  | 29 | } | 
|  | 30 |  | 
|  | 31 | type apiLevelsSingleton struct{} | 
|  | 32 |  | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 33 | func createApiLevelsJson(ctx SingletonContext, file WritablePath, | 
| Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 34 | apiLevelsMap map[string]int) { | 
|  | 35 |  | 
|  | 36 | jsonStr, err := json.Marshal(apiLevelsMap) | 
|  | 37 | if err != nil { | 
|  | 38 | ctx.Errorf(err.Error()) | 
|  | 39 | } | 
|  | 40 |  | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 41 | ctx.Build(pctx, BuildParams{ | 
| Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 42 | Rule:        WriteFile, | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 43 | Description: "generate " + file.Base(), | 
|  | 44 | Output:      file, | 
| Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 45 | Args: map[string]string{ | 
|  | 46 | "content": string(jsonStr[:]), | 
|  | 47 | }, | 
|  | 48 | }) | 
|  | 49 | } | 
|  | 50 |  | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 51 | func GetApiLevelsJson(ctx PathContext) WritablePath { | 
| Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 52 | return PathForOutput(ctx, "api_levels.json") | 
|  | 53 | } | 
|  | 54 |  | 
| Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 55 | var apiLevelsMapKey = NewOnceKey("ApiLevelsMap") | 
|  | 56 |  | 
| Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 57 | func getApiLevelsMap(config Config) map[string]int { | 
| Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 58 | return config.Once(apiLevelsMapKey, func() interface{} { | 
| Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 59 | baseApiLevel := 9000 | 
|  | 60 | apiLevelsMap := map[string]int{ | 
|  | 61 | "G":     9, | 
|  | 62 | "I":     14, | 
|  | 63 | "J":     16, | 
|  | 64 | "J-MR1": 17, | 
|  | 65 | "J-MR2": 18, | 
|  | 66 | "K":     19, | 
|  | 67 | "L":     21, | 
|  | 68 | "L-MR1": 22, | 
|  | 69 | "M":     23, | 
|  | 70 | "N":     24, | 
|  | 71 | "N-MR1": 25, | 
|  | 72 | "O":     26, | 
|  | 73 | "O-MR1": 27, | 
|  | 74 | "P":     28, | 
| Ian Pedowitz | 851de71 | 2019-05-11 17:02:50 +0000 | [diff] [blame] | 75 | "Q":     29, | 
| Svet Ganov | 3b0b84b | 2020-04-29 17:14:15 -0700 | [diff] [blame] | 76 | "R":     30, | 
| Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 77 | } | 
| Jooyung Han | 424175d | 2020-04-08 09:22:26 +0900 | [diff] [blame] | 78 | for i, codename := range config.PlatformVersionActiveCodenames() { | 
| Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 79 | apiLevelsMap[codename] = baseApiLevel + i | 
|  | 80 | } | 
| Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 81 |  | 
| Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 82 | return apiLevelsMap | 
|  | 83 | }).(map[string]int) | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | // Converts an API level string into its numeric form. | 
|  | 87 | // * Codenames are decoded. | 
|  | 88 | // * Numeric API levels are simply converted. | 
| Jooyung Han | 29e91d2 | 2020-04-02 01:41:41 +0900 | [diff] [blame] | 89 | // * "current" is mapped to FutureApiLevel(10000) | 
|  | 90 | // * "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] | 91 | func ApiStrToNum(ctx BaseModuleContext, apiLevel string) (int, error) { | 
| Jooyung Han | 29e91d2 | 2020-04-02 01:41:41 +0900 | [diff] [blame] | 92 | if apiLevel == "current" { | 
|  | 93 | return FutureApiLevel, nil | 
|  | 94 | } | 
|  | 95 | if num, ok := getApiLevelsMap(ctx.Config())[apiLevel]; ok { | 
| Dan Albert | 6bc5b83 | 2018-05-03 15:42:34 -0700 | [diff] [blame] | 96 | return num, nil | 
|  | 97 | } | 
| Jooyung Han | 29e91d2 | 2020-04-02 01:41:41 +0900 | [diff] [blame] | 98 | if num, err := strconv.Atoi(apiLevel); err == nil { | 
|  | 99 | return num, nil | 
|  | 100 | } | 
|  | 101 | 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] | 102 | } | 
|  | 103 |  | 
|  | 104 | func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) { | 
|  | 105 | apiLevelsMap := getApiLevelsMap(ctx.Config()) | 
| Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 106 | apiLevelsJson := GetApiLevelsJson(ctx) | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 107 | createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap) | 
| Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 108 | } |