Skip to content

Commit

Permalink
cmd/gomobile: make sure gobind is installed and updated
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Elias Naur committed Mar 26, 2018
1 parent 6b7c05d commit b07e525
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 6 deletions.
15 changes: 13 additions & 2 deletions cmd/gomobile/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
)
Expand Down Expand Up @@ -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:
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/gomobile/bind_androidapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/gomobile/bind_iosapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
4 changes: 4 additions & 0 deletions cmd/gomobile/build_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions cmd/gomobile/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
5 changes: 5 additions & 0 deletions cmd/gomobile/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions cmd/gomobile/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b07e525

Please sign in to comment.