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"
|
2019-03-01 21:40:45 +01:00
|
|
|
"runtime"
|
2015-08-28 13:39:30 -04:00
|
|
|
"strings"
|
2015-07-16 20:36:27 -04:00
|
|
|
"testing"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
2017-02-21 18:48:50 +01:00
|
|
|
func TestImportPackagesPathCleaning(t *testing.T) {
|
2019-03-01 21:40:45 +01:00
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
t.Skip("not available on Android")
|
|
|
|
}
|
2017-02-21 18:48:50 +01:00
|
|
|
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 = ""
|
2018-04-03 16:24:44 +02:00
|
|
|
bindJavaPkg = ""
|
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"
|
2015-07-16 20:36:27 -04:00
|
|
|
|
2015-08-28 13:39:30 -04:00
|
|
|
tests := []struct {
|
2018-04-03 16:24:44 +02:00
|
|
|
javaPkg string
|
2015-08-28 13:39:30 -04:00
|
|
|
}{
|
|
|
|
{
|
2018-04-03 16:24:44 +02:00
|
|
|
// Empty javaPkg
|
2015-08-28 13:39:30 -04:00
|
|
|
},
|
|
|
|
{
|
2018-04-03 16:24:44 +02:00
|
|
|
javaPkg: "com.example.foo",
|
2015-08-28 13:39:30 -04:00
|
|
|
},
|
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
|
2018-03-16 11:20:43 +01:00
|
|
|
gopath = filepath.SplitList(goEnv("GOPATH"))[0]
|
2015-08-28 13:39:30 -04:00
|
|
|
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
|
2018-03-07 22:51:37 +01:00
|
|
|
JavaPkg string
|
2015-08-28 13:39:30 -04:00
|
|
|
}{
|
|
|
|
outputData: defaultOutputData(),
|
|
|
|
AndroidPlatform: platform,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:24:44 +02:00
|
|
|
func TestBindIOS(t *testing.T) {
|
|
|
|
if !xcodeAvailable() {
|
|
|
|
t.Skip("Xcode is missing")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
xout = os.Stderr
|
|
|
|
buildN = false
|
|
|
|
buildX = false
|
|
|
|
buildO = ""
|
|
|
|
buildTarget = ""
|
|
|
|
bindPrefix = ""
|
|
|
|
}()
|
|
|
|
buildN = true
|
|
|
|
buildX = true
|
|
|
|
buildO = "Asset.framework"
|
|
|
|
buildTarget = "ios/arm"
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
prefix string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// empty prefix
|
|
|
|
},
|
|
|
|
{
|
|
|
|
prefix: "Foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
|
|
bindPrefix = tc.prefix
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
xout = buf
|
|
|
|
gopath = filepath.SplitList(goEnv("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
|
|
|
|
Prefix string
|
|
|
|
}{
|
|
|
|
outputData: defaultOutputData(),
|
|
|
|
Prefix: tc.prefix,
|
|
|
|
}
|
|
|
|
|
|
|
|
wantBuf := new(bytes.Buffer)
|
|
|
|
if err := bindIOSTmpl.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-04-19 23:38:10 +02:00
|
|
|
GOOS=android CGO_ENABLED=1 gobind -lang=go,java -outdir=$WORK{{if .JavaPkg}} -javapkg={{.JavaPkg}}{{end}} golang.org/x/mobile/asset
|
2019-03-16 15:42:20 +01:00
|
|
|
GOOS=android GOARCH=arm CC=$NDK_PATH/toolchains/llvm/prebuilt/{{.NDKARCH}}/bin/armv7a-linux-androideabi16-clang CXX=$NDK_PATH/toolchains/llvm/prebuilt/{{.NDKARCH}}/bin/armv7a-linux-androideabi16-clang++ CGO_ENABLED=1 GOARM=7 GOPATH=$WORK:$GOPATH GO111MODULE=off go build -x -buildmode=c-shared -o=$WORK/android/src/main/jniLibs/armeabi-v7a/libgojni.so gobind
|
2018-03-07 22:51:37 +01:00
|
|
|
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 .
|
|
|
|
`))
|
2018-04-03 16:24:44 +02:00
|
|
|
|
|
|
|
var bindIOSTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
|
|
|
|
WORK=$WORK
|
2018-04-19 23:38:10 +02:00
|
|
|
GOOS=darwin CGO_ENABLED=1 gobind -lang=go,objc -outdir=$WORK -tags=ios{{if .Prefix}} -prefix={{.Prefix}}{{end}} golang.org/x/mobile/asset
|
2019-03-19 17:20:14 +02:00
|
|
|
GOARM=7 GOOS=darwin GOARCH=arm CC=iphoneos-clang CXX=iphoneos-clang++ CGO_CFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch armv7 CGO_CXXFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch armv7 CGO_LDFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch armv7 CGO_ENABLED=1 GOPATH=$WORK:$GOPATH GO111MODULE=off go build -tags ios -x -buildmode=c-archive -o $WORK/asset-arm.a gobind
|
2018-04-03 16:24:44 +02:00
|
|
|
rm -r -f "Asset.framework"
|
|
|
|
mkdir -p Asset.framework/Versions/A/Headers
|
|
|
|
ln -s A Asset.framework/Versions/Current
|
|
|
|
ln -s Versions/Current/Headers Asset.framework/Headers
|
|
|
|
ln -s Versions/Current/Asset Asset.framework/Asset
|
|
|
|
xcrun lipo -create -arch armv7 $WORK/asset-arm.a -o Asset.framework/Versions/A/Asset
|
|
|
|
cp $WORK/src/gobind/{{.Prefix}}Asset.objc.h Asset.framework/Versions/A/Headers/{{.Prefix}}Asset.objc.h
|
|
|
|
mkdir -p Asset.framework/Versions/A/Headers
|
2019-03-06 01:10:55 +01:00
|
|
|
cp $WORK/src/gobind/Universe.objc.h Asset.framework/Versions/A/Headers/Universe.objc.h
|
2018-04-03 16:24:44 +02:00
|
|
|
mkdir -p Asset.framework/Versions/A/Headers
|
|
|
|
cp $WORK/src/gobind/ref.h Asset.framework/Versions/A/Headers/ref.h
|
|
|
|
mkdir -p Asset.framework/Versions/A/Headers
|
|
|
|
mkdir -p Asset.framework/Versions/A/Headers
|
|
|
|
mkdir -p Asset.framework/Versions/A/Resources
|
|
|
|
ln -s Versions/Current/Resources Asset.framework/Resources
|
|
|
|
mkdir -p Asset.framework/Resources
|
|
|
|
mkdir -p Asset.framework/Versions/A/Modules
|
|
|
|
ln -s Versions/Current/Modules Asset.framework/Modules
|
|
|
|
`))
|