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

Commit

Permalink
Always capture command output and report package name
Browse files Browse the repository at this point in the history
Fixes #440

Prior to #437 the name of the package under compile or test would be
printed *before* the operation started. This was a problem, because the
compilation output could occur later, and not line up with the package
name.

After #437, package name output was surpressed until the compile/test
action completed successfully. This helped improve the output for tests
but as the overall action, the print would never occur, only the output
to stdout from the child process.

This PR solves the problem at the source by delegating the responsibility
for printing the name of the package before any child output. If all
actions complete, then the final action in the set will print the name
of the package.

As it is now unused, `context.run` has been removed.

TODO:

- [ ] decide if the output should go to stderr, not stdout
- [ ] decide if we should include a # prefix as the go tool does
  • Loading branch information
davecheney committed Nov 8, 2015
1 parent d75229e commit 87e2e4b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 21 deletions.
47 changes: 41 additions & 6 deletions cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package gb
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

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

func cgo(pkg *Package) (*Action, []string, []string, error) {
Expand Down Expand Up @@ -210,7 +213,12 @@ func rungcc1(pkg *Package, cgoCFLAGS []string, ofile, cfile string) error {
)
t0 := time.Now()
gcc := gccCmd(pkg, pkg.Dir)
err := run(pkg.Dir, nil, gcc[0], append(gcc[1:], args...)...)
var buf bytes.Buffer
err := runOut(&buf, pkg.Dir, nil, gcc[0], append(gcc[1:], args...)...)
if err != nil {
log.Infof(pkg.ImportPath)
io.Copy(os.Stdout, &buf)
}
pkg.Record(gcc[0], time.Since(t0))
return err
}
Expand All @@ -228,7 +236,12 @@ func rungpp1(pkg *Package, cgoCFLAGS []string, ofile, cfile string) error {
)
t0 := time.Now()
gxx := gxxCmd(pkg, pkg.Dir)
err := run(pkg.Dir, nil, gxx[0], append(gxx[1:], args...)...)
var buf bytes.Buffer
err := runOut(&buf, pkg.Dir, nil, gxx[0], append(gxx[1:], args...)...)
if err != nil {
log.Infof(pkg.ImportPath)
io.Copy(os.Stdout, &buf)
}
pkg.Record(gxx[0], time.Since(t0))
return err
}
Expand All @@ -247,7 +260,12 @@ func gccld(pkg *Package, cgoCFLAGS, cgoLDFLAGS []string, ofile string, ofiles []
} else {
cmd = gccCmd(pkg, pkg.Dir)
}
err := run(pkg.Dir, nil, cmd[0], append(cmd[1:], args...)...)
var buf bytes.Buffer
err := runOut(&buf, pkg.Dir, nil, cmd[0], append(cmd[1:], args...)...)
if err != nil {
log.Infof(pkg.ImportPath)
io.Copy(os.Stdout, &buf)
}
pkg.Record("gccld", time.Since(t0))
return err
}
Expand All @@ -272,7 +290,12 @@ func rungcc3(pkg *Package, dir string, ofile string, ofiles []string) error {
args = append(args, libgcc)
}
t0 := time.Now()
err := run(dir, nil, cmd[0], append(cmd[1:], args...)...)
var buf bytes.Buffer
err := runOut(&buf, dir, nil, cmd[0], append(cmd[1:], args...)...)
if err != nil {
log.Infof(pkg.ImportPath)
io.Copy(os.Stdout, &buf)
}
pkg.Record("gcc3", time.Since(t0))
return err
}
Expand Down Expand Up @@ -386,7 +409,13 @@ func runcgo1(pkg *Package, cflags, ldflags []string) error {
"CGO_CFLAGS=" + strings.Join(quoteFlags(cflags), " "),
"CGO_LDFLAGS=" + strings.Join(quoteFlags(ldflags), " "),
}
return run(pkg.Dir, cgoenv, cgo, args...)
var buf bytes.Buffer
err := runOut(&buf, pkg.Dir, cgoenv, cgo, args...)
if err != nil {
log.Infof(pkg.ImportPath)
io.Copy(os.Stdout, &buf)
}
return err
}

