Merge "bp2build of libraries with stubs should handle non-implementation deps"

This commit is contained in:
Yu Liu
2023-02-22 23:35:04 +00:00
committed by Gerrit Code Review
2 changed files with 97 additions and 37 deletions

View File

@@ -4084,3 +4084,56 @@ cc_library {
},
})
}
func TestCcLibraryInApexWithStubSharedLibs(t *testing.T) {
runCcLibrarySharedTestCase(t, Bp2buildTestCase{
Description: "cc_library with in apex with stub shared_libs and export_shared_lib_headers",
ModuleTypeUnderTest: "cc_library",
ModuleTypeUnderTestFactory: cc.LibraryFactory,
Blueprint: `
cc_library {
name: "barlib",
stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
bazel_module: { bp2build_available: false },
}
cc_library {
name: "bazlib",
stubs: { symbol_file: "bar.map.txt", versions: ["28", "29", "current"] },
bazel_module: { bp2build_available: false },
}
cc_library {
name: "foo",
shared_libs: ["barlib", "bazlib"],
export_shared_lib_headers: ["bazlib"],
apex_available: [
"apex_available:platform",
],
}`,
ExpectedBazelTargets: []string{
MakeBazelTarget("cc_library_static", "foo_bp2build_cc_library_static", AttrNameToString{
"implementation_dynamic_deps": `select({
"//build/bazel/rules/apex:android-in_apex": [":barlib_stub_libs_current"],
"//conditions:default": [":barlib"],
})`,
"dynamic_deps": `select({
"//build/bazel/rules/apex:android-in_apex": [":bazlib_stub_libs_current"],
"//conditions:default": [":bazlib"],
})`,
"local_includes": `["."]`,
"tags": `["apex_available=apex_available:platform"]`,
}),
MakeBazelTarget("cc_library_shared", "foo", AttrNameToString{
"implementation_dynamic_deps": `select({
"//build/bazel/rules/apex:android-in_apex": [":barlib_stub_libs_current"],
"//conditions:default": [":barlib"],
})`,
"dynamic_deps": `select({
"//build/bazel/rules/apex:android-in_apex": [":bazlib_stub_libs_current"],
"//conditions:default": [":bazlib"],
})`,
"local_includes": `["."]`,
"tags": `["apex_available=apex_available:platform"]`,
}),
},
})
}

View File

@@ -1080,43 +1080,13 @@ func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversion
la.resolveTargetApexProp(ctx, props)
if axis == bazel.NoConfigAxis || (axis == bazel.OsConfigurationAxis && config == bazel.OsAndroid) {
// If a dependency in la.implementationDynamicDeps has stubs, its stub variant should be
// used when the dependency is linked in a APEX. The dependencies in NoConfigAxis and
// OsConfigurationAxis/OsAndroid are grouped by having stubs or not, so Bazel select()
// statement can be used to choose source/stub variants of them.
depsWithStubs := []bazel.Label{}
for _, l := range sharedDeps.implementation.Includes {
dep, _ := ctx.ModuleFromName(l.OriginalModuleName)
if m, ok := dep.(*Module); ok && m.HasStubsVariants() {
depsWithStubs = append(depsWithStubs, l)
}
}
if len(depsWithStubs) > 0 {
implDynamicDeps := bazel.SubtractBazelLabelList(sharedDeps.implementation, bazel.MakeLabelList(depsWithStubs))
la.implementationDynamicDeps.SetSelectValue(axis, config, implDynamicDeps)
stubLibLabels := []bazel.Label{}
for _, l := range depsWithStubs {
l.Label = l.Label + stubsSuffix
stubLibLabels = append(stubLibLabels, l)
}
inApexSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex)
nonApexSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex)
defaultSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey)
if axis == bazel.NoConfigAxis {
(&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
(&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
(&defaultSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.FirstUniqueBazelLabelList(inApexSelectValue))
la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, bazel.FirstUniqueBazelLabelList(nonApexSelectValue))
la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.FirstUniqueBazelLabelList(defaultSelectValue))
} else if config == bazel.OsAndroid {
(&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
(&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.FirstUniqueBazelLabelList(inApexSelectValue))
la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, bazel.FirstUniqueBazelLabelList(nonApexSelectValue))
}
}
// If a dependency in la.implementationDynamicDeps or la.dynamicDeps has stubs, its
// stub variant should be used when the dependency is linked in a APEX. The
// dependencies in NoConfigAxis and OsConfigurationAxis/OsAndroid are grouped by
// having stubs or not, so Bazel select() statement can be used to choose
// source/stub variants of them.
setStubsForDynamicDeps(ctx, axis, config, sharedDeps.export, &la.dynamicDeps, 0)
setStubsForDynamicDeps(ctx, axis, config, sharedDeps.implementation, &la.implementationDynamicDeps, 1)
}
if !BoolDefault(props.Pack_relocations, packRelocationsDefault) {
@@ -1173,6 +1143,43 @@ func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversion
}
}
func setStubsForDynamicDeps(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis,
config string, dynamicLibs bazel.LabelList, dynamicDeps *bazel.LabelListAttribute, ind int) {
depsWithStubs := []bazel.Label{}
for _, l := range dynamicLibs.Includes {
dep, _ := ctx.ModuleFromName(l.OriginalModuleName)
if m, ok := dep.(*Module); ok && m.HasStubsVariants() {
depsWithStubs = append(depsWithStubs, l)
}
}
if len(depsWithStubs) > 0 {
implDynamicDeps := bazel.SubtractBazelLabelList(dynamicLibs, bazel.MakeLabelList(depsWithStubs))
dynamicDeps.SetSelectValue(axis, config, implDynamicDeps)
stubLibLabels := []bazel.Label{}
for _, l := range depsWithStubs {
l.Label = l.Label + stubsSuffix
stubLibLabels = append(stubLibLabels, l)
}
inApexSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex)
nonApexSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex)
defaultSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey)
if axis == bazel.NoConfigAxis {
(&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
(&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
(&defaultSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.FirstUniqueBazelLabelList(inApexSelectValue))
dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, bazel.FirstUniqueBazelLabelList(nonApexSelectValue))
dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.FirstUniqueBazelLabelList(defaultSelectValue))
} else if config == bazel.OsAndroid {
(&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
(&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.FirstUniqueBazelLabelList(inApexSelectValue))
dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, bazel.FirstUniqueBazelLabelList(nonApexSelectValue))
}
}
}
func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) {
bp2BuildPropParseHelper(ctx, module, &StripProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
if stripProperties, ok := props.(*StripProperties); ok {