Merge "Support tailoring clang --target flag based on min sdk version." am: ba4638f529 am: 41b4e249ce am: 0db78356ba

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2011702

Change-Id: I3567734c306f7f2659a879fc2b2d95e610bca1b9
This commit is contained in:
Yu Liu 2022-03-19 16:41:20 +00:00 committed by Automerger Merge Worker
commit 6b9500730d
14 changed files with 78 additions and 3 deletions

View file

@ -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),
)
}

View file

@ -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"`,
},
},
},

View file

@ -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"`,
}),
})
}

View file

@ -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"`,
}),
},
})

View file

@ -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"`,
}),
},
})

View file

@ -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"`,
}),
},
})

View file

@ -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"`,
}),
},
})

View file

@ -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
}

View file

@ -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) {

View file

@ -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
}

View file

@ -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

View file

@ -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{}

View file

@ -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{

View file

@ -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{