Commit 178307c3 authored by Russ Cox's avatar Russ Cox

cmd/go: address review comments

Address review comments from earlier CLs.
These are changes I was too scared to try to push
down into the original CLs (thanks, Git).

Change-Id: I0e428fad73d71bd2a7d08178cf2e856de3cef19f
Reviewed-on: https://go-review.googlesource.com/36257Reviewed-by: default avatarDavid Crawshaw <crawshaw@golang.org>
parent 707cadd7
This diff is collapsed.
...@@ -8,8 +8,6 @@ package base ...@@ -8,8 +8,6 @@ package base
import ( import (
"bytes" "bytes"
"cmd/go/internal/cfg"
"cmd/go/internal/str"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
...@@ -19,6 +17,9 @@ import ( ...@@ -19,6 +17,9 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"sync" "sync"
"cmd/go/internal/cfg"
"cmd/go/internal/str"
) )
// A Command is an implementation of a go command // A Command is an implementation of a go command
......
...@@ -5,8 +5,10 @@ ...@@ -5,8 +5,10 @@
package base package base
import ( import (
"cmd/go/internal/str"
"flag" "flag"
"cmd/go/internal/cfg"
"cmd/go/internal/str"
) )
// A StringsFlag is a command-line flag that interprets its argument // A StringsFlag is a command-line flag that interprets its argument
...@@ -28,6 +30,6 @@ func (v *StringsFlag) String() string { ...@@ -28,6 +30,6 @@ func (v *StringsFlag) String() string {
// AddBuildFlagsNX adds the -n and -x build flags to the flag set. // AddBuildFlagsNX adds the -n and -x build flags to the flag set.
func AddBuildFlagsNX(flags *flag.FlagSet) { func AddBuildFlagsNX(flags *flag.FlagSet) {
flags.BoolVar(&BuildN, "n", false, "") flags.BoolVar(&cfg.BuildN, "n", false, "")
flags.BoolVar(&BuildX, "x", false, "") flags.BoolVar(&cfg.BuildX, "x", false, "")
} }
...@@ -10,7 +10,15 @@ import ( ...@@ -10,7 +10,15 @@ import (
"strings" "strings"
) )
var Cwd, _ = os.Getwd() func getwd() string {
wd, err := os.Getwd()
if err != nil {
Fatalf("cannot determine current directory: %v", err)
}
return wd
}
var Cwd = getwd()
// ShortPath returns an absolute or relative name for path, whatever is shorter. // ShortPath returns an absolute or relative name for path, whatever is shorter.
func ShortPath(path string) string { func ShortPath(path string) string {
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
package cfg package cfg
import ( import (
"flag"
"go/build" "go/build"
"os" "os"
"path/filepath" "path/filepath"
...@@ -30,14 +29,18 @@ var ( ...@@ -30,14 +29,18 @@ var (
BuildRace bool // -race flag BuildRace bool // -race flag
BuildToolexec []string // -toolexec flag BuildToolexec []string // -toolexec flag
BuildToolchainName string BuildToolchainName string
BuildToolchainCompiler string BuildToolchainCompiler func() string
BuildToolchainLinker string BuildToolchainLinker func() string
BuildV bool // -v flag BuildV bool // -v flag
BuildWork bool // -work flag BuildWork bool // -work flag
BuildX bool // -x flag BuildX bool // -x flag
) )
func init() {
BuildToolchainCompiler = func() string { return "missing-compiler" }
BuildToolchainLinker = func() string { return "missing-linker" }
}
// The test coverage mode affects package loading. Sigh. // The test coverage mode affects package loading. Sigh.
var TestCoverMode string // -covermode flag var TestCoverMode string // -covermode flag
...@@ -50,8 +53,10 @@ type EnvVar struct { ...@@ -50,8 +53,10 @@ type EnvVar struct {
// OrigEnv is the original environment of the program at startup. // OrigEnv is the original environment of the program at startup.
var OrigEnv []string var OrigEnv []string
// NewEnv is the new environment for running commands. // CmdEnv is the new environment for running go tool commands.
var NewEnv []EnvVar // User binaries (during go test or go run) are run with OrigEnv,
// not CmdEnv.
var CmdEnv []EnvVar
// Global build parameters (used during package load) // Global build parameters (used during package load)
var ( var (
...@@ -61,12 +66,6 @@ var ( ...@@ -61,12 +66,6 @@ var (
Gopath []string Gopath []string
) )
// AddBuildFlagsNX adds the -n and -x build flags to the flag set.
func AddBuildFlagsNX(flags *flag.FlagSet) {
flags.BoolVar(&BuildN, "n", false, "")
flags.BoolVar(&BuildX, "x", false, "")
}
var ( var (
GOROOT = filepath.Clean(runtime.GOROOT()) GOROOT = filepath.Clean(runtime.GOROOT())
GOBIN = os.Getenv("GOBIN") GOBIN = os.Getenv("GOBIN")
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package clean implements the ``go clean'' command.
package clean package clean
import ( import (
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package doc implements the ``go doc'' command.
package doc package doc
import ( import (
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package env // Package envcmd implements the ``go env'' command.
package envcmd
import ( import (
"fmt" "fmt"
...@@ -103,7 +104,7 @@ func ExtraEnvVars() []cfg.EnvVar { ...@@ -103,7 +104,7 @@ func ExtraEnvVars() []cfg.EnvVar {
} }
func runEnv(cmd *base.Command, args []string) { func runEnv(cmd *base.Command, args []string) {
env := cfg.NewEnv env := cfg.CmdEnv
env = append(env, ExtraEnvVars()...) env = append(env, ExtraEnvVars()...)
if len(args) > 0 { if len(args) > 0 {
for _, name := range args { for _, name := range args {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package fix implements the ``go fix'' command.
package fix package fix
import ( import (
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package fmt // Package fmtcmd implements the ``go fmt'' command.
package fmtcmd
import ( import (
"os" "os"
...@@ -15,7 +16,7 @@ import ( ...@@ -15,7 +16,7 @@ import (
) )
func init() { func init() {
cfg.AddBuildFlagsNX(&CmdFmt.Flag) base.AddBuildFlagsNX(&CmdFmt.Flag)
} }
var CmdFmt = &base.Command{ var CmdFmt = &base.Command{
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package generate implements the ``go generate'' command.
package generate package generate
import ( import (
......
...@@ -2,17 +2,17 @@ ...@@ -2,17 +2,17 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package help implements "go help". // Package help implements the ``go help'' command.
package help package help
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt" "fmt"
"html/template"
"io" "io"
"os" "os"
"strings" "strings"
"text/template"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
......
...@@ -2,19 +2,21 @@ ...@@ -2,19 +2,21 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package list implements the ``go list'' command.
package list package list
import ( import (
"bufio" "bufio"
"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/load"
"cmd/go/internal/work"
"encoding/json" "encoding/json"
"io" "io"
"os" "os"
"strings" "strings"
"text/template" "text/template"
"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/load"
"cmd/go/internal/work"
) )
var CmdList = &base.Command{ var CmdList = &base.Command{
......
...@@ -883,7 +883,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) *Package ...@@ -883,7 +883,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) *Package
p.Internal.Target = filepath.Join(cfg.GOROOTpkg, "tool", full) p.Internal.Target = filepath.Join(cfg.GOROOTpkg, "tool", full)
} }
if p.Internal.Target != "" && cfg.BuildContext.GOOS == "windows" { if p.Internal.Target != "" && cfg.BuildContext.GOOS == "windows" {
p.Internal.Target += ".Internal.Exe" p.Internal.Target += ".exe"
} }
} else if p.Internal.Local { } else if p.Internal.Local {
// Local import turned into absolute path. // Local import turned into absolute path.
...@@ -1562,10 +1562,10 @@ func isStale(p *Package) (bool, string) { ...@@ -1562,10 +1562,10 @@ func isStale(p *Package) (bool, string) {
// Excluding $GOROOT used to also fix issue 4106, but that's now // Excluding $GOROOT used to also fix issue 4106, but that's now
// taken care of above (at least when the installed Go is a released version). // taken care of above (at least when the installed Go is a released version).
if p.Root != cfg.GOROOT { if p.Root != cfg.GOROOT {
if olderThan(cfg.BuildToolchainCompiler) { if olderThan(cfg.BuildToolchainCompiler()) {
return true, "newer compiler" return true, "newer compiler"
} }
if p.Internal.Build.IsCommand() && olderThan(cfg.BuildToolchainLinker) { if p.Internal.Build.IsCommand() && olderThan(cfg.BuildToolchainLinker()) {
return true, "newer linker" return true, "newer linker"
} }
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package run implements the ``go run'' command.
package run package run
import ( import (
...@@ -66,7 +67,7 @@ func runRun(cmd *base.Command, args []string) { ...@@ -66,7 +67,7 @@ func runRun(cmd *base.Command, args []string) {
} }
for _, file := range files { for _, file := range files {
if strings.HasSuffix(file, "_test.go") { if strings.HasSuffix(file, "_test.go") {
// goFilesPackage is going to assign this to TestGoFiles. // GoFilesPackage is going to assign this to TestGoFiles.
// Reject since it won't be part of the build. // Reject since it won't be part of the build.
base.Fatalf("go run: cannot run *_test.go files (%s)", file) base.Fatalf("go run: cannot run *_test.go files (%s)", file)
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package tool implements the ``go tool'' command.
package tool package tool
import ( import (
......
...@@ -2,12 +2,14 @@ ...@@ -2,12 +2,14 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package version implements the ``go version'' command.
package version package version
import ( import (
"cmd/go/internal/base"
"fmt" "fmt"
"runtime" "runtime"
"cmd/go/internal/base"
) )
var CmdVersion = &base.Command{ var CmdVersion = &base.Command{
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Package vet implements the ``go vet'' command.
package vet package vet
import ( import (
......
...@@ -180,8 +180,8 @@ func (c buildCompiler) Set(value string) error { ...@@ -180,8 +180,8 @@ func (c buildCompiler) Set(value string) error {
return fmt.Errorf("unknown compiler %q", value) return fmt.Errorf("unknown compiler %q", value)
} }
cfg.BuildToolchainName = value cfg.BuildToolchainName = value
cfg.BuildToolchainCompiler = BuildToolchain.compiler() cfg.BuildToolchainCompiler = BuildToolchain.compiler
cfg.BuildToolchainLinker = BuildToolchain.linker() cfg.BuildToolchainLinker = BuildToolchain.linker
cfg.BuildContext.Compiler = value cfg.BuildContext.Compiler = value
return nil return nil
} }
...@@ -1317,9 +1317,11 @@ func (b *Builder) build(a *Action) (err error) { ...@@ -1317,9 +1317,11 @@ func (b *Builder) build(a *Action) (err error) {
sfiles = nil sfiles = nil
} }
cgoExe := base.Tool("cgo") var cgoExe string
if a.cgo != nil && a.cgo.Target != "" { if a.cgo != nil && a.cgo.Target != "" {
cgoExe = a.cgo.Target cgoExe = a.cgo.Target
} else {
cgoExe = base.Tool("cgo")
} }
outGo, outObj, err := b.cgo(a, cgoExe, obj, pcCFLAGS, pcLDFLAGS, cgofiles, objdirCgofiles, gccfiles, cxxfiles, a.Package.MFiles, a.Package.FFiles) outGo, outObj, err := b.cgo(a, cgoExe, obj, pcCFLAGS, pcLDFLAGS, cgofiles, objdirCgofiles, gccfiles, cxxfiles, a.Package.MFiles, a.Package.FFiles)
if err != nil { if err != nil {
......
...@@ -20,9 +20,9 @@ import ( ...@@ -20,9 +20,9 @@ import (
"cmd/go/internal/cfg" "cmd/go/internal/cfg"
"cmd/go/internal/clean" "cmd/go/internal/clean"
"cmd/go/internal/doc" "cmd/go/internal/doc"
"cmd/go/internal/env" "cmd/go/internal/envcmd"
"cmd/go/internal/fix" "cmd/go/internal/fix"
fmtcmd "cmd/go/internal/fmt" "cmd/go/internal/fmtcmd"
"cmd/go/internal/generate" "cmd/go/internal/generate"
"cmd/go/internal/get" "cmd/go/internal/get"
"cmd/go/internal/help" "cmd/go/internal/help"
...@@ -40,7 +40,7 @@ func init() { ...@@ -40,7 +40,7 @@ func init() {
work.CmdBuild, work.CmdBuild,
clean.CmdClean, clean.CmdClean,
doc.CmdDoc, doc.CmdDoc,
env.CmdEnv, envcmd.CmdEnv,
bug.CmdBug, bug.CmdBug,
fix.CmdFix, fix.CmdFix,
fmtcmd.CmdFmt, fmtcmd.CmdFmt,
...@@ -114,8 +114,8 @@ func main() { ...@@ -114,8 +114,8 @@ func main() {
// but in practice there might be skew // but in practice there might be skew
// This makes sure we all agree. // This makes sure we all agree.
cfg.OrigEnv = os.Environ() cfg.OrigEnv = os.Environ()
cfg.NewEnv = env.MkEnv() cfg.CmdEnv = envcmd.MkEnv()
for _, env := range cfg.NewEnv { for _, env := range cfg.CmdEnv {
if os.Getenv(env.Name) != env.Value { if os.Getenv(env.Name) != env.Value {
os.Setenv(env.Name, env.Value) os.Setenv(env.Name, env.Value)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment