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:
|
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// package blah
|
2014-10-03 11:49:58 +02:00
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// import (
|
|
|
|
// "blueprint"
|
|
|
|
// )
|
2014-10-03 11:49:58 +02:00
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// var (
|
|
|
|
// pctx = NewPackageContext("path/to/blah")
|
2014-10-03 11:49:58 +02:00
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// myPrivateVar = pctx.StaticVariable("myPrivateVar", "abcdef")
|
|
|
|
// MyExportedVar = pctx.StaticVariable("MyExportedVar", "$myPrivateVar 123456!")
|
2014-10-03 11:49:58 +02:00
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// SomeRule = pctx.StaticRule(...)
|
|
|
|
// )
|
2014-10-03 11:49:58 +02:00
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// // ...
|
2014-10-03 11:49:58 +02:00
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// 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
|
2022-11-04 04:19:48 +01:00
|
|
|
VariableFunc(name string, f func(ctx VariableFuncContext, config interface{}) (string, error)) Variable
|
2015-11-26 00:29:32 +01:00
|
|
|
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
|
|
|
|
2022-01-11 04:46:23 +01:00
|
|
|
var _ PackageContext = (*packageContext)(nil)
|
2015-11-26 00:29:32 +01:00
|
|
|
|
|
|
|
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
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// import (
|
|
|
|
// "blueprint"
|
|
|
|
// "foo/bar"
|
|
|
|
// )
|
2014-06-06 04:50:44 +02:00
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// var pctx = NewPackagePath("blah")
|
2014-10-03 11:49:58 +02:00
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// func init() {
|
|
|
|
// pctx.Import("foo/bar")
|
|
|
|
// }
|
2014-06-06 04:50:44 +02:00
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// ...
|
2014-06-06 04:50:44 +02:00
|
|
|
//
|
2023-02-21 20:40:56 +01:00
|
|
|
// func (m *MyModule) GenerateBuildActions(ctx blueprint.Module) {
|
|
|
|
// ctx.Build(pctx, blueprint.BuildParams{
|
|
|
|
// 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 {
|
2024-01-18 21:00:20 +01:00
|
|
|
pctx *packageContext
|
|
|
|
name_ string
|
|
|
|
value_ 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 {
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[v.pctx]) + v.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
Optimize memory usage of ninjaString
ninjaString is an interface, which uses 16 bytes of memory on top
of the size of the concrete type. A literalNinjaString is a string,
which is another 16 bytes for the string header for a total of 32
bytes. A varNinjaString is two slices, which are 24 bytes each
for the slice headers, for a total of 64 bytes. The slices contain
the first constant string, and then altenrating variable and string
parts of the ninjaString, resulting in 16 bytes plus 32 bytes per
variable.
This patch replaces the ninjaString interface with a *ninjaString
concrete struct type. The ninjaString struct is a string and a
pointer to a slice of variable references, for a total of 24 bytes.
ninjaStrings with no variable references (the equivalent of the old
literalNinjaString) have a nil slice, and now use 24 bytes instead
of 32 bytes.
ninjaStrings with variable references allocate a slice of variable
references that contain 32-bit start and end offsets and a Variable
interface, but reuse the original string and so avoid the extra
string headers, resulting in 24 bytes for the slice header, and
24 bytes per variable.
These savings reduce the peak memory usage averaged across 10 runs of
/bin/time -v build/soong/soong_ui.bash --make-mode nothing
on the internal master branch cf_x86_64_phone-userdebug build
from 50114842kB to 45577638kB, a savings of 4537204kB or 9%.
The new Benchmark_parseNinjaString shows savings in both time and
memory. Before:
Benchmark_parseNinjaString/constant/1-128 594251787 2.006 ns/op 0 B/op 0 allocs/op
Benchmark_parseNinjaString/constant/10-128 21191347 65.57 ns/op 16 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/100-128 9983748 130.2 ns/op 112 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/1000-128 2632527 445.1 ns/op 1024 B/op 1 allocs/op
Benchmark_parseNinjaString/variable/1-128 2964896 419.4 ns/op 176 B/op 4 allocs/op
Benchmark_parseNinjaString/variable/10-128 1807341 670.6 ns/op 192 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/100-128 1000000 1092 ns/op 352 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/1000-128 300649 3773 ns/op 1584 B/op 7 allocs/op
Benchmark_parseNinjaString/variables/1-128 2858432 441.6 ns/op 176 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/2-128 2360505 513.4 ns/op 208 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/3-128 1867136 635.6 ns/op 240 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/4-128 1584045 752.1 ns/op 272 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/5-128 1338189 885.8 ns/op 304 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/10-128 1000000 1468 ns/op 464 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/100-128 88768 12895 ns/op 3712 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/1000-128 8972 133627 ns/op 32896 B/op 4 allocs/op
After:
Benchmark_parseNinjaString/constant/1-128 584600864 2.004 ns/op 0 B/op 0 allocs/op
Benchmark_parseNinjaString/constant/10-128 19274581 64.84 ns/op 16 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/100-128 9017640 127.6 ns/op 112 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/1000-128 2630797 453.0 ns/op 1024 B/op 1 allocs/op
Benchmark_parseNinjaString/variable/1-128 3460422 347.0 ns/op 136 B/op 4 allocs/op
Benchmark_parseNinjaString/variable/10-128 2103404 519.9 ns/op 152 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/100-128 1315778 906.5 ns/op 312 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/1000-128 354812 3284 ns/op 1544 B/op 7 allocs/op
Benchmark_parseNinjaString/variables/1-128 3386868 361.5 ns/op 136 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/2-128 2675594 456.9 ns/op 160 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/3-128 2344670 520.0 ns/op 192 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/4-128 1919482 648.1 ns/op 208 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/5-128 1560556 723.9 ns/op 240 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/10-128 1000000 1169 ns/op 352 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/100-128 116738 10168 ns/op 2800 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/1000-128 10000 105646 ns/op 24688 B/op 4 allocs/op
Bug: 286423944
Test: ninja_strings_test.go
Test: out/soong/build*.ninja is the same before and after this change
Change-Id: I1ecffbaccb0d0469a41fa31255c1b17311e01687
2023-06-15 01:14:10 +02:00
|
|
|
func (v *staticVariable) value(VariableFuncContext, 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 {
|
2024-01-18 21:00:20 +01:00
|
|
|
pctx *packageContext
|
|
|
|
name_ string
|
|
|
|
value_ func(VariableFuncContext, interface{}) (string, error)
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2022-11-04 04:19:48 +01:00
|
|
|
// VariableFuncContext is passed to VariableFunc functions.
|
|
|
|
type VariableFuncContext interface {
|
|
|
|
// 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.
|
|
|
|
GlobWithDeps(globPattern string, excludes []string) ([]string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type variableFuncContext struct {
|
|
|
|
context *Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *variableFuncContext) GlobWithDeps(pattern string,
|
|
|
|
excludes []string) ([]string, error) {
|
|
|
|
return v.context.glob(pattern, excludes)
|
|
|
|
}
|
|
|
|
|
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,
|
2022-11-04 04:19:48 +01:00
|
|
|
f func(ctx VariableFuncContext, config interface{}) (string, error)) Variable {
|
2014-10-03 11:49:58 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-11-04 04:19:48 +01:00
|
|
|
fun := func(ctx VariableFuncContext, 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 {
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[v.pctx]) + v.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
Optimize memory usage of ninjaString
ninjaString is an interface, which uses 16 bytes of memory on top
of the size of the concrete type. A literalNinjaString is a string,
which is another 16 bytes for the string header for a total of 32
bytes. A varNinjaString is two slices, which are 24 bytes each
for the slice headers, for a total of 64 bytes. The slices contain
the first constant string, and then altenrating variable and string
parts of the ninjaString, resulting in 16 bytes plus 32 bytes per
variable.
This patch replaces the ninjaString interface with a *ninjaString
concrete struct type. The ninjaString struct is a string and a
pointer to a slice of variable references, for a total of 24 bytes.
ninjaStrings with no variable references (the equivalent of the old
literalNinjaString) have a nil slice, and now use 24 bytes instead
of 32 bytes.
ninjaStrings with variable references allocate a slice of variable
references that contain 32-bit start and end offsets and a Variable
interface, but reuse the original string and so avoid the extra
string headers, resulting in 24 bytes for the slice header, and
24 bytes per variable.
These savings reduce the peak memory usage averaged across 10 runs of
/bin/time -v build/soong/soong_ui.bash --make-mode nothing
on the internal master branch cf_x86_64_phone-userdebug build
from 50114842kB to 45577638kB, a savings of 4537204kB or 9%.
The new Benchmark_parseNinjaString shows savings in both time and
memory. Before:
Benchmark_parseNinjaString/constant/1-128 594251787 2.006 ns/op 0 B/op 0 allocs/op
Benchmark_parseNinjaString/constant/10-128 21191347 65.57 ns/op 16 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/100-128 9983748 130.2 ns/op 112 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/1000-128 2632527 445.1 ns/op 1024 B/op 1 allocs/op
Benchmark_parseNinjaString/variable/1-128 2964896 419.4 ns/op 176 B/op 4 allocs/op
Benchmark_parseNinjaString/variable/10-128 1807341 670.6 ns/op 192 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/100-128 1000000 1092 ns/op 352 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/1000-128 300649 3773 ns/op 1584 B/op 7 allocs/op
Benchmark_parseNinjaString/variables/1-128 2858432 441.6 ns/op 176 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/2-128 2360505 513.4 ns/op 208 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/3-128 1867136 635.6 ns/op 240 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/4-128 1584045 752.1 ns/op 272 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/5-128 1338189 885.8 ns/op 304 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/10-128 1000000 1468 ns/op 464 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/100-128 88768 12895 ns/op 3712 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/1000-128 8972 133627 ns/op 32896 B/op 4 allocs/op
After:
Benchmark_parseNinjaString/constant/1-128 584600864 2.004 ns/op 0 B/op 0 allocs/op
Benchmark_parseNinjaString/constant/10-128 19274581 64.84 ns/op 16 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/100-128 9017640 127.6 ns/op 112 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/1000-128 2630797 453.0 ns/op 1024 B/op 1 allocs/op
Benchmark_parseNinjaString/variable/1-128 3460422 347.0 ns/op 136 B/op 4 allocs/op
Benchmark_parseNinjaString/variable/10-128 2103404 519.9 ns/op 152 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/100-128 1315778 906.5 ns/op 312 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/1000-128 354812 3284 ns/op 1544 B/op 7 allocs/op
Benchmark_parseNinjaString/variables/1-128 3386868 361.5 ns/op 136 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/2-128 2675594 456.9 ns/op 160 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/3-128 2344670 520.0 ns/op 192 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/4-128 1919482 648.1 ns/op 208 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/5-128 1560556 723.9 ns/op 240 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/10-128 1000000 1169 ns/op 352 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/100-128 116738 10168 ns/op 2800 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/1000-128 10000 105646 ns/op 24688 B/op 4 allocs/op
Bug: 286423944
Test: ninja_strings_test.go
Test: out/soong/build*.ninja is the same before and after this change
Change-Id: I1ecffbaccb0d0469a41fa31255c1b17311e01687
2023-06-15 01:14:10 +02:00
|
|
|
func (v *variableFunc) value(ctx VariableFuncContext, config interface{}) (*ninjaString, error) {
|
2022-11-04 04:19:48 +01:00
|
|
|
value, err := v.value_(ctx, config)
|
2014-05-28 01:34:41 +02:00
|
|
|
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_
|
|
|
|
}
|
|
|
|
|
Optimize memory usage of ninjaString
ninjaString is an interface, which uses 16 bytes of memory on top
of the size of the concrete type. A literalNinjaString is a string,
which is another 16 bytes for the string header for a total of 32
bytes. A varNinjaString is two slices, which are 24 bytes each
for the slice headers, for a total of 64 bytes. The slices contain
the first constant string, and then altenrating variable and string
parts of the ninjaString, resulting in 16 bytes plus 32 bytes per
variable.
This patch replaces the ninjaString interface with a *ninjaString
concrete struct type. The ninjaString struct is a string and a
pointer to a slice of variable references, for a total of 24 bytes.
ninjaStrings with no variable references (the equivalent of the old
literalNinjaString) have a nil slice, and now use 24 bytes instead
of 32 bytes.
ninjaStrings with variable references allocate a slice of variable
references that contain 32-bit start and end offsets and a Variable
interface, but reuse the original string and so avoid the extra
string headers, resulting in 24 bytes for the slice header, and
24 bytes per variable.
These savings reduce the peak memory usage averaged across 10 runs of
/bin/time -v build/soong/soong_ui.bash --make-mode nothing
on the internal master branch cf_x86_64_phone-userdebug build
from 50114842kB to 45577638kB, a savings of 4537204kB or 9%.
The new Benchmark_parseNinjaString shows savings in both time and
memory. Before:
Benchmark_parseNinjaString/constant/1-128 594251787 2.006 ns/op 0 B/op 0 allocs/op
Benchmark_parseNinjaString/constant/10-128 21191347 65.57 ns/op 16 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/100-128 9983748 130.2 ns/op 112 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/1000-128 2632527 445.1 ns/op 1024 B/op 1 allocs/op
Benchmark_parseNinjaString/variable/1-128 2964896 419.4 ns/op 176 B/op 4 allocs/op
Benchmark_parseNinjaString/variable/10-128 1807341 670.6 ns/op 192 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/100-128 1000000 1092 ns/op 352 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/1000-128 300649 3773 ns/op 1584 B/op 7 allocs/op
Benchmark_parseNinjaString/variables/1-128 2858432 441.6 ns/op 176 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/2-128 2360505 513.4 ns/op 208 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/3-128 1867136 635.6 ns/op 240 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/4-128 1584045 752.1 ns/op 272 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/5-128 1338189 885.8 ns/op 304 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/10-128 1000000 1468 ns/op 464 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/100-128 88768 12895 ns/op 3712 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/1000-128 8972 133627 ns/op 32896 B/op 4 allocs/op
After:
Benchmark_parseNinjaString/constant/1-128 584600864 2.004 ns/op 0 B/op 0 allocs/op
Benchmark_parseNinjaString/constant/10-128 19274581 64.84 ns/op 16 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/100-128 9017640 127.6 ns/op 112 B/op 1 allocs/op
Benchmark_parseNinjaString/constant/1000-128 2630797 453.0 ns/op 1024 B/op 1 allocs/op
Benchmark_parseNinjaString/variable/1-128 3460422 347.0 ns/op 136 B/op 4 allocs/op
Benchmark_parseNinjaString/variable/10-128 2103404 519.9 ns/op 152 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/100-128 1315778 906.5 ns/op 312 B/op 7 allocs/op
Benchmark_parseNinjaString/variable/1000-128 354812 3284 ns/op 1544 B/op 7 allocs/op
Benchmark_parseNinjaString/variables/1-128 3386868 361.5 ns/op 136 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/2-128 2675594 456.9 ns/op 160 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/3-128 2344670 520.0 ns/op 192 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/4-128 1919482 648.1 ns/op 208 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/5-128 1560556 723.9 ns/op 240 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/10-128 1000000 1169 ns/op 352 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/100-128 116738 10168 ns/op 2800 B/op 4 allocs/op
Benchmark_parseNinjaString/variables/1000-128 10000 105646 ns/op 24688 B/op 4 allocs/op
Bug: 286423944
Test: ninja_strings_test.go
Test: out/soong/build*.ninja is the same before and after this change
Change-Id: I1ecffbaccb0d0469a41fa31255c1b17311e01687
2023-06-15 01:14:10 +02:00
|
|
|
func (v *argVariable) value(ctx VariableFuncContext, 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 {
|
2024-01-18 21:00:20 +01:00
|
|
|
pctx *packageContext
|
|
|
|
name_ string
|
|
|
|
params PoolParams
|
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 {
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[p.pctx]) + p.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
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)
|
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 {
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[p.pctx]) + p.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
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_
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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 {
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[r.pctx]) + r.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
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
|
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 {
|
2014-10-03 11:49:58 +02:00
|
|
|
return packageNamespacePrefix(pkgNames[r.pctx]) + r.name_
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
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_
|
|
|
|
}
|
|
|
|
|
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...)
|
|
|
|
}
|