2015-01-23 23:15:10 +01:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
package blueprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Singleton interface {
|
|
|
|
GenerateBuildActions(SingletonContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SingletonContext interface {
|
2014-06-12 03:31:16 +02:00
|
|
|
Config() interface{}
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
ModuleName(module Module) string
|
|
|
|
ModuleDir(module Module) string
|
2015-12-18 03:02:11 +01:00
|
|
|
ModuleSubDir(module Module) string
|
2014-05-28 01:34:41 +02:00
|
|
|
BlueprintFile(module Module) string
|
|
|
|
|
|
|
|
ModuleErrorf(module Module, format string, args ...interface{})
|
|
|
|
Errorf(format string, args ...interface{})
|
2015-12-10 03:06:23 +01:00
|
|
|
Failed() bool
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
Variable(pctx PackageContext, name, value string)
|
|
|
|
Rule(pctx PackageContext, name string, params RuleParams, argNames ...string) Rule
|
|
|
|
Build(pctx PackageContext, params BuildParams)
|
2014-05-28 01:34:41 +02:00
|
|
|
RequireNinjaVersion(major, minor, micro int)
|
|
|
|
|
2015-11-19 01:01:01 +01:00
|
|
|
// SetNinjaBuildDir sets the value of the top-level "builddir" Ninja variable
|
2014-05-28 01:34:41 +02:00
|
|
|
// that controls where Ninja stores its build log files. This value can be
|
2015-11-19 01:01:01 +01:00
|
|
|
// set at most one time for a single build, later calls are ignored.
|
2015-11-26 00:29:32 +01:00
|
|
|
SetNinjaBuildDir(pctx PackageContext, value string)
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2016-01-15 00:42:54 +01:00
|
|
|
// Eval takes a string with embedded ninja variables, and returns a string
|
|
|
|
// with all of the variables recursively expanded. Any variables references
|
|
|
|
// are expanded in the scope of the PackageContext.
|
|
|
|
Eval(pctx PackageContext, ninjaStr string) (string, error)
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
VisitAllModules(visit func(Module))
|
|
|
|
VisitAllModulesIf(pred func(Module) bool, visit func(Module))
|
2014-06-17 19:24:24 +02:00
|
|
|
VisitDepsDepthFirst(module Module, visit func(Module))
|
|
|
|
VisitDepsDepthFirstIf(module Module, pred func(Module) bool,
|
|
|
|
visit func(Module))
|
2014-06-26 02:21:54 +02:00
|
|
|
|
2015-11-18 01:22:29 +01:00
|
|
|
VisitAllModuleVariants(module Module, visit func(Module))
|
|
|
|
|
|
|
|
PrimaryModule(module Module) Module
|
|
|
|
FinalModule(module Module) Module
|
|
|
|
|
2014-06-26 02:21:54 +02:00
|
|
|
AddNinjaFileDeps(deps ...string)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ SingletonContext = (*singletonContext)(nil)
|
|
|
|
|
|
|
|
type singletonContext struct {
|
|
|
|
context *Context
|
2014-06-12 03:31:16 +02:00
|
|
|
config interface{}
|
2014-05-28 01:34:41 +02:00
|
|
|
scope *localScope
|
2016-01-15 00:42:54 +01:00
|
|
|
globals *liveTracker
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2014-06-26 02:21:54 +02:00
|
|
|
ninjaFileDeps []string
|
|
|
|
errs []error
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
actionDefs localBuildActions
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:31:16 +02:00
|
|
|
func (s *singletonContext) Config() interface{} {
|
2014-05-28 01:34:41 +02:00
|
|
|
return s.config
|
|
|
|
}
|
|
|
|
|
2014-12-18 01:12:41 +01:00
|
|
|
func (s *singletonContext) ModuleName(logicModule Module) string {
|
2015-05-13 23:36:24 +02:00
|
|
|
return s.context.ModuleName(logicModule)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2014-12-18 01:12:41 +01:00
|
|
|
func (s *singletonContext) ModuleDir(logicModule Module) string {
|
2015-05-13 23:36:24 +02:00
|
|
|
return s.context.ModuleDir(logicModule)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-12-18 03:02:11 +01:00
|
|
|
func (s *singletonContext) ModuleSubDir(logicModule Module) string {
|
|
|
|
return s.context.ModuleSubDir(logicModule)
|
|
|
|
}
|
|
|
|
|
2014-12-18 01:12:41 +01:00
|
|
|
func (s *singletonContext) BlueprintFile(logicModule Module) string {
|
2015-05-13 23:36:24 +02:00
|
|
|
return s.context.BlueprintFile(logicModule)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2016-01-07 22:43:09 +01:00
|
|
|
func (s *singletonContext) error(err error) {
|
|
|
|
if err != nil {
|
|
|
|
s.errs = append(s.errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 01:12:41 +01:00
|
|
|
func (s *singletonContext) ModuleErrorf(logicModule Module, format string,
|
2014-05-28 01:34:41 +02:00
|
|
|
args ...interface{}) {
|
|
|
|
|
2016-01-07 22:43:09 +01:00
|
|
|
s.error(s.context.ModuleErrorf(logicModule, format, args...))
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *singletonContext) Errorf(format string, args ...interface{}) {
|
|
|
|
// TODO: Make this not result in the error being printed as "internal error"
|
2016-01-07 22:43:09 +01:00
|
|
|
s.error(fmt.Errorf(format, args...))
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-12-10 03:06:23 +01:00
|
|
|
func (s *singletonContext) Failed() bool {
|
|
|
|
return len(s.errs) > 0
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (s *singletonContext) Variable(pctx PackageContext, name, value string) {
|
2014-10-03 11:49:58 +02:00
|
|
|
s.scope.ReparentTo(pctx)
|
2014-07-01 03:07:17 +02:00
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
v, err := s.scope.AddLocalVariable(name, value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.actionDefs.variables = append(s.actionDefs.variables, v)
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (s *singletonContext) Rule(pctx PackageContext, name string,
|
2014-10-03 11:49:58 +02:00
|
|
|
params RuleParams, argNames ...string) Rule {
|
2014-06-06 05:00:22 +02:00
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
s.scope.ReparentTo(pctx)
|
2014-07-01 03:07:17 +02:00
|
|
|
|
2014-06-06 05:00:22 +02:00
|
|
|
r, err := s.scope.AddLocalRule(name, ¶ms, argNames...)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.actionDefs.rules = append(s.actionDefs.rules, r)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (s *singletonContext) Build(pctx PackageContext, params BuildParams) {
|
2014-10-03 11:49:58 +02:00
|
|
|
s.scope.ReparentTo(pctx)
|
2014-07-01 03:07:17 +02:00
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
def, err := parseBuildParams(s.scope, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.actionDefs.buildDefs = append(s.actionDefs.buildDefs, def)
|
|
|
|
}
|
|
|
|
|
2016-01-15 00:42:54 +01:00
|
|
|
func (s *singletonContext) Eval(pctx PackageContext, str string) (string, error) {
|
|
|
|
s.scope.ReparentTo(pctx)
|
|
|
|
|
|
|
|
ninjaStr, err := parseNinjaString(s.scope, str)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.globals.addNinjaStringDeps(ninjaStr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ninjaStr.Eval(s.globals.variables)
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
func (s *singletonContext) RequireNinjaVersion(major, minor, micro int) {
|
|
|
|
s.context.requireNinjaVersion(major, minor, micro)
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (s *singletonContext) SetNinjaBuildDir(pctx PackageContext, value string) {
|
2014-10-03 11:49:58 +02:00
|
|
|
s.scope.ReparentTo(pctx)
|
2014-09-25 02:51:52 +02:00
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
ninjaValue, err := parseNinjaString(s.scope, value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-11-19 01:01:01 +01:00
|
|
|
s.context.setNinjaBuildDir(ninjaValue)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *singletonContext) VisitAllModules(visit func(Module)) {
|
2015-05-13 23:36:24 +02:00
|
|
|
s.context.VisitAllModules(visit)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *singletonContext) VisitAllModulesIf(pred func(Module) bool,
|
|
|
|
visit func(Module)) {
|
|
|
|
|
2015-05-13 23:36:24 +02:00
|
|
|
s.context.VisitAllModulesIf(pred, visit)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
2014-06-17 19:24:24 +02:00
|
|
|
|
|
|
|
func (s *singletonContext) VisitDepsDepthFirst(module Module,
|
|
|
|
visit func(Module)) {
|
|
|
|
|
2015-05-13 23:36:24 +02:00
|
|
|
s.context.VisitDepsDepthFirst(module, visit)
|
2014-06-17 19:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *singletonContext) VisitDepsDepthFirstIf(module Module,
|
|
|
|
pred func(Module) bool, visit func(Module)) {
|
|
|
|
|
2015-05-13 23:36:24 +02:00
|
|
|
s.context.VisitDepsDepthFirstIf(module, pred, visit)
|
2014-06-17 19:24:24 +02:00
|
|
|
}
|
2014-06-26 02:21:54 +02:00
|
|
|
|
2015-11-18 01:22:29 +01:00
|
|
|
func (s *singletonContext) PrimaryModule(module Module) Module {
|
|
|
|
return s.context.PrimaryModule(module)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *singletonContext) FinalModule(module Module) Module {
|
|
|
|
return s.context.FinalModule(module)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *singletonContext) VisitAllModuleVariants(module Module, visit func(Module)) {
|
|
|
|
s.context.VisitAllModuleVariants(module, visit)
|
|
|
|
}
|
|
|
|
|
2014-06-26 02:21:54 +02:00
|
|
|
func (s *singletonContext) AddNinjaFileDeps(deps ...string) {
|
|
|
|
s.ninjaFileDeps = append(s.ninjaFileDeps, deps...)
|
|
|
|
}
|