diff --git a/bp2build/prebuilt_etc_conversion_test.go b/bp2build/prebuilt_etc_conversion_test.go index 6738b4bcb..b1e70dc5f 100644 --- a/bp2build/prebuilt_etc_conversion_test.go +++ b/bp2build/prebuilt_etc_conversion_test.go @@ -181,3 +181,87 @@ prebuilt_etc { "dir": `"etc"`, })}}) } + +func TestFilenameAsProperty(t *testing.T) { + runPrebuiltEtcTestCase(t, Bp2buildTestCase{ + Description: "prebuilt_etc - filename is specified as a property ", + Filesystem: map[string]string{}, + Blueprint: ` +prebuilt_etc { + name: "foo", + src: "fooSrc", + filename: "fooFileName", +} +`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{ + "filename": `"fooFileName"`, + "src": `"fooSrc"`, + "dir": `"etc"`, + })}}) +} + +func TestFileNameFromSrc(t *testing.T) { + runPrebuiltEtcTestCase(t, Bp2buildTestCase{ + Description: "prebuilt_etc - filename_from_src is true ", + Filesystem: map[string]string{}, + Blueprint: ` +prebuilt_etc { + name: "foo", + filename_from_src: true, + src: "fooSrc", +} +`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{ + "filename": `"fooSrc"`, + "src": `"fooSrc"`, + "dir": `"etc"`, + })}}) +} + +func TestFileNameFromSrcMultipleSrcs(t *testing.T) { + runPrebuiltEtcTestCase(t, Bp2buildTestCase{ + Description: "prebuilt_etc - filename_from_src is true but there are multiple srcs", + Filesystem: map[string]string{}, + Blueprint: ` +prebuilt_etc { + name: "foo", + filename_from_src: true, + arch: { + arm: { + src: "barSrc", + }, + arm64: { + src: "bazSrc", + }, + } +} +`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{ + "filename_from_src": `True`, + "dir": `"etc"`, + "src": `select({ + "//build/bazel/platforms/arch:arm": "barSrc", + "//build/bazel/platforms/arch:arm64": "bazSrc", + "//conditions:default": None, + })`, + })}}) +} + +func TestFilenameFromModuleName(t *testing.T) { + runPrebuiltEtcTestCase(t, Bp2buildTestCase{ + Description: "prebuilt_etc - neither filename nor filename_from_src are specified ", + Filesystem: map[string]string{}, + Blueprint: ` +prebuilt_etc { + name: "foo", +} +`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{ + "filename": `"foo"`, + "dir": `"etc"`, + })}}) +} diff --git a/etc/prebuilt_etc.go b/etc/prebuilt_etc.go index 719771f4c..b2361ce84 100644 --- a/etc/prebuilt_etc.go +++ b/etc/prebuilt_etc.go @@ -670,10 +670,11 @@ func generatePrebuiltSnapshot(s snapshot.SnapshotSingleton, ctx android.Singleto // For Bazel / bp2build type bazelPrebuiltFileAttributes struct { - Src bazel.LabelAttribute - Filename string - Dir string - Installable bazel.BoolAttribute + Src bazel.LabelAttribute + Filename bazel.LabelAttribute + Dir string + Installable bazel.BoolAttribute + Filename_from_src bazel.BoolAttribute } // ConvertWithBp2build performs bp2build conversion of PrebuiltEtc @@ -694,8 +695,18 @@ func (module *PrebuiltEtc) ConvertWithBp2build(ctx android.TopDownMutatorContext } var filename string - if module.properties.Filename != nil { - filename = *module.properties.Filename + var filenameFromSrc bool + moduleProps := module.properties + + if moduleProps.Filename != nil && *moduleProps.Filename != "" { + filename = *moduleProps.Filename + } else if moduleProps.Filename_from_src != nil && *moduleProps.Filename_from_src { + if moduleProps.Src != nil { + filename = *moduleProps.Src + } + filenameFromSrc = true + } else { + filename = ctx.ModuleName() } var dir = module.installDirBase @@ -714,11 +725,16 @@ func (module *PrebuiltEtc) ConvertWithBp2build(ctx android.TopDownMutatorContext attrs := &bazelPrebuiltFileAttributes{ Src: src, - Filename: filename, Dir: dir, Installable: installable, } + if filename != "" { + attrs.Filename = bazel.LabelAttribute{Value: &bazel.Label{Label: filename}} + } else if filenameFromSrc { + attrs.Filename_from_src = bazel.BoolAttribute{Value: moduleProps.Filename_from_src} + } + props := bazel.BazelTargetModuleProperties{ Rule_class: "prebuilt_file", Bzl_load_location: "//build/bazel/rules:prebuilt_file.bzl",