// runcgo2 invokes the cgo tool to create _cgo_import.go
Expand All @@ -412,7 +441,13 @@ func runcgo2(pkg *Package, dynout, ofile string) error {
default:
return fmt.Errorf("unsuppored Go version: %v", runtime.Version)
}
return run(pkg.Dir, nil, cgo, args...)
var buf bytes.Buffer
err := runOut(&buf, pkg.Dir, nil, cgo, args...)
if err != nil {
log.Infof(pkg.ImportPath)
io.Copy(os.Stdout, &buf)
}
return err
}

// cgoworkdir returns the cgo working directory for this package.
Expand Down
10 changes: 0 additions & 10 deletions context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gb

import (
"bytes"
"fmt"
"go/build"
"io"
Expand Down Expand Up @@ -213,15 +212,6 @@ func (c *Context) ctxString() string {
return strings.Join(v, "-")
}

func run(dir string, env []string, command string, args ...string) error {
var buf bytes.Buffer
err := runOut(&buf, dir, env, command, args...)
if err != nil {
return fmt.Errorf("# %s %s: %v\n%s", command, strings.Join(args, " "), err, buf.String())
}
return nil
}

func runOut(output io.Writer, dir string, env []string, command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Dir = dir
Expand Down
44 changes: 39 additions & 5 deletions gc.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package gb

import (
"bytes"
"fmt"
"go/build"
"io"
"os"
"path/filepath"
"runtime"

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

// gc toolchain
Expand Down Expand Up @@ -73,7 +77,13 @@ func (t *gcToolchain) Asm(pkg *Package, srcdir, ofile, sfile string) error {
if err := mkdir(filepath.Dir(ofile)); err != nil {
return fmt.Errorf("gc:asm: %v", err)
}
return run(srcdir, nil, t.as, args...)
var buf bytes.Buffer
err := runOut(&buf, srcdir, nil, t.as, args...)
if err != nil {
log.Infof(pkg.ImportPath)
io.Copy(os.Stdout, &buf)
}
return err
}

func (t *gcToolchain) Ld(pkg *Package, searchpaths []string, outfile, afile string) error {
Expand All @@ -88,7 +98,13 @@ func (t *gcToolchain) Ld(pkg *Package, searchpaths []string, outfile, afile stri
if err := mkdir(filepath.Dir(outfile)); err != nil {
return fmt.Errorf("gc:ld: %v", err)
}
return run(".", nil, t.ld, args...)
var buf bytes.Buffer
err := runOut(&buf, ".", nil, t.ld, args...)
if err != nil {
log.Infof(pkg.ImportPath)
io.Copy(os.Stdout, &buf)
}
return err
}

func (t *gcToolchain) Cc(pkg *Package, ofile, cfile string) error {
Expand All @@ -105,14 +121,26 @@ func (t *gcToolchain) Cc(pkg *Package, ofile, cfile string) error {
"-D", "GOARCH_" + pkg.gotargetarch,
cfile,
}
return run(pkg.Dir, nil, t.cc, args...)
var buf bytes.Buffer
err := runOut(&buf, pkg.Dir, nil, t.cc, args...)
if err != nil {
log.Infof(pkg.ImportPath)
io.Copy(os.Stdout, &buf)
}
return err
}

func (t *gcToolchain) Pack(pkg *Package, afiles ...string) error {
args := []string{"r"}
args = append(args, afiles...)
dir := filepath.Dir(afiles[0])
return run(dir, nil, t.pack, args...)
var buf bytes.Buffer
err := runOut(&buf, dir, nil, t.pack, args...)
if err != nil {
log.Infof(pkg.ImportPath)
io.Copy(os.Stdout, &buf)
}
return err
}

func (t *gcToolchain) compiler() string { return t.gc }
Expand Down Expand Up @@ -141,5 +169,11 @@ func (t *gcToolchain) Gc(pkg *Package, searchpaths []string, importpath, srcdir,
if err := mkdir(filepath.Join(filepath.Dir(outfile), pkg.Name)); err != nil {
return fmt.Errorf("gc:gc: %v", err)
}
return runOut(os.Stdout, srcdir, nil, t.gc, args...)
var buf bytes.Buffer
err := runOut(&buf, srcdir, nil, t.gc, args...)
if err != nil {
log.Infof(pkg.ImportPath)
io.Copy(os.Stdout, &buf)
}
return err
}

0 comments on commit 87e2e4b

Please sign in to comment.