2015-02-05 21:33:48 +00: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 (
|
2015-02-16 13:47:49 -05:00
|
|
|
"bytes"
|
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
|
|
|
"errors"
|
2015-02-05 21:33:48 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2015-06-30 14:05:08 -04:00
|
|
|
"time"
|
2015-02-05 21:33:48 +00:00
|
|
|
)
|
|
|
|
|
2015-02-16 13:47:49 -05:00
|
|
|
var (
|
|
|
|
goos = runtime.GOOS
|
|
|
|
goarch = runtime.GOARCH
|
|
|
|
ndkarch string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2015-06-11 09:56:44 -07:00
|
|
|
switch runtime.GOARCH {
|
|
|
|
case "amd64":
|
2015-02-16 13:47:49 -05:00
|
|
|
ndkarch = "x86_64"
|
2015-06-11 09:56:44 -07:00
|
|
|
case "386":
|
|
|
|
ndkarch = "x86"
|
|
|
|
default:
|
2015-02-16 13:47:49 -05:00
|
|
|
ndkarch = runtime.GOARCH
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 15:20:18 -05:00
|
|
|
|
2015-02-05 21:33:48 +00:00
|
|
|
var cmdInit = &command{
|
|
|
|
run: runInit,
|
|
|
|
Name: "init",
|
2015-03-26 14:21:54 -04:00
|
|
|
Usage: "[-u]",
|
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
|
|
|
Short: "install mobile compiler toolchain",
|
2015-02-05 21:33:48 +00:00
|
|
|
Long: `
|
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
|
|
|
Init builds copies of the Go standard library for mobile devices.
|
|
|
|
It uses Xcode, if available, to build for iOS and uses the Android
|
|
|
|
NDK from the ndk-bundle SDK package or from the -ndk flag, to build
|
|
|
|
for Android.
|
|
|
|
If a OpenAL source directory is specified with -openal, init will
|
|
|
|
also build an Android version of OpenAL for use with gomobile build
|
|
|
|
and gomobile install.
|
2015-02-05 21:33:48 +00:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
var (
|
|
|
|
initNDK string // -ndk
|
|
|
|
initOpenAL string // -openal
|
|
|
|
)
|
2015-02-18 01:45:11 -05:00
|
|
|
|
|
|
|
func 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
|
|
|
cmdInit.flag.StringVar(&initNDK, "ndk", "", "Android NDK path")
|
|
|
|
cmdInit.flag.StringVar(&initOpenAL, "openal", "", "OpenAL source path")
|
2015-02-18 01:45:11 -05:00
|
|
|
}
|
|
|
|
|
2015-02-05 21:33:48 +00:00
|
|
|
func runInit(cmd *command) error {
|
2015-02-11 13:53:52 -05:00
|
|
|
gopaths := filepath.SplitList(goEnv("GOPATH"))
|
|
|
|
if len(gopaths) == 0 {
|
|
|
|
return fmt.Errorf("GOPATH is not set")
|
|
|
|
}
|
2015-05-03 10:50:35 -04:00
|
|
|
gomobilepath = filepath.Join(gopaths[0], "pkg/gomobile")
|
2015-12-10 15:52:40 -05:00
|
|
|
|
|
|
|
verpath := filepath.Join(gomobilepath, "version")
|
2015-07-21 15:44:18 -04:00
|
|
|
if buildX || buildN {
|
2015-05-03 10:50:35 -04:00
|
|
|
fmt.Fprintln(xout, "GOMOBILE="+gomobilepath)
|
2015-02-12 08:23:25 -05:00
|
|
|
}
|
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
|
|
|
removeAll(gomobilepath)
|
2015-02-11 13:53:52 -05:00
|
|
|
|
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 := mkdir(gomobilepath); err != nil {
|
2015-05-03 10:50:35 -04:00
|
|
|
return err
|
2015-02-05 21:33:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 08:23:25 -05:00
|
|
|
if buildN {
|
2015-05-03 10:50:35 -04:00
|
|
|
tmpdir = filepath.Join(gomobilepath, "work")
|
2015-02-12 08:23:25 -05:00
|
|
|
} else {
|
|
|
|
var err error
|
2015-05-03 10:50:35 -04:00
|
|
|
tmpdir, err = ioutil.TempDir(gomobilepath, "work-")
|
2015-02-12 08:23:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 15:44:18 -04:00
|
|
|
if buildX || buildN {
|
2015-02-16 13:47:49 -05:00
|
|
|
fmt.Fprintln(xout, "WORK="+tmpdir)
|
2015-02-05 21:33:48 +00:00
|
|
|
}
|
2015-07-27 16:33:08 -04:00
|
|
|
defer func() {
|
|
|
|
if buildWork {
|
|
|
|
fmt.Printf("WORK=%s\n", tmpdir)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
removeAll(tmpdir)
|
|
|
|
}()
|
2015-02-05 21:33:48 +00:00
|
|
|
|
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 buildN {
|
|
|
|
initNDK = "$NDK_PATH"
|
|
|
|
initOpenAL = "$OPENAL_PATH"
|
|
|
|
} else {
|
|
|
|
toolsDir := filepath.Join("prebuilt", archNDK(), "bin")
|
|
|
|
// Try the ndk-bundle SDK package package, if installed.
|
|
|
|
if initNDK == "" {
|
|
|
|
if sdkHome := os.Getenv("ANDROID_HOME"); sdkHome != "" {
|
|
|
|
path := filepath.Join(sdkHome, "ndk-bundle")
|
|
|
|
if st, err := os.Stat(filepath.Join(path, toolsDir)); err == nil && st.IsDir() {
|
|
|
|
initNDK = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if initNDK != "" {
|
|
|
|
var err error
|
|
|
|
if initNDK, err = filepath.Abs(initNDK); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Check if the platform directory contains a known subdirectory.
|
|
|
|
if _, err := os.Stat(filepath.Join(initNDK, toolsDir)); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("%q does not point to an Android NDK.", initNDK)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ndkFile := filepath.Join(gomobilepath, "android_ndk_root")
|
|
|
|
if err := ioutil.WriteFile(ndkFile, []byte(initNDK), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if initOpenAL != "" {
|
|
|
|
var err error
|
|
|
|
if initOpenAL, err = filepath.Abs(initOpenAL); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-02-05 21:33:48 +00:00
|
|
|
}
|
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 = initNDK
|
|
|
|
if err := envInit(); err != nil {
|
2015-07-16 13:32:51 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-24 16:54:11 -04:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
// Install common x/mobile packages for local development.
|
|
|
|
// These are often slow to compile (due to cgo) and easy to forget.
|
|
|
|
//
|
|
|
|
// Limited to darwin for now as it is common for linux to
|
|
|
|
// not have GLES installed.
|
|
|
|
//
|
|
|
|
// TODO: consider testing GLES installation and suggesting it here
|
|
|
|
for _, pkg := range commonPkgs {
|
|
|
|
if err := installPkg(pkg, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-20 13:50:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 14:37:57 -04:00
|
|
|
// Install standard libraries for cross compilers.
|
2015-06-30 14:05:08 -04:00
|
|
|
start := time.Now()
|
2017-01-29 12:51:07 +01:00
|
|
|
// Ideally this would be -buildmode=c-shared.
|
|
|
|
// https://golang.org/issue/13234.
|
|
|
|
androidArgs := []string{"-gcflags=-shared", "-ldflags=-shared"}
|
|
|
|
for _, arch := range archs {
|
|
|
|
env := androidEnv[arch]
|
2015-12-11 17:19:23 -05:00
|
|
|
if err := installStd(env, androidArgs...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-29 14:37:57 -04:00
|
|
|
}
|
2015-12-11 17:19:23 -05:00
|
|
|
|
2015-06-30 14:05:08 -04:00
|
|
|
if err := installDarwin(); err != nil {
|
|
|
|
return err
|
2015-06-29 14:37:57 -04:00
|
|
|
}
|
|
|
|
|
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 := installOpenAL(gomobilepath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:44:18 -04:00
|
|
|
if buildX || buildN {
|
2015-06-29 14:37:57 -04:00
|
|
|
printcmd("go version > %s", verpath)
|
|
|
|
}
|
|
|
|
if !buildN {
|
2015-11-17 13:24:48 -05:00
|
|
|
if err := ioutil.WriteFile(verpath, goVersionOut, 0644); err != nil {
|
2015-06-29 14:37:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-06-30 14:05:08 -04:00
|
|
|
if buildV {
|
|
|
|
took := time.Since(start) / time.Second * time.Second
|
|
|
|
fmt.Fprintf(os.Stderr, "\nDone, build took %s.\n", took)
|
|
|
|
}
|
2015-06-29 14:37:57 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-02-18 01:45:11 -05:00
|
|
|
|
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 installOpenAL(gomobilepath string) error {
|
|
|
|
if ndkRoot == "" || initOpenAL == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var cmake string
|
|
|
|
if buildN {
|
|
|
|
cmake = "cmake"
|
|
|
|
} else {
|
2017-02-21 22:37:37 +01:00
|
|
|
sdkRoot := os.Getenv("ANDROID_HOME")
|
|
|
|
if sdkRoot == "" {
|
|
|
|
return nil
|
|
|
|
}
|
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
|
|
|
var err error
|
|
|
|
cmake, err = exec.LookPath("cmake")
|
|
|
|
if err != nil {
|
|
|
|
cmakePath := filepath.Join(sdkRoot, "cmake")
|
|
|
|
cmakeDir, err := os.Open(cmakePath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Skip OpenAL install if the cmake package is not installed.
|
|
|
|
return errors.New("cmake was not found in the PATH. Please install it through the Android SDK manager.")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer cmakeDir.Close()
|
|
|
|
// There might be multiple versions of CMake installed. Use any one for now.
|
|
|
|
cmakeVers, err := cmakeDir.Readdirnames(1)
|
|
|
|
if err != nil || len(cmakeVers) == 0 {
|
|
|
|
return errors.New("cmake was not found in the PATH. Please install it through the Android SDK manager.")
|
|
|
|
}
|
|
|
|
cmake = filepath.Join(cmakePath, cmakeVers[0], "bin", "cmake")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var alTmpDir string
|
|
|
|
if buildN {
|
|
|
|
alTmpDir = filepath.Join(gomobilepath, "work")
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
alTmpDir, err = ioutil.TempDir(gomobilepath, "openal-release-")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer removeAll(alTmpDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range []string{"include/AL/al.h", "include/AL/alc.h"} {
|
|
|
|
dst := filepath.Join(gomobilepath, f)
|
|
|
|
src := filepath.Join(initOpenAL, f)
|
|
|
|
if err := copyFile(dst, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toolsDir := filepath.Join(ndkRoot, "prebuilt", archNDK(), "bin")
|
|
|
|
py27 := filepath.Join(toolsDir, "python2.7")
|
|
|
|
var make string
|
|
|
|
if !buildN && runtime.GOOS == "windows" {
|
|
|
|
var err error
|
|
|
|
make, err = exec.LookPath("nmake")
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
make = filepath.Join(toolsDir, "make")
|
|
|
|
}
|
|
|
|
for _, arch := range archs {
|
|
|
|
t := ndk[arch]
|
|
|
|
abi := t.arch
|
|
|
|
if abi == "arm" {
|
|
|
|
abi = "armeabi"
|
|
|
|
}
|
|
|
|
// Split android-XX to get the api version.
|
|
|
|
platform := strings.SplitN(t.platform, "-", 2)
|
|
|
|
api := platform[1]
|
|
|
|
buildDir := alTmpDir + "/build/" + abi
|
|
|
|
toolchain := buildDir + "/toolchain"
|
|
|
|
// standalone ndk toolchains make openal-soft's build config easier.
|
|
|
|
cmd := exec.Command(py27,
|
|
|
|
"build/tools/make_standalone_toolchain.py",
|
|
|
|
"--arch="+t.arch,
|
|
|
|
"--api="+api,
|
|
|
|
"--install-dir="+toolchain)
|
|
|
|
cmd.Dir = ndkRoot
|
|
|
|
if err := runCmd(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(cmake,
|
|
|
|
initOpenAL,
|
|
|
|
"-DCMAKE_TOOLCHAIN_FILE="+initOpenAL+"/XCompile-Android.txt",
|
|
|
|
"-DHOST="+t.toolPrefix)
|
|
|
|
cmd.Dir = buildDir
|
|
|
|
orgPath := os.Getenv("PATH")
|
|
|
|
if !buildN {
|
|
|
|
cmd.Env = []string{"PATH=" + toolchain + "/bin" + string(os.PathListSeparator) + orgPath}
|
|
|
|
}
|
|
|
|
if err := runCmd(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(make)
|
|
|
|
cmd.Dir = buildDir
|
|
|
|
if err := runCmd(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dst := filepath.Join(gomobilepath, "lib", t.abi, "libopenal.so")
|
|
|
|
src := filepath.Join(alTmpDir, "build", abi, "libopenal.so")
|
|
|
|
if err := copyFile(dst, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-20 13:50:18 -04:00
|
|
|
var commonPkgs = []string{
|
|
|
|
"golang.org/x/mobile/gl",
|
|
|
|
"golang.org/x/mobile/app",
|
|
|
|
"golang.org/x/mobile/exp/app/debug",
|
|
|
|
}
|
|
|
|
|
2015-06-30 14:05:08 -04:00
|
|
|
func installDarwin() error {
|
2017-01-27 18:29:22 +01:00
|
|
|
if !xcodeAvailable() {
|
|
|
|
return nil
|
2015-02-16 13:47:49 -05:00
|
|
|
}
|
2015-07-16 13:32:51 -04:00
|
|
|
if err := installStd(darwinArmEnv); err != nil {
|
2015-06-30 14:05:08 -04:00
|
|
|
return err
|
2015-02-20 07:29:25 -05:00
|
|
|
}
|
2015-07-16 13:32:51 -04:00
|
|
|
if err := installStd(darwinArm64Env); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO(crawshaw): darwin/386 for the iOS simulator?
|
|
|
|
if err := installStd(darwinAmd64Env, "-tags=ios"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-06-19 09:32:28 -07:00
|
|
|
}
|
|
|
|
|
2015-07-16 13:32:51 -04:00
|
|
|
func installStd(env []string, args ...string) error {
|
2015-08-20 13:50:18 -04:00
|
|
|
return installPkg("std", env, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func installPkg(pkg string, env []string, args ...string) error {
|
|
|
|
tOS, tArch, pd := getenv(env, "GOOS"), getenv(env, "GOARCH"), pkgdir(env)
|
|
|
|
if tOS != "" && tArch != "" {
|
|
|
|
if buildV {
|
|
|
|
fmt.Fprintf(os.Stderr, "\n# Installing %s for %s/%s.\n", pkg, tOS, tArch)
|
|
|
|
}
|
|
|
|
args = append(args, "-pkgdir="+pd)
|
|
|
|
} else {
|
|
|
|
if buildV {
|
|
|
|
fmt.Fprintf(os.Stderr, "\n# Installing %s.\n", pkg)
|
|
|
|
}
|
2015-06-30 14:05:08 -04:00
|
|
|
}
|
2015-06-29 14:37:57 -04:00
|
|
|
|
2016-03-27 16:28:52 +02:00
|
|
|
cmd := exec.Command("go", "install")
|
2015-07-16 13:32:51 -04:00
|
|
|
cmd.Args = append(cmd.Args, args...)
|
2015-06-30 14:05:08 -04:00
|
|
|
if buildV {
|
|
|
|
cmd.Args = append(cmd.Args, "-v")
|
|
|
|
}
|
2015-07-16 13:32:51 -04:00
|
|
|
if buildX {
|
|
|
|
cmd.Args = append(cmd.Args, "-x")
|
2015-06-19 09:32:28 -07:00
|
|
|
}
|
2015-07-27 16:33:08 -04:00
|
|
|
if buildWork {
|
|
|
|
cmd.Args = append(cmd.Args, "-work")
|
|
|
|
}
|
2015-08-20 13:50:18 -04:00
|
|
|
cmd.Args = append(cmd.Args, pkg)
|
2015-07-19 21:29:35 -04:00
|
|
|
cmd.Env = append([]string{}, env...)
|
|
|
|
return runCmd(cmd)
|
2015-02-05 21:33:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 08:23:25 -05:00
|
|
|
func mkdir(dir string) error {
|
2015-07-21 15:44:18 -04:00
|
|
|
if buildX || buildN {
|
2015-02-12 08:23:25 -05:00
|
|
|
printcmd("mkdir -p %s", dir)
|
|
|
|
}
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return os.MkdirAll(dir, 0755)
|
|
|
|
}
|
|
|
|
|
|
|
|
func symlink(src, dst string) error {
|
2015-07-21 15:44:18 -04:00
|
|
|
if buildX || buildN {
|
2015-02-12 08:23:25 -05:00
|
|
|
printcmd("ln -s %s %s", src, dst)
|
|
|
|
}
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-01 11:09:15 -05:00
|
|
|
if goos == "windows" {
|
|
|
|
return doCopyAll(dst, src)
|
|
|
|
}
|
2015-02-12 08:23:25 -05:00
|
|
|
return os.Symlink(src, dst)
|
|
|
|
}
|
|
|
|
|
2015-02-24 22:52:42 -05:00
|
|
|
func rm(name string) error {
|
2015-07-21 15:44:18 -04:00
|
|
|
if buildX || buildN {
|
2015-02-24 22:52:42 -05:00
|
|
|
printcmd("rm %s", name)
|
|
|
|
}
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return os.Remove(name)
|
|
|
|
}
|
|
|
|
|
2015-03-01 11:09:15 -05:00
|
|
|
func doCopyAll(dst, src string) error {
|
2015-02-16 13:47:49 -05:00
|
|
|
return filepath.Walk(src, func(path string, info os.FileInfo, errin error) (err error) {
|
|
|
|
if errin != nil {
|
|
|
|
return errin
|
|
|
|
}
|
|
|
|
prefixLen := len(src)
|
|
|
|
if len(path) > prefixLen {
|
|
|
|
prefixLen++ // file separator
|
|
|
|
}
|
|
|
|
outpath := filepath.Join(dst, path[prefixLen:])
|
|
|
|
if info.IsDir() {
|
|
|
|
return os.Mkdir(outpath, 0755)
|
|
|
|
}
|
|
|
|
in, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
out, err := os.OpenFile(outpath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, info.Mode())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if errc := out.Close(); err == nil {
|
|
|
|
err = errc
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
_, err = io.Copy(out, in)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-12 08:23:25 -05:00
|
|
|
func removeAll(path string) error {
|
2015-07-21 15:44:18 -04:00
|
|
|
if buildX || buildN {
|
2015-04-20 17:11:20 -04:00
|
|
|
printcmd(`rm -r -f "%s"`, path)
|
2015-02-12 08:23:25 -05:00
|
|
|
}
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-27 16:33:08 -04:00
|
|
|
|
2015-03-01 11:09:15 -05:00
|
|
|
// os.RemoveAll behaves differently in windows.
|
|
|
|
// http://golang.org/issues/9606
|
|
|
|
if goos == "windows" {
|
|
|
|
resetReadOnlyFlagAll(path)
|
|
|
|
}
|
|
|
|
|
2015-02-12 08:23:25 -05:00
|
|
|
return os.RemoveAll(path)
|
|
|
|
}
|
|
|
|
|
2015-03-01 11:09:15 -05:00
|
|
|
func resetReadOnlyFlagAll(path string) error {
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return os.Chmod(path, 0666)
|
|
|
|
}
|
|
|
|
fd, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
names, _ := fd.Readdirnames(-1)
|
|
|
|
for _, name := range names {
|
|
|
|
resetReadOnlyFlagAll(path + string(filepath.Separator) + name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-05 21:33:48 +00:00
|
|
|
func goEnv(name string) string {
|
|
|
|
if val := os.Getenv(name); val != "" {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
val, err := exec.Command("go", "env", name).Output()
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // the Go tool was tested to work earlier
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(val))
|
|
|
|
}
|
2015-07-19 21:29:35 -04:00
|
|
|
|
|
|
|
func runCmd(cmd *exec.Cmd) error {
|
2015-07-21 15:44:18 -04:00
|
|
|
if buildX || buildN {
|
2015-07-19 21:29:35 -04:00
|
|
|
dir := ""
|
|
|
|
if cmd.Dir != "" {
|
|
|
|
dir = "PWD=" + cmd.Dir + " "
|
|
|
|
}
|
|
|
|
env := strings.Join(cmd.Env, " ")
|
|
|
|
if env != "" {
|
|
|
|
env += " "
|
|
|
|
}
|
|
|
|
printcmd("%s%s%s", dir, env, strings.Join(cmd.Args, " "))
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
if buildV {
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
} else {
|
|
|
|
cmd.Stdout = buf
|
|
|
|
cmd.Stderr = buf
|
|
|
|
}
|
|
|
|
|
2015-07-27 16:33:08 -04:00
|
|
|
if buildWork {
|
|
|
|
if goos == "windows" {
|
|
|
|
cmd.Env = append(cmd.Env, `TEMP=`+tmpdir)
|
|
|
|
cmd.Env = append(cmd.Env, `TMP=`+tmpdir)
|
|
|
|
} else {
|
|
|
|
cmd.Env = append(cmd.Env, `TMPDIR=`+tmpdir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-19 21:29:35 -04:00
|
|
|
if !buildN {
|
|
|
|
cmd.Env = environ(cmd.Env)
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return fmt.Errorf("%s failed: %v%s", strings.Join(cmd.Args, " "), err, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|