Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 801a6695 authored by Jingwen Chen's avatar Jingwen Chen Committed by Gerrit Code Review
Browse files

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

parents 398ba4fc 885ee7ad
Loading
Loading
Loading
Loading
+87 −8
Original line number Diff line number Diff line
@@ -274,8 +274,10 @@ func TestModuleTypeBp2Build(t *testing.T) {
		moduleTypeUnderTestFactory android.ModuleFactory
		bp                         string
		expectedBazelTarget        string
		description                string
	}{
		{
			description:                "filegroup with no srcs",
			moduleTypeUnderTest:        "filegroup",
			moduleTypeUnderTestFactory: android.FileGroupFactory,
			bp: `filegroup {
@@ -289,6 +291,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
)`,
		},
		{
			description:                "filegroup with srcs",
			moduleTypeUnderTest:        "filegroup",
			moduleTypeUnderTestFactory: android.FileGroupFactory,
			bp: `filegroup {
@@ -304,18 +307,19 @@ func TestModuleTypeBp2Build(t *testing.T) {
)`,
		},
		{
			description:                "genrule with command line variable replacements",
			moduleTypeUnderTest:        "genrule",
			moduleTypeUnderTestFactory: genrule.GenRuleFactory,
			bp: `genrule {
    name: "foo",
    out: ["foo.out"],
    srcs: ["foo.in"],
    tool_files: [":foo.tool"],
    cmd: "$(location :foo.tool) arg $(in) $(out)",
    tools: [":foo.tool"],
    cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
}`,
			expectedBazelTarget: `genrule(
    name = "foo",
    cmd = "$(location :foo.tool) arg $(SRCS) $(OUTS)",
    cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
    outs = [
        "foo.out",
    ],
@@ -328,18 +332,44 @@ func TestModuleTypeBp2Build(t *testing.T) {
)`,
		},
		{
			description:                "genrule using $(locations :label)",
			moduleTypeUnderTest:        "genrule",
			moduleTypeUnderTestFactory: genrule.GenRuleFactory,
			bp: `genrule {
    name: "foo",
    out: ["foo.out"],
    srcs: ["foo.in"],
    tools: [":foo.tool"],
    cmd: "$(location :foo.tool) --out-dir=$(genDir) $(in)",
    tools: [":foo.tools"],
    cmd: "$(locations :foo.tools) -s $(out) $(in)",
}`,
			expectedBazelTarget: `genrule(
    name = "foo",
    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) --out-dir=$(GENDIR) $(SRCS)",
    cmd = "$(location :foo.tool) -s $(OUTS) $(SRCS)",
    outs = [
        "foo.out",
    ],
@@ -348,6 +378,54 @@ func TestModuleTypeBp2Build(t *testing.T) {
    ],
    tools = [
        ":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]
		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]
		if actualBazelTarget.content != testCase.expectedBazelTarget {
			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,
				actualBazelTarget.content,
			)
+7 −3
Original line number Diff line number Diff line
@@ -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),