Skip to content

Commit 5a428b6

Browse files
committed
Hash natives
1 parent a705e0d commit 5a428b6

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

build/build.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package build
88
import (
99
"bytes"
1010
"crypto/sha256"
11+
"errors"
1112
"fmt"
1213
"go/ast"
1314
"go/build"
@@ -240,6 +241,22 @@ func ImportDir(dir string, mode build.ImportMode, installSuffix string, buildTag
240241
return pkg, nil
241242
}
242243

244+
// newNativesContext returns a new nativesContext, with special considerations
245+
// for the syscall package.
246+
func newNativesContext(importPath string) *simpleCtx {
247+
nativesContext := embeddedCtx(&withPrefix{fs: natives.FS, prefix: DefaultGOROOT}, "", nil)
248+
249+
if importPath == "syscall" {
250+
// Special handling for the syscall package, which uses OS native
251+
// GOOS/GOARCH pair. This will no longer be necessary after
252+
// https://github.com/gopherjs/gopherjs/issues/693.
253+
nativesContext.bctx.GOARCH = build.Default.GOARCH
254+
nativesContext.bctx.BuildTags = append(nativesContext.bctx.BuildTags, "js")
255+
}
256+
257+
return nativesContext
258+
}
259+
243260
// parseAndAugment parses and returns all .go files of given pkg.
244261
// Standard Go library packages are augmented with files in compiler/natives folder.
245262
// If isTest is true and pkg.ImportPath has no _test suffix, package is built for running internal tests.
@@ -262,15 +279,7 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
262279
importPath = importPath[:len(importPath)-5]
263280
}
264281

265-
nativesContext := embeddedCtx(&withPrefix{fs: natives.FS, prefix: DefaultGOROOT}, "", nil)
266-
267-
if importPath == "syscall" {
268-
// Special handling for the syscall package, which uses OS native
269-
// GOOS/GOARCH pair. This will no longer be necessary after
270-
// https://github.com/gopherjs/gopherjs/issues/693.
271-
nativesContext.bctx.GOARCH = build.Default.GOARCH
272-
nativesContext.bctx.BuildTags = append(nativesContext.bctx.BuildTags, "js")
273-
}
282+
nativesContext := newNativesContext(importPath)
274283

275284
if nativesPkg, err := nativesContext.Import(importPath, "", 0); err == nil {
276285
names := nativesPkg.GoFiles
@@ -699,6 +708,13 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
699708
hashFile := func() error {
700709
fp := filepath.Join(pkg.Dir, name)
701710
file, err := buildutil.OpenFile(pkg.bctx, name)
711+
if errors.Is(err, os.ErrNotExist) {
712+
nativesCtx := newNativesContext(pkg.ImportPath)
713+
if nativesPkg, e := nativesCtx.Import(pkg.ImportPath, "", 0); e == nil {
714+
fullPath := path.Join(nativesPkg.Dir, name)
715+
file, err = nativesCtx.bctx.OpenFile(fullPath)
716+
}
717+
}
702718
if err != nil {
703719
return fmt.Errorf("failed to open %v: %v", fp, err)
704720
}

0 commit comments

Comments
 (0)