cmd/goimports: work around lack of runtime/trace in gccgo

Moves runtime/trace support (including its command line flag) behind
a "gc" build tag to allow goimports to build under gccgo, which does
not support runtime/trace.

Updates golang/go#15544.

Change-Id: I017a44089c0a4f3d3ba98815d57a141e25b3fe56
Reviewed-on: https://go-review.googlesource.com/26998
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Tamir Duberstein 2016-08-12 21:48:14 -04:00 committed by Brad Fitzpatrick
parent 68cf18548e
commit 0e9f43fcb6
3 changed files with 41 additions and 8 deletions

View File

@ -19,7 +19,6 @@ import (
"path/filepath"
"runtime"
"runtime/pprof"
"runtime/trace"
"strings"
"golang.org/x/tools/imports"
@ -36,7 +35,6 @@ var (
cpuProfile = flag.String("cpuprofile", "", "CPU profile output")
memProfile = flag.String("memprofile", "", "memory profile output")
memProfileRate = flag.Int("memrate", 0, "if > 0, sets runtime.MemProfileRate")
traceProfile = flag.String("trace", "", "trace profile output")
options = &imports.Options{
TabWidth: 8,
@ -227,12 +225,10 @@ func gofmtMain() {
defer flush()
defer pprof.StopCPUProfile()
}
if *traceProfile != "" {
bw, flush := bufferedFileWriter(*traceProfile)
trace.Start(bw)
defer flush()
defer trace.Stop()
}
// doTrace is a conditionally compiled wrapper around runtime/trace. It is
// used to allow goimports to compile under gccgo, which does not support
// runtime/trace. See https://golang.org/issue/15544.
defer doTrace()()
if *memProfileRate > 0 {
runtime.MemProfileRate = *memProfileRate
bw, flush := bufferedFileWriter(*memProfile)

View File

@ -0,0 +1,26 @@
// Copyright 2016 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.
// +build gc
package main
import (
"flag"
"runtime/trace"
)
var traceProfile = flag.String("trace", "", "trace profile output")
func doTrace() func() {
if *traceProfile != "" {
bw, flush := bufferedFileWriter(*traceProfile)
trace.Start(bw)
return func() {
flush()
trace.Stop()
}
}
return func() {}
}

View File

@ -0,0 +1,11 @@
// Copyright 2016 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.
// +build !gc
package main
func doTrace() func() {
return func() {}
}