Commit a918864c authored by Russ Cox's avatar Russ Cox

cmd/go: split out cmd/go/internal/cfg

This is one CL in a long sequence of changes to break up the
go command from one package into a plausible group of packages.

This sequence is concerned only with moving code, not changing
or cleaning up code. There will still be more cleanup after this sequence.

The entire sequence will be submitted together: it is not a goal
for the tree to build at every step.

For #18653.

Change-Id: Icb3f168ade91e7da5fcab89ac75b768daefff359
Reviewed-on: https://go-review.googlesource.com/36191Reviewed-by: 's avatarDavid Crawshaw <crawshaw@golang.org>
parent 762eb408
...@@ -6,6 +6,7 @@ package main ...@@ -6,6 +6,7 @@ package main
import ( import (
"bytes" "bytes"
"cmd/go/internal/cfg"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
...@@ -28,7 +29,7 @@ The report includes useful system information. ...@@ -28,7 +29,7 @@ The report includes useful system information.
} }
func init() { func init() {
cmdBug.Flag.BoolVar(&buildV, "v", false, "") cmdBug.Flag.BoolVar(&cfg.BuildV, "v", false, "")
} }
func runBug(cmd *Command, args []string) { func runBug(cmd *Command, args []string) {
...@@ -38,13 +39,13 @@ func runBug(cmd *Command, args []string) { ...@@ -38,13 +39,13 @@ func runBug(cmd *Command, args []string) {
fmt.Fprint(&buf, "#### System details\n\n") fmt.Fprint(&buf, "#### System details\n\n")
fmt.Fprintln(&buf, "```") fmt.Fprintln(&buf, "```")
fmt.Fprintf(&buf, "go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH) fmt.Fprintf(&buf, "go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)
env := newEnv env := cfg.NewEnv
env = append(env, extraEnvVars()...) env = append(env, extraEnvVars()...)
for _, e := range env { for _, e := range env {
// Hide the TERM environment variable from "go bug". // Hide the TERM environment variable from "go bug".
// See issue #18128 // See issue #18128
if e.name != "TERM" { if e.Name != "TERM" {
fmt.Fprintf(&buf, "%s=\"%s\"\n", e.name, e.value) fmt.Fprintf(&buf, "%s=\"%s\"\n", e.Name, e.Value)
} }
} }
printGoDetails(&buf) printGoDetails(&buf)
...@@ -97,7 +98,7 @@ func printOSDetails(w io.Writer) { ...@@ -97,7 +98,7 @@ func printOSDetails(w io.Writer) {
if err == nil { if err == nil {
fmt.Fprintf(w, "/etc/release: %s\n", out) fmt.Fprintf(w, "/etc/release: %s\n", out)
} else { } else {
if buildV { if cfg.BuildV {
fmt.Printf("failed to read /etc/release: %v\n", err) fmt.Printf("failed to read /etc/release: %v\n", err)
} }
} }
...@@ -114,7 +115,7 @@ func printCDetails(w io.Writer) { ...@@ -114,7 +115,7 @@ func printCDetails(w io.Writer) {
// Print up to the first newline. // Print up to the first newline.
fmt.Fprintf(w, "gdb --version: %s\n", firstLine(out)) fmt.Fprintf(w, "gdb --version: %s\n", firstLine(out))
} else { } else {
if buildV { if cfg.BuildV {
fmt.Printf("failed to run gdb --version: %v\n", err) fmt.Printf("failed to run gdb --version: %v\n", err)
} }
} }
...@@ -123,7 +124,7 @@ func printCDetails(w io.Writer) { ...@@ -123,7 +124,7 @@ func printCDetails(w io.Writer) {
func inspectGoVersion(w io.Writer) { func inspectGoVersion(w io.Writer) {
data, err := httpGET("https://golang.org/VERSION?m=text") data, err := httpGET("https://golang.org/VERSION?m=text")
if err != nil { if err != nil {
if buildV { if cfg.BuildV {
fmt.Printf("failed to read from golang.org/VERSION: %v\n", err) fmt.Printf("failed to read from golang.org/VERSION: %v\n", err)
} }
return return
...@@ -150,7 +151,7 @@ func printCmdOut(w io.Writer, prefix, path string, args ...string) { ...@@ -150,7 +151,7 @@ func printCmdOut(w io.Writer, prefix, path string, args ...string) {
cmd := exec.Command(path, args...) cmd := exec.Command(path, args...)
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
if buildV { if cfg.BuildV {
fmt.Printf("%s %s: %v\n", path, strings.Join(args, " "), err) fmt.Printf("%s %s: %v\n", path, strings.Join(args, " "), err)
} }
return return
......
This diff is collapsed.
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package main package main
import ( import (
"cmd/go/internal/cfg"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
...@@ -172,7 +173,7 @@ func clean(p *Package) { ...@@ -172,7 +173,7 @@ func clean(p *Package) {
} }
} }
if buildN || buildX { if cfg.BuildN || cfg.BuildX {
b.showcmd(p.Dir, "rm -f %s", strings.Join(allRemove, " ")) b.showcmd(p.Dir, "rm -f %s", strings.Join(allRemove, " "))
} }
...@@ -185,9 +186,9 @@ func clean(p *Package) { ...@@ -185,9 +186,9 @@ func clean(p *Package) {
if dir.IsDir() { if dir.IsDir() {
// TODO: Remove once Makefiles are forgotten. // TODO: Remove once Makefiles are forgotten.
if cleanDir[name] { if cleanDir[name] {
if buildN || buildX { if cfg.BuildN || cfg.BuildX {
b.showcmd(p.Dir, "rm -r %s", name) b.showcmd(p.Dir, "rm -r %s", name)
if buildN { if cfg.BuildN {
continue continue
} }
} }
...@@ -198,7 +199,7 @@ func clean(p *Package) { ...@@ -198,7 +199,7 @@ func clean(p *Package) {
continue continue
} }
if buildN { if cfg.BuildN {
continue continue
} }
...@@ -208,10 +209,10 @@ func clean(p *Package) { ...@@ -208,10 +209,10 @@ func clean(p *Package) {
} }
if cleanI && p.target != "" { if cleanI && p.target != "" {
if buildN || buildX { if cfg.BuildN || cfg.BuildX {
b.showcmd("", "rm -f %s", p.target) b.showcmd("", "rm -f %s", p.target)
} }
if !buildN { if !cfg.BuildN {
removeFile(p.target) removeFile(p.target)
} }
} }
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
package main package main
import "cmd/go/internal/cfg"
var cmdDoc = &Command{ var cmdDoc = &Command{
Run: runDoc, Run: runDoc,
UsageLine: "doc [-u] [-c] [package|[package.]symbol[.method]]", UsageLine: "doc [-u] [-c] [package|[package.]symbol[.method]]",
...@@ -114,5 +116,5 @@ Flags: ...@@ -114,5 +116,5 @@ Flags:
} }
func runDoc(cmd *Command, args []string) { func runDoc(cmd *Command, args []string) {
run(buildToolExec, tool("doc"), args) run(cfg.BuildToolexec, tool("doc"), args)
} }
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package main package main
import ( import (
"cmd/go/internal/cfg"
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
...@@ -25,22 +26,18 @@ each named variable on its own line. ...@@ -25,22 +26,18 @@ each named variable on its own line.
`, `,
} }
type envVar struct { func mkEnv() []cfg.EnvVar {
name, value string
}
func mkEnv() []envVar {
var b builder var b builder
b.init() b.init()
env := []envVar{ env := []cfg.EnvVar{
{"GOARCH", goarch}, {"GOARCH", cfg.Goarch},
{"GOBIN", gobin}, {"GOBIN", gobin},
{"GOEXE", exeSuffix}, {"GOEXE", cfg.ExeSuffix},
{"GOHOSTARCH", runtime.GOARCH}, {"GOHOSTARCH", runtime.GOARCH},
{"GOHOSTOS", runtime.GOOS}, {"GOHOSTOS", runtime.GOOS},
{"GOOS", goos}, {"GOOS", cfg.Goos},
{"GOPATH", buildContext.GOPATH}, {"GOPATH", cfg.BuildContext.GOPATH},
{"GORACE", os.Getenv("GORACE")}, {"GORACE", os.Getenv("GORACE")},
{"GOROOT", goroot}, {"GOROOT", goroot},
{"GOTOOLDIR", toolDir}, {"GOTOOLDIR", toolDir},
...@@ -50,48 +47,48 @@ func mkEnv() []envVar { ...@@ -50,48 +47,48 @@ func mkEnv() []envVar {
} }
if gccgoBin != "" { if gccgoBin != "" {
env = append(env, envVar{"GCCGO", gccgoBin}) env = append(env, cfg.EnvVar{"GCCGO", gccgoBin})
} else { } else {
env = append(env, envVar{"GCCGO", gccgoName}) env = append(env, cfg.EnvVar{"GCCGO", gccgoName})
} }
switch goarch { switch cfg.Goarch {
case "arm": case "arm":
env = append(env, envVar{"GOARM", os.Getenv("GOARM")}) env = append(env, cfg.EnvVar{"GOARM", os.Getenv("GOARM")})
case "386": case "386":
env = append(env, envVar{"GO386", os.Getenv("GO386")}) env = append(env, cfg.EnvVar{"GO386", os.Getenv("GO386")})
} }
cmd := b.gccCmd(".") cmd := b.gccCmd(".")
env = append(env, envVar{"CC", cmd[0]}) env = append(env, cfg.EnvVar{"CC", cmd[0]})
env = append(env, envVar{"GOGCCFLAGS", strings.Join(cmd[3:], " ")}) env = append(env, cfg.EnvVar{"GOGCCFLAGS", strings.Join(cmd[3:], " ")})
cmd = b.gxxCmd(".") cmd = b.gxxCmd(".")
env = append(env, envVar{"CXX", cmd[0]}) env = append(env, cfg.EnvVar{"CXX", cmd[0]})
if buildContext.CgoEnabled { if cfg.BuildContext.CgoEnabled {
env = append(env, envVar{"CGO_ENABLED", "1"}) env = append(env, cfg.EnvVar{"CGO_ENABLED", "1"})
} else { } else {
env = append(env, envVar{"CGO_ENABLED", "0"}) env = append(env, cfg.EnvVar{"CGO_ENABLED", "0"})
} }
return env return env
} }
func findEnv(env []envVar, name string) string { func findEnv(env []cfg.EnvVar, name string) string {
for _, e := range env { for _, e := range env {
if e.name == name { if e.Name == name {
return e.value return e.Value
} }
} }
return "" return ""
} }
// extraEnvVars returns environment variables that should not leak into child processes. // extraEnvVars returns environment variables that should not leak into child processes.
func extraEnvVars() []envVar { func extraEnvVars() []cfg.EnvVar {
var b builder var b builder
b.init() b.init()
cppflags, cflags, cxxflags, fflags, ldflags := b.cflags(&Package{}) cppflags, cflags, cxxflags, fflags, ldflags := b.cflags(&Package{})
return []envVar{ return []cfg.EnvVar{
{"PKG_CONFIG", b.pkgconfigCmd()}, {"PKG_CONFIG", b.pkgconfigCmd()},
{"CGO_CFLAGS", strings.Join(cflags, " ")}, {"CGO_CFLAGS", strings.Join(cflags, " ")},
{"CGO_CPPFLAGS", strings.Join(cppflags, " ")}, {"CGO_CPPFLAGS", strings.Join(cppflags, " ")},
...@@ -102,7 +99,7 @@ func extraEnvVars() []envVar { ...@@ -102,7 +99,7 @@ func extraEnvVars() []envVar {
} }
func runEnv(cmd *Command, args []string) { func runEnv(cmd *Command, args []string) {
env := newEnv env := cfg.NewEnv
env = append(env, extraEnvVars()...) env = append(env, extraEnvVars()...)
if len(args) > 0 { if len(args) > 0 {
for _, name := range args { for _, name := range args {
...@@ -112,16 +109,16 @@ func runEnv(cmd *Command, args []string) { ...@@ -112,16 +109,16 @@ func runEnv(cmd *Command, args []string) {
} }
for _, e := range env { for _, e := range env {
if e.name != "TERM" { if e.Name != "TERM" {
switch runtime.GOOS { switch runtime.GOOS {
default: default:
fmt.Printf("%s=\"%s\"\n", e.name, e.value) fmt.Printf("%s=\"%s\"\n", e.Name, e.Value)
case "plan9": case "plan9":
if strings.IndexByte(e.value, '\x00') < 0 { if strings.IndexByte(e.Value, '\x00') < 0 {
fmt.Printf("%s='%s'\n", e.name, strings.Replace(e.value, "'", "''", -1)) fmt.Printf("%s='%s'\n", e.Name, strings.Replace(e.Value, "'", "''", -1))
} else { } else {
v := strings.Split(e.value, "\x00") v := strings.Split(e.Value, "\x00")
fmt.Printf("%s=(", e.name) fmt.Printf("%s=(", e.Name)
for x, s := range v { for x, s := range v {
if x > 0 { if x > 0 {
fmt.Printf(" ") fmt.Printf(" ")
...@@ -131,7 +128,7 @@ func runEnv(cmd *Command, args []string) { ...@@ -131,7 +128,7 @@ func runEnv(cmd *Command, args []string) {
fmt.Printf(")\n") fmt.Printf(")\n")
} }
case "windows": case "windows":
fmt.Printf("set %s=%s\n", e.name, e.value) fmt.Printf("set %s=%s\n", e.Name, e.Value)
} }
} }
} }
......
...@@ -4,7 +4,10 @@ ...@@ -4,7 +4,10 @@
package main package main
import "cmd/go/internal/str" import (
"cmd/go/internal/cfg"
"cmd/go/internal/str"
)
var cmdFix = &Command{ var cmdFix = &Command{
Run: runFix, Run: runFix,
...@@ -27,6 +30,6 @@ func runFix(cmd *Command, args []string) { ...@@ -27,6 +30,6 @@ func runFix(cmd *Command, args []string) {
// Use pkg.gofiles instead of pkg.Dir so that // Use pkg.gofiles instead of pkg.Dir so that
// the command only applies to this package, // the command only applies to this package,
// not to packages in subdirectories. // not to packages in subdirectories.
run(str.StringList(buildToolExec, tool("fix"), relPaths(pkg.allgofiles))) run(str.StringList(cfg.BuildToolexec, tool("fix"), relPaths(pkg.allgofiles)))
} }
} }
...@@ -7,6 +7,7 @@ package main ...@@ -7,6 +7,7 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"cmd/go/internal/cfg"
"fmt" "fmt"
"io" "io"
"log" "log"
...@@ -200,7 +201,7 @@ func (g *Generator) run() (ok bool) { ...@@ -200,7 +201,7 @@ func (g *Generator) run() (ok bool) {
}() }()
g.dir, g.file = filepath.Split(g.path) g.dir, g.file = filepath.Split(g.path)
g.dir = filepath.Clean(g.dir) // No final separator please. g.dir = filepath.Clean(g.dir) // No final separator please.
if buildV { if cfg.BuildV {
fmt.Fprintf(os.Stderr, "%s\n", shortPath(g.path)) fmt.Fprintf(os.Stderr, "%s\n", shortPath(g.path))
} }
...@@ -255,10 +256,10 @@ func (g *Generator) run() (ok bool) { ...@@ -255,10 +256,10 @@ func (g *Generator) run() (ok bool) {
continue continue
} }
// Run the command line. // Run the command line.
if buildN || buildX { if cfg.BuildN || cfg.BuildX {
fmt.Fprintf(os.Stderr, "%s\n", strings.Join(words, " ")) fmt.Fprintf(os.Stderr, "%s\n", strings.Join(words, " "))
} }
if buildN { if cfg.BuildN {
continue continue
} }
g.exec(words) g.exec(words)
...@@ -277,8 +278,8 @@ func isGoGenerate(buf []byte) bool { ...@@ -277,8 +278,8 @@ func isGoGenerate(buf []byte) bool {
// single go:generate command. // single go:generate command.
func (g *Generator) setEnv() { func (g *Generator) setEnv() {
g.env = []string{ g.env = []string{
"GOARCH=" + buildContext.GOARCH, "GOARCH=" + cfg.BuildContext.GOARCH,
"GOOS=" + buildContext.GOOS, "GOOS=" + cfg.BuildContext.GOOS,
"GOFILE=" + g.file, "GOFILE=" + g.file,
"GOLINE=" + strconv.Itoa(g.lineNum), "GOLINE=" + strconv.Itoa(g.lineNum),
"GOPACKAGE=" + g.pkg, "GOPACKAGE=" + g.pkg,
...@@ -393,7 +394,7 @@ func (g *Generator) exec(words []string) { ...@@ -393,7 +394,7 @@ func (g *Generator) exec(words []string) {
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
// Run the command in the package directory. // Run the command in the package directory.
cmd.Dir = g.dir cmd.Dir = g.dir
cmd.Env = mergeEnvLists(g.env, origEnv) cmd.Env = mergeEnvLists(g.env, cfg.OrigEnv)
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
g.errorf("running %q: %s", words[0], err) g.errorf("running %q: %s", words[0], err)
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package main package main
import ( import (
"cmd/go/internal/cfg"
"cmd/go/internal/str" "cmd/go/internal/str"
"fmt" "fmt"
"go/build" "go/build"
...@@ -303,7 +304,7 @@ func download(arg string, parent *Package, stk *importStack, mode int) { ...@@ -303,7 +304,7 @@ func download(arg string, parent *Package, stk *importStack, mode int) {
// due to wildcard expansion. // due to wildcard expansion.
for _, p := range pkgs { for _, p := range pkgs {
if *getFix { if *getFix {
run(buildToolExec, str.StringList(tool("fix"), relPaths(p.allgofiles))) run(cfg.BuildToolexec, str.StringList(tool("fix"), relPaths(p.allgofiles)))
// The imports might have changed, so reload again. // The imports might have changed, so reload again.
p = reloadPackage(arg, stk) p = reloadPackage(arg, stk)
...@@ -424,7 +425,7 @@ func downloadPackage(p *Package) error { ...@@ -424,7 +425,7 @@ func downloadPackage(p *Package) error {
if p.build.SrcRoot == "" { if p.build.SrcRoot == "" {
// Package not found. Put in first directory of $GOPATH. // Package not found. Put in first directory of $GOPATH.
list := filepath.SplitList(buildContext.GOPATH) list := filepath.SplitList(cfg.BuildContext.GOPATH)
if len(list) == 0 { if len(list) == 0 {
return fmt.Errorf("cannot download, $GOPATH not set. For more details see: 'go help gopath'") return fmt.Errorf("cannot download, $GOPATH not set. For more details see: 'go help gopath'")
} }
...@@ -446,7 +447,7 @@ func downloadPackage(p *Package) error { ...@@ -446,7 +447,7 @@ func downloadPackage(p *Package) error {
} }
downloadRootCache[root] = true downloadRootCache[root] = true
if buildV { if cfg.BuildV {
fmt.Fprintf(os.Stderr, "%s (download)\n", rootPath) fmt.Fprintf(os.Stderr, "%s (download)\n", rootPath)
} }
...@@ -473,7 +474,7 @@ func downloadPackage(p *Package) error { ...@@ -473,7 +474,7 @@ func downloadPackage(p *Package) error {
if err = os.MkdirAll(parent, 0777); err != nil { if err = os.MkdirAll(parent, 0777); err != nil {
return err return err
} }
if buildV && !gopathExisted && p.build.Root == buildContext.GOPATH { if cfg.BuildV && !gopathExisted && p.build.Root == cfg.BuildContext.GOPATH {
fmt.Fprintf(os.Stderr, "created GOPATH=%s; see 'go help gopath'\n", p.build.Root) fmt.Fprintf(os.Stderr, "created GOPATH=%s; see 'go help gopath'\n", p.build.Root)
} }
...@@ -487,7 +488,7 @@ func downloadPackage(p *Package) error { ...@@ -487,7 +488,7 @@ func downloadPackage(p *Package) error {
} }
} }
if buildN { if cfg.BuildN {
// Do not show tag sync in -n; it's noise more than anything, // Do not show tag sync in -n; it's noise more than anything,
// and since we're not running commands, no tag will be found. // and since we're not running commands, no tag will be found.
// But avoid printing nothing. // But avoid printing nothing.
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
package main package main
import ( import (
"cmd/go/internal/cfg"
"cmd/internal/browser" "cmd/internal/browser"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
...@@ -79,7 +80,7 @@ func httpsOrHTTP(importPath string, security securityMode) (urlStr string, body ...@@ -79,7 +80,7 @@ func httpsOrHTTP(importPath string, security securityMode) (urlStr string, body
} }
u.RawQuery = "go-get=1" u.RawQuery = "go-get=1"
urlStr = u.String() urlStr = u.String()
if buildV { if cfg.BuildV {
log.Printf("Fetching %s", urlStr) log.Printf("Fetching %s", urlStr)
} }
if security == insecure && scheme == "https" { // fail earlier if security == insecure && scheme == "https" { // fail earlier
...@@ -96,7 +97,7 @@ func httpsOrHTTP(importPath string, security securityMode) (urlStr string, body ...@@ -96,7 +97,7 @@ func httpsOrHTTP(importPath string, security securityMode) (urlStr string, body
} }
urlStr, res, err := fetch("https") urlStr, res, err := fetch("https")
if err != nil { if err != nil {
if buildV { if cfg.BuildV {
log.Printf("https fetch failed: %v", err) log.Printf("https fetch failed: %v", err)
} }
if security == insecure { if security == insecure {
...@@ -110,7 +111,7 @@ func httpsOrHTTP(importPath string, security securityMode) (urlStr string, body ...@@ -110,7 +111,7 @@ func httpsOrHTTP(importPath string, security securityMode) (urlStr string, body
} }
// Note: accepting a non-200 OK here, so people can serve a // Note: accepting a non-200 OK here, so people can serve a
// meta import in their http 404 page. // meta import in their http 404 page.
if buildV { if cfg.BuildV {
log.Printf("Parsing meta tags from %s (status code %d)", urlStr, res.StatusCode) log.Printf("Parsing meta tags from %s (status code %d)", urlStr, res.StatusCode)
} }
return urlStr, res.Body, nil return urlStr, res.Body, nil
......
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package cfg holds configuration shared by multiple parts
// of the go command.
package cfg
import (
"go/build"
"runtime"
)
// These are general "build flags" used by build and other commands.
var (
BuildA bool // -a flag
BuildBuildmode string // -buildmode flag
BuildContext = build.Default
BuildI bool // -i flag
BuildLdflags []string // -ldflags flag
BuildLinkshared bool // -linkshared flag
BuildMSan bool // -msan flag
BuildN bool // -n flag
BuildO string // -o flag
BuildP = runtime.NumCPU() // -p flag
BuildPkgdir string // -pkgdir flag
BuildRace bool // -race flag
BuildToolexec []string // -toolexec flag
BuildToolchainName string
BuildToolchainCompiler string
BuildToolchainLinker string
BuildV bool // -v flag
BuildWork bool // -work flag
BuildX bool // -x flag
)
// The test coverage mode affects package loading. Sigh.
var TestCoverMode string // -covermode flag
// An EnvVar is an environment variable Name=Value.
type EnvVar struct {
Name string
Value string
}
// OrigEnv is the original environment of the program at startup.
var OrigEnv []string
// NewEnv is the new environment for running commands.
var NewEnv []EnvVar
// Global build parameters (used during package load)
var (
Goarch string
Goos string
ExeSuffix string
Gopath []string
)
...@@ -6,6 +6,7 @@ package main ...@@ -6,6 +6,7 @@ package main
import ( import (
"bufio" "bufio"
"cmd/go/internal/cfg"
"encoding/json" "encoding/json"
"io" "io"
"os" "os"
...@@ -165,7 +166,7 @@ func runList(cmd *Command, args []string) { ...@@ -165,7 +166,7 @@ func runList(cmd *Command, args []string) {
var cachedCtxt *Context var cachedCtxt *Context
context := func() *Context { context := func() *Context {
if cachedCtxt == nil { if cachedCtxt == nil {
cachedCtxt = newContext(&buildContext) cachedCtxt = newContext(&cfg.BuildContext)
} }
return cachedCtxt return cachedCtxt
} }
......
...@@ -7,6 +7,7 @@ package main ...@@ -7,6 +7,7 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"cmd/go/internal/cfg"
"cmd/go/internal/str" "cmd/go/internal/str"
"flag" "flag"
"fmt" "fmt"
...@@ -119,9 +120,6 @@ func setExitStatus(n int) { ...@@ -119,9 +120,6 @@ func setExitStatus(n int) {
exitMu.Unlock() exitMu.Unlock()
} }
var origEnv []string
var newEnv []envVar
func main() { func main() {
_ = go11tag _ = go11tag
flag.Usage = usage flag.Usage = usage
...@@ -141,7 +139,7 @@ func main() { ...@@ -141,7 +139,7 @@ func main() {
// Diagnose common mistake: GOPATH==GOROOT. // Diagnose common mistake: GOPATH==GOROOT.
// This setting is equivalent to not setting GOPATH at all, // This setting is equivalent to not setting GOPATH at all,
// which is not what most people want when they do it. // which is not what most people want when they do it.
if gopath := buildContext.GOPATH; gopath == runtime.GOROOT() { if gopath := cfg.BuildContext.GOPATH; gopath == runtime.GOROOT() {
fmt.Fprintf(os.Stderr, "warning: GOPATH set to GOROOT (%s) has no effect\n", gopath) fmt.Fprintf(os.Stderr, "warning: GOPATH set to GOROOT (%s) has no effect\n", gopath)
} else { } else {
for _, p := range filepath.SplitList(gopath) { for _, p := range filepath.SplitList(gopath) {
...@@ -169,11 +167,11 @@ func main() { ...@@ -169,11 +167,11 @@ func main() {
// the same default computation of these as we do, // the same default computation of these as we do,
// 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.
origEnv = os.Environ() cfg.OrigEnv = os.Environ()
newEnv = mkEnv() cfg.NewEnv = mkEnv()
for _, env := range newEnv { for _, env := range cfg.NewEnv {
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)
} }
} }
...@@ -455,9 +453,9 @@ func exitIfErrors() { ...@@ -455,9 +453,9 @@ func exitIfErrors() {
func run(cmdargs ...interface{}) { func run(cmdargs ...interface{}) {
cmdline := str.StringList(cmdargs...) cmdline := str.StringList(cmdargs...)
if buildN || buildX { if cfg.BuildN || cfg.BuildX {
fmt.Printf("%s\n", strings.Join(cmdline, " ")) fmt.Printf("%s\n", strings.Join(cmdline, " "))
if buildN { if cfg.BuildN {
return return
} }
} }
...@@ -602,12 +600,12 @@ func matchPackages(pattern string) []string { ...@@ -602,12 +600,12 @@ func matchPackages(pattern string) []string {
have := map[string]bool{ have := map[string]bool{
"builtin": true, // ignore pseudo-package that exists only for documentation "builtin": true, // ignore pseudo-package that exists only for documentation
} }
if !buildContext.CgoEnabled { if !cfg.BuildContext.CgoEnabled {
have["runtime/cgo"] = true // ignore during walk have["runtime/cgo"] = true // ignore during walk
} }
var pkgs []string var pkgs []string
for _, src := range buildContext.SrcDirs() { for _, src := range cfg.BuildContext.SrcDirs() {
if (pattern == "std" || pattern == "cmd") && src != gorootSrc { if (pattern == "std" || pattern == "cmd") && src != gorootSrc {
continue continue
} }
...@@ -643,7 +641,7 @@ func matchPackages(pattern string) []string { ...@@ -643,7 +641,7 @@ func matchPackages(pattern string) []string {
if !match(name) { if !match(name) {
return nil return nil
} }
_, err = buildContext.ImportDir(path, 0) _, err = cfg.BuildContext.ImportDir(path, 0)
if err != nil { if err != nil {
if _, noGo := err.(*build.NoGoError); noGo { if _, noGo := err.(*build.NoGoError); noGo {
return nil return nil
...@@ -720,7 +718,7 @@ func matchPackagesInFS(pattern string) []string { ...@@ -720,7 +718,7 @@ func matchPackagesInFS(pattern string) []string {
// as not matching the pattern. Go 1.5 and earlier skipped, but that // as not matching the pattern. Go 1.5 and earlier skipped, but that
// behavior means people miss serious mistakes. // behavior means people miss serious mistakes.
// See golang.org/issue/11407. // See golang.org/issue/11407.
if p, err := buildContext.ImportDir(path, 0); err != nil && (p == nil || len(p.InvalidGoFiles) == 0) { if p, err := cfg.BuildContext.ImportDir(path, 0); err != nil && (p == nil || len(p.InvalidGoFiles) == 0) {
if _, noGo := err.(*build.NoGoError); !noGo { if _, noGo := err.(*build.NoGoError); !noGo {
log.Print(err) log.Print(err)
} }
......
...@@ -6,6 +6,7 @@ package main ...@@ -6,6 +6,7 @@ package main
import ( import (
"bytes" "bytes"
"cmd/go/internal/cfg"
"cmd/go/internal/str" "cmd/go/internal/str"
"crypto/sha1" "crypto/sha1"
"errors" "errors"
...@@ -143,11 +144,11 @@ type CoverVar struct { ...@@ -143,11 +144,11 @@ type CoverVar struct {
func (p *Package) copyBuild(pp *build.Package) { func (p *Package) copyBuild(pp *build.Package) {
p.build = pp p.build = pp
if pp.PkgTargetRoot != "" && buildPkgdir != "" { if pp.PkgTargetRoot != "" && cfg.BuildPkgdir != "" {
old := pp.PkgTargetRoot old := pp.PkgTargetRoot
pp.PkgRoot = buildPkgdir pp.PkgRoot = cfg.BuildPkgdir
pp.PkgTargetRoot = buildPkgdir pp.PkgTargetRoot = cfg.BuildPkgdir
pp.PkgObj = filepath.Join(buildPkgdir, strings.TrimPrefix(pp.PkgObj, old)) pp.PkgObj = filepath.Join(cfg.BuildPkgdir, strings.TrimPrefix(pp.PkgObj, old))
} }
p.Dir = pp.Dir p.Dir = pp.Dir
...@@ -364,7 +365,7 @@ func loadImport(path, srcDir string, parent *Package, stk *importStack, importPo ...@@ -364,7 +365,7 @@ func loadImport(path, srcDir string, parent *Package, stk *importStack, importPo
// Not vendoring, or we already found the vendored path. // Not vendoring, or we already found the vendored path.
buildMode |= build.IgnoreVendor buildMode |= build.IgnoreVendor
} }
bp, err := buildContext.Import(path, srcDir, buildMode) bp, err := cfg.BuildContext.Import(path, srcDir, buildMode)
bp.ImportPath = importPath bp.ImportPath = importPath
if gobin != "" { if gobin != "" {
bp.BinDir = gobin bp.BinDir = gobin
...@@ -587,7 +588,7 @@ func disallowInternal(srcDir string, p *Package, stk *importStack) *Package { ...@@ -587,7 +588,7 @@ func disallowInternal(srcDir string, p *Package, stk *importStack) *Package {
} }
// We can't check standard packages with gccgo. // We can't check standard packages with gccgo.
if buildContext.Compiler == "gccgo" && p.Standard { if cfg.BuildContext.Compiler == "gccgo" && p.Standard {
return p return p
} }
...@@ -846,7 +847,7 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package ...@@ -846,7 +847,7 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
useBindir := p.Name == "main" useBindir := p.Name == "main"
if !p.Standard { if !p.Standard {
switch buildBuildmode { switch cfg.BuildBuildmode {
case "c-archive", "c-shared", "plugin": case "c-archive", "c-shared", "plugin":
useBindir = false useBindir = false
} }
...@@ -861,8 +862,8 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package ...@@ -861,8 +862,8 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
return p return p
} }
_, elem := filepath.Split(p.Dir) _, elem := filepath.Split(p.Dir)
full := buildContext.GOOS + "_" + buildContext.GOARCH + "/" + elem full := cfg.BuildContext.GOOS + "_" + cfg.BuildContext.GOARCH + "/" + elem
if buildContext.GOOS != toolGOOS || buildContext.GOARCH != toolGOARCH { if cfg.BuildContext.GOOS != toolGOOS || cfg.BuildContext.GOARCH != toolGOARCH {
// Install cross-compiled binaries to subdirectories of bin. // Install cross-compiled binaries to subdirectories of bin.
elem = full elem = full
} }
...@@ -880,7 +881,7 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package ...@@ -880,7 +881,7 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
// Override all the usual logic and force it into the tool directory. // Override all the usual logic and force it into the tool directory.
p.target = filepath.Join(gorootPkg, "tool", full) p.target = filepath.Join(gorootPkg, "tool", full)
} }
if p.target != "" && buildContext.GOOS == "windows" { if p.target != "" && cfg.BuildContext.GOOS == "windows" {
p.target += ".exe" p.target += ".exe"
} }
} else if p.local { } else if p.local {
...@@ -889,12 +890,12 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package ...@@ -889,12 +890,12 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
p.target = "" p.target = ""
} else { } else {
p.target = p.build.PkgObj p.target = p.build.PkgObj
if buildLinkshared { if cfg.BuildLinkshared {
shlibnamefile := p.target[:len(p.target)-2] + ".shlibname" shlibnamefile := p.target[:len(p.target)-2] + ".shlibname"
shlib, err := ioutil.ReadFile(shlibnamefile) shlib, err := ioutil.ReadFile(shlibnamefile)
if err == nil { if err == nil {
libname := strings.TrimSpace(string(shlib)) libname := strings.TrimSpace(string(shlib))
if buildContext.Compiler == "gccgo" { if cfg.BuildContext.Compiler == "gccgo" {
p.Shlib = filepath.Join(p.build.PkgTargetRoot, "shlibs", libname) p.Shlib = filepath.Join(p.build.PkgTargetRoot, "shlibs", libname)
} else { } else {
p.Shlib = filepath.Join(p.build.PkgTargetRoot, libname) p.Shlib = filepath.Join(p.build.PkgTargetRoot, libname)
...@@ -918,23 +919,23 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package ...@@ -918,23 +919,23 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
importPaths = append(importPaths, "syscall") importPaths = append(importPaths, "syscall")
} }
if buildContext.CgoEnabled && p.Name == "main" && !p.Goroot { if cfg.BuildContext.CgoEnabled && p.Name == "main" && !p.Goroot {
// Currently build modes c-shared, pie (on systems that do not // Currently build modes c-shared, pie (on systems that do not
// support PIE with internal linking mode), plugin, and // support PIE with internal linking mode), plugin, and
// -linkshared force external linking mode, as of course does // -linkshared force external linking mode, as of course does
// -ldflags=-linkmode=external. External linking mode forces // -ldflags=-linkmode=external. External linking mode forces
// an import of runtime/cgo. // an import of runtime/cgo.
pieCgo := buildBuildmode == "pie" && (buildContext.GOOS != "linux" || buildContext.GOARCH != "amd64") pieCgo := cfg.BuildBuildmode == "pie" && (cfg.BuildContext.GOOS != "linux" || cfg.BuildContext.GOARCH != "amd64")
linkmodeExternal := false linkmodeExternal := false
for i, a := range buildLdflags { for i, a := range cfg.BuildLdflags {
if a == "-linkmode=external" { if a == "-linkmode=external" {
linkmodeExternal = true linkmodeExternal = true
} }
if a == "-linkmode" && i+1 < len(buildLdflags) && buildLdflags[i+1] == "external" { if a == "-linkmode" && i+1 < len(cfg.BuildLdflags) && cfg.BuildLdflags[i+1] == "external" {
linkmodeExternal = true linkmodeExternal = true
} }
} }
if buildBuildmode == "c-shared" || buildBuildmode == "plugin" || pieCgo || buildLinkshared || linkmodeExternal { if cfg.BuildBuildmode == "c-shared" || cfg.BuildBuildmode == "plugin" || pieCgo || cfg.BuildLinkshared || linkmodeExternal {
importPaths = append(importPaths, "runtime/cgo") importPaths = append(importPaths, "runtime/cgo")
} }
} }
...@@ -945,19 +946,19 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package ...@@ -945,19 +946,19 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
importPaths = append(importPaths, "runtime") importPaths = append(importPaths, "runtime")
// When race detection enabled everything depends on runtime/race. // When race detection enabled everything depends on runtime/race.
// Exclude certain packages to avoid circular dependencies. // Exclude certain packages to avoid circular dependencies.
if buildRace && (!p.Standard || !raceExclude[p.ImportPath]) { if cfg.BuildRace && (!p.Standard || !raceExclude[p.ImportPath]) {
importPaths = append(importPaths, "runtime/race") importPaths = append(importPaths, "runtime/race")
} }
// MSan uses runtime/msan. // MSan uses runtime/msan.
if buildMSan && (!p.Standard || !raceExclude[p.ImportPath]) { if cfg.BuildMSan && (!p.Standard || !raceExclude[p.ImportPath]) {
importPaths = append(importPaths, "runtime/msan") importPaths = append(importPaths, "runtime/msan")
} }
// On ARM with GOARM=5, everything depends on math for the link. // On ARM with GOARM=5, everything depends on math for the link.
if p.Name == "main" && goarch == "arm" { if p.Name == "main" && cfg.Goarch == "arm" {
importPaths = append(importPaths, "math") importPaths = append(importPaths, "math")
} }
// In coverage atomic mode everything depends on sync/atomic. // In coverage atomic mode everything depends on sync/atomic.
if testCoverMode == "atomic" && (!p.Standard || (p.ImportPath != "runtime/cgo" && p.ImportPath != "runtime/race" && p.ImportPath != "sync/atomic")) { if cfg.TestCoverMode == "atomic" && (!p.Standard || (p.ImportPath != "runtime/cgo" && p.ImportPath != "runtime/race" && p.ImportPath != "sync/atomic")) {
importPaths = append(importPaths, "sync/atomic") importPaths = append(importPaths, "sync/atomic")
} }
} }
...@@ -1082,14 +1083,14 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package ...@@ -1082,14 +1083,14 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
} }
// unsafe is a fake package. // unsafe is a fake package.
if p.Standard && (p.ImportPath == "unsafe" || buildContext.Compiler == "gccgo") { if p.Standard && (p.ImportPath == "unsafe" || cfg.BuildContext.Compiler == "gccgo") {
p.target = "" p.target = ""
} }
p.Target = p.target p.Target = p.target
// If cgo is not enabled, ignore cgo supporting sources // If cgo is not enabled, ignore cgo supporting sources
// just as we ignore go files containing import "C". // just as we ignore go files containing import "C".
if !buildContext.CgoEnabled { if !cfg.BuildContext.CgoEnabled {
p.CFiles = nil p.CFiles = nil
p.CXXFiles = nil p.CXXFiles = nil
p.MFiles = nil p.MFiles = nil
...@@ -1102,7 +1103,7 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package ...@@ -1102,7 +1103,7 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
} }
// The gc toolchain only permits C source files with cgo. // The gc toolchain only permits C source files with cgo.
if len(p.CFiles) > 0 && !p.usesCgo() && !p.usesSwig() && buildContext.Compiler == "gc" { if len(p.CFiles) > 0 && !p.usesCgo() && !p.usesSwig() && cfg.BuildContext.Compiler == "gc" {
p.Error = &PackageError{ p.Error = &PackageError{
ImportStack: stk.copy(), ImportStack: stk.copy(),
Err: fmt.Sprintf("C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CFiles, " ")), Err: fmt.Sprintf("C source files not allowed when not using cgo or SWIG: %s", strings.Join(p.CFiles, " ")),
...@@ -1445,7 +1446,7 @@ var isGoRelease = strings.HasPrefix(runtime.Version(), "go1") ...@@ -1445,7 +1446,7 @@ var isGoRelease = strings.HasPrefix(runtime.Version(), "go1")
// isStale reports whether package p needs to be rebuilt, // isStale reports whether package p needs to be rebuilt,
// along with the reason why. // along with the reason why.
func isStale(p *Package) (bool, string) { func isStale(p *Package) (bool, string) {
if p.Standard && (p.ImportPath == "unsafe" || buildContext.Compiler == "gccgo") { if p.Standard && (p.ImportPath == "unsafe" || cfg.BuildContext.Compiler == "gccgo") {
// fake, builtin package // fake, builtin package
return false, "builtin package" return false, "builtin package"
} }
...@@ -1473,7 +1474,7 @@ func isStale(p *Package) (bool, string) { ...@@ -1473,7 +1474,7 @@ func isStale(p *Package) (bool, string) {
} }
// If the -a flag is given, rebuild everything. // If the -a flag is given, rebuild everything.
if buildA { if cfg.BuildA {
return true, "build -a flag in use" return true, "build -a flag in use"
} }
...@@ -1560,10 +1561,10 @@ func isStale(p *Package) (bool, string) { ...@@ -1560,10 +1561,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 != goroot { if p.Root != goroot {
if olderThan(buildToolchainCompiler) { if olderThan(cfg.BuildToolchainCompiler) {
return true, "newer compiler" return true, "newer compiler"
} }
if p.build.IsCommand() && olderThan(buildToolchainLinker) { if p.build.IsCommand() && olderThan(cfg.BuildToolchainLinker) {
return true, "newer linker" return true, "newer linker"
} }
} }
...@@ -1643,7 +1644,7 @@ func computeBuildID(p *Package) { ...@@ -1643,7 +1644,7 @@ func computeBuildID(p *Package) {
// Include the content of runtime/internal/sys/zversion.go in the hash // Include the content of runtime/internal/sys/zversion.go in the hash
// for package runtime. This will give package runtime a // for package runtime. This will give package runtime a
// different build ID in each Go release. // different build ID in each Go release.
if p.Standard && p.ImportPath == "runtime/internal/sys" && buildContext.Compiler != "gccgo" { if p.Standard && p.ImportPath == "runtime/internal/sys" && cfg.BuildContext.Compiler != "gccgo" {
data, err := ioutil.ReadFile(filepath.Join(p.Dir, "zversion.go")) data, err := ioutil.ReadFile(filepath.Join(p.Dir, "zversion.go"))
if err != nil { if err != nil {
fatalf("go: %s", err) fatalf("go: %s", err)
...@@ -1694,7 +1695,7 @@ func loadPackage(arg string, stk *importStack) *Package { ...@@ -1694,7 +1695,7 @@ func loadPackage(arg string, stk *importStack) *Package {
stk.push(arg) stk.push(arg)
defer stk.pop() defer stk.pop()
bp, err := buildContext.ImportDir(filepath.Join(gorootSrc, arg), 0) bp, err := cfg.BuildContext.ImportDir(filepath.Join(gorootSrc, arg), 0)
bp.ImportPath = arg bp.ImportPath = arg
bp.Goroot = true bp.Goroot = true
bp.BinDir = gorootBin bp.BinDir = gorootBin
...@@ -1722,7 +1723,7 @@ func loadPackage(arg string, stk *importStack) *Package { ...@@ -1722,7 +1723,7 @@ func loadPackage(arg string, stk *importStack) *Package {
// referring to io/ioutil rather than a hypothetical import of // referring to io/ioutil rather than a hypothetical import of
// "./ioutil". // "./ioutil".
if build.IsLocalImport(arg) { if build.IsLocalImport(arg) {
bp, _ := buildContext.ImportDir(filepath.Join(cwd, arg), build.FindOnly) bp, _ := cfg.BuildContext.ImportDir(filepath.Join(cwd, arg), build.FindOnly)
if bp.ImportPath != "" && bp.ImportPath != "." { if bp.ImportPath != "" && bp.ImportPath != "." {
arg = bp.ImportPath arg = bp.ImportPath
} }
...@@ -1867,7 +1868,7 @@ var ( ...@@ -1867,7 +1868,7 @@ var (
// It only supports the gc toolchain. // It only supports the gc toolchain.
// Other toolchain maintainers should adjust this function. // Other toolchain maintainers should adjust this function.
func readBuildID(name, target string) (id string, err error) { func readBuildID(name, target string) (id string, err error) {
if buildToolchainName != "gc" { if cfg.BuildToolchainName != "gc" {
return "", errBuildIDToolchain return "", errBuildIDToolchain
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package main package main
import ( import (
"cmd/go/internal/cfg"
"cmd/go/internal/str" "cmd/go/internal/str"
"io/ioutil" "io/ioutil"
"os" "os"
...@@ -160,9 +161,9 @@ func TestSharedLibName(t *testing.T) { ...@@ -160,9 +161,9 @@ func TestSharedLibName(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
oldGopath := buildContext.GOPATH oldGopath := cfg.BuildContext.GOPATH
defer func() { defer func() {
buildContext.GOPATH = oldGopath cfg.BuildContext.GOPATH = oldGopath
os.Chdir(cwd) os.Chdir(cwd)
err := os.RemoveAll(tmpGopath) err := os.RemoveAll(tmpGopath)
if err != nil { if err != nil {
...@@ -174,7 +175,7 @@ func TestSharedLibName(t *testing.T) { ...@@ -174,7 +175,7 @@ func TestSharedLibName(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
buildContext.GOPATH = tmpGopath cfg.BuildContext.GOPATH = tmpGopath
os.Chdir(root) os.Chdir(root)
} }
computed, err := libname(data.args, data.pkgs) computed, err := libname(data.args, data.pkgs)
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package main package main
import ( import (
"cmd/go/internal/cfg"
"cmd/go/internal/str" "cmd/go/internal/str"
"fmt" "fmt"
"os" "os"
...@@ -20,10 +21,10 @@ func findExecCmd() []string { ...@@ -20,10 +21,10 @@ func findExecCmd() []string {
return execCmd return execCmd
} }
execCmd = []string{} // avoid work the second time execCmd = []string{} // avoid work the second time
if goos == runtime.GOOS && goarch == runtime.GOARCH { if cfg.Goos == runtime.GOOS && cfg.Goarch == runtime.GOARCH {
return execCmd return execCmd
} }
path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch)) path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", cfg.Goos, cfg.Goarch))
if err == nil { if err == nil {
execCmd = []string{path} execCmd = []string{path}
} }
...@@ -117,7 +118,7 @@ func runRun(cmd *Command, args []string) { ...@@ -117,7 +118,7 @@ func runRun(cmd *Command, args []string) {
// this case could only happen if the provided source uses cgo // this case could only happen if the provided source uses cgo
// while cgo is disabled. // while cgo is disabled.
hint := "" hint := ""
if !buildContext.CgoEnabled { if !cfg.BuildContext.CgoEnabled {
hint = " (cgo is disabled)" hint = " (cgo is disabled)"
} }
fatalf("go run: no suitable source files%s", hint) fatalf("go run: no suitable source files%s", hint)
...@@ -132,9 +133,9 @@ func runRun(cmd *Command, args []string) { ...@@ -132,9 +133,9 @@ func runRun(cmd *Command, args []string) {
// been compiled. We ignore exit status. // been compiled. We ignore exit status.
func (b *builder) runProgram(a *action) error { func (b *builder) runProgram(a *action) error {
cmdline := str.StringList(findExecCmd(), a.deps[0].target, a.args) cmdline := str.StringList(findExecCmd(), a.deps[0].target, a.args)
if buildN || buildX { if cfg.BuildN || cfg.BuildX {
b.showcmd("", "%s", strings.Join(cmdline, " ")) b.showcmd("", "%s", strings.Join(cmdline, " "))
if buildN { if cfg.BuildN {
return nil return nil
} }
} }
...@@ -149,7 +150,7 @@ func runStdin(cmdline []string) { ...@@ -149,7 +150,7 @@ func runStdin(cmdline []string) {
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Env = origEnv cmd.Env = cfg.OrigEnv
startSigHandlers() startSigHandlers()
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
errorf("%v", err) errorf("%v", err)
......
...@@ -6,6 +6,7 @@ package main ...@@ -6,6 +6,7 @@ package main
import ( import (
"bytes" "bytes"
"cmd/go/internal/cfg"
"cmd/go/internal/str" "cmd/go/internal/str"
"errors" "errors"
"fmt" "fmt"
...@@ -375,9 +376,9 @@ See the documentation of the testing package for more information. ...@@ -375,9 +376,9 @@ See the documentation of the testing package for more information.
} }
var ( var (
testC bool // -c flag testC bool // -c flag
testCover bool // -cover flag testCover bool // -cover flag
testCoverMode string // -covermode flag // Note: testCoverMode is cfg.TestCoverMode (-covermode)
testCoverPaths []string // -coverpkg flag testCoverPaths []string // -coverpkg flag
testCoverPkgs []*Package // -coverpkg flag testCoverPkgs []*Package // -coverpkg flag
testO string // -o flag testO string // -o flag
...@@ -444,18 +445,18 @@ func runTest(cmd *Command, args []string) { ...@@ -444,18 +445,18 @@ func runTest(cmd *Command, args []string) {
// In these cases, streaming the output produces the same result // In these cases, streaming the output produces the same result
// as not streaming, just more immediately. // as not streaming, just more immediately.
testStreamOutput = len(pkgArgs) == 0 || testBench || testStreamOutput = len(pkgArgs) == 0 || testBench ||
(testShowPass && (len(pkgs) == 1 || buildP == 1)) (testShowPass && (len(pkgs) == 1 || cfg.BuildP == 1))
// For 'go test -i -o x.test', we want to build x.test. Imply -c to make the logic easier. // For 'go test -i -o x.test', we want to build x.test. Imply -c to make the logic easier.
if buildI && testO != "" { if cfg.BuildI && testO != "" {
testC = true testC = true
} }
var b builder var b builder
b.init() b.init()
if buildI { if cfg.BuildI {
buildV = testV cfg.BuildV = testV
deps := make(map[string]bool) deps := make(map[string]bool)
for dep := range testMainDeps { for dep := range testMainDeps {
...@@ -479,7 +480,7 @@ func runTest(cmd *Command, args []string) { ...@@ -479,7 +480,7 @@ func runTest(cmd *Command, args []string) {
if deps["C"] { if deps["C"] {
delete(deps, "C") delete(deps, "C")
deps["runtime/cgo"] = true deps["runtime/cgo"] = true
if goos == runtime.GOOS && goarch == runtime.GOARCH && !buildRace && !buildMSan { if cfg.Goos == runtime.GOOS && cfg.Goarch == runtime.GOARCH && !cfg.BuildRace && !cfg.BuildMSan {
deps["cmd/cgo"] = true deps["cmd/cgo"] = true
} }
} }
...@@ -535,7 +536,7 @@ func runTest(cmd *Command, args []string) { ...@@ -535,7 +536,7 @@ func runTest(cmd *Command, args []string) {
p.Stale = true // rebuild p.Stale = true // rebuild
p.StaleReason = "rebuild for coverage" p.StaleReason = "rebuild for coverage"
p.fake = true // do not warn about rebuild p.fake = true // do not warn about rebuild
p.coverMode = testCoverMode p.coverMode = cfg.TestCoverMode
var coverFiles []string var coverFiles []string
coverFiles = append(coverFiles, p.GoFiles...) coverFiles = append(coverFiles, p.GoFiles...)
coverFiles = append(coverFiles, p.CgoFiles...) coverFiles = append(coverFiles, p.CgoFiles...)
...@@ -625,10 +626,10 @@ func runTest(cmd *Command, args []string) { ...@@ -625,10 +626,10 @@ func runTest(cmd *Command, args []string) {
args = " " + args args = " " + args
} }
extraOpts := "" extraOpts := ""
if buildRace { if cfg.BuildRace {
extraOpts = "-race " extraOpts = "-race "
} }
if buildMSan { if cfg.BuildMSan {
extraOpts = "-msan " extraOpts = "-msan "
} }
fmt.Fprintf(os.Stderr, "installing these packages with 'go test %s-i%s' will speed future tests.\n\n", extraOpts, args) fmt.Fprintf(os.Stderr, "installing these packages with 'go test %s-i%s' will speed future tests.\n\n", extraOpts, args)
...@@ -775,7 +776,7 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a ...@@ -775,7 +776,7 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a
ptest.build.ImportPos = m ptest.build.ImportPos = m
if localCover { if localCover {
ptest.coverMode = testCoverMode ptest.coverMode = cfg.TestCoverMode
var coverFiles []string var coverFiles []string
coverFiles = append(coverFiles, ptest.GoFiles...) coverFiles = append(coverFiles, ptest.GoFiles...)
coverFiles = append(coverFiles, ptest.CgoFiles...) coverFiles = append(coverFiles, ptest.CgoFiles...)
...@@ -884,10 +885,10 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a ...@@ -884,10 +885,10 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a
recompileForTest(pmain, p, ptest, testDir) recompileForTest(pmain, p, ptest, testDir)
} }
if buildContext.GOOS == "darwin" { if cfg.BuildContext.GOOS == "darwin" {
if buildContext.GOARCH == "arm" || buildContext.GOARCH == "arm64" { if cfg.BuildContext.GOARCH == "arm" || cfg.BuildContext.GOARCH == "arm64" {
t.IsIOS = true t.IsIOS = true
t.NeedOS = true t.NeedCgo = true
} }
} }
if t.TestMain == nil { if t.TestMain == nil {
...@@ -900,7 +901,7 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a ...@@ -900,7 +901,7 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a
} }
} }
if !buildN { if !cfg.BuildN {
// writeTestmain writes _testmain.go. This must happen after recompileForTest, // writeTestmain writes _testmain.go. This must happen after recompileForTest,
// because recompileForTest modifies XXX. // because recompileForTest modifies XXX.
if err := writeTestmain(filepath.Join(testDir, "_testmain.go"), t); err != nil { if err := writeTestmain(filepath.Join(testDir, "_testmain.go"), t); err != nil {
...@@ -928,8 +929,8 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a ...@@ -928,8 +929,8 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a
a := b.action(modeBuild, modeBuild, pmain) a := b.action(modeBuild, modeBuild, pmain)
a.objdir = testDir + string(filepath.Separator) a.objdir = testDir + string(filepath.Separator)
a.objpkg = filepath.Join(testDir, "main.a") a.objpkg = filepath.Join(testDir, "main.a")
a.target = filepath.Join(testDir, testBinary) + exeSuffix a.target = filepath.Join(testDir, testBinary) + cfg.ExeSuffix
if goos == "windows" { if cfg.Goos == "windows" {
// There are many reserved words on Windows that, // There are many reserved words on Windows that,
// if used in the name of an executable, cause Windows // if used in the name of an executable, cause Windows
// to try to ask for extra permissions. // to try to ask for extra permissions.
...@@ -954,7 +955,7 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a ...@@ -954,7 +955,7 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a
// we could just do this always on Windows. // we could just do this always on Windows.
for _, bad := range windowsBadWords { for _, bad := range windowsBadWords {
if strings.Contains(testBinary, bad) { if strings.Contains(testBinary, bad) {
a.target = filepath.Join(testDir, "test.test") + exeSuffix a.target = filepath.Join(testDir, "test.test") + cfg.ExeSuffix
break break
} }
} }
...@@ -963,7 +964,7 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a ...@@ -963,7 +964,7 @@ func builderTest(b *builder, p *Package) (buildAction, runAction, printAction *a
if testC || testNeedBinary { if testC || testNeedBinary {
// -c or profiling flag: create action to copy binary to ./test.out. // -c or profiling flag: create action to copy binary to ./test.out.
target := filepath.Join(cwd, testBinary+exeSuffix) target := filepath.Join(cwd, testBinary+cfg.ExeSuffix)
if testO != "" { if testO != "" {
target = testO target = testO
if !filepath.IsAbs(target) { if !filepath.IsAbs(target) {
...@@ -1098,9 +1099,9 @@ func builderRunTest(b *builder, a *action) error { ...@@ -1098,9 +1099,9 @@ func builderRunTest(b *builder, a *action) error {
args := str.StringList(findExecCmd(), a.deps[0].target, testArgs) args := str.StringList(findExecCmd(), a.deps[0].target, testArgs)
a.testOutput = new(bytes.Buffer) a.testOutput = new(bytes.Buffer)
if buildN || buildX { if cfg.BuildN || cfg.BuildX {
b.showcmd("", "%s", strings.Join(args, " ")) b.showcmd("", "%s", strings.Join(args, " "))
if buildN { if cfg.BuildN {
return nil return nil
} }
} }
...@@ -1115,7 +1116,7 @@ func builderRunTest(b *builder, a *action) error { ...@@ -1115,7 +1116,7 @@ func builderRunTest(b *builder, a *action) error {
cmd := exec.Command(args[0], args[1:]...) cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = a.p.Dir cmd.Dir = a.p.Dir
cmd.Env = envForDir(cmd.Dir, origEnv) cmd.Env = envForDir(cmd.Dir, cfg.OrigEnv)
var buf bytes.Buffer var buf bytes.Buffer
if testStreamOutput { if testStreamOutput {
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
...@@ -1227,7 +1228,7 @@ func coveragePercentage(out []byte) string { ...@@ -1227,7 +1228,7 @@ func coveragePercentage(out []byte) string {
// builderCleanTest is the action for cleaning up after a test. // builderCleanTest is the action for cleaning up after a test.
func builderCleanTest(b *builder, a *action) error { func builderCleanTest(b *builder, a *action) error {
if buildWork { if cfg.BuildWork {
return nil return nil
} }
run := a.deps[0] run := a.deps[0]
...@@ -1345,7 +1346,7 @@ type testFuncs struct { ...@@ -1345,7 +1346,7 @@ type testFuncs struct {
} }
func (t *testFuncs) CoverMode() string { func (t *testFuncs) CoverMode() string {
return testCoverMode return cfg.TestCoverMode
} }
func (t *testFuncs) CoverEnabled() bool { func (t *testFuncs) CoverEnabled() bool {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package main package main
import ( import (
"cmd/go/internal/cfg"
"flag" "flag"
"fmt" "fmt"
"os" "os"
...@@ -31,7 +32,7 @@ type testFlagSpec struct { ...@@ -31,7 +32,7 @@ type testFlagSpec struct {
var testFlagDefn = []*testFlagSpec{ var testFlagDefn = []*testFlagSpec{
// local. // local.
{name: "c", boolVar: &testC}, {name: "c", boolVar: &testC},
{name: "i", boolVar: &buildI}, {name: "i", boolVar: &cfg.BuildI},
{name: "o"}, {name: "o"},
{name: "cover", boolVar: &testCover}, {name: "cover", boolVar: &testCover},
{name: "covermode"}, {name: "covermode"},
...@@ -169,7 +170,7 @@ func testFlags(args []string) (packageNames, passToTest []string) { ...@@ -169,7 +170,7 @@ func testFlags(args []string) (packageNames, passToTest []string) {
case "covermode": case "covermode":
switch value { switch value {
case "set", "count", "atomic": case "set", "count", "atomic":
testCoverMode = value cfg.TestCoverMode = value
default: default:
fatalf("invalid flag argument for -covermode: %q", value) fatalf("invalid flag argument for -covermode: %q", value)
} }
...@@ -186,11 +187,11 @@ func testFlags(args []string) (packageNames, passToTest []string) { ...@@ -186,11 +187,11 @@ func testFlags(args []string) (packageNames, passToTest []string) {
} }
} }
if testCoverMode == "" { if cfg.TestCoverMode == "" {
testCoverMode = "set" cfg.TestCoverMode = "set"
if buildRace { if cfg.BuildRace {
// Default coverage mode is atomic when -race is set. // Default coverage mode is atomic when -race is set.
testCoverMode = "atomic" cfg.TestCoverMode = "atomic"
} }
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package main package main
import ( import (
"cmd/go/internal/cfg"
"fmt" "fmt"
"go/build" "go/build"
"os" "os"
...@@ -50,7 +51,7 @@ func tool(toolName string) string { ...@@ -50,7 +51,7 @@ func tool(toolName string) string {
if toolIsWindows { if toolIsWindows {
toolPath += toolWindowsExtension toolPath += toolWindowsExtension
} }
if len(buildToolExec) > 0 { if len(cfg.BuildToolexec) > 0 {
return toolPath return toolPath
} }
// Give a nice message if there is no tool with that name. // Give a nice message if there is no tool with that name.
...@@ -115,7 +116,7 @@ func runTool(cmd *Command, args []string) { ...@@ -115,7 +116,7 @@ func runTool(cmd *Command, args []string) {
// or we're printing command lines too (-x mode). // or we're printing command lines too (-x mode).
// Assume if command exited cleanly (even with non-zero status) // Assume if command exited cleanly (even with non-zero status)
// it printed any messages it wanted to print. // it printed any messages it wanted to print.
if e, ok := err.(*exec.ExitError); !ok || !e.Exited() || buildX { if e, ok := err.(*exec.ExitError); !ok || !e.Exited() || cfg.BuildX {
fmt.Fprintf(os.Stderr, "go tool %s: %s\n", toolName, err) fmt.Fprintf(os.Stderr, "go tool %s: %s\n", toolName, err)
} }
setExitStatus(1) setExitStatus(1)
......
...@@ -6,6 +6,7 @@ package main ...@@ -6,6 +6,7 @@ package main
import ( import (
"bytes" "bytes"
"cmd/go/internal/cfg"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
...@@ -373,7 +374,7 @@ func (v *vcsCmd) run1(dir string, cmdline string, keyval []string, verbose bool) ...@@ -373,7 +374,7 @@ func (v *vcsCmd) run1(dir string, cmdline string, keyval []string, verbose bool)
cmd := exec.Command(v.cmd, args...) cmd := exec.Command(v.cmd, args...)
cmd.Dir = dir cmd.Dir = dir
cmd.Env = envForDir(cmd.Dir, os.Environ()) cmd.Env = envForDir(cmd.Dir, os.Environ())
if buildX { if cfg.BuildX {
fmt.Printf("cd %s\n", dir) fmt.Printf("cd %s\n", dir)
fmt.Printf("%s %s\n", v.cmd, strings.Join(args, " ")) fmt.Printf("%s %s\n", v.cmd, strings.Join(args, " "))
} }
...@@ -383,7 +384,7 @@ func (v *vcsCmd) run1(dir string, cmdline string, keyval []string, verbose bool) ...@@ -383,7 +384,7 @@ func (v *vcsCmd) run1(dir string, cmdline string, keyval []string, verbose bool)
err = cmd.Run() err = cmd.Run()
out := buf.Bytes() out := buf.Bytes()
if err != nil { if err != nil {
if verbose || buildV { if verbose || cfg.BuildV {
fmt.Fprintf(os.Stderr, "# cd %s; %s %s\n", dir, v.cmd, strings.Join(args, " ")) fmt.Fprintf(os.Stderr, "# cd %s; %s %s\n", dir, v.cmd, strings.Join(args, " "))
os.Stderr.Write(out) os.Stderr.Write(out)
} }
...@@ -687,7 +688,7 @@ func repoRootForImportDynamic(importPath string, security securityMode) (*repoRo ...@@ -687,7 +688,7 @@ func repoRootForImportDynamic(importPath string, security securityMode) (*repoRo
} }
return nil, fmt.Errorf("parse %s: no go-import meta tags (%s)", urlStr, err) return nil, fmt.Errorf("parse %s: no go-import meta tags (%s)", urlStr, err)
} }
if buildV { if cfg.BuildV {
log.Printf("get %q: found meta tag %#v at %s", importPath, mmi, urlStr) log.Printf("get %q: found meta tag %#v at %s", importPath, mmi, urlStr)
} }
// If the import was "uni.edu/bob/project", which said the // If the import was "uni.edu/bob/project", which said the
...@@ -697,7 +698,7 @@ func repoRootForImportDynamic(importPath string, security securityMode) (*repoRo ...@@ -697,7 +698,7 @@ func repoRootForImportDynamic(importPath string, security securityMode) (*repoRo
// non-evil student). Instead, first verify the root and see // non-evil student). Instead, first verify the root and see
// if it matches Bob's claim. // if it matches Bob's claim.
if mmi.Prefix != importPath { if mmi.Prefix != importPath {
if buildV { if cfg.BuildV {
log.Printf("get %q: verifying non-authoritative meta tag", importPath) log.Printf("get %q: verifying non-authoritative meta tag", importPath)
} }
urlStr0 := urlStr urlStr0 := urlStr
......
...@@ -7,6 +7,7 @@ package main ...@@ -7,6 +7,7 @@ package main
import ( import (
"path/filepath" "path/filepath"
"cmd/go/internal/cfg"
"cmd/go/internal/str" "cmd/go/internal/str"
) )
...@@ -52,5 +53,5 @@ func runVetFiles(p *Package, files []string) { ...@@ -52,5 +53,5 @@ func runVetFiles(p *Package, files []string) {
for i := range files { for i := range files {
files[i] = filepath.Join(p.Dir, files[i]) files[i] = filepath.Join(p.Dir, files[i])
} }
run(buildToolExec, tool("vet"), relPaths(files)) run(cfg.BuildToolexec, tool("vet"), relPaths(files))
} }
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