2015-07-10 16:47:46 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-07-16 13:32:51 -04:00
|
|
|
"os/exec"
|
2015-07-10 16:47:46 -06:00
|
|
|
"path/filepath"
|
2015-07-16 13:32:51 -04:00
|
|
|
"runtime"
|
2015-07-13 21:02:58 -04:00
|
|
|
"strings"
|
2015-07-10 16:47:46 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// General mobile build environment. Initialized by envInit.
|
|
|
|
var (
|
|
|
|
cwd string
|
|
|
|
gomobilepath string // $GOPATH/pkg/gomobile
|
|
|
|
|
2015-12-10 15:52:40 -05:00
|
|
|
androidEnv map[string][]string // android arch -> []string
|
|
|
|
|
2018-03-28 13:37:41 +02:00
|
|
|
darwinEnv map[string][]string
|
2015-07-24 16:47:16 -04:00
|
|
|
|
|
|
|
androidArmNM string
|
|
|
|
darwinArmNM string
|
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
|
|
|
|
2018-03-28 13:37:41 +02:00
|
|
|
allArchs = []string{"arm", "arm64", "386", "amd64"}
|
2015-07-10 16:47:46 -06:00
|
|
|
)
|
|
|
|
|
2015-07-16 13:32:51 -04:00
|
|
|
func buildEnvInit() (cleanup func(), err error) {
|
2015-07-10 16:47:46 -06:00
|
|
|
// Find gomobilepath.
|
|
|
|
gopath := goEnv("GOPATH")
|
|
|
|
for _, p := range filepath.SplitList(gopath) {
|
|
|
|
gomobilepath = filepath.Join(p, "pkg", "gomobile")
|
2015-07-16 20:36:27 -04:00
|
|
|
if _, err := os.Stat(gomobilepath); buildN || err == nil {
|
2015-07-10 16:47:46 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-07-16 20:36:27 -04:00
|
|
|
|
2015-07-10 16:47:46 -06:00
|
|
|
if buildX {
|
|
|
|
fmt.Fprintln(xout, "GOMOBILE="+gomobilepath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the toolchain is in a good state.
|
2015-07-16 20:36:27 -04:00
|
|
|
// Pick a temporary directory for assembling an apk/app.
|
2015-07-10 16:47:46 -06:00
|
|
|
if gomobilepath == "" {
|
|
|
|
return nil, errors.New("toolchain not installed, run `gomobile init`")
|
|
|
|
}
|
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
|
|
|
|
|
|
|
if err := envInit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-27 16:33:08 -04:00
|
|
|
cleanupFn := func() {
|
|
|
|
if buildWork {
|
|
|
|
fmt.Printf("WORK=%s\n", tmpdir)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
removeAll(tmpdir)
|
|
|
|
}
|
2015-07-16 13:32:51 -04:00
|
|
|
if buildN {
|
|
|
|
tmpdir = "$WORK"
|
2015-07-16 20:36:27 -04:00
|
|
|
cleanupFn = func() {}
|
2015-07-16 13:32:51 -04:00
|
|
|
} else {
|
|
|
|
tmpdir, err = ioutil.TempDir("", "gomobile-work-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if buildX {
|
|
|
|
fmt.Fprintln(xout, "WORK="+tmpdir)
|
|
|
|
}
|
|
|
|
|
2015-07-16 20:36:27 -04:00
|
|
|
return cleanupFn, nil
|
2015-07-16 13:32:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func envInit() (err error) {
|
|
|
|
// TODO(crawshaw): cwd only used by ctx.Import, which can take "."
|
|
|
|
cwd, err = os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-10 16:47:46 -06:00
|
|
|
// Setup the cross-compiler environments.
|
2018-03-15 17:30:30 +01:00
|
|
|
if hasNDK() {
|
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
|
|
|
androidEnv = make(map[string][]string)
|
|
|
|
for arch, toolchain := range ndk {
|
|
|
|
androidEnv[arch] = []string{
|
|
|
|
"GOOS=android",
|
|
|
|
"GOARCH=" + arch,
|
|
|
|
"CC=" + toolchain.Path("clang"),
|
|
|
|
"CXX=" + toolchain.Path("clang++"),
|
|
|
|
"CGO_ENABLED=1",
|
|
|
|
}
|
|
|
|
if arch == "arm" {
|
|
|
|
androidEnv[arch] = append(androidEnv[arch], "GOARM=7")
|
|
|
|
}
|
2015-12-10 15:52:40 -05:00
|
|
|
}
|
2015-07-16 13:32:51 -04:00
|
|
|
}
|
2015-07-10 16:47:46 -06:00
|
|
|
|
2017-01-27 18:29:22 +01:00
|
|
|
if !xcodeAvailable() {
|
2015-07-16 13:32:51 -04:00
|
|
|
return nil
|
2015-07-10 16:47:46 -06:00
|
|
|
}
|
2015-07-16 13:32:51 -04:00
|
|
|
|
2015-07-24 16:47:16 -04:00
|
|
|
darwinArmNM = "nm"
|
2018-03-28 13:37:41 +02:00
|
|
|
darwinEnv = make(map[string][]string)
|
|
|
|
for _, arch := range allArchs {
|
|
|
|
var env []string
|
|
|
|
var err error
|
|
|
|
var clang, cflags string
|
|
|
|
switch arch {
|
|
|
|
case "arm":
|
|
|
|
env = append(env, "GOARM=7")
|
|
|
|
fallthrough
|
|
|
|
case "arm64":
|
|
|
|
clang, cflags, err = envClang("iphoneos")
|
|
|
|
cflags += " -miphoneos-version-min=6.1"
|
|
|
|
case "386", "amd64":
|
|
|
|
clang, cflags, err = envClang("iphonesimulator")
|
|
|
|
cflags += " -mios-simulator-version-min=6.1"
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown GOARCH: %q", arch))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
env = append(env,
|
|
|
|
"GOOS=darwin",
|
|
|
|
"GOARCH="+arch,
|
|
|
|
"CC="+clang,
|
|
|
|
"CXX="+clang,
|
|
|
|
"CGO_CFLAGS="+cflags+" -arch "+archClang(arch),
|
|
|
|
"CGO_LDFLAGS="+cflags+" -arch "+archClang(arch),
|
|
|
|
"CGO_ENABLED=1",
|
|
|
|
)
|
|
|
|
darwinEnv[arch] = env
|
2015-07-10 16:47:46 -06:00
|
|
|
}
|
|
|
|
|
2015-07-16 13:32:51 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:30:30 +01:00
|
|
|
func hasNDK() bool {
|
|
|
|
if buildN {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
tcPath := filepath.Join(gomobilepath, "ndk-toolchains")
|
|
|
|
_, err := os.Stat(tcPath)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2015-07-16 13:32:51 -04:00
|
|
|
func envClang(sdkName string) (clang, cflags string, err error) {
|
2015-07-16 20:36:27 -04:00
|
|
|
if buildN {
|
|
|
|
return "clang-" + sdkName, "-isysroot=" + sdkName, nil
|
|
|
|
}
|
2015-07-16 13:32:51 -04:00
|
|
|
cmd := exec.Command("xcrun", "--sdk", sdkName, "--find", "clang")
|
2015-10-01 14:30:07 -04:00
|
|
|
out, err := cmd.CombinedOutput()
|
2015-07-16 13:32:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("xcrun --find: %v\n%s", err, out)
|
|
|
|
}
|
|
|
|
clang = strings.TrimSpace(string(out))
|
|
|
|
|
|
|
|
cmd = exec.Command("xcrun", "--sdk", sdkName, "--show-sdk-path")
|
2015-10-01 14:30:07 -04:00
|
|
|
out, err = cmd.CombinedOutput()
|
2015-07-16 13:32:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("xcrun --show-sdk-path: %v\n%s", err, out)
|
|
|
|
}
|
|
|
|
sdk := strings.TrimSpace(string(out))
|
|
|
|
return clang, "-isysroot " + sdk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func archClang(goarch string) string {
|
|
|
|
switch goarch {
|
|
|
|
case "arm":
|
|
|
|
return "armv7"
|
|
|
|
case "arm64":
|
|
|
|
return "arm64"
|
|
|
|
case "386":
|
|
|
|
return "i386"
|
|
|
|
case "amd64":
|
|
|
|
return "x86_64"
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown GOARCH: %q", goarch))
|
|
|
|
}
|
2015-07-10 16:47:46 -06:00
|
|
|
}
|
2015-07-13 21:02:58 -04:00
|
|
|
|
|
|
|
// environ merges os.Environ and the given "key=value" pairs.
|
|
|
|
// If a key is in both os.Environ and kv, kv takes precedence.
|
|
|
|
func environ(kv []string) []string {
|
|
|
|
cur := os.Environ()
|
|
|
|
new := make([]string, 0, len(cur)+len(kv))
|
|
|
|
|
|
|
|
envs := make(map[string]string, len(cur))
|
|
|
|
for _, ev := range cur {
|
|
|
|
elem := strings.SplitN(ev, "=", 2)
|
|
|
|
if len(elem) != 2 || elem[0] == "" {
|
|
|
|
// pass the env var of unusual form untouched.
|
|
|
|
// e.g. Windows may have env var names starting with "=".
|
|
|
|
new = append(new, ev)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if goos == "windows" {
|
|
|
|
elem[0] = strings.ToUpper(elem[0])
|
|
|
|
}
|
|
|
|
envs[elem[0]] = elem[1]
|
|
|
|
}
|
|
|
|
for _, ev := range kv {
|
|
|
|
elem := strings.SplitN(ev, "=", 2)
|
|
|
|
if len(elem) != 2 || elem[0] == "" {
|
|
|
|
panic(fmt.Sprintf("malformed env var %q from input", ev))
|
|
|
|
}
|
|
|
|
if goos == "windows" {
|
|
|
|
elem[0] = strings.ToUpper(elem[0])
|
|
|
|
}
|
|
|
|
envs[elem[0]] = elem[1]
|
|
|
|
}
|
|
|
|
for k, v := range envs {
|
|
|
|
new = append(new, k+"="+v)
|
|
|
|
}
|
|
|
|
return new
|
|
|
|
}
|
2015-07-16 13:32:51 -04:00
|
|
|
|
|
|
|
func getenv(env []string, key string) string {
|
|
|
|
prefix := key + "="
|
|
|
|
for _, kv := range env {
|
|
|
|
if strings.HasPrefix(kv, prefix) {
|
|
|
|
return kv[len(prefix):]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func archNDK() string {
|
|
|
|
if runtime.GOOS == "windows" && runtime.GOARCH == "386" {
|
|
|
|
return "windows"
|
|
|
|
} else {
|
|
|
|
var arch string
|
|
|
|
switch runtime.GOARCH {
|
|
|
|
case "386":
|
|
|
|
arch = "x86"
|
|
|
|
case "amd64":
|
|
|
|
arch = "x86_64"
|
|
|
|
default:
|
|
|
|
panic("unsupported GOARCH: " + runtime.GOARCH)
|
|
|
|
}
|
|
|
|
return runtime.GOOS + "-" + arch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 15:52:40 -05:00
|
|
|
type ndkToolchain struct {
|
|
|
|
arch string
|
|
|
|
abi string
|
|
|
|
platform string
|
|
|
|
gcc string
|
|
|
|
toolPrefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *ndkToolchain) Path(toolName string) string {
|
2018-03-15 17:30:30 +01:00
|
|
|
return filepath.Join(gomobilepath, "ndk-toolchains", tc.arch, "bin", tc.toolPrefix+"-"+toolName)
|
2015-12-10 15:52:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type ndkConfig map[string]ndkToolchain // map: GOOS->androidConfig.
|
|
|
|
|
|
|
|
func (nc ndkConfig) Toolchain(arch string) ndkToolchain {
|
|
|
|
tc, ok := nc[arch]
|
2017-01-29 12:51:07 +01:00
|
|
|
if !ok {
|
2015-12-10 15:52:40 -05:00
|
|
|
panic(`unsupported architecture: ` + arch)
|
|
|
|
}
|
|
|
|
return tc
|
|
|
|
}
|
|
|
|
|
|
|
|
var ndk = ndkConfig{
|
|
|
|
"arm": {
|
|
|
|
arch: "arm",
|
|
|
|
abi: "armeabi-v7a",
|
|
|
|
platform: "android-15",
|
2016-06-01 10:20:32 +02:00
|
|
|
gcc: "arm-linux-androideabi-4.9",
|
2015-12-10 15:52:40 -05:00
|
|
|
toolPrefix: "arm-linux-androideabi",
|
|
|
|
},
|
2016-03-14 17:47:25 -04:00
|
|
|
"arm64": {
|
|
|
|
arch: "arm64",
|
|
|
|
abi: "arm64-v8a",
|
|
|
|
platform: "android-21",
|
|
|
|
gcc: "aarch64-linux-android-4.9",
|
|
|
|
toolPrefix: "aarch64-linux-android",
|
|
|
|
},
|
2016-02-18 05:25:39 -05:00
|
|
|
|
|
|
|
"386": {
|
|
|
|
arch: "x86",
|
|
|
|
abi: "x86",
|
|
|
|
platform: "android-15",
|
2016-06-01 10:20:32 +02:00
|
|
|
gcc: "x86-4.9",
|
2016-02-18 05:25:39 -05:00
|
|
|
toolPrefix: "i686-linux-android",
|
|
|
|
},
|
|
|
|
"amd64": {
|
|
|
|
arch: "x86_64",
|
|
|
|
abi: "x86_64",
|
|
|
|
platform: "android-21",
|
|
|
|
gcc: "x86_64-4.9",
|
|
|
|
toolPrefix: "x86_64-linux-android",
|
|
|
|
},
|
2015-12-10 15:52:40 -05:00
|
|
|
}
|
2017-01-27 18:29:22 +01:00
|
|
|
|
|
|
|
func xcodeAvailable() bool {
|
2018-01-20 22:30:55 +09:00
|
|
|
err := exec.Command("xcrun", "xcodebuild", "-version").Run()
|
2017-01-27 18:29:22 +01:00
|
|
|
return err == nil
|
|
|
|
}
|