Merge "bp2build: support genrule $(location) and $(locations)"
This commit is contained in:
commit
801a669566
|
@ -274,8 +274,10 @@ func TestModuleTypeBp2Build(t *testing.T) {
|
||||||
moduleTypeUnderTestFactory android.ModuleFactory
|
moduleTypeUnderTestFactory android.ModuleFactory
|
||||||
bp string
|
bp string
|
||||||
expectedBazelTarget string
|
expectedBazelTarget string
|
||||||
|
description string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
description: "filegroup with no srcs",
|
||||||
moduleTypeUnderTest: "filegroup",
|
moduleTypeUnderTest: "filegroup",
|
||||||
moduleTypeUnderTestFactory: android.FileGroupFactory,
|
moduleTypeUnderTestFactory: android.FileGroupFactory,
|
||||||
bp: `filegroup {
|
bp: `filegroup {
|
||||||
|
@ -289,6 +291,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
|
||||||
)`,
|
)`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: "filegroup with srcs",
|
||||||
moduleTypeUnderTest: "filegroup",
|
moduleTypeUnderTest: "filegroup",
|
||||||
moduleTypeUnderTestFactory: android.FileGroupFactory,
|
moduleTypeUnderTestFactory: android.FileGroupFactory,
|
||||||
bp: `filegroup {
|
bp: `filegroup {
|
||||||
|
@ -304,18 +307,19 @@ func TestModuleTypeBp2Build(t *testing.T) {
|
||||||
)`,
|
)`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: "genrule with command line variable replacements",
|
||||||
moduleTypeUnderTest: "genrule",
|
moduleTypeUnderTest: "genrule",
|
||||||
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
|
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
|
||||||
bp: `genrule {
|
bp: `genrule {
|
||||||
name: "foo",
|
name: "foo",
|
||||||
out: ["foo.out"],
|
out: ["foo.out"],
|
||||||
srcs: ["foo.in"],
|
srcs: ["foo.in"],
|
||||||
tool_files: [":foo.tool"],
|
tools: [":foo.tool"],
|
||||||
cmd: "$(location :foo.tool) arg $(in) $(out)",
|
cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
|
||||||
}`,
|
}`,
|
||||||
expectedBazelTarget: `genrule(
|
expectedBazelTarget: `genrule(
|
||||||
name = "foo",
|
name = "foo",
|
||||||
cmd = "$(location :foo.tool) arg $(SRCS) $(OUTS)",
|
cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
|
||||||
outs = [
|
outs = [
|
||||||
"foo.out",
|
"foo.out",
|
||||||
],
|
],
|
||||||
|
@ -328,18 +332,44 @@ func TestModuleTypeBp2Build(t *testing.T) {
|
||||||
)`,
|
)`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
description: "genrule using $(locations :label)",
|
||||||
moduleTypeUnderTest: "genrule",
|
moduleTypeUnderTest: "genrule",
|
||||||
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
|
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
|
||||||
bp: `genrule {
|
bp: `genrule {
|
||||||
name: "foo",
|
name: "foo",
|
||||||
out: ["foo.out"],
|
out: ["foo.out"],
|
||||||
srcs: ["foo.in"],
|
srcs: ["foo.in"],
|
||||||
tools: [":foo.tool"],
|
tools: [":foo.tools"],
|
||||||
cmd: "$(location :foo.tool) --out-dir=$(genDir) $(in)",
|
cmd: "$(locations :foo.tools) -s $(out) $(in)",
|
||||||
}`,
|
}`,
|
||||||
expectedBazelTarget: `genrule(
|
expectedBazelTarget: `genrule(
|
||||||
name = "foo",
|
name = "foo",
|
||||||
cmd = "$(location :foo.tool) --out-dir=$(GENDIR) $(SRCS)",
|
cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
|
||||||
|
outs = [
|
||||||
|
"foo.out",
|
||||||
|
],
|
||||||
|
srcs = [
|
||||||
|
"foo.in",
|
||||||
|
],
|
||||||
|
tools = [
|
||||||
|
":foo.tools",
|
||||||
|
],
|
||||||
|
)`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "genrule using $(location) label should substitute first tool label automatically",
|
||||||
|
moduleTypeUnderTest: "genrule",
|
||||||
|
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
|
||||||
|
bp: `genrule {
|
||||||
|
name: "foo",
|
||||||
|
out: ["foo.out"],
|
||||||
|
srcs: ["foo.in"],
|
||||||
|
tool_files: [":foo.tool", ":other.tool"],
|
||||||
|
cmd: "$(location) -s $(out) $(in)",
|
||||||
|
}`,
|
||||||
|
expectedBazelTarget: `genrule(
|
||||||
|
name = "foo",
|
||||||
|
cmd = "$(location :foo.tool) -s $(OUTS) $(SRCS)",
|
||||||
outs = [
|
outs = [
|
||||||
"foo.out",
|
"foo.out",
|
||||||
],
|
],
|
||||||
|
@ -348,6 +378,54 @@ func TestModuleTypeBp2Build(t *testing.T) {
|
||||||
],
|
],
|
||||||
tools = [
|
tools = [
|
||||||
":foo.tool",
|
":foo.tool",
|
||||||
|
":other.tool",
|
||||||
|
],
|
||||||
|
)`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "genrule using $(locations) label should substitute first tool label automatically",
|
||||||
|
moduleTypeUnderTest: "genrule",
|
||||||
|
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
|
||||||
|
bp: `genrule {
|
||||||
|
name: "foo",
|
||||||
|
out: ["foo.out"],
|
||||||
|
srcs: ["foo.in"],
|
||||||
|
tools: [":foo.tool", ":other.tool"],
|
||||||
|
cmd: "$(locations) -s $(out) $(in)",
|
||||||
|
}`,
|
||||||
|
expectedBazelTarget: `genrule(
|
||||||
|
name = "foo",
|
||||||
|
cmd = "$(locations :foo.tool) -s $(OUTS) $(SRCS)",
|
||||||
|
outs = [
|
||||||
|
"foo.out",
|
||||||
|
],
|
||||||
|
srcs = [
|
||||||
|
"foo.in",
|
||||||
|
],
|
||||||
|
tools = [
|
||||||
|
":foo.tool",
|
||||||
|
":other.tool",
|
||||||
|
],
|
||||||
|
)`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "genrule without tools or tool_files can convert successfully",
|
||||||
|
moduleTypeUnderTest: "genrule",
|
||||||
|
moduleTypeUnderTestFactory: genrule.GenRuleFactory,
|
||||||
|
bp: `genrule {
|
||||||
|
name: "foo",
|
||||||
|
out: ["foo.out"],
|
||||||
|
srcs: ["foo.in"],
|
||||||
|
cmd: "cp $(in) $(out)",
|
||||||
|
}`,
|
||||||
|
expectedBazelTarget: `genrule(
|
||||||
|
name = "foo",
|
||||||
|
cmd = "cp $(SRCS) $(OUTS)",
|
||||||
|
outs = [
|
||||||
|
"foo.out",
|
||||||
|
],
|
||||||
|
srcs = [
|
||||||
|
"foo.in",
|
||||||
],
|
],
|
||||||
)`,
|
)`,
|
||||||
},
|
},
|
||||||
|
@ -367,13 +445,14 @@ func TestModuleTypeBp2Build(t *testing.T) {
|
||||||
|
|
||||||
bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir]
|
bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir]
|
||||||
if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
|
if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
|
||||||
t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
|
t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
actualBazelTarget := bazelTargets[0]
|
actualBazelTarget := bazelTargets[0]
|
||||||
if actualBazelTarget.content != testCase.expectedBazelTarget {
|
if actualBazelTarget.content != testCase.expectedBazelTarget {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
"Expected generated Bazel target to be '%s', got '%s'",
|
"%s: Expected generated Bazel target to be '%s', got '%s'",
|
||||||
|
testCase.description,
|
||||||
testCase.expectedBazelTarget,
|
testCase.expectedBazelTarget,
|
||||||
actualBazelTarget.content,
|
actualBazelTarget.content,
|
||||||
)
|
)
|
||||||
|
|
|
@ -797,12 +797,19 @@ func BazelGenruleFactory() android.Module {
|
||||||
func bp2buildMutator(ctx android.TopDownMutatorContext) {
|
func bp2buildMutator(ctx android.TopDownMutatorContext) {
|
||||||
if m, ok := ctx.Module().(*Module); ok {
|
if m, ok := ctx.Module().(*Module); ok {
|
||||||
name := "__bp2build__" + m.Name()
|
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 $@
|
// Replace in and out variables with $< and $@
|
||||||
var cmd string
|
var cmd string
|
||||||
if m.properties.Cmd != nil {
|
if m.properties.Cmd != nil {
|
||||||
cmd = strings.Replace(*m.properties.Cmd, "$(in)", "$(SRCS)", -1)
|
cmd = strings.Replace(*m.properties.Cmd, "$(in)", "$(SRCS)", -1)
|
||||||
cmd = strings.Replace(cmd, "$(out)", "$(OUTS)", -1)
|
cmd = strings.Replace(cmd, "$(out)", "$(OUTS)", -1)
|
||||||
cmd = strings.Replace(cmd, "$(genDir)", "$(GENDIR)", -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
|
// 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.
|
// Create the BazelTargetModule.
|
||||||
ctx.CreateModule(BazelGenruleFactory, &bazelGenruleAttributes{
|
ctx.CreateModule(BazelGenruleFactory, &bazelGenruleAttributes{
|
||||||
Name: proptools.StringPtr(name),
|
Name: proptools.StringPtr(name),
|
||||||
|
|
Loading…
Reference in New Issue