Skip to content

Commit 71f27d5

Browse files
myitcvflimzy
authored andcommitted
Use better key name for gopherjs binary hash
1 parent 4041566 commit 71f27d5

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

build/build.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,32 @@ const (
594594
hashDebug = false
595595
)
596596

597+
var (
598+
gopherJsBinaryHash string
599+
)
600+
601+
func hashGopherJsBinary() (string, error) {
602+
if gopherJsBinaryHash != "" {
603+
return gopherJsBinaryHash, nil
604+
}
605+
606+
binHash := sha256.New()
607+
binPath, err := os.Executable()
608+
if err != nil {
609+
return "", fmt.Errorf("could not locate GopherJS binary: %v", err)
610+
}
611+
binFile, err := os.Open(binPath)
612+
if err != nil {
613+
return "", fmt.Errorf("could not open %v: %v", binPath, err)
614+
}
615+
defer binFile.Close()
616+
if _, err := io.Copy(binHash, binFile); err != nil {
617+
return "", fmt.Errorf("failed to hash %v: %v", binPath, err)
618+
}
619+
gopherJsBinaryHash = fmt.Sprintf("%#x", binHash.Sum(nil))
620+
return gopherJsBinaryHash, nil
621+
}
622+
597623
func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
598624
if archive, ok := s.Archives[pkg.ImportPath]; ok {
599625
return archive, nil
@@ -603,7 +629,7 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
603629
// determine staleness. Set hashDebug to see this in action. The format is:
604630
//
605631
// ## sync
606-
// gopherjs bin: 0x519d22c6ab65a950f5b6278e4d65cb75dbd3a7eb1cf16e976a40b9f1febc0446
632+
// gopherjs binary hash: 0x519d22c6ab65a950f5b6278e4d65cb75dbd3a7eb1cf16e976a40b9f1febc0446
607633
// build tags:
608634
// import: internal/race
609635
// hash: 0xb966d7680c1c8ca75026f993c153aff0102dc9551f314e5352043187b5f9c9a6
@@ -625,28 +651,13 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
625651
if pkg.PkgObj != "" {
626652
fmt.Fprintf(hw, "## %v\n", pkg.ImportPath)
627653

628-
hashBin := func() error {
629-
binHash := sha256.New()
630-
binPath, err := os.Executable()
631-
if err != nil {
632-
return fmt.Errorf("could not locate GopherJS binary: %v", err)
633-
}
634-
binFile, err := os.Open(binPath)
635-
if err != nil {
636-
return fmt.Errorf("could not open %v: %v", binPath, err)
637-
}
638-
defer binFile.Close()
639-
if _, err := io.Copy(binHash, binFile); err != nil {
640-
return fmt.Errorf("failed to hash %v: %v", binPath, err)
641-
}
642-
fmt.Fprintf(hw, "gopherjs bin: %#x\n", binHash.Sum(nil))
643-
return nil
644-
}
645-
646-
if err := hashBin(); err != nil {
654+
binHash, err := hashGopherJsBinary()
655+
if err != nil {
647656
return nil, err
648657
}
649658

659+
fmt.Fprintf(hw, "gopherjs binary hash: %v\n", binHash)
660+
650661
orderedBuildTags := append([]string{}, s.options.BuildTags...)
651662
sort.Strings(orderedBuildTags)
652663

0 commit comments

Comments
 (0)