bp2build: add bazel_module: { bp2build_available } prop.

This CL adds a per-target allowlist to instruct bp2build on which modules it should generate Bazel targets for.

Test: soong tests
Change-Id: I869e66fce405c2c6689b381569b8cc0118cbcf76
This commit is contained in:
Jingwen Chen
2021-02-05 03:03:24 -05:00
parent 1fd14691dd
commit 77e8b7b6d2
7 changed files with 123 additions and 3 deletions

View File

@@ -49,9 +49,10 @@ func (bfg *bazelFilegroup) GenerateAndroidBuildActions(ctx ModuleContext) {}
func FilegroupBp2Build(ctx TopDownMutatorContext) { func FilegroupBp2Build(ctx TopDownMutatorContext) {
fg, ok := ctx.Module().(*fileGroup) fg, ok := ctx.Module().(*fileGroup)
if !ok { if !ok || !fg.properties.Bazel_module.Bp2build_available {
return return
} }
attrs := &bazelFilegroupAttributes{ attrs := &bazelFilegroupAttributes{
Srcs: BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs), Srcs: BazelLabelForModuleSrcExcludes(ctx, fg.properties.Srcs, fg.properties.Exclude_srcs),
} }

View File

@@ -17,6 +17,9 @@ package bazel
type bazelModuleProperties struct { type bazelModuleProperties struct {
// The label of the Bazel target replacing this Soong module. // The label of the Bazel target replacing this Soong module.
Label string Label string
// If true, bp2build will generate the converted Bazel target for this module.
Bp2build_available bool
} }
// Properties contains common module properties for Bazel migration purposes. // Properties contains common module properties for Bazel migration purposes.

View File

@@ -213,6 +213,8 @@ func generateBazelTarget(ctx bpToBuildContext, m blueprint.Module) BazelTarget {
// Delete it from being generated in the BUILD file. // Delete it from being generated in the BUILD file.
delete(props.Attrs, "bzl_load_location") delete(props.Attrs, "bzl_load_location")
delete(props.Attrs, "bp2build_available")
// Return the Bazel target with rule class and attributes, ready to be // Return the Bazel target with rule class and attributes, ready to be
// code-generated. // code-generated.
attributes := propsToAttributes(props.Attrs) attributes := propsToAttributes(props.Attrs)

View File

@@ -228,6 +228,7 @@ func TestGenerateBazelTargetModules(t *testing.T) {
name: "foo", name: "foo",
string_list_prop: ["a", "b"], string_list_prop: ["a", "b"],
string_prop: "a", string_prop: "a",
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTarget: `custom( expectedBazelTarget: `custom(
name = "foo", name = "foo",
@@ -382,6 +383,7 @@ func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
{ {
bp: `custom { bp: `custom {
name: "bar", name: "bar",
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTarget: `my_library( expectedBazelTarget: `my_library(
name = "bar", name = "bar",
@@ -474,6 +476,7 @@ genrule {
bp: `filegroup { bp: `filegroup {
name: "fg_foo", name: "fg_foo",
srcs: [], srcs: [],
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`filegroup( `filegroup(
@@ -491,6 +494,7 @@ genrule {
bp: `filegroup { bp: `filegroup {
name: "fg_foo", name: "fg_foo",
srcs: ["a", "b"], srcs: ["a", "b"],
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`filegroup( expectedBazelTargets: []string{`filegroup(
name = "fg_foo", name = "fg_foo",
@@ -510,6 +514,7 @@ genrule {
name: "fg_foo", name: "fg_foo",
srcs: ["a", "b"], srcs: ["a", "b"],
exclude_srcs: ["a"], exclude_srcs: ["a"],
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`filegroup( expectedBazelTargets: []string{`filegroup(
name = "fg_foo", name = "fg_foo",
@@ -527,6 +532,7 @@ genrule {
bp: `filegroup { bp: `filegroup {
name: "foo", name: "foo",
srcs: ["**/*.txt"], srcs: ["**/*.txt"],
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`filegroup( expectedBazelTargets: []string{`filegroup(
name = "foo", name = "foo",
@@ -552,6 +558,7 @@ genrule {
bp: `filegroup { bp: `filegroup {
name: "foo", name: "foo",
srcs: ["a.txt"], srcs: ["a.txt"],
bazel_module: { bp2build_available: true },
}`, }`,
dir: "other", dir: "other",
expectedBazelTargets: []string{`filegroup( expectedBazelTargets: []string{`filegroup(
@@ -567,6 +574,7 @@ genrule {
"other/Android.bp": `filegroup { "other/Android.bp": `filegroup {
name: "fg_foo", name: "fg_foo",
srcs: ["**/*.txt"], srcs: ["**/*.txt"],
bazel_module: { bp2build_available: true },
}`, }`,
"other/a.txt": "", "other/a.txt": "",
"other/b.txt": "", "other/b.txt": "",
@@ -585,6 +593,7 @@ genrule {
":foo", ":foo",
"c", "c",
], ],
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`filegroup( expectedBazelTargets: []string{`filegroup(
name = "foobar", name = "foobar",
@@ -612,6 +621,7 @@ genrule {
out: ["foo_tool.out"], out: ["foo_tool.out"],
srcs: ["foo_tool.in"], srcs: ["foo_tool.in"],
cmd: "cp $(in) $(out)", cmd: "cp $(in) $(out)",
bazel_module: { bp2build_available: true },
} }
genrule { genrule {
@@ -620,6 +630,7 @@ genrule {
srcs: ["foo.in"], srcs: ["foo.in"],
tools: [":foo.tool"], tools: [":foo.tool"],
cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)", cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`genrule( `genrule(
@@ -658,7 +669,8 @@ genrule {
out: ["foo_tool.out", "foo_tool2.out"], out: ["foo_tool.out", "foo_tool2.out"],
srcs: ["foo_tool.in"], srcs: ["foo_tool.in"],
cmd: "cp $(in) $(out)", cmd: "cp $(in) $(out)",
} bazel_module: { bp2build_available: true },
}
genrule { genrule {
name: "foo", name: "foo",
@@ -666,6 +678,7 @@ genrule {
srcs: ["foo.in"], srcs: ["foo.in"],
tools: [":foo.tools"], tools: [":foo.tools"],
cmd: "$(locations :foo.tools) -s $(out) $(in)", cmd: "$(locations :foo.tools) -s $(out) $(in)",
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{`genrule(
name = "foo", name = "foo",
@@ -705,6 +718,7 @@ genrule {
srcs: ["foo.in"], srcs: ["foo.in"],
tool_files: [":foo.tool"], tool_files: [":foo.tool"],
cmd: "$(locations :foo.tool) -s $(out) $(in)", cmd: "$(locations :foo.tool) -s $(out) $(in)",
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{`genrule(
name = "foo", name = "foo",
@@ -734,6 +748,7 @@ genrule {
srcs: [":other.tool"], srcs: [":other.tool"],
tool_files: [":foo.tool"], tool_files: [":foo.tool"],
cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)", cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{`genrule(
name = "foo", name = "foo",
@@ -763,6 +778,7 @@ genrule {
srcs: ["foo.in"], srcs: ["foo.in"],
tool_files: [":foo.tool", ":other.tool"], tool_files: [":foo.tool", ":other.tool"],
cmd: "$(location) -s $(out) $(in)", cmd: "$(location) -s $(out) $(in)",
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{`genrule(
name = "foo", name = "foo",
@@ -793,6 +809,7 @@ genrule {
srcs: ["foo.in"], srcs: ["foo.in"],
tools: [":foo.tool", ":other.tool"], tools: [":foo.tool", ":other.tool"],
cmd: "$(locations) -s $(out) $(in)", cmd: "$(locations) -s $(out) $(in)",
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{`genrule(
name = "foo", name = "foo",
@@ -822,6 +839,7 @@ genrule {
out: ["foo.out"], out: ["foo.out"],
srcs: ["foo.in"], srcs: ["foo.in"],
cmd: "cp $(in) $(out)", cmd: "cp $(in) $(out)",
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`genrule( expectedBazelTargets: []string{`genrule(
name = "foo", name = "foo",
@@ -927,6 +945,7 @@ genrule {
out: ["out"], out: ["out"],
srcs: ["in1"], srcs: ["in1"],
defaults: ["gen_defaults"], defaults: ["gen_defaults"],
bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTarget: `genrule( expectedBazelTarget: `genrule(
@@ -961,6 +980,7 @@ genrule {
srcs: ["in1"], srcs: ["in1"],
defaults: ["gen_defaults"], defaults: ["gen_defaults"],
cmd: "do-something $(in) $(out)", cmd: "do-something $(in) $(out)",
bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTarget: `genrule( expectedBazelTarget: `genrule(
@@ -999,6 +1019,7 @@ genrule {
name: "gen", name: "gen",
out: ["out"], out: ["out"],
defaults: ["gen_defaults1", "gen_defaults2"], defaults: ["gen_defaults1", "gen_defaults2"],
bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTarget: `genrule( expectedBazelTarget: `genrule(
@@ -1045,6 +1066,7 @@ genrule {
name: "gen", name: "gen",
out: ["out"], out: ["out"],
defaults: ["gen_defaults1"], defaults: ["gen_defaults1"],
bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTarget: `genrule( expectedBazelTarget: `genrule(
@@ -1097,3 +1119,80 @@ genrule {
} }
} }
} }
func TestAllowlistingBp2buildTargets(t *testing.T) {
testCases := []struct {
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator bp2buildMutator
bp string
expectedCount int
description string
}{
{
description: "explicitly unavailable",
moduleTypeUnderTest: "filegroup",
moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
bp: `filegroup {
name: "foo",
srcs: ["a", "b"],
bazel_module: { bp2build_available: false },
}`,
expectedCount: 0,
},
{
description: "implicitly unavailable",
moduleTypeUnderTest: "filegroup",
moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
bp: `filegroup {
name: "foo",
srcs: ["a", "b"],
}`,
expectedCount: 0,
},
{
description: "explicitly available",
moduleTypeUnderTest: "filegroup",
moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
bp: `filegroup {
name: "foo",
srcs: ["a", "b"],
bazel_module: { bp2build_available: true },
}`,
expectedCount: 1,
},
{
description: "generates more than 1 target if needed",
moduleTypeUnderTest: "custom",
moduleTypeUnderTestFactory: customModuleFactory,
moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
bp: `custom {
name: "foo",
bazel_module: { bp2build_available: true },
}`,
expectedCount: 3,
},
}
dir := "."
for _, testCase := range testCases {
config := android.TestConfig(buildDir, nil, testCase.bp, nil)
ctx := android.NewTestContext(config)
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
android.FailIfErrored(t, errs)
_, errs = ctx.ResolveDependencies(config)
android.FailIfErrored(t, errs)
bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[dir]
if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
}
}
}

View File

@@ -86,6 +86,10 @@ custom = rule(
"soong_module_name": attr.string(mandatory = True), "soong_module_name": attr.string(mandatory = True),
"soong_module_variant": attr.string(), "soong_module_variant": attr.string(),
"soong_module_deps": attr.label_list(providers = [SoongModuleInfo]), "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
# bazel_module start
# "label": attr.string(),
# "bp2build_available": attr.bool(),
# bazel_module end
"bool_prop": attr.bool(), "bool_prop": attr.bool(),
"bool_ptr_prop": attr.bool(), "bool_ptr_prop": attr.bool(),
"int64_ptr_prop": attr.int(), "int64_ptr_prop": attr.int(),

View File

@@ -27,6 +27,8 @@ type customModule struct {
android.ModuleBase android.ModuleBase
props customProps props customProps
bazelProps bazel.Properties
} }
// OutputFiles is needed because some instances of this module use dist with a // OutputFiles is needed because some instances of this module use dist with a
@@ -42,6 +44,7 @@ func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
func customModuleFactoryBase() android.Module { func customModuleFactoryBase() android.Module {
module := &customModule{} module := &customModule{}
module.AddProperties(&module.props) module.AddProperties(&module.props)
module.AddProperties(&module.bazelProps)
return module return module
} }
@@ -124,6 +127,10 @@ func (m *customBazelModule) GenerateAndroidBuildActions(ctx android.ModuleContex
func customBp2BuildMutator(ctx android.TopDownMutatorContext) { func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
if m, ok := ctx.Module().(*customModule); ok { if m, ok := ctx.Module().(*customModule); ok {
if !m.bazelProps.Bazel_module.Bp2build_available {
return
}
attrs := &customBazelModuleAttributes{ attrs := &customBazelModuleAttributes{
String_prop: m.props.String_prop, String_prop: m.props.String_prop,
String_list_prop: m.props.String_list_prop, String_list_prop: m.props.String_list_prop,
@@ -143,6 +150,10 @@ func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
// module to target. // module to target.
func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) { func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
if m, ok := ctx.Module().(*customModule); ok { if m, ok := ctx.Module().(*customModule); ok {
if !m.bazelProps.Bazel_module.Bp2build_available {
return
}
baseName := m.Name() baseName := m.Name()
attrs := &customBazelModuleAttributes{} attrs := &customBazelModuleAttributes{}

View File

@@ -800,7 +800,7 @@ func BazelGenruleFactory() android.Module {
func GenruleBp2Build(ctx android.TopDownMutatorContext) { func GenruleBp2Build(ctx android.TopDownMutatorContext) {
m, ok := ctx.Module().(*Module) m, ok := ctx.Module().(*Module)
if !ok { if !ok || !m.properties.Bazel_module.Bp2build_available {
return return
} }