Move values from moduleGroup to moduleInfo

Move most of the contents of moduleGroup into moduleInfo.  This will
eventually reduce moduleGroup to a simple list of variants of the
same module.

Change-Id: I4289eb9953509751d7637558cd6db73373ccdf78
This commit is contained in:
Colin Cross 2015-03-11 00:57:25 -07:00
parent 6134a5c66a
commit ed342d983c
3 changed files with 109 additions and 116 deletions

View file

@ -106,16 +106,8 @@ type localBuildActions struct {
} }
type moduleGroup struct { type moduleGroup struct {
// set during Parse name string
typeName string ninjaName string
ninjaName string
relBlueprintsFile string
pos scanner.Position
propertyPos map[string]scanner.Position
properties struct {
Name string
Deps []string
}
modules []*moduleInfo modules []*moduleInfo
@ -131,6 +123,16 @@ type moduleGroup struct {
} }
type moduleInfo struct { type moduleInfo struct {
// set during Parse
typeName string
relBlueprintsFile string
pos scanner.Position
propertyPos map[string]scanner.Position
properties struct {
Name string
Deps []string
}
name []subName name []subName
logicModule Module logicModule Module
group *moduleGroup group *moduleGroup
@ -663,12 +665,11 @@ func (c *Context) createVariants(origModule *moduleInfo, mutatorName string,
newModules := []*moduleInfo{} newModules := []*moduleInfo{}
origVariantName := origModule.name origVariantName := origModule.name
group := origModule.group
var errs []error var errs []error
for i, variantName := range variantNames { for i, variantName := range variantNames {
typeName := group.typeName typeName := origModule.typeName
factory, ok := c.moduleFactories[typeName] factory, ok := c.moduleFactories[typeName]
if !ok { if !ok {
panic(fmt.Sprintf("unrecognized module type %q during cloning", typeName)) panic(fmt.Sprintf("unrecognized module type %q during cloning", typeName))
@ -683,14 +684,14 @@ func (c *Context) createVariants(origModule *moduleInfo, mutatorName string,
newProperties = origModule.moduleProperties newProperties = origModule.moduleProperties
} else { } else {
props := []interface{}{ props := []interface{}{
&group.properties, &origModule.properties,
} }
newLogicModule, newProperties = factory() newLogicModule, newProperties = factory()
newProperties = append(props, newProperties...) newProperties = append(props, newProperties...)
if len(newProperties) != len(origModule.moduleProperties) { if len(newProperties) != len(origModule.moduleProperties) {
panic("mismatched properties array length in " + group.properties.Name) panic("mismatched properties array length in " + origModule.properties.Name)
} }
for i := range newProperties { for i := range newProperties {
@ -708,13 +709,12 @@ func (c *Context) createVariants(origModule *moduleInfo, mutatorName string,
} }
newVariantName = append(newVariantName, newSubName) newVariantName = append(newVariantName, newSubName)
newModule := &moduleInfo{ m := *origModule
group: group, newModule := &m
directDeps: append([]*moduleInfo(nil), origModule.directDeps...), newModule.directDeps = append([]*moduleInfo(nil), origModule.directDeps...)
logicModule: newLogicModule, newModule.logicModule = newLogicModule
name: newVariantName, newModule.name = newVariantName
moduleProperties: newProperties, newModule.moduleProperties = newProperties
}
newModules = append(newModules, newModule) newModules = append(newModules, newModule)
c.moduleInfo[newModule.logicModule] = newModule c.moduleInfo[newModule.logicModule] = newModule
@ -747,9 +747,9 @@ func (c *Context) convertDepsToVariant(module *moduleInfo, newSubName subName) (
if newDep == nil { if newDep == nil {
errs = append(errs, &Error{ errs = append(errs, &Error{
Err: fmt.Errorf("failed to find variant %q for module %q needed by %q", Err: fmt.Errorf("failed to find variant %q for module %q needed by %q",
newSubName.variantName, dep.group.properties.Name, newSubName.variantName, dep.properties.Name,
module.group.properties.Name), module.properties.Name),
Pos: module.group.pos, Pos: module.pos,
}) })
continue continue
} }
@ -779,67 +779,69 @@ func (c *Context) processModuleDef(moduleDef *parser.Module,
} }
logicModule, properties := factory() logicModule, properties := factory()
group := &moduleGroup{
module := &moduleInfo{
logicModule: logicModule,
typeName: typeName, typeName: typeName,
relBlueprintsFile: relBlueprintsFile, relBlueprintsFile: relBlueprintsFile,
} }
props := []interface{}{ props := []interface{}{
&group.properties, &module.properties,
} }
properties = append(props, properties...) properties = append(props, properties...)
module.moduleProperties = properties
propertyMap, errs := unpackProperties(moduleDef.Properties, properties...) propertyMap, errs := unpackProperties(moduleDef.Properties, properties...)
if len(errs) > 0 { if len(errs) > 0 {
return nil, errs return nil, errs
} }
ninjaName := toNinjaName(group.properties.Name) module.pos = moduleDef.Type.Pos
module.propertyPos = make(map[string]scanner.Position)
// The sanitizing in toNinjaName can result in collisions, uniquify the name if it
// already exists
for i := 0; c.moduleNinjaNames[ninjaName] != nil; i++ {
ninjaName = toNinjaName(group.properties.Name) + strconv.Itoa(i)
}
c.moduleNinjaNames[ninjaName] = group
group.ninjaName = ninjaName
group.pos = moduleDef.Type.Pos
group.propertyPos = make(map[string]scanner.Position)
for name, propertyDef := range propertyMap { for name, propertyDef := range propertyMap {
group.propertyPos[name] = propertyDef.Pos module.propertyPos[name] = propertyDef.Pos
} }
module := &moduleInfo{
group: group,
logicModule: logicModule,
moduleProperties: properties,
}
group.modules = []*moduleInfo{module}
return module, nil return module, nil
} }
func (c *Context) addModules(modules []*moduleInfo) (errs []error) { func (c *Context) addModules(modules []*moduleInfo) (errs []error) {
for _, module := range modules { for _, module := range modules {
name := module.group.properties.Name name := module.properties.Name
if first, present := c.moduleGroups[name]; present { c.moduleInfo[module.logicModule] = module
if group, present := c.moduleGroups[name]; present {
errs = append(errs, []error{ errs = append(errs, []error{
&Error{ &Error{
Err: fmt.Errorf("module %q already defined", name), Err: fmt.Errorf("module %q already defined", name),
Pos: module.group.pos, Pos: module.pos,
}, },
&Error{ &Error{
Err: fmt.Errorf("<-- previous definition here"), Err: fmt.Errorf("<-- previous definition here"),
Pos: first.pos, Pos: group.modules[0].pos,
}, },
}...) }...)
continue continue
} } else {
ninjaName := toNinjaName(module.properties.Name)
c.moduleGroups[name] = module.group // The sanitizing in toNinjaName can result in collisions, uniquify the name if it
c.moduleInfo[module.logicModule] = module // already exists
for i := 0; c.moduleNinjaNames[ninjaName] != nil; i++ {
ninjaName = toNinjaName(module.properties.Name) + strconv.Itoa(i)
}
c.moduleNinjaNames[ninjaName] = group
group := &moduleGroup{
name: module.properties.Name,
ninjaName: ninjaName,
modules: []*moduleInfo{module},
}
module.group = group
c.moduleGroups[name] = group
}
} }
return errs return errs
@ -873,29 +875,26 @@ func (c *Context) ResolveDependencies(config interface{}) []error {
// this set consists of the union of those module names listed in its "deps" // this set consists of the union of those module names listed in its "deps"
// property and those returned by its DynamicDependencies method. Otherwise it // property and those returned by its DynamicDependencies method. Otherwise it
// is simply those names listed in its "deps" property. // is simply those names listed in its "deps" property.
func (c *Context) moduleDepNames(group *moduleGroup, func (c *Context) moduleDepNames(module *moduleInfo,
config interface{}) ([]string, []error) { config interface{}) ([]string, []error) {
depNamesSet := make(map[string]bool) depNamesSet := make(map[string]bool)
depNames := []string{} depNames := []string{}
for _, depName := range group.properties.Deps { for _, depName := range module.properties.Deps {
if !depNamesSet[depName] { if !depNamesSet[depName] {
depNamesSet[depName] = true depNamesSet[depName] = true
depNames = append(depNames, depName) depNames = append(depNames, depName)
} }
} }
if len(group.modules) != 1 { logicModule := module.logicModule
panic("expected a single module during moduleDepNames")
}
logicModule := group.modules[0].logicModule
dynamicDepender, ok := logicModule.(DynamicDependerModule) dynamicDepender, ok := logicModule.(DynamicDependerModule)
if ok { if ok {
ddmctx := &baseModuleContext{ ddmctx := &baseModuleContext{
context: c, context: c,
config: config, config: config,
group: group, module: module,
} }
dynamicDeps := dynamicDepender.DynamicDependencies(ddmctx) dynamicDeps := dynamicDepender.DynamicDependencies(ddmctx)
@ -920,23 +919,22 @@ func (c *Context) moduleDepNames(group *moduleGroup,
// modules. // modules.
func (c *Context) resolveDependencies(config interface{}) (errs []error) { func (c *Context) resolveDependencies(config interface{}) (errs []error) {
for _, group := range c.moduleGroups { for _, group := range c.moduleGroups {
depNames, newErrs := c.moduleDepNames(group, config) for _, module := range group.modules {
if len(newErrs) > 0 { depNames, newErrs := c.moduleDepNames(module, config)
errs = append(errs, newErrs...)
continue
}
if len(group.modules) != 1 {
panic("expected a single module in resolveDependencies")
}
group.modules[0].directDeps = make([]*moduleInfo, 0, len(depNames))
for _, depName := range depNames {
newErrs := c.addDependency(group.modules[0], depName)
if len(newErrs) > 0 { if len(newErrs) > 0 {
errs = append(errs, newErrs...) errs = append(errs, newErrs...)
continue continue
} }
module.directDeps = make([]*moduleInfo, 0, len(depNames))
for _, depName := range depNames {
newErrs := c.addDependency(module, depName)
if len(newErrs) > 0 {
errs = append(errs, newErrs...)
continue
}
}
} }
} }
@ -944,9 +942,9 @@ func (c *Context) resolveDependencies(config interface{}) (errs []error) {
} }
func (c *Context) addDependency(module *moduleInfo, depName string) []error { func (c *Context) addDependency(module *moduleInfo, depName string) []error {
depsPos := module.group.propertyPos["deps"] depsPos := module.propertyPos["deps"]
if depName == module.group.properties.Name { if depName == module.properties.Name {
return []error{&Error{ return []error{&Error{
Err: fmt.Errorf("%q depends on itself", depName), Err: fmt.Errorf("%q depends on itself", depName),
Pos: depsPos, Pos: depsPos,
@ -957,14 +955,14 @@ func (c *Context) addDependency(module *moduleInfo, depName string) []error {
if !ok { if !ok {
return []error{&Error{ return []error{&Error{
Err: fmt.Errorf("%q depends on undefined module %q", Err: fmt.Errorf("%q depends on undefined module %q",
module.group.properties.Name, depName), module.properties.Name, depName),
Pos: depsPos, Pos: depsPos,
}} }}
} }
if len(depInfo.modules) != 1 { if len(depInfo.modules) != 1 {
panic(fmt.Sprintf("cannot add dependency from %s to %s, it already has multiple variants", panic(fmt.Sprintf("cannot add dependency from %s to %s, it already has multiple variants",
module.group.properties.Name, depInfo.properties.Name)) module.properties.Name, depInfo.modules[0].properties.Name))
} }
module.directDeps = append(module.directDeps, depInfo.modules[0]) module.directDeps = append(module.directDeps, depInfo.modules[0])
@ -1035,7 +1033,7 @@ func (c *Context) updateDependencies() (errs []error) {
// their own module to the list. // their own module to the list.
errs = append(errs, &Error{ errs = append(errs, &Error{
Err: fmt.Errorf("encountered dependency cycle:"), Err: fmt.Errorf("encountered dependency cycle:"),
Pos: cycle[len(cycle)-1].pos, Pos: cycle[len(cycle)-1].modules[0].pos,
}) })
// Iterate backwards through the cycle list. // Iterate backwards through the cycle list.
@ -1044,9 +1042,9 @@ func (c *Context) updateDependencies() (errs []error) {
nextGroup := cycle[i] nextGroup := cycle[i]
errs = append(errs, &Error{ errs = append(errs, &Error{
Err: fmt.Errorf(" %q depends on %q", Err: fmt.Errorf(" %q depends on %q",
curGroup.properties.Name, curGroup.name,
nextGroup.properties.Name), nextGroup.name),
Pos: curGroup.propertyPos["deps"], Pos: curGroup.modules[0].propertyPos["deps"],
}) })
curGroup = nextGroup curGroup = nextGroup
} }
@ -1214,10 +1212,9 @@ func (c *Context) runTopDownMutator(config interface{},
baseModuleContext: baseModuleContext{ baseModuleContext: baseModuleContext{
context: c, context: c,
config: config, config: config,
group: group, module: module,
}, },
module: module, name: name,
name: name,
} }
mutator(mctx) mutator(mctx)
@ -1244,10 +1241,9 @@ func (c *Context) runBottomUpMutator(config interface{},
baseModuleContext: baseModuleContext{ baseModuleContext: baseModuleContext{
context: c, context: c,
config: config, config: config,
group: group, module: module,
}, },
module: module, name: name,
name: name,
} }
mutator(mctx) mutator(mctx)
@ -1332,10 +1328,9 @@ func (c *Context) generateModuleBuildActions(config interface{},
baseModuleContext: baseModuleContext{ baseModuleContext: baseModuleContext{
context: c, context: c,
config: config, config: config,
group: group, module: module,
}, },
module: module, scope: scope,
scope: scope,
} }
mctx.module.logicModule.GenerateBuildActions(mctx) mctx.module.logicModule.GenerateBuildActions(mctx)
@ -2005,8 +2000,8 @@ func (s moduleGroupSorter) Len() int {
} }
func (s moduleGroupSorter) Less(i, j int) bool { func (s moduleGroupSorter) Less(i, j int) bool {
iName := s[i].properties.Name iName := s[i].name
jName := s[j].properties.Name jName := s[j].name
return iName < jName return iName < jName
} }
@ -2036,17 +2031,17 @@ func (c *Context) writeAllModuleActions(nw *ninjaWriter) error {
// 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.pos relPos := info.modules[0].pos
relPos.Filename = info.relBlueprintsFile relPos.Filename = info.modules[0].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.typeName] factory := c.moduleFactories[info.modules[0].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.properties, "properties": info.modules[0].properties,
"typeName": info.typeName, "typeName": info.modules[0].typeName,
"goFactory": factoryName, "goFactory": factoryName,
"pos": relPos, "pos": relPos,
} }

View file

@ -149,21 +149,21 @@ var _ BaseModuleContext = (*baseModuleContext)(nil)
type baseModuleContext struct { type baseModuleContext struct {
context *Context context *Context
config interface{} config interface{}
group *moduleGroup module *moduleInfo
errs []error errs []error
} }
func (d *baseModuleContext) ModuleName() string { func (d *baseModuleContext) ModuleName() string {
return d.group.properties.Name return d.module.properties.Name
} }
func (d *baseModuleContext) ContainsProperty(name string) bool { func (d *baseModuleContext) ContainsProperty(name string) bool {
_, ok := d.group.propertyPos[name] _, ok := d.module.propertyPos[name]
return ok return ok
} }
func (d *baseModuleContext) ModuleDir() string { func (d *baseModuleContext) ModuleDir() string {
return filepath.Dir(d.group.relBlueprintsFile) return filepath.Dir(d.module.relBlueprintsFile)
} }
func (d *baseModuleContext) Config() interface{} { func (d *baseModuleContext) Config() interface{} {
@ -184,14 +184,14 @@ func (d *baseModuleContext) ModuleErrorf(format string,
d.errs = append(d.errs, &Error{ d.errs = append(d.errs, &Error{
Err: fmt.Errorf(format, args...), Err: fmt.Errorf(format, args...),
Pos: d.group.pos, Pos: d.module.pos,
}) })
} }
func (d *baseModuleContext) PropertyErrorf(property, format string, func (d *baseModuleContext) PropertyErrorf(property, format string,
args ...interface{}) { args ...interface{}) {
pos, ok := d.group.propertyPos[property] pos, ok := d.module.propertyPos[property]
if !ok { if !ok {
panic(fmt.Errorf("property %q was not set for this module", property)) panic(fmt.Errorf("property %q was not set for this module", property))
} }
@ -210,24 +210,23 @@ var _ ModuleContext = (*moduleContext)(nil)
type moduleContext struct { type moduleContext struct {
baseModuleContext baseModuleContext
module *moduleInfo
scope *localScope scope *localScope
ninjaFileDeps []string ninjaFileDeps []string
actionDefs localBuildActions actionDefs localBuildActions
} }
func (m *moduleContext) OtherModuleName(module Module) string { func (m *moduleContext) OtherModuleName(logicModule Module) string {
info := m.context.moduleInfo[module] module := m.context.moduleInfo[logicModule]
return info.group.properties.Name return module.properties.Name
} }
func (m *moduleContext) OtherModuleErrorf(module Module, format string, func (m *moduleContext) OtherModuleErrorf(logicModule Module, format string,
args ...interface{}) { args ...interface{}) {
info := m.context.moduleInfo[module] module := m.context.moduleInfo[logicModule]
m.errs = append(m.errs, &Error{ m.errs = append(m.errs, &Error{
Err: fmt.Errorf(format, args...), Err: fmt.Errorf(format, args...),
Pos: info.group.pos, Pos: module.pos,
}) })
} }
@ -314,7 +313,6 @@ func (m *moduleContext) VisitAllModuleVariants(visit func(Module)) {
type mutatorContext struct { type mutatorContext struct {
baseModuleContext baseModuleContext
module *moduleInfo
name string name string
dependenciesModified bool dependenciesModified bool
} }

View file

@ -72,17 +72,17 @@ func (s *singletonContext) Config() interface{} {
func (s *singletonContext) ModuleName(logicModule Module) string { func (s *singletonContext) ModuleName(logicModule Module) string {
module := s.context.moduleInfo[logicModule] module := s.context.moduleInfo[logicModule]
return module.group.properties.Name return module.properties.Name
} }
func (s *singletonContext) ModuleDir(logicModule Module) string { func (s *singletonContext) ModuleDir(logicModule Module) string {
module := s.context.moduleInfo[logicModule] module := s.context.moduleInfo[logicModule]
return filepath.Dir(module.group.relBlueprintsFile) return filepath.Dir(module.relBlueprintsFile)
} }
func (s *singletonContext) BlueprintFile(logicModule Module) string { func (s *singletonContext) BlueprintFile(logicModule Module) string {
module := s.context.moduleInfo[logicModule] module := s.context.moduleInfo[logicModule]
return module.group.relBlueprintsFile return module.relBlueprintsFile
} }
func (s *singletonContext) ModuleErrorf(logicModule Module, format string, func (s *singletonContext) ModuleErrorf(logicModule Module, format string,
@ -91,7 +91,7 @@ func (s *singletonContext) ModuleErrorf(logicModule Module, format string,
module := s.context.moduleInfo[logicModule] module := s.context.moduleInfo[logicModule]
s.errs = append(s.errs, &Error{ s.errs = append(s.errs, &Error{
Err: fmt.Errorf(format, args...), Err: fmt.Errorf(format, args...),
Pos: module.group.pos, Pos: module.pos,
}) })
} }