Consolidate cc-specific cqueries.

There is little overlap at this point, but we expect these to converge
more over time, to handle exported includes, libs, etc., this will allow
those changes to be handled in one place and allow more consolidation of
code.

Test: bp2build generate & sync; mixed build libc
Change-Id: I51685dad9f4fc11a634965a3c9e84f4a0e279ecb
This commit is contained in:
Liz Kammer 2021-04-09 14:07:00 -04:00
parent 8d62a4f725
commit b71794d8e6
4 changed files with 45 additions and 82 deletions

View File

@ -67,11 +67,7 @@ type BazelContext interface {
// TODO(cparsons): Other cquery-related methods should be added here. // TODO(cparsons): Other cquery-related methods should be added here.
// Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order). // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool)
// GetPrebuiltCcStaticLibraryFiles returns paths to prebuilt cc static libraries, and whether the
// results were available
GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool)
// ** End cquery methods // ** End cquery methods
@ -127,9 +123,8 @@ var _ BazelContext = noopBazelContext{}
type MockBazelContext struct { type MockBazelContext struct {
OutputBaseDir string OutputBaseDir string
LabelToOutputFiles map[string][]string LabelToOutputFiles map[string][]string
LabelToOutputFilesAndCcObjectFiles map[string]cquery.GetOutputFilesAndCcObjectFiles_Result LabelToCcInfo map[string]cquery.CcInfo
LabelToCcStaticLibraryFiles map[string][]string
} }
func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) { func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
@ -137,13 +132,8 @@ func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]str
return result, ok return result, ok
} }
func (m MockBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) { func (m MockBazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool) {
result, ok := m.LabelToOutputFilesAndCcObjectFiles[label] result, ok := m.LabelToCcInfo[label]
return result.OutputFiles, result.CcObjectFiles, ok
}
func (m MockBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
result, ok := m.LabelToCcStaticLibraryFiles[label]
return result, ok return result, ok
} }
@ -173,39 +163,21 @@ func (bazelCtx *bazelContext) GetOutputFiles(label string, archType ArchType) ([
return ret, ok return ret, ok
} }
func (bazelCtx *bazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) { func (bazelCtx *bazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool) {
var outputFiles []string result, ok := bazelCtx.cquery(label, cquery.GetCcInfo, archType)
var ccObjects []string
result, ok := bazelCtx.cquery(label, cquery.GetOutputFilesAndCcObjectFiles, archType)
if ok {
bazelOutput := strings.TrimSpace(result)
returnResult := cquery.GetOutputFilesAndCcObjectFiles.ParseResult(bazelOutput)
outputFiles = returnResult.OutputFiles
ccObjects = returnResult.CcObjectFiles
}
return outputFiles, ccObjects, ok
}
// GetPrebuiltCcStaticLibraryFiles returns a slice of prebuilt static libraries for the given
// label/archType if there are query results; otherwise, it enqueues the query and returns false.
func (bazelCtx *bazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
result, ok := bazelCtx.cquery(label, cquery.GetPrebuiltCcStaticLibraryFiles, archType)
if !ok { if !ok {
return nil, false return cquery.CcInfo{}, ok
} }
bazelOutput := strings.TrimSpace(result) bazelOutput := strings.TrimSpace(result)
ret := cquery.GetPrebuiltCcStaticLibraryFiles.ParseResult(bazelOutput) return cquery.GetCcInfo.ParseResult(bazelOutput), ok
return ret, ok
} }
func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) { func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
panic("unimplemented") panic("unimplemented")
} }
func (n noopBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) { func (n noopBazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool) {
panic("unimplemented") panic("unimplemented")
} }

View File

