2020-12-11 06:19:18 +08:00
|
|
|
// Copyright 2020 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 bazel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-12-24 01:02:11 +08:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2020-12-11 06:19:18 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
)
|
|
|
|
|
|
|
|
// artifact contains relevant portions of Bazel's aquery proto, Artifact.
|
|
|
|
// Represents a single artifact, whether it's a source file or a derived output file.
|
|
|
|
type artifact struct {
|
2020-12-24 01:02:11 +08:00
|
|
|
Id int
|
|
|
|
PathFragmentId int
|
|
|
|
}
|
|
|
|
|
|
|
|
type pathFragment struct {
|
|
|
|
Id int
|
|
|
|
Label string
|
|
|
|
ParentId int
|
2020-12-11 06:19:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// KeyValuePair represents Bazel's aquery proto, KeyValuePair.
|
|
|
|
type KeyValuePair struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// depSetOfFiles contains relevant portions of Bazel's aquery proto, DepSetOfFiles.
|
|
|
|
// Represents a data structure containing one or more files. Depsets in Bazel are an efficient
|
|
|
|
// data structure for storing large numbers of file paths.
|
|
|
|
type depSetOfFiles struct {
|
2021-01-20 00:36:50 +08:00
|
|
|
Id int
|
|
|
|
DirectArtifactIds []int
|
|
|
|
TransitiveDepSetIds []int
|
2020-12-11 06:19:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// action contains relevant portions of Bazel's aquery proto, Action.
|
|
|
|
// Represents a single command line invocation in the Bazel build graph.
|
|
|
|
type action struct {
|
|
|
|
Arguments []string
|
|
|
|
EnvironmentVariables []KeyValuePair
|
2020-12-24 01:02:11 +08:00
|
|
|
InputDepSetIds []int
|
2020-12-11 06:19:18 +08:00
|
|
|
Mnemonic string
|
2020-12-24 01:02:11 +08:00
|
|
|
OutputIds []int
|
2020-12-11 06:19:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// actionGraphContainer contains relevant portions of Bazel's aquery proto, ActionGraphContainer.
|
|
|
|
// An aquery response from Bazel contains a single ActionGraphContainer proto.
|
|
|
|
type actionGraphContainer struct {
|
|
|
|
Artifacts []artifact
|
|
|
|
Actions []action
|
|
|
|
DepSetOfFiles []depSetOfFiles
|
2020-12-24 01:02:11 +08:00
|
|
|
PathFragments []pathFragment
|
2020-12-11 06:19:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// BuildStatement contains information to register a build statement corresponding (one to one)
|
|
|
|
// with a Bazel action from Bazel's action graph.
|
|
|
|
type BuildStatement struct {
|
|
|
|
Command string
|
2021-03-26 04:42:37 +08:00
|
|
|
Depfile *string
|
2020-12-11 06:19:18 +08:00
|
|
|
OutputPaths []string
|
|
|
|
InputPaths []string
|
|
|
|
Env []KeyValuePair
|
|
|
|
Mnemonic string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AqueryBuildStatements returns an array of BuildStatements which should be registered (and output
|
|
|
|
// to a ninja file) to correspond one-to-one with the given action graph json proto (from a bazel
|
|
|
|
// aquery invocation).
|
2021-01-16 01:22:41 +08:00
|
|
|
func AqueryBuildStatements(aqueryJsonProto []byte) ([]BuildStatement, error) {
|
2020-12-11 06:19:18 +08:00
|
|
|
buildStatements := []BuildStatement{}
|
|
|
|
|
|
|
|
var aqueryResult actionGraphContainer
|
2021-01-16 01:22:41 +08:00
|
|
|
err := json.Unmarshal(aqueryJsonProto, &aqueryResult)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-11 06:19:18 +08:00
|
|
|
|
2020-12-24 01:02:11 +08:00
|
|
|
pathFragments := map[int]pathFragment{}
|
|
|
|
for _, pathFragment := range aqueryResult.PathFragments {
|
|
|
|
pathFragments[pathFragment.Id] = pathFragment
|
|
|
|
}
|
|
|
|
artifactIdToPath := map[int]string{}
|
2020-12-11 06:19:18 +08:00
|
|
|
for _, artifact := range aqueryResult.Artifacts {
|
2020-12-24 01:02:11 +08:00
|
|
|
artifactPath, err := expandPathFragment(artifact.PathFragmentId, pathFragments)
|
|
|
|
if err != nil {
|
2021-01-16 01:22:41 +08:00
|
|
|
return nil, err
|
2020-12-24 01:02:11 +08:00
|
|
|
}
|
|
|
|
artifactIdToPath[artifact.Id] = artifactPath
|
2020-12-11 06:19:18 +08:00
|
|
|
}
|
2021-01-20 00:36:50 +08:00
|
|
|
|
|
|
|
depsetIdToDepset := map[int]depSetOfFiles{}
|
2020-12-11 06:19:18 +08:00
|
|
|
for _, depset := range aqueryResult.DepSetOfFiles {
|
2021-01-20 00:36:50 +08:00
|
|
|
depsetIdToDepset[depset.Id] = depset
|
2020-12-11 06:19:18 +08:00
|
|
|
}
|
|
|
|
|
2021-01-20 00:36:50 +08:00
|
|
|
// depsetIdToArtifactIdsCache is a memoization of depset flattening, because flattening
|
|
|
|
// may be an expensive operation.
|
|
|
|
depsetIdToArtifactIdsCache := map[int][]int{}
|
|
|
|
|
2021-02-23 05:13:50 +08:00
|
|
|
// Do a pass through all actions to identify which artifacts are middleman artifacts.
|
|
|
|
// These will be omitted from the inputs of other actions.
|
|
|
|
// TODO(b/180945500): Handle middleman actions; without proper handling, depending on generated
|
|
|
|
// headers may cause build failures.
|
|
|
|
middlemanArtifactIds := map[int]bool{}
|
2020-12-11 06:19:18 +08:00
|
|
|
for _, actionEntry := range aqueryResult.Actions {
|
2021-02-23 05:13:50 +08:00
|
|
|
if actionEntry.Mnemonic == "Middleman" {
|
|
|
|
for _, outputId := range actionEntry.OutputIds {
|
|
|
|
middlemanArtifactIds[outputId] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, actionEntry := range aqueryResult.Actions {
|
|
|
|
if shouldSkipAction(actionEntry) {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-11 06:19:18 +08:00
|
|
|
outputPaths := []string{}
|
2021-03-26 04:42:37 +08:00
|
|
|
var depfile *string
|
2020-12-11 06:19:18 +08:00
|
|
|
for _, outputId := range actionEntry.OutputIds {
|
2021-01-16 01:22:41 +08:00
|
|
|
outputPath, exists := artifactIdToPath[outputId]
|
|
|
|
if !exists {
|
|
|
|
return nil, fmt.Errorf("undefined outputId %d", outputId)
|
|
|
|
}
|
2021-03-26 04:42:37 +08:00
|
|
|
ext := filepath.Ext(outputPath)
|
|
|
|
if ext == ".d" {
|
|
|
|
if depfile != nil {
|
|
|
|
return nil, fmt.Errorf("found multiple potential depfiles %q, %q", *depfile, outputPath)
|
|
|
|
} else {
|
|
|
|
depfile = &outputPath
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
outputPaths = append(outputPaths, outputPath)
|
|
|
|
}
|
2020-12-11 06:19:18 +08:00
|
|
|
}
|
|
|
|
inputPaths := []string{}
|
|
|
|
for _, inputDepSetId := range actionEntry.InputDepSetIds {
|
2021-01-20 00:36:50 +08:00
|
|
|
inputArtifacts, err :=
|
|
|
|
artifactIdsFromDepsetId(depsetIdToDepset, depsetIdToArtifactIdsCache, inputDepSetId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-01-16 01:22:41 +08:00
|
|
|
}
|
|
|
|
for _, inputId := range inputArtifacts {
|
2021-02-23 05:13:50 +08:00
|
|
|
if _, isMiddlemanArtifact := middlemanArtifactIds[inputId]; isMiddlemanArtifact {
|
|
|
|
// Omit middleman artifacts.
|
|
|
|
continue
|
|
|
|
}
|
2021-01-16 01:22:41 +08:00
|
|
|
inputPath, exists := artifactIdToPath[inputId]
|
|
|
|
if !exists {
|
|
|
|
return nil, fmt.Errorf("undefined input artifactId %d", inputId)
|
|
|
|
}
|
|
|
|
inputPaths = append(inputPaths, inputPath)
|
2020-12-11 06:19:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
buildStatement := BuildStatement{
|
|
|
|
Command: strings.Join(proptools.ShellEscapeList(actionEntry.Arguments), " "),
|
2021-03-26 04:42:37 +08:00
|
|
|
Depfile: depfile,
|
2020-12-11 06:19:18 +08:00
|
|
|
OutputPaths: outputPaths,
|
|
|
|
InputPaths: inputPaths,
|
|
|
|
Env: actionEntry.EnvironmentVariables,
|
|
|
|
Mnemonic: actionEntry.Mnemonic}
|
2021-02-23 05:13:50 +08:00
|
|
|
if len(actionEntry.Arguments) < 1 {
|
2021-03-26 04:42:37 +08:00
|
|
|
return nil, fmt.Errorf("received action with no command: [%v]", buildStatement)
|
2021-02-23 05:13:50 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-12-11 06:19:18 +08:00
|
|
|
buildStatements = append(buildStatements, buildStatement)
|
|
|
|
}
|
|
|
|
|
2021-01-16 01:22:41 +08:00
|
|
|
return buildStatements, nil
|
2020-12-11 06:19:18 +08:00
|
|
|
}
|
2020-12-24 01:02:11 +08:00
|
|
|
|
2021-02-23 05:13:50 +08:00
|
|
|
func shouldSkipAction(a action) bool {
|
|
|
|
// TODO(b/180945121): Handle symlink actions.
|
|
|
|
if a.Mnemonic == "Symlink" || a.Mnemonic == "SourceSymlinkManifest" || a.Mnemonic == "SymlinkTree" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// TODO(b/180945500): Handle middleman actions; without proper handling, depending on generated
|
|
|
|
// headers may cause build failures.
|
|
|
|
if a.Mnemonic == "Middleman" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Skip "Fail" actions, which are placeholder actions designed to always fail.
|
|
|
|
if a.Mnemonic == "Fail" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// TODO(b/180946980): Handle FileWrite. The aquery proto currently contains no information
|
|
|
|
// about the contents that are written.
|
|
|
|
if a.Mnemonic == "FileWrite" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-20 00:36:50 +08:00
|
|
|
func artifactIdsFromDepsetId(depsetIdToDepset map[int]depSetOfFiles,
|
|
|
|
depsetIdToArtifactIdsCache map[int][]int, depsetId int) ([]int, error) {
|
|
|
|
if result, exists := depsetIdToArtifactIdsCache[depsetId]; exists {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
if depset, exists := depsetIdToDepset[depsetId]; exists {
|
|
|
|
result := depset.DirectArtifactIds
|
|
|
|
for _, childId := range depset.TransitiveDepSetIds {
|
|
|
|
childArtifactIds, err :=
|
|
|
|
artifactIdsFromDepsetId(depsetIdToDepset, depsetIdToArtifactIdsCache, childId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, childArtifactIds...)
|
|
|
|
}
|
|
|
|
depsetIdToArtifactIdsCache[depsetId] = result
|
|
|
|
return result, nil
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("undefined input depsetId %d", depsetId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:02:11 +08:00
|
|
|
func expandPathFragment(id int, pathFragmentsMap map[int]pathFragment) (string, error) {
|
|
|
|
labels := []string{}
|
|
|
|
currId := id
|
|
|
|
// Only positive IDs are valid for path fragments. An ID of zero indicates a terminal node.
|
|
|
|
for currId > 0 {
|
|
|
|
currFragment, ok := pathFragmentsMap[currId]
|
|
|
|
if !ok {
|
2021-01-16 01:22:41 +08:00
|
|
|
return "", fmt.Errorf("undefined path fragment id %d", currId)
|
2020-12-24 01:02:11 +08:00
|
|
|
}
|
|
|
|
labels = append([]string{currFragment.Label}, labels...)
|
|
|
|
currId = currFragment.ParentId
|
|
|
|
}
|
|
|
|
return filepath.Join(labels...), nil
|
|
|
|
}
|