diff --git a/cmd/goimports/goimports.go b/cmd/goimports/goimports.go index fc61ac26..cf45e49b 100644 --- a/cmd/goimports/goimports.go +++ b/cmd/goimports/goimports.go @@ -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) diff --git a/cmd/goimports/goimports_gc.go b/cmd/goimports/goimports_gc.go new file mode 100644 index 00000000..21d867ea --- /dev/null +++ b/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() {} +} diff --git a/cmd/goimports/goimports_not_gc.go b/cmd/goimports/goimports_not_gc.go new file mode 100644 index 00000000..f5531ceb --- /dev/null +++ b/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() {} +}