diff --git a/android/api_levels.go b/android/api_levels.go index de566250c..27a3b7fd8 100644 --- a/android/api_levels.go +++ b/android/api_levels.go @@ -18,6 +18,8 @@ import ( "encoding/json" "fmt" "strconv" + + "android/soong/starlark_fmt" ) func init() { @@ -380,3 +382,21 @@ func (a *apiLevelsSingleton) GenerateBuildActions(ctx SingletonContext) { apiLevelsJson := GetApiLevelsJson(ctx) createApiLevelsJson(ctx, apiLevelsJson, apiLevelsMap) } + +func printApiLevelsStarlarkDict(config Config) string { + apiLevelsMap := GetApiLevelsMap(config) + valDict := make(map[string]string, len(apiLevelsMap)) + for k, v := range apiLevelsMap { + valDict[k] = strconv.Itoa(v) + } + return starlark_fmt.PrintDict(valDict, 0) +} + +func StarlarkApiLevelConfigs(config Config) string { + return fmt.Sprintf(`# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT. +_api_levels = %s + +api_levels = _api_levels +`, printApiLevelsStarlarkDict(config), + ) +} \ No newline at end of file diff --git a/bp2build/cc_binary_conversion_test.go b/bp2build/cc_binary_conversion_test.go index 8d94079ac..17337f021 100644 --- a/bp2build/cc_binary_conversion_test.go +++ b/bp2build/cc_binary_conversion_test.go @@ -15,12 +15,13 @@ package bp2build import ( - "android/soong/android" - "android/soong/cc" - "android/soong/genrule" "fmt" "strings" "testing" + + "android/soong/android" + "android/soong/cc" + "android/soong/genrule" ) const ( @@ -127,6 +128,8 @@ func TestBasicCcBinary(t *testing.T) { keep_symbols_list: ["symbol"], none: true, }, + sdk_version: "current", + min_sdk_version: "29", } `, targets: []testBazelTarget{ @@ -150,6 +153,8 @@ func TestBasicCcBinary(t *testing.T) { "keep_symbols_list": ["symbol"], "none": True, }`, + "sdk_version": `"current"`, + "min_sdk_version": `"29"`, }, }, }, diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go index 8fde6550f..d84e15650 100644 --- a/bp2build/cc_library_conversion_test.go +++ b/bp2build/cc_library_conversion_test.go @@ -115,6 +115,8 @@ cc_library { }, }, include_build_directory: false, + sdk_version: "current", + min_sdk_version: "29", } `, expectedBazelTargets: makeCcLibraryTargets("foo-lib", attrNameToString{ @@ -140,6 +142,8 @@ cc_library { "//build/bazel/platforms/os:linux_bionic": ["bionic.cpp"], "//conditions:default": [], })`, + "sdk_version": `"current"`, + "min_sdk_version": `"29"`, }), }) } diff --git a/bp2build/cc_library_headers_conversion_test.go b/bp2build/cc_library_headers_conversion_test.go index e4cfa358b..e5bb12010 100644 --- a/bp2build/cc_library_headers_conversion_test.go +++ b/bp2build/cc_library_headers_conversion_test.go @@ -112,6 +112,8 @@ cc_library_headers { export_include_dirs: ["arch_x86_64_exported_include_dir"], }, }, + sdk_version: "current", + min_sdk_version: "29", // TODO: Also support export_header_lib_headers }`, @@ -130,6 +132,8 @@ cc_library_headers { ":lib-1", ":lib-2", ]`, + "sdk_version": `"current"`, + "min_sdk_version": `"29"`, }), }, }) diff --git a/bp2build/cc_library_shared_conversion_test.go b/bp2build/cc_library_shared_conversion_test.go index 78192fed4..22c9dfedf 100644 --- a/bp2build/cc_library_shared_conversion_test.go +++ b/bp2build/cc_library_shared_conversion_test.go @@ -136,6 +136,8 @@ cc_library_shared { "header_lib_1", "header_lib_2" ], + sdk_version: "current", + min_sdk_version: "29", // TODO: Also support export_header_lib_headers }`, @@ -174,6 +176,8 @@ cc_library_shared { ":whole_static_lib_1", ":whole_static_lib_2", ]`, + "sdk_version": `"current"`, + "min_sdk_version": `"29"`, }), }, }) diff --git a/bp2build/cc_library_static_conversion_test.go b/bp2build/cc_library_static_conversion_test.go index 205bf4d28..be10e866d 100644 --- a/bp2build/cc_library_static_conversion_test.go +++ b/bp2build/cc_library_static_conversion_test.go @@ -166,6 +166,8 @@ cc_library_static { "header_lib_1", "header_lib_2" ], + sdk_version: "current", + min_sdk_version: "29", // TODO: Also support export_header_lib_headers }`, @@ -202,6 +204,8 @@ cc_library_static { ":whole_static_lib_1", ":whole_static_lib_2", ]`, + "sdk_version": `"current"`, + "min_sdk_version": `"29"`, }), }, }) diff --git a/bp2build/cc_object_conversion_test.go b/bp2build/cc_object_conversion_test.go index 0a6c3175d..ea5808665 100644 --- a/bp2build/cc_object_conversion_test.go +++ b/bp2build/cc_object_conversion_test.go @@ -55,6 +55,8 @@ func TestCcObjectSimple(t *testing.T) { "a/b/*.c" ], exclude_srcs: ["a/b/exclude.c"], + sdk_version: "current", + min_sdk_version: "29", } `, expectedBazelTargets: []string{ @@ -71,6 +73,8 @@ func TestCcObjectSimple(t *testing.T) { ]`, "srcs": `["a/b/c.c"]`, "system_dynamic_deps": `[]`, + "sdk_version": `"current"`, + "min_sdk_version": `"29"`, }), }, }) diff --git a/bp2build/conversion.go b/bp2build/conversion.go index 3ab846b41..91e614d23 100644 --- a/bp2build/conversion.go +++ b/bp2build/conversion.go @@ -36,6 +36,7 @@ func CreateSoongInjectionFiles(cfg android.Config, metrics CodegenMetrics) []Baz } files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`)) files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent))) + files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(cfg))) return files } diff --git a/bp2build/conversion_test.go b/bp2build/conversion_test.go index c94a9236e..d65ece8c7 100644 --- a/bp2build/conversion_test.go +++ b/bp2build/conversion_test.go @@ -114,6 +114,10 @@ func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) { dir: "api_levels", basename: "api_levels.json", }, + { + dir: "api_levels", + basename: "api_levels.bzl", + }, } if len(files) != len(expectedFilePaths) { diff --git a/cc/binary.go b/cc/binary.go index 9262f217e..89e7262cc 100644 --- a/cc/binary.go +++ b/cc/binary.go @@ -630,6 +630,8 @@ func binaryBp2build(ctx android.TopDownMutatorContext, m *Module, typ string) { }, Features: baseAttrs.features, + + sdkAttributes: bp2BuildParseSdkAttributes(m), } ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{ @@ -673,4 +675,6 @@ type binaryAttributes struct { Strip stripAttributes Features bazel.StringListAttribute + + sdkAttributes } diff --git a/cc/bp2build.go b/cc/bp2build.go index 379d6f246..93d11c74c 100644 --- a/cc/bp2build.go +++ b/cc/bp2build.go @@ -52,6 +52,8 @@ type staticOrSharedAttributes struct { System_dynamic_deps bazel.LabelListAttribute Enabled bazel.BoolAttribute + + sdkAttributes } // groupSrcsByExtension partitions `srcs` into groups based on file extension. @@ -539,6 +541,18 @@ func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) } } +func bp2BuildParseSdkAttributes(module *Module) sdkAttributes { + return sdkAttributes { + Sdk_version: module.Properties.Sdk_version, + Min_sdk_version: module.Properties.Min_sdk_version, + } +} + +type sdkAttributes struct { + Sdk_version *string + Min_sdk_version *string +} + // Convenience struct to hold all attributes parsed from linker properties. type linkerAttributes struct { deps bazel.LabelListAttribute diff --git a/cc/library.go b/cc/library.go index 5fa3471f0..035a90e1c 100644 --- a/cc/library.go +++ b/cc/library.go @@ -316,6 +316,7 @@ func libraryBp2Build(ctx android.TopDownMutatorContext, m *Module) { Implementation_whole_archive_deps: linkerAttrs.implementationWholeArchiveDeps, Whole_archive_deps: *linkerAttrs.wholeArchiveDeps.Clone().Append(staticAttrs.Whole_archive_deps), System_dynamic_deps: *linkerAttrs.systemDynamicDeps.Clone().Append(staticAttrs.System_dynamic_deps), + sdkAttributes: bp2BuildParseSdkAttributes(m), } sharedCommonAttrs := staticOrSharedAttributes{ @@ -331,6 +332,7 @@ func libraryBp2Build(ctx android.TopDownMutatorContext, m *Module) { Implementation_dynamic_deps: *linkerAttrs.implementationDynamicDeps.Clone().Append(sharedAttrs.Implementation_dynamic_deps), Whole_archive_deps: *linkerAttrs.wholeArchiveDeps.Clone().Append(sharedAttrs.Whole_archive_deps), System_dynamic_deps: *linkerAttrs.systemDynamicDeps.Clone().Append(sharedAttrs.System_dynamic_deps), + sdkAttributes: bp2BuildParseSdkAttributes(m), } staticTargetAttrs := &bazelCcLibraryStaticAttributes{ @@ -2481,6 +2483,7 @@ func sharedOrStaticLibraryBp2Build(ctx android.TopDownMutatorContext, module *Mo Whole_archive_deps: linkerAttrs.wholeArchiveDeps, Implementation_whole_archive_deps: linkerAttrs.implementationWholeArchiveDeps, System_dynamic_deps: linkerAttrs.systemDynamicDeps, + sdkAttributes: bp2BuildParseSdkAttributes(module), } var attrs interface{} diff --git a/cc/library_headers.go b/cc/library_headers.go index 5d38fba03..41ebcc766 100644 --- a/cc/library_headers.go +++ b/cc/library_headers.go @@ -117,6 +117,7 @@ type bazelCcLibraryHeadersAttributes struct { Deps bazel.LabelListAttribute Implementation_deps bazel.LabelListAttribute System_dynamic_deps bazel.LabelListAttribute + sdkAttributes } func libraryHeadersBp2Build(ctx android.TopDownMutatorContext, module *Module) { @@ -132,6 +133,7 @@ func libraryHeadersBp2Build(ctx android.TopDownMutatorContext, module *Module) { Deps: linkerAttrs.deps, System_dynamic_deps: linkerAttrs.systemDynamicDeps, Hdrs: baseAttributes.hdrs, + sdkAttributes: bp2BuildParseSdkAttributes(module), } props := bazel.BazelTargetModuleProperties{ diff --git a/cc/object.go b/cc/object.go index fdd0b113c..bd5bd4517 100644 --- a/cc/object.go +++ b/cc/object.go @@ -133,6 +133,7 @@ type bazelObjectAttributes struct { Absolute_includes bazel.StringListAttribute Stl *string Linker_script bazel.LabelAttribute + sdkAttributes } // objectBp2Build is the bp2build converter from cc_object modules to the @@ -191,6 +192,7 @@ func objectBp2Build(ctx android.TopDownMutatorContext, m *Module) { Absolute_includes: compilerAttrs.absoluteIncludes, Stl: compilerAttrs.stl, Linker_script: linkerScript, + sdkAttributes: bp2BuildParseSdkAttributes(m), } props := bazel.BazelTargetModuleProperties{