diff --git a/android/filegroup.go b/android/filegroup.go index 5a8c4b9bc..b6e37a50d 100644 --- a/android/filegroup.go +++ b/android/filegroup.go @@ -22,7 +22,6 @@ import ( "android/soong/bazel" "android/soong/bazel/cquery" "android/soong/ui/metrics/bp2build_metrics_proto" - "github.com/google/blueprint" "github.com/google/blueprint/proptools" ) @@ -106,8 +105,10 @@ func (fg *fileGroup) ConvertWithBp2build(ctx Bp2buildMutatorContext) { if f.Label == fg.Name() { if len(srcs.Value.Includes) > 1 { ctx.ModuleErrorf("filegroup '%s' cannot contain a file with the same name", fg.Name()) + ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_SRC_NAME_COLLISION, "") + } else { + panic("This situation should have been handled by FileGroupFactory's call to InitBazelModuleAsHandcrafted") } - ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_SRC_NAME_COLLISION, "") return } } @@ -253,6 +254,16 @@ func FileGroupFactory() Module { module.AddProperties(&module.properties) InitAndroidModule(module) InitBazelModule(module) + AddBazelHandcraftedHook(module, func(ctx LoadHookContext) string { + // If there is a single src with the same name as the filegroup module name, + // then don't generate this filegroup. It will be OK for other targets + // to depend on this source file by name directly. + fg := ctx.Module().(*fileGroup) + if len(fg.properties.Srcs) == 1 && fg.Name() == fg.properties.Srcs[0] { + return fg.Name() + } + return "" + }) InitDefaultableModule(module) return module } diff --git a/bp2build/filegroup_conversion_test.go b/bp2build/filegroup_conversion_test.go index cb2e2076a..9c49dac6f 100644 --- a/bp2build/filegroup_conversion_test.go +++ b/bp2build/filegroup_conversion_test.go @@ -40,7 +40,9 @@ filegroup { srcs: ["foo"], } `, - ExpectedBazelTargets: []string{}}) + ExpectedBazelTargets: []string{}, + ExpectedHandcraftedModules: []string{"foo"}}, + ) } func TestFilegroupSameNameAsFile_MultipleFiles(t *testing.T) {