From ab6d790165d557a2764f76f6dff6bfba57450310 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 11 Mar 2015 16:17:52 -0700 Subject: [PATCH] Move build actions into modules Move actionDefs from moduleGroup to moduleInfo. This will result in multiple build.ninja headers for a single Blueprint file module if it has multiple variants, each one specifying the variant used to generate the following rules. Change-Id: I414cd4d3266da8c2e92118b295569627ddf48cdd --- context.go | 58 ++++++++++++++++++++++++++----------------------- live_tracker.go | 22 +++++++++++++++++++ 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/context.go b/context.go index 0d18f76..8f5cedb 100644 --- a/context.go +++ b/context.go @@ -110,9 +110,6 @@ type moduleGroup struct { ninjaName string modules []*moduleInfo - - // set during PrepareBuildActions - actionDefs localBuildActions } type moduleInfo struct { @@ -145,6 +142,9 @@ type moduleInfo struct { // set during each runMutator splitModules []*moduleInfo + + // set during PrepareBuildActions + actionDefs localBuildActions } type variantMap map[string]string @@ -1379,7 +1379,7 @@ func (c *Context) generateModuleBuildActions(config interface{}, depsCh <- mctx.ninjaFileDeps - newErrs := c.processLocalBuildActions(&module.group.actionDefs, + newErrs := c.processLocalBuildActions(&module.actionDefs, &mctx.actionDefs, liveGlobals) if len(newErrs) > 0 { errsCh <- newErrs @@ -1460,18 +1460,16 @@ func (c *Context) processLocalBuildActions(out, in *localBuildActions, // definitions are live. As we go through copying those live locals to the // moduleGroup we remove them from the live globals set. for _, v := range in.variables { - _, isLive := liveGlobals.variables[v] + isLive := liveGlobals.RemoveVariableIfLive(v) if isLive { out.variables = append(out.variables, v) - delete(liveGlobals.variables, v) } } for _, r := range in.rules { - _, isLive := liveGlobals.rules[r] + isLive := liveGlobals.RemoveRuleIfLive(r) if isLive { out.rules = append(out.rules, r) - delete(liveGlobals.rules, r) } } @@ -1729,8 +1727,8 @@ func (c *Context) AllTargets() (map[string]string, error) { targets := map[string]string{} // Collect all the module build targets. - for _, info := range c.moduleGroups { - for _, buildDef := range info.actionDefs.buildDefs { + for _, module := range c.moduleInfo { + for _, buildDef := range module.actionDefs.buildDefs { ruleName := buildDef.Rule.fullName(c.pkgNames) for _, output := range buildDef.Outputs { outputValue, err := output.Eval(c.globalVariables) @@ -2029,19 +2027,23 @@ func (c *Context) writeGlobalRules(nw *ninjaWriter) error { return nil } -type moduleGroupSorter []*moduleGroup +type moduleSorter []*moduleInfo -func (s moduleGroupSorter) Len() int { +func (s moduleSorter) Len() int { return len(s) } -func (s moduleGroupSorter) Less(i, j int) bool { - iName := s[i].name - jName := s[j].name +func (s moduleSorter) Less(i, j int) bool { + iName := s[i].properties.Name + jName := s[j].properties.Name + if iName == jName { + iName = s[i].subName() + jName = s[j].subName() + } return iName < jName } -func (s moduleGroupSorter) Swap(i, j int) { +func (s moduleSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] } @@ -2053,33 +2055,34 @@ func (c *Context) writeAllModuleActions(nw *ninjaWriter) error { panic(err) } - infos := make([]*moduleGroup, 0, len(c.moduleGroups)) - for _, info := range c.moduleGroups { - infos = append(infos, info) + modules := make([]*moduleInfo, 0, len(c.moduleInfo)) + for _, module := range c.moduleInfo { + modules = append(modules, module) } - sort.Sort(moduleGroupSorter(infos)) + sort.Sort(moduleSorter(modules)) buf := bytes.NewBuffer(nil) - for _, info := range infos { + for _, module := range modules { buf.Reset() // In order to make the bootstrap build manifest independent of the // build dir we need to output the Blueprints file locations in the // comments as paths relative to the source directory. - relPos := info.modules[0].pos - relPos.Filename = info.modules[0].relBlueprintsFile + relPos := module.pos + relPos.Filename = module.relBlueprintsFile // Get the name and location of the factory function for the module. - factory := c.moduleFactories[info.modules[0].typeName] + factory := c.moduleFactories[module.typeName] factoryFunc := runtime.FuncForPC(reflect.ValueOf(factory).Pointer()) factoryName := factoryFunc.Name() infoMap := map[string]interface{}{ - "properties": info.modules[0].properties, - "typeName": info.modules[0].typeName, + "properties": module.properties, + "typeName": module.typeName, "goFactory": factoryName, "pos": relPos, + "variant": module.subName(), } err = headerTemplate.Execute(buf, infoMap) if err != nil { @@ -2096,7 +2099,7 @@ func (c *Context) writeAllModuleActions(nw *ninjaWriter) error { return err } - err = c.writeLocalBuildActions(nw, &info.actionDefs) + err = c.writeLocalBuildActions(nw, &module.actionDefs) if err != nil { return err } @@ -2245,6 +2248,7 @@ they were generated by the following Go packages: var moduleHeaderTemplate = `# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Module: {{.properties.Name}} +Variant: {{.variant}} Type: {{.typeName}} Factory: {{.goFactory}} Defined: {{.pos}} diff --git a/live_tracker.go b/live_tracker.go index 5a74a5b..4157f95 100644 --- a/live_tracker.go +++ b/live_tracker.go @@ -167,3 +167,25 @@ func (l *liveTracker) addNinjaStringDeps(str *ninjaString) error { } return nil } + +func (l *liveTracker) RemoveVariableIfLive(v Variable) bool { + l.Lock() + defer l.Unlock() + + _, isLive := l.variables[v] + if isLive { + delete(l.variables, v) + } + return isLive +} + +func (l *liveTracker) RemoveRuleIfLive(r Rule) bool { + l.Lock() + defer l.Unlock() + + _, isLive := l.rules[r] + if isLive { + delete(l.rules, r) + } + return isLive +}