Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
Move cgoEnabled out of importer/
Browse files Browse the repository at this point in the history
  • Loading branch information
davecheney committed Dec 31, 2015
1 parent 8c78ffd commit 8e47ba9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 68 deletions.
1 change: 0 additions & 1 deletion cmd/gb/gb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,6 @@ func main() { println("hello world") }
gb.cd(gb.tempdir)
tmpdir := gb.tempDir("tmp")
gb.setenv("TMP", tmpdir)
gb.setenv("DEBUG", ".")
gb.run("build")
gb.mustBeEmpty(tmpdir)
name := "hello"
Expand Down
55 changes: 44 additions & 11 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ import (
"github.com/constabulary/gb/importer"
)

// Importer resolves the import path to a package.
type Importer interface {
Import(path string) (*importer.Package, error)
}

// Context represents an execution of one or more Targets inside a Project.
type Context struct {
*Project

*importer.Context
importers []Importer
importers []interface {
Import(path string) (*importer.Package, error)
}

pkgs map[string]*Package // map of package paths to resolved packages

Expand Down Expand Up @@ -161,15 +157,18 @@ func (p *Project) NewContext(opts ...func(*Context) error) (*Context, error) {
ic := importer.Context{
GOOS: ctx.gotargetos,
GOARCH: ctx.gotargetarch,
CgoEnabled: cgoEnabled, // TODO(dfc)
CgoEnabled: cgoEnabled(ctx.gohostos, ctx.gohostarch, ctx.gotargetos, ctx.gotargetarch),
ReleaseTags: releaseTags,
BuildTags: ctx.buildtags,
}

ctx.importers = append(ctx.importers,
&importer.Importer{
Context: &ic,
Root: runtime.GOROOT(),
})
},
)

for _, dir := range p.Srcdirs() {
ctx.importers = append(ctx.importers,
&importer.Importer{
Expand All @@ -178,8 +177,6 @@ func (p *Project) NewContext(opts ...func(*Context) error) (*Context, error) {
})
}

ctx.Context = &ic

// C and unsafe are fake packages synthesised by the compiler.
// Insert fake packages into the package cache.
for _, name := range []string{"C", "unsafe"} {
Expand Down Expand Up @@ -510,3 +507,39 @@ NextVar:
}
return out
}

func cgoEnabled(gohostos, gohostarch, gotargetos, gotargetarch string) bool {
switch os.Getenv("CGO_ENABLED") {
case "1":
return true
case "0":
return false
default:
// cgo must be explicitly enabled for cross compilation builds
if gohostos == gotargetos && gohostarch == gotargetarch {
switch gotargetos + "/" + gotargetarch {
case "darwin/386", "darwin/amd64", "darwin/arm", "darwin/arm64":
return true
case "dragonfly/amd64":
return true
case "freebsd/386", "freebsd/amd64", "freebsd/arm":
return true
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le":
return true
case "android/386", "android/amd64", "android/arm":
return true
case "netbsd/386", "netbsd/amd64", "netbsd/arm":
return true
case "openbsd/386", "openbsd/amd64":
return true
case "solaris/amd64":
return true
case "windows/386", "windows/amd64":
return true
default:
return false
}
}
return false
}
}
19 changes: 19 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,22 @@ func TestContextLoadPackage(t *testing.T) {
}
}
}

func TestCgoEnabled(t *testing.T) {
tests := []struct {
gohostos, gohostarch string
gotargetos, gotargetarch string
want bool
}{{
"linux", "amd64", "linux", "amd64", true,
}, {
"linux", "amd64", "linux", "386", false,
}}

for _, tt := range tests {
got := cgoEnabled(tt.gohostos, tt.gohostarch, tt.gotargetos, tt.gotargetarch)
if got != tt.want {
t.Errorf("cgoEnabled(%q, %q, %q, %q): got %v, want %v", tt.gohostos, tt.gohostarch, tt.gotargetos, tt.gotargetarch, got, tt.want)
}
}
}
1 change: 0 additions & 1 deletion gb.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

var releaseTags = build.Default.ReleaseTags
var cgoEnabled = build.Default.CgoEnabled

// Toolchain represents a standardised set of command line tools
// used to build and test Go programs.
Expand Down
36 changes: 0 additions & 36 deletions importer/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,42 +368,6 @@ func saveCgo(di *Package, filename string, cg *ast.CommentGroup) error {
return nil
}

func cgoEnabled(gohostos, gohostarch, gotargetos, gotargetarch string) bool {
switch os.Getenv("CGO_ENABLED") {
case "1":
return true
case "0":
return false
default:
// cgo must be explicitly enabled for cross compilation builds
if gohostos == gotargetos && gohostarch == gotargetarch {
switch gotargetos + "/" + gotargetarch {
case "darwin/386", "darwin/amd64", "darwin/arm", "darwin/arm64":
return true
case "dragonfly/amd64":
return true
case "freebsd/386", "freebsd/amd64", "freebsd/arm":
return true
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le":
return true
case "android/386", "android/amd64", "android/arm":
return true
case "netbsd/386", "netbsd/amd64", "netbsd/arm":
return true
case "openbsd/386", "openbsd/amd64":
return true
case "solaris/amd64":
return true
case "windows/386", "windows/amd64":
return true
default:
return false
}
}
return false
}
}

func cleanImports(m map[string][]token.Position) ([]string, map[string][]token.Position) {
if len(m) == 0 {
return nil, nil
Expand Down
19 changes: 0 additions & 19 deletions importer/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,3 @@ func TestSplitQuoted(t *testing.T) {
}
}
}

func TestCgoEnabled(t *testing.T) {
tests := []struct {
gohostos, gohostarch string
gotargetos, gotargetarch string
want bool
}{{
"linux", "amd64", "linux", "amd64", true,
}, {
"linux", "amd64", "linux", "386", false,
}}

for _, tt := range tests {
got := cgoEnabled(tt.gohostos, tt.gohostarch, tt.gotargetos, tt.gotargetarch)
if got != tt.want {
t.Errorf("cgoEnabled(%q, %q, %q, %q): got %v, want %v", tt.gohostos, tt.gohostarch, tt.gotargetos, tt.gotargetarch, got, tt.want)
}
}
}

0 comments on commit 8e47ba9

Please sign in to comment.