2014-05-28 01:34:41 +02:00
|
|
|
package blueprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2014-07-03 01:40:31 +02:00
|
|
|
"text/scanner"
|
2014-05-28 01:34:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Module interface {
|
|
|
|
GenerateBuildActions(ModuleContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ModuleContext interface {
|
|
|
|
ModuleName() string
|
2014-06-23 02:02:55 +02:00
|
|
|
OtherModuleName(m Module) string
|
2014-05-28 01:34:41 +02:00
|
|
|
ModuleDir() string
|
2014-06-12 03:31:16 +02:00
|
|
|
Config() interface{}
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2014-07-03 01:40:31 +02:00
|
|
|
Errorf(pos scanner.Position, fmt string, args ...interface{})
|
2014-05-28 01:34:41 +02:00
|
|
|
ModuleErrorf(fmt string, args ...interface{})
|
|
|
|
PropertyErrorf(property, fmt string, args ...interface{})
|
2014-06-23 02:02:55 +02:00
|
|
|
OtherModuleErrorf(m Module, fmt string, args ...interface{})
|
2014-07-03 01:40:31 +02:00
|
|
|
Failed() bool
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
Variable(name, value string)
|
2014-06-06 05:00:22 +02:00
|
|
|
Rule(name string, params RuleParams, argNames ...string) Rule
|
2014-05-28 01:34:41 +02:00
|
|
|
Build(params BuildParams)
|
|
|
|
|
|
|
|
VisitDepsDepthFirst(visit func(Module))
|
|
|
|
VisitDepsDepthFirstIf(pred func(Module) bool, visit func(Module))
|
2014-06-26 02:21:54 +02:00
|
|
|
|
|
|
|
AddNinjaFileDeps(deps ...string)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ ModuleContext = (*moduleContext)(nil)
|
|
|
|
|
|
|
|
type moduleContext struct {
|
|
|
|
context *Context
|
2014-06-12 03:31:16 +02:00
|
|
|
config interface{}
|
2014-05-28 01:34:41 +02:00
|
|
|
module Module
|
|
|
|
scope *localScope
|
|
|
|
info *moduleInfo
|
|
|
|
|
2014-06-26 02:21:54 +02:00
|
|
|
ninjaFileDeps []string
|
|
|
|
errs []error
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
actionDefs localBuildActions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *moduleContext) ModuleName() string {
|
|
|
|
return m.info.properties.Name
|
|
|
|
}
|
|
|
|
|
2014-06-23 02:02:55 +02:00
|
|
|
func (m *moduleContext) OtherModuleName(module Module) string {
|
|
|
|
info := m.context.moduleInfo[module]
|
|
|
|
return info.properties.Name
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
func (m *moduleContext) ModuleDir() string {
|
2014-06-13 05:06:31 +02:00
|
|
|
return filepath.Dir(m.info.relBlueprintsFile)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-12 03:31:16 +02:00
|
|
|
func (m *moduleContext) Config() interface{} {
|
2014-05-28 01:34:41 +02:00
|
|
|
return m.config
|
|
|
|
}
|
|
|
|
|
2014-07-03 01:40:31 +02:00
|
|
|
func (m *moduleContext) Errorf(pos scanner.Position, format string,
|
|
|
|
args ...interface{}) {
|
|
|
|
|
|
|
|
m.errs = append(m.errs, &Error{
|
|
|
|
Err: fmt.Errorf(format, args...),
|
|
|
|
Pos: pos,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
func (m *moduleContext) ModuleErrorf(format string, args ...interface{}) {
|
|
|
|
m.errs = append(m.errs, &Error{
|
|
|
|
Err: fmt.Errorf(format, args...),
|
|
|
|
Pos: m.info.pos,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *moduleContext) PropertyErrorf(property, format string,
|
|
|
|
args ...interface{}) {
|
|
|
|
|
|
|
|
pos, ok := m.info.propertyPos[property]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("property %q was not set for this module", property))
|
|
|
|
}
|
|
|
|
|
|
|
|
m.errs = append(m.errs, &Error{
|
|
|
|
Err: fmt.Errorf(format, args...),
|
|
|
|
Pos: pos,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-06-23 02:02:55 +02:00
|
|
|
func (m *moduleContext) OtherModuleErrorf(module Module, format string,
|
|
|
|
args ...interface{}) {
|
|
|
|
|
|
|
|
info := m.context.moduleInfo[module]
|
|
|
|
m.errs = append(m.errs, &Error{
|
|
|
|
Err: fmt.Errorf(format, args...),
|
|
|
|
Pos: info.pos,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-03 01:40:31 +02:00
|
|
|
func (m *moduleContext) Failed() bool {
|
|
|
|
return len(m.errs) > 0
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
func (m *moduleContext) Variable(name, value string) {
|
2014-07-01 03:07:17 +02:00
|
|
|
const skip = 2
|
|
|
|
m.scope.ReparentToCallerPackage(skip)
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
v, err := m.scope.AddLocalVariable(name, value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.actionDefs.variables = append(m.actionDefs.variables, v)
|
|
|
|
}
|
|
|
|
|
2014-06-06 05:00:22 +02:00
|
|
|
func (m *moduleContext) Rule(name string, params RuleParams,
|
|
|
|
argNames ...string) Rule {
|
|
|
|
|
2014-07-01 03:07:17 +02:00
|
|
|
const skip = 2
|
|
|
|
m.scope.ReparentToCallerPackage(skip)
|
|
|
|
|
2014-06-06 05:00:22 +02:00
|
|
|
r, err := m.scope.AddLocalRule(name, ¶ms, argNames...)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.actionDefs.rules = append(m.actionDefs.rules, r)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *moduleContext) Build(params BuildParams) {
|
2014-07-01 03:07:17 +02:00
|
|
|
const skip = 2
|
|
|
|
m.scope.ReparentToCallerPackage(skip)
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
def, err := parseBuildParams(m.scope, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.actionDefs.buildDefs = append(m.actionDefs.buildDefs, def)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *moduleContext) VisitDepsDepthFirst(visit func(Module)) {
|
|
|
|
m.context.visitDepsDepthFirst(m.module, visit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *moduleContext) VisitDepsDepthFirstIf(pred func(Module) bool,
|
|
|
|
visit func(Module)) {
|
|
|
|
|
|
|
|
m.context.visitDepsDepthFirstIf(m.module, pred, visit)
|
|
|
|
}
|
2014-06-26 02:21:54 +02:00
|
|
|
|
|
|
|
func (m *moduleContext) AddNinjaFileDeps(deps ...string) {
|
|
|
|
m.ninjaFileDeps = append(m.ninjaFileDeps, deps...)
|
|
|
|
}
|