Support comments in build rules.

Tested: sh tests/test.sh
This commit is contained in:
Doug Evans 2015-11-08 12:21:58 -08:00
parent a17f759660
commit fcc6739e34
3 changed files with 15 additions and 5 deletions

View file

@ -73,6 +73,7 @@ type RuleParams struct {
// Ninja build statement. The Args field contains variable names and values
// that are set within the build statement's scope in the Ninja file.
type BuildParams struct {
Comment string // The comment that will appear above the definition.
Rule Rule // The rule to invoke.
Outputs []string // The list of output targets.
Inputs []string // The list of explicit input dependencies.
@ -236,6 +237,7 @@ func (r *ruleDef) WriteTo(nw *ninjaWriter, name string,
// A buildDef describes a build target definition.
type buildDef struct {
Comment string
Rule Rule
Outputs []*ninjaString
Inputs []*ninjaString
@ -248,9 +250,11 @@ type buildDef struct {
func parseBuildParams(scope scope, params *BuildParams) (*buildDef,
error) {
comment := params.Comment
rule := params.Rule
b := &buildDef{
Comment: comment,
Rule: rule,
}
@ -315,13 +319,14 @@ func parseBuildParams(scope scope, params *BuildParams) (*buildDef,
func (b *buildDef) WriteTo(nw *ninjaWriter, pkgNames map[*PackageContext]string) error {
var (
comment = b.Comment
rule = b.Rule.fullName(pkgNames)
outputs = valueList(b.Outputs, pkgNames, outputEscaper)
explicitDeps = valueList(b.Inputs, pkgNames, inputEscaper)
implicitDeps = valueList(b.Implicits, pkgNames, inputEscaper)
orderOnlyDeps = valueList(b.OrderOnly, pkgNames, inputEscaper)
)
err := nw.Build(rule, outputs, explicitDeps, implicitDeps, orderOnlyDeps)
err := nw.Build(comment, rule, outputs, explicitDeps, implicitDeps, orderOnlyDeps)
if err != nil {
return err
}

View file

@ -103,7 +103,7 @@ func (n *ninjaWriter) Rule(name string) error {
return err
}
func (n *ninjaWriter) Build(rule string, outputs, explicitDeps, implicitDeps,
func (n *ninjaWriter) Build(comment string, rule string, outputs, explicitDeps, implicitDeps,
orderOnlyDeps []string) error {
n.justDidBlankLine = false
@ -116,6 +116,10 @@ func (n *ninjaWriter) Build(rule string, outputs, explicitDeps, implicitDeps,
maxLineLen: maxLineLen,
}
if comment != "" {
wrapper.Comment(comment)
}
wrapper.WriteString("build")
for _, output := range outputs {

View file

@ -49,10 +49,10 @@ var ninjaWriterTestCases = []struct {
},
{
input: func(w *ninjaWriter) {
ck(w.Build("foo", []string{"o1", "o2"}, []string{"e1", "e2"},
ck(w.Build("foo comment", "foo", []string{"o1", "o2"}, []string{"e1", "e2"},
[]string{"i1", "i2"}, []string{"oo1", "oo2"}))
},
output: "build o1 o2: foo e1 e2 | i1 i2 || oo1 oo2\n",
output: "# foo comment\nbuild o1 o2: foo e1 e2 | i1 i2 || oo1 oo2\n",
},
{
input: func(w *ninjaWriter) {
@ -88,7 +88,7 @@ var ninjaWriterTestCases = []struct {
ck(w.ScopedAssign("command", "echo out: $out in: $in _arg: $_arg"))
ck(w.ScopedAssign("pool", "p"))
ck(w.BlankLine())
ck(w.Build("r", []string{"foo.o"}, []string{"foo.in"}, nil, nil))
ck(w.Build("r comment", "r", []string{"foo.o"}, []string{"foo.in"}, nil, nil))
ck(w.ScopedAssign("_arg", "arg value"))
},
output: `pool p
@ -99,6 +99,7 @@ rule r
command = echo out: $out in: $in _arg: $_arg
pool = p
# r comment
build foo.o: r foo.in
_arg = arg value
`,