cmd/gomobile: update tests and add bind tests

Change-Id: I9e2fdf179b1e07a8b0d9b612913814768f8284dc
Reviewed-on: https://go-review.googlesource.com/12331
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
David Crawshaw 2015-07-16 20:36:27 -04:00
Родитель 7d5958d761
Коммит ea6f260cd0
6 изменённых файлов: 105 добавлений и 29 удалений

Просмотреть файл

@ -65,7 +65,11 @@ func goAndroidBind(pkg *build.Package) error {
}
dst := filepath.Join(androidDir, "src/main/java/go/LoadJNI.java")
if err := ioutil.WriteFile(dst, []byte(loadSrc), 0664); err != nil {
genLoadJNI := func(w io.Writer) error {
_, err := io.WriteString(w, loadSrc)
return err
}
if err := writeFile(dst, genLoadJNI); err != nil {
return err
}

67
cmd/gomobile/bind_test.go Normal file
Просмотреть файл

@ -0,0 +1,67 @@
// 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/filepath"
"testing"
"text/template"
)
// TODO(crawshaw): TestBindIOS
func TestBindAndroid(t *testing.T) {
if os.Getenv("ANDROID_HOME") == "" {
t.Skip("ANDROID_HOME not found, skipping bind")
}
buf := new(bytes.Buffer)
defer func() {
xout = os.Stderr
buildN = false
buildX = false
}()
xout = buf
buildN = true
buildX = true
buildO = "asset.aar"
buildTarget = "android"
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)
}
diff, err := diffOutput(buf.String(), bindAndroidTmpl)
if err != nil {
t.Fatalf("computing diff failed: %v", err)
}
if diff != "" {
t.Errorf("unexpected output:\n%s", diff)
}
}
var bindAndroidTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
WORK=$WORK
gobind -lang=go golang.org/x/mobile/asset > $WORK/go_asset/go_assetmain.go
mkdir -p $WORK/go_asset
mkdir -p $WORK/androidlib
GOOS=android GOARCH=arm GOARM=7 CC=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-gcc{{.EXE}} CXX=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-g++{{.EXE}} CGO_ENABLED=1 go build -pkgdir=$GOMOBILE/pkg_android_arm -tags="" -x -buildmode=c-shared -o=$WORK/android/src/main/jniLibs/armeabi-v7a/libgojni.so $WORK/androidlib/main.go
gobind -lang=java golang.org/x/mobile/asset > $WORK/android/src/main/java/go/asset/Asset.java
mkdir -p $WORK/android/src/main/java/go/asset
mkdir -p $WORK/android/src/main/java/go
rm $WORK/android/src/main/java/go/Seq.java
ln -s $GOPATH/src/golang.org/x/mobile/bind/java/Seq.java $WORK/android/src/main/java/go/Seq.java
javac -d $WORK/javac-output -source 1.7 -target 1.7 -bootclasspath $ANDROID_HOME/platforms/android-22/android.jar *.java
jar c -C $WORK/javac-output .
rm -r -f "$WORK/javac-output"
`))

Просмотреть файл

@ -134,6 +134,9 @@ func printcmd(format string, args ...interface{}) {
if tmpdir != "" {
cmd = strings.Replace(cmd, tmpdir, "$WORK", -1)
}
if androidHome := os.Getenv("ANDROID_HOME"); androidHome != "" {
cmd = strings.Replace(cmd, androidHome, "$ANDROID_HOME", -1)
}
if gomobilepath != "" {
cmd = strings.Replace(cmd, gomobilepath, "$GOMOBILE", -1)
}

Просмотреть файл

@ -22,14 +22,13 @@ func TestBuild(t *testing.T) {
xout = buf
buildN = true
buildX = true
buildO = "basic.apk"
buildTarget = "android"
gopath = filepath.SplitList(os.Getenv("GOPATH"))[0]
if goos == "windows" {
os.Setenv("HOMEDRIVE", "C:")
}
cmdBuild.flag.Parse([]string{"golang.org/x/mobile/example/basic"})
if buildTarget != "android" {
t.Fatalf("-target=%q, want android", buildTarget)
}
err := runBuild(cmdBuild)
if err != nil {
t.Log(buf.String())
@ -43,11 +42,9 @@ func TestBuild(t *testing.T) {
if diff != "" {
t.Errorf("unexpected output:\n%s", diff)
}
}
var buildTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
WORK=$WORK
CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 CC=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-gcc{{.EXE}} CXX=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-g++{{.EXE}} GOGCCFLAGS="-fPIC -marm -pthread -fmessage-length=0" go build -tags="" -x -buildmode=c-shared -o $WORK/libbasic.so golang.org/x/mobile/example/basic
rm -r -f "$WORK"
GOOS=android GOARCH=arm GOARM=7 CC=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-gcc{{.EXE}} CXX=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-g++{{.EXE}} CGO_ENABLED=1 go build -pkgdir=$GOMOBILE/pkg_android_arm -tags="" -x -buildmode=c-shared -o $WORK/libbasic.so golang.org/x/mobile/example/basic
`))

Просмотреть файл

