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
diff --git a/context.go b/context.go
index 0d18f76..8f5cedb 100644
--- a/context.go
+++ b/context.go
@@ -110,9 +110,6 @@
 	ninjaName string
 
 	modules []*moduleInfo
-
-	// set during PrepareBuildActions
-	actionDefs localBuildActions
 }
 
 type moduleInfo struct {
@@ -145,6 +142,9 @@
 
 	// set during each runMutator
 	splitModules []*moduleInfo
+
+	// set during PrepareBuildActions
+	actionDefs localBuildActions
 }
 
 type variantMap map[string]string
@@ -1379,7 +1379,7 @@
 
 		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 @@
 	// 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 @@
 	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 @@
 	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 @@
 		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 @@
 			return err
 		}
 
-		err = c.writeLocalBuildActions(nw, &info.actionDefs)
+		err = c.writeLocalBuildActions(nw, &module.actionDefs)
 		if err != nil {
 			return err
 		}
@@ -2245,6 +2248,7 @@
 
 var moduleHeaderTemplate = `# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 Module:  {{.properties.Name}}
+Variant: {{.variant}}
 Type:    {{.typeName}}
 Factory: {{.goFactory}}
 Defined: {{.pos}}