Commit 810809eb authored by Elias Naur's avatar Elias Naur

misc/android: copy less from GOROOT to the device

The android emulator builders is running out of space after CL 165797
copied most of GOROOT to the device.
The pkg directory is by far the largest, so only include what seems
necessary to build the x/ repositories: pkg/android_$GOARCH and
pkg/tool/android_$GOARCH.

While here, rename the device root directory to match the exec
wrapper name and make sure the deferred cleanups actually run before
os.Exit.

Hopefully fixes the emulator builders.

Updates #23824

Change-Id: I4d1e3ab2c89fd1e5818503d323ddb87f073094da
Reviewed-on: https://go-review.googlesource.com/c/go/+/166397
Run-TryBot: Elias Naur <mail@eliasnaur.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent cf469165
...@@ -10,6 +10,7 @@ package main ...@@ -10,6 +10,7 @@ package main
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"go/build" "go/build"
"io" "io"
...@@ -25,7 +26,7 @@ import ( ...@@ -25,7 +26,7 @@ import (
"syscall" "syscall"
) )
func run(args ...string) string { func run(args ...string) (string, error) {
if flags := os.Getenv("GOANDROID_ADB_FLAGS"); flags != "" { if flags := os.Getenv("GOANDROID_ADB_FLAGS"); flags != "" {
args = append(strings.Split(flags, " "), args...) args = append(strings.Split(flags, " "), args...)
} }
...@@ -45,40 +46,51 @@ func run(args ...string) string { ...@@ -45,40 +46,51 @@ func run(args ...string) string {
log.Printf("adb %s", strings.Join(args, " ")) log.Printf("adb %s", strings.Join(args, " "))
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
log.Fatalf("adb %s: %v", strings.Join(args, " "), err) return "", fmt.Errorf("adb %s: %v", strings.Join(args, " "), err)
} }
return buf.String() return buf.String(), nil
} }
const ( const (
deviceRoot = "/data/local/tmp/go_exec_android" deviceRoot = "/data/local/tmp/go_android_exec"
deviceGoroot = deviceRoot + "/goroot" deviceGoroot = deviceRoot + "/goroot"
) )
func main() { func main() {
log.SetFlags(0) log.SetFlags(0)
log.SetPrefix("go_android_exec: ") log.SetPrefix("go_android_exec: ")
exitCode, err := runMain()
if err != nil {
log.Fatal(err)
}
os.Exit(exitCode)
}
func runMain() (int, error) {
// Concurrent use of adb is flaky, so serialize adb commands. // Concurrent use of adb is flaky, so serialize adb commands.
// See https://github.com/golang/go/issues/23795 or // See https://github.com/golang/go/issues/23795 or
// https://issuetracker.google.com/issues/73230216. // https://issuetracker.google.com/issues/73230216.
lockPath := filepath.Join(os.TempDir(), "go_android_exec-adb-lock") lockPath := filepath.Join(os.TempDir(), "go_android_exec-adb-lock")
lock, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0666) lock, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0666)
if err != nil { if err != nil {
log.Fatal(err) return 0, err
} }
defer lock.Close() defer lock.Close()
if err := syscall.Flock(int(lock.Fd()), syscall.LOCK_EX); err != nil { if err := syscall.Flock(int(lock.Fd()), syscall.LOCK_EX); err != nil {
log.Fatal(err) return 0, err
} }
// 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.
run("wait-for-device", "exec-out", "while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;") if _, err := run("wait-for-device", "exec-out", "while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;"); err != nil {
return 0, err
}
// Done once per make.bash. // Done once per make.bash.
adbCopyGoroot() if err := adbCopyGoroot(); err != nil {
return 0, err
}
// Prepare a temporary directory that will be cleaned up at the end. // Prepare a temporary directory that will be cleaned up at the end.
// Binary names can conflict. // Binary names can conflict.
...@@ -93,27 +105,38 @@ func main() { ...@@ -93,27 +105,38 @@ func main() {
// "$GOROOT/src/mime/multipart" or "$GOPATH/src/golang.org/x/mobile". // "$GOROOT/src/mime/multipart" or "$GOPATH/src/golang.org/x/mobile".
// We extract everything after the $GOROOT or $GOPATH to run on the // We extract everything after the $GOROOT or $GOPATH to run on the
// same relative directory on the target device. // same relative directory on the target device.
subdir, inGoRoot := subdir() subdir, inGoRoot, err := subdir()
if err != nil {
return 0, err
}
deviceCwd := filepath.Join(deviceGopath, subdir) deviceCwd := filepath.Join(deviceGopath, subdir)
if inGoRoot { if inGoRoot {
deviceCwd = filepath.Join(deviceGoroot, subdir) deviceCwd = filepath.Join(deviceGoroot, subdir)
} else { } else {
run("exec-out", "mkdir", "-p", deviceCwd) if _, err := run("exec-out", "mkdir", "-p", deviceCwd); err != nil {
adbCopyTestdata(deviceCwd, subdir) return 0, err
}
if err := adbCopyTestdata(deviceCwd, subdir); err != nil {
return 0, err
}
// Copy .go files from the package. // Copy .go files from the package.
goFiles, err := filepath.Glob("*.go") goFiles, err := filepath.Glob("*.go")
if err != nil { if err != nil {
log.Fatal(err) return 0, err
} }
if len(goFiles) > 0 { if len(goFiles) > 0 {
args := append(append([]string{"push"}, goFiles...), deviceCwd) args := append(append([]string{"push"}, goFiles...), deviceCwd)
run(args...) if _, err := run(args...); err != nil {
return 0, err
}
} }
} }
deviceBin := fmt.Sprintf("%s/%s", deviceGotmp, binName) deviceBin := fmt.Sprintf("%s/%s", deviceGotmp, binName)
run("push", os.Args[1], deviceBin) if _, err := run("push", os.Args[1], deviceBin); err != nil {
return 0, err
}
// Forward SIGQUIT from the go command to show backtraces from // Forward SIGQUIT from the go command to show backtraces from
// the binary instead of from this wrapper. // the binary instead of from this wrapper.
...@@ -140,60 +163,62 @@ func main() { ...@@ -140,60 +163,62 @@ func main() {
`; cd "` + deviceCwd + `"` + `; cd "` + deviceCwd + `"` +
"; '" + deviceBin + "' " + strings.Join(os.Args[2:], " ") + "; '" + deviceBin + "' " + strings.Join(os.Args[2:], " ") +
"; echo -n " + exitstr + "$?" "; echo -n " + exitstr + "$?"
output := run("exec-out", cmd) output, err := run("exec-out", cmd)
signal.Reset(syscall.SIGQUIT) signal.Reset(syscall.SIGQUIT)
close(quit) close(quit)
if err != nil {
return 0, err
}
exitIdx := strings.LastIndex(output, exitstr) exitIdx := strings.LastIndex(output, exitstr)
if exitIdx == -1 { if exitIdx == -1 {
log.Fatalf("no exit code: %q", output) return 0, fmt.Errorf("no exit code: %q", output)
} }
code, err := strconv.Atoi(output[exitIdx+len(exitstr):]) code, err := strconv.Atoi(output[exitIdx+len(exitstr):])
if err != nil { if err != nil {
log.Fatalf("bad exit code: %v", err) return 0, fmt.Errorf("bad exit code: %v", err)
} }
os.Exit(code) return code, nil
} }
// subdir determines the package based on the current working directory, // subdir determines the package based on the current working directory,
// and returns the path to the package source relative to $GOROOT (or $GOPATH). // and returns the path to the package source relative to $GOROOT (or $GOPATH).
func subdir() (pkgpath string, underGoRoot bool) { func subdir() (pkgpath string, underGoRoot bool, err error) {
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err != nil { if err != nil {
log.Fatal(err) return "", false, err
} }
cwd, err = filepath.EvalSymlinks(cwd) cwd, err = filepath.EvalSymlinks(cwd)
if err != nil { if err != nil {
log.Fatal(err) return "", false, err
} }
goroot, err := filepath.EvalSymlinks(runtime.GOROOT()) goroot, err := filepath.EvalSymlinks(runtime.GOROOT())
if err != nil { if err != nil {
log.Fatal(err) return "", false, err
} }
if strings.HasPrefix(cwd, goroot) { if strings.HasPrefix(cwd, goroot) {
subdir, err := filepath.Rel(goroot, cwd) subdir, err := filepath.Rel(goroot, cwd)
if err != nil { if err != nil {
log.Fatal(err) return "", false, err
} }
return subdir, true return subdir, true, nil
} }
for _, p := range filepath.SplitList(build.Default.GOPATH) { for _, p := range filepath.SplitList(build.Default.GOPATH) {
pabs, err := filepath.EvalSymlinks(p) pabs, err := filepath.EvalSymlinks(p)
if err != nil { if err != nil {
log.Fatal(err) return "", false, err
} }
if !strings.HasPrefix(cwd, pabs) { if !strings.HasPrefix(cwd, pabs) {
continue continue
} }
subdir, err := filepath.Rel(pabs, cwd) subdir, err := filepath.Rel(pabs, cwd)
if err == nil { if err == nil {
return subdir, false return subdir, false, nil
} }
} }
log.Fatalf("the current path %q is not in either GOROOT(%q) or GOPATH(%q)", return "", false, fmt.Errorf("the current path %q is not in either GOROOT(%q) or GOPATH(%q)",
cwd, runtime.GOROOT(), build.Default.GOPATH) cwd, runtime.GOROOT(), build.Default.GOPATH)
return "", false
} }
// adbCopyTestdata copies testdata directories from subdir to deviceCwd // adbCopyTestdata copies testdata directories from subdir to deviceCwd
...@@ -201,14 +226,18 @@ func subdir() (pkgpath string, underGoRoot bool) { ...@@ -201,14 +226,18 @@ func subdir() (pkgpath string, underGoRoot bool) {
// It is common for tests to reach out into testdata from parent // It is common for tests to reach out into testdata from parent
// packages, so copy testdata directories all the way up to the root // packages, so copy testdata directories all the way up to the root
// of subdir. // of subdir.
func adbCopyTestdata(deviceCwd, subdir string) { func adbCopyTestdata(deviceCwd, subdir string) error {
dir := "" dir := ""
for { for {
testdata := filepath.Join(dir, "testdata") testdata := filepath.Join(dir, "testdata")
if _, err := os.Stat(testdata); err == nil { if _, err := os.Stat(testdata); err == nil {
devicePath := filepath.Join(deviceCwd, dir) devicePath := filepath.Join(deviceCwd, dir)
run("exec-out", "mkdir", "-p", devicePath) if _, err := run("exec-out", "mkdir", "-p", devicePath); err != nil {
run("push", testdata, devicePath) return err
}
if _, err := run("push", testdata, devicePath); err != nil {
return err
}
} }
if subdir == "." { if subdir == "." {
break break
...@@ -216,6 +245,7 @@ func adbCopyTestdata(deviceCwd, subdir string) { ...@@ -216,6 +245,7 @@ func adbCopyTestdata(deviceCwd, subdir string) {
subdir = filepath.Dir(subdir) subdir = filepath.Dir(subdir)
dir = filepath.Join(dir, "..") dir = filepath.Join(dir, "..")
} }
return nil
} }
// adbCopyGoroot clears deviceRoot for previous versions of GOROOT, GOPATH // adbCopyGoroot clears deviceRoot for previous versions of GOROOT, GOPATH
...@@ -223,49 +253,78 @@ func adbCopyTestdata(deviceCwd, subdir string) { ...@@ -223,49 +253,78 @@ func adbCopyTestdata(deviceCwd, subdir string) {
// including the go tool built for android. // including the go tool built for android.
// A lock file ensures this only happens once, even with concurrent exec // A lock file ensures this only happens once, even with concurrent exec
// wrappers. // wrappers.
func adbCopyGoroot() { func adbCopyGoroot() error {
// Also known by cmd/dist. The bootstrap command deletes the file. // Also known by cmd/dist. The bootstrap command deletes the file.
statPath := filepath.Join(os.TempDir(), "go_android_exec-adb-sync-status") statPath := filepath.Join(os.TempDir(), "go_android_exec-adb-sync-status")
stat, err := os.OpenFile(statPath, os.O_CREATE|os.O_RDWR, 0666) stat, err := os.OpenFile(statPath, os.O_CREATE|os.O_RDWR, 0666)
if err != nil { if err != nil {
log.Fatal(err) return err
} }
defer stat.Close() defer stat.Close()
// Serialize check and copying. // Serialize check and copying.
if err := syscall.Flock(int(stat.Fd()), syscall.LOCK_EX); err != nil { if err := syscall.Flock(int(stat.Fd()), syscall.LOCK_EX); err != nil {
log.Fatal(err) return err
} }
s, err := ioutil.ReadAll(stat) s, err := ioutil.ReadAll(stat)
if err != nil { if err != nil {
log.Fatal(err) return err
} }
if string(s) == "done" { if string(s) == "done" {
return return nil
} }
// Delete GOROOT, GOPATH and any leftover test data. // Delete GOROOT, GOPATH and any leftover test data.
run("exec-out", "rm", "-rf", deviceRoot) if _, err := run("exec-out", "rm", "-rf", deviceRoot); err != nil {
return err
}
deviceBin := filepath.Join(deviceGoroot, "bin") deviceBin := filepath.Join(deviceGoroot, "bin")
run("exec-out", "mkdir", "-p", deviceBin) if _, err := run("exec-out", "mkdir", "-p", deviceBin); err != nil {
return err
}
goroot := runtime.GOROOT() goroot := runtime.GOROOT()
// Build go for android. // Build go for android.
goCmd := filepath.Join(goroot, "bin", "go") goCmd := filepath.Join(goroot, "bin", "go")
tmpGo, err := ioutil.TempFile("", "go_android_exec-cmd-go-*") tmpGo, err := ioutil.TempFile("", "go_android_exec-cmd-go-*")
if err != nil { if err != nil {
log.Fatal(err) return err
} }
tmpGo.Close() tmpGo.Close()
defer os.Remove(tmpGo.Name()) defer os.Remove(tmpGo.Name())
if out, err := exec.Command(goCmd, "build", "-o", tmpGo.Name(), "cmd/go").CombinedOutput(); err != nil { if out, err := exec.Command(goCmd, "build", "-o", tmpGo.Name(), "cmd/go").CombinedOutput(); err != nil {
log.Fatalf("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")
run("push", tmpGo.Name(), deviceGo) if _, err := run("push", tmpGo.Name(), deviceGo); err != nil {
for _, dir := range []string{"pkg", "src", "test", "lib", "api"} { return err
run("push", filepath.Join(goroot, dir), filepath.Join(deviceGoroot)) }
for _, dir := range []string{"src", "test", "lib", "api"} {
if _, err := run("push", filepath.Join(goroot, dir), filepath.Join(deviceGoroot)); err != nil {
return err
}
}
// Copy only the relevant from pkg.
if _, err := run("exec-out", "mkdir", "-p", filepath.Join(deviceGoroot, "pkg", "tool")); err != nil {
return err
}
if _, err := run("push", filepath.Join(goroot, "pkg", "include"), filepath.Join(deviceGoroot, "pkg")); err != nil {
return err
}
runtimea, err := exec.Command(goCmd, "list", "-f", "{{.Target}}", "runtime").Output()
pkgdir := filepath.Dir(string(runtimea))
if pkgdir == "" {
return errors.New("could not find android pkg dir")
}
if _, err := run("push", pkgdir, filepath.Join(deviceGoroot, "pkg")); err != nil {
return err
}
tooldir := filepath.Join(goroot, "pkg", "tool", filepath.Base(pkgdir))
if _, err := run("push", tooldir, filepath.Join(deviceGoroot, "pkg", "tool")); err != nil {
return err
} }
if _, err := stat.Write([]byte("done")); err != nil { if _, err := stat.Write([]byte("done")); err != nil {
log.Fatal(err) return err
} }
return nil
} }
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