Handle .proto files in different package for filegroups

Followup to aosp/2693190, this CL adds the support to filegroups.
<mod>__bp2build_converted is now an alias to a proto_library
target <mod>_proto. This proto_library will be created in a different
package if the .proto file exists in a different package.

Test: bp2build unit tests
Test: TH
Bug: 292583584
Change-Id: I8ca452aacf1a86dfc9e218464e38aab89afa5a29
This commit is contained in:
Spandan Das 2023-08-04 02:19:53 +00:00
parent c53767e434
commit df3ec82b62
3 changed files with 81 additions and 5 deletions

View file

@ -24,6 +24,7 @@ import (
"android/soong/ui/metrics/bp2build_metrics_proto"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
)
func init() {
@ -141,8 +142,14 @@ func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) {
attrs)
} else {
if fg.ShouldConvertToProtoLibrary(ctx) {
pkgToSrcs := partitionSrcsByPackage(ctx.ModuleDir(), bazel.MakeLabelList(srcs.Value.Includes))
if len(pkgToSrcs) > 1 {
ctx.ModuleErrorf("TODO: Add bp2build support for multiple package .protosrcs in filegroup")
return
}
pkg := SortedKeys(pkgToSrcs)[0]
attrs := &ProtoAttrs{
Srcs: srcs,
Srcs: bazel.MakeLabelListAttribute(pkgToSrcs[pkg]),
Strip_import_prefix: fg.properties.Path,
}
@ -151,13 +158,39 @@ func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) {
// TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug.
"manual",
}
if pkg != ctx.ModuleDir() {
// Since we are creating the proto_library in a subpackage, create an import_prefix relative to the current package
if rel, err := filepath.Rel(ctx.ModuleDir(), pkg); err != nil {
ctx.ModuleErrorf("Could not get relative path for %v %v", pkg, err)
} else if rel != "." {
attrs.Import_prefix = &rel
// Strip the package prefix
attrs.Strip_import_prefix = proptools.StringPtr("")
}
}
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{Rule_class: "proto_library"},
CommonAttributes{
Name: fg.Name() + convertedProtoLibrarySuffix,
Name: fg.Name() + "_proto",
Dir: proptools.StringPtr(pkg),
Tags: bazel.MakeStringListAttribute(tags),
},
attrs)
// Create an alias in the current dir. The actual target might exist in a different package, but rdeps
// can reliabily use this alias
ctx.CreateBazelTargetModule(
bazel.BazelTargetModuleProperties{Rule_class: "alias"},
CommonAttributes{
Name: fg.Name() + convertedProtoLibrarySuffix,
// TODO(b/246997908): we can remove this tag if we could figure out a solution for this bug.
Tags: bazel.MakeStringListAttribute(tags),
},
&bazelAliasAttributes{
Actual: bazel.MakeLabelAttribute("//" + pkg + ":" + fg.Name() + "_proto"),
},
)
}
// TODO(b/242847534): Still convert to a filegroup because other unconverted

View file

@ -2434,11 +2434,17 @@ cc_library {
}), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
"dynamic_deps": `[":libprotobuf-cpp-lite"]`,
"whole_archive_deps": `[":a_cc_proto_lite"]`,
}), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
}), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
"srcs": `["a_fg.proto"]`,
"tags": `[
"apex_available=//apex_available:anyapex",
"manual",
]`,
}), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
"actual": `"//.:a_fg_proto_proto"`,
"tags": `[
"apex_available=//apex_available:anyapex",
"manual",
]`,
}), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
"srcs": `["a_fg.proto"]`,
@ -2476,11 +2482,17 @@ cc_library {
}), MakeBazelTarget("cc_library_shared", "a", AttrNameToString{
"dynamic_deps": `[":libprotobuf-cpp-lite"]`,
"whole_archive_deps": `[":a_cc_proto_lite"]`,
}), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_bp2build_converted", AttrNameToString{
}), MakeBazelTargetNoRestrictions("proto_library", "a_fg_proto_proto", AttrNameToString{
"srcs": `["a_fg.proto"]`,
"tags": `[
"apex_available=//apex_available:anyapex",
"manual",
]`,
}), MakeBazelTargetNoRestrictions("alias", "a_fg_proto_bp2build_converted", AttrNameToString{
"actual": `"//.:a_fg_proto_proto"`,
"tags": `[
"apex_available=//apex_available:anyapex",
"manual",
]`,
}), MakeBazelTargetNoRestrictions("filegroup", "a_fg_proto", AttrNameToString{
"srcs": `["a_fg.proto"]`,

View file

@ -137,12 +137,19 @@ filegroup {
path: "proto",
}`,
ExpectedBazelTargets: []string{
MakeBazelTargetNoRestrictions("proto_library", "foo_bp2build_converted", AttrNameToString{
MakeBazelTargetNoRestrictions("proto_library", "foo_proto", AttrNameToString{
"srcs": `["proto/foo.proto"]`,
"strip_import_prefix": `"proto"`,
"tags": `[
"apex_available=//apex_available:anyapex",
"manual",
]`,
}),
MakeBazelTargetNoRestrictions("alias", "foo_bp2build_converted", AttrNameToString{
"actual": `"//.:foo_proto"`,
"tags": `[
"apex_available=//apex_available:anyapex",
"manual",
]`,
}),
MakeBazelTargetNoRestrictions("filegroup", "foo", AttrNameToString{
@ -170,3 +177,27 @@ filegroup {
]`}),
}})
}
func TestFilegroupWithProtoInDifferentPackage(t *testing.T) {
runFilegroupTestCase(t, Bp2buildTestCase{
Description: "filegroup with .proto in different package",
Filesystem: map[string]string{
"subdir/Android.bp": "",
},
Blueprint: `
filegroup {
name: "foo",
srcs: ["subdir/foo.proto"],
}`,
Dir: "subdir", // check in subdir
ExpectedBazelTargets: []string{
MakeBazelTargetNoRestrictions("proto_library", "foo_proto", AttrNameToString{
"srcs": `["//subdir:foo.proto"]`,
"import_prefix": `"subdir"`,
"strip_import_prefix": `""`,
"tags": `[
"apex_available=//apex_available:anyapex",
"manual",
]`}),
}})
}