Fix replacements of namespace module srcs in genrule

expandSrcsForBazel always prefixed : in OriginalModuleName. The
exceptions to this are filegroups that appear in a different soong
namespace. For these cases, we were not correctly substituting the soong
module name with the equivalent bazel label.

Test: go test ./bp2build
Change-Id: If090f3f8819835177c1f4d191b3eef6bb6e30ace
This commit is contained in:
Spandan Das
2023-08-17 22:35:04 +00:00
parent f768e6e27e
commit f62e80a127
2 changed files with 46 additions and 1 deletions

View File

@@ -378,7 +378,13 @@ func expandSrcsForBazel(ctx BazelConversionPathContext, paths, expandedExcludes
if m, tag := SrcIsModuleWithTag(p); m != "" {
l := getOtherModuleLabel(ctx, m, tag, BazelModuleLabel)
if l != nil && !InList(l.Label, expandedExcludes) {
l.OriginalModuleName = fmt.Sprintf(":%s", m)
if strings.HasPrefix(m, "//") {
// this is a module in a soong namespace
// It appears as //<namespace>:<module_name> in srcs, and not ://<namespace>:<module_name>
l.OriginalModuleName = m
} else {
l.OriginalModuleName = fmt.Sprintf(":%s", m)
}
labels.Includes = append(labels.Includes, *l)
}
} else {

View File

@@ -27,6 +27,8 @@ import (
func registerGenruleModuleTypes(ctx android.RegistrationContext) {
ctx.RegisterModuleType("genrule_defaults", func() android.Module { return genrule.DefaultsFactory() })
ctx.RegisterModuleType("cc_binary", func() android.Module { return cc.BinaryFactory() })
ctx.RegisterModuleType("soong_namespace", func() android.Module { return android.NamespaceFactory() })
}
func runGenruleTestCase(t *testing.T, tc Bp2buildTestCase) {
@@ -913,3 +915,40 @@ func TestGenruleWithProductVariableConfiguredCmd(t *testing.T) {
})
}
}
func TestGenruleWithModulesInNamespaces(t *testing.T) {
bp := `
genrule {
name: "mygenrule",
cmd: "echo $(location //mynamespace:mymodule) > $(out)",
srcs: ["//mynamespace:mymodule"],
out: ["myout"],
}
`
fs := map[string]string{
"mynamespace/Android.bp": `soong_namespace {}`,
"mynamespace/dir/Android.bp": `cc_binary {name: "mymodule"}`,
}
expectedBazelTargets := []string{
MakeBazelTargetNoRestrictions("genrule", "mygenrule", AttrNameToString{
// The fully qualified soong label is <namespace>:<module_name>
// - here the prefix is mynamespace
// The fully qualifed bazel label is <package>:<module_name>
// - here the prefix is mynamespace/dir, since there is a BUILD file at each level of this FS path
"cmd": `"echo $(location //mynamespace/dir:mymodule) > $(OUTS)"`,
"outs": `["myout"]`,
"srcs": `["//mynamespace/dir:mymodule"]`,
}),
}
t.Run("genrule that uses module from a different namespace", func(t *testing.T) {
runGenruleTestCase(t, Bp2buildTestCase{
Blueprint: bp,
Filesystem: fs,
ModuleTypeUnderTest: "genrule",
ModuleTypeUnderTestFactory: genrule.GenRuleFactory,
ExpectedBazelTargets: expectedBazelTargets,
})
})
}