@ -5,14 +5,14 @@ import (
) )
var ( var (
GetOutputFiles = &getOutputFilesRequestType{} GetOutputFiles = &getOutputFilesRequestType{}
GetOutputFilesAndCcObjectFiles = &getOutputFilesAndCcObjectFilesType{} GetCcInfo = &getCcInfoType{}
GetPrebuiltCcStaticLibraryFiles = &getPrebuiltCcStaticLibraryFiles{}
) )
type GetOutputFilesAndCcObjectFiles_Result struct { type CcInfo struct {
OutputFiles []string OutputFiles []string
CcObjectFiles []string CcObjectFiles []string
CcStaticLibraryFiles []string
} }
type getOutputFilesRequestType struct{} type getOutputFilesRequestType struct{}
@ -42,12 +42,12 @@ func (g getOutputFilesRequestType) ParseResult(rawString string) []string {
return strings.Split(rawString, ", ") return strings.Split(rawString, ", ")
} }
type getOutputFilesAndCcObjectFilesType struct{} type getCcInfoType struct{}
// Name returns a string name for this request type. Such request type names must be unique, // Name returns a string name for this request type. Such request type names must be unique,
// and must only consist of alphanumeric characters. // and must only consist of alphanumeric characters.
func (g getOutputFilesAndCcObjectFilesType) Name() string { func (g getCcInfoType) Name() string {
return "getOutputFilesAndCcObjectFiles" return "getCcInfo"
} }
// StarlarkFunctionBody returns a starlark function body to process this request type. // StarlarkFunctionBody returns a starlark function body to process this request type.
@ -58,61 +58,49 @@ func (g getOutputFilesAndCcObjectFilesType) Name() string {
// - `target` is the only parameter to this function (a configured target). // - `target` is the only parameter to this function (a configured target).
// - The return value must be a string. // - The return value must be a string.
// - The function body should not be indented outside of its own scope. // - The function body should not be indented outside of its own scope.
func (g getOutputFilesAndCcObjectFilesType) StarlarkFunctionBody() string { func (g getCcInfoType) StarlarkFunctionBody() string {
return ` return `
outputFiles = [f.path for f in target.files.to_list()] outputFiles = [f.path for f in target.files.to_list()]
ccObjectFiles = [] ccObjectFiles = []
staticLibraries = []
linker_inputs = providers(target)["CcInfo"].linking_context.linker_inputs.to_list() linker_inputs = providers(target)["CcInfo"].linking_context.linker_inputs.to_list()
for linker_input in linker_inputs: for linker_input in linker_inputs:
for library in linker_input.libraries: for library in linker_input.libraries:
for object in library.objects: for object in library.objects:
ccObjectFiles += [object.path] ccObjectFiles += [object.path]
return ', '.join(outputFiles) + "|" + ', '.join(ccObjectFiles)` if library.static_library:
staticLibraries.append(library.static_library.path)
returns = [
outputFiles,
staticLibraries,
ccObjectFiles,
]
return "|".join([", ".join(r) for r in returns])`
} }
// ParseResult returns a value obtained by parsing the result of the request's Starlark function. // ParseResult returns a value obtained by parsing the result of the request's Starlark function.
// The given rawString must correspond to the string output which was created by evaluating the // The given rawString must correspond to the string output which was created by evaluating the
// Starlark given in StarlarkFunctionBody. // Starlark given in StarlarkFunctionBody.
func (g getOutputFilesAndCcObjectFilesType) ParseResult(rawString string) GetOutputFilesAndCcObjectFiles_Result { func (g getCcInfoType) ParseResult(rawString string) CcInfo {
var outputFiles []string var outputFiles []string
var ccObjects []string var ccObjects []string
splitString := strings.Split(rawString, "|") splitString := strings.Split(rawString, "|")
outputFilesString := splitString[0] outputFilesString := splitString[0]
ccObjectsString := splitString[1] ccStaticLibrariesString := splitString[1]
ccObjectsString := splitString[2]
outputFiles = splitOrEmpty(outputFilesString, ", ") outputFiles = splitOrEmpty(outputFilesString, ", ")
ccStaticLibraries := splitOrEmpty(ccStaticLibrariesString, ", ")
ccObjects = splitOrEmpty(ccObjectsString, ", ") ccObjects = splitOrEmpty(ccObjectsString, ", ")
return GetOutputFilesAndCcObjectFiles_Result{outputFiles, ccObjects} return CcInfo{
} OutputFiles: outputFiles,
CcObjectFiles: ccObjects,
type getPrebuiltCcStaticLibraryFiles struct{} CcStaticLibraryFiles: ccStaticLibraries,
}
// Name returns the name of the starlark function to get prebuilt cc static library files
func (g getPrebuiltCcStaticLibraryFiles) Name() string {
return "getPrebuiltCcStaticLibraryFiles"
}
// StarlarkFunctionBody returns the unindented body of a starlark function for extracting the static
// library paths from a cc_import module.
func (g getPrebuiltCcStaticLibraryFiles) StarlarkFunctionBody() string {
return `
linker_inputs = providers(target)["CcInfo"].linking_context.linker_inputs.to_list()
static_libraries = []
for linker_input in linker_inputs:
for library in linker_input.libraries:
static_libraries.append(library.static_library.path)
return ', '.join(static_libraries)`
}
// ParseResult returns a slice of bazel output paths to static libraries if any exist for the given
// rawString corresponding to the string output which was created by evaluating the
// StarlarkFunctionBody.
func (g getPrebuiltCcStaticLibraryFiles) ParseResult(rawString string) []string {
return strings.Split(rawString, ", ")
} }
// splitOrEmpty is a modification of strings.Split() that returns an empty list // splitOrEmpty is a modification of strings.Split() that returns an empty list

View File

@ -483,7 +483,9 @@ type staticLibraryBazelHandler struct {
func (handler *staticLibraryBazelHandler) generateBazelBuildActions(ctx android.ModuleContext, label string) bool { func (handler *staticLibraryBazelHandler) generateBazelBuildActions(ctx android.ModuleContext, label string) bool {
bazelCtx := ctx.Config().BazelContext bazelCtx := ctx.Config().BazelContext
outputPaths, objPaths, ok := bazelCtx.GetOutputFilesAndCcObjectFiles(label, ctx.Arch().ArchType) ccInfo, ok := bazelCtx.GetCcInfo(label, ctx.Arch().ArchType)
outputPaths := ccInfo.OutputFiles
objPaths := ccInfo.CcObjectFiles
if !ok { if !ok {
return ok return ok
} }

View File

@ -329,7 +329,8 @@ type prebuiltStaticLibraryBazelHandler struct {
func (h *prebuiltStaticLibraryBazelHandler) generateBazelBuildActions(ctx android.ModuleContext, label string) bool { func (h *prebuiltStaticLibraryBazelHandler) generateBazelBuildActions(ctx android.ModuleContext, label string) bool {
bazelCtx := ctx.Config().BazelContext bazelCtx := ctx.Config().BazelContext
staticLibs, ok := bazelCtx.GetPrebuiltCcStaticLibraryFiles(label, ctx.Arch().ArchType) ccInfo, ok := bazelCtx.GetCcInfo(label, ctx.Arch().ArchType)
staticLibs := ccInfo.CcStaticLibraryFiles
if !ok { if !ok {
return false return false
} }