2
0
mirror of synced 2025-02-23 06:48:15 +00:00

cmd/gomobile: check version with working cmd/dist

Fixes golang/go#10760 maybe, I have been unable to reproduce it.

Change-Id: Iaeec631f5a6be29b312969df12f6bd6b95bc2aa1
Reviewed-on: https://go-review.googlesource.com/9880
Reviewed-by: Burcu Dogan <jbd@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
David Crawshaw 2015-05-08 14:52:18 -04:00 committed by Hyang-Ah Hana Kim
parent f3d33d5ec9
commit ddd3a9e786

View File

@ -107,10 +107,6 @@ func runInit(cmd *command) error {
defer removeAll(tmpdir)
goroot := goEnv("GOROOT")
if err := checkVersionMatch(goroot, version); err != nil {
return err
}
tmpGoroot := filepath.Join(tmpdir, "go")
if err := copyGoroot(tmpGoroot, goroot); err != nil {
return err
@ -149,6 +145,7 @@ func runInit(cmd *command) error {
make.Env = []string{
`PATH=` + envpath,
`GOOS=android`,
`GOROOT=` + tmpGoroot, // set to override any bad os.Environ
`GOARCH=arm`,
`GOARM=7`,
`CGO_ENABLED=1`,
@ -181,6 +178,9 @@ func runInit(cmd *command) error {
if err := make.Run(); err != nil {
return err
}
if err := checkVersionMatch(tmpGoroot, version); err != nil {
return err
}
}
// Move the Go cross compiler toolchain into GOPATH.
@ -377,33 +377,34 @@ func goVersion() ([]byte, error) {
// This is typically not a problem when using the a release version, but
// it is easy for development environments to drift, causing unexpected
// errors.
func checkVersionMatch(goroot string, version []byte) error {
//
// checkVersionMatch is run after the tmpGoroot is built, so the dist
// command is available to call.
func checkVersionMatch(tmpGoroot string, version []byte) error {
if buildN {
return nil
}
version = bytes.TrimPrefix(version, []byte("go version "))
version = bytes.Trim(version, "\n")
// Build a temporary copy of cmd/dist to get the version string
// associated with the goroot.
distv := filepath.Join(tmpdir, "distv.exe")
cmd := exec.Command("go", "build", "-o", distv)
cmd.Dir = filepath.Join(goroot, "src/cmd/dist")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("cannot build cmd/dist: %v (%s)", err, out)
dist := filepath.Join(tmpGoroot, "pkg/tool/"+goEnv("GOOS")+"_"+goEnv("GOARCH")+"/dist")
if goos == "windows" {
dist += ".exe"
}
cmd = exec.Command(distv, "version")
cmd.Dir = goroot
out, err = cmd.Output()
cmd := exec.Command(dist, "version")
cmd.Dir = tmpGoroot
cmd.Env = []string{
"GOROOT=" + tmpGoroot,
`PATH=` + os.Getenv("PATH"),
}
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("cannot get cmd/dist version: %v (%s)", err, out)
}
out = bytes.Trim(out, "\n")
if !bytes.HasPrefix(version, out) {
return fmt.Errorf("Go command out of sync with GOROOT. The command `go version` reports:\n\t%s\nbut the GOROOT %q is version:\n\t%s\nRebuild Go.", version, goroot, out)
return fmt.Errorf("Go command out of sync with GOROOT. The command `go version` reports:\n\t%s\nbut the GOROOT %q is version:\n\t%s\nRebuild Go.", version, goEnv("GOROOT"), out)
}
return nil
}