From aa873e1c65c28092c269b94d03b330eb2a2c1dc9 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Tue, 22 Nov 2016 13:39:58 -0800 Subject: [PATCH] Support deps and depfile properties on build statements Ninja supports deps and depfile properties specified on build statements, allow them to be specified in BuildParams. Change-Id: I46eac5571350426f1419908def21f7d042f8739a --- ninja_defs.go | 57 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/ninja_defs.go b/ninja_defs.go index cda2559..8737ed2 100644 --- a/ninja_defs.go +++ b/ninja_defs.go @@ -77,6 +77,8 @@ type RuleParams struct { // 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. + Depfile string // The dependency file name. + Deps Deps // The format of the dependency file. Rule Rule // The rule to invoke. Outputs []string // The list of explicit output targets. ImplicitOutputs []string // The list of implicit output targets. @@ -229,17 +231,9 @@ func (r *ruleDef) WriteTo(nw *ninjaWriter, name string, } } - var keys []string - for k := range r.Variables { - keys = append(keys, k) - } - sort.Strings(keys) - - for _, name := range keys { - err = nw.ScopedAssign(name, r.Variables[name].Value(pkgNames)) - if err != nil { - return err - } + err = writeVariables(nw, r.Variables, pkgNames) + if err != nil { + return err } return nil @@ -256,6 +250,7 @@ type buildDef struct { Implicits []*ninjaString OrderOnly []*ninjaString Args map[Variable]*ninjaString + Variables map[string]*ninjaString Optional bool } @@ -306,6 +301,24 @@ func parseBuildParams(scope scope, params *BuildParams) (*buildDef, b.Optional = params.Optional + if params.Depfile != "" || params.Deps != DepsNone { + if b.Variables == nil { + b.Variables = make(map[string]*ninjaString) + } + } + + if params.Depfile != "" { + value, err := parseNinjaString(scope, params.Depfile) + if err != nil { + return nil, fmt.Errorf("error parsing Depfile param: %s", err) + } + b.Variables["depfile"] = value + } + + if params.Deps != DepsNone { + b.Variables["deps"] = simpleNinjaString(params.Deps.String()) + } + argNameScope := rule.scope() if len(params.Args) > 0 { @@ -360,6 +373,11 @@ func (b *buildDef) WriteTo(nw *ninjaWriter, pkgNames map[*packageContext]string) args[argVar.fullName(pkgNames)] = value.Value(pkgNames) } + err = writeVariables(nw, b.Variables, pkgNames) + if err != nil { + return err + } + var keys []string for k := range args { keys = append(keys, k) @@ -389,3 +407,20 @@ func valueList(list []*ninjaString, pkgNames map[*packageContext]string, } return result } + +func writeVariables(nw *ninjaWriter, variables map[string]*ninjaString, + pkgNames map[*packageContext]string) error { + var keys []string + for k := range variables { + keys = append(keys, k) + } + sort.Strings(keys) + + for _, name := range keys { + err := nw.ScopedAssign(name, variables[name].Value(pkgNames)) + if err != nil { + return err + } + } + return nil +}