Clean up bazel_handler.go am: b43ae1eec5

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2144600

Change-Id: I62e8112c4823fe88bde444ac4027a50d25972cf6
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Sasha Smundak
2022-07-07 04:41:23 +00:00
committed by Automerger Merge Worker

View File

@@ -188,26 +188,26 @@ type MockBazelContext struct {
LabelToPythonBinary map[string]string LabelToPythonBinary map[string]string
} }
func (m MockBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) { func (m MockBazelContext) QueueBazelRequest(_ string, _ cqueryRequest, _ configKey) {
panic("unimplemented") panic("unimplemented")
} }
func (m MockBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) { func (m MockBazelContext) GetOutputFiles(label string, _ configKey) ([]string, error) {
result, _ := m.LabelToOutputFiles[label] result, _ := m.LabelToOutputFiles[label]
return result, nil return result, nil
} }
func (m MockBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) { func (m MockBazelContext) GetCcInfo(label string, _ configKey) (cquery.CcInfo, error) {
result, _ := m.LabelToCcInfo[label] result, _ := m.LabelToCcInfo[label]
return result, nil return result, nil
} }
func (m MockBazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) { func (m MockBazelContext) GetPythonBinary(label string, _ configKey) (string, error) {
result, _ := m.LabelToPythonBinary[label] result, _ := m.LabelToPythonBinary[label]
return result, nil return result, nil
} }
func (m MockBazelContext) InvokeBazel(config Config) error { func (m MockBazelContext) InvokeBazel(_ Config) error {
panic("unimplemented") panic("unimplemented")
} }
@@ -261,23 +261,23 @@ func (bazelCtx *bazelContext) GetPythonBinary(label string, cfgKey configKey) (s
return "", fmt.Errorf("no bazel response found for %v", key) return "", fmt.Errorf("no bazel response found for %v", key)
} }
func (n noopBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) { func (n noopBazelContext) QueueBazelRequest(_ string, _ cqueryRequest, _ configKey) {
panic("unimplemented") panic("unimplemented")
} }
func (n noopBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) { func (n noopBazelContext) GetOutputFiles(_ string, _ configKey) ([]string, error) {
panic("unimplemented") panic("unimplemented")
} }
func (n noopBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) { func (n noopBazelContext) GetCcInfo(_ string, _ configKey) (cquery.CcInfo, error) {
panic("unimplemented") panic("unimplemented")
} }
func (n noopBazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) { func (n noopBazelContext) GetPythonBinary(_ string, _ configKey) (string, error) {
panic("unimplemented") panic("unimplemented")
} }
func (n noopBazelContext) InvokeBazel(config Config) error { func (n noopBazelContext) InvokeBazel(_ Config) error {
panic("unimplemented") panic("unimplemented")
} }
@@ -319,7 +319,7 @@ func bazelPathsFromConfig(c *config) (*bazelPaths, error) {
p := bazelPaths{ p := bazelPaths{
soongOutDir: c.soongOutDir, soongOutDir: c.soongOutDir,
} }
missingEnvVars := []string{} var missingEnvVars []string
if len(c.Getenv("BAZEL_HOME")) > 1 { if len(c.Getenv("BAZEL_HOME")) > 1 {
p.homeDir = c.Getenv("BAZEL_HOME") p.homeDir = c.Getenv("BAZEL_HOME")
} else { } else {
@@ -380,10 +380,8 @@ type mockBazelRunner struct {
extraFlags []string extraFlags []string
} }
func (r *mockBazelRunner) issueBazelCommand(paths *bazelPaths, func (r *mockBazelRunner) issueBazelCommand(_ *bazelPaths, _ bazel.RunName,
runName bazel.RunName, command bazelCommand, extraFlags ...string) (string, string, error) {
command bazelCommand,
extraFlags ...string) (string, string, error) {
r.commands = append(r.commands, command) r.commands = append(r.commands, command)
r.extraFlags = append(r.extraFlags, strings.Join(extraFlags, " ")) r.extraFlags = append(r.extraFlags, strings.Join(extraFlags, " "))
if ret, ok := r.bazelCommandResults[command]; ok { if ret, ok := r.bazelCommandResults[command]; ok {
@@ -411,7 +409,9 @@ func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.
command.command, command.command,
} }
cmdFlags = append(cmdFlags, command.expression) cmdFlags = append(cmdFlags, command.expression)
cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(paths, runName)) cmdFlags = append(cmdFlags,
// TODO(asmundak): is it needed in every build?
"--profile="+shared.BazelMetricsFilename(paths, runName),
// Set default platforms to canonicalized values for mixed builds requests. // Set default platforms to canonicalized values for mixed builds requests.
// If these are set in the bazelrc, they will have values that are // If these are set in the bazelrc, they will have values that are
@@ -420,17 +420,19 @@ func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.
// //
// The actual platform values here may be overridden by configuration // The actual platform values here may be overridden by configuration
// transitions from the buildroot. // transitions from the buildroot.
cmdFlags = append(cmdFlags, fmt.Sprintf("--platforms=%s", "//build/bazel/platforms:android_target"),
fmt.Sprintf("--platforms=%s", "//build/bazel/platforms:android_target")) fmt.Sprintf("--extra_toolchains=%s", "//prebuilts/clang/host/linux-x86:all"),
cmdFlags = append(cmdFlags,
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 // This should be parameterized on the host OS, but let's restrict to linux
// to keep things simple for now. // to keep things simple for now.
cmdFlags = append(cmdFlags, fmt.Sprintf("--host_platform=%s", "//build/bazel/platforms:linux_x86_64"),
fmt.Sprintf("--host_platform=%s", "//build/bazel/platforms:linux_x86_64"))
// Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network. // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network.
cmdFlags = append(cmdFlags, "--experimental_repository_disable_download") "--experimental_repository_disable_download",
// Suppress noise
"--ui_event_filters=-INFO",
"--noshow_progress")
cmdFlags = append(cmdFlags, extraFlags...) cmdFlags = append(cmdFlags, extraFlags...)
bazelCmd := exec.Command(paths.bazelPath, cmdFlags...) bazelCmd := exec.Command(paths.bazelPath, cmdFlags...)
@@ -697,8 +699,6 @@ func (p *bazelPaths) outDir() string {
func (context *bazelContext) InvokeBazel(config Config) error { func (context *bazelContext) InvokeBazel(config Config) error {
context.results = make(map[cqueryKey]string) context.results = make(map[cqueryKey]string)
var cqueryOutput string
var cqueryErr string
var err error var err error
soongInjectionPath := absolutePath(context.paths.injectedFilesDir()) soongInjectionPath := absolutePath(context.paths.injectedFilesDir())
@@ -715,45 +715,27 @@ func (context *bazelContext) InvokeBazel(config Config) error {
return err return err
} }
} }
err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666) if err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666); err != nil {
if err != nil {
return err return err
} }
if err = ioutil.WriteFile(filepath.Join(mixedBuildsPath, "main.bzl"), context.mainBzlFileContents(), 0666); err != nil {
err = ioutil.WriteFile(
filepath.Join(mixedBuildsPath, "main.bzl"),
context.mainBzlFileContents(), 0666)
if err != nil {
return err return err
} }
if err = ioutil.WriteFile(filepath.Join(mixedBuildsPath, "BUILD.bazel"), context.mainBuildFileContents(), 0666); err != nil {
err = ioutil.WriteFile(
filepath.Join(mixedBuildsPath, "BUILD.bazel"),
context.mainBuildFileContents(), 0666)
if err != nil {
return err return err
} }
cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery") cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery")
err = ioutil.WriteFile( if err = ioutil.WriteFile(absolutePath(cqueryFileRelpath), context.cqueryStarlarkFileContents(), 0666); err != nil {
absolutePath(cqueryFileRelpath),
context.cqueryStarlarkFileContents(), 0666)
if err != nil {
return err return err
} }
buildrootLabel := "@soong_injection//mixed_builds:buildroot" const buildrootLabel = "@soong_injection//mixed_builds:buildroot"
cqueryOutput, cqueryErr, err = context.issueBazelCommand( cqueryCmd := bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)}
context.paths, cqueryOutput, cqueryErr, err := context.issueBazelCommand(context.paths, bazel.CqueryBuildRootRunName, cqueryCmd,
bazel.CqueryBuildRootRunName, "--output=starlark", "--starlark:file="+absolutePath(cqueryFileRelpath))
bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)},
"--output=starlark",
"--starlark:file="+absolutePath(cqueryFileRelpath))
err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"),
[]byte(cqueryOutput), 0666)
if err != nil { if err != nil {
return err err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"), []byte(cqueryOutput), 0666)
} }
if err != nil { if err != nil {
return err return err
} }
@@ -765,7 +747,6 @@ func (context *bazelContext) InvokeBazel(config Config) error {
cqueryResults[splitLine[0]] = splitLine[1] cqueryResults[splitLine[0]] = splitLine[1]
} }
} }
for val := range context.requests { for val := range context.requests {
if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok { if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok {
context.results[val] = cqueryResult context.results[val] = cqueryResult
@@ -777,37 +758,27 @@ func (context *bazelContext) InvokeBazel(config Config) error {
// Issue an aquery command to retrieve action information about the bazel build tree. // Issue an aquery command to retrieve action information about the bazel build tree.
// //
var aqueryOutput string
var coverageFlags []string
if Bool(config.productVariables.ClangCoverage) {
coverageFlags = append(coverageFlags, "--collect_code_coverage")
if len(config.productVariables.NativeCoveragePaths) > 0 ||
len(config.productVariables.NativeCoverageExcludePaths) > 0 {
includePaths := JoinWithPrefixAndSeparator(config.productVariables.NativeCoveragePaths, "+", ",")
excludePaths := JoinWithPrefixAndSeparator(config.productVariables.NativeCoverageExcludePaths, "-", ",")
if len(includePaths) > 0 && len(excludePaths) > 0 {
includePaths += ","
}
coverageFlags = append(coverageFlags, fmt.Sprintf(`--instrumentation_filter=%s`,
includePaths+excludePaths))
}
}
extraFlags := append([]string{"--output=jsonproto", "--include_file_write_contents"}, coverageFlags...)
aqueryOutput, _, err = context.issueBazelCommand(
context.paths,
bazel.AqueryBuildRootRunName,
bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)},
// Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's // 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. // proto sources, which would add a number of unnecessary dependencies.
extraFlags...) extraFlags := []string{"--output=jsonproto", "--include_file_write_contents"}
if Bool(config.productVariables.ClangCoverage) {
if err != nil { extraFlags = append(extraFlags, "--collect_code_coverage")
return err paths := make([]string, 0, 2)
if p := config.productVariables.NativeCoveragePaths; len(p) > 0 {
paths = append(paths, JoinWithPrefixAndSeparator(p, "+", ","))
} }
if p := config.productVariables.NativeCoverageExcludePaths; len(p) > 0 {
paths = append(paths, JoinWithPrefixAndSeparator(p, "-", ","))
}
if len(paths) > 0 {
extraFlags = append(extraFlags, "--instrumentation_filter="+strings.Join(paths, ","))
}
}
aqueryCmd := bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)}
if aqueryOutput, _, err := context.issueBazelCommand(context.paths, bazel.AqueryBuildRootRunName, aqueryCmd,
extraFlags...); err == nil {
context.buildStatements, context.depsets, err = bazel.AqueryBuildStatements([]byte(aqueryOutput)) context.buildStatements, context.depsets, err = bazel.AqueryBuildStatements([]byte(aqueryOutput))
}
if err != nil { if err != nil {
return err return err
} }
@@ -815,12 +786,8 @@ func (context *bazelContext) InvokeBazel(config Config) error {
// Issue a build command of the phony root to generate symlink forests for dependencies of the // Issue a build command of the phony root to generate symlink forests for dependencies of the
// Bazel build. This is necessary because aquery invocations do not generate this symlink forest, // Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
// but some of symlinks may be required to resolve source dependencies of the build. // but some of symlinks may be required to resolve source dependencies of the build.
_, _, err = context.issueBazelCommand( buildCmd := bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"}
context.paths, if _, _, err = context.issueBazelCommand(context.paths, bazel.BazelBuildPhonyRootRunName, buildCmd); err != nil {
bazel.BazelBuildPhonyRootRunName,
bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"})
if err != nil {
return err return err
} }