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
This commit is contained in:
Colin Cross 2015-03-11 16:17:52 -07:00
parent 7addea35a1
commit ab6d790165
2 changed files with 53 additions and 27 deletions

View file

@ -110,9 +110,6 @@ type moduleGroup struct {
ninjaName string ninjaName string
modules []*moduleInfo modules []*moduleInfo
// set during PrepareBuildActions
actionDefs localBuildActions
} }
type moduleInfo struct { type moduleInfo struct {
@ -145,6 +142,9 @@ type moduleInfo struct {
// set during each runMutator // set during each runMutator
splitModules []*moduleInfo splitModules []*moduleInfo
// set during PrepareBuildActions
actionDefs localBuildActions
} }
type variantMap map[string]string type variantMap map[string]string
@ -1379,7 +1379,7 @@ func (c *Context) generateModuleBuildActions(config interface{},
depsCh <- mctx.ninjaFileDeps depsCh <- mctx.ninjaFileDeps
newErrs := c.processLocalBuildActions(&module.group.actionDefs, newErrs := c.processLocalBuildActions(&module.actionDefs,
&mctx.actionDefs, liveGlobals) &mctx.actionDefs, liveGlobals)
if len(newErrs) > 0 { if len(newErrs) > 0 {
errsCh <- newErrs 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 // definitions are live. As we go through copying those live locals to the
// moduleGroup we remove them from the live globals set. // moduleGroup we remove them from the live globals set.
for _, v := range in.variables { for _, v := range in.variables {
_, isLive := liveGlobals.variables[v] isLive := liveGlobals.RemoveVariableIfLive(v)
if isLive { if isLive {
out.variables = append(out.variables, v) out.variables = append(out.variables, v)
delete(liveGlobals.variables, v)
} }
} }
for _, r := range in.rules { for _, r := range in.rules {
_, isLive := liveGlobals.rules[r] isLive := liveGlobals.RemoveRuleIfLive(r)
if isLive { if isLive {
out.rules = append(out.rules, r) 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{} targets := map[string]string{}
// Collect all the module build targets. // Collect all the module build targets.
for _, info := range c.moduleGroups { for _, module := range c.moduleInfo {
for _, buildDef := range info.actionDefs.buildDefs { for _, buildDef := range module.actionDefs.buildDefs {
ruleName := buildDef.Rule.fullName(c.pkgNames) ruleName := buildDef.Rule.fullName(c.pkgNames)
for _, output := range buildDef.Outputs { for _, output := range buildDef.Outputs {
outputValue, err := output.Eval(c.globalVariables) outputValue, err := output.Eval(c.globalVariables)
@ -2029,19 +2027,23 @@ func (c *Context) writeGlobalRules(nw *ninjaWriter) error {
return nil return nil
} }
type moduleGroupSorter []*moduleGroup type moduleSorter []*moduleInfo
func (s moduleGroupSorter) Len() int { func (s moduleSorter) Len() int {
return len(s) return len(s)
} }
func (s moduleGroupSorter) Less(i, j int) bool { func (s moduleSorter) Less(i, j int) bool {
iName := s[i].name iName := s[i].properties.Name
jName := s[j].name jName := s[j].properties.Name
if iName == jName {
iName = s[i].subName()
jName = s[j].subName()
}
return iName < jName 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] s[i], s[j] = s[j], s[i]
} }
@ -2053,33 +2055,34 @@ func (c *Context) writeAllModuleActions(nw *ninjaWriter) error {
panic(err) panic(err)
} }
infos := make([]*moduleGroup, 0, len(c.moduleGroups)) modules := make([]*moduleInfo, 0, len(c.moduleInfo))
for _, info := range c.moduleGroups { for _, module := range c.moduleInfo {
infos = append(infos, info) modules = append(modules, module)
} }
sort.Sort(moduleGroupSorter(infos)) sort.Sort(moduleSorter(modules))
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
for _, info := range infos { for _, module := range modules {
buf.Reset() buf.Reset()
// In order to make the bootstrap build manifest independent of the // In order to make the bootstrap build manifest independent of the
// build dir we need to output the Blueprints file locations in the // build dir we need to output the Blueprints file locations in the
// comments as paths relative to the source directory. // comments as paths relative to the source directory.
relPos := info.modules[0].pos relPos := module.pos
relPos.Filename = info.modules[0].relBlueprintsFile relPos.Filename = module.relBlueprintsFile
// Get the name and location of the factory function for the module. // 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()) factoryFunc := runtime.FuncForPC(reflect.ValueOf(factory).Pointer())
factoryName := factoryFunc.Name() factoryName := factoryFunc.Name()
infoMap := map[string]interface{}{ infoMap := map[string]interface{}{
"properties": info.modules[0].properties, "properties": module.properties,
"typeName": info.modules[0].typeName, "typeName": module.typeName,
"goFactory": factoryName, "goFactory": factoryName,
"pos": relPos, "pos": relPos,
"variant": module.subName(),
} }
err = headerTemplate.Execute(buf, infoMap) err = headerTemplate.Execute(buf, infoMap)
if err != nil { if err != nil {
@ -2096,7 +2099,7 @@ func (c *Context) writeAllModuleActions(nw *ninjaWriter) error {
return err return err
} }
err = c.writeLocalBuildActions(nw, &info.actionDefs) err = c.writeLocalBuildActions(nw, &module.actionDefs)
if err != nil { if err != nil {
return err return err
} }
@ -2245,6 +2248,7 @@ they were generated by the following Go packages:
var moduleHeaderTemplate = `# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # var moduleHeaderTemplate = `# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Module: {{.properties.Name}} Module: {{.properties.Name}}
Variant: {{.variant}}
Type: {{.typeName}} Type: {{.typeName}}
Factory: {{.goFactory}} Factory: {{.goFactory}}
Defined: {{.pos}} Defined: {{.pos}}

View file

@ -167,3 +167,25 @@ func (l *liveTracker) addNinjaStringDeps(str *ninjaString) error {
} }
return nil 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
}