Skip to content

Commit 3ed84d1

Browse files
committed
Move compiler hash calc to init phase
1 parent f0ccf72 commit 3ed84d1

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

build/build.go

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ import (
3030
"golang.org/x/tools/go/buildutil"
3131
)
3232

33+
const (
34+
hashDebug = true
35+
)
36+
37+
var (
38+
compilerBinaryHash string
39+
)
40+
41+
func init() {
42+
// We do this here because it will only fail in truly bad situations, i.e.
43+
// machine running out of resources. We also panic if there is a problem
44+
// because it's unlikely anything else will be useful/work
45+
h, err := hashCompilerBinary()
46+
if err != nil {
47+
panic(err)
48+
}
49+
compilerBinaryHash = h
50+
}
51+
3352
type ImportCError struct {
3453
pkgPath string
3554
}
@@ -592,17 +611,9 @@ func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*Packag
592611
return pkg, archive, nil
593612
}
594613

595-
const (
596-
hashDebug = false
597-
)
598-
599-
var (
600-
gopherJsBinaryHash string
601-
)
602-
603-
func hashGopherJsBinary() (string, error) {
604-
if gopherJsBinaryHash != "" {
605-
return gopherJsBinaryHash, nil
614+
func hashCompilerBinary() (string, error) {
615+
if compilerBinaryHash != "" {
616+
return compilerBinaryHash, nil
606617
}
607618

608619
binHash := sha256.New()
@@ -618,8 +629,8 @@ func hashGopherJsBinary() (string, error) {
618629
if _, err := io.Copy(binHash, binFile); err != nil {
619630
return "", fmt.Errorf("failed to hash %v: %v", binPath, err)
620631
}
621-
gopherJsBinaryHash = fmt.Sprintf("%#x", binHash.Sum(nil))
622-
return gopherJsBinaryHash, nil
632+
compilerBinaryHash = fmt.Sprintf("%#x", binHash.Sum(nil))
633+
return compilerBinaryHash, nil
623634
}
624635

625636
func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
@@ -630,14 +641,14 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
630641
// For non-main and test packages we build up a hash that will help
631642
// determine staleness. Set hashDebug to see this in action. The format is:
632643
//
633-
// ## sync
634-
// gopherjs binary hash: 0x519d22c6ab65a950f5b6278e4d65cb75dbd3a7eb1cf16e976a40b9f1febc0446
635-
// build tags:
636-
// import: internal/race
644+
// ## <package>
645+
// compiler binary hash: 0x519d22c6ab65a950f5b6278e4d65cb75dbd3a7eb1cf16e976a40b9f1febc0446
646+
// build tags: <list of build tags>
647+
// import: <import path>
637648
// hash: 0xb966d7680c1c8ca75026f993c153aff0102dc9551f314e5352043187b5f9c9a6
638649
// ...
639650
//
640-
// file: /home/myitcv/gos/src/sync/cond.go
651+
// file: <file path>
641652
// <file contents>
642653
// N bytes
643654
// ...
@@ -652,13 +663,7 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
652663

653664
if pkg.PkgObj != "" {
654665
fmt.Fprintf(hw, "## %v\n", pkg.ImportPath)
655-
656-
binHash, err := hashGopherJsBinary()
657-
if err != nil {
658-
return nil, err
659-
}
660-
661-
fmt.Fprintf(hw, "gopherjs binary hash: %v\n", binHash)
666+
fmt.Fprintf(hw, "compiler binary hash: %v\n", compilerBinaryHash)
662667

663668
orderedBuildTags := append([]string{}, s.options.BuildTags...)
664669
sort.Strings(orderedBuildTags)

0 commit comments

Comments
 (0)