Skip to content

Commit

Permalink
cmd/go: split out cmd/go/internal/work
Browse files Browse the repository at this point in the history
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: Icdd181098f9f0e81f68bf201e6867cdd8f820300
Reviewed-on: https://go-review.googlesource.com/36197
Reviewed-by: David Crawshaw <[email protected]>
  • Loading branch information
rsc committed Feb 3, 2017
1 parent eb93b20 commit 3c667ef
Show file tree
Hide file tree
Showing 18 changed files with 878 additions and 784 deletions.
44 changes: 0 additions & 44 deletions src/cmd/go/build_test.go

This file was deleted.

20 changes: 11 additions & 9 deletions src/cmd/go/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
package main

import (
"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/load"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/load"
"cmd/go/internal/work"
)

var cmdClean = &base.Command{
Expand Down Expand Up @@ -74,7 +76,7 @@ func init() {
// mentioned explicitly in the docs but they
// are part of the build flags.

addBuildFlags(cmdClean)
work.AddBuildFlags(cmdClean)
}

func runClean(cmd *base.Command, args []string) {
Expand Down Expand Up @@ -124,8 +126,8 @@ func clean(p *load.Package) {
return
}

var b builder
b.print = fmt.Print
var b work.Builder
b.Print = fmt.Print

packageFile := map[string]bool{}
if p.Name != "main" {
Expand Down Expand Up @@ -176,7 +178,7 @@ func clean(p *load.Package) {
}

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, " "))
}

