mirror of
https://github.com/golang/go.git
synced 2025-05-28 10:51:22 +00:00
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:
parent
67f6b8c987
commit
9a932c5712
@ -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"):]
|
||||
|
@ -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):]
|
||||
}
|
||||
|
||||
|
@ -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):]
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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 != "" {
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user