cmd/gobind,cmd/gomobile: support the default GOPATH

Instead of using os.Getenv("GOPATH"), use go env GOPATH to determine
the effective GOPATH.

Fixes golang/go#21658

Change-Id: I03f897969e30fc3256d171aa7b32c101a9342a1a
Reviewed-on: https://go-review.googlesource.com/101117
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/cmd/gobind/main.go b/cmd/gobind/main.go
index 03f8059..73552c5 100644
--- a/cmd/gobind/main.go
+++ b/cmd/gobind/main.go
@@ -5,6 +5,7 @@
 package main
 
 import (
+	"bytes"
 	"flag"
 	"fmt"
 	"go/build"
@@ -90,6 +91,13 @@
 			log.Fatal(err)
 		}
 	}
+	// Determine GOPATH from go env GOPATH in case the default $HOME/go GOPATH
+	// is in effect.
+	if out, err := exec.Command("go", "env", "GOPATH").Output(); err != nil {
+		log.Fatal(err)
+	} else {
+		ctx.GOPATH = string(bytes.TrimSpace(out))
+	}
 	if len(classes) > 0 || len(otypes) > 0 {
 		// After generation, reverse bindings needs to be in the GOPATH
 		// for user packages to build.
diff --git a/cmd/gomobile/bind_androidapp.go b/cmd/gomobile/bind_androidapp.go
index 45a8c37..06b7f0c 100644
--- a/cmd/gomobile/bind_androidapp.go
+++ b/cmd/gomobile/bind_androidapp.go
@@ -53,7 +53,7 @@
 	for _, arch := range androidArchs {
 		env := androidEnv[arch]
 		// Add the generated packages to GOPATH
-		gopath := fmt.Sprintf("GOPATH=%s%c%s", tmpdir, filepath.ListSeparator, os.Getenv("GOPATH"))
+		gopath := fmt.Sprintf("GOPATH=%s%c%s", tmpdir, filepath.ListSeparator, goEnv("GOPATH"))
 		env = append(env, gopath)
 		toolchain := ndk.Toolchain(arch)
 
diff --git a/cmd/gomobile/bind_iosapp.go b/cmd/gomobile/bind_iosapp.go
index 1c9d698..c7f327a 100644
--- a/cmd/gomobile/bind_iosapp.go
+++ b/cmd/gomobile/bind_iosapp.go
@@ -9,7 +9,6 @@
 	"go/build"
 	"io"
 	"io/ioutil"
-	"os"
 	"os/exec"
 	"path/filepath"
 	"strings"
@@ -37,7 +36,7 @@
 	}
 
 	srcDir := filepath.Join(tmpdir, "src", "gobind")
-	gopath := fmt.Sprintf("GOPATH=%s%c%s", tmpdir, filepath.ListSeparator, os.Getenv("GOPATH"))
+	gopath := fmt.Sprintf("GOPATH=%s%c%s", tmpdir, filepath.ListSeparator, goEnv("GOPATH"))
 
 	name := pkgs[0].Name
 	title := strings.Title(name)
diff --git a/cmd/gomobile/bind_test.go b/cmd/gomobile/bind_test.go
index 729fda1..857c802 100644
--- a/cmd/gomobile/bind_test.go
+++ b/cmd/gomobile/bind_test.go
@@ -68,7 +68,7 @@
 
 		buf := new(bytes.Buffer)
 		xout = buf
-		gopath = filepath.SplitList(os.Getenv("GOPATH"))[0]
+		gopath = filepath.SplitList(goEnv("GOPATH"))[0]
 		if goos == "windows" {
 			os.Setenv("HOMEDRIVE", "C:")
 		}
diff --git a/cmd/gomobile/build_darwin_test.go b/cmd/gomobile/build_darwin_test.go
index 50dc864..d375a7a 100644
--- a/cmd/gomobile/build_darwin_test.go
+++ b/cmd/gomobile/build_darwin_test.go
@@ -24,7 +24,7 @@
 	buildX = true
 	buildO = "basic.app"
 	buildTarget = "ios"
-	gopath = filepath.SplitList(os.Getenv("GOPATH"))[0]
+	gopath = filepath.SplitList(goEnv("GOPATH"))[0]
 	cmdBuild.flag.Parse([]string{"golang.org/x/mobile/example/basic"})
 	oldTags := ctx.BuildTags
 	ctx.BuildTags = []string{"tag1"}
diff --git a/cmd/gomobile/build_test.go b/cmd/gomobile/build_test.go
index 97af848..5b4358d 100644
--- a/cmd/gomobile/build_test.go
+++ b/cmd/gomobile/build_test.go
@@ -76,7 +76,7 @@
 	buildX = true
 	buildO = "basic.apk"
 	buildTarget = "android/arm"
-	gopath = filepath.ToSlash(filepath.SplitList(os.Getenv("GOPATH"))[0])
+	gopath = filepath.ToSlash(filepath.SplitList(goEnv("GOPATH"))[0])
 	if goos == "windows" {
 		os.Setenv("HOMEDRIVE", "C:")
 	}