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"
|
2017-12-11 23:39:10 +01:00
|
|
|
|
2017-02-01 22:21:35 +01:00
|
|
|
"github.com/google/blueprint/pathtools"
|
2014-05-28 01:34:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2016-07-26 00:51:50 +02:00
|
|
|
ModuleType(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
|
|
|
|
Run globs during earlier bootstrap phases
Instead of sometimes re-running minibp/the primary builder during the
next phase, run bpglob earlier to check dependencies.
We've run into issues where the environment is slightly different
between bootstrapping phase and the main build phase. It's also a
problem because our primary builder (Soong) exports information used by
another tool (Kati) that runs in between the bootstrapping phases and
the main phase. When Soong would run in the main phase, it could get out
of sync, and would require the build to be run again.
To do this, add a "subninja" include a build-globs.ninja file to each
build.ninja file. The first time, this will be an empty file, but we'll
always run minibp / the primary builder anyway. When the builder runs,
in addition to writing a dependency file, write out the
build-globs.ninja file with the rules to run bpglob.
Since bpglob may need to be run very early, before it would normally be
built, build it with microfactory.
Change-Id: I89fcd849a8729e892f163d40060ab90b5d4dfa5d
2018-07-06 06:56:59 +02:00
|
|
|
// AddSubninja adds a ninja file to include with subninja. This should likely
|
|
|
|
// only ever be used inside bootstrap to handle glob rules.
|
|
|
|
AddSubninja(file string)
|
|
|
|
|
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)
|
2016-11-01 19:10:51 +01:00
|
|
|
|
2018-02-23 23:49:45 +01:00
|
|
|
// GlobWithDeps returns a list of files and directories that match the
|
|
|
|
// specified pattern but do not match any of the patterns in excludes.
|
|
|
|
// Any directories will have a '/' suffix. It also adds efficient
|
|
|
|
// dependencies to rerun the primary builder whenever a file matching
|
|
|
|
// the pattern as added or removed, without rerunning if a file that
|
|
|
|
// does not match the pattern is added to a searched directory.
|
2016-11-01 19:10:51 +01:00
|
|
|
GlobWithDeps(pattern string, excludes []string) ([]string, error)
|
2017-02-01 22:21:35 +01:00
|
|
|
|
|
|
|
Fs() pathtools.FileSystem
|
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)
|
|
|
|
}
|
|
|
|
|
2016-07-26 00:51:50 +02:00
|
|
|
func (s *singletonContext) ModuleType(logicModule Module) string {
|
|
|
|
return s.context.ModuleType(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
|
|
|
}
|
|
|
|
|
Run globs during earlier bootstrap phases
Instead of sometimes re-running minibp/the primary builder during the
next phase, run bpglob earlier to check dependencies.
We've run into issues where the environment is slightly different
between bootstrapping phase and the main build phase. It's also a
problem because our primary builder (Soong) exports information used by
another tool (Kati) that runs in between the bootstrapping phases and
the main phase. When Soong would run in the main phase, it could get out
of sync, and would require the build to be run again.
To do this, add a "subninja" include a build-globs.ninja file to each
build.ninja file. The first time, this will be an empty file, but we'll
always run minibp / the primary builder anyway. When the builder runs,
in addition to writing a dependency file, write out the
build-globs.ninja file with the rules to run bpglob.
Since bpglob may need to be run very early, before it would normally be
built, build it with microfactory.
Change-Id: I89fcd849a8729e892f163d40060ab90b5d4dfa5d
2018-07-06 06:56:59 +02:00
|
|
|
func (s *singletonContext) AddSubninja(file string) {
|
|
|
|
s.context.subninjas = append(s.context.subninjas, file)
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
func (s *singletonContext) VisitAllModules(visit func(Module)) {
|
2018-09-19 22:37:29 +02:00
|
|
|
var visitingModule Module
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
panic(newPanicErrorf(r, "VisitAllModules(%s) for module %s",
|
|
|
|
funcName(visit), visitingModule))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.context.VisitAllModules(func(m Module) {
|
|
|
|
visitingModule = m
|
|
|
|
visit(m)
|
|
|
|
})
|
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...)
|
|
|
|
}
|
2016-11-01 19:10:51 +01:00
|
|
|
|
|
|
|
func (s *singletonContext) GlobWithDeps(pattern string,
|
|
|
|
excludes []string) ([]string, error) {
|
|
|
|
return s.context.glob(pattern, excludes)
|
|
|
|
}
|
2017-02-01 22:21:35 +01:00
|
|
|
|
|
|
|
func (s *singletonContext) Fs() pathtools.FileSystem {
|
|
|
|
return s.context.fs
|
|
|
|
}
|