Skip to content

Commit

Permalink
cmd/goimports: work around lack of runtime/trace in gccgo
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
tamird authored and bradfitz committed Aug 13, 2016
1 parent 68cf185 commit 0e9f43f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
12 changes: 4 additions & 8 deletions cmd/goimports/goimports.go
Expand Up @@ -19,7 +19,6 @@ import (
"path/filepath"
"runtime"
"runtime/pprof"
"runtime/trace"
"strings"

"golang.org/x/tools/imports"
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions cmd/goimports/goimports_gc.go
@@ -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() {}
}
11 changes: 11 additions & 0 deletions cmd/goimports/goimports_not_gc.go
@@ -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() {}
}

0 comments on commit 0e9f43f

Please sign in to comment.