2
0
mirror of synced 2025-02-23 23:08:14 +00:00
David Crawshaw e5193c59b3 cmd/gomobile: reorganize build logic
The goal here is to remove several inconsistencies between
-target=android and -target=ios support, along with making the flow
of the command follow the path you might expect given a certain set
of flags, and preparing for `gomobile bind` support of ios. In
particular, building non-main packages now works with both targets
and the initialization of global build state is clearer.

The reorg also is designed around an nm trick I thought of
yesterday to do better package import scanning without a slow
all-file scan. This will give better detection of x/mobile/app and
x/mobile/exp/audio/al packages. There's a TODO about it, and I'll do
it in a future CL.

Tested with:

	go test golang.org/x/mobile/cmd/gomobile
	gomobile init
	gomobile bind golang.org/x/mobile/asset
	go test golang.org/x/mobile/bind/java
	gomobile build -target=ios golang.org/x/mobile/example/basic
	gomobile build -target=ios golang.org/x/mobile/gl
	gomobile build -target=android golang.org/x/mobile/gl
	gomobile build -target=android golang.org/x/mobile/example/basic
	(Along with manual testing of basic on an android device.)

That might make a pretty good _test.go.

Change-Id: I41230008c3c15db25a11c33b9eaca4abada9f411
Reviewed-on: https://go-review.googlesource.com/12051
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-07-13 18:54:16 +00:00

106 lines
2.5 KiB
Go

package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
// General mobile build environment. Initialized by envInit.
var (
cwd string
gomobilepath string // $GOPATH/pkg/gomobile
ndkccpath string // $GOPATH/pkg/gomobile/android-{{.NDK}}
darwinArmEnv []string
darwinArm64Env []string
androidArmEnv []string
)
func envInit() (cleanup func(), err error) {
cwd, err = os.Getwd()
if err != nil {
return nil, err
}
// Find gomobilepath.
gopath := goEnv("GOPATH")
for _, p := range filepath.SplitList(gopath) {
gomobilepath = filepath.Join(p, "pkg", "gomobile")
if _, err := os.Stat(gomobilepath); err == nil {
break
}
}
if buildX {
fmt.Fprintln(xout, "GOMOBILE="+gomobilepath)
}
// Check the toolchain is in a good state.
version, err := goVersion()
if err != nil {
return nil, err
}
if gomobilepath == "" {
return nil, errors.New("toolchain not installed, run `gomobile init`")
}
verpath := filepath.Join(gomobilepath, "version")
installedVersion, err := ioutil.ReadFile(verpath)
if err != nil {
return nil, errors.New("toolchain partially installed, run `gomobile init`")
}
if !bytes.Equal(installedVersion, version) {
return nil, errors.New("toolchain out of date, run `gomobile init`")
}
// Setup the cross-compiler environments.
// TODO(crawshaw): Remove ndkccpath global.
ndkccpath = filepath.Join(gomobilepath, "android-"+ndkVersion)
ndkccbin := filepath.Join(ndkccpath, "arm", "bin")
androidEnv := []string{
"CC=" + filepath.Join(ndkccbin, "arm-linux-androideabi-gcc"),
"CXX=" + filepath.Join(ndkccbin, "arm-linux-androideabi-g++"),
`GOGCCFLAGS="-fPIC -marm -pthread -fmessage-length=0"`,
}
androidArmEnv = append([]string{
"GOOS=android",
"GOARCH=arm",
"GOARM=7",
}, androidEnv...)
// TODO(jbd): Remove clangwrap.sh dependency by implementing clangwrap.sh
// in Go in this package.
goroot := goEnv("GOROOT")
iosEnv := []string{
"CC=" + filepath.Join(goroot, "misc/ios/clangwrap.sh"),
"CCX=" + filepath.Join(goroot, "misc/ios/clangwrap.sh"),
}
darwinArmEnv = append([]string{
"GOOS=darwin",
"GOARCH=arm",
}, iosEnv...)
darwinArm64Env = append([]string{
"GOOS=darwin",
"GOARCH=arm64",
}, iosEnv...)
// We need a temporary directory when assembling an apk/app.
if buildN {
tmpdir = "$WORK"
} else {
tmpdir, err = ioutil.TempDir("", "gomobile-work-")
if err != nil {
return nil, err
}
}
if buildX {
fmt.Fprintln(xout, "WORK="+tmpdir)
}
return func() { removeAll(tmpdir) }, nil
}