1
0
mirror of https://github.com/golang/go synced 2024-11-17 12:24:51 -07:00

cmd/go: import runtime/cgo when externally linking

Restore CL 477195, which was reverted in CL 477795.
This version includes CL 477397, which fixes the test problems
with CL 477195. CL 477397 was not submitted because it had an
unrelated failure on darwin-amd64. That failure is fixed by CL 477736.

Fixes #31544

Change-Id: I3a2258cd0ca295cede3511ab212e56fd0114f94a
Reviewed-on: https://go-review.googlesource.com/c/go/+/477839
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Ian Lance Taylor 2023-03-20 12:53:22 -07:00 committed by Gopher Robot
parent 86ad2d570c
commit c7f5b3595b
2 changed files with 69 additions and 4 deletions

View File

@ -684,6 +684,15 @@ const (
// GetTestDeps is for download (part of "go get") and indicates // GetTestDeps is for download (part of "go get") and indicates
// that test dependencies should be fetched too. // that test dependencies should be fetched too.
GetTestDeps GetTestDeps
// The remainder are internal modes for calls to loadImport.
// cmdlinePkg is for a package mentioned on the command line.
cmdlinePkg
// cmdlinePkgLiteral is for a package mentioned on the command line
// without using any wildcards or meta-patterns.
cmdlinePkgLiteral
) )
// LoadImport scans the directory named by path, which must be an import path, // LoadImport scans the directory named by path, which must be an import path,
@ -738,18 +747,30 @@ func loadImport(ctx context.Context, opts PackageOpts, pre *preload, path, srcDi
return p return p
} }
setCmdline := func(p *Package) {
if mode&cmdlinePkg != 0 {
p.Internal.CmdlinePkg = true
}
if mode&cmdlinePkgLiteral != 0 {
p.Internal.CmdlinePkgLiteral = true
}
}
importPath := bp.ImportPath importPath := bp.ImportPath
p := packageCache[importPath] p := packageCache[importPath]
if p != nil { if p != nil {
stk.Push(path) stk.Push(path)
p = reusePackage(p, stk) p = reusePackage(p, stk)
stk.Pop() stk.Pop()
setCmdline(p)
} else { } else {
p = new(Package) p = new(Package)
p.Internal.Local = build.IsLocalImport(path) p.Internal.Local = build.IsLocalImport(path)
p.ImportPath = importPath p.ImportPath = importPath
packageCache[importPath] = p packageCache[importPath] = p
setCmdline(p)
// Load package. // Load package.
// loadPackageData may return bp != nil even if an error occurs, // loadPackageData may return bp != nil even if an error occurs,
// in order to return partial information. // in order to return partial information.
@ -2849,15 +2870,15 @@ func PackagesAndErrors(ctx context.Context, opts PackageOpts, patterns []string)
if pkg == "" { if pkg == "" {
panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern())) panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern()))
} }
p := loadImport(ctx, opts, pre, pkg, base.Cwd(), nil, &stk, nil, 0) mode := cmdlinePkg
p.Match = append(p.Match, m.Pattern())
p.Internal.CmdlinePkg = true
if m.IsLiteral() { if m.IsLiteral() {
// Note: do not set = m.IsLiteral unconditionally // Note: do not set = m.IsLiteral unconditionally
// because maybe we'll see p matching both // because maybe we'll see p matching both
// a literal and also a non-literal pattern. // a literal and also a non-literal pattern.
p.Internal.CmdlinePkgLiteral = true mode |= cmdlinePkgLiteral
} }
p := loadImport(ctx, opts, pre, pkg, base.Cwd(), nil, &stk, nil, mode)
p.Match = append(p.Match, m.Pattern())
if seenPkg[p] { if seenPkg[p] {
continue continue
} }

View File

@ -0,0 +1,44 @@
[short] skip 'links and runs binaries'
# This test requires external linking. Assume that if cgo is supported
# then external linking works.
[!cgo] skip 'requires a C linker'
# Only run on Unix systems.
[GOOS:windows] skip
[GOOS:plan9] skip
# Ordinary build should work.
go build
exec ./hello
stdout Hello
# Building with -linkmode=external should not say anything about
# runtime/cgo (issue #31544).
go build -ldflags=-linkmode=external
! stderr runtime/cgo
exec ./hello
stdout Hello
# Some targets don't support -static
[GOOS:darwin] skip 'no static linking on Darwin'
[GOOS:solaris] skip 'no static linking on Solaris'
# Building with -linkmode=external -extldflags=-static should work.
go build -ldflags='-linkmode=external -extldflags=-static'
! stderr runtime/cgo
exec ./hello
stdout Hello
-- go.mod --
module hello
go 1.20
-- hello.go --
package main
import "fmt"
func main() {
fmt.Println("Hello, world")
}