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"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
"github.com/google/blueprint/proptools"
|
2019-04-13 02:11:38 +08:00
|
|
|
|
|
|
|
"android/soong/shared"
|
2019-01-31 09:32:39 +08:00
|
|
|
)
|
|
|
|
|
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 {
|
2019-02-03 13:25:18 +08:00
|
|
|
commands []*RuleBuilderCommand
|
2019-02-12 06:11:09 +08:00
|
|
|
installs RuleBuilderInstalls
|
2019-02-16 02:39:37 +08:00
|
|
|
temporariesSet map[WritablePath]bool
|
2019-02-03 13:25:18 +08:00
|
|
|
restat bool
|
2019-04-13 02:11:38 +08:00
|
|
|
sbox bool
|
|
|
|
sboxOutDir WritablePath
|
2019-02-06 14:31:15 +08:00
|
|
|
missingDeps []string
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// NewRuleBuilder returns a newly created RuleBuilder.
|
|
|
|
func NewRuleBuilder() *RuleBuilder {
|
2019-02-03 13:25:18 +08:00
|
|
|
return &RuleBuilder{
|
2019-02-16 02:39:37 +08:00
|
|
|
temporariesSet: make(map[WritablePath]bool),
|
2019-02-03 13:25:18 +08:00
|
|
|
}
|
2019-02-02 08:42:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RuleBuilderInstall is a tuple of install from and to locations.
|
|
|
|
type RuleBuilderInstall struct {
|
2019-02-16 02:39:37 +08:00
|
|
|
From Path
|
|
|
|
To string
|
2019-02-02 08:42:32 +08:00
|
|
|
}
|
|
|
|
|
2019-02-12 06:11:09 +08:00
|
|
|
type RuleBuilderInstalls []RuleBuilderInstall
|
|
|
|
|
|
|
|
// String returns the RuleBuilderInstalls in the form used by $(call copy-many-files) in Make, a space separated
|
|
|
|
// list of from:to tuples.
|
|
|
|
func (installs RuleBuilderInstalls) String() string {
|
|
|
|
sb := strings.Builder{}
|
|
|
|
for i, install := range installs {
|
|
|
|
if i != 0 {
|
|
|
|
sb.WriteRune(' ')
|
|
|
|
}
|
2019-02-16 02:39:37 +08:00
|
|
|
sb.WriteString(install.From.String())
|
2019-02-12 06:11:09 +08:00
|
|
|
sb.WriteRune(':')
|
|
|
|
sb.WriteString(install.To)
|
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
2019-02-06 14:31:15 +08:00
|
|
|
// MissingDeps adds modules to the list of missing dependencies. If MissingDeps
|
|
|
|
// is called with a non-empty input, any call to Build will result in a rule
|
|
|
|
// that will print an error listing the missing dependencies and fail.
|
|
|
|
// MissingDeps should only be called if Config.AllowMissingDependencies() is
|
|
|
|
// true.
|
|
|
|
func (r *RuleBuilder) MissingDeps(missingDeps []string) {
|
|
|
|
r.missingDeps = append(r.missingDeps, missingDeps...)
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Restat marks the rule as a restat rule, which will be passed to ModuleContext.Rule in BuildParams.Restat.
|
2019-04-13 02:11:38 +08:00
|
|
|
//
|
|
|
|
// Restat is not compatible with Sbox()
|
2019-01-31 09:32:39 +08:00
|
|
|
func (r *RuleBuilder) Restat() *RuleBuilder {
|
2019-04-13 02:11:38 +08:00
|
|
|
if r.sbox {
|
|
|
|
panic("Restat() is not compatible with Sbox()")
|
|
|
|
}
|
2019-01-31 09:32:39 +08:00
|
|
|
r.restat = true
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-04-13 02:11:38 +08:00
|
|
|
// Sbox marks the rule as needing to be wrapped by sbox. The WritablePath should point to the output
|
|
|
|
// directory that sbox will wipe. It should not be written to by any other rule. sbox will ensure
|
|
|
|
// that all outputs have been written, and will discard any output files that were not specified.
|
|
|
|
//
|
|
|
|
// Sbox is not compatible with Restat()
|
|
|
|
func (r *RuleBuilder) Sbox(outputDir WritablePath) *RuleBuilder {
|
|
|
|
if r.sbox {
|
|
|
|
panic("Sbox() may not be called more than once")
|
|
|
|
}
|
|
|
|
if len(r.commands) > 0 {
|
|
|
|
panic("Sbox() may not be called after Command()")
|
|
|
|
}
|
|
|
|
if r.restat {
|
|
|
|
panic("Sbox() is not compatible with Restat()")
|
|
|
|
}
|
|
|
|
r.sbox = true
|
|
|
|
r.sboxOutDir = outputDir
|
|
|
|
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-02-16 02:39:37 +08:00
|
|
|
func (r *RuleBuilder) Install(from Path, to string) {
|
2019-01-31 09:32:39 +08:00
|
|
|
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 {
|
2019-04-13 02:11:38 +08:00
|
|
|
command := &RuleBuilderCommand{
|
|
|
|
sbox: r.sbox,
|
|
|
|
sboxOutDir: r.sboxOutDir,
|
|
|
|
}
|
2019-01-31 09:32:39 +08:00
|
|
|
r.commands = append(r.commands, command)
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
2019-02-03 13:25:18 +08:00
|
|
|
// Temporary marks an output of a command as an intermediate file that will be used as an input to another command
|
|
|
|
// in the same rule, and should not be listed in Outputs.
|
2019-02-16 02:39:37 +08:00
|
|
|
func (r *RuleBuilder) Temporary(path WritablePath) {
|
2019-02-03 13:25:18 +08:00
|
|
|
r.temporariesSet[path] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary
|
|
|
|
// when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary.
|
|
|
|
func (r *RuleBuilder) DeleteTemporaryFiles() {
|
2019-02-16 02:39:37 +08:00
|
|
|
var temporariesList WritablePaths
|
2019-02-03 13:25:18 +08:00
|
|
|
|
|
|
|
for intermediate := range r.temporariesSet {
|
|
|
|
temporariesList = append(temporariesList, intermediate)
|
|
|
|
}
|
2019-02-16 02:39:37 +08:00
|
|
|
|
|
|
|
sort.Slice(temporariesList, func(i, j int) bool {
|
|
|
|
return temporariesList[i].String() < temporariesList[j].String()
|
|
|
|
})
|
2019-02-03 13:25:18 +08:00
|
|
|
|
|
|
|
r.Command().Text("rm").Flag("-f").Outputs(temporariesList)
|
|
|
|
}
|
|
|
|
|
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-02-16 02:39:37 +08:00
|
|
|
func (r *RuleBuilder) Inputs() Paths {
|
2019-01-31 09:32:39 +08:00
|
|
|
outputs := r.outputSet()
|
2019-04-13 02:11:38 +08:00
|
|
|
depFiles := r.depFileSet()
|
2019-01-31 09:32:39 +08:00
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
inputs := make(map[string]Path)
|
2019-01-31 09:32:39 +08:00
|
|
|
for _, c := range r.commands {
|
|
|
|
for _, input := range c.inputs {
|
2019-04-13 02:11:38 +08:00
|
|
|
inputStr := input.String()
|
|
|
|
if _, isOutput := outputs[inputStr]; !isOutput {
|
|
|
|
if _, isDepFile := depFiles[inputStr]; !isDepFile {
|
|
|
|
inputs[input.String()] = input
|
|
|
|
}
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
var inputList Paths
|
|
|
|
for _, input := range inputs {
|
2019-01-31 09:32:39 +08:00
|
|
|
inputList = append(inputList, input)
|
|
|
|
}
|
2019-02-16 02:39:37 +08:00
|
|
|
|
|
|
|
sort.Slice(inputList, func(i, j int) bool {
|
|
|
|
return inputList[i].String() < inputList[j].String()
|
|
|
|
})
|
2019-01-31 09:32:39 +08:00
|
|
|
|
|
|
|
return inputList
|
|
|
|
}
|
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
func (r *RuleBuilder) outputSet() map[string]WritablePath {
|
|
|
|
outputs := make(map[string]WritablePath)
|
2019-01-31 09:32:39 +08:00
|
|
|
for _, c := range r.commands {
|
|
|
|
for _, output := range c.outputs {
|
2019-02-16 02:39:37 +08:00
|
|
|
outputs[output.String()] = output
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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-02-16 02:39:37 +08:00
|
|
|
func (r *RuleBuilder) Outputs() WritablePaths {
|
2019-01-31 09:32:39 +08:00
|
|
|
outputs := r.outputSet()
|
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
var outputList WritablePaths
|
|
|
|
for _, output := range outputs {
|
2019-02-03 13:25:18 +08:00
|
|
|
if !r.temporariesSet[output] {
|
|
|
|
outputList = append(outputList, output)
|
|
|
|
}
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
2019-02-16 02:39:37 +08:00
|
|
|
|
|
|
|
sort.Slice(outputList, func(i, j int) bool {
|
|
|
|
return outputList[i].String() < outputList[j].String()
|
|
|
|
})
|
|
|
|
|
2019-01-31 09:32:39 +08:00
|
|
|
return outputList
|
|
|
|
}
|
|
|
|
|
2019-04-13 02:11:38 +08:00
|
|
|
func (r *RuleBuilder) depFileSet() map[string]WritablePath {
|
|
|
|
depFiles := make(map[string]WritablePath)
|
|
|
|
for _, c := range r.commands {
|
|
|
|
for _, depFile := range c.depFiles {
|
|
|
|
depFiles[depFile.String()] = depFile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return depFiles
|
|
|
|
}
|
|
|
|
|
2019-03-30 06:33:06 +08:00
|
|
|
// DepFiles returns the list of paths that were passed to the RuleBuilderCommand methods that take depfile paths, such
|
|
|
|
// as RuleBuilderCommand.DepFile or RuleBuilderCommand.FlagWithDepFile.
|
|
|
|
func (r *RuleBuilder) DepFiles() WritablePaths {
|
|
|
|
var depFiles WritablePaths
|
|
|
|
|
|
|
|
for _, c := range r.commands {
|
|
|
|
for _, depFile := range c.depFiles {
|
|
|
|
depFiles = append(depFiles, depFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return depFiles
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// Installs returns the list of tuples passed to Install.
|
2019-02-12 06:11:09 +08:00
|
|
|
func (r *RuleBuilder) Installs() RuleBuilderInstalls {
|
|
|
|
return append(RuleBuilderInstalls(nil), r.installs...)
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
func (r *RuleBuilder) toolsSet() map[string]Path {
|
|
|
|
tools := make(map[string]Path)
|
2019-01-31 09:32:39 +08:00
|
|
|
for _, c := range r.commands {
|
2019-02-03 13:25:18 +08:00
|
|
|
for _, tool := range c.tools {
|
2019-02-16 02:39:37 +08:00
|
|
|
tools[tool.String()] = tool
|
2019-02-03 13:25:18 +08:00
|
|
|
}
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
2019-02-03 13:25:18 +08:00
|
|
|
|
2019-01-31 09:32:39 +08:00
|
|
|
return tools
|
|
|
|
}
|
|
|
|
|
2019-02-03 13:25:18 +08:00
|
|
|
// Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method.
|
2019-02-16 02:39:37 +08:00
|
|
|
func (r *RuleBuilder) Tools() Paths {
|
2019-02-03 13:25:18 +08:00
|
|
|
toolsSet := r.toolsSet()
|
|
|
|
|
2019-02-16 02:39:37 +08:00
|
|
|
var toolsList Paths
|
|
|
|
for _, tool := range toolsSet {
|
2019-02-03 13:25:18 +08:00
|
|
|
toolsList = append(toolsList, tool)
|
|
|
|
}
|
2019-02-16 02:39:37 +08:00
|
|
|
|
|
|
|
sort.Slice(toolsList, func(i, j int) bool {
|
|
|
|
return toolsList[i].String() < toolsList[j].String()
|
|
|
|
})
|
|
|
|
|
2019-02-03 13:25:18 +08:00
|
|
|
return toolsList
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-07-09 08:07:18 +08:00
|
|
|
commands = append(commands, c.buf.String())
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
2019-03-30 06:33:06 +08:00
|
|
|
func (r *RuleBuilder) depFileMergerCmd(ctx PathContext, depFiles WritablePaths) *RuleBuilderCommand {
|
2019-04-13 02:11:38 +08:00
|
|
|
return r.Command().
|
2019-07-09 08:08:34 +08:00
|
|
|
BuiltTool(ctx, "dep_fixer").
|
2019-04-13 02:11:38 +08:00
|
|
|
Inputs(depFiles.Paths())
|
2019-03-30 06:33:06 +08:00
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// 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) {
|
2019-03-30 06:33:06 +08:00
|
|
|
name = ninjaNameEscape(name)
|
|
|
|
|
2019-02-06 14:31:15 +08:00
|
|
|
if len(r.missingDeps) > 0 {
|
|
|
|
ctx.Build(pctx, BuildParams{
|
|
|
|
Rule: ErrorRule,
|
2019-02-16 02:39:37 +08:00
|
|
|
Outputs: r.Outputs(),
|
2019-02-06 14:31:15 +08:00
|
|
|
Description: desc,
|
|
|
|
Args: map[string]string{
|
|
|
|
"error": "missing dependencies: " + strings.Join(r.missingDeps, ", "),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-30 06:33:06 +08:00
|
|
|
var depFile WritablePath
|
|
|
|
var depFormat blueprint.Deps
|
|
|
|
if depFiles := r.DepFiles(); len(depFiles) > 0 {
|
|
|
|
depFile = depFiles[0]
|
|
|
|
depFormat = blueprint.DepsGCC
|
|
|
|
if len(depFiles) > 1 {
|
|
|
|
// Add a command locally that merges all depfiles together into the first depfile.
|
2019-04-13 02:11:38 +08:00
|
|
|
r.depFileMergerCmd(ctx, depFiles)
|
|
|
|
|
|
|
|
if r.sbox {
|
|
|
|
// Check for Rel() errors, as all depfiles should be in the output dir
|
|
|
|
for _, path := range depFiles[1:] {
|
|
|
|
Rel(ctx, r.sboxOutDir.String(), path.String())
|
|
|
|
}
|
|
|
|
}
|
2019-03-30 06:33:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 02:11:38 +08:00
|
|
|
tools := r.Tools()
|
|
|
|
commands := r.Commands()
|
|
|
|
outputs := r.Outputs()
|
|
|
|
|
|
|
|
if len(commands) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(outputs) == 0 {
|
|
|
|
panic("No outputs specified from any Commands")
|
2019-03-30 06:33:06 +08:00
|
|
|
}
|
|
|
|
|
2019-04-13 02:11:38 +08:00
|
|
|
commandString := strings.Join(proptools.NinjaEscapeList(commands), " && ")
|
|
|
|
|
|
|
|
if r.sbox {
|
|
|
|
sboxOutputs := make([]string, len(outputs))
|
|
|
|
for i, output := range outputs {
|
|
|
|
sboxOutputs[i] = "__SBOX_OUT_DIR__/" + Rel(ctx, r.sboxOutDir.String(), output.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if depFile != nil {
|
|
|
|
sboxOutputs = append(sboxOutputs, "__SBOX_OUT_DIR__/"+Rel(ctx, r.sboxOutDir.String(), depFile.String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
commandString = proptools.ShellEscape(commandString)
|
|
|
|
if !strings.HasPrefix(commandString, `'`) {
|
|
|
|
commandString = `'` + commandString + `'`
|
|
|
|
}
|
|
|
|
|
|
|
|
sboxCmd := &RuleBuilderCommand{}
|
2019-07-09 08:08:34 +08:00
|
|
|
sboxCmd.BuiltTool(ctx, "sbox").
|
2019-04-13 02:11:38 +08:00
|
|
|
Flag("-c").Text(commandString).
|
|
|
|
Flag("--sandbox-path").Text(shared.TempDirForOutDir(PathForOutput(ctx).String())).
|
|
|
|
Flag("--output-root").Text(r.sboxOutDir.String()).
|
|
|
|
Flags(sboxOutputs)
|
|
|
|
|
2019-07-09 08:07:18 +08:00
|
|
|
commandString = sboxCmd.buf.String()
|
2019-04-13 02:11:38 +08:00
|
|
|
tools = append(tools, sboxCmd.tools...)
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
2019-04-13 02:11:38 +08:00
|
|
|
|
|
|
|
// Ninja doesn't like multiple outputs when depfiles are enabled, move all but the first output to
|
|
|
|
// ImplicitOutputs. RuleBuilder never uses "$out", so the distinction between Outputs and ImplicitOutputs
|
|
|
|
// doesn't matter.
|
|
|
|
output := outputs[0]
|
|
|
|
implicitOutputs := outputs[1:]
|
|
|
|
|
|
|
|
ctx.Build(pctx, BuildParams{
|
|
|
|
Rule: ctx.Rule(pctx, name, blueprint.RuleParams{
|
|
|
|
Command: commandString,
|
|
|
|
CommandDeps: tools.Strings(),
|
|
|
|
Restat: r.restat,
|
|
|
|
}),
|
|
|
|
Implicits: r.Inputs(),
|
|
|
|
Output: output,
|
|
|
|
ImplicitOutputs: implicitOutputs,
|
|
|
|
Depfile: depFile,
|
|
|
|
Deps: depFormat,
|
|
|
|
Description: desc,
|
|
|
|
})
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-07-09 08:07:18 +08:00
|
|
|
buf strings.Builder
|
2019-03-30 06:33:06 +08:00
|
|
|
inputs Paths
|
|
|
|
outputs WritablePaths
|
|
|
|
depFiles WritablePaths
|
|
|
|
tools Paths
|
2019-04-13 02:11:38 +08:00
|
|
|
|
|
|
|
sbox bool
|
|
|
|
sboxOutDir WritablePath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RuleBuilderCommand) addInput(path Path) string {
|
|
|
|
if c.sbox {
|
|
|
|
if rel, isRel, _ := maybeRelErr(c.sboxOutDir.String(), path.String()); isRel {
|
|
|
|
return "__SBOX_OUT_DIR__/" + rel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.inputs = append(c.inputs, path)
|
|
|
|
return path.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RuleBuilderCommand) outputStr(path Path) string {
|
|
|
|
if c.sbox {
|
|
|
|
// Errors will be handled in RuleBuilder.Build where we have a context to report them
|
|
|
|
rel, _, _ := maybeRelErr(c.sboxOutDir.String(), path.String())
|
|
|
|
return "__SBOX_OUT_DIR__/" + rel
|
|
|
|
}
|
|
|
|
return path.String()
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-07-09 08:07:18 +08:00
|
|
|
if c.buf.Len() > 0 {
|
|
|
|
c.buf.WriteByte(' ')
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
2019-07-09 08:07:18 +08:00
|
|
|
c.buf.WriteString(text)
|
2019-01-31 09:32:39 +08:00
|
|
|
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-03-30 06:32:51 +08:00
|
|
|
// Flags 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.
|
|
|
|
func (c *RuleBuilderCommand) Flags(flags []string) *RuleBuilderCommand {
|
|
|
|
for _, flag := range flags {
|
|
|
|
c.Text(flag)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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-12 06:11:09 +08:00
|
|
|
// FlagForEachArg adds the specified flag joined with each argument to the command line. The result is identical to
|
|
|
|
// calling FlagWithArg for argument.
|
|
|
|
func (c *RuleBuilderCommand) FlagForEachArg(flag string, args []string) *RuleBuilderCommand {
|
|
|
|
for _, arg := range args {
|
|
|
|
c.FlagWithArg(flag, arg)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-28 00:56:41 +08:00
|
|
|
// FlagWithList adds the specified flag and list of arguments to the command line, with the arguments joined by sep
|
2019-02-02 08:42:32 +08:00
|
|
|
// 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-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand {
|
2019-01-31 09:32:39 +08:00
|
|
|
c.tools = append(c.tools, path)
|
2019-02-16 02:39:37 +08:00
|
|
|
return c.Text(path.String())
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
2019-07-09 08:08:34 +08:00
|
|
|
// BuiltTool adds the specified tool path that was built using a host Soong module to the command line. The path will
|
|
|
|
// be also added to the dependencies returned by RuleBuilder.Tools.
|
|
|
|
//
|
|
|
|
// It is equivalent to:
|
|
|
|
// cmd.Tool(ctx.Config().HostToolPath(ctx, tool))
|
|
|
|
func (c *RuleBuilderCommand) BuiltTool(ctx PathContext, tool string) *RuleBuilderCommand {
|
|
|
|
return c.Tool(ctx.Config().HostToolPath(ctx, tool))
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrebuiltBuildTool adds the specified tool path from prebuils/build-tools. The path will be also added to the
|
|
|
|
// dependencies returned by RuleBuilder.Tools.
|
|
|
|
//
|
|
|
|
// It is equivalent to:
|
|
|
|
// cmd.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool))
|
|
|
|
func (c *RuleBuilderCommand) PrebuiltBuildTool(ctx PathContext, tool string) *RuleBuilderCommand {
|
|
|
|
return c.Tool(ctx.Config().PrebuiltBuildTool(ctx, tool))
|
|
|
|
}
|
|
|
|
|
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-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand {
|
2019-04-13 02:11:38 +08:00
|
|
|
return c.Text(c.addInput(path))
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2019-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand {
|
2019-02-02 08:42:32 +08:00
|
|
|
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-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand {
|
2019-04-13 02:11:38 +08:00
|
|
|
c.addInput(path)
|
2019-01-31 09:32:39 +08:00
|
|
|
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-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand {
|
2019-04-13 02:11:38 +08:00
|
|
|
for _, path := range paths {
|
|
|
|
c.addInput(path)
|
|
|
|
}
|
2019-01-31 09:32:39 +08:00
|
|
|
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-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand {
|
2019-01-31 09:32:39 +08:00
|
|
|
c.outputs = append(c.outputs, path)
|
2019-04-13 02:11:38 +08:00
|
|
|
return c.Text(c.outputStr(path))
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2019-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand {
|
2019-02-02 08:42:32 +08:00
|
|
|
for _, path := range paths {
|
|
|
|
c.Output(path)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-06-05 08:10:41 +08:00
|
|
|
// OutputDir adds the output directory to the command line. This is only available when used with RuleBuilder.Sbox,
|
|
|
|
// and will be the temporary output directory managed by sbox, not the final one.
|
|
|
|
func (c *RuleBuilderCommand) OutputDir() *RuleBuilderCommand {
|
|
|
|
if !c.sbox {
|
|
|
|
panic("OutputDir only valid with Sbox")
|
|
|
|
}
|
|
|
|
return c.Text("__SBOX_OUT_DIR__")
|
|
|
|
}
|
|
|
|
|
2019-03-30 06:33:06 +08:00
|
|
|
// DepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles and adds it to the command
|
|
|
|
// line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles are added to
|
|
|
|
// commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the depfiles together.
|
|
|
|
func (c *RuleBuilderCommand) DepFile(path WritablePath) *RuleBuilderCommand {
|
|
|
|
c.depFiles = append(c.depFiles, path)
|
2019-04-13 02:11:38 +08:00
|
|
|
return c.Text(c.outputStr(path))
|
2019-03-30 06:33:06 +08:00
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying
|
|
|
|
// the command line.
|
2019-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand {
|
2019-01-31 09:32:39 +08:00
|
|
|
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.
|
2019-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand {
|
2019-02-02 08:42:32 +08:00
|
|
|
c.outputs = append(c.outputs, paths...)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-03-30 06:33:06 +08:00
|
|
|
// ImplicitDepFile adds the specified depfile path to the paths returned by RuleBuilder.DepFiles without modifying
|
|
|
|
// the command line, and causes RuleBuilder.Build file to set the depfile flag for ninja. If multiple depfiles
|
|
|
|
// are added to commands in a single RuleBuilder then RuleBuilder.Build will add an extra command to merge the
|
|
|
|
// depfiles together.
|
|
|
|
func (c *RuleBuilderCommand) ImplicitDepFile(path WritablePath) *RuleBuilderCommand {
|
|
|
|
c.depFiles = append(c.depFiles, path)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// 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-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand {
|
2019-04-13 02:11:38 +08:00
|
|
|
return c.Text(flag + c.addInput(path))
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
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-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand {
|
2019-04-13 02:11:38 +08:00
|
|
|
strs := make([]string, len(paths))
|
|
|
|
for i, path := range paths {
|
|
|
|
strs[i] = c.addInput(path)
|
|
|
|
}
|
|
|
|
return c.FlagWithList(flag, strs, sep)
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2019-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand {
|
2019-02-02 08:42:32 +08:00
|
|
|
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-02-16 02:39:37 +08:00
|
|
|
func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand {
|
2019-01-31 09:32:39 +08:00
|
|
|
c.outputs = append(c.outputs, path)
|
2019-04-13 02:11:38 +08:00
|
|
|
return c.Text(flag + c.outputStr(path))
|
2019-01-31 09:32:39 +08:00
|
|
|
}
|
|
|
|
|
2019-03-30 06:33:06 +08:00
|
|
|
// FlagWithDepFile adds the specified flag and depfile path to the command line, with no separator between them. The path
|
|
|
|
// will also be added to the outputs returned by RuleBuilder.Outputs.
|
|
|
|
func (c *RuleBuilderCommand) FlagWithDepFile(flag string, path WritablePath) *RuleBuilderCommand {
|
|
|
|
c.depFiles = append(c.depFiles, path)
|
2019-04-13 02:11:38 +08:00
|
|
|
return c.Text(flag + c.outputStr(path))
|
2019-03-30 06:33:06 +08:00
|
|
|
}
|
|
|
|
|
2019-02-02 08:42:32 +08:00
|
|
|
// String returns the command line.
|
|
|
|
func (c *RuleBuilderCommand) String() string {
|
2019-07-09 08:07:18 +08:00
|
|
|
return c.buf.String()
|
2019-02-02 08:42:32 +08:00
|
|
|
}
|
2019-03-30 06:33:06 +08:00
|
|
|
|
|
|
|
func ninjaNameEscape(s string) string {
|
|
|
|
b := []byte(s)
|
|
|
|
escaped := false
|
|
|
|
for i, c := range b {
|
|
|
|
valid := (c >= 'a' && c <= 'z') ||
|
|
|
|
(c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= '0' && c <= '9') ||
|
|
|
|
(c == '_') ||
|
|
|
|
(c == '-') ||
|
|
|
|
(c == '.')
|
|
|
|
if !valid {
|
|
|
|
b[i] = '_'
|
|
|
|
escaped = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if escaped {
|
|
|
|
s = string(b)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|