From c83398ffb410ff9539a279f9231bc1bccd086dab Mon Sep 17 00:00:00 2001 From: Chris Parsons Date: Wed, 31 May 2023 18:41:41 +0000 Subject: [PATCH] implement USE_BAZEL_VERSION for m This allows USE_BAZEL_VERSION to be set for m builds, which will use Bazelisk for any Bazel invocations during those builds. This should be used only for manual debugging, typically to either test new Bazel features, verify Bazel compatibility with Android, or culprit find new Bazel breakages. Test: Manually run builds with USE_BAZEL_VERSION, toggled off and on to ensure the build was rerun. Tested with a broken commit, a working commit, and 'last_green' special term Change-Id: I8b475dca5c8d4bd849ee3724a8c3aca9b631bcb8 --- bazel/bazel_proxy.go | 28 +++++++++++++++++++--------- cmd/soong_build/main.go | 6 ++++++ ui/build/config.go | 10 ++++++++++ ui/build/soong.go | 2 +- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/bazel/bazel_proxy.go b/bazel/bazel_proxy.go index 2940b9995..229818da0 100644 --- a/bazel/bazel_proxy.go +++ b/bazel/bazel_proxy.go @@ -26,10 +26,11 @@ import ( "time" ) -// Logs fatal events of ProxyServer. +// Logs events of ProxyServer. type ServerLogger interface { Fatal(v ...interface{}) Fatalf(format string, v ...interface{}) + Println(v ...interface{}) } // CmdRequest is a request to the Bazel Proxy server. @@ -71,9 +72,10 @@ type ProxyClient struct { // The ProxyServer will only live as long as soong_ui does; the // underlying Bazel server will live past the duration of the build. type ProxyServer struct { - logger ServerLogger - outDir string - workspaceDir string + logger ServerLogger + outDir string + workspaceDir string + bazeliskVersion string // The server goroutine will listen on this channel and stop handling requests // once it is written to. done chan struct{} @@ -119,12 +121,17 @@ func (b *ProxyClient) IssueCommand(req CmdRequest) (CmdResponse, error) { } // NewProxyServer is a constructor for a ProxyServer. -func NewProxyServer(logger ServerLogger, outDir string, workspaceDir string) *ProxyServer { +func NewProxyServer(logger ServerLogger, outDir string, workspaceDir string, bazeliskVersion string) *ProxyServer { + if len(bazeliskVersion) > 0 { + logger.Println("** Using Bazelisk for this build, due to env var USE_BAZEL_VERSION=" + bazeliskVersion + " **") + } + return &ProxyServer{ - logger: logger, - outDir: outDir, - workspaceDir: workspaceDir, - done: make(chan struct{}), + logger: logger, + outDir: outDir, + workspaceDir: workspaceDir, + done: make(chan struct{}), + bazeliskVersion: bazeliskVersion, } } @@ -155,6 +162,9 @@ func (b *ProxyServer) handleRequest(conn net.Conn) error { return fmt.Errorf("Error decoding request: %s", err) } + if len(b.bazeliskVersion) > 0 { + req.Env = append(req.Env, "USE_BAZEL_VERSION="+b.bazeliskVersion) + } stdout, stderr, cmdErr := ExecBazel("./build/bazel/bin/bazel", b.workspaceDir, req) errorString := "" if cmdErr != nil { diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go index cf6c1c7b8..d8c59b8ff 100644 --- a/cmd/soong_build/main.go +++ b/cmd/soong_build/main.go @@ -544,6 +544,12 @@ func main() { } writeMetrics(configuration, ctx.EventHandler, metricsDir) } + + // Register this environment variablesas being an implicit dependencies of + // soong_build. Changes to this environment variable will result in + // retriggering soong_build. + configuration.Getenv("USE_BAZEL_VERSION") + writeUsedEnvironmentFile(configuration) // Touch the output file so that it's the newest file created by soong_build. diff --git a/ui/build/config.go b/ui/build/config.go index 8ec96800f..b0fa5ea1f 100644 --- a/ui/build/config.go +++ b/ui/build/config.go @@ -1712,6 +1712,16 @@ func (c *configImpl) IsPersistentBazelEnabled() bool { return c.Environment().IsEnvTrue("USE_PERSISTENT_BAZEL") } +// GetBazeliskBazelVersion returns the Bazel version to use for this build, +// or the empty string if the current canonical prod Bazel should be used. +// This environment variable should only be set to debug the build system. +// The Bazel version, if set, will be passed to Bazelisk, and Bazelisk will +// handle downloading and invoking the correct Bazel binary. +func (c *configImpl) GetBazeliskBazelVersion() string { + value, _ := c.Environment().Get("USE_BAZEL_VERSION") + return value +} + func (c *configImpl) BazelModulesForceEnabledByFlag() string { return c.bazelForceEnabledModules } diff --git a/ui/build/soong.go b/ui/build/soong.go index 18bf3b912..5af58812c 100644 --- a/ui/build/soong.go +++ b/ui/build/soong.go @@ -521,7 +521,7 @@ func runSoong(ctx Context, config Config) { defer ctx.EndTrace() if config.IsPersistentBazelEnabled() { - bazelProxy := bazel.NewProxyServer(ctx.Logger, config.OutDir(), filepath.Join(config.SoongOutDir(), "workspace")) + bazelProxy := bazel.NewProxyServer(ctx.Logger, config.OutDir(), filepath.Join(config.SoongOutDir(), "workspace"), config.GetBazeliskBazelVersion()) bazelProxy.Start() defer bazelProxy.Close() }