bp2build: support genrule $(location) and $(locations)

Soong genrules support $(location) and $(locations) cmd variable
shortcuts without labels. The shortcut is for the location of the first
tool module from the concatenation of the tools and tool_files
properties.

Bazel doesn't support this shortcut, so the bp2build mapping needs to
support it.

Documentation:
https://cs.android.com/android/platform/superproject/+/master:build/soong/genrule/genrule.go;l=95-96;drc=316e07c593ab66599c74725cf482dedbb32b2875
Code: https://cs.android.com/android/platform/superproject/+/master:build/soong/genrule/genrule.go;l=236-246;drc=316e07c593ab66599c74725cf482dedbb32b2875

Test: build_conversion_test.go
Test: TH

Change-Id: I8aa98ae460af3a3fbb0a7835572518680dc7ade1
This commit is contained in:
Jingwen Chen
2021-01-26 03:16:49 -05:00
parent dcc329af9f
commit 885ee7ad86
2 changed files with 94 additions and 11 deletions

View File

@@ -797,12 +797,19 @@ func BazelGenruleFactory() android.Module {
func bp2buildMutator(ctx android.TopDownMutatorContext) {
if m, ok := ctx.Module().(*Module); ok {
name := "__bp2build__" + m.Name()
// Bazel only has the "tools" attribute.
tools := append(m.properties.Tools, m.properties.Tool_files...)
// Replace in and out variables with $< and $@
var cmd string
if m.properties.Cmd != nil {
cmd = strings.Replace(*m.properties.Cmd, "$(in)", "$(SRCS)", -1)
cmd = strings.Replace(cmd, "$(out)", "$(OUTS)", -1)
cmd = strings.Replace(cmd, "$(genDir)", "$(GENDIR)", -1)
if len(tools) > 0 {
cmd = strings.Replace(cmd, "$(location)", fmt.Sprintf("$(location %s)", tools[0]), -1)
cmd = strings.Replace(cmd, "$(locations)", fmt.Sprintf("$(locations %s)", tools[0]), -1)
}
}
// The Out prop is not in an immediately accessible field
@@ -816,9 +823,6 @@ func bp2buildMutator(ctx android.TopDownMutatorContext) {
}
}
// Bazel only has the "tools" attribute.
tools := append(m.properties.Tools, m.properties.Tool_files...)
// Create the BazelTargetModule.
ctx.CreateModule(BazelGenruleFactory, &bazelGenruleAttributes{
Name: proptools.StringPtr(name),