Skip to content

Commit

Permalink
build: Don't use cgo for "os/user", "crypto/x509".
Browse files Browse the repository at this point in the history
This changes fixes GopherJS ability to build os/user package. In turn,
this change also fixes crypto/x509 and net/http on darwin. The goal is
to ensure that os/user builds without errors, because it may end up
being imported by some stdlib packages (e.g., crypto/x509 on darwin
in Go 1.9).

This is accomplished by disabling cgo for these standard library
packages. Normally, GopherJS build context enables cgo for the purpose
of catching when a user package uses cgo and reporting that:

	CgoEnabled: true, // detect `import "C"` to throw proper error
	...
	if len(pkg.CgoFiles) > 0 {
		return nil, &ImportCError{path}
	}

But we don't want to use cgo for standard library packages. They have
support for building without cgo by using !cgo build constraints, which
we want to activate. Setting bctxt.CgoEnabled to false achieves that.

Add to CI a test of os/user package for gopherjs, as well as ensure
net/http builds successfully. We can't run its tests because there
are too many and they require the HTTP server, but only client is
implemented in GopherJS.

Fixes #664.
  • Loading branch information
dmitshur committed Aug 25, 2017
1 parent 6759a49 commit 00a3767
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 3 additions & 2 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func importWithSrcDir(path string, srcDir string, mode build.ImportMode, install
case "math/big":
// Use pure Go version of math/big; we don't want non-Go assembly versions.
bctx.BuildTags = append(bctx.BuildTags, "math_big_pure_go")
case "crypto/x509", "os/user":
// These stdlib packages have cgo and non-cgo versions (via build tags); we want the latter.
bctx.CgoEnabled = false
}
pkg, err := bctx.Import(path, srcDir, mode)
if err != nil {
Expand All @@ -109,8 +112,6 @@ func importWithSrcDir(path string, srcDir string, mode build.ImportMode, install
pkg.GoFiles = exclude(pkg.GoFiles, "fd_poll_runtime.go")
case "crypto/rand":
pkg.GoFiles = []string{"rand.go", "util.go"}
case "crypto/x509":
pkg.CgoFiles = nil
}

if len(pkg.CgoFiles) > 0 {
Expand Down
2 changes: 2 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test:
- go tool vet *.go # Go package in root directory.
- for d in */; do echo $d; done | grep -v tests/ | grep -v third_party/ | xargs go tool vet # All subdirectories except "tests", "third_party".
- diff -u <(echo -n) <(go list ./compiler/natives/src/...) # All those packages should have // +build js.
- gopherjs install -v net/http # Should build successfully (can't run tests, since only client is supported).
- >
gopherjs test -v --short
github.com/gopherjs/gopherjs/tests
Expand Down Expand Up @@ -108,6 +109,7 @@ test:
net/rpc/jsonrpc
net/textproto
net/url
os/user
path
path/filepath
reflect
Expand Down

0 comments on commit 00a3767

Please sign in to comment.