Skip to content

Commit cd35323

Browse files
timothy-kingGo LUCI
authored and
Go LUCI
committed
internal/exportdata: introduce shared library for exportdata
Deduplicates FindPkg and FindExportData which were shared by go/internal/gcimporter and cmd/compile/internal/importer into a new package internal/exportdata. This change only moves code. Change-Id: I1daf24dd79fafbe9014b2b15671dcde46b54711e Reviewed-on: https://go-review.googlesource.com/c/go/+/626700 Commit-Queue: Tim King <taking@google.com> Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 9fe70bc commit cd35323

File tree

10 files changed

+244
-434
lines changed

10 files changed

+244
-434
lines changed

src/cmd/compile/internal/importer/exportdata.go

-76
This file was deleted.

src/cmd/compile/internal/importer/gcimporter.go

+4-136
Original file line numberDiff line numberDiff line change
@@ -2,155 +2,23 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// This file contains the FindPkg and Import functions for tests
6-
// to use gc-generated object files.
5+
// This file implements the Import function for tests to use gc-generated object files.
76

87
package importer
98

109
import (
1110
"bufio"
12-
"bytes"
13-
"errors"
1411
"fmt"
15-
"go/build"
12+
"internal/exportdata"
1613
"internal/pkgbits"
1714
"internal/saferio"
1815
"io"
1916
"os"
20-
"os/exec"
21-
"path/filepath"
2217
"strings"
23-
"sync"
2418

2519
"cmd/compile/internal/types2"
2620
)
2721

