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>
This commit is contained in:
Elias Naur 2018-03-16 11:20:43 +01:00
parent b07e525bd7
commit 5d852261b1
6 changed files with 13 additions and 6 deletions

View File

@ -5,6 +5,7 @@
package main
import (
"bytes"
"flag"
"fmt"
"go/build"
@ -90,6 +91,13 @@ func run() {
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.

View File

@ -53,7 +53,7 @@ func goAndroidBind(gobind string, pkgs []*build.Package, androidArchs []string)
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)

View File

@ -9,7 +9,6 @@ import (
"go/build"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
@ -37,7 +36,7 @@ func goIOSBind(gobind string, pkgs []*build.Package) error {
}
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)

View File

@ -68,7 +68,7 @@ func TestBindAndroid(t *testing.T) {
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:")
}

View File

@ -24,7 +24,7 @@ func TestIOSBuild(t *testing.T) {
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"}

View File

@ -76,7 +76,7 @@ func TestAndroidBuild(t *testing.T) {
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:")
}