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

Refactor Package.Objdir into Workdir(pkg) #381

Merged
merged 1 commit into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,20 @@ func link(pkg *Package) error {
return err
}

// Workdir returns the working directory for a package.
func Workdir(pkg *Package) string {
switch pkg.Scope {
case "test":
ip := strings.TrimSuffix(filepath.FromSlash(pkg.ImportPath), "_test")
return filepath.Join(pkg.Workdir(), ip, "_test", filepath.Dir(filepath.FromSlash(pkg.ImportPath)))
default:
return filepath.Join(pkg.Workdir(), filepath.Dir(filepath.FromSlash(pkg.ImportPath)))
}
}

// objfile returns the name of the object file for this package
func objfile(pkg *Package) string {
return filepath.Join(pkg.Objdir(), objname(pkg))
return filepath.Join(Workdir(pkg), objname(pkg))
}

func objname(pkg *Package) string {
Expand Down
32 changes: 29 additions & 3 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,14 @@ func TestCgoobjdir(t *testing.T) {
pkg string // package name
want string // objdir result
}{
{pkg: "b", want: "main/_cgo"},
{pkg: "b", want: "b/_cgo"},
{pkg: "nested/a", want: "nested/a/_cgo"},
{pkg: "nested/b", want: "nested/b/_cgo"},
}

ctx := testContext(t)
defer ctx.Destroy()
for _, tt := range tests {
ctx := testContext(t)
defer ctx.Destroy()
pkg, err := ctx.ResolvePackage(tt.pkg)
if err != nil {
t.Fatal(err)
Expand All @@ -247,6 +247,32 @@ func TestCgoobjdir(t *testing.T) {
}
}

func TestWorkdir(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"},
}

ctx := testContext(t)
defer ctx.Destroy()
for _, tt := range tests {
pkg, err := ctx.ResolvePackage(tt.pkg)
if err != nil {
t.Error(err)
continue
}
got := Workdir(pkg)
want := filepath.Join(ctx.Workdir(), tt.want)
if want != got {
t.Errorf("Workdir(Package{Name: %v, ImportPath: %v, Scope: %v}): want %s, got %s", pkg.Name, pkg.ImportPath, pkg.Scope, want, got)
}
}
}

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

defun := filepath.Join(pkg.Objdir(), "_cgo_defun.o")
defun := filepath.Join(cgoobjdir(pkg), "_cgo_defun.o")
rundefun := Action{
Name: "cc: " + pkg.ImportPath + ": _cgo_defun_c",
Deps: runcgo1,
Expand Down Expand Up @@ -429,7 +429,7 @@ func runcgo2(pkg *Package, dynout, ofile string) error {
}

func cgoobjdir(pkg *Package) string {
return filepath.Join(pkg.Objdir(), pkg.Name, "_cgo")
return filepath.Join(Workdir(pkg), pkgname(pkg), "_cgo")
}

// gccCmd returns a gcc command line prefix.
Expand Down
2 changes: 1 addition & 1 deletion cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func buildTestMain(pkg *gb.Package) (*gb.Package, error) {
if pkg.Scope != "test" {
return nil, fmt.Errorf("package %q is not test scoped", pkg.Name)
}
dir := pkg.Objdir()
dir := gb.Workdir(pkg)
if err := mkdir(dir); err != nil {
return nil, fmt.Errorf("buildTestmain: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (t *gcToolchain) Cc(pkg *Package, ofile, cfile string) error {
args := []string{
"-F", "-V", "-w",
"-trimpath", pkg.Workdir(),
"-I", pkg.Objdir(),
"-I", Workdir(pkg),
"-I", filepath.Join(pkg.Context.Context.GOROOT, "pkg", pkg.gohostos+"_"+pkg.gohostarch), // for runtime.h
"-o", ofile,
"-D", "GOOS_" + pkg.gotargetos,
Expand Down
11 changes: 0 additions & 11 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,6 @@ func (p *Package) Complete() bool {
return extFiles == 0
}

// Objdir returns the destination for object files compiled for this Package.
func (pkg *Package) Objdir() string {
switch pkg.Scope {
case "test":
ip := strings.TrimSuffix(filepath.FromSlash(pkg.ImportPath), "_test")
return filepath.Join(pkg.Workdir(), ip, "_test", filepath.Dir(filepath.FromSlash(pkg.ImportPath)))
default:
return filepath.Join(pkg.Workdir(), filepath.Dir(filepath.FromSlash(pkg.ImportPath)))
}
}

// Binfile returns the destination of the compiled target of this command.
func (pkg *Package) Binfile() string {
// TODO(dfc) should have a check for package main, or should be merged in to objfile.
Expand Down
25 changes: 0 additions & 25 deletions package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,6 @@ 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)
}
}
}

func TestPackageIsMain(t *testing.T) {
var tests = []struct {
pkg *Package
Expand Down