Skip to content

Commit f681903

Browse files
committed
build: Make fewer copies of NewBuildContext.
Call NewBuildContext at the top of Import, ImportDir, and inside NewSession. Previously, it was called more often than necessary (instead of being preserved for entire build session). Pass build context by value to importWithSrcDir so it can be safely modified there, without the changes persisisting from one package to the next. The build context will need to be available in more places in an upcoming commit. Storing it in Session helps with that.
1 parent fcfa75a commit f681903

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

build/build.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func (e *ImportCError) Error() string {
3333
return e.pkgPath + `: importing "C" is not supported by GopherJS`
3434
}
3535

36+
// NewBuildContext creates a build context for building Go packages
37+
// with GopherJS compiler.
3638
func NewBuildContext(installSuffix string, buildTags []string) *build.Context {
3739
return &build.Context{
3840
GOROOT: build.Default.GOROOT,
@@ -72,11 +74,12 @@ func Import(path string, mode build.ImportMode, installSuffix string, buildTags
7274
// Import will not be able to resolve relative import paths.
7375
wd = ""
7476
}
75-
return importWithSrcDir(path, wd, mode, installSuffix, buildTags)
77+
bctx := NewBuildContext(installSuffix, buildTags)
78+
return importWithSrcDir(*bctx, path, wd, mode, installSuffix)
7679
}
7780

78-
func importWithSrcDir(path string, srcDir string, mode build.ImportMode, installSuffix string, buildTags []string) (*PackageData, error) {
79-
bctx := NewBuildContext(installSuffix, buildTags)
81+
func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build.ImportMode, installSuffix string) (*PackageData, error) {
82+
// bctx is passed by value, so it can be modified here.
8083
switch path {
8184
case "syscall":
8285
// syscall needs to use a typical GOARCH like amd64 to pick up definitions for _Socklen, BpfInsn, IFNAMSIZ, Timeval, BpfStat, SYS_FCNTL, Flock_t, etc.
@@ -410,6 +413,7 @@ type PackageData struct {
410413

411414
type Session struct {
412415
options *Options
416+
bctx *build.Context
413417
Archives map[string]*compiler.Archive
414418
Types map[string]*types.Package
415419
Watcher *fsnotify.Watcher
@@ -428,6 +432,7 @@ func NewSession(options *Options) *Session {
428432
options: options,
429433
Archives: make(map[string]*compiler.Archive),
430434
}
435+
s.bctx = NewBuildContext(s.InstallSuffix(), s.options.BuildTags)
431436
s.Types = make(map[string]*types.Package)
432437
if options.Watch {
433438
if out, err := exec.Command("ulimit", "-n").Output(); err == nil {
@@ -456,7 +461,7 @@ func (s *Session) BuildDir(packagePath string, importPath string, pkgObj string)
456461
if s.Watcher != nil {
457462
s.Watcher.Add(packagePath)
458463
}
459-
buildPkg, err := NewBuildContext(s.InstallSuffix(), s.options.BuildTags).ImportDir(packagePath, 0)
464+
buildPkg, err := s.bctx.ImportDir(packagePath, 0)
460465
if err != nil {
461466
return err
462467
}
@@ -514,7 +519,7 @@ func (s *Session) BuildImportPath(path string) (*compiler.Archive, error) {
514519
}
515520

516521
func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*PackageData, *compiler.Archive, error) {
517-
pkg, err := importWithSrcDir(path, srcDir, 0, s.InstallSuffix(), s.options.BuildTags)
522+
pkg, err := importWithSrcDir(*s.bctx, path, srcDir, 0, s.InstallSuffix())
518523
if s.Watcher != nil && pkg != nil { // add watch even on error
519524
s.Watcher.Add(pkg.Dir)
520525
}

0 commit comments

Comments
 (0)