Merge "Support main repository prefixing in cquery"

This commit is contained in:
Christopher Parsons
2022-09-29 15:21:22 +00:00
committed by Gerrit Code Review
3 changed files with 23 additions and 8 deletions

View File

@@ -104,9 +104,16 @@ type cqueryKey struct {
configKey configKey configKey configKey
} }
func makeCqueryKey(label string, cqueryRequest cqueryRequest, cfgKey configKey) cqueryKey {
if strings.HasPrefix(label, "//") {
// Normalize Bazel labels to specify main repository explicitly.
label = "@" + label
}
return cqueryKey{label, cqueryRequest, cfgKey}
}
func (c cqueryKey) String() string { func (c cqueryKey) String() string {
return fmt.Sprintf("cquery(%s,%s,%s)", c.label, c.requestType.Name(), c.configKey) return fmt.Sprintf("cquery(%s,%s,%s)", c.label, c.requestType.Name(), c.configKey)
} }
// BazelContext is a context object useful for interacting with Bazel during // BazelContext is a context object useful for interacting with Bazel during
@@ -261,23 +268,24 @@ func (m MockBazelContext) AqueryDepsets() []bazel.AqueryDepset {
var _ BazelContext = MockBazelContext{} var _ BazelContext = MockBazelContext{}
func (bazelCtx *bazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) { func (bazelCtx *bazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) {
key := cqueryKey{label, requestType, cfgKey} key := makeCqueryKey(label, requestType, cfgKey)
bazelCtx.requestMutex.Lock() bazelCtx.requestMutex.Lock()
defer bazelCtx.requestMutex.Unlock() defer bazelCtx.requestMutex.Unlock()
bazelCtx.requests[key] = true bazelCtx.requests[key] = true
} }
func (bazelCtx *bazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) { func (bazelCtx *bazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) {
key := cqueryKey{label, cquery.GetOutputFiles, cfgKey} key := makeCqueryKey(label, cquery.GetOutputFiles, cfgKey)
if rawString, ok := bazelCtx.results[key]; ok { if rawString, ok := bazelCtx.results[key]; ok {
bazelOutput := strings.TrimSpace(rawString) bazelOutput := strings.TrimSpace(rawString)
return cquery.GetOutputFiles.ParseResult(bazelOutput), nil return cquery.GetOutputFiles.ParseResult(bazelOutput), nil
} }
return nil, fmt.Errorf("no bazel response found for %v", key) return nil, fmt.Errorf("no bazel response found for %v", key)
} }
func (bazelCtx *bazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) { func (bazelCtx *bazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) {
key := cqueryKey{label, cquery.GetCcInfo, cfgKey} key := makeCqueryKey(label, cquery.GetCcInfo, cfgKey)
if rawString, ok := bazelCtx.results[key]; ok { if rawString, ok := bazelCtx.results[key]; ok {
bazelOutput := strings.TrimSpace(rawString) bazelOutput := strings.TrimSpace(rawString)
return cquery.GetCcInfo.ParseResult(bazelOutput) return cquery.GetCcInfo.ParseResult(bazelOutput)
@@ -286,7 +294,7 @@ func (bazelCtx *bazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.
} }
func (bazelCtx *bazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) { func (bazelCtx *bazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) {
key := cqueryKey{label, cquery.GetPythonBinary, cfgKey} key := makeCqueryKey(label, cquery.GetPythonBinary, cfgKey)
if rawString, ok := bazelCtx.results[key]; ok { if rawString, ok := bazelCtx.results[key]; ok {
bazelOutput := strings.TrimSpace(rawString) bazelOutput := strings.TrimSpace(rawString)
return cquery.GetPythonBinary.ParseResult(bazelOutput), nil return cquery.GetPythonBinary.ParseResult(bazelOutput), nil
@@ -295,7 +303,7 @@ func (bazelCtx *bazelContext) GetPythonBinary(label string, cfgKey configKey) (s
} }
func (bazelCtx *bazelContext) GetApexInfo(label string, cfgKey configKey) (cquery.ApexCqueryInfo, error) { func (bazelCtx *bazelContext) GetApexInfo(label string, cfgKey configKey) (cquery.ApexCqueryInfo, error) {
key := cqueryKey{label, cquery.GetApexInfo, cfgKey} key := makeCqueryKey(label, cquery.GetApexInfo, cfgKey)
if rawString, ok := bazelCtx.results[key]; ok { if rawString, ok := bazelCtx.results[key]; ok {
return cquery.GetApexInfo.ParseResult(strings.TrimSpace(rawString)), nil return cquery.GetApexInfo.ParseResult(strings.TrimSpace(rawString)), nil
} }
@@ -755,6 +763,10 @@ def get_arch(target):
def format(target): def format(target):
id_string = str(target.label) + "|" + get_arch(target) id_string = str(target.label) + "|" + get_arch(target)
# TODO(b/248106697): Remove once Bazel is updated to always normalize labels.
if id_string.startswith("//"):
id_string = "@" + id_string
# Main switch section # Main switch section
%s %s
# This target was not requested via cquery, and thus must be a dependency # This target was not requested via cquery, and thus must be a dependency

View File

@@ -13,10 +13,10 @@ import (
var testConfig = TestConfig("out", nil, "", nil) var testConfig = TestConfig("out", nil, "", nil)
func TestRequestResultsAfterInvokeBazel(t *testing.T) { func TestRequestResultsAfterInvokeBazel(t *testing.T) {
label := "//foo:bar" label := "@//foo:bar"
cfg := configKey{"arm64_armv8-a", Android} cfg := configKey{"arm64_armv8-a", Android}
bazelContext, _ := testBazelContext(t, map[bazelCommand]string{ bazelContext, _ := testBazelContext(t, map[bazelCommand]string{
bazelCommand{command: "cquery", expression: "deps(@soong_injection//mixed_builds:buildroot, 2)"}: `//foo:bar|arm64_armv8-a|android>>out/foo/bar.txt`, bazelCommand{command: "cquery", expression: "deps(@soong_injection//mixed_builds:buildroot, 2)"}: `@//foo:bar|arm64_armv8-a|android>>out/foo/bar.txt`,
}) })
bazelContext.QueueBazelRequest(label, cquery.GetOutputFiles, cfg) bazelContext.QueueBazelRequest(label, cquery.GetOutputFiles, cfg)
err := bazelContext.InvokeBazel(testConfig) err := bazelContext.InvokeBazel(testConfig)

View File

@@ -181,6 +181,9 @@ func (g getCcInfoType) ParseResult(rawString string) (CcInfo, error) {
decoder := json.NewDecoder(strings.NewReader(rawString)) decoder := json.NewDecoder(strings.NewReader(rawString))
decoder.DisallowUnknownFields() //useful to detect typos, e.g. in unit tests decoder.DisallowUnknownFields() //useful to detect typos, e.g. in unit tests
err := decoder.Decode(&ccInfo) err := decoder.Decode(&ccInfo)
if err != nil {
return ccInfo, fmt.Errorf("error parsing CcInfo result. %s RAW STRING: %s", err, rawString)
}
return ccInfo, err return ccInfo, err
} }