Skip to content

Commit 3c1914f

Browse files
committed
cmd/compile: use file content, not suffix, to distinguish .a and .o files
This allows reading from package storage systems that may not preserve the .a suffix (used with -importcfg). Fixes #20579 (combined with CLs earlier in stack). Change-Id: If2fc6a3d01bd0170a757e1f2ba9a22a4d9be7dbf Reviewed-on: https://go-review.googlesource.com/44853 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 4f2269e commit 3c1914f

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/cmd/compile/internal/gc/main.go

+17-24
Original file line numberDiff line numberDiff line change
@@ -750,21 +750,6 @@ func arsize(b *bufio.Reader, name string) int {
750750
return i
751751
}
752752

753-
func skiptopkgdef(b *bufio.Reader) bool {
754-
// archive header
755-
p, err := b.ReadString('\n')
756-
if err != nil {
757-
log.Fatalf("reading input: %v", err)
758-
}
759-
if p != "!<arch>\n" {
760-
return false
761-
}
762-
763-
// package export block should be first
764-
sz := arsize(b, "__.PKGDEF")
765-
return sz > 0
766-
}
767-
768753
var idirs []string
769754

770755
func addidir(dir string) {
@@ -975,14 +960,6 @@ func importfile(f *Val) *types.Pkg {
975960
defer impf.Close()
976961
imp := bufio.NewReader(impf)
977962

978-
const pkgSuffix = ".a"
979-
if strings.HasSuffix(file, pkgSuffix) {
980-
if !skiptopkgdef(imp) {
981-
yyerror("import %s: not a package file", file)
982-
errorexit()
983-
}
984-
}
985-
986963
// check object header
987964
p, err := imp.ReadString('\n')
988965
if err != nil {
@@ -992,6 +969,22 @@ func importfile(f *Val) *types.Pkg {
992969
p = p[:len(p)-1]
993970
}
994971

972+
if p == "!<arch>" { // package archive
973+
// package export block should be first
974+
sz := arsize(imp, "__.PKGDEF")
975+
if sz <= 0 {
976+
yyerror("import %s: not a package file", file)
977+
errorexit()
978+
}
979+
p, err = imp.ReadString('\n')
980+
if err != nil {
981+
log.Fatalf("reading input: %v", err)
982+
}
983+
if len(p) > 0 {
984+
p = p[:len(p)-1]
985+
}
986+
}
987+
995988
if p != "empty archive" {
996989
if !strings.HasPrefix(p, "go object ") {
997990
yyerror("import %s: not a go object file: %s", file, p)
@@ -1030,7 +1023,7 @@ func importfile(f *Val) *types.Pkg {
10301023
Ctxt.AddImport(path_)
10311024
} else {
10321025
// For file "/Users/foo/go/pkg/darwin_amd64/math.a" record "math.a".
1033-
Ctxt.AddImport(file[len(file)-len(path_)-len(pkgSuffix):])
1026+
Ctxt.AddImport(file[len(file)-len(path_)-len(".a"):])
10341027
}
10351028

10361029
// In the importfile, if we find:

0 commit comments

Comments
 (0)