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

Commit

Permalink
cgo: use unique directory for build
Browse files Browse the repository at this point in the history
Fixes #372

Use a unique temporary directory for cgo build files.
  • Loading branch information
davecheney committed Sep 16, 2015
2 parents 20070e3 + 1ce894b commit 4ffc2cb
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 20 deletions.
50 changes: 50 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,31 @@ func TestBuildPackages(t *testing.T) {
}
}

func TestObjfile(t *testing.T) {
var tests = []struct {
pkg string // package name
want string // objfile result
}{
{pkg: "b", want: "b/main.a"},
{pkg: "nested/a", want: "nested/a.a"},
{pkg: "nested/b", want: "nested/b.a"},
}

for _, tt := range tests {
ctx := testContext(t)
defer ctx.Destroy()
pkg, err := ctx.ResolvePackage(tt.pkg)
if err != nil {
t.Fatal(err)
}
got := objfile(pkg)
want := filepath.Join(ctx.Workdir(), tt.want)
if want != got {
t.Errorf("(%s).Objdir(): want %s, got %s", tt.pkg, want, got)
}
}
}

func TestPkgname(t *testing.T) {
tests := []struct {
pkg string
Expand All @@ -197,6 +222,31 @@ func TestPkgname(t *testing.T) {
}
}

func TestCgoobjdir(t *testing.T) {
var tests = []struct {
pkg string // package name
want string // objdir result
}{
{pkg: "b", want: "main/_cgo"},
{pkg: "nested/a", want: "nested/a/_cgo"},
{pkg: "nested/b", want: "nested/b/_cgo"},
}

for _, tt := range tests {
ctx := testContext(t)
defer ctx.Destroy()
pkg, err := ctx.ResolvePackage(tt.pkg)
if err != nil {
t.Fatal(err)
}
got := cgoobjdir(pkg)
want := filepath.Join(ctx.Workdir(), tt.want)
if want != got {
t.Errorf("(%s).cgoobjdir(): want %s, got %s", tt.pkg, want, got)
}
}
}

func sameErr(e1, e2 error) bool {
if e1 != nil && e2 != nil {
return e1.Error() == e2.Error()
Expand Down
36 changes: 20 additions & 16 deletions cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ func cgo14(pkg *Package) (*Action, []string, []string, error) {
}),
}

cgofiles := []string{filepath.Join(pkg.Objdir(), "_cgo_gotypes.go")}
cgofiles := []string{filepath.Join(cgoobjdir(pkg), "_cgo_gotypes.go")}
for _, f := range pkg.CgoFiles {
cgofiles = append(cgofiles, filepath.Join(pkg.Objdir(), stripext(f)+".cgo1.go"))
cgofiles = append(cgofiles, filepath.Join(cgoobjdir(pkg), stripext(f)+".cgo1.go"))
}
cfiles := []string{
filepath.Join(pkg.Objdir(), "_cgo_main.c"),
filepath.Join(pkg.Objdir(), "_cgo_export.c"),
filepath.Join(cgoobjdir(pkg), "_cgo_main.c"),
filepath.Join(cgoobjdir(pkg), "_cgo_export.c"),
}
cfiles = append(cfiles, pkg.CFiles...)

for _, f := range pkg.CgoFiles {
cfiles = append(cfiles, filepath.Join(pkg.Objdir(), stripext(f)+".cgo2.c"))
cfiles = append(cfiles, filepath.Join(cgoobjdir(pkg), stripext(f)+".cgo2.c"))
}

cflags := append(cgoCPPFLAGS, cgoCFLAGS...)
Expand All @@ -80,7 +80,7 @@ func cgo14(pkg *Package) (*Action, []string, []string, error) {
}),
}

dynout := filepath.Join(pkg.Objdir(), "_cgo_import.c")
dynout := filepath.Join(cgoobjdir(pkg), "_cgo_import.c")
imports := stripext(dynout) + ".o"
runcgo2 := Action{
Name: "runcgo2: " + pkg.ImportPath,
Expand Down Expand Up @@ -128,18 +128,18 @@ func cgo15(pkg *Package) (*Action, []string, []string, error) {
},
}

cgofiles := []string{filepath.Join(pkg.Objdir(), "_cgo_gotypes.go")}
cgofiles := []string{filepath.Join(cgoobjdir(pkg), "_cgo_gotypes.go")}
for _, f := range pkg.CgoFiles {
cgofiles = append(cgofiles, filepath.Join(pkg.Objdir(), stripext(f)+".cgo1.go"))
cgofiles = append(cgofiles, filepath.Join(cgoobjdir(pkg), stripext(f)+".cgo1.go"))
}
cfiles := []string{
filepath.Join(pkg.Objdir(), "_cgo_main.c"),
filepath.Join(pkg.Objdir(), "_cgo_export.c"),
filepath.Join(cgoobjdir(pkg), "_cgo_main.c"),
filepath.Join(cgoobjdir(pkg), "_cgo_export.c"),
}
cfiles = append(cfiles, pkg.CFiles...)

for _, f := range pkg.CgoFiles {
cfiles = append(cfiles, filepath.Join(pkg.Objdir(), stripext(f)+".cgo2.c"))
cfiles = append(cfiles, filepath.Join(cgoobjdir(pkg), stripext(f)+".cgo2.c"))
}

