2019-01-31 02:32:39 +01: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 (
|
2020-11-14 01:23:53 +01:00
|
|
|
"crypto/sha256"
|
2019-01-31 02:32:39 +01:00
|
|
|
"fmt"
|
2020-11-14 01:23:53 +01:00
|
|
|
"path/filepath"
|
2019-01-31 02:32:39 +01:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-11-12 17:29:30 +01:00
|
|
|
"testing"
|
2019-01-31 02:32:39 +01:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
"github.com/google/blueprint/proptools"
|
2021-05-24 23:24:12 +02:00
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2019-04-12 20:11:38 +02:00
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
"android/soong/cmd/sbox/sbox_proto"
|
2021-03-13 02:24:45 +01:00
|
|
|
"android/soong/remoteexec"
|
2021-03-23 21:44:30 +01:00
|
|
|
"android/soong/response"
|
2019-04-12 20:11:38 +02:00
|
|
|
"android/soong/shared"
|
2019-01-31 02:32:39 +01:00
|
|
|
)
|
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
const sboxSandboxBaseDir = "__SBOX_SANDBOX_DIR__"
|
|
|
|
const sboxOutSubDir = "out"
|
2020-11-25 01:32:22 +01:00
|
|
|
const sboxToolsSubDir = "tools"
|
2020-11-12 17:29:30 +01:00
|
|
|
const sboxOutDir = sboxSandboxBaseDir + "/" + sboxOutSubDir
|
2020-11-14 01:23:53 +01:00
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
// RuleBuilder provides an alternative to ModuleContext.Rule and ModuleContext.Build to add a command line to the build
|
|
|
|
// graph.
|
2019-01-31 02:32:39 +01:00
|
|
|
type RuleBuilder struct {
|
2020-11-17 02:32:30 +01:00
|
|
|
pctx PackageContext
|
|
|
|
ctx BuilderContext
|
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
commands []*RuleBuilderCommand
|
|
|
|
installs RuleBuilderInstalls
|
|
|
|
temporariesSet map[WritablePath]bool
|
|
|
|
restat bool
|
|
|
|
sbox bool
|
|
|
|
highmem bool
|
|
|
|
remoteable RemoteRuleSupports
|
2021-03-13 02:24:45 +01:00
|
|
|
rbeParams *remoteexec.REParams
|
2020-11-17 02:32:30 +01:00
|
|
|
outDir WritablePath
|
2020-11-25 01:32:22 +01:00
|
|
|
sboxTools bool
|
2021-03-13 02:52:23 +01:00
|
|
|
sboxInputs bool
|
2020-11-12 17:29:30 +01:00
|
|
|
sboxManifestPath WritablePath
|
|
|
|
missingDeps []string
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
// NewRuleBuilder returns a newly created RuleBuilder.
|
2020-11-17 02:32:30 +01:00
|
|
|
func NewRuleBuilder(pctx PackageContext, ctx BuilderContext) *RuleBuilder {
|
2019-02-03 06:25:18 +01:00
|
|
|
return &RuleBuilder{
|
2020-11-17 02:32:30 +01:00
|
|
|
pctx: pctx,
|
|
|
|
ctx: ctx,
|
2019-02-15 19:39:37 +01:00
|
|
|
temporariesSet: make(map[WritablePath]bool),
|
2019-02-03 06:25:18 +01:00
|
|
|
}
|
2019-02-02 01:42:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RuleBuilderInstall is a tuple of install from and to locations.
|
|
|
|
type RuleBuilderInstall struct {
|
2019-02-15 19:39:37 +01:00
|
|
|
From Path
|
|
|
|
To string
|
2019-02-02 01:42:32 +01:00
|
|
|
}
|
|
|
|
|
2019-02-11 23:11:09 +01: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-15 19:39:37 +01:00
|
|
|
sb.WriteString(install.From.String())
|
2019-02-11 23:11:09 +01:00
|
|
|
sb.WriteRune(':')
|
|
|
|
sb.WriteString(install.To)
|
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
2019-02-06 07:31:15 +01: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 01:42:32 +01:00
|
|
|
// Restat marks the rule as a restat rule, which will be passed to ModuleContext.Rule in BuildParams.Restat.
|
2019-04-12 20:11:38 +02:00
|
|
|
//
|
|
|
|
// Restat is not compatible with Sbox()
|
2019-01-31 02:32:39 +01:00
|
|
|
func (r *RuleBuilder) Restat() *RuleBuilder {
|
2019-04-12 20:11:38 +02:00
|
|
|
if r.sbox {
|
|
|
|
panic("Restat() is not compatible with Sbox()")
|
|
|
|
}
|
2019-01-31 02:32:39 +01:00
|
|
|
r.restat = true
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-11-15 22:18:43 +01:00
|
|
|
// HighMem marks the rule as a high memory rule, which will limit how many run in parallel with other high memory
|
|
|
|
// rules.
|
|
|
|
func (r *RuleBuilder) HighMem() *RuleBuilder {
|
|
|
|
r.highmem = true
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remoteable marks the rule as supporting remote execution.
|
|
|
|
func (r *RuleBuilder) Remoteable(supports RemoteRuleSupports) *RuleBuilder {
|
|
|
|
r.remoteable = supports
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-03-13 02:24:45 +01:00
|
|
|
// Rewrapper marks the rule as running inside rewrapper using the given params in order to support
|
|
|
|
// running on RBE. During RuleBuilder.Build the params will be combined with the inputs, outputs
|
|
|
|
// and tools known to RuleBuilder to prepend an appropriate rewrapper command line to the rule's
|
|
|
|
// command line.
|
|
|
|
func (r *RuleBuilder) Rewrapper(params *remoteexec.REParams) *RuleBuilder {
|
|
|
|
if !r.sboxInputs {
|
|
|
|
panic(fmt.Errorf("RuleBuilder.Rewrapper must be called after RuleBuilder.SandboxInputs"))
|
|
|
|
}
|
|
|
|
r.rbeParams = params
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
// Sbox marks the rule as needing to be wrapped by sbox. The outputDir should point to the output
|
|
|
|
// directory that sbox will wipe. It should not be written to by any other rule. manifestPath should
|
|
|
|
// point to a location where sbox's manifest will be written and must be outside outputDir. sbox
|
|
|
|
// will ensure that all outputs have been written, and will discard any output files that were not
|
|
|
|
// specified.
|
2019-04-12 20:11:38 +02:00
|
|
|
//
|
|
|
|
// Sbox is not compatible with Restat()
|
2020-11-12 17:29:30 +01:00
|
|
|
func (r *RuleBuilder) Sbox(outputDir WritablePath, manifestPath WritablePath) *RuleBuilder {
|
2019-04-12 20:11:38 +02:00
|
|
|
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
|
2020-11-17 02:32:30 +01:00
|
|
|
r.outDir = outputDir
|
2020-11-12 17:29:30 +01:00
|
|
|
r.sboxManifestPath = manifestPath
|
2019-04-12 20:11:38 +02:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-11-25 01:32:22 +01:00
|
|
|
// SandboxTools enables tool sandboxing for the rule by copying any referenced tools into the
|
|
|
|
// sandbox.
|
|
|
|
func (r *RuleBuilder) SandboxTools() *RuleBuilder {
|
|
|
|
if !r.sbox {
|
|
|
|
panic("SandboxTools() must be called after Sbox()")
|
|
|
|
}
|
|
|
|
if len(r.commands) > 0 {
|
|
|
|
panic("SandboxTools() may not be called after Command()")
|
|
|
|
}
|
|
|
|
r.sboxTools = true
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-03-13 02:52:23 +01:00
|
|
|
// SandboxInputs enables input sandboxing for the rule by copying any referenced inputs into the
|
|
|
|
// sandbox. It also implies SandboxTools().
|
|
|
|
//
|
|
|
|
// Sandboxing inputs requires RuleBuilder to be aware of all references to input paths. Paths
|
|
|
|
// that are passed to RuleBuilder outside of the methods that expect inputs, for example
|
|
|
|
// FlagWithArg, must use RuleBuilderCommand.PathForInput to translate the path to one that matches
|
|
|
|
// the sandbox layout.
|
|
|
|
func (r *RuleBuilder) SandboxInputs() *RuleBuilder {
|
|
|
|
if !r.sbox {
|
|
|
|
panic("SandboxInputs() must be called after Sbox()")
|
|
|
|
}
|
|
|
|
if len(r.commands) > 0 {
|
|
|
|
panic("SandboxInputs() may not be called after Command()")
|
|
|
|
}
|
|
|
|
r.sboxTools = true
|
|
|
|
r.sboxInputs = true
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
// Install associates an output of the rule with an install location, which can be retrieved later using
|
|
|
|
// RuleBuilder.Installs.
|
2019-02-15 19:39:37 +01:00
|
|
|
func (r *RuleBuilder) Install(from Path, to string) {
|
2019-01-31 02:32:39 +01:00
|
|
|
r.installs = append(r.installs, RuleBuilderInstall{from, to})
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01: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 02:32:39 +01:00
|
|
|
func (r *RuleBuilder) Command() *RuleBuilderCommand {
|
2019-04-12 20:11:38 +02:00
|
|
|
command := &RuleBuilderCommand{
|
2020-11-17 02:32:30 +01:00
|
|
|
rule: r,
|
2019-04-12 20:11:38 +02:00
|
|
|
}
|
2019-01-31 02:32:39 +01:00
|
|
|
r.commands = append(r.commands, command)
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
2019-02-03 06:25:18 +01: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-15 19:39:37 +01:00
|
|
|
func (r *RuleBuilder) Temporary(path WritablePath) {
|
2019-02-03 06:25:18 +01: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-15 19:39:37 +01:00
|
|
|
var temporariesList WritablePaths
|
2019-02-03 06:25:18 +01:00
|
|
|
|
|
|
|
for intermediate := range r.temporariesSet {
|
|
|
|
temporariesList = append(temporariesList, intermediate)
|
|
|
|
}
|
2019-02-15 19:39:37 +01:00
|
|
|
|
|
|
|
sort.Slice(temporariesList, func(i, j int) bool {
|
|
|
|
return temporariesList[i].String() < temporariesList[j].String()
|
|
|
|
})
|
2019-02-03 06:25:18 +01:00
|
|
|
|
|
|
|
r.Command().Text("rm").Flag("-f").Outputs(temporariesList)
|
|
|
|
}
|
|
|
|
|
2020-02-22 01:55:19 +01:00
|
|
|
// Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take
|
2020-11-14 01:23:53 +01:00
|
|
|
// input paths, such as RuleBuilderCommand.Input, RuleBuilderCommand.Implicit, or
|
2020-02-22 01:55:19 +01:00
|
|
|
// RuleBuilderCommand.FlagWithInput. Inputs to a command that are also outputs of another command
|
|
|
|
// in the same RuleBuilder are filtered out. The list is sorted and duplicates removed.
|
2019-02-15 19:39:37 +01:00
|
|
|
func (r *RuleBuilder) Inputs() Paths {
|
2019-01-31 02:32:39 +01:00
|
|
|
outputs := r.outputSet()
|
2019-04-12 20:11:38 +02:00
|
|
|
depFiles := r.depFileSet()
|
2019-01-31 02:32:39 +01:00
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
inputs := make(map[string]Path)
|
2019-01-31 02:32:39 +01:00
|
|
|
for _, c := range r.commands {
|
2020-06-13 23:38:27 +02:00
|
|
|
for _, input := range append(c.inputs, c.implicits...) {
|
2019-04-12 20:11:38 +02:00
|
|
|
inputStr := input.String()
|
|
|
|
if _, isOutput := outputs[inputStr]; !isOutput {
|
|
|
|
if _, isDepFile := depFiles[inputStr]; !isDepFile {
|
|
|
|
inputs[input.String()] = input
|
|
|
|
}
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
var inputList Paths
|
|
|
|
for _, input := range inputs {
|
2019-01-31 02:32:39 +01:00
|
|
|
inputList = append(inputList, input)
|
|
|
|
}
|
2019-02-15 19:39:37 +01:00
|
|
|
|
|
|
|
sort.Slice(inputList, func(i, j int) bool {
|
|
|
|
return inputList[i].String() < inputList[j].String()
|
|
|
|
})
|
2019-01-31 02:32:39 +01:00
|
|
|
|
|
|
|
return inputList
|
|
|
|
}
|
|
|
|
|
2020-02-22 01:55:19 +01:00
|
|
|
// OrderOnlys returns the list of paths that were passed to the RuleBuilderCommand.OrderOnly or
|
|
|
|
// RuleBuilderCommand.OrderOnlys. The list is sorted and duplicates removed.
|
|
|
|
func (r *RuleBuilder) OrderOnlys() Paths {
|
|
|
|
orderOnlys := make(map[string]Path)
|
|
|
|
for _, c := range r.commands {
|
|
|
|
for _, orderOnly := range c.orderOnlys {
|
|
|
|
orderOnlys[orderOnly.String()] = orderOnly
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var orderOnlyList Paths
|
|
|
|
for _, orderOnly := range orderOnlys {
|
|
|
|
orderOnlyList = append(orderOnlyList, orderOnly)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(orderOnlyList, func(i, j int) bool {
|
|
|
|
return orderOnlyList[i].String() < orderOnlyList[j].String()
|
|
|
|
})
|
|
|
|
|
|
|
|
return orderOnlyList
|
|
|
|
}
|
|
|
|
|
2021-04-21 20:45:23 +02:00
|
|
|
// Validations returns the list of paths that were passed to RuleBuilderCommand.Validation or
|
|
|
|
// RuleBuilderCommand.Validations. The list is sorted and duplicates removed.
|
|
|
|
func (r *RuleBuilder) Validations() Paths {
|
|
|
|
validations := make(map[string]Path)
|
|
|
|
for _, c := range r.commands {
|
|
|
|
for _, validation := range c.validations {
|
|
|
|
validations[validation.String()] = validation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var validationList Paths
|
|
|
|
for _, validation := range validations {
|
|
|
|
validationList = append(validationList, validation)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(validationList, func(i, j int) bool {
|
|
|
|
return validationList[i].String() < validationList[j].String()
|
|
|
|
})
|
|
|
|
|
|
|
|
return validationList
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
func (r *RuleBuilder) outputSet() map[string]WritablePath {
|
|
|
|
outputs := make(map[string]WritablePath)
|
2019-01-31 02:32:39 +01:00
|
|
|
for _, c := range r.commands {
|
|
|
|
for _, output := range c.outputs {
|
2019-02-15 19:39:37 +01:00
|
|
|
outputs[output.String()] = output
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return outputs
|
|
|
|
}
|
|
|
|
|
2020-02-22 01:55:19 +01: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. The list is sorted and duplicates removed.
|
2019-02-15 19:39:37 +01:00
|
|
|
func (r *RuleBuilder) Outputs() WritablePaths {
|
2019-01-31 02:32:39 +01:00
|
|
|
outputs := r.outputSet()
|
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
var outputList WritablePaths
|
|
|
|
for _, output := range outputs {
|
2019-02-03 06:25:18 +01:00
|
|
|
if !r.temporariesSet[output] {
|
|
|
|
outputList = append(outputList, output)
|
|
|
|
}
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
2019-02-15 19:39:37 +01:00
|
|
|
|
|
|
|
sort.Slice(outputList, func(i, j int) bool {
|
|
|
|
return outputList[i].String() < outputList[j].String()
|
|
|
|
})
|
|
|
|
|
2019-01-31 02:32:39 +01:00
|
|
|
return outputList
|
|
|
|
}
|
|
|
|
|
2020-09-23 06:30:02 +02:00
|
|
|
func (r *RuleBuilder) symlinkOutputSet() map[string]WritablePath {
|
|
|
|
symlinkOutputs := make(map[string]WritablePath)
|
|
|
|
for _, c := range r.commands {
|
|
|
|
for _, symlinkOutput := range c.symlinkOutputs {
|
|
|
|
symlinkOutputs[symlinkOutput.String()] = symlinkOutput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return symlinkOutputs
|
|
|
|
}
|
|
|
|
|
|
|
|
// SymlinkOutputs returns the list of paths that the executor (Ninja) would
|
|
|
|
// verify, after build edge completion, that:
|
|
|
|
//
|
|
|
|
// 1) Created output symlinks match the list of paths in this list exactly (no more, no fewer)
|
|
|
|
// 2) Created output files are *not* declared in this list.
|
|
|
|
//
|
|
|
|
// These symlink outputs are expected to be a subset of outputs or implicit
|
|
|
|
// outputs, or they would fail validation at build param construction time
|
|
|
|
// later, to support other non-rule-builder approaches for constructing
|
|
|
|
// statements.
|
|
|
|
func (r *RuleBuilder) SymlinkOutputs() WritablePaths {
|
|
|
|
symlinkOutputs := r.symlinkOutputSet()
|
|
|
|
|
|
|
|
var symlinkOutputList WritablePaths
|
|
|
|
for _, symlinkOutput := range symlinkOutputs {
|
|
|
|
symlinkOutputList = append(symlinkOutputList, symlinkOutput)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(symlinkOutputList, func(i, j int) bool {
|
|
|
|
return symlinkOutputList[i].String() < symlinkOutputList[j].String()
|
|
|
|
})
|
|
|
|
|
|
|
|
return symlinkOutputList
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:11:38 +02: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-29 23:33:06 +01: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 01:42:32 +01:00
|
|
|
// Installs returns the list of tuples passed to Install.
|
2019-02-11 23:11:09 +01:00
|
|
|
func (r *RuleBuilder) Installs() RuleBuilderInstalls {
|
|
|
|
return append(RuleBuilderInstalls(nil), r.installs...)
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
func (r *RuleBuilder) toolsSet() map[string]Path {
|
|
|
|
tools := make(map[string]Path)
|
2019-01-31 02:32:39 +01:00
|
|
|
for _, c := range r.commands {
|
2019-02-03 06:25:18 +01:00
|
|
|
for _, tool := range c.tools {
|
2019-02-15 19:39:37 +01:00
|
|
|
tools[tool.String()] = tool
|
2019-02-03 06:25:18 +01:00
|
|
|
}
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
2019-02-03 06:25:18 +01:00
|
|
|
|
2019-01-31 02:32:39 +01:00
|
|
|
return tools
|
|
|
|
}
|
|
|
|
|
2020-02-22 01:55:19 +01:00
|
|
|
// Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method. The
|
|
|
|
// list is sorted and duplicates removed.
|
2019-02-15 19:39:37 +01:00
|
|
|
func (r *RuleBuilder) Tools() Paths {
|
2019-02-03 06:25:18 +01:00
|
|
|
toolsSet := r.toolsSet()
|
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
var toolsList Paths
|
|
|
|
for _, tool := range toolsSet {
|
2019-02-03 06:25:18 +01:00
|
|
|
toolsList = append(toolsList, tool)
|
|
|
|
}
|
2019-02-15 19:39:37 +01:00
|
|
|
|
|
|
|
sort.Slice(toolsList, func(i, j int) bool {
|
|
|
|
return toolsList[i].String() < toolsList[j].String()
|
|
|
|
})
|
|
|
|
|
2019-02-03 06:25:18 +01:00
|
|
|
return toolsList
|
|
|
|
}
|
|
|
|
|
2019-07-11 19:59:15 +02:00
|
|
|
// RspFileInputs returns the list of paths that were passed to the RuleBuilderCommand.FlagWithRspFileInputList method.
|
|
|
|
func (r *RuleBuilder) RspFileInputs() Paths {
|
|
|
|
var rspFileInputs Paths
|
|
|
|
for _, c := range r.commands {
|
2021-03-20 00:22:12 +01:00
|
|
|
for _, rspFile := range c.rspFiles {
|
|
|
|
rspFileInputs = append(rspFileInputs, rspFile.paths...)
|
2019-07-11 19:59:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rspFileInputs
|
|
|
|
}
|
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
func (r *RuleBuilder) rspFiles() []rspFileAndPaths {
|
|
|
|
var rspFiles []rspFileAndPaths
|
2021-03-13 02:48:14 +01:00
|
|
|
for _, c := range r.commands {
|
2021-03-20 00:22:12 +01:00
|
|
|
rspFiles = append(rspFiles, c.rspFiles...)
|
2021-03-13 02:48:14 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
return rspFiles
|
2021-03-13 02:48:14 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 19:59:15 +02:00
|
|
|
// Commands returns a slice containing the built command line for each call to RuleBuilder.Command.
|
2019-01-31 02:32:39 +01:00
|
|
|
func (r *RuleBuilder) Commands() []string {
|
|
|
|
var commands []string
|
|
|
|
for _, c := range r.commands {
|
2019-07-11 19:59:15 +02:00
|
|
|
commands = append(commands, c.String())
|
|
|
|
}
|
|
|
|
return commands
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
// BuilderContext is a subset of ModuleContext and SingletonContext.
|
2019-02-02 01:41:11 +01:00
|
|
|
type BuilderContext interface {
|
|
|
|
PathContext
|
|
|
|
Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule
|
|
|
|
Build(PackageContext, BuildParams)
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
var _ BuilderContext = ModuleContext(nil)
|
|
|
|
var _ BuilderContext = SingletonContext(nil)
|
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
func (r *RuleBuilder) depFileMergerCmd(depFiles WritablePaths) *RuleBuilderCommand {
|
2019-04-12 20:11:38 +02:00
|
|
|
return r.Command().
|
2020-11-17 02:32:30 +01:00
|
|
|
BuiltTool("dep_fixer").
|
2019-04-12 20:11:38 +02:00
|
|
|
Inputs(depFiles.Paths())
|
2019-03-29 23:33:06 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
// Build adds the built command line to the build graph, with dependencies on Inputs and Tools, and output files for
|
|
|
|
// Outputs.
|
2020-11-17 02:32:30 +01:00
|
|
|
func (r *RuleBuilder) Build(name string, desc string) {
|
2019-03-29 23:33:06 +01:00
|
|
|
name = ninjaNameEscape(name)
|
|
|
|
|
2019-02-06 07:31:15 +01:00
|
|
|
if len(r.missingDeps) > 0 {
|
2020-11-17 02:32:30 +01:00
|
|
|
r.ctx.Build(pctx, BuildParams{
|
2019-02-06 07:31:15 +01:00
|
|
|
Rule: ErrorRule,
|
2019-02-15 19:39:37 +01:00
|
|
|
Outputs: r.Outputs(),
|
2019-02-06 07:31:15 +01:00
|
|
|
Description: desc,
|
|
|
|
Args: map[string]string{
|
|
|
|
"error": "missing dependencies: " + strings.Join(r.missingDeps, ", "),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-29 23:33:06 +01: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.
|
2020-11-17 02:32:30 +01:00
|
|
|
r.depFileMergerCmd(depFiles)
|
2019-04-12 20:11:38 +02:00
|
|
|
|
|
|
|
if r.sbox {
|
2020-11-12 17:29:30 +01:00
|
|
|
// Check for Rel() errors, as all depfiles should be in the output dir. Errors
|
|
|
|
// will be reported to the ctx.
|
2019-04-12 20:11:38 +02:00
|
|
|
for _, path := range depFiles[1:] {
|
2020-11-17 02:32:30 +01:00
|
|
|
Rel(r.ctx, r.outDir.String(), path.String())
|
2019-04-12 20:11:38 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-29 23:33:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:11:38 +02:00
|
|
|
tools := r.Tools()
|
2021-03-13 02:51:32 +01:00
|
|
|
commands := r.Commands()
|
2019-04-12 20:11:38 +02:00
|
|
|
outputs := r.Outputs()
|
2020-11-14 01:23:53 +01:00
|
|
|
inputs := r.Inputs()
|
2021-03-20 00:22:12 +01:00
|
|
|
rspFiles := r.rspFiles()
|
2019-04-12 20:11:38 +02:00
|
|
|
|
|
|
|
if len(commands) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(outputs) == 0 {
|
|
|
|
panic("No outputs specified from any Commands")
|
2019-03-29 23:33:06 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 19:59:15 +02:00
|
|
|
commandString := strings.Join(commands, " && ")
|
2019-04-12 20:11:38 +02:00
|
|
|
|
|
|
|
if r.sbox {
|
2020-11-12 17:29:30 +01:00
|
|
|
// If running the command inside sbox, write the rule data out to an sbox
|
|
|
|
// manifest.textproto.
|
|
|
|
manifest := sbox_proto.Manifest{}
|
|
|
|
command := sbox_proto.Command{}
|
|
|
|
manifest.Commands = append(manifest.Commands, &command)
|
|
|
|
command.Command = proto.String(commandString)
|
|
|
|
|
|
|
|
if depFile != nil {
|
|
|
|
manifest.OutputDepfile = proto.String(depFile.String())
|
|
|
|
}
|
|
|
|
|
2020-11-25 01:32:22 +01:00
|
|
|
// If sandboxing tools is enabled, add copy rules to the manifest to copy each tool
|
|
|
|
// into the sbox directory.
|
|
|
|
if r.sboxTools {
|
|
|
|
for _, tool := range tools {
|
|
|
|
command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
|
|
|
|
From: proto.String(tool.String()),
|
|
|
|
To: proto.String(sboxPathForToolRel(r.ctx, tool)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, c := range r.commands {
|
|
|
|
for _, tool := range c.packagedTools {
|
|
|
|
command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
|
|
|
|
From: proto.String(tool.srcPath.String()),
|
|
|
|
To: proto.String(sboxPathForPackagedToolRel(tool)),
|
|
|
|
Executable: proto.Bool(tool.executable),
|
|
|
|
})
|
|
|
|
tools = append(tools, tool.srcPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 02:52:23 +01:00
|
|
|
// If sandboxing inputs is enabled, add copy rules to the manifest to copy each input
|
|
|
|
// into the sbox directory.
|
|
|
|
if r.sboxInputs {
|
|
|
|
for _, input := range inputs {
|
|
|
|
command.CopyBefore = append(command.CopyBefore, &sbox_proto.Copy{
|
|
|
|
From: proto.String(input.String()),
|
|
|
|
To: proto.String(r.sboxPathForInputRel(input)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
// If using rsp files copy them and their contents into the sbox directory with
|
|
|
|
// the appropriate path mappings.
|
|
|
|
for _, rspFile := range rspFiles {
|
2021-03-23 21:44:30 +01:00
|
|
|
command.RspFiles = append(command.RspFiles, &sbox_proto.RspFile{
|
2021-03-20 00:22:12 +01:00
|
|
|
File: proto.String(rspFile.file.String()),
|
2021-03-23 21:44:30 +01:00
|
|
|
// These have to match the logic in sboxPathForInputRel
|
|
|
|
PathMappings: []*sbox_proto.PathMapping{
|
|
|
|
{
|
|
|
|
From: proto.String(r.outDir.String()),
|
|
|
|
To: proto.String(sboxOutSubDir),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
From: proto.String(PathForOutput(r.ctx).String()),
|
|
|
|
To: proto.String(sboxOutSubDir),
|
|
|
|
},
|
|
|
|
},
|
2021-03-13 02:52:23 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
command.Chdir = proto.Bool(true)
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
// Add copy rules to the manifest to copy each output file from the sbox directory.
|
2020-11-25 01:32:22 +01:00
|
|
|
// to the output directory after running the commands.
|
2019-04-12 20:11:38 +02:00
|
|
|
sboxOutputs := make([]string, len(outputs))
|
|
|
|
for i, output := range outputs {
|
2020-11-17 02:32:30 +01:00
|
|
|
rel := Rel(r.ctx, r.outDir.String(), output.String())
|
2020-11-12 17:29:30 +01:00
|
|
|
sboxOutputs[i] = filepath.Join(sboxOutDir, rel)
|
|
|
|
command.CopyAfter = append(command.CopyAfter, &sbox_proto.Copy{
|
|
|
|
From: proto.String(filepath.Join(sboxOutSubDir, rel)),
|
|
|
|
To: proto.String(output.String()),
|
|
|
|
})
|
2019-04-12 20:11:38 +02:00
|
|
|
}
|
|
|
|
|
2021-03-12 02:18:21 +01:00
|
|
|
// Outputs that were marked Temporary will not be checked that they are in the output
|
|
|
|
// directory by the loop above, check them here.
|
|
|
|
for path := range r.temporariesSet {
|
|
|
|
Rel(r.ctx, r.outDir.String(), path.String())
|
|
|
|
}
|
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
// Add a hash of the list of input files to the manifest so that the textproto file
|
|
|
|
// changes when the list of input files changes and causes the sbox rule that
|
|
|
|
// depends on it to rerun.
|
|
|
|
command.InputHash = proto.String(hashSrcFiles(inputs))
|
|
|
|
|
|
|
|
// Verify that the manifest textproto is not inside the sbox output directory, otherwise
|
|
|
|
// it will get deleted when the sbox rule clears its output directory.
|
2020-11-17 02:32:30 +01:00
|
|
|
_, manifestInOutDir := MaybeRel(r.ctx, r.outDir.String(), r.sboxManifestPath.String())
|
2020-11-12 17:29:30 +01:00
|
|
|
if manifestInOutDir {
|
2020-11-17 02:32:30 +01:00
|
|
|
ReportPathErrorf(r.ctx, "sbox rule %q manifestPath %q must not be in outputDir %q",
|
|
|
|
name, r.sboxManifestPath.String(), r.outDir.String())
|
2019-04-12 20:11:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
// Create a rule to write the manifest as a the textproto.
|
2021-05-24 23:24:12 +02:00
|
|
|
pbText, err := prototext.Marshal(&manifest)
|
|
|
|
if err != nil {
|
|
|
|
ReportPathErrorf(r.ctx, "sbox manifest failed to marshal: %q", err)
|
|
|
|
}
|
|
|
|
WriteFileRule(r.ctx, r.sboxManifestPath, string(pbText))
|
2020-11-12 17:29:30 +01:00
|
|
|
|
|
|
|
// Generate a new string to use as the command line of the sbox rule. This uses
|
|
|
|
// a RuleBuilderCommand as a convenience method of building the command line, then
|
|
|
|
// converts it to a string to replace commandString.
|
2020-11-17 02:32:30 +01:00
|
|
|
sboxCmd := &RuleBuilderCommand{
|
|
|
|
rule: &RuleBuilder{
|
|
|
|
ctx: r.ctx,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sboxCmd.Text("rm -rf").Output(r.outDir)
|
2020-11-12 17:29:30 +01:00
|
|
|
sboxCmd.Text("&&")
|
2020-11-17 02:32:30 +01:00
|
|
|
sboxCmd.BuiltTool("sbox").
|
|
|
|
Flag("--sandbox-path").Text(shared.TempDirForOutDir(PathForOutput(r.ctx).String())).
|
2020-11-12 17:29:30 +01:00
|
|
|
Flag("--manifest").Input(r.sboxManifestPath)
|
2019-04-12 20:11:38 +02:00
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
// Replace the command string, and add the sbox tool and manifest textproto to the
|
|
|
|
// dependencies of the final sbox rule.
|
2019-07-09 02:07:18 +02:00
|
|
|
commandString = sboxCmd.buf.String()
|
2019-04-12 20:11:38 +02:00
|
|
|
tools = append(tools, sboxCmd.tools...)
|
2020-11-12 17:29:30 +01:00
|
|
|
inputs = append(inputs, sboxCmd.inputs...)
|
2021-03-13 02:24:45 +01:00
|
|
|
|
|
|
|
if r.rbeParams != nil {
|
2021-03-23 21:44:30 +01:00
|
|
|
// RBE needs a list of input files to copy to the remote builder. For inputs already
|
|
|
|
// listed in an rsp file, pass the rsp file directly to rewrapper. For the rest,
|
|
|
|
// create a new rsp file to pass to rewrapper.
|
|
|
|
var remoteRspFiles Paths
|
|
|
|
var remoteInputs Paths
|
|
|
|
|
|
|
|
remoteInputs = append(remoteInputs, inputs...)
|
|
|
|
remoteInputs = append(remoteInputs, tools...)
|
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
for _, rspFile := range rspFiles {
|
|
|
|
remoteInputs = append(remoteInputs, rspFile.file)
|
|
|
|
remoteRspFiles = append(remoteRspFiles, rspFile.file)
|
2021-03-23 21:44:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(remoteInputs) > 0 {
|
|
|
|
inputsListFile := r.sboxManifestPath.ReplaceExtension(r.ctx, "rbe_inputs.list")
|
|
|
|
writeRspFileRule(r.ctx, inputsListFile, remoteInputs)
|
|
|
|
remoteRspFiles = append(remoteRspFiles, inputsListFile)
|
|
|
|
// Add the new rsp file as an extra input to the rule.
|
|
|
|
inputs = append(inputs, inputsListFile)
|
2021-03-13 02:24:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
r.rbeParams.OutputFiles = outputs.Strings()
|
2021-03-23 21:44:30 +01:00
|
|
|
r.rbeParams.RSPFiles = remoteRspFiles.Strings()
|
2021-03-13 02:24:45 +01:00
|
|
|
rewrapperCommand := r.rbeParams.NoVarTemplate(r.ctx.Config().RBEWrapper())
|
|
|
|
commandString = rewrapperCommand + " bash -c '" + strings.ReplaceAll(commandString, `'`, `'\''`) + "'"
|
|
|
|
}
|
2020-11-14 01:23:53 +01:00
|
|
|
} else {
|
|
|
|
// If not using sbox the rule will run the command directly, put the hash of the
|
|
|
|
// list of input files in a comment at the end of the command line to ensure ninja
|
|
|
|
// reruns the rule when the list of input files changes.
|
|
|
|
commandString += " # hash of input list: " + hashSrcFiles(inputs)
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
2019-04-12 20:11:38 +02:00
|
|
|
|
|
|
|
// Ninja doesn't like multiple outputs when depfiles are enabled, move all but the first output to
|
2021-03-13 02:48:14 +01:00
|
|
|
// ImplicitOutputs. RuleBuilder doesn't use "$out", so the distinction between Outputs and
|
2019-07-11 19:59:15 +02:00
|
|
|
// ImplicitOutputs doesn't matter.
|
2019-04-12 20:11:38 +02:00
|
|
|
output := outputs[0]
|
|
|
|
implicitOutputs := outputs[1:]
|
|
|
|
|
2019-07-11 19:59:15 +02:00
|
|
|
var rspFile, rspFileContent string
|
2021-03-20 00:22:12 +01:00
|
|
|
var rspFileInputs Paths
|
|
|
|
if len(rspFiles) > 0 {
|
|
|
|
// The first rsp files uses Ninja's rsp file support for the rule
|
|
|
|
rspFile = rspFiles[0].file.String()
|
2021-03-23 21:44:30 +01:00
|
|
|
// Use "$in" for rspFileContent to avoid duplicating the list of files in the dependency
|
|
|
|
// list and in the contents of the rsp file. Inputs to the rule that are not in the
|
|
|
|
// rsp file will be listed in Implicits instead of Inputs so they don't show up in "$in".
|
|
|
|
rspFileContent = "$in"
|
2021-03-20 00:22:12 +01:00
|
|
|
rspFileInputs = append(rspFileInputs, rspFiles[0].paths...)
|
|
|
|
|
|
|
|
for _, rspFile := range rspFiles[1:] {
|
|
|
|
// Any additional rsp files need an extra rule to write the file.
|
|
|
|
writeRspFileRule(r.ctx, rspFile.file, rspFile.paths)
|
|
|
|
// The main rule needs to depend on the inputs listed in the extra rsp file.
|
|
|
|
inputs = append(inputs, rspFile.paths...)
|
|
|
|
// The main rule needs to depend on the extra rsp file.
|
|
|
|
inputs = append(inputs, rspFile.file)
|
|
|
|
}
|
2019-07-11 19:59:15 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 22:18:43 +01:00
|
|
|
var pool blueprint.Pool
|
2020-11-17 02:32:30 +01:00
|
|
|
if r.ctx.Config().UseGoma() && r.remoteable.Goma {
|
2019-11-15 22:18:43 +01:00
|
|
|
// When USE_GOMA=true is set and the rule is supported by goma, allow jobs to run outside the local pool.
|
2020-11-17 02:32:30 +01:00
|
|
|
} else if r.ctx.Config().UseRBE() && r.remoteable.RBE {
|
2020-04-01 04:14:52 +02:00
|
|
|
// When USE_RBE=true is set and the rule is supported by RBE, use the remotePool.
|
|
|
|
pool = remotePool
|
2019-11-15 22:18:43 +01:00
|
|
|
} else if r.highmem {
|
|
|
|
pool = highmemPool
|
2020-11-17 02:32:30 +01:00
|
|
|
} else if r.ctx.Config().UseRemoteBuild() {
|
2019-11-15 22:18:43 +01:00
|
|
|
pool = localPool
|
|
|
|
}
|
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
r.ctx.Build(r.pctx, BuildParams{
|
|
|
|
Rule: r.ctx.Rule(pctx, name, blueprint.RuleParams{
|
2021-03-13 02:51:32 +01:00
|
|
|
Command: proptools.NinjaEscape(commandString),
|
2021-03-17 00:49:52 +01:00
|
|
|
CommandDeps: proptools.NinjaEscapeList(tools.Strings()),
|
2019-07-11 19:59:15 +02:00
|
|
|
Restat: r.restat,
|
2021-03-17 00:49:52 +01:00
|
|
|
Rspfile: proptools.NinjaEscape(rspFile),
|
2019-07-11 19:59:15 +02:00
|
|
|
RspfileContent: rspFileContent,
|
2019-11-15 22:18:43 +01:00
|
|
|
Pool: pool,
|
2019-04-12 20:11:38 +02:00
|
|
|
}),
|
2019-07-11 19:59:15 +02:00
|
|
|
Inputs: rspFileInputs,
|
2020-11-14 01:23:53 +01:00
|
|
|
Implicits: inputs,
|
2021-04-21 20:32:19 +02:00
|
|
|
OrderOnly: r.OrderOnlys(),
|
2021-04-21 20:45:23 +02:00
|
|
|
Validations: r.Validations(),
|
2019-04-12 20:11:38 +02:00
|
|
|
Output: output,
|
|
|
|
ImplicitOutputs: implicitOutputs,
|
2020-09-23 06:30:02 +02:00
|
|
|
SymlinkOutputs: r.SymlinkOutputs(),
|
2019-04-12 20:11:38 +02:00
|
|
|
Depfile: depFile,
|
|
|
|
Deps: depFormat,
|
|
|
|
Description: desc,
|
|
|
|
})
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01: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 02:32:39 +01:00
|
|
|
type RuleBuilderCommand struct {
|
2020-11-17 02:32:30 +01:00
|
|
|
rule *RuleBuilder
|
|
|
|
|
2020-09-23 06:30:02 +02:00
|
|
|
buf strings.Builder
|
|
|
|
inputs Paths
|
|
|
|
implicits Paths
|
|
|
|
orderOnlys Paths
|
2021-04-21 20:45:23 +02:00
|
|
|
validations Paths
|
2020-09-23 06:30:02 +02:00
|
|
|
outputs WritablePaths
|
|
|
|
symlinkOutputs WritablePaths
|
|
|
|
depFiles WritablePaths
|
|
|
|
tools Paths
|
2020-11-25 01:32:22 +01:00
|
|
|
packagedTools []PackagingSpec
|
2021-03-20 00:22:12 +01:00
|
|
|
rspFiles []rspFileAndPaths
|
|
|
|
}
|
|
|
|
|
|
|
|
type rspFileAndPaths struct {
|
|
|
|
file WritablePath
|
|
|
|
paths Paths
|
2019-04-12 20:11:38 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 12:24:48 +02:00
|
|
|
func checkPathNotNil(path Path) {
|
|
|
|
if path == nil {
|
|
|
|
panic("rule_builder paths cannot be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:11:38 +02:00
|
|
|
func (c *RuleBuilderCommand) addInput(path Path) string {
|
2021-10-04 12:24:48 +02:00
|
|
|
checkPathNotNil(path)
|
2019-04-12 20:11:38 +02:00
|
|
|
c.inputs = append(c.inputs, path)
|
2021-03-13 02:52:23 +01:00
|
|
|
return c.PathForInput(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RuleBuilderCommand) addImplicit(path Path) {
|
2021-10-04 12:24:48 +02:00
|
|
|
checkPathNotNil(path)
|
2021-03-13 02:52:23 +01:00
|
|
|
c.implicits = append(c.implicits, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RuleBuilderCommand) addOrderOnly(path Path) {
|
2021-10-04 12:24:48 +02:00
|
|
|
checkPathNotNil(path)
|
2021-03-13 02:52:23 +01:00
|
|
|
c.orderOnlys = append(c.orderOnlys, path)
|
2019-04-12 20:11:38 +02:00
|
|
|
}
|
|
|
|
|
2021-03-13 02:52:23 +01:00
|
|
|
// PathForInput takes an input path and returns the appropriate path to use on the command line. If
|
|
|
|
// sbox was enabled via a call to RuleBuilder.Sbox() and the path was an output path it returns a
|
|
|
|
// path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the
|
|
|
|
// original path.
|
|
|
|
func (c *RuleBuilderCommand) PathForInput(path Path) string {
|
2020-11-17 02:32:30 +01:00
|
|
|
if c.rule.sbox {
|
2021-03-13 02:52:23 +01:00
|
|
|
rel, inSandbox := c.rule._sboxPathForInputRel(path)
|
|
|
|
if inSandbox {
|
|
|
|
rel = filepath.Join(sboxSandboxBaseDir, rel)
|
2020-06-13 23:38:27 +02:00
|
|
|
}
|
2021-03-13 02:52:23 +01:00
|
|
|
return rel
|
2020-06-13 23:38:27 +02:00
|
|
|
}
|
|
|
|
return path.String()
|
|
|
|
}
|
|
|
|
|
2021-03-13 02:52:23 +01:00
|
|
|
// PathsForInputs takes a list of input paths and returns the appropriate paths to use on the
|
|
|
|
// command line. If sbox was enabled via a call to RuleBuilder.Sbox() a path was an output path, it
|
|
|
|
// returns the path with the placeholder prefix used for outputs in sbox. If sbox is not enabled it
|
|
|
|
// returns the original paths.
|
|
|
|
func (c *RuleBuilderCommand) PathsForInputs(paths Paths) []string {
|
|
|
|
ret := make([]string, len(paths))
|
|
|
|
for i, path := range paths {
|
|
|
|
ret[i] = c.PathForInput(path)
|
|
|
|
}
|
|
|
|
return ret
|
2020-02-22 01:55:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
// PathForOutput takes an output path and returns the appropriate path to use on the command
|
|
|
|
// line. If sbox was enabled via a call to RuleBuilder.Sbox(), it returns a path with the
|
|
|
|
// placeholder prefix used for outputs in sbox. If sbox is not enabled it returns the
|
|
|
|
// original path.
|
|
|
|
func (c *RuleBuilderCommand) PathForOutput(path WritablePath) string {
|
|
|
|
if c.rule.sbox {
|
|
|
|
// Errors will be handled in RuleBuilder.Build where we have a context to report them
|
|
|
|
rel, _, _ := maybeRelErr(c.rule.outDir.String(), path.String())
|
|
|
|
return filepath.Join(sboxOutDir, rel)
|
2019-04-12 20:11:38 +02:00
|
|
|
}
|
|
|
|
return path.String()
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 01:32:22 +01:00
|
|
|
func sboxPathForToolRel(ctx BuilderContext, path Path) string {
|
|
|
|
// Errors will be handled in RuleBuilder.Build where we have a context to report them
|
2021-10-26 04:15:55 +02:00
|
|
|
toolDir := pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "", false)
|
|
|
|
relOutSoong, isRelOutSoong, _ := maybeRelErr(toolDir.String(), path.String())
|
|
|
|
if isRelOutSoong {
|
|
|
|
// The tool is in the Soong output directory, it will be copied to __SBOX_OUT_DIR__/tools/out
|
|
|
|
return filepath.Join(sboxToolsSubDir, "out", relOutSoong)
|
2020-11-25 01:32:22 +01:00
|
|
|
}
|
2021-10-26 00:40:32 +02:00
|
|
|
if ctx.Config().KatiEnabled() {
|
|
|
|
toolDir = toolDir.ToMakePath()
|
|
|
|
relOut, isRelOut, _ := maybeRelErr(toolDir.String(), path.String())
|
|
|
|
if isRelOut {
|
|
|
|
// The tool is in the Make output directory, it will be copied to __SBOX_OUT_DIR__/tools/out
|
|
|
|
return filepath.Join(sboxToolsSubDir, "out", relOut)
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 01:32:22 +01:00
|
|
|
// The tool is in the source directory, it will be copied to __SBOX_OUT_DIR__/tools/src
|
|
|
|
return filepath.Join(sboxToolsSubDir, "src", path.String())
|
|
|
|
}
|
|
|
|
|
2021-03-13 02:52:23 +01:00
|
|
|
func (r *RuleBuilder) _sboxPathForInputRel(path Path) (rel string, inSandbox bool) {
|
|
|
|
// Errors will be handled in RuleBuilder.Build where we have a context to report them
|
|
|
|
rel, isRelSboxOut, _ := maybeRelErr(r.outDir.String(), path.String())
|
|
|
|
if isRelSboxOut {
|
|
|
|
return filepath.Join(sboxOutSubDir, rel), true
|
|
|
|
}
|
|
|
|
if r.sboxInputs {
|
|
|
|
// When sandboxing inputs all inputs have to be copied into the sandbox. Input files that
|
|
|
|
// are outputs of other rules could be an arbitrary absolute path if OUT_DIR is set, so they
|
|
|
|
// will be copied to relative paths under __SBOX_OUT_DIR__/out.
|
|
|
|
rel, isRelOut, _ := maybeRelErr(PathForOutput(r.ctx).String(), path.String())
|
|
|
|
if isRelOut {
|
|
|
|
return filepath.Join(sboxOutSubDir, rel), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path.String(), false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuleBuilder) sboxPathForInputRel(path Path) string {
|
|
|
|
rel, _ := r._sboxPathForInputRel(path)
|
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RuleBuilder) sboxPathsForInputsRel(paths Paths) []string {
|
|
|
|
ret := make([]string, len(paths))
|
|
|
|
for i, path := range paths {
|
|
|
|
ret[i] = r.sboxPathForInputRel(path)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-03-24 06:30:35 +01:00
|
|
|
func sboxPathForPackagedToolRel(spec PackagingSpec) string {
|
|
|
|
return filepath.Join(sboxToolsSubDir, "out", spec.relPathInPackage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PathForPackagedTool takes a PackageSpec for a tool and returns the corresponding path for the
|
2020-11-25 01:32:22 +01:00
|
|
|
// tool after copying it into the sandbox. This can be used on the RuleBuilder command line to
|
|
|
|
// reference the tool.
|
2021-03-24 06:30:35 +01:00
|
|
|
func (c *RuleBuilderCommand) PathForPackagedTool(spec PackagingSpec) string {
|
|
|
|
if !c.rule.sboxTools {
|
|
|
|
panic("PathForPackagedTool() requires SandboxTools()")
|
|
|
|
}
|
2020-11-25 01:32:22 +01:00
|
|
|
|
2021-03-24 06:30:35 +01:00
|
|
|
return filepath.Join(sboxSandboxBaseDir, sboxPathForPackagedToolRel(spec))
|
2020-11-25 01:32:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// PathForTool takes a path to a tool, which may be an output file or a source file, and returns
|
|
|
|
// the corresponding path for the tool in the sbox sandbox if sbox is enabled, or the original path
|
|
|
|
// if it is not. This can be used on the RuleBuilder command line to reference the tool.
|
|
|
|
func (c *RuleBuilderCommand) PathForTool(path Path) string {
|
|
|
|
if c.rule.sbox && c.rule.sboxTools {
|
|
|
|
return filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(c.rule.ctx, path))
|
|
|
|
}
|
|
|
|
return path.String()
|
|
|
|
}
|
|
|
|
|
2021-03-24 06:30:35 +01:00
|
|
|
// PathsForTools takes a list of paths to tools, which may be output files or source files, and
|
|
|
|
// returns the corresponding paths for the tools in the sbox sandbox if sbox is enabled, or the
|
|
|
|
// original paths if it is not. This can be used on the RuleBuilder command line to reference the tool.
|
|
|
|
func (c *RuleBuilderCommand) PathsForTools(paths Paths) []string {
|
|
|
|
if c.rule.sbox && c.rule.sboxTools {
|
|
|
|
var ret []string
|
|
|
|
for _, path := range paths {
|
|
|
|
ret = append(ret, filepath.Join(sboxSandboxBaseDir, sboxPathForToolRel(c.rule.ctx, path)))
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
return paths.Strings()
|
|
|
|
}
|
|
|
|
|
2020-11-25 01:32:22 +01:00
|
|
|
// PackagedTool adds the specified tool path to the command line. It can only be used with tool
|
|
|
|
// sandboxing enabled by SandboxTools(), and will copy the tool into the sandbox.
|
|
|
|
func (c *RuleBuilderCommand) PackagedTool(spec PackagingSpec) *RuleBuilderCommand {
|
|
|
|
if !c.rule.sboxTools {
|
|
|
|
panic("PackagedTool() requires SandboxTools()")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.packagedTools = append(c.packagedTools, spec)
|
|
|
|
c.Text(sboxPathForPackagedToolRel(spec))
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImplicitPackagedTool copies the specified tool into the sandbox without modifying the command
|
|
|
|
// line. It can only be used with tool sandboxing enabled by SandboxTools().
|
|
|
|
func (c *RuleBuilderCommand) ImplicitPackagedTool(spec PackagingSpec) *RuleBuilderCommand {
|
|
|
|
if !c.rule.sboxTools {
|
|
|
|
panic("ImplicitPackagedTool() requires SandboxTools()")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.packagedTools = append(c.packagedTools, spec)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImplicitPackagedTools copies the specified tools into the sandbox without modifying the command
|
|
|
|
// line. It can only be used with tool sandboxing enabled by SandboxTools().
|
|
|
|
func (c *RuleBuilderCommand) ImplicitPackagedTools(specs []PackagingSpec) *RuleBuilderCommand {
|
|
|
|
if !c.rule.sboxTools {
|
|
|
|
panic("ImplicitPackagedTools() requires SandboxTools()")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.packagedTools = append(c.packagedTools, specs...)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01: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 02:32:39 +01:00
|
|
|
func (c *RuleBuilderCommand) Text(text string) *RuleBuilderCommand {
|
2019-07-09 02:07:18 +02:00
|
|
|
if c.buf.Len() > 0 {
|
|
|
|
c.buf.WriteByte(' ')
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
2019-07-09 02:07:18 +02:00
|
|
|
c.buf.WriteString(text)
|
2019-01-31 02:32:39 +01:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01: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 02:32:39 +01:00
|
|
|
func (c *RuleBuilderCommand) Textf(format string, a ...interface{}) *RuleBuilderCommand {
|
|
|
|
return c.Text(fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01: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 02:32:39 +01:00
|
|
|
func (c *RuleBuilderCommand) Flag(flag string) *RuleBuilderCommand {
|
|
|
|
return c.Text(flag)
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:13:59 +02:00
|
|
|
// OptionalFlag adds the specified raw text to the command line if it is not nil. 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) OptionalFlag(flag *string) *RuleBuilderCommand {
|
|
|
|
if flag != nil {
|
|
|
|
c.Text(*flag)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-03-29 23:32:51 +01: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 01:42:32 +01: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 02:32:39 +01:00
|
|
|
func (c *RuleBuilderCommand) FlagWithArg(flag, arg string) *RuleBuilderCommand {
|
|
|
|
return c.Text(flag + arg)
|
|
|
|
}
|
|
|
|
|
2019-02-11 23:11:09 +01: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-27 17:56:41 +01:00
|
|
|
// FlagWithList adds the specified flag and list of arguments to the command line, with the arguments joined by sep
|
2019-02-02 01:42:32 +01: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 02:32:39 +01:00
|
|
|
func (c *RuleBuilderCommand) FlagWithList(flag string, list []string, sep string) *RuleBuilderCommand {
|
|
|
|
return c.Text(flag + strings.Join(list, sep))
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01: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-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) Tool(path Path) *RuleBuilderCommand {
|
2021-10-04 12:24:48 +02:00
|
|
|
checkPathNotNil(path)
|
2019-01-31 02:32:39 +01:00
|
|
|
c.tools = append(c.tools, path)
|
2020-11-25 01:32:22 +01:00
|
|
|
return c.Text(c.PathForTool(path))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools.
|
|
|
|
func (c *RuleBuilderCommand) ImplicitTool(path Path) *RuleBuilderCommand {
|
2021-10-04 12:24:48 +02:00
|
|
|
checkPathNotNil(path)
|
2020-11-25 01:32:22 +01:00
|
|
|
c.tools = append(c.tools, path)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tool adds the specified tool path to the dependencies returned by RuleBuilder.Tools.
|
|
|
|
func (c *RuleBuilderCommand) ImplicitTools(paths Paths) *RuleBuilderCommand {
|
2021-10-04 12:24:48 +02:00
|
|
|
for _, path := range paths {
|
|
|
|
c.ImplicitTool(path)
|
|
|
|
}
|
2020-11-25 01:32:22 +01:00
|
|
|
return c
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 02:08:34 +02: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))
|
2020-11-17 02:32:30 +01:00
|
|
|
func (c *RuleBuilderCommand) BuiltTool(tool string) *RuleBuilderCommand {
|
|
|
|
return c.Tool(c.rule.ctx.Config().HostToolPath(c.rule.ctx, tool))
|
2019-07-09 02:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 01:42:32 +01: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-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) Input(path Path) *RuleBuilderCommand {
|
2019-04-12 20:11:38 +02:00
|
|
|
return c.Text(c.addInput(path))
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01: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-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) Inputs(paths Paths) *RuleBuilderCommand {
|
2019-02-02 01:42:32 +01: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-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) Implicit(path Path) *RuleBuilderCommand {
|
2020-06-13 23:38:27 +02:00
|
|
|
c.addImplicit(path)
|
2019-01-31 02:32:39 +01:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
// Implicits adds the specified input paths to the dependencies returned by RuleBuilder.Inputs without modifying the
|
|
|
|
// command line.
|
2019-02-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) Implicits(paths Paths) *RuleBuilderCommand {
|
2019-04-12 20:11:38 +02:00
|
|
|
for _, path := range paths {
|
2020-06-13 23:38:27 +02:00
|
|
|
c.addImplicit(path)
|
2019-04-12 20:11:38 +02:00
|
|
|
}
|
2019-01-31 02:32:39 +01:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-06-13 23:38:27 +02:00
|
|
|
// GetImplicits returns the command's implicit inputs.
|
|
|
|
func (c *RuleBuilderCommand) GetImplicits() Paths {
|
|
|
|
return c.implicits
|
|
|
|
}
|
|
|
|
|
2020-02-22 01:55:19 +01:00
|
|
|
// OrderOnly adds the specified input path to the dependencies returned by RuleBuilder.OrderOnlys
|
|
|
|
// without modifying the command line.
|
|
|
|
func (c *RuleBuilderCommand) OrderOnly(path Path) *RuleBuilderCommand {
|
|
|
|
c.addOrderOnly(path)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// OrderOnlys adds the specified input paths to the dependencies returned by RuleBuilder.OrderOnlys
|
|
|
|
// without modifying the command line.
|
|
|
|
func (c *RuleBuilderCommand) OrderOnlys(paths Paths) *RuleBuilderCommand {
|
|
|
|
for _, path := range paths {
|
|
|
|
c.addOrderOnly(path)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-04-21 20:45:23 +02:00
|
|
|
// Validation adds the specified input path to the validation dependencies by
|
|
|
|
// RuleBuilder.Validations without modifying the command line.
|
|
|
|
func (c *RuleBuilderCommand) Validation(path Path) *RuleBuilderCommand {
|
2021-10-04 12:24:48 +02:00
|
|
|
checkPathNotNil(path)
|
2021-04-21 20:45:23 +02:00
|
|
|
c.validations = append(c.validations, path)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validations adds the specified input paths to the validation dependencies by
|
|
|
|
// RuleBuilder.Validations without modifying the command line.
|
|
|
|
func (c *RuleBuilderCommand) Validations(paths Paths) *RuleBuilderCommand {
|
2021-10-04 12:24:48 +02:00
|
|
|
for _, path := range paths {
|
|
|
|
c.Validation(path)
|
|
|
|
}
|
2021-04-21 20:45:23 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01: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-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) Output(path WritablePath) *RuleBuilderCommand {
|
2021-10-04 12:24:48 +02:00
|
|
|
checkPathNotNil(path)
|
2019-01-31 02:32:39 +01:00
|
|
|
c.outputs = append(c.outputs, path)
|
2020-11-17 02:32:30 +01:00
|
|
|
return c.Text(c.PathForOutput(path))
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01: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-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) Outputs(paths WritablePaths) *RuleBuilderCommand {
|
2019-02-02 01:42:32 +01:00
|
|
|
for _, path := range paths {
|
|
|
|
c.Output(path)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-06-05 02:10:41 +02: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 {
|
2020-11-17 02:32:30 +01:00
|
|
|
if !c.rule.sbox {
|
2019-06-05 02:10:41 +02:00
|
|
|
panic("OutputDir only valid with Sbox")
|
|
|
|
}
|
2020-11-14 01:23:53 +01:00
|
|
|
return c.Text(sboxOutDir)
|
2019-06-05 02:10:41 +02:00
|
|
|
}
|
|
|
|
|
2019-03-29 23:33:06 +01: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 {
|
2021-10-04 12:24:48 +02:00
|
|
|
checkPathNotNil(path)
|
2019-03-29 23:33:06 +01:00
|
|
|
c.depFiles = append(c.depFiles, path)
|
2020-11-17 02:32:30 +01:00
|
|
|
return c.Text(c.PathForOutput(path))
|
2019-03-29 23:33:06 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
// ImplicitOutput adds the specified output path to the dependencies returned by RuleBuilder.Outputs without modifying
|
|
|
|
// the command line.
|
2019-02-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) ImplicitOutput(path WritablePath) *RuleBuilderCommand {
|
2019-01-31 02:32:39 +01:00
|
|
|
c.outputs = append(c.outputs, path)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
// ImplicitOutputs adds the specified output paths to the dependencies returned by RuleBuilder.Outputs without modifying
|
|
|
|
// the command line.
|
2019-02-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) ImplicitOutputs(paths WritablePaths) *RuleBuilderCommand {
|
2019-02-02 01:42:32 +01:00
|
|
|
c.outputs = append(c.outputs, paths...)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-09-23 06:30:02 +02:00
|
|
|
// ImplicitSymlinkOutput declares the specified path as an implicit output that
|
|
|
|
// will be a symlink instead of a regular file. Does not modify the command
|
|
|
|
// line.
|
|
|
|
func (c *RuleBuilderCommand) ImplicitSymlinkOutput(path WritablePath) *RuleBuilderCommand {
|
2021-10-04 12:24:48 +02:00
|
|
|
checkPathNotNil(path)
|
2020-09-23 06:30:02 +02:00
|
|
|
c.symlinkOutputs = append(c.symlinkOutputs, path)
|
|
|
|
return c.ImplicitOutput(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImplicitSymlinkOutputs declares the specified paths as implicit outputs that
|
|
|
|
// will be a symlinks instead of regular files. Does not modify the command
|
|
|
|
// line.
|
|
|
|
func (c *RuleBuilderCommand) ImplicitSymlinkOutputs(paths WritablePaths) *RuleBuilderCommand {
|
|
|
|
for _, path := range paths {
|
|
|
|
c.ImplicitSymlinkOutput(path)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SymlinkOutput declares the specified path as an output that will be a symlink
|
|
|
|
// instead of a regular file. Modifies the command line.
|
|
|
|
func (c *RuleBuilderCommand) SymlinkOutput(path WritablePath) *RuleBuilderCommand {
|
2021-10-04 12:24:48 +02:00
|
|
|
checkPathNotNil(path)
|
2020-09-23 06:30:02 +02:00
|
|
|
c.symlinkOutputs = append(c.symlinkOutputs, path)
|
|
|
|
return c.Output(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SymlinkOutputsl declares the specified paths as outputs that will be symlinks
|
|
|
|
// instead of regular files. Modifies the command line.
|
|
|
|
func (c *RuleBuilderCommand) SymlinkOutputs(paths WritablePaths) *RuleBuilderCommand {
|
|
|
|
for _, path := range paths {
|
|
|
|
c.SymlinkOutput(path)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-03-29 23:33:06 +01: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 01:42:32 +01: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-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) FlagWithInput(flag string, path Path) *RuleBuilderCommand {
|
2019-04-12 20:11:38 +02:00
|
|
|
return c.Text(flag + c.addInput(path))
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01: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-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) FlagWithInputList(flag string, paths Paths, sep string) *RuleBuilderCommand {
|
2019-04-12 20:11:38 +02: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 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01: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-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) FlagForEachInput(flag string, paths Paths) *RuleBuilderCommand {
|
2019-02-02 01:42:32 +01: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-15 19:39:37 +01:00
|
|
|
func (c *RuleBuilderCommand) FlagWithOutput(flag string, path WritablePath) *RuleBuilderCommand {
|
2019-01-31 02:32:39 +01:00
|
|
|
c.outputs = append(c.outputs, path)
|
2020-11-17 02:32:30 +01:00
|
|
|
return c.Text(flag + c.PathForOutput(path))
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 23:33:06 +01: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)
|
2020-11-17 02:32:30 +01:00
|
|
|
return c.Text(flag + c.PathForOutput(path))
|
2019-03-29 23:33:06 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
// FlagWithRspFileInputList adds the specified flag and path to an rspfile to the command line, with
|
|
|
|
// no separator between them. The paths will be written to the rspfile. If sbox is enabled, the
|
|
|
|
// rspfile must be outside the sbox directory. The first use of FlagWithRspFileInputList in any
|
|
|
|
// RuleBuilderCommand of a RuleBuilder will use Ninja's rsp file support for the rule, additional
|
|
|
|
// uses will result in an auxiliary rules to write the rspFile contents.
|
2021-03-13 02:48:14 +01:00
|
|
|
func (c *RuleBuilderCommand) FlagWithRspFileInputList(flag string, rspFile WritablePath, paths Paths) *RuleBuilderCommand {
|
2019-07-11 19:59:15 +02:00
|
|
|
// Use an empty slice if paths is nil, the non-nil slice is used as an indicator that the rsp file must be
|
|
|
|
// generated.
|
|
|
|
if paths == nil {
|
|
|
|
paths = Paths{}
|
|
|
|
}
|
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
c.rspFiles = append(c.rspFiles, rspFileAndPaths{rspFile, paths})
|
2021-03-13 02:48:14 +01:00
|
|
|
|
|
|
|
if c.rule.sbox {
|
|
|
|
if _, isRel, _ := maybeRelErr(c.rule.outDir.String(), rspFile.String()); isRel {
|
|
|
|
panic(fmt.Errorf("FlagWithRspFileInputList rspfile %q must not be inside out dir %q",
|
|
|
|
rspFile.String(), c.rule.outDir.String()))
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 19:59:15 +02:00
|
|
|
|
2021-03-13 02:52:23 +01:00
|
|
|
c.FlagWithArg(flag, c.PathForInput(rspFile))
|
2019-07-11 19:59:15 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
// String returns the command line.
|
|
|
|
func (c *RuleBuilderCommand) String() string {
|
2019-07-09 02:07:18 +02:00
|
|
|
return c.buf.String()
|
2019-02-02 01:42:32 +01:00
|
|
|
}
|
2019-03-29 23:33:06 +01:00
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
// RuleBuilderSboxProtoForTests takes the BuildParams for the manifest passed to RuleBuilder.Sbox()
|
|
|
|
// and returns sbox testproto generated by the RuleBuilder.
|
|
|
|
func RuleBuilderSboxProtoForTests(t *testing.T, params TestingBuildParams) *sbox_proto.Manifest {
|
|
|
|
t.Helper()
|
|
|
|
content := ContentFromFileRuleForTests(t, params)
|
|
|
|
manifest := sbox_proto.Manifest{}
|
2021-05-24 23:24:12 +02:00
|
|
|
err := prototext.Unmarshal([]byte(content), &manifest)
|
2020-11-12 17:29:30 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal manifest: %s", err.Error())
|
|
|
|
}
|
|
|
|
return &manifest
|
|
|
|
}
|
|
|
|
|
2019-03-29 23:33:06 +01: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
|
|
|
|
}
|
2020-11-14 01:23:53 +01:00
|
|
|
|
|
|
|
// hashSrcFiles returns a hash of the list of source files. It is used to ensure the command line
|
|
|
|
// or the sbox textproto manifest change even if the input files are not listed on the command line.
|
|
|
|
func hashSrcFiles(srcFiles Paths) string {
|
|
|
|
h := sha256.New()
|
|
|
|
srcFileList := strings.Join(srcFiles.Strings(), "\n")
|
|
|
|
h.Write([]byte(srcFileList))
|
|
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
}
|
2020-11-17 02:32:30 +01:00
|
|
|
|
|
|
|
// BuilderContextForTesting returns a BuilderContext for the given config that can be used for tests
|
|
|
|
// that need to call methods that take a BuilderContext.
|
|
|
|
func BuilderContextForTesting(config Config) BuilderContext {
|
|
|
|
pathCtx := PathContextForTesting(config)
|
|
|
|
return builderContextForTests{
|
|
|
|
PathContext: pathCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type builderContextForTests struct {
|
|
|
|
PathContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func (builderContextForTests) Rule(PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (builderContextForTests) Build(PackageContext, BuildParams) {}
|
2021-03-13 02:24:45 +01:00
|
|
|
|
2021-03-23 21:44:30 +01:00
|
|
|
func writeRspFileRule(ctx BuilderContext, rspFile WritablePath, paths Paths) {
|
|
|
|
buf := &strings.Builder{}
|
|
|
|
err := response.WriteRspFile(buf, paths.Strings())
|
|
|
|
if err != nil {
|
|
|
|
// There should never be I/O errors writing to a bytes.Buffer.
|
|
|
|
panic(err)
|
2021-03-13 02:24:45 +01:00
|
|
|
}
|
2021-03-23 21:44:30 +01:00
|
|
|
WriteFileRule(ctx, rspFile, buf.String())
|
2021-03-13 02:24:45 +01:00
|
|
|
}
|