diff --git a/bp2build/genrule_conversion_test.go b/bp2build/genrule_conversion_test.go index 160395ba9..349088136 100644 --- a/bp2build/genrule_conversion_test.go +++ b/bp2build/genrule_conversion_test.go @@ -15,12 +15,13 @@ package bp2build import ( + "fmt" + "testing" + "android/soong/android" "android/soong/cc" "android/soong/genrule" "android/soong/java" - "fmt" - "testing" ) func registerGenruleModuleTypes(ctx android.RegistrationContext) { @@ -643,3 +644,50 @@ genrule { }) } } + +func TestCcGenruleArchAndExcludeSrcs(t *testing.T) { + name := "cc_genrule with arch" + bp := ` + cc_genrule { + name: "foo", + srcs: [ + "foo1.in", + "foo2.in", + ], + exclude_srcs: ["foo2.in"], + arch: { + arm: { + srcs: [ + "foo1_arch.in", + "foo2_arch.in", + ], + exclude_srcs: ["foo2_arch.in"], + }, + }, + cmd: "cat $(in) > $(out)", + bazel_module: { bp2build_available: true }, + }` + + expectedBazelAttrs := AttrNameToString{ + "srcs": `["foo1.in"] + select({ + "//build/bazel/platforms/arch:arm": ["foo1_arch.in"], + "//conditions:default": [], + })`, + "cmd": `"cat $(SRCS) > $(OUTS)"`, + "target_compatible_with": `["//build/bazel/platforms/os:android"]`, + } + + expectedBazelTargets := []string{ + MakeBazelTargetNoRestrictions("genrule", "foo", expectedBazelAttrs), + } + + t.Run(name, func(t *testing.T) { + RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, + Bp2buildTestCase{ + ModuleTypeUnderTest: "cc_genrule", + ModuleTypeUnderTestFactory: cc.GenRuleFactory, + Blueprint: bp, + ExpectedBazelTargets: expectedBazelTargets, + }) + }) +} diff --git a/cc/genrule_test.go b/cc/genrule_test.go index f25f70416..0d16e6261 100644 --- a/cc/genrule_test.go +++ b/cc/genrule_test.go @@ -40,14 +40,13 @@ func TestArchGenruleCmd(t *testing.T) { name: "gen", tool_files: ["tool"], cmd: "$(location tool) $(in) $(out)", + out: ["out_arm"], arch: { arm: { srcs: ["foo"], - out: ["out_arm"], }, arm64: { srcs: ["bar"], - out: ["out_arm64"], }, }, } @@ -70,7 +69,7 @@ func TestArchGenruleCmd(t *testing.T) { t.Errorf(`want arm inputs %v, got %v`, expected, gen.Implicits.Strings()) } - gen = ctx.ModuleForTests("gen", "android_arm64_armv8-a").Output("out_arm64") + gen = ctx.ModuleForTests("gen", "android_arm64_armv8-a").Output("out_arm") expected = []string{"bar"} if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) { t.Errorf(`want arm64 inputs %v, got %v`, expected, gen.Implicits.Strings()) diff --git a/genrule/genrule.go b/genrule/genrule.go index d4ed36301..14895c9e6 100644 --- a/genrule/genrule.go +++ b/genrule/genrule.go @@ -875,7 +875,7 @@ func GenRuleFactory() android.Module { type genRuleProperties struct { // names of the output files that will be generated - Out []string `android:"arch_variant"` + Out []string } type bazelGenruleAttributes struct { @@ -893,11 +893,27 @@ func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) { tools_prop.Append(tool_files_prop) tools := bazel.MakeLabelListAttribute(tools_prop) - srcs := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Srcs)) + srcs := bazel.LabelListAttribute{} + srcs_labels := bazel.LabelList{} + // Only cc_genrule is arch specific + if ctx.ModuleType() == "cc_genrule" { + for axis, configToProps := range m.GetArchVariantProperties(ctx, &generatorProperties{}) { + for config, props := range configToProps { + if props, ok := props.(*generatorProperties); ok { + labels := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs) + srcs_labels.Append(labels) + srcs.SetSelectValue(axis, config, labels) + } + } + } + } else { + srcs_labels = android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs) + srcs = bazel.MakeLabelListAttribute(srcs_labels) + } var allReplacements bazel.LabelList allReplacements.Append(tools.Value) - allReplacements.Append(srcs.Value) + allReplacements.Append(bazel.FirstUniqueBazelLabelList(srcs_labels)) // Replace in and out variables with $< and $@ var cmd string