cflags := append(cgoCPPFLAGS, cgoCFLAGS...)
Expand All @@ -154,7 +154,7 @@ func cgo15(pkg *Package) (*Action, []string, []string, error) {
}),
}

dynout := filepath.Join(pkg.Objdir(), "_cgo_import.go")
dynout := filepath.Join(cgoobjdir(pkg), "_cgo_import.go")
runcgo2 := Action{
Name: "runcgo2: " + pkg.ImportPath,
Deps: []*Action{&gcc2},
Expand Down Expand Up @@ -183,7 +183,7 @@ func cgocc(pkg *Package, cflags, cxxflags, cfiles, cxxfiles []string, deps ...*A
var ofiles []string
for _, cfile := range cfiles {
cfile := cfile
ofile := filepath.Join(pkg.Objdir(), stripext(filepath.Base(cfile))+".o")
ofile := filepath.Join(cgoobjdir(pkg), stripext(filepath.Base(cfile))+".o")
ofiles = append(ofiles, ofile)
cc = append(cc, &Action{
Name: "rungcc1: " + pkg.ImportPath + ": " + cfile,
Expand All @@ -196,7 +196,7 @@ func cgocc(pkg *Package, cflags, cxxflags, cfiles, cxxfiles []string, deps ...*A

for _, cxxfile := range cxxfiles {
cxxfile := cxxfile
ofile := filepath.Join(pkg.Objdir(), stripext(filepath.Base(cxxfile))+".o")
ofile := filepath.Join(cgoobjdir(pkg), stripext(filepath.Base(cxxfile))+".o")
ofiles = append(ofiles, ofile)
cc = append(cc, &Action{
Name: "rung++1: " + pkg.ImportPath + ": " + cxxfile,
Expand Down Expand Up @@ -387,7 +387,7 @@ func quoteFlags(flags []string) []string {
// runcgo1 invokes the cgo tool to process pkg.CgoFiles.
func runcgo1(pkg *Package, cflags, ldflags []string) error {
cgo := cgotool(pkg.Context)
objdir := pkg.Objdir()
objdir := cgoobjdir(pkg)
if err := mkdir(objdir); err != nil {
return err
}
Expand Down Expand Up @@ -422,7 +422,7 @@ func runcgo1(pkg *Package, cflags, ldflags []string) error {
// runcgo2 invokes the cgo tool to create _cgo_import.go
func runcgo2(pkg *Package, dynout, ofile string) error {
cgo := cgotool(pkg.Context)
objdir := pkg.Objdir()
objdir := cgoobjdir(pkg)

args := []string{
"-objdir", objdir,
Expand All @@ -444,3 +444,7 @@ func runcgo2(pkg *Package, dynout, ofile string) error {
}
return run(pkg.Dir, nil, cgo, args...)
}

func cgoobjdir(pkg *Package) string {
return filepath.Join(pkg.Objdir(), pkg.Name, "_cgo")
}
3 changes: 3 additions & 0 deletions cmd/gb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func main() {

log.Debugf("args: %v", args)
if err := command.Run(ctx, args); err != nil {
if !noDestroyContext {
ctx.Destroy()
}
log.Fatalf("command %q failed: %v", name, err)
}
}
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (c *Context) Pkgdir() string {
func (c *Context) Suffix() string {
suffix := c.ctxString()
if suffix != "" {
suffix = "-"+suffix
suffix = "-" + suffix
}
return suffix
}
Expand Down
3 changes: 0 additions & 3 deletions gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os"
"path/filepath"
"runtime"

"github.com/constabulary/gb/log"
)

// gc toolchain
Expand Down Expand Up @@ -122,7 +120,6 @@ func (t *gcToolchain) compiler() string { return t.gc }
func (t *gcToolchain) linker() string { return t.ld }

func (t *gcToolchain) Gc(pkg *Package, searchpaths []string, importpath, srcdir, outfile string, files []string) error {
log.Debugf("gc:gc %v %v %v %v", importpath, srcdir, outfile, files)
args := append(pkg.gcflags, "-p", importpath, "-pack")
args = append(args, "-o", outfile)
for _, d := range searchpaths {
Expand Down
25 changes: 25 additions & 0 deletions package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,28 @@ func TestPackageBinfile(t *testing.T) {
}
}
}

func TestPackageObjdir(t *testing.T) {
var tests = []struct {
pkg string // package name
want string // objdir result
}{
{pkg: "b", want: ""},
{pkg: "nested/a", want: "nested"},
{pkg: "nested/b", want: "nested"},
}

for _, tt := range tests {
ctx := testContext(t)
defer ctx.Destroy()
pkg, err := ctx.ResolvePackage(tt.pkg)
if err != nil {
t.Fatal(err)
}
got := pkg.Objdir()
want := filepath.Join(ctx.Workdir(), tt.want)
if want != got {
t.Errorf("(%s).Objdir(): want %s, got %s", tt.pkg, want, got)
}
}
}
3 changes: 3 additions & 0 deletions testdata/src/nested/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package a

const A = `a`
3 changes: 3 additions & 0 deletions testdata/src/nested/b/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package b

const A = `b`

0 comments on commit 4ffc2cb

Please sign in to comment.