internal/buildcfg: initialize GOROOT to runtime.GOROOT

In the beginning the Go compiler was in C, and C had a function
'getgoroot' that returned GOROOT from either the environment or a
generated constant. 'getgoroot' was mechanically converted to Go
(as obj.Getgoroot) in CL 3046.

obj.Getgoroot begat obj.GOROOT. obj.GOROOT begat objabi.GOROOT,
which begat buildcfg.GOROOT.

As far as I can tell, today's buildcfg.GOROOT is functionally
identical to runtime.GOROOT(). Let's reduce some complexity by
defining it in those terms.

While we're thinking about buildcfg.GOROOT, also check whether it is
non-empty: if the toolchain is built with -trimpath, the value of
GOROOT might not be valid or meaningful if the user invokes
cmd/compile or cmd/link directly, or via a build tool other than
cmd/go that doesn't care as much about GOROOT. (As of CL 390024,
runtime.GOROOT will return the empty string instead of a bogus one
when built with -trimpath.)

For #51461.

Change-Id: I9fec020d5fa65d4aff0dd39b805f5ca93f86c36e
Reviewed-on: https://go-review.googlesource.com/c/go/+/393155
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Bryan C. Mills 2022-03-15 16:31:02 -04:00 committed by Bryan Mills
parent 67f6b8c987
commit 9a932c5712
6 changed files with 8 additions and 8 deletions

View File

@ -405,7 +405,7 @@ func uriIfy(f string) DocumentURI {
// Return filename, replacing a first occurrence of $GOROOT with the
// actual value of the GOROOT (because LSP does not speak "$GOROOT").
func uprootedPath(filename string) string {
if !strings.HasPrefix(filename, "$GOROOT/") {
if buildcfg.GOROOT == "" || !strings.HasPrefix(filename, "$GOROOT/") {
return filename
}
return buildcfg.GOROOT + filename[len("$GOROOT"):]

View File

@ -209,7 +209,7 @@ func (pr *pkgReader) posBaseIdx(idx int) *src.PosBase {
// require being more consistent about when we use native vs UNIX
// file paths.
const dollarGOROOT = "$GOROOT"
if strings.HasPrefix(filename, dollarGOROOT) {
if buildcfg.GOROOT != "" && strings.HasPrefix(filename, dollarGOROOT) {
filename = buildcfg.GOROOT + filename[len(dollarGOROOT):]
}

View File

@ -39,7 +39,7 @@ func AbsFile(dir, file, rewrites string) string {
}
abs, rewritten := ApplyRewrites(abs, rewrites)
if !rewritten && hasPathPrefix(abs, buildcfg.GOROOT) {
if !rewritten && buildcfg.GOROOT != "" && hasPathPrefix(abs, buildcfg.GOROOT) {
abs = "$GOROOT" + abs[len(buildcfg.GOROOT):]
}

View File

@ -390,7 +390,9 @@ func libinit(ctxt *Link) {
suffix = "asan"
}
Lflag(ctxt, filepath.Join(buildcfg.GOROOT, "pkg", fmt.Sprintf("%s_%s%s%s", buildcfg.GOOS, buildcfg.GOARCH, suffixsep, suffix)))
if buildcfg.GOROOT != "" {
Lflag(ctxt, filepath.Join(buildcfg.GOROOT, "pkg", fmt.Sprintf("%s_%s%s%s", buildcfg.GOOS, buildcfg.GOARCH, suffixsep, suffix)))
}
mayberemoveoutfile()

View File

@ -121,7 +121,6 @@ func Main(arch *sys.Arch, theArch Arch) {
final := gorootFinal()
addstrdata1(ctxt, "runtime.defaultGOROOT="+final)
addstrdata1(ctxt, "internal/buildcfg.defaultGOROOT="+final)
buildVersion := buildcfg.Version
if goexperiment := buildcfg.Experiment.String(); goexperiment != "" {

View File

@ -15,13 +15,12 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
var (
defaultGOROOT string // set by linker
GOROOT = envOr("GOROOT", defaultGOROOT)
GOROOT = runtime.GOROOT() // cached for efficiency
GOARCH = envOr("GOARCH", defaultGOARCH)
GOOS = envOr("GOOS", defaultGOOS)
GO386 = envOr("GO386", defaultGO386)