2
0
mirror of synced 2025-02-22 22:38:18 +00:00

cmd/gomobile: use standard env to run Go tool

Now that we no longer modify the user's $GOROOT and run make.bash,
the need for isolating the environment is gone. Pass through
whatever users want, overriding only those necesssary for corss
compilation.

While here, remove some dead code.

Fixes golang/go#11672.

Change-Id: Iaf867913eaa1311519a4d5a7a8169228ebf21346
Reviewed-on: https://go-review.googlesource.com/12128
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
David Crawshaw 2015-07-13 21:02:58 -04:00
parent 83f38234e7
commit c8fe70554f
5 changed files with 54 additions and 143 deletions

View File

@ -196,41 +196,6 @@ func init() {
addBuildFlagsNVX(cmdBind)
}
// environ merges os.Environ and the given "key=value" pairs.
func environ(kv []string) []string {
envs := map[string]string{}
cur := os.Environ()
new := make([]string, 0, len(cur)+len(kv))
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
}
func goBuild(src string, env []string, args ...string) error {
cmd := exec.Command(
`go`,
@ -248,12 +213,8 @@ func goBuild(src string, env []string, args ...string) error {
}
cmd.Args = append(cmd.Args, args...)
cmd.Args = append(cmd.Args, src)
cmd.Env = []string{"CGO_ENABLED=1"}
cmd.Env = append(cmd.Env, env...)
cmd.Env = append(cmd.Env,
`CGO_ENABLED=1`,
`GOROOT=`+goEnv("GOROOT"),
`GOPATH=`+goEnv("GOPATH"),
)
buf := new(bytes.Buffer)
buf.WriteByte('\n')
if buildV {

View File

@ -48,6 +48,6 @@ func TestBuild(t *testing.T) {
var buildTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
WORK=$WORK
GOOS=android GOARCH=arm GOARM=7 CC=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-gcc{{.EXE}} CXX=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-g++{{.EXE}} GOGCCFLAGS="-fPIC -marm -pthread -fmessage-length=0" CGO_ENABLED=1 GOROOT=$GOROOT GOPATH=$GOPATH go build -tags="" -x -buildmode=c-shared -o $WORK/libbasic.so golang.org/x/mobile/example/basic
CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 CC=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-gcc{{.EXE}} CXX=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-g++{{.EXE}} GOGCCFLAGS="-fPIC -marm -pthread -fmessage-length=0" go build -tags="" -x -buildmode=c-shared -o $WORK/libbasic.so golang.org/x/mobile/example/basic
rm -r -f "$WORK"
`))

View File

@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// General mobile build environment. Initialized by envInit.
@ -103,3 +104,39 @@ func envInit() (cleanup func(), err error) {
return func() { removeAll(tmpdir) }, nil
}
// 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
}

View File

@ -190,7 +190,6 @@ func installStd(tOS, tArch string, env []string) error {
if tArch == "arm" {
cmd.Env = append(cmd.Env, "GOARM=7")
}
cmd.Env = appendCommonEnv(cmd.Env)
if buildX {
printcmd("%s", strings.Join(cmd.Env, " ")+" "+strings.Join(cmd.Args, " "))
}
@ -206,6 +205,7 @@ func installStd(tOS, tArch string, env []string) error {
}
if !buildN {
cmd.Env = environ(cmd.Env)
if err := cmd.Run(); err != nil {
return fmt.Errorf("go install std for %s/%s failed: %v%s", tOS, tArch, err, buf)
}
@ -213,20 +213,6 @@ func installStd(tOS, tArch string, env []string) error {
return nil
}
func appendCommonEnv(env []string) []string {
if goos == "windows" {
env = append(env, `TEMP=`+tmpdir)
env = append(env, `TMP=`+tmpdir)
env = append(env, `HOMEDRIVE=`+os.Getenv("HOMEDRIVE"))
env = append(env, `HOMEPATH=`+os.Getenv("HOMEPATH"))
} else {
env = append(env, `TMPDIR=`+tmpdir)
// for default the go1.4 bootstrap
env = append(env, `HOME=`+os.Getenv("HOME"))
}
return env
}
func removeGomobilepkg() {
dir, err := os.Open(gomobilepath)
if err != nil {
@ -315,45 +301,6 @@ func goVersion() ([]byte, error) {
return exec.Command(gobin, "version").CombinedOutput()
}
// checkVersionMatch makes sure that the go command in the path matches
// the GOROOT that will be used for building the cross compiler.
//
// This is typically not a problem when using the a release version, but
// it is easy for development environments to drift, causing unexpected
// errors.
//
// checkVersionMatch is run after the tmpGoroot is built, so the dist
// command is available to call.
func checkVersionMatch(tmpGoroot string, version []byte) error {
if buildN {
return nil
}
version = bytes.TrimPrefix(version, []byte("go version "))
version = bytes.Trim(version, "\n")
dist := filepath.Join(tmpGoroot, "pkg/tool/"+goEnv("GOOS")+"_"+goEnv("GOARCH")+"/dist")
if goos == "windows" {
dist += ".exe"
}
cmd := exec.Command(dist, "version")
cmd.Dir = tmpGoroot
cmd.Env = []string{
"GOROOT=" + tmpGoroot,
`PATH=` + os.Getenv("PATH"),
}
cmd.Env = appendCommonEnv(cmd.Env)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("cannot get cmd/dist version: %v (%s)", err, out)
}
out = bytes.Trim(out, "\n")
if !bytes.HasPrefix(version, out) {
return fmt.Errorf("Go command out of sync with GOROOT. The command `go version` reports:\n\t%s\nbut the GOROOT %q is version:\n\t%s\nRebuild Go.", version, goEnv("GOROOT"), out)
}
return nil
}
func fetchOpenAL() error {
url := "https://dl.google.com/go/mobile/gomobile-" + openALVersion + ".tar.gz"
archive, err := fetch(url)
@ -584,32 +531,6 @@ func fetch(url string) (dst string, err error) {
return dst, nil
}
// copyGoroot copies GOROOT from src to dst.
//
// It skips the pkg directory, which is not necessary for make.bash,
// and symlinks .git to avoid a 70MB copy.
func copyGoroot(dst, src string) error {
if err := mkdir(filepath.Join(dst, "pkg")); err != nil {
return err
}
for _, dir := range []string{"lib", "src", "misc"} {
if err := copyAll(filepath.Join(dst, dir), filepath.Join(src, dir)); err != nil {
return err
}
}
return symlink(filepath.Join(src, ".git"), filepath.Join(dst, ".git"))
}
func copyAll(dst, src string) error {
if buildX {
printcmd("cp -a %s %s", src, dst)
}
if buildN {
return nil
}
return doCopyAll(dst, src)
}
func doCopyAll(dst, src string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, errin error) (err error) {
if errin != nil {

View File

@ -57,16 +57,14 @@ func diffOutput(got string, wantTmpl *template.Template) (string, error) {
wantBuf := new(bytes.Buffer)
data := outputData{
NDK: ndkVersion,
GOOS: goos,
GOARCH: goarch,
GOPATH: gopath,
NDKARCH: ndkarch,
BuildScript: unixBuildScript,
NDK: ndkVersion,
GOOS: goos,
GOARCH: goarch,
GOPATH: gopath,
NDKARCH: ndkarch,
}
if goos == "windows" {
data.EXE = ".exe"
data.BuildScript = windowsBuildScript
}
if err := wantTmpl.Execute(wantBuf, data); err != nil {
return "", err
@ -79,20 +77,14 @@ func diffOutput(got string, wantTmpl *template.Template) (string, error) {
}
type outputData struct {
NDK string
GOOS string
GOARCH string
GOPATH string
NDKARCH string
EXE string // .extension for executables. (ex. ".exe" for windows)
BuildScript string
NDK string
GOOS string
GOARCH string
GOPATH string
NDKARCH string
EXE string // .extension for executables. (ex. ".exe" for windows)
}
const (
unixBuildScript = `TMPDIR=$WORK HOME=$HOME go install std`
windowsBuildScript = `TEMP=$WORK TMP=$WORK HOMEDRIVE=C: HOMEPATH=$HOMEPATH go install std`
)
var initTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
mkdir -p $GOMOBILE/android-{{.NDK}}
WORK=/GOPATH1/pkg/gomobile/work
@ -117,11 +109,11 @@ mv $WORK/openal/include/AL $GOMOBILE/android-{{.NDK}}/arm/sysroot/usr/include/AL
mkdir -p $GOMOBILE/android-{{.NDK}}/openal
mv $WORK/openal/lib $GOMOBILE/android-{{.NDK}}/openal/lib
rm -r -f "$GOROOT/pkg/android_arm"
PATH=$PATH GOOS=android GOARCH=arm CGO_ENABLED=1 CC=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-gcc{{.EXE}} CXX=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-g++{{.EXE}} GOARM=7 {{.BuildScript}}
PATH=$PATH GOOS=android GOARCH=arm CGO_ENABLED=1 CC=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-gcc{{.EXE}} CXX=$GOMOBILE/android-{{.NDK}}/arm/bin/arm-linux-androideabi-g++{{.EXE}} GOARM=7 go install std
{{if eq .GOOS "darwin"}}rm -r -f "$GOROOT/pkg/darwin_arm"
PATH=$PATH GOOS=darwin GOARCH=arm CGO_ENABLED=1 CC=$GOROOT/misc/ios/clangwrap.sh CXX=$GOROOT/misc/ios/clangwrap.sh GOARM=7 {{.BuildScript}}
PATH=$PATH GOOS=darwin GOARCH=arm CGO_ENABLED=1 CC=$GOROOT/misc/ios/clangwrap.sh CXX=$GOROOT/misc/ios/clangwrap.sh GOARM=7 go install std
rm -r -f "$GOROOT/pkg/darwin_arm64"
PATH=$PATH GOOS=darwin GOARCH=arm64 CGO_ENABLED=1 CC=$GOROOT/misc/ios/clangwrap.sh CXX=$GOROOT/misc/ios/clangwrap.sh {{.BuildScript}}
PATH=$PATH GOOS=darwin GOARCH=arm64 CGO_ENABLED=1 CC=$GOROOT/misc/ios/clangwrap.sh CXX=$GOROOT/misc/ios/clangwrap.sh go install std
{{end}}go version > $GOMOBILE/version
rm -r -f "$WORK"
`))