Skip to content

Commit 0be2d52

Browse files
committed
cmd/go: use -importcfg to invoke compiler, linker
This is a step toward using cached build artifacts: the importcfg will direct the compiler and linker to read them right from the cache if necessary. However, this CL does not have a cache yet, so it still reads them from the usual install location or build location. Even so, this fixes a long-standing issue that -I and -L (no longer used) are not expressive enough to describe complex GOPATH setups. Shared libraries are handled enough that all.bash passes, but there may still be more work to do here. If so, tests and fixes can be added in follow-up CLs. Gccgo will need updating to support -importcfg as well. Fixes #14271. Change-Id: I5c52a0a5df0ffbf7436e1130c74e9e24fceff80f Reviewed-on: https://go-review.googlesource.com/56279 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: David Crawshaw <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent a299345 commit 0be2d52

File tree

7 files changed

+220
-155
lines changed

7 files changed

+220
-155
lines changed

misc/cgo/testshared/shared_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func run(t *testing.T, msg string, args ...string) {
4747
func goCmd(t *testing.T, args ...string) {
4848
newargs := []string{args[0], "-installsuffix=" + suffix}
4949
if testing.Verbose() {
50-
newargs = append(newargs, "-v")
50+
newargs = append(newargs, "-x")
5151
}
5252
newargs = append(newargs, args[1:]...)
5353
c := exec.Command("go", newargs...)
@@ -58,6 +58,7 @@ func goCmd(t *testing.T, args ...string) {
5858
c.Stdout = os.Stdout
5959
c.Stderr = os.Stderr
6060
err = c.Run()
61+
output = []byte("(output above)")
6162
} else {
6263
output, err = c.CombinedOutput()
6364
}

src/cmd/go/internal/cfg/cfg.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,28 @@ func isGOROOT(path string) bool {
128128
}
129129
return stat.IsDir()
130130
}
131+
132+
// ExternalLinkingForced reports whether external linking is being
133+
// forced even for programs that do not use cgo.
134+
func ExternalLinkingForced() bool {
135+
if !BuildContext.CgoEnabled {
136+
return false
137+
}
138+
// Currently build modes c-shared, pie (on systems that do not
139+
// support PIE with internal linking mode (currently all
140+
// systems: issue #18968)), plugin, and -linkshared force
141+
// external linking mode, as of course does
142+
// -ldflags=-linkmode=external. External linking mode forces
143+
// an import of runtime/cgo.
144+
pieCgo := BuildBuildmode == "pie"
145+
linkmodeExternal := false
146+
for i, a := range BuildLdflags {
147+
if a == "-linkmode=external" {
148+
linkmodeExternal = true
149+
}
150+
if a == "-linkmode" && i+1 < len(BuildLdflags) && BuildLdflags[i+1] == "external" {
151+
linkmodeExternal = true
152+
}
153+
}
154+
return BuildBuildmode == "c-shared" || BuildBuildmode == "plugin" || pieCgo || BuildLinkshared || linkmodeExternal
155+
}

src/cmd/go/internal/load/pkg.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type PackageInternal struct {
9999
SFiles []string
100100
AllGoFiles []string // gofiles + IgnoredGoFiles, absolute paths
101101
Target string // installed file for this package (may be executable)
102+
Pkgfile string // where package will be (or is already) built or installed
102103
Fake bool // synthesized package
103104
External bool // synthesized external test package
104105
ForceLibrary bool // this package is a library (even if named "main")
@@ -951,26 +952,8 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
951952
importPaths = append(importPaths, "syscall")
952953
}
953954

954-
if cfg.BuildContext.CgoEnabled && p.Name == "main" && !p.Goroot {
955-
// Currently build modes c-shared, pie (on systems that do not
956-
// support PIE with internal linking mode (currently all
957-
// systems: issue #18968)), plugin, and -linkshared force
958-
// external linking mode, as of course does
959-
// -ldflags=-linkmode=external. External linking mode forces
960-
// an import of runtime/cgo.
961-
pieCgo := cfg.BuildBuildmode == "pie"
962-
linkmodeExternal := false
963-
for i, a := range cfg.BuildLdflags {
964-
if a == "-linkmode=external" {
965-
linkmodeExternal = true
966-
}
967-
if a == "-linkmode" && i+1 < len(cfg.BuildLdflags) && cfg.BuildLdflags[i+1] == "external" {
968-
linkmodeExternal = true
969-
}
970-
}
971-
if cfg.BuildBuildmode == "c-shared" || cfg.BuildBuildmode == "plugin" || pieCgo || cfg.BuildLinkshared || linkmodeExternal {
972-
importPaths = append(importPaths, "runtime/cgo")
973-
}
955+
if p.Name == "main" && !p.Goroot && cfg.ExternalLinkingForced() {
956+
importPaths = append(importPaths, "runtime/cgo")
974957
}
975958

976959
// Everything depends on runtime, except runtime, its internal

src/cmd/go/internal/test/test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,11 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
887887

888888
// The generated main also imports testing, regexp, and os.
889889
stk.Push("testmain")
890-
for _, dep := range testMainDeps {
890+
deps := testMainDeps
891+
if cfg.ExternalLinkingForced() {
892+
deps = str.StringList(deps, "runtime/cgo")
893+
}
894+
for _, dep := range deps {
891895
if dep == ptest.ImportPath {
892896
pmain.Internal.Imports = append(pmain.Internal.Imports, ptest)
893897
} else {

0 commit comments

Comments
 (0)