Skip to content

Commit

Permalink
build: bootstrap: unset goarch when calling bootstrapRewriteFile
Browse files Browse the repository at this point in the history
The GOOS and GOARCH environment variables should be unset before calling
mkbuildcfg, as the target GOOS and GOARCH is not relevant while compiling the
bootstrap Go compiler using the C-based go-bootstrap go1.4 compiler.

This change fixes a build failure when GOARCH=riscv64:

Building Go toolchain1 using go-1.4-bootstrap-20171003.
src/cmd/compile/internal/ssa/rewriteRISCV64.go:4814
invalid operation: y << x (shift count type int64, must be unsigned integer)

This is because:

 - buildtool.go:198: calls bootstrapRewriteFile(src)
 - bootstrapRewriteFile: buildtool.go:283 calls:
 - isUnneededSSARewriteFile: checks os.Getenv("GOARCH")
 - isUnneededSSARewriteFile: returns "", false
 - bootstrapRewriteFile: calls bootstrapFixImports
 - boostrapFixImports: generates code go1.4 cannot compile

By unsetting GOARCH here we are causing isUnneededSSARewriteFile to return true
instead of false which generates stub functions bypassing the incompatible code.

buildtool.go:272 in isUnneededSSARewriteFile is the only place the GOARCH
environment variable is checked apart from the xinit function.

This patch simply moves the os.Setenv("GOARCH", "") to before the block of code
where bootstrapRewriteFile is called.

[Buildroot]: submitted to upstream:

- golang#52583
- https://go-review.googlesource.com/c/go/+/400376
- GitHub-Pull-Request: golang#52362

Signed-off-by: Christian Stewart <christian@paral.in>
  • Loading branch information
paralin committed May 13, 2022
1 parent a8f2a2b commit ce7ba52
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions src/cmd/dist/buildtool.go
Expand Up @@ -130,6 +130,31 @@ func bootstrapBuildTools() {
base := pathf("%s/src/bootstrap", workspace)
xmkdirall(base)

// Set up environment for invoking Go 1.4 go command.
// GOROOT points at Go 1.4 GOROOT,
// GOPATH points at our bootstrap workspace,
// GOBIN is empty, so that binaries are installed to GOPATH/bin,
// and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty,
// so that Go 1.4 builds whatever kind of binary it knows how to build.
// Restore GOROOT, GOPATH, and GOBIN when done.
// Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH,
// because setup will take care of those when bootstrapBuildTools returns.
// Note: GOARCH is read by bootstrapRewriteFile -> isUnneededSSARewriteFile.

defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
os.Setenv("GOROOT", goroot_bootstrap)

defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
os.Setenv("GOPATH", workspace)

defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
os.Setenv("GOBIN", "")

os.Setenv("GOOS", "")
os.Setenv("GOHOSTOS", "")
os.Setenv("GOARCH", "")
os.Setenv("GOHOSTARCH", "")

// Copy source code into $GOROOT/pkg/bootstrap and rewrite import paths.
writefile("module bootstrap\n", pathf("%s/%s", base, "go.mod"), 0)
for _, dir := range bootstrapDirs {
Expand Down Expand Up @@ -176,30 +201,6 @@ func bootstrapBuildTools() {
})
}

// Set up environment for invoking Go 1.4 go command.
// GOROOT points at Go 1.4 GOROOT,
// GOPATH points at our bootstrap workspace,
// GOBIN is empty, so that binaries are installed to GOPATH/bin,
// and GOOS, GOHOSTOS, GOARCH, and GOHOSTOS are empty,
// so that Go 1.4 builds whatever kind of binary it knows how to build.
// Restore GOROOT, GOPATH, and GOBIN when done.
// Don't bother with GOOS, GOHOSTOS, GOARCH, and GOHOSTARCH,
// because setup will take care of those when bootstrapBuildTools returns.

defer os.Setenv("GOROOT", os.Getenv("GOROOT"))
os.Setenv("GOROOT", goroot_bootstrap)

defer os.Setenv("GOPATH", os.Getenv("GOPATH"))
os.Setenv("GOPATH", workspace)

defer os.Setenv("GOBIN", os.Getenv("GOBIN"))
os.Setenv("GOBIN", "")

os.Setenv("GOOS", "")
os.Setenv("GOHOSTOS", "")
os.Setenv("GOARCH", "")
os.Setenv("GOHOSTARCH", "")

// Run Go 1.4 to build binaries. Use -gcflags=-l to disable inlining to
// workaround bugs in Go 1.4's compiler. See discussion thread:
// https://groups.google.com/d/msg/golang-dev/Ss7mCKsvk8w/Gsq7VYI0AwAJ
Expand Down

0 comments on commit ce7ba52

Please sign in to comment.