2019-01-31 09:32:39 +08:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
|
|
|
package android
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
)
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build
|
|
|
|
// graph.
|
2019-01-31 09:32:39 +08:00
|
|
|
type RuleBuilder struct {
|
|
|
|
commands []*RuleBuilderCommand
|
|
|
|
installs []RuleBuilderInstall
|
|
|
|
restat bool
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// NewRuleBuilder returns a newly created RuleBuilder.
|
|
|
|
func NewRuleBuilder() *RuleBuilder {
|
|
|
|
return &RuleBuilder{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuleBuilderInstall is a tuple of install from and to locations.
|
|
|
|
type RuleBuilderInstall struct {
|
|
|
|
From, To string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restat marks the rule as a restat rule, which will be passed to ModuleContext.Rule in BuildParams.Restat.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (r *RuleBuilder) Restat() *RuleBuilder {
|
|
|
|
r.restat = true
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Install associates an output of the rule with an install location, which can be retrieved later using
|
|
|
|
// RuleBuilder.Installs.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (r *RuleBuilder) Install(from, to string) {
|
|
|
|
r.installs = append(r.installs, RuleBuilderInstall{from, to})
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Command returns a new RuleBuilderCommand for the rule. The commands will be ordered in the rule by when they were
|
|
|
|
// created by this method. That can be mutated through their methods in any order, as long as the mutations do not
|
|
|
|
// race with any call to Build.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (r *RuleBuilder) Command() *RuleBuilderCommand {
|
|
|
|
command := &RuleBuilderCommand{}
|
|
|
|
r.commands = append(r.commands, command)
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take input paths, such
|
|
|
|
// as RuleBuilderCommand.Input, RuleBuilderComand.Implicit, or RuleBuilderCommand.FlagWithInput. Inputs to a command
|
|
|
|
// that are also outputs of another command in the same RuleBuilder are filtered out.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (r *RuleBuilder) Inputs() []string {
|
|
|
|
outputs := r.outputSet()
|
|
|
|
|
|
|
|
inputs := make(map[string]bool)
|
|
|
|
for _, c := range r.commands {
|
|
|
|
for _, input := range c.inputs {
|
|
|
|
if !outputs[input] {
|
|
|
|
inputs[input] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var inputList []string
|
|
|
|
for input := range inputs {
|
|
|
|
inputList = append(inputList, input)
|
|
|
|
}
|
|
|
|
sort.Strings(inputList)
|
|
|
|
|
|
|
|
return inputList
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuleBuilder) outputSet() map[string]bool {
|
|
|
|
outputs := make(map[string]bool)
|
|
|
|
for _, c := range r.commands {
|
|
|
|
for _, output := range c.outputs {
|
|
|
|
outputs[output] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return outputs
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Outputs returns the list of paths that were passed to the RuleBuilderCommand methods that take output paths, such
|
|
|
|
// as RuleBuilderCommand.Output, RuleBuilderCommand.ImplicitOutput, or RuleBuilderCommand.FlagWithInput.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (r *RuleBuilder) Outputs() []string {
|
|
|
|
outputs := r.outputSet()
|
|
|
|
|
|
|
|
var outputList []string
|
|
|
|
for output := range outputs {
|
|
|
|
outputList = append(outputList, output)
|
|
|
|
}
|
|
|
|
sort.Strings(outputList)
|
|
|
|
return outputList
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Installs returns the list of tuples passed to Install.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (r *RuleBuilder) Installs() []RuleBuilderInstall {
|
|
|
|
return append([]RuleBuilderInstall(nil), r.installs...)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (r *RuleBuilder) Tools() []string {
|
|
|
|
var tools []string
|
|
|
|
for _, c := range r.commands {
|
|
|
|
tools = append(tools, c.tools...)
|
|
|
|
}
|
|
|
|
return tools
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Commands returns a slice containing a the built command line for each call to RuleBuilder.Command.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (r *RuleBuilder) Commands() []string {
|
|
|
|
var commands []string
|
|
|
|
for _, c := range r.commands {
|
|
|
|
commands = append(commands, string(c.buf))
|
|
|
|
}
|
|
|
|
return commands
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// BuilderContext is a subset of ModuleContext and SingletonContext.
|
2019-02-02 08:41:11 +08:00
|
|
|
type BuilderContext interface {
|
|
|
|
PathContext
|
|
|
|
Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule
|
|
|
|
Build(PackageContext, BuildParams)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
var _ BuilderContext = ModuleContext(nil)
|
|
|
|
var _ BuilderContext = SingletonContext(nil)
|
|
|
|
|
|
|
|
// Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
|
|
|
|
// Outputs.
|
2019-02-02 08:41:11 +08:00
|
|
|
func (r *RuleBuilder) Build(pctx PackageContext, ctx BuilderContext, name string, desc string) {
|
|
|
|
// TODO: convert RuleBuilder arguments and storage to Paths
|
|
|
|
mctx, _ := ctx.(ModuleContext)
|
2019-01-31 09:32:39 +08:00
|
|
|
var inputs Paths
|
|
|
|
for _, input := range r.Inputs() {
|
2019-02-02 08:41:11 +08:00
|
|
|
// Module output paths
|
|
|
|
if mctx != nil {
|
|
|
|
rel, isRel := MaybeRel(ctx, PathForModuleOut(mctx).String(), input)
|
|
|
|
if isRel {
|
|
|
|
inputs = append(inputs, PathForModuleOut(mctx, rel))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other output paths
|
|
|
|
rel, isRel := MaybeRel(ctx, PathForOutput(ctx).String(), input)
|
2019-01-31 09:32:39 +08:00
|
|
|
if isRel {
|
2019-02-02 08:41:11 +08:00
|
|
|
inputs = append(inputs, PathForOutput(ctx, rel))
|
|
|
|
continue
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
2019-02-02 08:41:11 +08:00
|
|
|
|
|
|
|
// TODO: remove this once boot image is moved to where PathForOutput can find it.
|
|
|
|
inputs = append(inputs, &unknownRulePath{input})
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var outputs WritablePaths
|
|
|
|
for _, output := range r.Outputs() {
|
2019-02-02 08:41:11 +08:00
|
|
|
if mctx != nil {
|
|
|
|
rel := Rel(ctx, PathForModuleOut(mctx).String(), output)
|
|
|
|
outputs = append(outputs, PathForModuleOut(mctx, rel))
|
|
|
|
} else {
|
|
|
|
rel := Rel(ctx, PathForOutput(ctx).String(), output)
|
|
|
|
outputs = append(outputs, PathForOutput(ctx, rel))
|
|
|
|
}
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Commands()) > 0 {
|
|
|
|
ctx.Build(pctx, BuildParams{
|
|
|
|
Rule: ctx.Rule(pctx, name, blueprint.RuleParams{
|
|
|
|
Command: strings.Join(proptools.NinjaEscape(r.Commands()), " && "),
|
|
|
|
CommandDeps: r.Tools(),
|
|
|
|
}),
|
|
|
|
Implicits: inputs,
|
|
|
|
Outputs: outputs,
|
|
|
|
Description: desc,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// RuleBuilderCommand is a builder for a command in a command line. It can be mutated by its methods to add to the
|
|
|
|
// command and track dependencies. The methods mutate the RuleBuilderCommand in place, as well as return the
|
|
|
|
// RuleBuilderCommand, so they can be used chained or unchained. All methods that add text implicitly add a single
|
|
|
|
// space as a separator from the previous method.
|
2019-01-31 09:32:39 +08:00
|
|
|
type RuleBuilderCommand struct {
|
|
|
|
buf []byte
|
|
|
|
inputs []string
|
|
|
|
outputs []string
|
|
|
|
tools []string
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Text adds the specified raw text to the command line. The text should not contain input or output paths or the
|
|
|
|
// rule will not have them listed in its dependencies or outputs.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand {
|
|
|
|
if len(c.buf) > 0 {
|
|
|
|
c.buf = append(c.buf, ' ')
|
|
|
|
}
|
|
|
|
c.buf = append(c.buf, text...)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Textf adds the specified formatted text to the command line. The text should not contain input or output paths or
|
|
|
|
// the rule will not have them listed in its dependencies or outputs.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand {
|
|
|
|
return c.Text(fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Flag adds the specified raw text to the command line. The text should not contain input or output paths or the
|
|
|
|
// rule will not have them listed in its dependencies or outputs.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand {
|
|
|
|
return c.Text(flag)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// FlagWithArg adds the specified flag and argument text to the command line, with no separator between them. The flag
|
|
|
|
// and argument should not contain input or output paths or the rule will not have them listed in its dependencies or
|
|
|
|
// outputs.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand {
|
|
|
|
return c.Text(flag + arg)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// FlagWithArg adds the specified flag and list of arguments to the command line, with the arguments joined by sep
|
|
|
|
// and no separator between the flag and arguments. The flag and arguments should not contain input or output paths or
|
|
|
|
// the rule will not have them listed in its dependencies or outputs.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand {
|
|
|
|
return c.Text(flag + strings.Join(list, sep))
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Tool adds the specified tool path to the command line. The path will be also added to the dependencies returned by
|
|
|
|
// RuleBuilder.Tools.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) Tool(path string) *RuleBuilderCommand {
|
|
|
|
c.tools = append(c.tools, path)
|
|
|
|
return c.Text(path)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Input adds the specified input path to the command line. The path will also be added to the dependencies returned by
|
|
|
|
// RuleBuilder.Inputs.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) Input(path string) *RuleBuilderCommand {
|
|
|
|
c.inputs = append(c.inputs, path)
|
|
|
|
return c.Text(path)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Inputs adds the specified input paths to the command line, separated by spaces. The paths will also be added to the
|
|
|
|
// dependencies returned by RuleBuilder.Inputs.
|
|
|
|
func (c *RuleBuilderCommand) Inputs(paths []string) *RuleBuilderCommand {
|
|
|
|
for _, path := range paths {
|
|
|
|
c.Input(path)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implicit adds the specified input path to the dependencies returned by RuleBuilder.Inputs without modifying the
|
|
|
|
// command line.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) Implicit(path string) *RuleBuilderCommand {
|
|
|
|
c.inputs = append(c.inputs, path)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the
|
|
|
|
// command line.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) Implicits(paths []string) *RuleBuilderCommand {
|
|
|
|
c.inputs = append(c.inputs, paths...)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Output adds the specified output path to the command line. The path will also be added to the outputs returned by
|
|
|
|
// RuleBuilder.Outputs.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) Output(path string) *RuleBuilderCommand {
|
|
|
|
c.outputs = append(c.outputs, path)
|
|
|
|
return c.Text(path)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Outputs adds the specified output paths to the command line, separated by spaces. The paths will also be added to
|
|
|
|
// the outputs returned by RuleBuilder.Outputs.
|
|
|
|
func (c *RuleBuilderCommand) Outputs(paths []string) *RuleBuilderCommand {
|
|
|
|
for _, path := range paths {
|
|
|
|
c.Output(path)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying
|
|
|
|
// the command line.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) ImplicitOutput(path string) *RuleBuilderCommand {
|
|
|
|
c.outputs = append(c.outputs, path)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying
|
|
|
|
// the command line.
|
|
|
|
func (c *RuleBuilderCommand) ImplicitOutputs(paths []string) *RuleBuilderCommand {
|
|
|
|
c.outputs = append(c.outputs, paths...)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlagWithInput adds the specified flag and input path to the command line, with no separator between them. The path
|
|
|
|
// will also be added to the dependencies returned by RuleBuilder.Inputs.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) FlagWithInput(flag, path string) *RuleBuilderCommand {
|
|
|
|
c.inputs = append(c.inputs, path)
|
|
|
|
return c.Text(flag + path)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// FlagWithInputList adds the specified flag and input paths to the command line, with the inputs joined by sep
|
|
|
|
// and no separator between the flag and inputs. The input paths will also be added to the dependencies returned by
|
|
|
|
// RuleBuilder.Inputs.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths []string, sep string) *RuleBuilderCommand {
|
|
|
|
c.inputs = append(c.inputs, paths...)
|
|
|
|
return c.FlagWithList(flag, paths, sep)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// FlagForEachInput adds the specified flag joined with each input path to the command line. The input paths will also
|
|
|
|
// be added to the dependencies returned by RuleBuilder.Inputs. The result is identical to calling FlagWithInput for
|
|
|
|
// each input path.
|
|
|
|
func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths []string) *RuleBuilderCommand {
|
|
|
|
for _, path := range paths {
|
|
|
|
c.FlagWithInput(flag, path)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlagWithOutput adds the specified flag and output path to the command line, with no separator between them. The path
|
|
|
|
// will also be added to the outputs returned by RuleBuilder.Outputs.
|
2019-01-31 09:32:39 +08:00
|
|
|
func (c *RuleBuilderCommand) FlagWithOutput(flag, path string) *RuleBuilderCommand {
|
|
|
|
c.outputs = append(c.outputs, path)
|
|
|
|
return c.Text(flag + path)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// String returns the command line.
|
|
|
|
func (c *RuleBuilderCommand) String() string {
|
|
|
|
return string(c.buf)
|
|
|
|
}
|
|
|
|
|
2019-01-31 09:32:39 +08:00
|
|
|
type unknownRulePath struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Path = (*unknownRulePath)(nil)
|
|
|
|
|
|
|
|
func (p *unknownRulePath) String() string { return p.path }
|
|
|
|
func (p *unknownRulePath) Ext() string { return filepath.Ext(p.path) }
|
|
|
|
func (p *unknownRulePath) Base() string { return filepath.Base(p.path) }
|
|
|
|
func (p *unknownRulePath) Rel() string { return p.path }
|