@@ -26,13 +26,12 @@ import (
26
26
"github.com/gopherjs/gopherjs/compiler"
27
27
"github.com/gopherjs/gopherjs/compiler/astutil"
28
28
"github.com/gopherjs/gopherjs/compiler/gopherjspkg"
29
- "github.com/gopherjs/gopherjs/compiler/natives"
29
+
30
30
"github.com/neelance/sourcemap"
31
31
"github.com/shurcooL/httpfs/vfsutil"
32
32
"golang.org/x/tools/go/buildutil"
33
33
34
34
"github.com/gopherjs/gopherjs/build/cache"
35
- _ "github.com/gopherjs/gopherjs/build/versionhack" // go/build release tags hack.
36
35
)
37
36
38
37
// DefaultGOROOT is the default GOROOT value for builds.
@@ -48,27 +47,20 @@ var DefaultGOROOT = func() string {
48
47
return build .Default .GOROOT
49
48
}()
50
49
51
- // ImportCError is returned when GopherJS attempts to build a package that uses
52
- // CGo.
53
- type ImportCError struct {
54
- pkgPath string
55
- }
56
-
57
- func (e * ImportCError ) Error () string {
58
- return e .pkgPath + `: importing "C" is not supported by GopherJS`
59
- }
60
-
61
50
// NewBuildContext creates a build context for building Go packages
62
51
// with GopherJS compiler.
63
52
//
64
53
// Core GopherJS packages (i.e., "github.com/gopherjs/gopherjs/js", "github.com/gopherjs/gopherjs/nosync")
65
54
// are loaded from gopherjspkg.FS virtual filesystem if not present in GOPATH or
66
55
// go.mod.
67
56
func NewBuildContext (installSuffix string , buildTags []string ) XContext {
68
- gopherjsRoot := filepath .Join (DefaultGOROOT , "src" , "github.com" , "gopherjs" , "gopherjs" )
57
+ e := DefaultEnv ()
58
+ e .InstallSuffix = installSuffix
59
+ e .BuildTags = buildTags
60
+ realGOROOT := goCtx (e )
69
61
return & chainedCtx {
70
- primary : goCtx ( installSuffix , buildTags ) ,
71
- secondary : embeddedCtx ( & withPrefix { gopherjspkg . FS , gopherjsRoot }, installSuffix , buildTags ),
62
+ primary : realGOROOT ,
63
+ secondary : gopherjsCtx ( e ),
72
64
}
73
65
}
74
66
@@ -103,7 +95,7 @@ func statFile(path string) (os.FileInfo, error) {
103
95
func Import (path string , mode build.ImportMode , installSuffix string , buildTags []string ) (* PackageData , error ) {
104
96
wd , err := os .Getwd ()
105
97
if err != nil {
106
- // Getwd may fail if we're in GOARCH =js mode. That's okay, handle
98
+ // Getwd may fail if we're in GOOS =js mode. That's okay, handle
107
99
// it by falling back to empty working directory. It just means
108
100
// Import will not be able to resolve relative import paths.
109
101
wd = ""
@@ -180,15 +172,7 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
180
172
importPath = importPath [:len (importPath )- 5 ]
181
173
}
182
174
183
- nativesContext := embeddedCtx (& withPrefix {fs : natives .FS , prefix : DefaultGOROOT }, "" , nil )
184
-
185
- if importPath == "syscall" {
186
- // Special handling for the syscall package, which uses OS native
187
- // GOOS/GOARCH pair. This will no longer be necessary after
188
- // https://github.com/gopherjs/gopherjs/issues/693.
189
- nativesContext .bctx .GOARCH = build .Default .GOARCH
190
- nativesContext .bctx .BuildTags = append (nativesContext .bctx .BuildTags , "js" )
191
- }
175
+ nativesContext := overlayCtx (xctx .Env ())
192
176
193
177
if nativesPkg , err := nativesContext .Import (importPath , "" , 0 ); err == nil {
194
178
names := nativesPkg .GoFiles
@@ -321,8 +305,6 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
321
305
322
306
// Options controls build process behavior.
323
307
type Options struct {
324
- GOROOT string
325
- GOPATH string
326
308
Verbose bool
327
309
Quiet bool
328
310
Watch bool
@@ -366,6 +348,10 @@ type PackageData struct {
366
348
bctx * build.Context // The original build context this package came from.
367
349
}
368
350
351
+ func (p PackageData ) String () string {
352
+ return fmt .Sprintf ("%s [is_test=%v]" , p .ImportPath , p .IsTest )
353
+ }
354
+
369
355
// InternalBuildContext returns the build context that produced the package.
370
356
//
371
357
// WARNING: This function is a part of internal API and will be removed in
@@ -439,30 +425,26 @@ type Session struct {
439
425
440
426
// NewSession creates a new GopherJS build session.
441
427
func NewSession (options * Options ) (* Session , error ) {
442
- if options .GOROOT == "" {
443
- options .GOROOT = DefaultGOROOT
444
- }
445
- if options .GOPATH == "" {
446
- options .GOPATH = build .Default .GOPATH
447
- }
448
428
options .Verbose = options .Verbose || options .Watch
449
429
450
- // Go distribution version check.
451
- if err := compiler .CheckGoVersion (options .GOROOT ); err != nil {
452
- return nil , err
453
- }
454
-
455
430
s := & Session {
456
431
options : options ,
457
432
UpToDateArchives : make (map [string ]* compiler.Archive ),
458
433
}
459
434
s .xctx = NewBuildContext (s .InstallSuffix (), s .options .BuildTags )
435
+ env := s .xctx .Env ()
436
+
437
+ // Go distribution version check.
438
+ if err := compiler .CheckGoVersion (env .GOROOT ); err != nil {
439
+ return nil , err
440
+ }
441
+
460
442
s .buildCache = cache.BuildCache {
461
- GOOS : s . xctx . GOOS () ,
462
- GOARCH : "js" ,
463
- GOROOT : options .GOROOT ,
464
- GOPATH : options .GOPATH ,
465
- BuildTags : options .BuildTags ,
443
+ GOOS : env . GOOS ,
444
+ GOARCH : env . GOARCH ,
445
+ GOROOT : env .GOROOT ,
446
+ GOPATH : env .GOPATH ,
447
+ BuildTags : append ([] string {}, env .BuildTags ... ) ,
466
448
Minify : options .Minify ,
467
449
TestedPackage : options .TestedPackage ,
468
450
}
@@ -496,7 +478,7 @@ func (s *Session) InstallSuffix() string {
496
478
497
479
// GoRelease returns Go release version this session is building with.
498
480
func (s * Session ) GoRelease () string {
499
- return compiler .GoRelease (s .options .GOROOT )
481
+ return compiler .GoRelease (s .xctx . Env () .GOROOT )
500
482
}
501
483
502
484
// BuildFiles passed to the GopherJS tool as if they were a package.
@@ -513,7 +495,7 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, packagePath stri
513
495
// This ephemeral package doesn't have a unique import path to be used as a
514
496
// build cache key, so we never cache it.
515
497
SrcModTime : time .Now ().Add (time .Hour ),
516
- bctx : & goCtx (s .InstallSuffix (), s . options . BuildTags ).bctx ,
498
+ bctx : & goCtx (s .xctx . Env () ).bctx ,
517
499
}
518
500
519
501
for _ , file := range filenames {
@@ -667,6 +649,12 @@ func (s *Session) ImportResolverFor(pkg *PackageData) func(string) (*compiler.Ar
667
649
}
668
650
}
669
651
652
+ // SourceMappingCallback returns a call back for compiler.SourceMapFilter
653
+ // configured for the current build session.
654
+ func (s * Session ) SourceMappingCallback (m * sourcemap.Map ) func (generatedLine , generatedColumn int , originalPos token.Position ) {
655
+ return NewMappingCallback (m , s .xctx .Env ().GOROOT , s .xctx .Env ().GOPATH , s .options .MapToLocalDisk )
656
+ }
657
+
670
658
// WriteCommandPackage writes the final JavaScript output file at pkgObj path.
671
659
func (s * Session ) WriteCommandPackage (archive * compiler.Archive , pkgObj string ) error {
672
660
if err := os .MkdirAll (filepath .Dir (pkgObj ), 0777 ); err != nil {
@@ -692,7 +680,7 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string)
692
680
fmt .Fprintf (codeFile , "//# sourceMappingURL=%s.map\n " , filepath .Base (pkgObj ))
693
681
}()
694
682
695
- sourceMapFilter .MappingCallback = NewMappingCallback ( m , s . options . GOROOT , s . options . GOPATH , s . options . MapToLocalDisk )
683
+ sourceMapFilter .MappingCallback = s . SourceMappingCallback ( m )
696
684
}
697
685
698
686
deps , err := compiler .ImportDependencies (archive , func (path string ) (* compiler.Archive , error ) {
0 commit comments