Merge "Revert "Use proto output instead of jsonproto output for aquery""

This commit is contained in:
Jason Wu 2022-10-27 05:24:40 +00:00 committed by Gerrit Code Review
commit ab17626ba6
7 changed files with 568 additions and 772 deletions

View file

@ -558,6 +558,7 @@ func (r *builtinBazelRunner) createBazelCommand(paths *bazelPaths, runName bazel
// The actual platform values here may be overridden by configuration
// transitions from the buildroot.
fmt.Sprintf("--extra_toolchains=%s", "//prebuilts/clang/host/linux-x86:all"),
// This should be parameterized on the host OS, but let's restrict to linux
// to keep things simple for now.
fmt.Sprintf("--host_platform=%s", "//build/bazel/platforms:linux_x86_64"),
@ -926,7 +927,7 @@ func (context *bazelContext) InvokeBazel(config Config) error {
//
// Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
// proto sources, which would add a number of unnecessary dependencies.
extraFlags := []string{"--output=proto", "--include_file_write_contents"}
extraFlags := []string{"--output=jsonproto", "--include_file_write_contents"}
if Bool(config.productVariables.ClangCoverage) {
extraFlags = append(extraFlags, "--collect_code_coverage")
paths := make([]string, 0, 2)

View file

@ -1,7 +1,6 @@
package android
import (
"encoding/json"
"os"
"path/filepath"
"reflect"
@ -9,8 +8,6 @@ import (
"testing"
"android/soong/bazel/cquery"
"google.golang.org/protobuf/proto"
analysis_v2_proto "prebuilts/bazel/common/proto/analysis_v2"
)
var testConfig = TestConfig("out", nil, "", nil)
@ -68,56 +65,52 @@ func TestInvokeBazelPopulatesBuildStatements(t *testing.T) {
var testCases = []testCase{
{`
{
"artifacts": [
{ "id": 1, "path_fragment_id": 1 },
{ "id": 2, "path_fragment_id": 2 }],
"actions": [{
"target_Id": 1,
"action_Key": "x",
"mnemonic": "x",
"arguments": ["touch", "foo"],
"input_dep_set_ids": [1],
"output_Ids": [1],
"primary_output_id": 1
}],
"dep_set_of_files": [
{ "id": 1, "direct_artifact_ids": [1, 2] }],
"path_fragments": [
{ "id": 1, "label": "one" },
{ "id": 2, "label": "two" }]
"artifacts": [
{ "id": 1, "pathFragmentId": 1 },
{ "id": 2, "pathFragmentId": 2 }],
"actions": [{
"targetId": 1,
"actionKey": "x",
"mnemonic": "x",
"arguments": ["touch", "foo"],
"inputDepSetIds": [1],
"outputIds": [1],
"primaryOutputId": 1
}],
"depSetOfFiles": [
{ "id": 1, "directArtifactIds": [1, 2] }],
"pathFragments": [
{ "id": 1, "label": "one" },
{ "id": 2, "label": "two" }]
}`,
"cd 'test/exec_root' && rm -f 'one' && touch foo",
}, {`
{
"artifacts": [
{ "id": 1, "path_fragment_id": 10 },
{ "id": 2, "path_fragment_id": 20 }],
"actions": [{
"target_Id": 100,
"action_Key": "x",
"mnemonic": "x",
"arguments": ["bogus", "command"],
"output_Ids": [1, 2],
"primary_output_id": 1
}],
"path_fragments": [
{ "id": 10, "label": "one", "parent_id": 30 },
{ "id": 20, "label": "one.d", "parent_id": 30 },
{ "id": 30, "label": "parent" }]
"artifacts": [
{ "id": 1, "pathFragmentId": 10 },
{ "id": 2, "pathFragmentId": 20 }],
"actions": [{
"targetId": 100,
"actionKey": "x",
"mnemonic": "x",
"arguments": ["bogus", "command"],
"outputIds": [1, 2],
"primaryOutputId": 1
}],
"pathFragments": [
{ "id": 10, "label": "one", "parentId": 30 },
{ "id": 20, "label": "one.d", "parentId": 30 },
{ "id": 30, "label": "parent" }]
}`,
`cd 'test/exec_root' && rm -f 'parent/one' && bogus command && sed -i'' -E 's@(^|\s|")bazel-out/@\1test/bazel_out/@g' 'parent/one.d'`,
},
}
for i, testCase := range testCases {
data, err := JsonToActionGraphContainer(testCase.input)
if err != nil {
t.Error(err)
}
bazelContext, _ := testBazelContext(t, map[bazelCommand]string{
bazelCommand{command: "aquery", expression: "deps(@soong_injection//mixed_builds:buildroot)"}: string(data)})
bazelCommand{command: "aquery", expression: "deps(@soong_injection//mixed_builds:buildroot)"}: testCase.input})
err = bazelContext.InvokeBazel(testConfig)
err := bazelContext.InvokeBazel(testConfig)
if err != nil {
t.Fatalf("testCase #%d: did not expect error invoking Bazel, but got %s", i+1, err)
}
@ -201,14 +194,3 @@ func testBazelContext(t *testing.T, bazelCommandResults map[bazelCommand]string)
requests: map[cqueryKey]bool{},
}, p.soongOutDir
}
// Transform the json format to ActionGraphContainer
func JsonToActionGraphContainer(inputString string) ([]byte, error) {
var aqueryProtoResult analysis_v2_proto.ActionGraphContainer
err := json.Unmarshal([]byte(inputString), &aqueryProtoResult)
if err != nil {
return []byte(""), err
}
data, _ := proto.Marshal(&aqueryProtoResult)
return data, err
}

View file

@ -20,7 +20,6 @@ bootstrap_go_package {
"soong_build",
],
deps: [
"bazel_analysis_v2_proto",
"blueprint",
],
}

View file

@ -17,6 +17,7 @@ package bazel
import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"path/filepath"
"reflect"
@ -24,8 +25,6 @@ import (
"strings"
"github.com/google/blueprint/proptools"
"google.golang.org/protobuf/proto"
analysis_v2_proto "prebuilts/bazel/common/proto/analysis_v2"
)
type artifactId int
@ -313,79 +312,11 @@ func (a *aqueryArtifactHandler) artifactPathsFromDepsetHash(depsetHash string) (
// BuildStatements are one-to-one with actions in the given action graph, and AqueryDepsets
// are one-to-one with Bazel's depSetOfFiles objects.
func AqueryBuildStatements(aqueryJsonProto []byte) ([]BuildStatement, []AqueryDepset, error) {
aqueryProto := &analysis_v2_proto.ActionGraphContainer{}
err := proto.Unmarshal(aqueryJsonProto, aqueryProto)
var aqueryResult actionGraphContainer
err := json.Unmarshal(aqueryJsonProto, &aqueryResult)
if err != nil {
return nil, nil, err
}
aqueryResult := actionGraphContainer{}
for _, protoArtifact := range aqueryProto.Artifacts {
aqueryResult.Artifacts = append(aqueryResult.Artifacts, artifact{artifactId(protoArtifact.Id),
pathFragmentId(protoArtifact.PathFragmentId)})
}
for _, protoAction := range aqueryProto.Actions {
var environmentVariable []KeyValuePair
var inputDepSetIds []depsetId
var outputIds []artifactId
var substitutions []KeyValuePair
for _, protoEnvironmentVariable := range protoAction.EnvironmentVariables {
environmentVariable = append(environmentVariable, KeyValuePair{
protoEnvironmentVariable.Key, protoEnvironmentVariable.Value,
})
}
for _, protoInputDepSetIds := range protoAction.InputDepSetIds {
inputDepSetIds = append(inputDepSetIds, depsetId(protoInputDepSetIds))
}
for _, protoOutputIds := range protoAction.OutputIds {
outputIds = append(outputIds, artifactId(protoOutputIds))
}
for _, protoSubstitutions := range protoAction.Substitutions {
substitutions = append(substitutions, KeyValuePair{
protoSubstitutions.Key, protoSubstitutions.Value,
})
}
aqueryResult.Actions = append(aqueryResult.Actions,
action{
Arguments: protoAction.Arguments,
EnvironmentVariables: environmentVariable,
InputDepSetIds: inputDepSetIds,
Mnemonic: protoAction.Mnemonic,
OutputIds: outputIds,
TemplateContent: protoAction.TemplateContent,
Substitutions: substitutions,
FileContents: protoAction.FileContents})
}
for _, protoDepSetOfFiles := range aqueryProto.DepSetOfFiles {
var directArtifactIds []artifactId
var transitiveDepSetIds []depsetId
for _, protoDirectArtifactIds := range protoDepSetOfFiles.DirectArtifactIds {
directArtifactIds = append(directArtifactIds, artifactId(protoDirectArtifactIds))
}
for _, protoTransitiveDepSetIds := range protoDepSetOfFiles.TransitiveDepSetIds {
transitiveDepSetIds = append(transitiveDepSetIds, depsetId(protoTransitiveDepSetIds))
}
aqueryResult.DepSetOfFiles = append(aqueryResult.DepSetOfFiles,
depSetOfFiles{
Id: depsetId(protoDepSetOfFiles.Id),
DirectArtifactIds: directArtifactIds,
TransitiveDepSetIds: transitiveDepSetIds})
}
for _, protoPathFragments := range aqueryProto.PathFragments {
aqueryResult.PathFragments = append(aqueryResult.PathFragments,
pathFragment{
Id: pathFragmentId(protoPathFragments.Id),
Label: protoPathFragments.Label,
ParentId: pathFragmentId(protoPathFragments.ParentId)})
}
aqueryHandler, err := newAqueryHandler(aqueryResult)
if err != nil {
return nil, nil, err

File diff suppressed because it is too large Load diff

9
go.mod
View file

@ -13,12 +13,7 @@ exclude github.com/golang/protobuf v1.5.0
replace github.com/google/go-cmp v0.5.5 => ../../external/go-cmp
require prebuilts/bazel/common/proto/analysis_v2 v0.0.0
replace prebuilts/bazel/common/proto/analysis_v2 => ../../prebuilts/bazel/common/proto/analysis_v2
require prebuilts/bazel/common/proto/build v0.0.0 // indirect
replace prebuilts/bazel/common/proto/build => ../../prebuilts/bazel/common/proto/build
// Indirect dep from go-cmp
exclude golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
go 1.18

View file

@ -85,7 +85,6 @@ function create_mock_soong {
copy_directory build/blueprint
copy_directory build/soong
copy_directory build/make/tools/rbcrun
copy_directory prebuilts/bazel/common/proto
symlink_directory prebuilts/sdk
symlink_directory prebuilts/go