2015-06-19 12:27:15 -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"
|
|
|
|
"path/filepath"
|
2015-12-11 18:36:28 -05:00
|
|
|
"strings"
|
2015-06-19 12:27:15 -04:00
|
|
|
"testing"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
2015-08-02 19:52:31 -04:00
|
|
|
func TestRFC1034Label(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in, want string
|
|
|
|
}{
|
|
|
|
{"a", "a"},
|
|
|
|
{"123", "-23"},
|
|
|
|
{"a.b.c", "a-b-c"},
|
|
|
|
{"a-b", "a-b"},
|
|
|
|
{"a:b", "a-b"},
|
|
|
|
{"a?b", "a-b"},
|
|
|
|
{"αβγ", "---"},
|
|
|
|
{"💩", "--"},
|
|
|
|
{"My App", "My-App"},
|
|
|
|
{"...", ""},
|
|
|
|
{".-.", "--"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
if got := rfc1034Label(tc.in); got != tc.want {
|
|
|
|
t.Errorf("rfc1034Label(%q) = %q, want %q", tc.in, got, tc.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:08:01 -05:00
|
|
|
func TestAndroidPkgName(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in, want string
|
|
|
|
}{
|
|
|
|
{"a", "a"},
|
|
|
|
{"a123", "a123"},
|
|
|
|
{"a.b.c", "a_b_c"},
|
|
|
|
{"a-b", "a_b"},
|
|
|
|
{"a:b", "a_b"},
|
|
|
|
{"a?b", "a_b"},
|
|
|
|
{"αβγ", "go___"},
|
|
|
|
{"💩", "go_"},
|
|
|
|
{"My App", "My_App"},
|
|
|
|
{"...", "go___"},
|
|
|
|
{".-.", "go___"},
|
|
|
|
{"abstract", "abstract_"},
|
|
|
|
{"Abstract", "Abstract"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
if got := androidPkgName(tc.in); got != tc.want {
|
|
|
|
t.Errorf("len %d", len(tc.in))
|
|
|
|
t.Errorf("androidPkgName(%q) = %q, want %q", tc.in, got, tc.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 14:45:53 -07:00
|
|
|
func TestAndroidBuild(t *testing.T) {
|
2015-06-19 12:27:15 -04:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
defer func() {
|
|
|
|
xout = os.Stderr
|
|
|
|
buildN = false
|
|
|
|
buildX = false
|
|
|
|
}()
|
|
|
|
xout = buf
|
|
|
|
buildN = true
|
|
|
|
buildX = true
|
2015-07-16 20:36:27 -04:00
|
|
|
buildO = "basic.apk"
|
|
|
|
buildTarget = "android"
|
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-08-24 17:32:49 -04:00
|
|
|
gopath = filepath.ToSlash(filepath.SplitList(os.Getenv("GOPATH"))[0])
|
2015-06-19 12:27:15 -04:00
|
|
|
if goos == "windows" {
|
|
|
|
os.Setenv("HOMEDRIVE", "C:")
|
|
|
|
}
|
|
|
|
cmdBuild.flag.Parse([]string{"golang.org/x/mobile/example/basic"})
|
2015-10-28 12:45:15 -07:00
|
|
|
ctx.BuildTags = []string{"tag1"}
|
2015-06-19 12:27:15 -04:00
|
|
|
err := runBuild(cmdBuild)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(buf.String())
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-07-21 14:45:53 -07:00
|
|
|
diff, err := diffOutput(buf.String(), androidBuildTmpl)
|
2015-06-19 12:27:15 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("computing diff failed: %v", err)
|
|
|
|
}
|
|
|
|
if diff != "" {
|
|
|
|
t.Errorf("unexpected output:\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 14:45:53 -07:00
|
|
|
var androidBuildTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
|
2015-07-10 16:47:46 -06:00
|
|
|
WORK=$WORK
|
2015-12-11 17:19:23 -05:00
|
|
|
mkdir -p $WORK/lib/armeabi-v7a
|
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
|
|
|
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 --sysroot /NDK/platforms/android-15/arch-arm -gcc-toolchain /NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/{{.GOOS}}-{{.NDKARCH}} -I$GOMOBILE/include CGO_CPPFLAGS=-target armv7a-none-linux-androideabi --sysroot /NDK/platforms/android-15/arch-arm -gcc-toolchain /NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/{{.GOOS}}-{{.NDKARCH}} -I$GOMOBILE/include CGO_LDFLAGS=-target armv7a-none-linux-androideabi --sysroot /NDK/platforms/android-15/arch-arm -gcc-toolchain /NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/{{.GOOS}}-{{.NDKARCH}} -L/NDK/platforms/android-15/arch-arm/usr/lib -L$GOMOBILE/lib/arm CGO_ENABLED=1 GOARM=7 go build -pkgdir=$GOMOBILE/pkg_android_arm -tags tag1 -x -buildmode=c-shared -o $WORK/lib/armeabi-v7a/libbasic.so golang.org/x/mobile/example/basic
|
2015-06-19 12:27:15 -04:00
|
|
|
`))
|
2015-12-11 18:36:28 -05:00
|
|
|
|
|
|
|
func TestParseBuildTargetFlag(t *testing.T) {
|
|
|
|
androidArchs := "arm"
|
|
|
|
iosArchs := "arm,arm64,amd64"
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
in string
|
|
|
|
wantErr bool
|
|
|
|
wantOS string
|
|
|
|
wantArchs string
|
|
|
|
}{
|
|
|
|
{"android", false, "android", androidArchs},
|
|
|
|
{"android,android/arm", false, "android", androidArchs},
|
|
|
|
{"android/arm", false, "android", "arm"},
|
|
|
|
|
2015-12-18 12:04:02 -05:00
|
|
|
{"ios", false, "darwin", iosArchs},
|
|
|
|
{"ios,ios/arm", false, "darwin", iosArchs},
|
|
|
|
{"ios/arm", false, "darwin", "arm"},
|
|
|
|
{"ios/amd64", false, "darwin", "amd64"},
|
2015-12-11 18:36:28 -05:00
|
|
|
|
|
|
|
{"", true, "", ""},
|
|
|
|
{"linux", true, "", ""},
|
|
|
|
{"android/x86", true, "", ""},
|
|
|
|
{"android/arm5", true, "", ""},
|
|
|
|
{"ios/mips", true, "", ""},
|
|
|
|
{"android,ios", true, "", ""},
|
|
|
|
{"ios,android", true, "", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
gotOS, gotArchs, err := parseBuildTarget(tc.in)
|
|
|
|
if tc.wantErr {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("-target=%q; want error, got (%q, %q, nil)", tc.in, gotOS, gotArchs)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil || gotOS != tc.wantOS || strings.Join(gotArchs, ",") != tc.wantArchs {
|
|
|
|
t.Errorf("-target=%q; want (%v, [%v], nil), got (%q, %q, %v)",
|
|
|
|
tc.in, tc.wantOS, tc.wantArchs, gotOS, gotArchs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|