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.
// Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order).
GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool)
// GetPrebuiltCcStaticLibraryFiles returns paths to prebuilt cc static libraries, and whether the
// results were available
GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool)
GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool)
// ** End cquery methods
@ -127,9 +123,8 @@ var _ BazelContext = noopBazelContext{}
type MockBazelContext struct {
OutputBaseDir string
LabelToOutputFiles map[string][]string
LabelToOutputFilesAndCcObjectFiles map[string]cquery.GetOutputFilesAndCcObjectFiles_Result
LabelToCcStaticLibraryFiles map[string][]string
LabelToOutputFiles map[string][]string
LabelToCcInfo map[string]cquery.CcInfo
}
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
}
func (m MockBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
result, ok := m.LabelToOutputFilesAndCcObjectFiles[label]
return result.OutputFiles, result.CcObjectFiles, ok
}
func (m MockBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) {
result, ok := m.LabelToCcStaticLibraryFiles[label]
func (m MockBazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool) {
result, ok := m.LabelToCcInfo[label]
return result, ok
}
@ -173,39 +163,21 @@ func (bazelCtx *bazelContext) GetOutputFiles(label string, archType ArchType) ([
return ret, ok
}
func (bazelCtx *bazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) {
var outputFiles []string
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)
func (bazelCtx *bazelContext) GetCcInfo(label string, archType ArchType) (cquery.CcInfo, bool) {
result, ok := bazelCtx.cquery(label, cquery.GetCcInfo, archType)
if !ok {
return nil, false
return cquery.CcInfo{}, ok
}
bazelOutput := strings.TrimSpace(result)
ret := cquery.GetPrebuiltCcStaticLibraryFiles.ParseResult(bazelOutput)
return ret, ok
return cquery.GetCcInfo.ParseResult(bazelOutput), ok
}
func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) {
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")
}

View File

@ -5,14 +5,14 @@ import (
)
var (
GetOutputFiles = &getOutputFilesRequestType{}
GetOutputFilesAndCcObjectFiles = &getOutputFilesAndCcObjectFilesType{}
GetPrebuiltCcStaticLibraryFiles = &getPrebuiltCcStaticLibraryFiles{}
GetOutputFiles = &getOutputFilesRequestType{}
GetCcInfo = &getCcInfoType{}
)
type GetOutputFilesAndCcObjectFiles_Result struct {
OutputFiles []string
CcObjectFiles []string
type CcInfo struct {
OutputFiles []string
CcObjectFiles []string
CcStaticLibraryFiles []string
}
type getOutputFilesRequestType struct{}
@ -42,12 +42,12 @@ func (g getOutputFilesRequestType) ParseResult(rawString string) []string {
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,
// and must only consist of alphanumeric characters.
func (g getOutputFilesAndCcObjectFilesType) Name() string {
return "getOutputFilesAndCcObjectFiles"
func (g getCcInfoType) Name() string {
return "getCcInfo"
}
// 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).
// - The return value must be a string.
// - The function body should not be indented outside of its own scope.
func (g getOutputFilesAndCcObjectFilesType) StarlarkFunctionBody() string {
func (g getCcInfoType) StarlarkFunctionBody() string {
return `
outputFiles = [f.path for f in target.files.to_list()]
ccObjectFiles = []
staticLibraries = []
linker_inputs = providers(target)["CcInfo"].linking_context.linker_inputs.to_list()
for linker_input in linker_inputs:
for library in linker_input.libraries:
for object in library.objects:
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.
// The given rawString must correspond to the string output which was created by evaluating the
// Starlark given in StarlarkFunctionBody.
func (g getOutputFilesAndCcObjectFilesType) ParseResult(rawString string) GetOutputFilesAndCcObjectFiles_Result {
func (g getCcInfoType) ParseResult(rawString string) CcInfo {
var outputFiles []string
var ccObjects []string
splitString := strings.Split(rawString, "|")
outputFilesString := splitString[0]
ccObjectsString := splitString[1]
ccStaticLibrariesString := splitString[1]
ccObjectsString := splitString[2]
outputFiles = splitOrEmpty(outputFilesString, ", ")
ccStaticLibraries := splitOrEmpty(ccStaticLibrariesString, ", ")
ccObjects = splitOrEmpty(ccObjectsString, ", ")
return GetOutputFilesAndCcObjectFiles_Result{outputFiles, ccObjects}
}
type getPrebuiltCcStaticLibraryFiles struct{}
// 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, ", ")
return CcInfo{
OutputFiles: outputFiles,
CcObjectFiles: ccObjects,
CcStaticLibraryFiles: ccStaticLibraries,
}
}
// 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 {
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 {
return ok
}

View File

@ -329,7 +329,8 @@ type prebuiltStaticLibraryBazelHandler struct {
func (h *prebuiltStaticLibraryBazelHandler) generateBazelBuildActions(ctx android.ModuleContext, label string) bool {
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 {
return false
}