1
0
mirror of https://github.com/golang/go synced 2024-11-23 23:00:03 -07:00

cmd/go: report implicit cgo inputs in go list -compiled

Tools using go list -compiled expect to see an Imports list
that includes all the imports in CompiledGoFiles.
Make sure the list includes the cgo-generated imports.

Fixes #26136.

Change-Id: I6cfe14063f8edfe65a7af37522c7551272115b82
Reviewed-on: https://go-review.googlesource.com/128935
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
Russ Cox 2018-08-09 23:44:43 -04:00
parent e652b7e63f
commit 64205cd4b6
3 changed files with 51 additions and 9 deletions

View File

@ -20,6 +20,7 @@ import (
"cmd/go/internal/cfg" "cmd/go/internal/cfg"
"cmd/go/internal/load" "cmd/go/internal/load"
"cmd/go/internal/modload" "cmd/go/internal/modload"
"cmd/go/internal/str"
"cmd/go/internal/work" "cmd/go/internal/work"
) )
@ -146,7 +147,8 @@ instead of using the template format.
The -compiled flag causes list to set CompiledGoFiles to the Go source The -compiled flag causes list to set CompiledGoFiles to the Go source
files presented to the compiler. Typically this means that it repeats files presented to the compiler. Typically this means that it repeats
the files listed in GoFiles and then also adds the Go code generated the files listed in GoFiles and then also adds the Go code generated
by processing CgoFiles and SwigFiles. by processing CgoFiles and SwigFiles. The Imports list contains the
union of all imports from both GoFiles and CompiledGoFiles.
The -deps flag causes list to iterate over not just the named packages The -deps flag causes list to iterate over not just the named packages
but also all their dependencies. It visits them in a depth-first post-order but also all their dependencies. It visits them in a depth-first post-order
@ -517,6 +519,10 @@ func runList(cmd *base.Command, args []string) {
p.TestImports = p.Resolve(p.TestImports) p.TestImports = p.Resolve(p.TestImports)
p.XTestImports = p.Resolve(p.XTestImports) p.XTestImports = p.Resolve(p.XTestImports)
p.DepOnly = !cmdline[p] p.DepOnly = !cmdline[p]
if *listCompiled {
p.Imports = str.StringList(p.Imports, p.Internal.CompiledImports)
}
} }
if *listTest { if *listTest {

View File

@ -157,6 +157,7 @@ type PackageInternal struct {
// Unexported fields are not part of the public API. // Unexported fields are not part of the public API.
Build *build.Package Build *build.Package
Imports []*Package // this package's direct imports Imports []*Package // this package's direct imports
CompiledImports []string // additional Imports necessary when using CompiledGoFiles (all from standard library)
RawImports []string // this package's original imports as they appear in the text of the program RawImports []string // this package's original imports as they appear in the text of the program
ForceLibrary bool // this package is a library (even if named "main") ForceLibrary bool // this package is a library (even if named "main")
CmdlineFiles bool // package built from files listed on command line CmdlineFiles bool // package built from files listed on command line
@ -1327,31 +1328,37 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
// Build augmented import list to add implicit dependencies. // Build augmented import list to add implicit dependencies.
// Be careful not to add imports twice, just to avoid confusion. // Be careful not to add imports twice, just to avoid confusion.
importPaths := p.Imports importPaths := p.Imports
addImport := func(path string) { addImport := func(path string, forCompiler bool) {
for _, p := range importPaths { for _, p := range importPaths {
if path == p { if path == p {
return return
} }
} }
importPaths = append(importPaths, path) importPaths = append(importPaths, path)
if forCompiler {
p.Internal.CompiledImports = append(p.Internal.CompiledImports, path)
}
} }
// Cgo translation adds imports of "runtime/cgo" and "syscall", // Cgo translation adds imports of "unsafe", "runtime/cgo" and "syscall",
// except for certain packages, to avoid circular dependencies. // except for certain packages, to avoid circular dependencies.
if p.UsesCgo() {
addImport("unsafe", true)
}
if p.UsesCgo() && (!p.Standard || !cgoExclude[p.ImportPath]) && cfg.BuildContext.Compiler != "gccgo" { if p.UsesCgo() && (!p.Standard || !cgoExclude[p.ImportPath]) && cfg.BuildContext.Compiler != "gccgo" {
addImport("runtime/cgo") addImport("runtime/cgo", true)
} }
if p.UsesCgo() && (!p.Standard || !cgoSyscallExclude[p.ImportPath]) { if p.UsesCgo() && (!p.Standard || !cgoSyscallExclude[p.ImportPath]) {
addImport("syscall") addImport("syscall", true)
} }
// SWIG adds imports of some standard packages. // SWIG adds imports of some standard packages.
if p.UsesSwig() { if p.UsesSwig() {
if cfg.BuildContext.Compiler != "gccgo" { if cfg.BuildContext.Compiler != "gccgo" {
addImport("runtime/cgo") addImport("runtime/cgo", true)
} }
addImport("syscall") addImport("syscall", true)
addImport("sync") addImport("sync", true)
// TODO: The .swig and .swigcxx files can use // TODO: The .swig and .swigcxx files can use
// %go_import directives to import other packages. // %go_import directives to import other packages.
@ -1360,7 +1367,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
// The linker loads implicit dependencies. // The linker loads implicit dependencies.
if p.Name == "main" && !p.Internal.ForceLibrary { if p.Name == "main" && !p.Internal.ForceLibrary {
for _, dep := range LinkerDeps(p) { for _, dep := range LinkerDeps(p) {
addImport(dep) addImport(dep, false)
} }
} }

View File

@ -0,0 +1,29 @@
[!cgo] skip
# go list should report import "C"
cd x
go list -f '{{.Imports}}'
! stdout runtime/cgo
! stdout unsafe
! stdout syscall
stdout C
stdout unicode
stdout unicode/utf16
# go list -compiled should report imports in compiled files as well,
# adding "runtime/cgo", "unsafe", and "syscall" but not dropping "C".
go list -compiled -f '{{.Imports}}'
stdout runtime/cgo
stdout unsafe
stdout syscall
stdout C
stdout unicode
stdout unicode/utf16
-- x/x.go --
package x
import "C"
import "unicode" // does not use unsafe, syscall, runtime/cgo, unicode/utf16
-- x/x1.go --
package x
import "unicode/utf16" // does not use unsafe, syscall, runtime/cgo, unicode