The gobind and gomobile bind tools have historically overlapped: gobind outputs generated bindings, and gomobile bind will generate bindings before building them. However, the gobind bindings were never used for building and thus allowed to not be complete. To simplify version control, debugging, instrumentation and build system flexibility, this CL upgrades the gobind tool to be the canonical binding generator and change gomobile bind to use gobind instead of its own generator code. This greatly simplifies gomobile bind, but also paves the way to skip gomobile bind entirely. For example: $ gobind -outdir=$GOPATH golang.org/x/mobile/example/bind/hello $ GOOS=android GOARCH=arm64 CC=<ndk-toolchain>/bin/clang go build -buildmode=c-shared -o libgobind.so gobind $ ls libgobind.* libgobind.h libgobind.so The same applies to iOS, although the go build command line is more involved. By skipping gomobile it is possible to freely customize the Android or iOS SDK level or any other flags not supported by gomobile bind. By checking in the generated source code, the cost of supporting gomobile in a custom build system is also decreased. Change-Id: I59c14a77d625ac1377c23b3213672e0d83a48c85 Reviewed-on: https://go-review.googlesource.com/99316 Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
120 lines
3.8 KiB
Go
120 lines
3.8 KiB
Go
// Copyright 2015 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"text/template"
|
|
)
|
|
|
|
// TODO(crawshaw): TestBindIOS
|
|
|
|
func TestImportPackagesPathCleaning(t *testing.T) {
|
|
slashPath := "golang.org/x/mobile/example/bind/hello/"
|
|
pkgs, err := importPackages([]string{slashPath})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p := pkgs[0]
|
|
if c := path.Clean(slashPath); p.ImportPath != c {
|
|
t.Errorf("expected %s; got %s", c, p.ImportPath)
|
|
}
|
|
}
|
|
|
|
func TestBindAndroid(t *testing.T) {
|
|
androidHome := os.Getenv("ANDROID_HOME")
|
|
if androidHome == "" {
|
|
t.Skip("ANDROID_HOME not found, skipping bind")
|
|
}
|
|
platform, err := androidAPIPath()
|
|
if err != nil {
|
|
t.Skip("No android API platform found in $ANDROID_HOME, skipping bind")
|
|
}
|
|
platform = strings.Replace(platform, androidHome, "$ANDROID_HOME", -1)
|
|
|
|
defer func() {
|
|
xout = os.Stderr
|
|
buildN = false
|
|
buildX = false
|
|
buildO = ""
|
|
buildTarget = ""
|
|
}()
|
|
buildN = true
|
|
buildX = true
|
|
buildO = "asset.aar"
|
|
buildTarget = "android/arm"
|
|
ndkRoot = "/NDK"
|
|
|
|
tests := []struct {
|
|
javaPkg string
|
|
wantGobind string
|
|
}{
|
|
{
|
|
wantGobind: "gobind -lang=java",
|
|
},
|
|
{
|
|
javaPkg: "com.example.foo",
|
|
wantGobind: "gobind -lang=java -javapkg=com.example.foo",
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
bindJavaPkg = tc.javaPkg
|
|
|
|
buf := new(bytes.Buffer)
|
|
xout = buf
|
|
gopath = filepath.SplitList(os.Getenv("GOPATH"))[0]
|
|
if goos == "windows" {
|
|
os.Setenv("HOMEDRIVE", "C:")
|
|
}
|
|
cmdBind.flag.Parse([]string{"golang.org/x/mobile/asset"})
|
|
err := runBind(cmdBind)
|
|
if err != nil {
|
|
t.Log(buf.String())
|
|
t.Fatal(err)
|
|
}
|
|
got := filepath.ToSlash(buf.String())
|
|
|
|
data := struct {
|
|
outputData
|
|
AndroidPlatform string
|
|
GobindJavaCmd string
|
|
JavaPkg string
|
|
}{
|
|
outputData: defaultOutputData(),
|
|
AndroidPlatform: platform,
|
|
GobindJavaCmd: tc.wantGobind,
|
|
JavaPkg: tc.javaPkg,
|
|
}
|
|
|
|
wantBuf := new(bytes.Buffer)
|
|
if err := bindAndroidTmpl.Execute(wantBuf, data); err != nil {
|
|
t.Errorf("%+v: computing diff failed: %v", tc, err)
|
|
continue
|
|
}
|
|
|
|
diff, err := diff(got, wantBuf.String())
|
|
if err != nil {
|
|
t.Errorf("%+v: computing diff failed: %v", tc, err)
|
|
continue
|
|
}
|
|
if diff != "" {
|
|
t.Errorf("%+v: unexpected output:\n%s", tc, diff)
|
|
}
|
|
}
|
|
}
|
|
|
|
var bindAndroidTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
|
|
WORK=$WORK
|
|
gobind -lang=go,java -outdir=$WORK{{if .JavaPkg}} -javapkg={{.JavaPkg}}{{end}} golang.org/x/mobile/asset
|
|
GOOS=android GOARCH=arm CC=/NDK/toolchains/llvm/prebuilt/{{.GOOS}}-{{.NDKARCH}}/bin/clang{{.EXE}} CXX=/NDK/toolchains/llvm/prebuilt/{{.GOOS}}-{{.NDKARCH}}/bin/clang++{{.EXE}} CGO_CFLAGS=-target armv7a-none-linux-androideabi -gcc-toolchain /NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/{{.GOOS}}-{{.NDKARCH}} --sysroot /NDK/sysroot -isystem /NDK/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=15 -I$GOMOBILE/include CGO_CPPFLAGS=-target armv7a-none-linux-androideabi -gcc-toolchain /NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/{{.GOOS}}-{{.NDKARCH}} --sysroot /NDK/sysroot -isystem /NDK/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=15 -I$GOMOBILE/include CGO_LDFLAGS=-target armv7a-none-linux-androideabi -gcc-toolchain /NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/{{.GOOS}}-{{.NDKARCH}} --sysroot /NDK/platforms/android-15/arch-arm -L$GOMOBILE/lib/arm CGO_ENABLED=1 GOARM=7 GOPATH=$WORK:$GOPATH go build -pkgdir=$GOMOBILE/pkg_android_arm -x -buildmode=c-shared -o=$WORK/android/src/main/jniLibs/armeabi-v7a/libgojni.so gobind
|
|
PWD=$WORK/java javac -d $WORK/javac-output -source 1.7 -target 1.7 -bootclasspath {{.AndroidPlatform}}/android.jar *.java
|
|
jar c -C $WORK/javac-output .
|
|
`))
|