Skip to content

Commit c7f5b35

Browse files
ianlancetaylorgopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 86ad2d5 commit c7f5b35

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

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

+25-4
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,15 @@ const (
684684
// GetTestDeps is for download (part of "go get") and indicates
685685
// that test dependencies should be fetched too.
686686
GetTestDeps
687+
688+
// The remainder are internal modes for calls to loadImport.
689+
690+
// cmdlinePkg is for a package mentioned on the command line.
691+
cmdlinePkg
692+
693+
// cmdlinePkgLiteral is for a package mentioned on the command line
694+
// without using any wildcards or meta-patterns.
695+
cmdlinePkgLiteral
687696
)
688697

689698
// 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
738747
return p
739748
}
740749

750+
setCmdline := func(p *Package) {
751+
if mode&cmdlinePkg != 0 {
752+
p.Internal.CmdlinePkg = true
753+
}
754+
if mode&cmdlinePkgLiteral != 0 {
755+
p.Internal.CmdlinePkgLiteral = true
756+
}
757+
}
758+
741759
importPath := bp.ImportPath
742760
p := packageCache[importPath]
743761
if p != nil {
744762
stk.Push(path)
745763
p = reusePackage(p, stk)
746764
stk.Pop()
765+
setCmdline(p)
747766
} else {
748767
p = new(Package)
749768
p.Internal.Local = build.IsLocalImport(path)
750769
p.ImportPath = importPath
751770
packageCache[importPath] = p
752771

772+
setCmdline(p)
773+
753774
// Load package.
754775
// loadPackageData may return bp != nil even if an error occurs,
755776
// in order to return partial information.
@@ -2849,15 +2870,15 @@ func PackagesAndErrors(ctx context.Context, opts PackageOpts, patterns []string)
28492870
if pkg == "" {
28502871
panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern()))
28512872
}
2852-
p := loadImport(ctx, opts, pre, pkg, base.Cwd(), nil, &stk, nil, 0)
2853-
p.Match = append(p.Match, m.Pattern())
2854-
p.Internal.CmdlinePkg = true
2873+
mode := cmdlinePkg
28552874
if m.IsLiteral() {
28562875
// Note: do not set = m.IsLiteral unconditionally
28572876
// because maybe we'll see p matching both
28582877
// a literal and also a non-literal pattern.
2859-
p.Internal.CmdlinePkgLiteral = true
2878+
mode |= cmdlinePkgLiteral
28602879
}
2880+
p := loadImport(ctx, opts, pre, pkg, base.Cwd(), nil, &stk, nil, mode)
2881+
p.Match = append(p.Match, m.Pattern())
28612882
if seenPkg[p] {
28622883
continue
28632884
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[short] skip 'links and runs binaries'
2+
3+
# This test requires external linking. Assume that if cgo is supported
4+
# then external linking works.
5+
[!cgo] skip 'requires a C linker'
6+
7+
# Only run on Unix systems.
8+
[GOOS:windows] skip
9+
[GOOS:plan9] skip
10+
11+
# Ordinary build should work.
12+
go build
13+
exec ./hello
14+
stdout Hello
15+
16+
# Building with -linkmode=external should not say anything about
17+
# runtime/cgo (issue #31544).
18+
go build -ldflags=-linkmode=external
19+
! stderr runtime/cgo
20+
exec ./hello
21+
stdout Hello
22+
23+
# Some targets don't support -static
24+
[GOOS:darwin] skip 'no static linking on Darwin'
25+
[GOOS:solaris] skip 'no static linking on Solaris'
26+
27+
# Building with -linkmode=external -extldflags=-static should work.
28+
go build -ldflags='-linkmode=external -extldflags=-static'
29+
! stderr runtime/cgo
30+
exec ./hello
31+
stdout Hello
32+
33+
-- go.mod --
34+
module hello
35+
36+
go 1.20
37+
-- hello.go --
38+
package main
39+
40+
import "fmt"
41+
42+
func main() {
43+
fmt.Println("Hello, world")
44+
}

0 commit comments

Comments
 (0)