summaryrefslogtreecommitdiff
path: root/src/cmd/go/run.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/go/run.go')
-rw-r--r--src/cmd/go/run.go39
1 files changed, 34 insertions, 5 deletions
diff --git a/src/cmd/go/run.go b/src/cmd/go/run.go
index e6dadd229..ef8aa95a3 100644
--- a/src/cmd/go/run.go
+++ b/src/cmd/go/run.go
@@ -8,16 +8,43 @@ import (
"fmt"
"os"
"os/exec"
+ "runtime"
"strings"
)
+var execCmd []string // -exec flag, for run and test
+
+func findExecCmd() []string {
+ if execCmd != nil {
+ return execCmd
+ }
+ execCmd = []string{} // avoid work the second time
+ if goos == runtime.GOOS && goarch == runtime.GOARCH {
+ return execCmd
+ }
+ path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch))
+ if err == nil {
+ execCmd = []string{path}
+ }
+ return execCmd
+}
+
var cmdRun = &Command{
- UsageLine: "run [build flags] gofiles... [arguments...]",
+ UsageLine: "run [build flags] [-exec xprog] gofiles... [arguments...]",
Short: "compile and run Go program",
Long: `
Run compiles and runs the main package comprising the named Go source files.
A Go source file is defined to be a file ending in a literal ".go" suffix.
+By default, 'go run' runs the compiled binary directly: 'a.out arguments...'.
+If the -exec flag is given, 'go run' invokes the binary using xprog: 'xprog a.out arguments...'.
+If the -exec flag is not given, GOOS or GOARCH is different from the system
+default, and a program named go_$GOOS_$GOARCH_exec can be found
+on the current search path, 'go run' invokes the binary using that program,
+for example 'go_nacl_386_exec a.out arguments...'. This allows execution of
+cross-compiled programs when a simulator or other execution method is
+available.
+
For more about build flags, see 'go help build'.
See also: go build.
@@ -28,6 +55,7 @@ func init() {
cmdRun.Run = runRun // break init loop
addBuildFlags(cmdRun)
+ cmdRun.Flag.Var((*stringsFlag)(&execCmd), "exec", "")
}
func printStderr(args ...interface{}) (int, error) {
@@ -58,6 +86,7 @@ func runRun(cmd *Command, args []string) {
if p.Error != nil {
fatalf("%s", p.Error)
}
+ p.omitDWARF = true
for _, err := range p.DepsErrors {
errorf("%s", err)
}
@@ -89,20 +118,20 @@ func runRun(cmd *Command, args []string) {
// runProgram is the action for running a binary that has already
// been compiled. We ignore exit status.
func (b *builder) runProgram(a *action) error {
+ cmdline := stringList(findExecCmd(), a.deps[0].target, a.args)
if buildN || buildX {
- b.showcmd("", "%s %s", a.deps[0].target, strings.Join(a.args, " "))
+ b.showcmd("", "%s", strings.Join(cmdline, " "))
if buildN {
return nil
}
}
- runStdin(a.deps[0].target, a.args)
+ runStdin(cmdline)
return nil
}
// runStdin is like run, but connects Stdin.
-func runStdin(cmdargs ...interface{}) {
- cmdline := stringList(cmdargs...)
+func runStdin(cmdline []string) {
cmd := exec.Command(cmdline[0], cmdline[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout