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

Commit b70a1a90 authored by Colin Cross's avatar Colin Cross
Browse files

Remove unescaped spans support from RuleBuilder

Now that rsp files use an explicit path instead of $out.rsp the
unescaped spans and NinjaEscapedCommands() support can be removed
from RuleBuilder.

Bug: 182612695
Test: rule_builder_test.go
Change-Id: I6705151ff0528c59aa1af56ee52c294da15a7a66
parent 70c47412
Loading
Loading
Loading
Loading
+3 −40
Original line number Diff line number Diff line
@@ -409,16 +409,6 @@ func (r *RuleBuilder) Commands() []string {
	return commands
}

// NinjaEscapedCommands returns a slice containing the built command line after ninja escaping for each call to
// RuleBuilder.Command.
func (r *RuleBuilder) NinjaEscapedCommands() []string {
	var commands []string
	for _, c := range r.commands {
		commands = append(commands, c.NinjaEscapedString())
	}
	return commands
}

// BuilderContext is a subset of ModuleContext and SingletonContext.
type BuilderContext interface {
	PathContext
@@ -473,7 +463,7 @@ func (r *RuleBuilder) Build(name string, desc string) {
	}

	tools := r.Tools()
	commands := r.NinjaEscapedCommands()
	commands := r.Commands()
	outputs := r.Outputs()
	inputs := r.Inputs()
	rspFileInputs := r.RspFileInputs()
@@ -547,7 +537,7 @@ func (r *RuleBuilder) Build(name string, desc string) {
		}

		// Create a rule to write the manifest as a the textproto.
		WriteFileRule(r.ctx, r.sboxManifestPath, proto.MarshalTextString(&manifest))
		WriteFileRule(r.ctx, r.sboxManifestPath, proptools.NinjaEscape(proto.MarshalTextString(&manifest)))

		// Generate a new string to use as the command line of the sbox rule.  This uses
		// a RuleBuilderCommand as a convenience method of building the command line, then
@@ -601,7 +591,7 @@ func (r *RuleBuilder) Build(name string, desc string) {

	r.ctx.Build(r.pctx, BuildParams{
		Rule: r.ctx.Rule(pctx, name, blueprint.RuleParams{
			Command:        commandString,
			Command:        proptools.NinjaEscape(commandString),
			CommandDeps:    proptools.NinjaEscapeList(tools.Strings()),
			Restat:         r.restat,
			Rspfile:        proptools.NinjaEscape(rspFile),
@@ -637,9 +627,6 @@ type RuleBuilderCommand struct {
	packagedTools  []PackagingSpec
	rspFileInputs  Paths
	rspFile        WritablePath

	// spans [start,end) of the command that should not be ninja escaped
	unescapedSpans [][2]int
}

func (c *RuleBuilderCommand) addInput(path Path) string {
@@ -1069,11 +1056,6 @@ func (c *RuleBuilderCommand) String() string {
	return c.buf.String()
}

// String returns the command line.
func (c *RuleBuilderCommand) NinjaEscapedString() string {
	return ninjaEscapeExceptForSpans(c.String(), c.unescapedSpans)
}

// RuleBuilderSboxProtoForTests takes the BuildParams for the manifest passed to RuleBuilder.Sbox()
// and returns sbox testproto generated by the RuleBuilder.
func RuleBuilderSboxProtoForTests(t *testing.T, params TestingBuildParams) *sbox_proto.Manifest {
@@ -1087,25 +1069,6 @@ func RuleBuilderSboxProtoForTests(t *testing.T, params TestingBuildParams) *sbox
	return &manifest
}

func ninjaEscapeExceptForSpans(s string, spans [][2]int) string {
	if len(spans) == 0 {
		return proptools.NinjaEscape(s)
	}

	sb := strings.Builder{}
	sb.Grow(len(s) * 11 / 10)

	i := 0
	for _, span := range spans {
		sb.WriteString(proptools.NinjaEscape(s[i:span[0]]))
		sb.WriteString(s[span[0]:span[1]])
		i = span[1]
	}
	sb.WriteString(proptools.NinjaEscape(s[i:]))

	return sb.String()
}

func ninjaNameEscape(s string) string {
	b := []byte(s)
	escaped := false
+0 −84
Original line number Diff line number Diff line
@@ -283,16 +283,6 @@ func ExampleRuleBuilderCommand_String() {
	// FOO=foo echo $FOO
}

func ExampleRuleBuilderCommand_NinjaEscapedString() {
	ctx := builderContext()
	fmt.Println(NewRuleBuilder(pctx, ctx).Command().
		Text("FOO=foo").
		Text("echo $FOO").
		NinjaEscapedString())
	// Output:
	// FOO=foo echo $$FOO
}

func TestRuleBuilder(t *testing.T) {
	fs := map[string][]byte{
		"dep_fixer":  nil,
@@ -631,80 +621,6 @@ func TestRuleBuilder_Build(t *testing.T) {
	})
}

func Test_ninjaEscapeExceptForSpans(t *testing.T) {
	type args struct {
		s     string
		spans [][2]int
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		{
			name: "empty",
			args: args{
				s: "",
			},
			want: "",
		},
		{
			name: "unescape none",
			args: args{
				s: "$abc",
			},
			want: "$$abc",
		},
		{
			name: "unescape all",
			args: args{
				s:     "$abc",
				spans: [][2]int{{0, 4}},
			},
			want: "$abc",
		},
		{
			name: "unescape first",
			args: args{
				s:     "$abc$",
				spans: [][2]int{{0, 1}},
			},
			want: "$abc$$",
		},
		{
			name: "unescape last",
			args: args{
				s:     "$abc$",
				spans: [][2]int{{4, 5}},
			},
			want: "$$abc$",
		},
		{
			name: "unescape middle",
			args: args{
				s:     "$a$b$c$",
				spans: [][2]int{{2, 5}},
			},
			want: "$$a$b$c$$",
		},
		{
			name: "unescape multiple",
			args: args{
				s:     "$a$b$c$",
				spans: [][2]int{{2, 3}, {4, 5}},
			},
			want: "$$a$b$c$$",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := ninjaEscapeExceptForSpans(tt.args.s, tt.args.spans); got != tt.want {
				t.Errorf("ninjaEscapeExceptForSpans() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestRuleBuilderHashInputs(t *testing.T) {
	// The basic idea here is to verify that the command (in the case of a
	// non-sbox rule) or the sbox textproto manifest contain a hash of the