2015-07-16 20:36:27 -04:00
|
|
|
// 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"
|
2017-02-21 18:48:50 +01:00
|
|
|
"path"
|
2015-07-16 20:36:27 -04:00
|
|
|
"path/filepath"
|
2015-08-28 13:39:30 -04:00
|
|
|
"strings"
|
2015-07-16 20:36:27 -04:00
|
|
|
"testing"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(crawshaw): TestBindIOS
|
|
|
|
|
2017-02-21 18:48:50 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 20:36:27 -04:00
|
|
|
func TestBindAndroid(t *testing.T) {
|
2015-08-28 13:39:30 -04:00
|
|
|
androidHome := os.Getenv("ANDROID_HOME")
|
|
|
|
if androidHome == "" {
|
2015-07-16 20:36:27 -04:00
|
|
|
t.Skip("ANDROID_HOME not found, skipping bind")
|
|
|
|
}
|
2015-08-28 13:39:30 -04:00
|
|
|
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)
|
2015-07-16 20:36:27 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
xout = os.Stderr
|
|
|
|
buildN = false
|
|
|
|
buildX = false
|
2015-08-28 13:39:30 -04:00
|
|
|
buildO = ""
|
|
|
|
buildTarget = ""
|
2015-07-16 20:36:27 -04:00
|
|
|
}()
|
|
|
|
buildN = true
|
|
|
|
buildX = true
|
|
|
|
buildO = "asset.aar"
|
2017-01-29 12:51:07 +01:00
|
|
|
buildTarget = "android/arm"
|
cmd/gomobile: replace stripped NDK with external NDK
Gomobile has up until now used stripped NDKs hosted by Google. This
arrangement adds maintenance overhead and blocks the use of custom
NDKs or custom API levels. Also, as noted in issue 16211, the stripped
NDK is no longer tiny because Gomobile supports more platforms.
This CL removed the code for generating and packaging stripped NDKs and
adds support for using external NDKs to the gomobile tool.
gomobile init will now use the NDK installed by the Android SDK manager,
if present, or a user specified NDK if the -ndk flag is given. If no
NDK was found or specified, Android initialization is skipped. gomobile
will instruct the user to run init with a valid NDK if bind or build is
invoked without Android initialization.
gomobile init will also attempt to build OpenAL for Android if the -openal
flag specifies a source directory. It needs cmake and, on Windows, nmake
installed. If gomobile build is run on an app that requires
golang.org/x/mobile/exp/audio/al and OpenAL wasn't built by init, the user
is instructed to do so.
Tested on Linux, macOS, Windows.
Fixes golang/go#16211
Fixes golang/go#18522
Change-Id: Ia38f6e43e671a207dad562678c65225b426e7e3e
Reviewed-on: https://go-review.googlesource.com/35173
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-13 00:59:09 +01:00
|
|
|
ndkRoot = "/NDK"
|
2015-07-16 20:36:27 -04:00
|
|
|
|
2015-08-28 13:39:30 -04:00
|
|
|
tests := []struct {
|
|
|
|
javaPkg string
|
|
|
|
wantGobind string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
wantGobind: "gobind -lang=java",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
javaPkg: "com.example.foo",
|
|
|
|
wantGobind: "gobind -lang=java -javapkg=com.example.foo",
|
|
|
|
},
|
2015-07-16 20:36:27 -04:00
|
|
|
}
|
2015-08-28 13:39:30 -04:00
|
|
|
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
|
2018-03-07 22:51:37 +01:00
|
|
|
JavaPkg string
|
2015-08-28 13:39:30 -04:00
|
|
|
}{
|
|
|
|
outputData: defaultOutputData(),
|
|
|
|
AndroidPlatform: platform,
|
|
|
|
GobindJavaCmd: tc.wantGobind,
|
2018-03-07 22:51:37 +01:00
|
|
|
JavaPkg: tc.javaPkg,
|
2015-08-28 13:39:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2015-07-16 20:36:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var bindAndroidTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
|
|
|
|
WORK=$WORK
|
2018-03-07 22:51:37 +01:00
|
|
|
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
|
2015-07-16 20:36:27 -04:00
|
|
|
jar c -C $WORK/javac-output .
|
|
|
|
`))
|