Loading android/api_levels.go +46 −53 Original line number Diff line number Diff line Loading @@ -15,12 +15,10 @@ package android import ( "android/soong/starlark_import" "encoding/json" "fmt" "strconv" "android/soong/bazel" "android/soong/starlark_fmt" ) func init() { Loading Loading @@ -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. Loading Loading @@ -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 { Loading Loading @@ -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", Loading @@ -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() 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), ) } android/sdk.go +3 −0 Original line number Diff line number Diff line Loading @@ -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 Loading bp2build/conversion.go +5 −2 Original line number Diff line number Diff line Loading @@ -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, "")) Loading bp2build/conversion_test.go +0 −4 Original line number Diff line number Diff line Loading @@ -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", Loading java/java.go +4 −1 Original line number Diff line number Diff line Loading @@ -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) } Loading Loading
android/api_levels.go +46 −53 Original line number Diff line number Diff line Loading @@ -15,12 +15,10 @@ package android import ( "android/soong/starlark_import" "encoding/json" "fmt" "strconv" "android/soong/bazel" "android/soong/starlark_fmt" ) func init() { Loading Loading @@ -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. Loading Loading @@ -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 { Loading Loading @@ -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", Loading @@ -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() 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), ) }
android/sdk.go +3 −0 Original line number Diff line number Diff line Loading @@ -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 Loading
bp2build/conversion.go +5 −2 Original line number Diff line number Diff line Loading @@ -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, "")) Loading
bp2build/conversion_test.go +0 −4 Original line number Diff line number Diff line Loading @@ -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", Loading
java/java.go +4 −1 Original line number Diff line number Diff line Loading @@ -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) } Loading