Commit d5d4e82f authored by Hyang-Ah Hana Kim's avatar Hyang-Ah Hana Kim Committed by Hyang-Ah Hana Kim

misc/android: choose the right subdirectory for bin under GOPATH.

This change includes the cleanup of temporary files created during
the binary execution as well.

Change-Id: Ic01a0a537d1daafcaa3acda1ec344aff5dcddfc2
Reviewed-on: https://go-review.googlesource.com/2903Reviewed-by: 's avatarDavid Crawshaw <crawshaw@golang.org>
parent 36f456c7
...@@ -2,9 +2,9 @@ Android ...@@ -2,9 +2,9 @@ Android
======= =======
For details on developing Go for Android, see the documentation in the For details on developing Go for Android, see the documentation in the
go.mobile subrepository: mobile subrepository:
https://code.google.com/p/go/source/browse/README?repo=mobile https://github.com/golang/mobile
To run the standard library tests, see androidtest.bash. Run it as To run the standard library tests, see androidtest.bash. Run it as
......
...@@ -9,6 +9,7 @@ package main ...@@ -9,6 +9,7 @@ package main
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"go/build"
"io" "io"
"log" "log"
"os" "os"
...@@ -32,33 +33,36 @@ func run(args ...string) string { ...@@ -32,33 +33,36 @@ func run(args ...string) string {
return buf.String() return buf.String()
} }
const (
// Directory structure on the target device androidtest.bash assumes.
deviceGoroot = "/data/local/tmp/goroot"
deviceGopath = "/data/local/tmp/gopath"
)
func main() { func main() {
log.SetFlags(0) log.SetFlags(0)
log.SetPrefix("go_android_exec: ") log.SetPrefix("go_android_exec: ")
// Determine thepackage by examining the current working // Prepare a temporary directory that will be cleaned up at the end.
deviceGotmp := fmt.Sprintf("/data/local/tmp/%s-%d",
filepath.Base(os.Args[1]), os.Getpid())
run("shell", "mkdir", "-p", deviceGotmp)
// Determine the package by examining the current working
// directory, which will look something like // directory, which will look something like
// "$GOROOT/src/mime/multipart". We extract everything // "$GOROOT/src/mime/multipart" or "$GOPATH/src/golang.org/x/mobile".
// after the $GOROOT to run on the same relative directory // We extract everything after the $GOROOT or $GOPATH to run on the
// on the target device. // same relative directory on the target device.
// subdir, inGoRoot := subdir()
// TODO(crawshaw): Pick useful subdir when we are not deviceCwd := filepath.Join(deviceGoroot, subdir)
// inside a GOROOT, e.g. we are in a GOPATH. if !inGoRoot {
cwd, err := os.Getwd() deviceCwd = filepath.Join(deviceGopath, subdir)
if err != nil {
log.Fatal(err)
} }
subdir, err := filepath.Rel(runtime.GOROOT(), cwd)
if err != nil {
log.Fatal(err)
}
subdir = filepath.ToSlash(subdir)
// Binary names can conflict. // Binary names can conflict.
// E.g. template.test from the {html,text}/template packages. // E.g. template.test from the {html,text}/template packages.
binName := filepath.Base(os.Args[1]) binName := filepath.Base(os.Args[1])
deviceGoroot := "/data/local/tmp/goroot" deviceBin := fmt.Sprintf("%s/%s-%d", deviceGotmp, binName, os.Getpid())
deviceBin := fmt.Sprintf("%s/%s-%d", deviceGoroot, binName, os.Getpid())
// The push of the binary happens in parallel with other tests. // The push of the binary happens in parallel with other tests.
// Unfortunately, a simultaneous call to adb shell hold open // Unfortunately, a simultaneous call to adb shell hold open
...@@ -71,19 +75,22 @@ func main() { ...@@ -71,19 +75,22 @@ func main() {
// The adb shell command will return an exit code of 0 regardless // The adb shell command will return an exit code of 0 regardless
// of the command run. E.g. // of the command run. E.g.
// $ adb shell false // $ adb shell false
// $ echo $? // $ echo $?
// 0 // 0
// https://code.google.com/p/android/issues/detail?id=3254 // https://code.google.com/p/android/issues/detail?id=3254
// So we append the exitcode to the output and parse it from there. // So we append the exitcode to the output and parse it from there.
const exitstr = "exitcode=" const exitstr = "exitcode="
cmd := `export TMPDIR="/data/local/tmp"` + cmd := `export TMPDIR="` + deviceGotmp + `"` +
`; export GOROOT="` + deviceGoroot + `"` + `; export GOROOT="` + deviceGoroot + `"` +
`; cd "$GOROOT/` + subdir + `"` + `; export GOPATH="` + deviceGopath + `"` +
`; cd "` + deviceCwd + `"` +
"; '" + deviceBin + "' " + strings.Join(os.Args[2:], " ") + "; '" + deviceBin + "' " + strings.Join(os.Args[2:], " ") +
"; echo -n " + exitstr + "$?" "; echo -n " + exitstr + "$?"
output := run("shell", cmd) output := run("shell", cmd)
run("shell", "rm '"+deviceBin+"'") // cleanup
run("shell", "rm", "-rf", deviceGotmp) // Clean up.
output = output[strings.LastIndex(output, "\n")+1:] output = output[strings.LastIndex(output, "\n")+1:]
if !strings.HasPrefix(output, exitstr) { if !strings.HasPrefix(output, exitstr) {
log.Fatalf("no exit code: %q", output) log.Fatalf("no exit code: %q", output)
...@@ -94,3 +101,32 @@ func main() { ...@@ -94,3 +101,32 @@ func main() {
} }
os.Exit(code) os.Exit(code)
} }
// subdir determines the package based on the current working directory,
// and returns the path to the package source relative to $GOROOT (or $GOPATH).
func subdir() (pkgpath string, underGoRoot bool) {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if root := runtime.GOROOT(); strings.HasPrefix(cwd, root) {
subdir, err := filepath.Rel(root, cwd)
if err != nil {
log.Fatal(err)
}
return subdir, true
}
for _, p := range filepath.SplitList(build.Default.GOPATH) {
if !strings.HasPrefix(cwd, p) {
continue
}
subdir, err := filepath.Rel(p, cwd)
if err == nil {
return subdir, false
}
}
log.Fatalf("the current path %q is not in either GOROOT(%q) or GOPATH(%q)",
cwd, runtime.GOROOT(), build.Default.GOPATH)
return "", false
}
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