Use api_levels_released_versions from starlark

Instead of exporting it to soong_injection.

Bug: 279095899
Test: m nothing
Change-Id: I7b93af233b7450848a475512b5f5682ece773c09
Merged-In: I7b93af233b7450848a475512b5f5682ece773c09
This commit is contained in:
Cole Faust 2023-04-28 12:32:27 -07:00
parent 934d5047ef
commit 3486740cc5
6 changed files with 62 additions and 60 deletions

View file

@ -15,12 +15,10 @@
package android
import (
"android/soong/starlark_import"
"encoding/json"
"fmt"
"strconv"
"android/soong/bazel"
"android/soong/starlark_fmt"
)
func init() {
@ -288,13 +286,17 @@ var LastWithoutModuleLibCoreSystemModules = uncheckedFinalApiLevel(31)
// ReplaceFinalizedCodenames returns the API level number associated with that API level
// if the `raw` input is the codename of an API level has been finalized.
// If the input is *not* a finalized codename, the input is returned unmodified.
func ReplaceFinalizedCodenames(config Config, raw string) string {
num, ok := getFinalCodenamesMap(config)[raw]
func ReplaceFinalizedCodenames(config Config, raw string) (string, error) {
finalCodenamesMap, err := getFinalCodenamesMap(config)
if err != nil {
return raw, err
}
num, ok := finalCodenamesMap[raw]
if !ok {
return raw
return raw, nil
}
return strconv.Itoa(num)
return strconv.Itoa(num), nil
}
// ApiLevelFrom converts the given string `raw` to an ApiLevel.
@ -344,7 +346,11 @@ func ApiLevelFromUserWithConfig(config Config, raw string) (ApiLevel, error) {
}
}
canonical, ok := getApiLevelsMapReleasedVersions()[raw]
apiLevelsReleasedVersions, err := getApiLevelsMapReleasedVersions()
if err != nil {
return NoneApiLevel, err
}
canonical, ok := apiLevelsReleasedVersions[raw]
if !ok {
asInt, err := strconv.Atoi(raw)
if err != nil {
@ -410,37 +416,21 @@ func GetApiLevelsJson(ctx PathContext) WritablePath {
return PathForOutput(ctx, "api_levels.json")
}
func getApiLevelsMapReleasedVersions() map[string]int {
return map[string]int{
"G": 9,
"I": 14,
"J": 16,
"J-MR1": 17,
"J-MR2": 18,
"K": 19,
"L": 21,
"L-MR1": 22,
"M": 23,
"N": 24,
"N-MR1": 25,
"O": 26,
"O-MR1": 27,
"P": 28,
"Q": 29,
"R": 30,
"S": 31,
"S-V2": 32,
"Tiramisu": 33,
}
func getApiLevelsMapReleasedVersions() (map[string]int, error) {
return starlark_import.GetStarlarkValue[map[string]int]("api_levels_released_versions")
}
var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap")
func getFinalCodenamesMap(config Config) map[string]int {
func getFinalCodenamesMap(config Config) (map[string]int, error) {
type resultStruct struct {
result map[string]int
err error
}
// This logic is replicated in starlark, if changing logic here update starlark code too
// https://cs.android.com/android/platform/superproject/+/master:build/bazel/rules/common/api.bzl;l=30;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061
return config.Once(finalCodenamesMapKey, func() interface{} {
apiLevelsMap := getApiLevelsMapReleasedVersions()
result := config.Once(finalCodenamesMapKey, func() interface{} {
apiLevelsMap, err := getApiLevelsMapReleasedVersions()
// TODO: Differentiate "current" and "future".
// The code base calls it FutureApiLevel, but the spelling is "current",
@ -453,41 +443,44 @@ func getFinalCodenamesMap(config Config) map[string]int {
// added in S, both of these are usable when building for "current" when
// neither R nor S are final, but the S APIs stop being available in a
// final R build.
if Bool(config.productVariables.Platform_sdk_final) {
if err == nil && Bool(config.productVariables.Platform_sdk_final) {
apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt()
}
return apiLevelsMap
}).(map[string]int)
return resultStruct{apiLevelsMap, err}
}).(resultStruct)
return result.result, result.err
}
var apiLevelsMapKey = NewOnceKey("ApiLevelsMap")
// ApiLevelsMap has entries for preview API levels
func GetApiLevelsMap(config Config) map[string]int {
func GetApiLevelsMap(config Config) (map[string]int, error) {
type resultStruct struct {
result map[string]int
err error
}
// This logic is replicated in starlark, if changing logic here update starlark code too
// https://cs.android.com/android/platform/superproject/+/master:build/bazel/rules/common/api.bzl;l=23;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061
return config.Once(apiLevelsMapKey, func() interface{} {
apiLevelsMap := getApiLevelsMapReleasedVersions()
for i, codename := range config.PlatformVersionAllPreviewCodenames() {
apiLevelsMap[codename] = previewAPILevelBase + i
result := config.Once(apiLevelsMapKey, func() interface{} {
apiLevelsMap, err := getApiLevelsMapReleasedVersions()
if err == nil {
for i, codename := range config.PlatformVersionAllPreviewCodenames() {
apiLevelsMap[codename] = previewAPILevelBase + i
}
}
return apiLevelsMap
}).(map[string]int)
return resultStruct{apiLevelsMap, err}
}).(resultStruct)
return result.result, result.err
}
func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) {
apiLevelsMap := GetApiLevelsMap(ctx.Config())
apiLevelsMap, err := GetApiLevelsMap(ctx.Config())
if err != nil {
ctx.Errorf("%s\n", err)
return
}
apiLevelsJson := GetApiLevelsJson(ctx)
createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap)
}
func StarlarkApiLevelConfigs(config Config) string {
return fmt.Sprintf(bazel.GeneratedBazelFileWarning+`
_api_levels_released_versions = %s
api_levels_released_versions = _api_levels_released_versions
`, starlark_fmt.PrintStringIntDict(getApiLevelsMapReleasedVersions(), 0),
)
}

View file

@ -830,6 +830,9 @@ type SdkMemberContext interface {
// IsTargetBuildBeforeTiramisu return true if the target build release for which this snapshot is
// being generated is before Tiramisu, i.e. S.
IsTargetBuildBeforeTiramisu() bool
// ModuleErrorf reports an error at the line number of the module type in the module definition.
ModuleErrorf(fmt string, args ...interface{})
}
// ExportedComponentsInfo contains information about the components that this module exports to an

View file

@ -59,14 +59,17 @@ func soongInjectionFiles(cfg android.Config, metrics CodegenMetrics) ([]BazelFil
files = append(files, newFile("product_config", "arch_configuration.bzl", android.StarlarkArchConfigurations()))
apiLevelsContent, err := json.Marshal(android.GetApiLevelsMap(cfg))
apiLevelsMap, err := android.GetApiLevelsMap(cfg)
if err != nil {
return nil, err
}
apiLevelsContent, err := json.Marshal(apiLevelsMap)
if err != nil {
return nil, err
}
files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`))
// TODO(b/269691302) value of apiLevelsContent is product variable dependent and should be avoided for soong injection
files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(cfg)))
files = append(files, newFile("api_levels", "platform_versions.bzl", platformVersionContents(cfg)))
files = append(files, newFile("allowlists", GeneratedBuildFileName, ""))

View file

@ -157,10 +157,6 @@ func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
dir: "api_levels",
basename: "api_levels.json",
},
{
dir: "api_levels",
basename: "api_levels.bzl",
},
{
dir: "api_levels",
basename: "platform_versions.bzl",

View file

@ -815,7 +815,10 @@ func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberCo
// If the min_sdk_version was set then add the canonical representation of the API level to the
// snapshot.
if j.deviceProperties.Min_sdk_version != nil {
canonical := android.ReplaceFinalizedCodenames(ctx.SdkModuleContext().Config(), j.minSdkVersion.String())
canonical, err := android.ReplaceFinalizedCodenames(ctx.SdkModuleContext().Config(), j.minSdkVersion.String())
if err != nil {
ctx.ModuleErrorf("%s", err)
}
p.MinSdkVersion = proptools.StringPtr(canonical)
}

View file

@ -1963,6 +1963,10 @@ type memberContext struct {
requiredTraits android.SdkMemberTraitSet
}
func (m *memberContext) ModuleErrorf(fmt string, args ...interface{}) {
m.sdkMemberContext.ModuleErrorf(fmt, args...)
}
func (m *memberContext) SdkModuleContext() android.ModuleContext {
return m.sdkMemberContext
}