Skip to content

Commit 3dac99a

Browse files
committed
cmd/compile: simplify fingerprint logic
Historically, we sometimes recorded imports based on either package path ("net/http") or object file path ("net/http.a"). But modern Go build systems always use package path, and the extra ".a" suffix doesn't mean anything anyway. Change-Id: I6060ef8bafa324168710d152a353f4d8db062133 Reviewed-on: https://go-review.googlesource.com/c/go/+/395254 Trust: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com>
1 parent 3dac914 commit 3dac99a

File tree

3 files changed

+24
-51
lines changed

3 files changed

+24
-51
lines changed

src/cmd/compile/internal/noder/import.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,16 +369,8 @@ func addFingerprint(path string, f *os.File, end int64) error {
369369
}
370370

371371
copy(fingerprint[:], buf[:])
372+
base.Ctxt.AddImport(path, fingerprint)
372373

373-
// assume files move (get installed) so don't record the full path
374-
if base.Flag.Cfg.PackageFile != nil {
375-
// If using a packageFile map, assume path_ can be recorded directly.
376-
base.Ctxt.AddImport(path, fingerprint)
377-
} else {
378-
// For file "/Users/foo/go/pkg/darwin_amd64/math.a" record "math.a".
379-
file := f.Name()
380-
base.Ctxt.AddImport(file[len(file)-len(path)-len(".a"):], fingerprint)
381-
}
382374
return nil
383375
}
384376

src/cmd/link/internal/ld/ld.go

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,7 @@ func (ctxt *Link) readImportCfg(file string) {
9696
}
9797

9898
func pkgname(ctxt *Link, lib string) string {
99-
name := path.Clean(lib)
100-
101-
// When using importcfg, we have the final package name.
102-
if ctxt.PackageFile != nil {
103-
return name
104-
}
105-
106-
// runtime.a -> runtime, runtime.6 -> runtime
107-
pkg := name
108-
if len(pkg) >= 2 && pkg[len(pkg)-2] == '.' {
109-
pkg = pkg[:len(pkg)-2]
110-
}
111-
return pkg
99+
return path.Clean(lib)
112100
}
113101

114102
func findlib(ctxt *Link, lib string) (string, bool) {
@@ -127,34 +115,25 @@ func findlib(ctxt *Link, lib string) (string, bool) {
127115
return "", false
128116
}
129117
} else {
130-
if filepath.IsAbs(name) {
131-
pname = name
132-
} else {
133-
pkg := pkgname(ctxt, lib)
134-
// Add .a if needed; the new -importcfg modes
135-
// do not put .a into the package name anymore.
136-
// This only matters when people try to mix
137-
// compiles using -importcfg with links not using -importcfg,
138-
// such as when running quick things like
139-
// 'go tool compile x.go && go tool link x.o'
140-
// by hand against a standard library built using -importcfg.
141-
if !strings.HasSuffix(name, ".a") && !strings.HasSuffix(name, ".o") {
142-
name += ".a"
143-
}
144-
// try dot, -L "libdir", and then goroot.
145-
for _, dir := range ctxt.Libdir {
146-
if ctxt.linkShared {
147-
pname = filepath.Join(dir, pkg+".shlibname")
148-
if _, err := os.Stat(pname); err == nil {
149-
isshlib = true
150-
break
151-
}
152-
}
153-
pname = filepath.Join(dir, name)
118+
pkg := pkgname(ctxt, lib)
119+
120+
// search -L "libdir" directories
121+
for _, dir := range ctxt.Libdir {
122+
if ctxt.linkShared {
123+
pname = filepath.Join(dir, pkg+".shlibname")
154124
if _, err := os.Stat(pname); err == nil {
125+
isshlib = true
155126
break
156127
}
157128
}
129+
pname = filepath.Join(dir, name+".a")
130+
if _, err := os.Stat(pname); err == nil {
131+
break
132+
}
133+
pname = filepath.Join(dir, name+".o")
134+
if _, err := os.Stat(pname); err == nil {
135+
break
136+
}
158137
}
159138
pname = filepath.Clean(pname)
160139
}

test/fixedbugs/bug369.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !nacl,!js,!windows,gc
1+
// +build !nacl,!js,gc
22
// run
33

44
// Copyright 2011 The Go Authors. All rights reserved.
@@ -29,10 +29,12 @@ func main() {
2929
return filepath.Join(tmpDir, name)
3030
}
3131

32-
run("go", "tool", "compile", "-p=pkg", "-N", "-o", tmp("slow.o"), "pkg.go")
33-
run("go", "tool", "compile", "-p=pkg", "-o", tmp("fast.o"), "pkg.go")
34-
run("go", "tool", "compile", "-p=main", "-D", tmpDir, "-o", tmp("main.o"), "main.go")
35-
run("go", "tool", "link", "-o", tmp("a.exe"), tmp("main.o"))
32+
check(os.Mkdir(tmp("test"), 0777))
33+
34+
run("go", "tool", "compile", "-p=test/slow", "-N", "-o", tmp("test/slow.o"), "pkg.go")
35+
run("go", "tool", "compile", "-p=test/fast", "-o", tmp("test/fast.o"), "pkg.go")
36+
run("go", "tool", "compile", "-p=main", "-D", "test", "-I", tmpDir, "-o", tmp("main.o"), "main.go")
37+
run("go", "tool", "link", "-L", tmpDir, "-o", tmp("a.exe"), tmp("main.o"))
3638
run(tmp("a.exe"))
3739
}
3840

0 commit comments

Comments
 (0)