blob: 0872066335f99280d5b72da93517353a072ea197 [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
Colin Cross0875c522017-11-28 17:34:01 -080027func ApiLevelsSingleton() Singleton {
Dan Albert30c9d6e2017-03-28 14:54:55 -070028 return &apiLevelsSingleton{}
29}
30
31type apiLevelsSingleton struct{}
32
Colin Cross0875c522017-11-28 17:34:01 -080033func createApiLevelsJson(ctx SingletonContext, file WritablePath,
Dan Albert30c9d6e2017-03-28 14:54:55 -070034 apiLevelsMap map[string]int) {
35
36 jsonStr, err := json.Marshal(apiLevelsMap)
37 if err != nil {
38 ctx.Errorf(err.Error())
39 }
40
Colin Cross0875c522017-11-28 17:34:01 -080041 ctx.Build(pctx, BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070042 Rule: WriteFile,
Colin Cross0875c522017-11-28 17:34:01 -080043 Description: "generate " + file.Base(),
44 Output: file,
Dan Albert30c9d6e2017-03-28 14:54:55 -070045 Args: map[string]string{
46 "content": string(jsonStr[:]),
47 },
48 })
49}
50
Colin Cross0875c522017-11-28 17:34:01 -080051func GetApiLevelsJson(ctx PathContext) WritablePath {
Dan Albert30c9d6e2017-03-28 14:54:55 -070052 return PathForOutput(ctx, "api_levels.json")
53}
54
Colin Cross571cccf2019-02-04 11:22:08 -080055var apiLevelsMapKey = NewOnceKey("ApiLevelsMap")
56
Dan Albert6bc5b832018-05-03 15:42:34 -070057func getApiLevelsMap(config Config) map[string]int {
Colin Cross571cccf2019-02-04 11:22:08 -080058 return config.Once(apiLevelsMapKey, func() interface{} {
Dan Albert6bc5b832018-05-03 15:42:34 -070059 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 Pedowitz851de712019-05-11 17:02:50 +000075 "Q": 29,
Svet Ganov3b0b84b2020-04-29 17:14:15 -070076 "R": 30,
Dan Albert6bc5b832018-05-03 15:42:34 -070077 }
Jooyung Han424175d2020-04-08 09:22:26 +090078 for i, codename := range config.PlatformVersionActiveCodenames() {
Dan Albert6bc5b832018-05-03 15:42:34 -070079 apiLevelsMap[codename] = baseApiLevel + i
80 }
Dan Albert30c9d6e2017-03-28 14:54:55 -070081
Dan Albert6bc5b832018-05-03 15:42:34 -070082 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 Han29e91d22020-04-02 01:41:41 +090089// * "current" is mapped to FutureApiLevel(10000)
90// * "minimum" is NDK specific and not handled with this. (refer normalizeNdkApiLevel in cc.go)
Colin Cross0ea8ba82019-06-06 14:33:29 -070091func ApiStrToNum(ctx BaseModuleContext, apiLevel string) (int, error) {
Jooyung Han29e91d22020-04-02 01:41:41 +090092 if apiLevel == "current" {
93 return FutureApiLevel, nil
94 }
95 if num, ok := getApiLevelsMap(ctx.Config())[apiLevel]; ok {
Dan Albert6bc5b832018-05-03 15:42:34 -070096 return num, nil
97 }
Jooyung Han29e91d22020-04-02 01:41:41 +090098 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 Albert6bc5b832018-05-03 15:42:34 -0700102}
103
104func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) {
105 apiLevelsMap := getApiLevelsMap(ctx.Config())
Dan Albert30c9d6e2017-03-28 14:54:55 -0700106 apiLevelsJson := GetApiLevelsJson(ctx)
Colin Cross0875c522017-11-28 17:34:01 -0800107 createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap)
Dan Albert30c9d6e2017-03-28 14:54:55 -0700108}