]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: delay gcc/clang flag support checks
authorRuss Cox <rsc@golang.org>
Fri, 27 Oct 2017 02:08:23 +0000 (22:08 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 27 Oct 2017 14:23:04 +0000 (14:23 +0000)
env.MkEnv was computing the full gcc command line to report as
$GOGCCFLAGS in "go env" output, which meant running gcc (or clang)
multiple times to discern which flags are available.
We also set $GOGCCFLAGS in the environment, but nothing actually uses
that as far as I can tell - it was always intended only for debugging.
Move GOGCCFLAGS to env.ExtraEnvVars, which displayed in "go env"
output but not set in child processes and not computed nearly as
often.

The effect is that trivial commands like "go help" or "go env GOARCH"
or "go tool -n compile" now run in about 0.01s instead of 0.1s,
because they no longer run gcc 4 times each.

go test -short cmd/go drops from 81s to 44s (and needs more trimming).

The $GOROOT/test suite drops from 92s to 33s, because the number of
gcc invocation drops from 13,336 to 0.

Overall, all.bash drops from 5m53s to 4m07s wall time.

Change-Id: Ia85abc89e1e2bb126b933aff3bf7c5f6c0984cd5
Reviewed-on: https://go-review.googlesource.com/73850
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/internal/envcmd/env.go
src/cmd/go/internal/work/exec.go

index c62a41901eb50a201adfc016de29c50a93e8b301..2e3e9d3973fd88dac5d2e5ae251d973d0375d56e 100644 (file)
@@ -73,11 +73,16 @@ func MkEnv() []cfg.EnvVar {
                env = append(env, cfg.EnvVar{Name: "GO386", Value: cfg.GO386})
        }
 
-       cmd := b.GccCmd(".", "")
-       env = append(env, cfg.EnvVar{Name: "CC", Value: cmd[0]})
-       env = append(env, cfg.EnvVar{Name: "GOGCCFLAGS", Value: strings.Join(cmd[3:], " ")})
-       cmd = b.GxxCmd(".", "")
-       env = append(env, cfg.EnvVar{Name: "CXX", Value: cmd[0]})
+       cc := cfg.DefaultCC
+       if env := strings.Fields(os.Getenv("CC")); len(env) > 0 {
+               cc = env[0]
+       }
+       cxx := cfg.DefaultCXX
+       if env := strings.Fields(os.Getenv("CXX")); len(env) > 0 {
+               cxx = env[0]
+       }
+       env = append(env, cfg.EnvVar{Name: "CC", Value: cc})
+       env = append(env, cfg.EnvVar{Name: "CXX", Value: cxx})
 
        if cfg.BuildContext.CgoEnabled {
                env = append(env, cfg.EnvVar{Name: "CGO_ENABLED", Value: "1"})
@@ -102,19 +107,45 @@ func ExtraEnvVars() []cfg.EnvVar {
        var b work.Builder
        b.Init()
        cppflags, cflags, cxxflags, fflags, ldflags := b.CFlags(&load.Package{})
+       cmd := b.GccCmd(".", "")
        return []cfg.EnvVar{
+               // Note: Update the switch in runEnv below when adding to this list.
                {Name: "CGO_CFLAGS", Value: strings.Join(cflags, " ")},
                {Name: "CGO_CPPFLAGS", Value: strings.Join(cppflags, " ")},
                {Name: "CGO_CXXFLAGS", Value: strings.Join(cxxflags, " ")},
                {Name: "CGO_FFLAGS", Value: strings.Join(fflags, " ")},
                {Name: "CGO_LDFLAGS", Value: strings.Join(ldflags, " ")},
                {Name: "PKG_CONFIG", Value: b.PkgconfigCmd()},
+               {Name: "GOGCCFLAGS", Value: strings.Join(cmd[3:], " ")},
        }
 }
 
 func runEnv(cmd *base.Command, args []string) {
        env := cfg.CmdEnv
-       env = append(env, ExtraEnvVars()...)
+
+       // Do we need to call ExtraEnvVars, which is a bit expensive?
+       // Only if we're listing all environment variables ("go env")
+       // or the variables being requested are in the extra list.
+       needExtra := true
+       if len(args) > 0 {
+               needExtra = false
+               for _, arg := range args {
+                       switch arg {
+                       case "CGO_CFLAGS",
+                               "CGO_CPPFLAGS",
+                               "CGO_CXXFLAGS",
+                               "CGO_FFLAGS",
+                               "CGO_LDFLAGS",
+                               "PKG_CONFIG",
+                               "GOGCCFLAGS":
+                               needExtra = true
+                       }
+               }
+       }
+       if needExtra {
+               env = append(env, ExtraEnvVars()...)
+       }
+
        if len(args) > 0 {
                if *envJson {
                        var es []cfg.EnvVar
index 385882b454a577e5744c264016db74f78d99a446..b652b71b4a9c214671fd025fb262587c367f6fc5 100644 (file)
@@ -1232,30 +1232,39 @@ func (b *Builder) gccld(p *load.Package, objdir, out string, flags []string, obj
        return b.run(p.Dir, p.ImportPath, nil, cmd, "-o", out, objs, flags)
 }
 
+// Grab these before main helpfully overwrites them.
+var (
+       origCC  = os.Getenv("CC")
+       origCXX = os.Getenv("CXX")
+)
+
 // gccCmd returns a gcc command line prefix
 // defaultCC is defined in zdefaultcc.go, written by cmd/dist.
 func (b *Builder) GccCmd(incdir, workdir string) []string {
-       return b.compilerCmd("CC", cfg.DefaultCC, incdir, workdir)
+       return b.compilerCmd(origCC, cfg.DefaultCC, incdir, workdir)
 }
 
 // gxxCmd returns a g++ command line prefix
 // defaultCXX is defined in zdefaultcc.go, written by cmd/dist.
 func (b *Builder) GxxCmd(incdir, workdir string) []string {
-       return b.compilerCmd("CXX", cfg.DefaultCXX, incdir, workdir)
+       return b.compilerCmd(origCXX, cfg.DefaultCXX, incdir, workdir)
 }
 
 // gfortranCmd returns a gfortran command line prefix.
 func (b *Builder) gfortranCmd(incdir, workdir string) []string {
-       return b.compilerCmd("FC", "gfortran", incdir, workdir)
+       return b.compilerCmd(os.Getenv("FC"), "gfortran", incdir, workdir)
 }
 
 // compilerCmd returns a command line prefix for the given environment
 // variable and using the default command when the variable is empty.
-func (b *Builder) compilerCmd(envvar, defcmd, incdir, workdir string) []string {
+func (b *Builder) compilerCmd(envValue, defcmd, incdir, workdir string) []string {
        // NOTE: env.go's mkEnv knows that the first three
        // strings returned are "gcc", "-I", incdir (and cuts them off).
 
-       compiler := envList(envvar, defcmd)
+       if envValue == "" {
+               envValue = defcmd
+       }
+       compiler := strings.Fields(envValue)
        a := []string{compiler[0], "-I", incdir}
        a = append(a, compiler[1:]...)