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 package android
import ( import (
"android/soong/starlark_import"
"encoding/json" "encoding/json"
"fmt" "fmt"
"strconv" "strconv"
"android/soong/bazel"
"android/soong/starlark_fmt"
) )
func init() { func init() {
@@ -288,13 +286,17 @@ var LastWithoutModuleLibCoreSystemModules = uncheckedFinalApiLevel(31)
// ReplaceFinalizedCodenames returns the API level number associated with that API level // 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 `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. // If the input is *not* a finalized codename, the input is returned unmodified.
func ReplaceFinalizedCodenames(config Config, raw string) string { func ReplaceFinalizedCodenames(config Config, raw string) (string, error) {
num, ok := getFinalCodenamesMap(config)[raw] finalCodenamesMap, err := getFinalCodenamesMap(config)
if err != nil {
return raw, err
}
num, ok := finalCodenamesMap[raw]
if !ok { 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. // 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 { if !ok {
asInt, err := strconv.Atoi(raw) asInt, err := strconv.Atoi(raw)
if err != nil { if err != nil {
@@ -410,37 +416,21 @@ func GetApiLevelsJson(ctx PathContext) WritablePath {
return PathForOutput(ctx, "api_levels.json") return PathForOutput(ctx, "api_levels.json")
} }
func getApiLevelsMapReleasedVersions() map[string]int { func getApiLevelsMapReleasedVersions() (map[string]int, error) {
return map[string]int{ return starlark_import.GetStarlarkValue[map[string]int]("api_levels_released_versions")
"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,
}
} }
var finalCodenamesMapKey = NewOnceKey("FinalCodenamesMap") 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 // 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 // https://cs.android.com/android/platform/superproject/+/master:build/bazel/rules/common/api.bzl;l=30;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061
return config.Once(finalCodenamesMapKey, func() interface{} { result := config.Once(finalCodenamesMapKey, func() interface{} {
apiLevelsMap := getApiLevelsMapReleasedVersions() apiLevelsMap, err := getApiLevelsMapReleasedVersions()
// TODO: Differentiate "current" and "future". // TODO: Differentiate "current" and "future".
// The code base calls it FutureApiLevel, but the spelling is "current", // 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 // 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 // neither R nor S are final, but the S APIs stop being available in a
// final R build. // final R build.
if Bool(config.productVariables.Platform_sdk_final) { if err == nil && Bool(config.productVariables.Platform_sdk_final) {
apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt() apiLevelsMap["current"] = config.PlatformSdkVersion().FinalOrFutureInt()
} }
return apiLevelsMap return resultStruct{apiLevelsMap, err}
}).(map[string]int) }).(resultStruct)
return result.result, result.err
} }
var apiLevelsMapKey = NewOnceKey("ApiLevelsMap") var apiLevelsMapKey = NewOnceKey("ApiLevelsMap")
// ApiLevelsMap has entries for preview API levels // 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 // 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 // https://cs.android.com/android/platform/superproject/+/master:build/bazel/rules/common/api.bzl;l=23;drc=231c7e8c8038fd478a79eb68aa5b9f5c64e0e061
return config.Once(apiLevelsMapKey, func() interface{} { result := config.Once(apiLevelsMapKey, func() interface{} {
apiLevelsMap := getApiLevelsMapReleasedVersions() apiLevelsMap, err := getApiLevelsMapReleasedVersions()
for i, codename := range config.PlatformVersionAllPreviewCodenames() { if err == nil {
apiLevelsMap[codename] = previewAPILevelBase + i for i, codename := range config.PlatformVersionAllPreviewCodenames() {
apiLevelsMap[codename] = previewAPILevelBase + i
}
} }
return apiLevelsMap return resultStruct{apiLevelsMap, err}
}).(map[string]int) }).(resultStruct)
return result.result, result.err
} }
func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) { 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) apiLevelsJson := GetApiLevelsJson(ctx)
createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap) 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 // IsTargetBuildBeforeTiramisu return true if the target build release for which this snapshot is
// being generated is before Tiramisu, i.e. S. // being generated is before Tiramisu, i.e. S.
IsTargetBuildBeforeTiramisu() bool 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 // 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())) 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 { if err != nil {
return nil, err return nil, err
} }
files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`)) 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 // 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.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("api_levels", "platform_versions.bzl", platformVersionContents(cfg)))
files = append(files, newFile("allowlists", GeneratedBuildFileName, "")) files = append(files, newFile("allowlists", GeneratedBuildFileName, ""))

View File

@@ -157,10 +157,6 @@ func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
dir: "api_levels", dir: "api_levels",
basename: "api_levels.json", basename: "api_levels.json",
}, },
{
dir: "api_levels",
basename: "api_levels.bzl",
},
{ {
dir: "api_levels", dir: "api_levels",
basename: "platform_versions.bzl", 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 // If the min_sdk_version was set then add the canonical representation of the API level to the
// snapshot. // snapshot.
if j.deviceProperties.Min_sdk_version != nil { 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) p.MinSdkVersion = proptools.StringPtr(canonical)
} }

View File

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