]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: simplify fingerprint logic
authorMatthew Dempsky <mdempsky@google.com>
Wed, 23 Mar 2022 19:22:50 +0000 (12:22 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 25 Mar 2022 21:58:03 +0000 (21:58 +0000)
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>
src/cmd/compile/internal/noder/import.go
src/cmd/link/internal/ld/ld.go
test/fixedbugs/bug369.go

index 0898a298ebc5496095a95bb9079927a04e457522..7ba1b23d12eb9d228945627bffb7fb645a17c487 100644 (file)
@@ -369,16 +369,8 @@ func addFingerprint(path string, f *os.File, end int64) error {
        }
 
        copy(fingerprint[:], buf[:])
+       base.Ctxt.AddImport(path, fingerprint)
 
-       // assume files move (get installed) so don't record the full path
-       if base.Flag.Cfg.PackageFile != nil {
-               // If using a packageFile map, assume path_ can be recorded directly.
-               base.Ctxt.AddImport(path, fingerprint)
-       } else {
-               // For file "/Users/foo/go/pkg/darwin_amd64/math.a" record "math.a".
-               file := f.Name()
-               base.Ctxt.AddImport(file[len(file)-len(path)-len(".a"):], fingerprint)
-       }
        return nil
 }
 
index 954921844c9280a30ec0a8f3f360248c74f6afa0..aaad152e6ff682ac5d37d5f24985c0fe4335d611 100644 (file)
@@ -96,19 +96,7 @@ func (ctxt *Link) readImportCfg(file string) {
 }
 
 func pkgname(ctxt *Link, lib string) string {
-       name := path.Clean(lib)
-
-       // When using importcfg, we have the final package name.
-       if ctxt.PackageFile != nil {
-               return name
-       }
-
-       // runtime.a -> runtime, runtime.6 -> runtime
-       pkg := name
-       if len(pkg) >= 2 && pkg[len(pkg)-2] == '.' {
-               pkg = pkg[:len(pkg)-2]
-       }
-       return pkg
+       return path.Clean(lib)
 }
 
 func findlib(ctxt *Link, lib string) (string, bool) {
@@ -127,34 +115,25 @@ func findlib(ctxt *Link, lib string) (string, bool) {
                        return "", false
                }
        } else {
-               if filepath.IsAbs(name) {
-                       pname = name
-               } else {
-                       pkg := pkgname(ctxt, lib)
-                       // Add .a if needed; the new -importcfg modes
-                       // do not put .a into the package name anymore.
-                       // This only matters when people try to mix
-                       // compiles using -importcfg with links not using -importcfg,
-                       // such as when running quick things like
-                       // 'go tool compile x.go && go tool link x.o'
-                       // by hand against a standard library built using -importcfg.
-                       if !strings.HasSuffix(name, ".a") && !strings.HasSuffix(name, ".o") {
-                               name += ".a"
-                       }
-                       // try dot, -L "libdir", and then goroot.
-                       for _, dir := range ctxt.Libdir {
-                               if ctxt.linkShared {
-                                       pname = filepath.Join(dir, pkg+".shlibname")
-                                       if _, err := os.Stat(pname); err == nil {
-                                               isshlib = true
-                                               break
-                                       }
-                               }
-                               pname = filepath.Join(dir, name)
+               pkg := pkgname(ctxt, lib)
+
+               // search -L "libdir" directories
+               for _, dir := range ctxt.Libdir {
+                       if ctxt.linkShared {
+                               pname = filepath.Join(dir, pkg+".shlibname")
                                if _, err := os.Stat(pname); err == nil {
+                                       isshlib = true
                                        break
                                }
                        }
+                       pname = filepath.Join(dir, name+".a")
+                       if _, err := os.Stat(pname); err == nil {
+                               break
+                       }
+                       pname = filepath.Join(dir, name+".o")
+                       if _, err := os.Stat(pname); err == nil {
+                               break
+                       }
                }
                pname = filepath.Clean(pname)
        }
index 8e50678c89915db0910af7d8a041df81b0a347d0..5bababd63ec2432d795434c26bf8227274b371a4 100644 (file)
@@ -1,4 +1,4 @@
-// +build !nacl,!js,!windows,gc
+// +build !nacl,!js,gc
 // run
 
 // Copyright 2011 The Go Authors. All rights reserved.
@@ -29,10 +29,12 @@ func main() {
                return filepath.Join(tmpDir, name)
        }
 
-       run("go", "tool", "compile", "-p=pkg", "-N", "-o", tmp("slow.o"), "pkg.go")
-       run("go", "tool", "compile", "-p=pkg", "-o", tmp("fast.o"), "pkg.go")
-       run("go", "tool", "compile", "-p=main", "-D", tmpDir, "-o", tmp("main.o"), "main.go")
-       run("go", "tool", "link", "-o", tmp("a.exe"), tmp("main.o"))
+       check(os.Mkdir(tmp("test"), 0777))
+
+       run("go", "tool", "compile", "-p=test/slow", "-N", "-o", tmp("test/slow.o"), "pkg.go")
+       run("go", "tool", "compile", "-p=test/fast", "-o", tmp("test/fast.o"), "pkg.go")
+       run("go", "tool", "compile", "-p=main", "-D", "test", "-I", tmpDir, "-o", tmp("main.o"), "main.go")
+       run("go", "tool", "link", "-L", tmpDir, "-o", tmp("a.exe"), tmp("main.o"))
        run(tmp("a.exe"))
 }