cmd/gomobile: add -work flag.

In order to make the artifacts of go build command preserved under WORK
directory, this change modifies TMPDIR (TEMP/TMP for windows)
environment variables to point to gomobile's tmpdir if -work flag is set.

> gomobile init -work
WORK=/gopath/pkg/gomobile/work-276689736

> ls /gopath/pkg/gomobile/work-276689736
README			go-build823903592	openal
android-ndk-r10e	go-build858075903
go-build365743399	go-build921886344

> gomobile build -work golang.org/x/mobile/example/basic
WORK=/tmp/gomobile-work-863381843

> ls /tmp/gomobile-work-863381843
go-build102034516	libbasic.so

> gomobile bind -work github.com/hyangah/ivy
WORK=/tmp/gomobile-work-355100962

> ls /tmp/gomobile-work-355100962
android			go-build284034365	javac-output
androidlib		go_ivy

Change-Id: I2f467e0063bc1c8b8c636a8cd6d100e86a99a91a
Reviewed-on: https://go-review.googlesource.com/12720
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Hyang-Ah (Hana) Kim 2015-07-27 16:33:08 -04:00 committed by Hyang-Ah Hana Kim
parent 405d6233b3
commit af3e09ede0
6 changed files with 43 additions and 13 deletions

View File

@ -56,8 +56,8 @@ installed. Support is not complete.
The -v flag provides verbose output, including the list of packages built.
The build flags -a, -i, -n, -x, -gcflags, -ldflags, and -tags are shared
with the build command. For documentation, see 'go help build'.
The build flags -a, -i, -n, -x, -gcflags, -ldflags, -tags, and -work
are shared with the build command. For documentation, see 'go help build'.
`,
}

View File

@ -265,7 +265,6 @@ func buildJar(w io.Writer, srcDir string) error {
return err
}
}
defer removeAll(dst)
apiPath, err := androidAPIPath()
if err != nil {

View File

@ -51,8 +51,8 @@ output file name depends on the package built.
The -v flag provides verbose output, including the list of packages built.
The build flags -a, -i, -n, -x, -gcflags, -ldflags, and -tags are shared
with the build command. For documentation, see 'go help build'.
The build flags -a, -i, -n, -x, -gcflags, -ldflags, -tags, and -work are
shared with the build command. For documentation, see 'go help build'.
`,
}
@ -208,6 +208,7 @@ var (
buildGcflags string // -gcflags
buildLdflags string // -ldflags
buildTarget string // -target
buildWork bool // -work
)
func addBuildFlags(cmd *command) {
@ -221,10 +222,11 @@ func addBuildFlags(cmd *command) {
cmd.flag.Var((*stringsFlag)(&ctx.BuildTags), "tags", "")
}
func addBuildFlagsNVX(cmd *command) {
func addBuildFlagsNVXWork(cmd *command) {
cmd.flag.BoolVar(&buildN, "n", false, "")
cmd.flag.BoolVar(&buildV, "v", false, "")
cmd.flag.BoolVar(&buildX, "x", false, "")
cmd.flag.BoolVar(&buildWork, "work", false, "")
}
type binInfo struct {
@ -234,15 +236,15 @@ type binInfo struct {
func init() {
addBuildFlags(cmdBuild)
addBuildFlagsNVX(cmdBuild)
addBuildFlagsNVXWork(cmdBuild)
addBuildFlags(cmdInstall)
addBuildFlagsNVX(cmdInstall)
addBuildFlagsNVXWork(cmdInstall)
addBuildFlagsNVX(cmdInit)
addBuildFlagsNVXWork(cmdInit)
addBuildFlags(cmdBind)
addBuildFlagsNVX(cmdBind)
addBuildFlagsNVXWork(cmdBind)
}
func goBuild(src string, env []string, args ...string) error {
@ -270,6 +272,9 @@ func goBuild(src string, env []string, args ...string) error {
if buildLdflags != "" {
cmd.Args = append(cmd.Args, "-ldflags", buildLdflags)
}
if buildWork {
cmd.Args = append(cmd.Args, "-work")
}
cmd.Args = append(cmd.Args, args...)
cmd.Args = append(cmd.Args, src)
cmd.Env = append([]string{}, env...)

View File

@ -56,7 +56,13 @@ func buildEnvInit() (cleanup func(), err error) {
return nil, errors.New("toolchain not installed, run `gomobile init`")
}
cleanupFn := func() { removeAll(tmpdir) }
cleanupFn := func() {
if buildWork {
fmt.Printf("WORK=%s\n", tmpdir)
return
}
removeAll(tmpdir)
}
if buildN {
tmpdir = "$WORK"
cleanupFn = func() {}

View File

@ -107,7 +107,13 @@ func runInit(cmd *command) error {
if buildX || buildN {
fmt.Fprintln(xout, "WORK="+tmpdir)
}
defer removeAll(tmpdir)
defer func() {
if buildWork {
fmt.Printf("WORK=%s\n", tmpdir)
return
}
removeAll(tmpdir)
}()
if err := fetchNDK(); err != nil {
return err
@ -178,6 +184,9 @@ func installStd(env []string, args ...string) error {
if buildX {
cmd.Args = append(cmd.Args, "-x")
}
if buildWork {
cmd.Args = append(cmd.Args, "-work")
}
cmd.Args = append(cmd.Args, "std")
cmd.Env = append([]string{}, env...)
return runCmd(cmd)
@ -536,6 +545,7 @@ func removeAll(path string) error {
if buildN {
return nil
}
// os.RemoveAll behaves differently in windows.
// http://golang.org/issues/9606
if goos == "windows" {
@ -600,6 +610,15 @@ func runCmd(cmd *exec.Cmd) error {
cmd.Stderr = buf
}
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)
}
}
if !buildN {
cmd.Env = environ(cmd.Env)
if err := cmd.Run(); err != nil {

View File

@ -21,7 +21,8 @@ attached mobile device.
Only -target android is supported. The 'adb' tool must be on the PATH.
The build flags -a, -i, -n, -x, and -tags are shared with the build command.
The build flags -a, -i, -n, -x, -gcflags, -ldflags, -tags, and -work are
shared with the build command.
For documentation, see 'go help build'.
`,
}