Skip to content

Commit c0aacdc

Browse files
ianlancetaylorgopherbot
authored andcommitted
cmd/go: import runtime/cgo when externally linking
Fixes #31544 Change-Id: Ic99875ad227876eb741e93653589310327c9c0ca Reviewed-on: https://go-review.googlesource.com/c/go/+/477195 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 2c6aaae commit c0aacdc

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

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

Lines changed: 25 additions & 4 deletions
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
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This test requires external linking. Assume that if cgo is supported
2+
# then external linking works.
3+
[!cgo] skip 'links and runs binaries'
4+
5+
# Only run on Unix systems that support -static.
6+
[GOOS:windows] skip
7+
[GOOS:plan9] skip
8+
9+
# Ordinary build should work.
10+
go build
11+
exec ./hello
12+
stdout Hello
13+
14+
# Building with -linkmode=external should not say anything about
15+
# runtime/cgo (issue #31544).
16+
go build -ldflags=-linkmode=external
17+
! stderr runtime/cgo
18+
exec ./hello
19+
stdout Hello
20+
21+
# Building with -linkmode=external -extldflags=-static should work.
22+
go build -ldflags='-linkmode=external -extldflags=-static'
23+
! stderr runtime/cgo
24+
exec ./hello
25+
stdout Hello
26+
27+
-- go.mod --
28+
module hello
29+
30+
go 1.20
31+
-- hello.go --
32+
package main
33+
34+
import "fmt"
35+
36+
func main() {
37+
fmt.Println("Hello, world")
38+
}

0 commit comments

Comments
 (0)