diff --git a/ninja_defs.go b/ninja_defs.go index a653796..0919ea4 100644 --- a/ninja_defs.go +++ b/ninja_defs.go @@ -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 } diff --git a/ninja_writer.go b/ninja_writer.go index 883228d..42b9aa8 100644 --- a/ninja_writer.go +++ b/ninja_writer.go @@ -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 { diff --git a/ninja_writer_test.go b/ninja_writer_test.go index 1bc456d..7b34d90 100644 --- a/ninja_writer_test.go +++ b/ninja_writer_test.go @@ -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 `,