2
0
mirror of synced 2025-02-23 23:08:14 +00:00

cmd/gomobile: make sure gobind is installed and updated

When running gomobile bind, make sure gobind exists. If not, instruct
the user to run gomobile init which will go install gobind.

Change-Id: I2d064ba58874fd5581c17417124561f3d1fb6b83
Reviewed-on: https://go-review.googlesource.com/101055
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
Elias Naur 2018-03-16 08:34:03 +01:00
parent 6b7c05d452
commit b07e525bd7
7 changed files with 31 additions and 6 deletions

View File

@ -11,6 +11,7 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
)
@ -96,6 +97,16 @@ func runBind(cmd *command) error {
return errors.New("no Android NDK path is set. Please run gomobile init with the ndk-bundle installed through the Android SDK manager or with the -ndk flag set.")
}
var gobind string
if !buildN {
gobind, err = exec.LookPath("gobind")
if err != nil {
return errors.New("gobind was not found. Please run gomobile init before trying again.")
}
} else {
gobind = "gobind"
}
var pkgs []*build.Package
switch len(args) {
case 0:
@ -117,10 +128,10 @@ func runBind(cmd *command) error {
switch targetOS {
case "android":
return goAndroidBind(pkgs, targetArchs)
return goAndroidBind(gobind, pkgs, targetArchs)
case "darwin":
// TODO: use targetArchs?
return goIOSBind(pkgs)
return goIOSBind(gobind, pkgs)
default:
return fmt.Errorf(`invalid -target=%q`, buildTarget)
}

View File

@ -17,14 +17,14 @@ import (
"strings"
)
func goAndroidBind(pkgs []*build.Package, androidArchs []string) error {
func goAndroidBind(gobind string, pkgs []*build.Package, androidArchs []string) error {
if sdkDir := os.Getenv("ANDROID_HOME"); sdkDir == "" {
return fmt.Errorf("this command requires ANDROID_HOME environment variable (path to the Android SDK)")
}
// Run gobind to generate the bindings
cmd := exec.Command(
"gobind",
gobind,
"-lang=go,java",
"-outdir="+tmpdir,
)

View File

@ -16,10 +16,10 @@ import (
"text/template"
)
func goIOSBind(pkgs []*build.Package) error {
func goIOSBind(gobind string, pkgs []*build.Package) error {
// Run gobind to generate the bindings
cmd := exec.Command(
"gobind",
gobind,
"-lang=go,objc",
"-outdir="+tmpdir,
)

View File

@ -26,7 +26,11 @@ func TestIOSBuild(t *testing.T) {
buildTarget = "ios"
gopath = filepath.SplitList(os.Getenv("GOPATH"))[0]
cmdBuild.flag.Parse([]string{"golang.org/x/mobile/example/basic"})
oldTags := ctx.BuildTags
ctx.BuildTags = []string{"tag1"}
defer func() {
ctx.BuildTags = oldTags
}()
err := runBuild(cmdBuild)
if err != nil {
t.Log(buf.String())

View File

@ -81,7 +81,11 @@ func TestAndroidBuild(t *testing.T) {
os.Setenv("HOMEDRIVE", "C:")
}
cmdBuild.flag.Parse([]string{"golang.org/x/mobile/example/basic"})
oldTags := ctx.BuildTags
ctx.BuildTags = []string{"tag1"}
defer func() {
ctx.BuildTags = oldTags
}()
err := runBuild(cmdBuild)
if err != nil {
t.Log(buf.String())

View File

@ -85,6 +85,11 @@ func runInit(cmd *command) error {
removeAll(tmpdir)
}()
// Make sure gobind is up to date.
if err := goInstall([]string{"golang.org/x/mobile/cmd/gobind"}, nil); err != nil {
return err
}
if buildN {
initNDK = "$NDK_PATH"
initOpenAL = "$OPENAL_PATH"

View File

@ -114,6 +114,7 @@ var initTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/
rm -r -f "$GOMOBILE"
mkdir -p $GOMOBILE
WORK={{.GOPATH}}/pkg/gomobile/work
go install -x golang.org/x/mobile/cmd/gobind
PWD=$NDK_PATH $NDK_PATH/prebuilt/{{.GOOS}}-{{.NDKARCH}}/bin/python2.7 build/tools/make_standalone_toolchain.py --arch=arm --api=15 --install-dir=$GOMOBILE/ndk-toolchains/arm
PWD=$NDK_PATH $NDK_PATH/prebuilt/{{.GOOS}}-{{.NDKARCH}}/bin/python2.7 build/tools/make_standalone_toolchain.py --arch=arm64 --api=21 --install-dir=$GOMOBILE/ndk-toolchains/arm64
PWD=$NDK_PATH $NDK_PATH/prebuilt/{{.GOOS}}-{{.NDKARCH}}/bin/python2.7 build/tools/make_standalone_toolchain.py --arch=x86 --api=15 --install-dir=$GOMOBILE/ndk-toolchains/x86