@ -30,15 +30,21 @@ func buildEnvInit() (cleanup func(), err error) {
gopath := goEnv("GOPATH")
for _, p := range filepath.SplitList(gopath) {
gomobilepath = filepath.Join(p, "pkg", "gomobile")
if _, err := os.Stat(gomobilepath); err == nil {
if _, err := os.Stat(gomobilepath); buildN || err == nil {
break
}
}
if err := envInit(); err != nil {
return nil, err
}
if buildX {
fmt.Fprintln(xout, "GOMOBILE="+gomobilepath)
}
// Check the toolchain is in a good state.
// Pick a temporary directory for assembling an apk/app.
version, err := goVersion()
if err != nil {
return nil, err
@ -46,23 +52,21 @@ func buildEnvInit() (cleanup func(), err error) {
if gomobilepath == "" {
return nil, errors.New("toolchain not installed, run `gomobile init`")
}
verpath := filepath.Join(gomobilepath, "version")
installedVersion, err := ioutil.ReadFile(verpath)
if err != nil {
return nil, errors.New("toolchain partially installed, run `gomobile init`")
}
if !bytes.Equal(installedVersion, version) {
return nil, errors.New("toolchain out of date, run `gomobile init`")
}
if err := envInit(); err != nil {
return nil, err
}
// We need a temporary directory when assembling an apk/app.
cleanupFn := func() { removeAll(tmpdir) }
if buildN {
tmpdir = "$WORK"
cleanupFn = func() {}
} else {
verpath := filepath.Join(gomobilepath, "version")
installedVersion, err := ioutil.ReadFile(verpath)
if err != nil {
return nil, errors.New("toolchain partially installed, run `gomobile init`")
}
if !bytes.Equal(installedVersion, version) {
return nil, errors.New("toolchain out of date, run `gomobile init`")
}
tmpdir, err = ioutil.TempDir("", "gomobile-work-")
if err != nil {
return nil, err
@ -72,7 +76,7 @@ func buildEnvInit() (cleanup func(), err error) {
fmt.Fprintln(xout, "WORK="+tmpdir)
}
return func() { removeAll(tmpdir) }, nil
return cleanupFn, nil
}
func envInit() (err error) {
@ -156,6 +160,9 @@ func envInit() (err error) {
}
func envClang(sdkName string) (clang, cflags string, err error) {
if buildN {
return "clang-" + sdkName, "-isysroot=" + sdkName, nil
}
cmd := exec.Command("xcrun", "--sdk", sdkName, "--find", "clang")
out, err := cmd.Output()
if err != nil {

Просмотреть файл

@ -87,7 +87,7 @@ type outputData struct {
var initTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
mkdir -p $GOMOBILE/android-{{.NDK}}
WORK=/GOPATH1/pkg/gomobile/work
WORK={{.GOPATH}}/pkg/gomobile/work
mkdir -p $GOMOBILE/dl
curl -o$GOMOBILE/dl/gomobile-{{.NDK}}-{{.GOOS}}-{{.NDKARCH}}.tar.gz https://dl.google.com/go/mobile/gomobile-{{.NDK}}-{{.GOOS}}-{{.NDKARCH}}.tar.gz
tar xfz $GOMOBILE/dl/gomobile-{{.NDK}}-{{.GOOS}}-{{.NDKARCH}}.tar.gz
@ -108,12 +108,10 @@ tar xfz $GOMOBILE/dl/gomobile-openal-soft-1.16.0.1.tar.gz
mv $WORK/openal/include/AL $GOMOBILE/android-{{.NDK}}/arm/sysroot/usr/include/AL
mkdir -p $GOMOBILE/android-{{.NDK}}/openal
mv $WORK/openal/lib $GOMOBILE/android-{{.NDK}}/openal/lib
rm -r -f "$GOROOT/pkg/android_arm"
PATH=$PATH GOOS=android GOARCH=arm CGO_ENABLED=1 CC=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-gcc{{.EXE}} CXX=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-g++{{.EXE}} GOARM=7 go install std
{{if eq .GOOS "darwin"}}rm -r -f "$GOROOT/pkg/darwin_arm"
PATH=$PATH GOOS=darwin GOARCH=arm CGO_ENABLED=1 CC=$GOROOT/misc/ios/clangwrap.sh CXX=$GOROOT/misc/ios/clangwrap.sh GOARM=7 go install std
rm -r -f "$GOROOT/pkg/darwin_arm64"
PATH=$PATH GOOS=darwin GOARCH=arm64 CGO_ENABLED=1 CC=$GOROOT/misc/ios/clangwrap.sh CXX=$GOROOT/misc/ios/clangwrap.sh go install std
{{if eq .GOOS "darwin"}}PATH=$PATH GOOS=android GOARCH=arm GOARM=7 CC=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-gcc{{.EXE}} CXX=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-g++ CGO_ENABLED=1 go install -pkgdir=$GOMOBILE/pkg_android_arm -x std
PATH=$PATH GOOS=darwin GOARCH=arm GOARM=7 CC=clang-iphoneos CXX=clang-iphoneos CGO_CFLAGS=-isysroot=iphoneos -arch armv7 CGO_LDFLAGS=-isysroot=iphoneos -arch armv7 CGO_ENABLED=1 go install -pkgdir=$GOMOBILE/pkg_darwin_arm -x std
PATH=$PATH GOOS=darwin GOARCH=arm64 CC=clang-iphoneos CXX=clang-iphoneos CGO_CFLAGS=-isysroot=iphoneos -arch arm64 CGO_LDFLAGS=-isysroot=iphoneos -arch arm64 CGO_ENABLED=1 go install -pkgdir=$GOMOBILE/pkg_darwin_arm64 -x std
PATH=$PATH GOOS=darwin GOARCH=amd64 CC=clang-iphonesimulator CXX=clang-iphonesimulator CGO_CFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=6.1 -arch x86_64 CGO_LDFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=6.1 -arch x86_64 CGO_ENABLED=1 go install -pkgdir=$GOMOBILE/pkg_darwin_amd64 -tags=ios -x std
{{end}}go version > $GOMOBILE/version
rm -r -f "$WORK"
`))