@@ -23,17 +23,11 @@ package gcimporter // import "golang.org/x/tools/internal/gcimporter"
23
23
24
24
import (
25
25
"bufio"
26
- "bytes"
27
26
"fmt"
28
- "go/build"
29
27
"go/token"
30
28
"go/types"
31
29
"io"
32
30
"os"
33
- "os/exec"
34
- "path/filepath"
35
- "strings"
36
- "sync"
37
31
)
38
32
39
33
const (
@@ -45,119 +39,6 @@ const (
45
39
trace = false
46
40
)
47
41
48
- var exportMap sync.Map // package dir → func() (string, bool)
49
-
50
- // lookupGorootExport returns the location of the export data
51
- // (normally found in the build cache, but located in GOROOT/pkg
52
- // in prior Go releases) for the package located in pkgDir.
53
- //
54
- // (We use the package's directory instead of its import path
55
- // mainly to simplify handling of the packages in src/vendor
56
- // and cmd/vendor.)
57
- func lookupGorootExport (pkgDir string ) (string , bool ) {
58
- f , ok := exportMap .Load (pkgDir )
59
- if ! ok {
60
- var (
61
- listOnce sync.Once
62
- exportPath string
63
- )
64
- f , _ = exportMap .LoadOrStore (pkgDir , func () (string , bool ) {
65
- listOnce .Do (func () {
66
- cmd := exec .Command ("go" , "list" , "-export" , "-f" , "{{.Export}}" , pkgDir )
67
- cmd .Dir = build .Default .GOROOT
68
- var output []byte
69
- output , err := cmd .Output ()
70
- if err != nil {
71
- return
72
- }
73
-
74
- exports := strings .Split (string (bytes .TrimSpace (output )), "\n " )
75
- if len (exports ) != 1 {
76
- return
77
- }
78
-
79
- exportPath = exports [0 ]
80
- })
81
-
82
- return exportPath , exportPath != ""
83
- })
84
- }
85
-
86
- return f .(func () (string , bool ))()
87
- }
88
-
89
- var pkgExts = [... ]string {".a" , ".o" }
90
-
91
- // FindPkg returns the filename and unique package id for an import
92
- // path based on package information provided by build.Import (using
93
- // the build.Default build.Context). A relative srcDir is interpreted
94
- // relative to the current working directory.
95
- // If no file was found, an empty filename is returned.
96
- func FindPkg (path , srcDir string ) (filename , id string ) {
97
- if path == "" {
98
- return
99
- }
100
-
101
- var noext string
102
- switch {
103
- default :
104
- // "x" -> "$GOPATH/pkg/$GOOS_$GOARCH/x.ext", "x"
105
- // Don't require the source files to be present.
106
- if abs , err := filepath .Abs (srcDir ); err == nil { // see issue 14282
107
- srcDir = abs
108
- }
109
- bp , _ := build .Import (path , srcDir , build .FindOnly | build .AllowBinary )
110
- if bp .PkgObj == "" {
111
- var ok bool
112
- if bp .Goroot && bp .Dir != "" {
113
- filename , ok = lookupGorootExport (bp .Dir )
114
- }
115
- if ! ok {
116
- id = path // make sure we have an id to print in error message
117
- return
118
- }
119
- } else {
120
- noext = strings .TrimSuffix (bp .PkgObj , ".a" )
121
- id = bp .ImportPath
122
- }
123
-
124
- case build .IsLocalImport (path ):
125
- // "./x" -> "/this/directory/x.ext", "/this/directory/x"
126
- noext = filepath .Join (srcDir , path )
127
- id = noext
128
-
129
- case filepath .IsAbs (path ):
130
- // for completeness only - go/build.Import
131
- // does not support absolute imports
132
- // "/x" -> "/x.ext", "/x"
133
- noext = path
134
- id = path
135
- }
136
-
137
- if false { // for debugging
138
- if path != id {
139
- fmt .Printf ("%s -> %s\n " , path , id )
140
- }
141
- }
142
-
143
- if filename != "" {
144
- if f , err := os .Stat (filename ); err == nil && ! f .IsDir () {
145
- return
146
- }
147
- }
148
-
149
- // try extensions
150
- for _ , ext := range pkgExts {
151
- filename = noext + ext
152
- if f , err := os .Stat (filename ); err == nil && ! f .IsDir () {
153
- return
154
- }
155
- }
156
-
157
- filename = "" // not found
158
- return
159
- }
160
-
161
42
// Import imports a gc-generated package given its import path and srcDir, adds
162
43
// the corresponding package object to the packages map, and returns the object.
163
44
// The packages map must contain all packages already imported.
0 commit comments