28-
var exportMap sync.Map // package dir → func() (string, error)
29-
30-
// lookupGorootExport returns the location of the export data
31-
// (normally found in the build cache, but located in GOROOT/pkg
32-
// in prior Go releases) for the package located in pkgDir.
33-
//
34-
// (We use the package's directory instead of its import path
35-
// mainly to simplify handling of the packages in src/vendor
36-
// and cmd/vendor.)
37-
func lookupGorootExport(pkgDir string) (string, error) {
38-
f, ok := exportMap.Load(pkgDir)
39-
if !ok {
40-
var (
41-
listOnce sync.Once
42-
exportPath string
43-
err error
44-
)
45-
f, _ = exportMap.LoadOrStore(pkgDir, func() (string, error) {
46-
listOnce.Do(func() {
47-
cmd := exec.Command(filepath.Join(build.Default.GOROOT, "bin", "go"), "list", "-export", "-f", "{{.Export}}", pkgDir)
48-
cmd.Dir = build.Default.GOROOT
49-
cmd.Env = append(os.Environ(), "PWD="+cmd.Dir, "GOROOT="+build.Default.GOROOT)
50-
var output []byte
51-
output, err = cmd.Output()
52-
if err != nil {
53-
if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
54-
err = errors.New(string(ee.Stderr))
55-
}
56-
return
57-
}
58-
59-
exports := strings.Split(string(bytes.TrimSpace(output)), "\n")
60-
if len(exports) != 1 {
61-
err = fmt.Errorf("go list reported %d exports; expected 1", len(exports))
62-
return
63-
}
64-
65-
exportPath = exports[0]
66-
})
67-
68-
return exportPath, err
69-
})
70-
}
71-
72-
return f.(func() (string, error))()
73-
}
74-
75-
var pkgExts = [...]string{".a", ".o"} // a file from the build cache will have no extension
76-
77-
// FindPkg returns the filename and unique package id for an import
78-
// path based on package information provided by build.Import (using
79-
// the build.Default build.Context). A relative srcDir is interpreted
80-
// relative to the current working directory.
81-
//
82-
// This function should only be used in tests.
83-
func FindPkg(path, srcDir string) (filename, id string, err error) {
84-
// TODO(taking): move FindPkg into src/internal and dedup src/go/internal/gcimporter.FindPkg
85-
86-
if path == "" {
87-
return "", "", errors.New("path is empty")
88-
}
89-
90-
var noext string
91-
switch {
92-
default:
93-
// "x" -> "$GOPATH/pkg/$GOOS_$GOARCH/x.ext", "x"
94-
// Don't require the source files to be present.
95-
if abs, err := filepath.Abs(srcDir); err == nil { // see issue 14282
96-
srcDir = abs
97-
}
98-
var bp *build.Package
99-
bp, err = build.Import(path, srcDir, build.FindOnly|build.AllowBinary)
100-
if bp.PkgObj == "" {
101-
if bp.Goroot && bp.Dir != "" {
102-
filename, err = lookupGorootExport(bp.Dir)
103-
if err == nil {
104-
_, err = os.Stat(filename)
105-
}
106-
if err == nil {
107-
return filename, bp.ImportPath, nil
108-
}
109-
}
110-
goto notfound
111-
} else {
112-
noext = strings.TrimSuffix(bp.PkgObj, ".a")
113-
}
114-
id = bp.ImportPath
115-
116-
case build.IsLocalImport(path):
117-
// "./x" -> "/this/directory/x.ext", "/this/directory/x"
118-
noext = filepath.Join(srcDir, path)
119-
id = noext
120-
121-
case filepath.IsAbs(path):
122-
// for completeness only - go/build.Import
123-
// does not support absolute imports
124-
// "/x" -> "/x.ext", "/x"
125-
noext = path
126-
id = path
127-
}
128-
129-
if false { // for debugging
130-
if path != id {
131-
fmt.Printf("%s -> %s\n", path, id)
132-
}
133-
}
134-
135-
// try extensions
136-
for _, ext := range pkgExts {
137-
filename = noext + ext
138-
f, statErr := os.Stat(filename)
139-
if statErr == nil && !f.IsDir() {
140-
return filename, id, nil
141-
}
142-
if err == nil {
143-
err = statErr
144-
}
145-
}
146-
147-
notfound:
148-
if err == nil {
149-
return "", path, fmt.Errorf("can't find import: %q", path)
150-
}
151-
return "", path, fmt.Errorf("can't find import: %q: %w", path, err)
152-
}
153-
15422
// Import imports a gc-generated package given its import path and srcDir, adds
15523
// the corresponding package object to the packages map, and returns the object.
15624
// The packages map must contain all packages already imported.
@@ -178,7 +46,7 @@ func Import(packages map[string]*types2.Package, path, srcDir string, lookup fun
17846
rc = f
17947
} else {
18048
var filename string
181-
filename, id, err = FindPkg(path, srcDir)
49+
filename, id, err = exportdata.FindPkg(path, srcDir)
18250
if filename == "" {
18351
if path == "unsafe" {
18452
return types2.Unsafe, nil
@@ -207,7 +75,7 @@ func Import(packages map[string]*types2.Package, path, srcDir string, lookup fun
20775
defer rc.Close()
20876

20977
buf := bufio.NewReader(rc)
210-
hdr, size, err := FindExportData(buf)
78+
hdr, size, err := exportdata.FindExportData(buf)
21179
if err != nil {
21280
return
21381
}

src/cmd/compile/internal/importer/gcimporter_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"cmd/compile/internal/types2"
1111
"fmt"
1212
"go/build"
13+
"internal/exportdata"
1314
"internal/testenv"
1415
"os"
1516
"os/exec"
@@ -101,7 +102,7 @@ func TestImportTestdata(t *testing.T) {
101102

102103
importMap := map[string]string{}
103104
for _, pkg := range wantImports {
104-
export, _, err := FindPkg(pkg, "testdata")
105+
export, _, err := exportdata.FindPkg(pkg, "testdata")
105106
if export == "" {
106107
t.Fatalf("no export data found for %s: %v", pkg, err)
107108
}
@@ -278,7 +279,7 @@ var importedObjectTests = []struct {
278279
{"math.Pi", "const Pi untyped float"},
279280
{"math.Sin", "func Sin(x float64) float64"},
280281
{"go/ast.NotNilFilter", "func NotNilFilter(_ string, v reflect.Value) bool"},
281-
{"go/internal/gcimporter.FindPkg", "func FindPkg(path string, srcDir string) (filename string, id string, err error)"},
282+
{"internal/exportdata.FindPkg", "func FindPkg(path string, srcDir string) (filename string, id string, err error)"},
282283

283284
// interfaces
284285
{"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key any) any}"},
@@ -440,7 +441,7 @@ func TestIssue13566(t *testing.T) {
440441
t.Fatal(err)
441442
}
442443

443-
jsonExport, _, err := FindPkg("encoding/json", "testdata")
444+
jsonExport, _, err := exportdata.FindPkg("encoding/json", "testdata")
444445
if jsonExport == "" {
445446
t.Fatalf("no export data found for encoding/json: %v", err)
446447
}

src/cmd/dist/buildtool.go

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ var bootstrapDirs = []string{
7373
"cmd/internal/cov/covcmd",
7474
"internal/bisect",
7575
"internal/buildcfg",
76+
"internal/exportdata",
7677
"internal/goarch",
7778
"internal/godebugs",
7879
"internal/goexperiment",

src/go/build/deps_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ var depsRules = `
549549
# crypto-aware packages
550550
551551
DEBUG, go/build, go/types, text/scanner, crypto/md5
552-
< internal/pkgbits
552+
< internal/pkgbits, internal/exportdata
553553
< go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
554554
< go/importer;
555555

src/go/internal/gcimporter/exportdata.go

-87
This file was deleted.

0 commit comments

Comments
 (0)