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 (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-06-06 04:50:44 +02:00
|
|
|
"reflect"
|
2019-02-28 23:38:42 +01:00
|
|
|
"regexp"
|
2014-05-28 01:34:41 +02:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2015-01-08 03:08:56 +01:00
|
|
|
"sync"
|
2014-05-28 01:34:41 +02:00
|
|
|
)
|
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
// A PackageContext provides a way to create package-scoped Ninja pools,
|
|
|
|
// rules, and variables. A Go package should create a single unexported
|
|
|
|
// package-scoped PackageContext variable that it uses to create all package-
|
|
|
|
// scoped Ninja object definitions. This PackageContext object should then be
|
|
|
|
// passed to all calls to define module- or singleton-specific Ninja
|
|
|
|
// definitions. For example:
|
|
|
|
//
|
|
|
|
// package blah
|
|
|
|
//
|
|
|
|
// import (
|
|
|
|
// "blueprint"
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// var (
|
|
|
|
// pctx = NewPackageContext("path/to/blah")
|
|
|
|
//
|
|
|
|
// myPrivateVar = pctx.StaticVariable("myPrivateVar", "abcdef")
|
|
|
|
// MyExportedVar = pctx.StaticVariable("MyExportedVar", "$myPrivateVar 123456!")
|
|
|
|
//
|
|
|
|
// SomeRule = pctx.StaticRule(...)
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// // ...
|
|
|
|
//
|
|
|
|
// func (m *MyModule) GenerateBuildActions(ctx blueprint.Module) {
|
|
|
|
// ctx.Build(pctx, blueprint.BuildParams{
|
|
|
|
// Rule: SomeRule,
|
|
|
|
// Outputs: []string{"$myPrivateVar"},
|
|
|
|
// })
|
|
|
|
// }
|
2015-11-26 00:29:32 +01:00
|
|
|
type PackageContext interface {
|
|
|
|
Import(pkgPath string)
|
|
|
|
ImportAs(as, pkgPath string)
|
|
|
|
|
|
|
|
StaticVariable(name, value string) Variable
|
|
|
|
VariableFunc(name string, f func(config interface{}) (string, error)) Variable
|
|
|
|
VariableConfigMethod(name string, method interface{}) Variable
|
|
|
|
|
|
|
|
StaticPool(name string, params PoolParams) Pool
|
|
|
|
PoolFunc(name string, f func(interface{}) (PoolParams, error)) Pool
|
|
|
|
|
|
|
|
StaticRule(name string, params RuleParams, argNames ...string) Rule
|
|
|
|
RuleFunc(name string, f func(interface{}) (RuleParams, error), argNames ...string) Rule
|
|
|
|
|
2015-12-19 00:18:03 +01:00
|
|
|
AddNinjaFileDeps(deps ...string)
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
getScope() *basicScope
|
|
|
|
}
|
|
|
|
|
|
|
|
type packageContext struct {
|
2015-12-19 00:18:03 +01:00
|
|
|
fullName string
|
|
|
|
shortName string
|
|
|
|
pkgPath string
|
|
|
|
scope *basicScope
|
|
|
|
ninjaFileDeps []string
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
2015-12-19 00:18:03 +01:00
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
var _ PackageContext = &packageContext{}
|
|
|
|
|
|
|
|
func (p *packageContext) getScope() *basicScope {
|
|
|
|
return p.scope
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
var packageContexts = map[string]*packageContext{}
|
2014-10-03 11:49:58 +02:00
|
|
|
|
|
|
|
// NewPackageContext creates a PackageContext object for a given package. The
|
|
|
|
// pkgPath argument should always be set to the full path used to import the
|
|
|
|
// package. This function may only be called from a Go package's init()
|
|
|
|
// function or as part of a package-scoped variable initialization.
|
2015-11-26 00:29:32 +01:00
|
|
|
func NewPackageContext(pkgPath string) PackageContext {
|
2014-10-03 11:49:58 +02:00
|
|
|
checkCalledFromInit()
|
|
|
|
|
|
|
|
if _, present := packageContexts[pkgPath]; present {
|
2016-06-01 00:53:32 +02:00
|
|
|
panic(fmt.Errorf("package %q already has a package context", pkgPath))
|
2014-10-03 11:49:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pkgName := pkgPathToName(pkgPath)
|
|
|
|
err := validateNinjaName(pkgName)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
i := strings.LastIndex(pkgPath, "/")
|
|
|
|
shortName := pkgPath[i+1:]
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
p := &packageContext{
|
2014-10-03 11:49:58 +02:00
|
|
|
fullName: pkgName,
|
|
|
|
shortName: shortName,
|
|
|
|
pkgPath: pkgPath,
|
|
|
|
scope: newScope(nil),
|
|
|
|
}
|
|
|
|
|
|
|
|
packageContexts[pkgPath] = p
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2016-08-30 00:07:16 +02:00
|
|
|
var Phony Rule = NewBuiltinRule("phony")
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2016-08-30 00:07:16 +02:00
|
|
|
var Console Pool = NewBuiltinPool("console")
|
2015-04-15 01:26:58 +02:00
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
var errRuleIsBuiltin = errors.New("the rule is a built-in")
|
2015-04-15 01:26:58 +02:00
|
|
|
var errPoolIsBuiltin = errors.New("the pool is a built-in")
|
2014-06-06 04:50:44 +02:00
|
|
|
var errVariableIsArg = errors.New("argument variables have no value")
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
// checkCalledFromInit panics if a Go package's init function is not on the
|
|
|
|
// call stack.
|
|
|
|
func checkCalledFromInit() {
|
|
|
|
for skip := 3; ; skip++ {
|
|
|
|
_, funcName, ok := callerName(skip)
|
|
|
|
if !ok {
|
|
|
|
panic("not called from an init func")
|
|
|
|
}
|
|
|
|
|
2019-02-28 23:38:42 +01:00
|
|
|
if funcName == "init" || strings.HasPrefix(funcName, "init·") ||
|
|
|
|
funcName == "init.ializers" || strings.HasPrefix(funcName, "init.") {
|
2014-10-03 11:49:58 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2019-02-28 23:38:42 +01:00
|
|
|
// A regex to find a package path within a function name. It finds the shortest string that is
|
|
|
|
// followed by '.' and doesn't have any '/'s left.
|
|
|
|
var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`)
|
|
|
|
|
2014-07-01 03:07:17 +02:00
|
|
|
// callerName returns the package path and function name of the calling
|
|
|
|
// function. The skip argument has the same meaning as the skip argument of
|
|
|
|
// runtime.Callers.
|
2014-10-03 11:49:58 +02:00
|
|
|
func callerName(skip int) (pkgPath, funcName string, ok bool) {
|
2014-07-01 03:07:17 +02:00
|
|
|
var pc [1]uintptr
|
|
|
|
n := runtime.Callers(skip+1, pc[:])
|
|
|
|
if n != 1 {
|
2014-10-03 11:49:58 +02:00
|
|
|
return "", "", false
|
2014-07-01 03:07:17 +02:00
|
|
|
}
|
2019-09-05 08:48:27 +02:00
|
|
|
frames := runtime.CallersFrames(pc[:])
|
|
|
|
frame, _ := frames.Next()
|
|
|
|
f := frame.Function
|
2019-02-28 23:38:42 +01:00
|
|
|
s := pkgPathRe.FindStringSubmatch(f)
|
|
|
|
if len(s) < 3 {
|
|
|
|
panic(fmt.Errorf("failed to extract package path and function name from %q", f))
|
2014-07-01 03:07:17 +02:00
|
|
|
}
|
|
|
|
|
2019-02-28 23:38:42 +01:00
|
|
|
return s[1], s[2], true
|
2014-07-01 03:07:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
// pkgPathToName makes a Ninja-friendly name out of a Go package name by
|
|
|
|
// replaceing all the '/' characters with '.'. We assume the results are
|
|
|
|
// unique, though this is not 100% guaranteed for Go package names that
|
|
|
|
// already contain '.' characters. Disallowing package names with '.' isn't
|
|
|
|
// reasonable since many package names contain the name of the hosting site
|
|
|
|
// (e.g. "code.google.com"). In practice this probably isn't really a
|
|
|
|
// problem.
|
|
|
|
func pkgPathToName(pkgPath string) string {
|
|
|
|
return strings.Replace(pkgPath, "/", ".", -1)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
// Import enables access to the exported Ninja pools, rules, and variables
|
|
|
|
// that are defined at the package scope of another Go package. Go's
|
|
|
|
// visibility rules apply to these references - capitalized names indicate
|
|
|
|
// that something is exported. It may only be called from a Go package's
|
|
|
|
// init() function. The Go package path passed to Import must have already
|
|
|
|
// been imported into the Go package using a Go import statement. The
|
|
|
|
// imported variables may then be accessed from Ninja strings as
|
|
|
|
// "${pkg.Variable}", while the imported rules can simply be accessed as
|
|
|
|
// exported Go variables from the package. For example:
|
2014-06-06 04:50:44 +02:00
|
|
|
//
|
|
|
|
// import (
|
|
|
|
// "blueprint"
|
|
|
|
// "foo/bar"
|
|
|
|
// )
|
|
|
|
//
|
2014-10-03 11:49:58 +02:00
|
|
|
// var pctx = NewPackagePath("blah")
|
|
|
|
//
|
2014-06-06 04:50:44 +02:00
|
|
|
// func init() {
|
2014-10-03 11:49:58 +02:00
|
|
|
// pctx.Import("foo/bar")
|
2014-06-06 04:50:44 +02:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// func (m *MyModule) GenerateBuildActions(ctx blueprint.Module) {
|
2014-10-03 11:49:58 +02:00
|
|
|
// ctx.Build(pctx, blueprint.BuildParams{
|
2014-06-06 04:50:44 +02:00
|
|
|
// Rule: bar.SomeRule,
|
|
|
|
// Outputs: []string{"${bar.SomeVariable}"},
|
|
|
|
// })
|
|
|
|
// }
|
2014-06-13 05:06:50 +02:00
|
|
|
//
|
|
|
|
// Note that the local name used to refer to the package in Ninja variable names
|
|
|
|
// is derived from pkgPath by extracting the last path component. This differs
|
|
|
|
// from Go's import declaration, which derives the local name from the package
|
|
|
|
// clause in the imported package. By convention these names are made to match,
|
|
|
|
// but this is not required.
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *packageContext) Import(pkgPath string) {
|
2014-10-03 11:49:58 +02:00
|
|
|
checkCalledFromInit()
|
|
|
|
importPkg, ok := packageContexts[pkgPath]
|
2014-05-28 01:34:41 +02:00
|
|
|
if !ok {
|
2014-10-03 11:49:58 +02:00
|
|
|
panic(fmt.Errorf("package %q has no context", pkgPath))
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
err := p.scope.AddImport(importPkg.shortName, importPkg.scope)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 05:06:50 +02:00
|
|
|
// ImportAs provides the same functionality as Import, but it allows the local
|
|
|
|
// name that will be used to refer to the package to be specified explicitly.
|
|
|
|
// It may only be called from a Go package's init() function.
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *packageContext) ImportAs(as, pkgPath string) {
|
2014-10-03 11:49:58 +02:00
|
|
|
checkCalledFromInit()
|
|
|
|
importPkg, ok := packageContexts[pkgPath]
|
2014-05-28 01:34:41 +02:00
|
|
|
if !ok {
|
2014-10-03 11:49:58 +02:00
|
|
|
panic(fmt.Errorf("package %q has no context", pkgPath))
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := validateNinjaName(as)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
err = p.scope.AddImport(as, importPkg.scope)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type staticVariable struct {
|
2021-01-22 01:49:25 +01:00
|
|
|
pctx *packageContext
|
|
|
|
name_ string
|
|
|
|
value_ string
|
|
|
|
fullName_ string
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 05:06:50 +02:00
|
|
|
// StaticVariable returns a Variable whose value does not depend on any
|
|
|
|
// configuration information. It may only be called during a Go package's
|
|
|
|
// initialization - either from the init() function or as part of a package-
|
|
|
|
// scoped variable's initialization.
|
|
|
|
//
|
|
|
|
// This function is usually used to initialize a package-scoped Go variable that
|
|
|
|
// represents a Ninja variable that will be output. The name argument should
|
|
|
|
// exactly match the Go variable name, and the value string may reference other
|
|
|
|
// Ninja variables that are visible within the calling Go package.
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *packageContext) StaticVariable(name, value string) Variable {
|
2014-10-03 11:49:58 +02:00
|
|
|
checkCalledFromInit()
|
2014-05-28 01:34:41 +02:00
|
|
|
err := validateNinjaName(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
v := &staticVariable{
|
|
|
|
pctx: p,
|
|
|
|
name_: name,
|
|
|
|
value_: value,
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
err = p.scope.AddVariable(v)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (v *staticVariable) packageContext() *packageContext {
|
2014-10-03 11:49:58 +02:00
|
|
|
return v.pctx
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *staticVariable) name() string {
|
|
|
|
return v.name_
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (v *staticVariable) fullName(pkgNames map[*packageContext]string) string {
|
2021-01-22 01:49:25 +01:00
|
|
|
if v.fullName_ != "" {
|
|
|
|
return v.fullName_
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[v.pctx]) + v.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
func (v *staticVariable) memoizeFullName(pkgNames map[*packageContext]string) {
|
|
|
|
v.fullName_ = v.fullName(pkgNames)
|
|
|
|
}
|
|
|
|
|
2020-01-29 21:58:03 +01:00
|
|
|
func (v *staticVariable) value(interface{}) (ninjaString, error) {
|
2014-10-03 11:49:58 +02:00
|
|
|
ninjaStr, err := parseNinjaString(v.pctx.scope, v.value_)
|
2014-06-14 03:19:16 +02:00
|
|
|
if err != nil {
|
2014-06-14 03:25:54 +02:00
|
|
|
err = fmt.Errorf("error parsing variable %s value: %s", v, err)
|
2014-06-14 03:19:16 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ninjaStr, nil
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
func (v *staticVariable) String() string {
|
2014-10-03 11:49:58 +02:00
|
|
|
return v.pctx.pkgPath + "." + v.name_
|
2014-06-14 03:25:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
type variableFunc struct {
|
2021-01-22 01:49:25 +01:00
|
|
|
pctx *packageContext
|
|
|
|
name_ string
|
|
|
|
value_ func(interface{}) (string, error)
|
|
|
|
fullName_ string
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// VariableFunc returns a Variable whose value is determined by a function that
|
2014-06-13 05:06:50 +02:00
|
|
|
// takes a config object as input and returns either the variable value or an
|
|
|
|
// error. It may only be called during a Go package's initialization - either
|
|
|
|
// from the init() function or as part of a package-scoped variable's
|
|
|
|
// initialization.
|
|
|
|
//
|
|
|
|
// This function is usually used to initialize a package-scoped Go variable that
|
|
|
|
// represents a Ninja variable that will be output. The name argument should
|
|
|
|
// exactly match the Go variable name, and the value string returned by f may
|
|
|
|
// reference other Ninja variables that are visible within the calling Go
|
|
|
|
// package.
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *packageContext) VariableFunc(name string,
|
2014-10-03 11:49:58 +02:00
|
|
|
f func(config interface{}) (string, error)) Variable {
|
|
|
|
|
|
|
|
checkCalledFromInit()
|
2014-06-13 05:06:50 +02:00
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
err := validateNinjaName(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
v := &variableFunc{
|
|
|
|
pctx: p,
|
|
|
|
name_: name,
|
|
|
|
value_: f,
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
err = p.scope.AddVariable(v)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2014-06-06 04:50:44 +02:00
|
|
|
// VariableConfigMethod returns a Variable whose value is determined by calling
|
2014-06-13 05:06:50 +02:00
|
|
|
// a method on the config object. The method must take no arguments and return
|
|
|
|
// a single string that will be the variable's value. It may only be called
|
|
|
|
// during a Go package's initialization - either from the init() function or as
|
|
|
|
// part of a package-scoped variable's initialization.
|
|
|
|
//
|
|
|
|
// This function is usually used to initialize a package-scoped Go variable that
|
|
|
|
// represents a Ninja variable that will be output. The name argument should
|
|
|
|
// exactly match the Go variable name, and the value string returned by method
|
|
|
|
// may reference other Ninja variables that are visible within the calling Go
|
|
|
|
// package.
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *packageContext) VariableConfigMethod(name string,
|
2014-10-03 11:49:58 +02:00
|
|
|
method interface{}) Variable {
|
|
|
|
|
|
|
|
checkCalledFromInit()
|
|
|
|
|
2014-06-06 04:50:44 +02:00
|
|
|
err := validateNinjaName(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
methodValue := reflect.ValueOf(method)
|
|
|
|
validateVariableMethod(name, methodValue)
|
|
|
|
|
2014-06-12 03:31:16 +02:00
|
|
|
fun := func(config interface{}) (string, error) {
|
2014-06-06 04:50:44 +02:00
|
|
|
result := methodValue.Call([]reflect.Value{reflect.ValueOf(config)})
|
|
|
|
resultStr := result[0].Interface().(string)
|
|
|
|
return resultStr, nil
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
v := &variableFunc{
|
|
|
|
pctx: p,
|
|
|
|
name_: name,
|
|
|
|
value_: fun,
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
err = p.scope.AddVariable(v)
|
2014-06-06 04:50:44 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (v *variableFunc) packageContext() *packageContext {
|
2014-10-03 11:49:58 +02:00
|
|
|
return v.pctx
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *variableFunc) name() string {
|
|
|
|
return v.name_
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (v *variableFunc) fullName(pkgNames map[*packageContext]string) string {
|
2021-01-22 01:49:25 +01:00
|
|
|
if v.fullName_ != "" {
|
|
|
|
return v.fullName_
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[v.pctx]) + v.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
func (v *variableFunc) memoizeFullName(pkgNames map[*packageContext]string) {
|
|
|
|
v.fullName_ = v.fullName(pkgNames)
|
|
|
|
}
|
|
|
|
|
2020-01-29 21:58:03 +01:00
|
|
|
func (v *variableFunc) value(config interface{}) (ninjaString, error) {
|
2014-05-28 01:34:41 +02:00
|
|
|
value, err := v.value_(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-14 03:19:16 +02:00
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
ninjaStr, err := parseNinjaString(v.pctx.scope, value)
|
2014-06-14 03:19:16 +02:00
|
|
|
if err != nil {
|
2014-06-14 03:25:54 +02:00
|
|
|
err = fmt.Errorf("error parsing variable %s value: %s", v, err)
|
2014-06-14 03:19:16 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ninjaStr, nil
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
func (v *variableFunc) String() string {
|
2014-10-03 11:49:58 +02:00
|
|
|
return v.pctx.pkgPath + "." + v.name_
|
2014-06-14 03:25:54 +02:00
|
|
|
}
|
|
|
|
|
2014-06-06 04:50:44 +02:00
|
|
|
func validateVariableMethod(name string, methodValue reflect.Value) {
|
|
|
|
methodType := methodValue.Type()
|
|
|
|
if methodType.Kind() != reflect.Func {
|
|
|
|
panic(fmt.Errorf("method given for variable %s is not a function",
|
|
|
|
name))
|
|
|
|
}
|
|
|
|
if n := methodType.NumIn(); n != 1 {
|
|
|
|
panic(fmt.Errorf("method for variable %s has %d inputs (should be 1)",
|
|
|
|
name, n))
|
|
|
|
}
|
|
|
|
if n := methodType.NumOut(); n != 1 {
|
|
|
|
panic(fmt.Errorf("method for variable %s has %d outputs (should be 1)",
|
|
|
|
name, n))
|
|
|
|
}
|
|
|
|
if kind := methodType.Out(0).Kind(); kind != reflect.String {
|
|
|
|
panic(fmt.Errorf("method for variable %s does not return a string",
|
|
|
|
name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
// An argVariable is a Variable that exists only when it is set by a build
|
|
|
|
// statement to pass a value to the rule being invoked. It has no value, so it
|
|
|
|
// can never be used to create a Ninja assignment statement. It is inserted
|
|
|
|
// into the rule's scope, which is used for name lookups within the rule and
|
|
|
|
// when assigning argument values as part of a build statement.
|
|
|
|
type argVariable struct {
|
|
|
|
name_ string
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (v *argVariable) packageContext() *packageContext {
|
2014-05-28 01:34:41 +02:00
|
|
|
panic("this should not be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *argVariable) name() string {
|
|
|
|
return v.name_
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (v *argVariable) fullName(pkgNames map[*packageContext]string) string {
|
2014-05-28 01:34:41 +02:00
|
|
|
return v.name_
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
func (v *argVariable) memoizeFullName(pkgNames map[*packageContext]string) {
|
|
|
|
// Nothing to do, full name is known at initialization.
|
|
|
|
}
|
|
|
|
|
2020-01-29 21:58:03 +01:00
|
|
|
func (v *argVariable) value(config interface{}) (ninjaString, error) {
|
2014-05-28 01:34:41 +02:00
|
|
|
return nil, errVariableIsArg
|
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
func (v *argVariable) String() string {
|
|
|
|
return "<arg>:" + v.name_
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
type staticPool struct {
|
2021-01-22 01:49:25 +01:00
|
|
|
pctx *packageContext
|
|
|
|
name_ string
|
|
|
|
params PoolParams
|
|
|
|
fullName_ string
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 05:06:50 +02:00
|
|
|
// StaticPool returns a Pool whose value does not depend on any configuration
|
|
|
|
// information. It may only be called during a Go package's initialization -
|
|
|
|
// either from the init() function or as part of a package-scoped Go variable's
|
|
|
|
// initialization.
|
|
|
|
//
|
|
|
|
// This function is usually used to initialize a package-scoped Go variable that
|
|
|
|
// represents a Ninja pool that will be output. The name argument should
|
|
|
|
// exactly match the Go variable name, and the params fields may reference other
|
|
|
|
// Ninja variables that are visible within the calling Go package.
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *packageContext) StaticPool(name string, params PoolParams) Pool {
|
2014-10-03 11:49:58 +02:00
|
|
|
checkCalledFromInit()
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
err := validateNinjaName(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
pool := &staticPool{
|
|
|
|
pctx: p,
|
|
|
|
name_: name,
|
|
|
|
params: params,
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
err = p.scope.AddPool(pool)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
return pool
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *staticPool) packageContext() *packageContext {
|
2014-10-03 11:49:58 +02:00
|
|
|
return p.pctx
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *staticPool) name() string {
|
|
|
|
return p.name_
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *staticPool) fullName(pkgNames map[*packageContext]string) string {
|
2021-01-22 01:49:25 +01:00
|
|
|
if p.fullName_ != "" {
|
|
|
|
return p.fullName_
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[p.pctx]) + p.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
func (p *staticPool) memoizeFullName(pkgNames map[*packageContext]string) {
|
|
|
|
p.fullName_ = p.fullName(pkgNames)
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:31:16 +02:00
|
|
|
func (p *staticPool) def(config interface{}) (*poolDef, error) {
|
2014-10-03 11:49:58 +02:00
|
|
|
def, err := parsePoolParams(p.pctx.scope, &p.params)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
2014-06-14 03:25:54 +02:00
|
|
|
panic(fmt.Errorf("error parsing PoolParams for %s: %s", p, err))
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
return def, nil
|
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
func (p *staticPool) String() string {
|
2014-10-03 11:49:58 +02:00
|
|
|
return p.pctx.pkgPath + "." + p.name_
|
2014-06-14 03:25:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
type poolFunc struct {
|
2015-11-26 00:29:32 +01:00
|
|
|
pctx *packageContext
|
2014-05-28 01:34:41 +02:00
|
|
|
name_ string
|
2014-06-12 03:31:16 +02:00
|
|
|
paramsFunc func(interface{}) (PoolParams, error)
|
2021-01-22 01:49:25 +01:00
|
|
|
fullName_ string
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 05:06:50 +02:00
|
|
|
// PoolFunc returns a Pool whose value is determined by a function that takes a
|
|
|
|
// config object as input and returns either the pool parameters or an error. It
|
|
|
|
// may only be called during a Go package's initialization - either from the
|
|
|
|
// init() function or as part of a package-scoped variable's initialization.
|
|
|
|
//
|
|
|
|
// This function is usually used to initialize a package-scoped Go variable that
|
|
|
|
// represents a Ninja pool that will be output. The name argument should
|
|
|
|
// exactly match the Go variable name, and the string fields of the PoolParams
|
|
|
|
// returned by f may reference other Ninja variables that are visible within the
|
|
|
|
// calling Go package.
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *packageContext) PoolFunc(name string, f func(interface{}) (PoolParams,
|
2014-10-03 11:49:58 +02:00
|
|
|
error)) Pool {
|
|
|
|
|
|
|
|
checkCalledFromInit()
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
err := validateNinjaName(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
pool := &poolFunc{
|
|
|
|
pctx: p,
|
|
|
|
name_: name,
|
|
|
|
paramsFunc: f,
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
err = p.scope.AddPool(pool)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
return pool
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *poolFunc) packageContext() *packageContext {
|
2014-10-03 11:49:58 +02:00
|
|
|
return p.pctx
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *poolFunc) name() string {
|
|
|
|
return p.name_
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *poolFunc) fullName(pkgNames map[*packageContext]string) string {
|
2021-01-22 01:49:25 +01:00
|
|
|
if p.fullName_ != "" {
|
|
|
|
return p.fullName_
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[p.pctx]) + p.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
func (p *poolFunc) memoizeFullName(pkgNames map[*packageContext]string) {
|
|
|
|
p.fullName_ = p.fullName(pkgNames)
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:31:16 +02:00
|
|
|
func (p *poolFunc) def(config interface{}) (*poolDef, error) {
|
2014-05-28 01:34:41 +02:00
|
|
|
params, err := p.paramsFunc(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
def, err := parsePoolParams(p.pctx.scope, ¶ms)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
2014-06-14 03:25:54 +02:00
|
|
|
panic(fmt.Errorf("error parsing PoolParams for %s: %s", p, err))
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
return def, nil
|
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
func (p *poolFunc) String() string {
|
2014-10-03 11:49:58 +02:00
|
|
|
return p.pctx.pkgPath + "." + p.name_
|
2014-06-14 03:25:54 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 01:26:58 +02:00
|
|
|
type builtinPool struct {
|
2015-04-21 01:41:55 +02:00
|
|
|
name_ string
|
2015-04-15 01:26:58 +02:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *builtinPool) packageContext() *packageContext {
|
2015-04-15 01:26:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *builtinPool) name() string {
|
|
|
|
return p.name_
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *builtinPool) fullName(pkgNames map[*packageContext]string) string {
|
2015-04-15 01:26:58 +02:00
|
|
|
return p.name_
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
func (p *builtinPool) memoizeFullName(pkgNames map[*packageContext]string) {
|
|
|
|
// Nothing to do, full name is known at initialization.
|
|
|
|
}
|
|
|
|
|
2015-04-15 01:26:58 +02:00
|
|
|
func (p *builtinPool) def(config interface{}) (*poolDef, error) {
|
|
|
|
return nil, errPoolIsBuiltin
|
|
|
|
}
|
|
|
|
|
2016-08-30 00:07:16 +02:00
|
|
|
// NewBuiltinPool returns a Pool object that refers to a pool name created outside of Blueprint
|
|
|
|
func NewBuiltinPool(name string) Pool {
|
|
|
|
return &builtinPool{
|
|
|
|
name_: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 01:26:58 +02:00
|
|
|
func (p *builtinPool) String() string {
|
|
|
|
return "<builtin>:" + p.name_
|
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
type staticRule struct {
|
2015-11-26 00:29:32 +01:00
|
|
|
pctx *packageContext
|
2015-01-08 03:08:56 +01:00
|
|
|
name_ string
|
|
|
|
params RuleParams
|
|
|
|
argNames map[string]bool
|
|
|
|
scope_ *basicScope
|
2021-01-22 01:49:25 +01:00
|
|
|
fullName_ string
|
2015-01-08 03:08:56 +01:00
|
|
|
sync.Mutex // protects scope_ during lazy creation
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 05:06:50 +02:00
|
|
|
// StaticRule returns a Rule whose value does not depend on any configuration
|
|
|
|
// information. It may only be called during a Go package's initialization -
|
|
|
|
// either from the init() function or as part of a package-scoped Go variable's
|
|
|
|
// initialization.
|
|
|
|
//
|
|
|
|
// This function is usually used to initialize a package-scoped Go variable that
|
|
|
|
// represents a Ninja rule that will be output. The name argument should
|
|
|
|
// exactly match the Go variable name, and the params fields may reference other
|
|
|
|
// Ninja variables that are visible within the calling Go package.
|
|
|
|
//
|
|
|
|
// The argNames arguments list Ninja variables that may be overridden by Ninja
|
|
|
|
// build statements that invoke the rule. These arguments may be referenced in
|
|
|
|
// any of the string fields of params. Arguments can shadow package-scoped
|
|
|
|
// variables defined within the caller's Go package, but they may not shadow
|
|
|
|
// those defined in another package. Shadowing a package-scoped variable
|
|
|
|
// results in the package-scoped variable's value being used for build
|
|
|
|
// statements that do not override the argument. For argument names that do not
|
|
|
|
// shadow package-scoped variables the default value is an empty string.
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *packageContext) StaticRule(name string, params RuleParams,
|
2014-10-03 11:49:58 +02:00
|
|
|
argNames ...string) Rule {
|
|
|
|
|
|
|
|
checkCalledFromInit()
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
err := validateNinjaName(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateArgNames(argNames)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("invalid argument name: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
argNamesSet := make(map[string]bool)
|
|
|
|
for _, argName := range argNames {
|
|
|
|
argNamesSet[argName] = true
|
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
ruleScope := (*basicScope)(nil) // This will get created lazily
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2015-01-08 03:08:56 +01:00
|
|
|
r := &staticRule{
|
|
|
|
pctx: p,
|
|
|
|
name_: name,
|
|
|
|
params: params,
|
|
|
|
argNames: argNamesSet,
|
|
|
|
scope_: ruleScope,
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
err = p.scope.AddRule(r)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (r *staticRule) packageContext() *packageContext {
|
2014-10-03 11:49:58 +02:00
|
|
|
return r.pctx
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *staticRule) name() string {
|
|
|
|
return r.name_
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (r *staticRule) fullName(pkgNames map[*packageContext]string) string {
|
2021-01-22 01:49:25 +01:00
|
|
|
if r.fullName_ != "" {
|
|
|
|
return r.fullName_
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[r.pctx]) + r.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
func (r *staticRule) memoizeFullName(pkgNames map[*packageContext]string) {
|
|
|
|
r.fullName_ = r.fullName(pkgNames)
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:31:16 +02:00
|
|
|
func (r *staticRule) def(interface{}) (*ruleDef, error) {
|
2014-05-28 01:34:41 +02:00
|
|
|
def, err := parseRuleParams(r.scope(), &r.params)
|
|
|
|
if err != nil {
|
2014-06-14 03:25:54 +02:00
|
|
|
panic(fmt.Errorf("error parsing RuleParams for %s: %s", r, err))
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
return def, nil
|
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
func (r *staticRule) scope() *basicScope {
|
|
|
|
// We lazily create the scope so that all the package-scoped variables get
|
|
|
|
// declared before the args are created. Otherwise we could incorrectly
|
|
|
|
// shadow a package-scoped variable with an arg variable.
|
2015-01-08 03:08:56 +01:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
if r.scope_ == nil {
|
2014-10-03 11:49:58 +02:00
|
|
|
r.scope_ = makeRuleScope(r.pctx.scope, r.argNames)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
return r.scope_
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *staticRule) isArg(argName string) bool {
|
|
|
|
return r.argNames[argName]
|
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
func (r *staticRule) String() string {
|
2014-10-03 11:49:58 +02:00
|
|
|
return r.pctx.pkgPath + "." + r.name_
|
2014-06-14 03:25:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
type ruleFunc struct {
|
2015-11-26 00:29:32 +01:00
|
|
|
pctx *packageContext
|
2014-05-28 01:34:41 +02:00
|
|
|
name_ string
|
2014-06-12 03:31:16 +02:00
|
|
|
paramsFunc func(interface{}) (RuleParams, error)
|
2014-05-28 01:34:41 +02:00
|
|
|
argNames map[string]bool
|
2014-06-14 03:25:54 +02:00
|
|
|
scope_ *basicScope
|
2021-01-22 01:49:25 +01:00
|
|
|
fullName_ string
|
2015-01-08 03:08:56 +01:00
|
|
|
sync.Mutex // protects scope_ during lazy creation
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 05:06:50 +02:00
|
|
|
// RuleFunc returns a Rule whose value is determined by a function that takes a
|
|
|
|
// config object as input and returns either the rule parameters or an error. It
|
|
|
|
// may only be called during a Go package's initialization - either from the
|
|
|
|
// init() function or as part of a package-scoped variable's initialization.
|
|
|
|
//
|
|
|
|
// This function is usually used to initialize a package-scoped Go variable that
|
|
|
|
// represents a Ninja rule that will be output. The name argument should
|
|
|
|
// exactly match the Go variable name, and the string fields of the RuleParams
|
|
|
|
// returned by f may reference other Ninja variables that are visible within the
|
|
|
|
// calling Go package.
|
|
|
|
//
|
|
|
|
// The argNames arguments list Ninja variables that may be overridden by Ninja
|
|
|
|
// build statements that invoke the rule. These arguments may be referenced in
|
|
|
|
// any of the string fields of the RuleParams returned by f. Arguments can
|
|
|
|
// shadow package-scoped variables defined within the caller's Go package, but
|
|
|
|
// they may not shadow those defined in another package. Shadowing a package-
|
|
|
|
// scoped variable results in the package-scoped variable's value being used for
|
|
|
|
// build statements that do not override the argument. For argument names that
|
|
|
|
// do not shadow package-scoped variables the default value is an empty string.
|
2015-11-26 00:29:32 +01:00
|
|
|
func (p *packageContext) RuleFunc(name string, f func(interface{}) (RuleParams,
|
2014-10-03 11:49:58 +02:00
|
|
|
error), argNames ...string) Rule {
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
checkCalledFromInit()
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
err := validateNinjaName(name)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateArgNames(argNames)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("invalid argument name: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
argNamesSet := make(map[string]bool)
|
|
|
|
for _, argName := range argNames {
|
|
|
|
argNamesSet[argName] = true
|
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
ruleScope := (*basicScope)(nil) // This will get created lazily
|
2014-05-28 01:34:41 +02:00
|
|
|
|
2015-01-08 03:08:56 +01:00
|
|
|
rule := &ruleFunc{
|
|
|
|
pctx: p,
|
|
|
|
name_: name,
|
|
|
|
paramsFunc: f,
|
|
|
|
argNames: argNamesSet,
|
|
|
|
scope_: ruleScope,
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
err = p.scope.AddRule(rule)
|
2014-05-28 01:34:41 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-10-03 11:49:58 +02:00
|
|
|
return rule
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (r *ruleFunc) packageContext() *packageContext {
|
2014-10-03 11:49:58 +02:00
|
|
|
return r.pctx
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ruleFunc) name() string {
|
|
|
|
return r.name_
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (r *ruleFunc) fullName(pkgNames map[*packageContext]string) string {
|
2021-01-22 01:49:25 +01:00
|
|
|
if r.fullName_ != "" {
|
|
|
|
return r.fullName_
|
|
|
|
}
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[r.pctx]) + r.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
func (r *ruleFunc) memoizeFullName(pkgNames map[*packageContext]string) {
|
|
|
|
r.fullName_ = r.fullName(pkgNames)
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:31:16 +02:00
|
|
|
func (r *ruleFunc) def(config interface{}) (*ruleDef, error) {
|
2014-05-28 01:34:41 +02:00
|
|
|
params, err := r.paramsFunc(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
def, err := parseRuleParams(r.scope(), ¶ms)
|
|
|
|
if err != nil {
|
2014-06-14 03:25:54 +02:00
|
|
|
panic(fmt.Errorf("error parsing RuleParams for %s: %s", r, err))
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
return def, nil
|
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
func (r *ruleFunc) scope() *basicScope {
|
2014-05-28 01:34:41 +02:00
|
|
|
// We lazily create the scope so that all the global variables get declared
|
|
|
|
// before the args are created. Otherwise we could incorrectly shadow a
|
|
|
|
// global variable with an arg variable.
|
2015-01-08 03:08:56 +01:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
if r.scope_ == nil {
|
2014-10-03 11:49:58 +02:00
|
|
|
r.scope_ = makeRuleScope(r.pctx.scope, r.argNames)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
return r.scope_
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ruleFunc) isArg(argName string) bool {
|
|
|
|
return r.argNames[argName]
|
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
func (r *ruleFunc) String() string {
|
2014-10-03 11:49:58 +02:00
|
|
|
return r.pctx.pkgPath + "." + r.name_
|
2014-06-14 03:25:54 +02:00
|
|
|
}
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
type builtinRule struct {
|
2015-01-08 03:08:56 +01:00
|
|
|
name_ string
|
|
|
|
scope_ *basicScope
|
|
|
|
sync.Mutex // protects scope_ during lazy creation
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (r *builtinRule) packageContext() *packageContext {
|
2014-05-28 01:34:41 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *builtinRule) name() string {
|
|
|
|
return r.name_
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:29:32 +01:00
|
|
|
func (r *builtinRule) fullName(pkgNames map[*packageContext]string) string {
|
2014-05-28 01:34:41 +02:00
|
|
|
return r.name_
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:49:25 +01:00
|
|
|
func (r *builtinRule) memoizeFullName(pkgNames map[*packageContext]string) {
|
|
|
|
// Nothing to do, full name is known at initialization.
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:31:16 +02:00
|
|
|
func (r *builtinRule) def(config interface{}) (*ruleDef, error) {
|
2014-05-28 01:34:41 +02:00
|
|
|
return nil, errRuleIsBuiltin
|
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
func (r *builtinRule) scope() *basicScope {
|
2015-01-08 03:08:56 +01:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2014-05-28 01:34:41 +02:00
|
|
|
if r.scope_ == nil {
|
|
|
|
r.scope_ = makeRuleScope(nil, nil)
|
|
|
|
}
|
|
|
|
return r.scope_
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *builtinRule) isArg(argName string) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-06-14 03:25:54 +02:00
|
|
|
func (r *builtinRule) String() string {
|
|
|
|
return "<builtin>:" + r.name_
|
|
|
|
}
|
2015-12-19 00:18:03 +01:00
|
|
|
|
2016-08-30 00:07:16 +02:00
|
|
|
// NewBuiltinRule returns a Rule object that refers to a rule that was created outside of Blueprint
|
|
|
|
func NewBuiltinRule(name string) Rule {
|
|
|
|
return &builtinRule{
|
|
|
|
name_: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-19 00:18:03 +01:00
|
|
|
func (p *packageContext) AddNinjaFileDeps(deps ...string) {
|
|
|
|
p.ninjaFileDeps = append(p.ninjaFileDeps, deps...)
|
|
|
|
}
|