Skip to content

Commit 49f2893

Browse files
committed
Disable CGo when loading packages.
GopherJS doesn't support CGo. This was originally enabled to address gopherjs#215 by providing a better error when attempting to build a CGo package (instead of a compiler panic). However, this approach makes GopherJS refuse to build packages, which have a CGo and purego versions guarded by the `cgo` build tag: GopherJS woudl try to load the cgo version and error out. After this change GopherJS won't attempt to load Go sources with `import "C"`, and it will load sources with `//go:build !cgo` instead. For packages that require CGo one of the following errors will be returned: - "No buildable sources" if all Go sources import C. - "Undefined symbol" for symbols defined in the Go sources that import C and that are referenced by other Go source files. - "Unknown import path C" if you try to `gopherjs run` a source with import C directly.
1 parent 41de9e3 commit 49f2893

File tree

3 files changed

+1
-28
lines changed

3 files changed

+1
-28
lines changed

build/build.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@ var DefaultGOROOT = func() string {
4747
return build.Default.GOROOT
4848
}()
4949

50-
// ImportCError is returned when GopherJS attempts to build a package that uses
51-
// CGo.
52-
type ImportCError struct {
53-
pkgPath string
54-
}
55-
56-
func (e *ImportCError) Error() string {
57-
return e.pkgPath + `: importing "C" is not supported by GopherJS`
58-
}
59-
6050
// NewBuildContext creates a build context for building Go packages
6151
// with GopherJS compiler.
6252
//

build/context.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ func (sc simpleCtx) Import(importPath string, srcDir string, mode build.ImportMo
9898
}
9999
pkg = sc.applyPostloadTweaks(pkg)
100100

101-
if len(pkg.CgoFiles) > 0 {
102-
return nil, &ImportCError{pkg.ImportPath}
103-
}
104-
105101
return &PackageData{
106102
Package: pkg,
107103
IsVirtual: sc.isVirtual,
@@ -224,10 +220,6 @@ func (sc simpleCtx) applyPreloadTweaks(importPath string, srcDir string, mode bu
224220
bctx.GOARCH = "wasm"
225221
}
226222
switch importPath {
227-
case "crypto/x509", "os/user":
228-
// These stdlib packages have cgo and non-cgo versions (via build tags); we
229-
// want the latter.
230-
bctx.CgoEnabled = false
231223
case "github.com/gopherjs/gopherjs/js", "github.com/gopherjs/gopherjs/nosync":
232224
// These packages are already embedded via gopherjspkg.FS virtual filesystem
233225
// (which can be safely vendored). Don't try to use vendor directory to
@@ -342,7 +334,7 @@ func goCtx(e Env) *simpleCtx {
342334
InstallSuffix: e.InstallSuffix,
343335
Compiler: "gc",
344336
BuildTags: append(append([]string{}, e.BuildTags...), defaultBuildTags...),
345-
CgoEnabled: true, // detect `import "C"` to throw proper error
337+
CgoEnabled: false, // CGo is not supported by GopherJS.
346338

347339
// go/build supports modules, but only when no FS access functions are
348340
// overridden and when provided ReleaseTags match those of the default

compiler/linkname.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,6 @@ func parseGoLinknames(fset *token.FileSet, pkgPath string, file *ast.File) ([]Go
101101

102102
obj := file.Scope.Lookup(localName)
103103
if obj == nil {
104-
if pkgPath == "syscall" {
105-
// Syscall uses go:cgo_import_dynamic pragma to import symbols from
106-
// dynamic libraries when build with GOOS=darwin, which GopherJS doesn't
107-
// support. Silently ignore such directives.
108-
//
109-
// In the long term https://github.com/gopherjs/gopherjs/issues/693 is a
110-
// preferred solution.
111-
return nil
112-
}
113104
return fmt.Errorf("//go:linkname local symbol %q is not found in the current source file", localName)
114105
}
115106

0 commit comments

Comments
 (0)