toRemove := map[string]bool{}
Expand All @@ -189,7 +191,7 @@ func clean(p *load.Package) {
// TODO: Remove once Makefiles are forgotten.
if cleanDir[name] {
if cfg.BuildN || cfg.BuildX {
b.showcmd(p.Dir, "rm -r %s", name)
b.Showcmd(p.Dir, "rm -r %s", name)
if cfg.BuildN {
continue
}
Expand All @@ -212,7 +214,7 @@ func clean(p *load.Package) {

if cleanI && p.Internal.Target != "" {
if cfg.BuildN || cfg.BuildX {
b.showcmd("", "rm -f %s", p.Internal.Target)
b.Showcmd("", "rm -f %s", p.Internal.Target)
}
if !cfg.BuildN {
removeFile(p.Internal.Target)
Expand Down
30 changes: 16 additions & 14 deletions src/cmd/go/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
package main

import (
"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/load"
"fmt"
"os"
"runtime"
"strings"

"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/load"
"cmd/go/internal/work"
)

var cmdEnv = &base.Command{
Expand All @@ -29,8 +31,8 @@ each named variable on its own line.
}

func mkEnv() []cfg.EnvVar {
var b builder
b.init()
var b work.Builder
b.Init()

env := []cfg.EnvVar{
{"GOARCH", cfg.Goarch},
Expand All @@ -48,10 +50,10 @@ func mkEnv() []cfg.EnvVar {
{"TERM", "dumb"},
}

if gccgoBin != "" {
env = append(env, cfg.EnvVar{"GCCGO", gccgoBin})
if work.GccgoBin != "" {
env = append(env, cfg.EnvVar{"GCCGO", work.GccgoBin})
} else {
env = append(env, cfg.EnvVar{"GCCGO", gccgoName})
env = append(env, cfg.EnvVar{"GCCGO", work.GccgoName})
}

switch cfg.Goarch {
Expand All @@ -61,10 +63,10 @@ func mkEnv() []cfg.EnvVar {
env = append(env, cfg.EnvVar{"GO386", os.Getenv("GO386")})
}

cmd := b.gccCmd(".")
cmd := b.GccCmd(".")
env = append(env, cfg.EnvVar{"CC", cmd[0]})
env = append(env, cfg.EnvVar{"GOGCCFLAGS", strings.Join(cmd[3:], " ")})
cmd = b.gxxCmd(".")
cmd = b.GxxCmd(".")
env = append(env, cfg.EnvVar{"CXX", cmd[0]})

if cfg.BuildContext.CgoEnabled {
Expand All @@ -87,11 +89,11 @@ func findEnv(env []cfg.EnvVar, name string) string {

// extraEnvVars returns environment variables that should not leak into child processes.
func extraEnvVars() []cfg.EnvVar {
var b builder
b.init()
cppflags, cflags, cxxflags, fflags, ldflags := b.cflags(&load.Package{})
var b work.Builder
b.Init()
cppflags, cflags, cxxflags, fflags, ldflags := b.CFlags(&load.Package{})
return []cfg.EnvVar{
{"PKG_CONFIG", b.pkgconfigCmd()},
{"PKG_CONFIG", b.PkgconfigCmd()},
{"CGO_CFLAGS", strings.Join(cflags, " ")},
{"CGO_CPPFLAGS", strings.Join(cppflags, " ")},
{"CGO_CXXFLAGS", strings.Join(cxxflags, " ")},
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/go/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"cmd/go/internal/base"
"cmd/go/internal/cfg"
"cmd/go/internal/load"
"cmd/go/internal/work"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -134,7 +135,7 @@ var (
)

func init() {
addBuildFlags(cmdGenerate)
work.AddBuildFlags(cmdGenerate)
cmdGenerate.Flag.StringVar(&generateRunFlag, "run", "", "")
}

Expand Down
5 changes: 3 additions & 2 deletions src/cmd/go/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"cmd/go/internal/cfg"
"cmd/go/internal/load"
"cmd/go/internal/str"
"cmd/go/internal/work"
"fmt"
"go/build"
"os"
Expand Down Expand Up @@ -83,7 +84,7 @@ var getFix = cmdGet.Flag.Bool("fix", false, "")
var getInsecure = cmdGet.Flag.Bool("insecure", false, "")

func init() {
addBuildFlags(cmdGet)
work.AddBuildFlags(cmdGet)
cmdGet.Run = runGet // break init loop
}

Expand Down Expand Up @@ -157,7 +158,7 @@ func runGet(cmd *base.Command, args []string) {
return
}

installPackages(args, true)
work.InstallPackages(args, true)
}

// downloadPaths prepares the list of paths to pass to download.
Expand Down
37 changes: 37 additions & 0 deletions src/cmd/go/internal/base/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 base

import "strings"

// envForDir returns a copy of the environment
// suitable for running in the given directory.
// The environment is the current process's environment
// but with an updated $PWD, so that an os.Getwd in the
// child will be faster.
func EnvForDir(dir string, base []string) []string {
// Internally we only use rooted paths, so dir is rooted.
// Even if dir is not rooted, no harm done.
return MergeEnvLists([]string{"PWD=" + dir}, base)
}

// MergeEnvLists merges the two environment lists such that
// variables with the same name in "in" replace those in "out".
// This always returns a newly allocated slice.
func MergeEnvLists(in, out []string) []string {
out = append([]string(nil), out...)
NextVar:
for _, inkv := range in {
k := strings.SplitAfterN(inkv, "=", 2)[0]
for i, outkv := range out {
if strings.HasPrefix(outkv, k) {
out[i] = inkv
continue NextVar
}
}
out = append(out, inkv)
}
return out
}
33 changes: 33 additions & 0 deletions src/cmd/go/internal/base/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 base

import (
"cmd/go/internal/str"
"flag"
)

// A StringsFlag is a command-line flag that interprets its argument
// as a space-separated list of possibly-quoted strings.
type StringsFlag []string

func (v *StringsFlag) Set(s string) error {
var err error
*v, err = str.SplitQuotedFields(s)
if *v == nil {
*v = []string{}
}
return err
}

func (v *StringsFlag) String() string {
return "<StringsFlag>"
}

// 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, "")
}
8 changes: 8 additions & 0 deletions src/cmd/go/internal/base/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package base
import (
"os"
"path/filepath"
"strings"
)

var Cwd, _ = os.Getwd()
Expand Down Expand Up @@ -34,3 +35,10 @@ func RelPaths(paths []string) []string {
}
return out
}

// IsTestFile reports whether the source file is a set of tests and should therefore
// be excluded from coverage analysis.
func IsTestFile(file string) bool {
// We don't cover tests, only the code they test.
return strings.HasSuffix(file, "_test.go")
}
44 changes: 44 additions & 0 deletions src/cmd/go/internal/str/str.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,47 @@ func Contains(x []string, s string) bool {
}
return false
}

func isSpaceByte(c byte) bool {
return c == ' ' || c == '\t' || c == '\n' || c == '\r'
}

// SplitQuotedFields splits s into a list of fields,
// allowing single or double quotes around elements.
// There is no unescaping or other processing within
// quoted fields.
func SplitQuotedFields(s string) ([]string, error) {
// Split fields allowing '' or "" around elements.
// Quotes further inside the string do not count.
var f []string
for len(s) > 0 {
for len(s) > 0 && isSpaceByte(s[0]) {
s = s[1:]
}
if len(s) == 0 {
break
}
// Accepted quoted string. No unescaping inside.
if s[0] == '"' || s[0] == '\'' {
quote := s[0]
s = s[1:]
i := 0
for i < len(s) && s[i] != quote {
i++
}
if i >= len(s) {
return nil, fmt.Errorf("unterminated %c string", quote)
}
f = append(f, s[:i])
s = s[i+1:]
continue
}
i := 0
for i < len(s) && !isSpaceByte(s[i]) {
i++
}
f = append(f, s[:i])
s = s[i:]
}
return f, nil
}
Loading

0 comments on commit 3c667ef

Please sign in to comment.