//
// When compiling packages, build ignores files that end in '_test.go'.
//
-// The -o flag, only allowed when compiling a single package,
-// forces build to write the resulting executable or object
-// to the named output file, instead of the default behavior described
-// in the last two paragraphs.
+// The -o flag forces build to write the resulting executable or object
+// to the named output file or directory, instead of the default behavior described
+// in the last two paragraphs. If the named output is a directory that exists,
+// then any resulting executables will be written to that directory.
//
// The -i flag installs the packages that are dependencies of the target.
//
When compiling packages, build ignores files that end in '_test.go'.
-The -o flag, only allowed when compiling a single package,
-forces build to write the resulting executable or object
-to the named output file, instead of the default behavior described
-in the last two paragraphs.
+The -o flag forces build to write the resulting executable or object
+to the named output file or directory, instead of the default behavior described
+in the last two paragraphs. If the named output is a directory that exists,
+then any resulting executables will be written to that directory.
The -i flag installs the packages that are dependencies of the target.
CmdInstall.Run = runInstall
CmdBuild.Flag.BoolVar(&cfg.BuildI, "i", false, "")
- CmdBuild.Flag.StringVar(&cfg.BuildO, "o", "", "output file")
+ CmdBuild.Flag.StringVar(&cfg.BuildO, "o", "", "output file or directory")
CmdInstall.Flag.BoolVar(&cfg.BuildI, "i", false, "")
}
if cfg.BuildO != "" {
+ // If the -o name exists and is a directory, then
+ // write all main packages to that directory.
+ // Otherwise require only a single package be built.
+ if bs, err := os.Stat(cfg.BuildO); err == nil && bs.IsDir() {
+ a := &Action{Mode: "go build"}
+ for _, p := range pkgs {
+ if p.Name != "main" {
+ continue
+ }
+ p.Target = filepath.Join(cfg.BuildO, load.DefaultExecName(p))
+ p.Target += cfg.ExeSuffix
+ p.Stale = true
+ p.StaleReason = "build -o flag in use"
+ a.Deps = append(a.Deps, b.AutoAction(ModeInstall, depMode, p))
+ }
+ if len(a.Deps) == 0 {
+ base.Fatalf("go build: no main packages to build")
+ }
+ b.Do(a)
+ return
+ }
if len(pkgs) > 1 {
- base.Fatalf("go build: cannot use -o with multiple packages")
+ base.Fatalf("go build: cannot write multiple packages to non-directory %s", cfg.BuildO)
} else if len(pkgs) == 0 {
base.Fatalf("no packages to build")
}