]> Cypherpunks.ru repositories - gostls13.git/commitdiff
internal/buildcfg: initialize GOROOT to runtime.GOROOT
authorBryan C. Mills <bcmills@google.com>
Tue, 15 Mar 2022 20:31:02 +0000 (16:31 -0400)
committerBryan Mills <bcmills@google.com>
Fri, 18 Mar 2022 21:52:11 +0000 (21:52 +0000)
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>

src/cmd/compile/internal/logopt/log_opts.go
src/cmd/compile/internal/noder/reader.go
src/cmd/internal/objabi/line.go
src/cmd/link/internal/ld/lib.go
src/cmd/link/internal/ld/main.go
src/internal/buildcfg/cfg.go

index 97ebf569448982ddc478cbe5cc2cf6211aa8bc2e..9fee83426f60309e88e95f5b446e9a1182f30dcc 100644 (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"):]
index 62875ba0730df9b48477f566b3aad1a759602e52..01e795183d853961bbb6d4d62fa66954224481dd 100644 (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):]
        }
 
index 0b1e0bb181c4d3866d96a1a0077a1c678c942c08..beee1291b554b48ede2cbc8f76e435f90f97125f 100644 (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):]
        }
 
index a81232b2a4908eb1e4aeceabdc6fb38bfa6f2239..61b1fcbecfa3e413bb9d131348da00b9d8e35230 100644 (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()
 
index d13c3ff8b61cc2b9adce7fe5e5b1009c3f3edbea..14f83566f5f1aa3e3f291d5f7ad7266d8a721978 100644 (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 != "" {
index 68c10a282465887f1b3b5aa2fb3f436888e8cf40..1066d0c18961e88d1e919d4a1e48940fcdd4b5ee 100644 (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)