Commit fb63ed2b authored by Elias Naur's avatar Elias Naur

misc/android: silence adb output unless an error occurs

Fixes #31917

Change-Id: I794e457b2245d355e2df5077078c67aa09e00ff9
Reviewed-on: https://go-review.googlesource.com/c/go/+/175920
Run-TryBot: Elias Naur <mail@eliasnaur.com>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent f766b680
...@@ -27,11 +27,8 @@ import ( ...@@ -27,11 +27,8 @@ import (
) )
func run(args ...string) (string, error) { func run(args ...string) (string, error) {
if flags := os.Getenv("GOANDROID_ADB_FLAGS"); flags != "" { cmd := adbCmd(args...)
args = append(strings.Split(flags, " "), args...)
}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := exec.Command("adb", args...)
cmd.Stdout = io.MultiWriter(os.Stdout, buf) cmd.Stdout = io.MultiWriter(os.Stdout, buf)
// If the adb subprocess somehow hangs, go test will kill this wrapper // If the adb subprocess somehow hangs, go test will kill this wrapper
// and wait for our os.Stderr (and os.Stdout) to close as a result. // and wait for our os.Stderr (and os.Stdout) to close as a result.
...@@ -43,7 +40,6 @@ func run(args ...string) (string, error) { ...@@ -43,7 +40,6 @@ func run(args ...string) (string, error) {
// forcing cmd.Run to use another pipe and goroutine to pass // forcing cmd.Run to use another pipe and goroutine to pass
// along stderr from adb. // along stderr from adb.
cmd.Stderr = struct{ io.Writer }{os.Stderr} cmd.Stderr = struct{ io.Writer }{os.Stderr}
log.Printf("adb %s", strings.Join(args, " "))
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
return "", fmt.Errorf("adb %s: %v", strings.Join(args, " "), err) return "", fmt.Errorf("adb %s: %v", strings.Join(args, " "), err)
...@@ -51,6 +47,21 @@ func run(args ...string) (string, error) { ...@@ -51,6 +47,21 @@ func run(args ...string) (string, error) {
return buf.String(), nil return buf.String(), nil
} }
func adb(args ...string) error {
if out, err := adbCmd(args...).CombinedOutput(); err != nil {
fmt.Fprintf(os.Stderr, "adb %s\n%s", strings.Join(args, " "), out)
return err
}
return nil
}
func adbCmd(args ...string) *exec.Cmd {
if flags := os.Getenv("GOANDROID_ADB_FLAGS"); flags != "" {
args = append(strings.Split(flags, " "), args...)
}
return exec.Command("adb", args...)
}
const ( const (
deviceRoot = "/data/local/tmp/go_android_exec" deviceRoot = "/data/local/tmp/go_android_exec"
deviceGoroot = deviceRoot + "/goroot" deviceGoroot = deviceRoot + "/goroot"
...@@ -83,7 +94,7 @@ func runMain() (int, error) { ...@@ -83,7 +94,7 @@ func runMain() (int, error) {
// In case we're booting a device or emulator alongside all.bash, wait for // In case we're booting a device or emulator alongside all.bash, wait for
// it to be ready. adb wait-for-device is not enough, we have to // it to be ready. adb wait-for-device is not enough, we have to
// wait for sys.boot_completed. // wait for sys.boot_completed.
if _, err := run("wait-for-device", "exec-out", "while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;"); err != nil { if err := adb("wait-for-device", "exec-out", "while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;"); err != nil {
return 0, err return 0, err
} }
...@@ -98,7 +109,7 @@ func runMain() (int, error) { ...@@ -98,7 +109,7 @@ func runMain() (int, error) {
binName := filepath.Base(os.Args[1]) binName := filepath.Base(os.Args[1])
deviceGotmp := fmt.Sprintf(deviceRoot+"/%s-%d", binName, os.Getpid()) deviceGotmp := fmt.Sprintf(deviceRoot+"/%s-%d", binName, os.Getpid())
deviceGopath := deviceGotmp + "/gopath" deviceGopath := deviceGotmp + "/gopath"
defer run("exec-out", "rm", "-rf", deviceGotmp) // Clean up. defer adb("exec-out", "rm", "-rf", deviceGotmp) // Clean up.
// Determine the package by examining the current working // Determine the package by examining the current working
// directory, which will look something like // directory, which will look something like
...@@ -113,7 +124,7 @@ func runMain() (int, error) { ...@@ -113,7 +124,7 @@ func runMain() (int, error) {
if inGoRoot { if inGoRoot {
deviceCwd = filepath.Join(deviceGoroot, subdir) deviceCwd = filepath.Join(deviceGoroot, subdir)
} else { } else {
if _, err := run("exec-out", "mkdir", "-p", deviceCwd); err != nil { if err := adb("exec-out", "mkdir", "-p", deviceCwd); err != nil {
return 0, err return 0, err
} }
if err := adbCopyTree(deviceCwd, subdir); err != nil { if err := adbCopyTree(deviceCwd, subdir); err != nil {
...@@ -127,14 +138,14 @@ func runMain() (int, error) { ...@@ -127,14 +138,14 @@ func runMain() (int, error) {
} }
if len(goFiles) > 0 { if len(goFiles) > 0 {
args := append(append([]string{"push"}, goFiles...), deviceCwd) args := append(append([]string{"push"}, goFiles...), deviceCwd)
if _, err := run(args...); err != nil { if err := adb(args...); err != nil {
return 0, err return 0, err
} }
} }
} }
deviceBin := fmt.Sprintf("%s/%s", deviceGotmp, binName) deviceBin := fmt.Sprintf("%s/%s", deviceGotmp, binName)
if _, err := run("push", os.Args[1], deviceBin); err != nil { if err := adb("push", os.Args[1], deviceBin); err != nil {
return 0, err return 0, err
} }
...@@ -146,7 +157,7 @@ func runMain() (int, error) { ...@@ -146,7 +157,7 @@ func runMain() (int, error) {
for range quit { for range quit {
// We don't have the PID of the running process; use the // We don't have the PID of the running process; use the
// binary name instead. // binary name instead.
run("exec-out", "killall -QUIT "+binName) adb("exec-out", "killall -QUIT "+binName)
} }
}() }()
// In light of // In light of
...@@ -232,10 +243,10 @@ func adbCopyTree(deviceCwd, subdir string) error { ...@@ -232,10 +243,10 @@ func adbCopyTree(deviceCwd, subdir string) error {
continue continue
} }
devicePath := filepath.Join(deviceCwd, dir) devicePath := filepath.Join(deviceCwd, dir)
if _, err := run("exec-out", "mkdir", "-p", devicePath); err != nil { if err := adb("exec-out", "mkdir", "-p", devicePath); err != nil {
return err return err
} }
if _, err := run("push", path, devicePath); err != nil { if err := adb("push", path, devicePath); err != nil {
return err return err
} }
} }
...@@ -273,11 +284,11 @@ func adbCopyGoroot() error { ...@@ -273,11 +284,11 @@ func adbCopyGoroot() error {
return nil return nil
} }
// Delete GOROOT, GOPATH and any leftover test data. // Delete GOROOT, GOPATH and any leftover test data.
if _, err := run("exec-out", "rm", "-rf", deviceRoot); err != nil { if err := adb("exec-out", "rm", "-rf", deviceRoot); err != nil {
return err return err
} }
deviceBin := filepath.Join(deviceGoroot, "bin") deviceBin := filepath.Join(deviceGoroot, "bin")
if _, err := run("exec-out", "mkdir", "-p", deviceBin); err != nil { if err := adb("exec-out", "mkdir", "-p", deviceBin); err != nil {
return err return err
} }
goroot := runtime.GOROOT() goroot := runtime.GOROOT()
...@@ -294,20 +305,20 @@ func adbCopyGoroot() error { ...@@ -294,20 +305,20 @@ func adbCopyGoroot() error {
return fmt.Errorf("failed to build go tool for device: %s\n%v", out, err) return fmt.Errorf("failed to build go tool for device: %s\n%v", out, err)
} }
deviceGo := filepath.Join(deviceBin, "go") deviceGo := filepath.Join(deviceBin, "go")
if _, err := run("push", tmpGo.Name(), deviceGo); err != nil { if err := adb("push", tmpGo.Name(), deviceGo); err != nil {
return err return err
} }
for _, dir := range []string{"src", "test", "lib", "api"} { for _, dir := range []string{"src", "test", "lib", "api"} {
if _, err := run("push", filepath.Join(goroot, dir), filepath.Join(deviceGoroot)); err != nil { if err := adb("push", filepath.Join(goroot, dir), filepath.Join(deviceGoroot)); err != nil {
return err return err
} }
} }
// Copy only the relevant from pkg. // Copy only the relevant from pkg.
if _, err := run("exec-out", "mkdir", "-p", filepath.Join(deviceGoroot, "pkg", "tool")); err != nil { if err := adb("exec-out", "mkdir", "-p", filepath.Join(deviceGoroot, "pkg", "tool")); err != nil {
return err return err
} }
if _, err := run("push", filepath.Join(goroot, "pkg", "include"), filepath.Join(deviceGoroot, "pkg")); err != nil { if err := adb("push", filepath.Join(goroot, "pkg", "include"), filepath.Join(deviceGoroot, "pkg")); err != nil {
return err return err
} }
runtimea, err := exec.Command(goCmd, "list", "-f", "{{.Target}}", "runtime").Output() runtimea, err := exec.Command(goCmd, "list", "-f", "{{.Target}}", "runtime").Output()
...@@ -315,11 +326,11 @@ func adbCopyGoroot() error { ...@@ -315,11 +326,11 @@ func adbCopyGoroot() error {
if pkgdir == "" { if pkgdir == "" {
return errors.New("could not find android pkg dir") return errors.New("could not find android pkg dir")
} }
if _, err := run("push", pkgdir, filepath.Join(deviceGoroot, "pkg")); err != nil { if err := adb("push", pkgdir, filepath.Join(deviceGoroot, "pkg")); err != nil {
return err return err
} }
tooldir := filepath.Join(goroot, "pkg", "tool", filepath.Base(pkgdir)) tooldir := filepath.Join(goroot, "pkg", "tool", filepath.Base(pkgdir))
if _, err := run("push", tooldir, filepath.Join(deviceGoroot, "pkg", "tool")); err != nil { if err := adb("push", tooldir, filepath.Join(deviceGoroot, "pkg", "tool")); err != nil {
return err return err
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment