]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: ensure GCC does not use ANSI escape sequences in errors
authorBenjamin Barenblat <bbaren@google.com>
Thu, 27 Aug 2020 20:12:18 +0000 (16:12 -0400)
committerIan Lance Taylor <iant@golang.org>
Thu, 27 Aug 2020 21:13:46 +0000 (21:13 +0000)
cgo parses GCC’s error messages to classify C identifiers referenced
from Go programs (are they integer constants? type names?). If GCC tries
to colorize its errors, cgo can’t figure out what GCC is saying. GCC
avoids escape sequences in this scenario by default, but the default
behavior can be overridden in at least two places:

  - The user can set `CGO_COPTS=-fdiagnostics-color`.

  - Whoever compiled GCC can configure GCC itself to always colorize
    output.

The most reliable way to ensure that GCC doesn’t colorize output is to
append `-fdiagnostics-color=never` to the GCC command line; do so.

Fixes #40415

Change-Id: Id4bdf8d92fac8b038340b4264f726e8fe38875b4
Reviewed-on: https://go-review.googlesource.com/c/go/+/248398
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/cgo/gcc.go
src/cmd/dist/test.go

index a59534ebd0aee083a17665df0c2ac8f25df70f6f..9179b5490e2bc41d1320f8bcced6718707f8da04 100644 (file)
@@ -369,7 +369,18 @@ func (p *Package) guessKinds(f *File) []*Name {
        fmt.Fprintf(&b, "#line 1 \"completed\"\n"+
                "int __cgo__1 = __cgo__2;\n")
 
-       stderr := p.gccErrors(b.Bytes())
+       // We need to parse the output from this gcc command, so ensure that it
+       // doesn't have any ANSI escape sequences in it. (TERM=dumb is
+       // insufficient; if the user specifies CGO_CFLAGS=-fdiagnostics-color,
+       // GCC will ignore TERM, and GCC can also be configured at compile-time
+       // to ignore TERM.)
+       stderr := p.gccErrors(b.Bytes(), "-fdiagnostics-color=never")
+       if strings.Contains(stderr, "unrecognized command line option") {
+               // We're using an old version of GCC that doesn't understand
+               // -fdiagnostics-color. Those versions can't print color anyway,
+               // so just rerun without that option.
+               stderr = p.gccErrors(b.Bytes())
+       }
        if stderr == "" {
                fatalf("%s produced no output\non input:\n%s", p.gccBaseCmd()[0], b.Bytes())
        }
@@ -1970,22 +1981,25 @@ func (p *Package) gccDefines(stdin []byte) string {
 // gccErrors runs gcc over the C program stdin and returns
 // the errors that gcc prints. That is, this function expects
 // gcc to fail.
-func (p *Package) gccErrors(stdin []byte) string {
+func (p *Package) gccErrors(stdin []byte, extraArgs ...string) string {
        // TODO(rsc): require failure
        args := p.gccCmd()
 
        // Optimization options can confuse the error messages; remove them.
-       nargs := make([]string, 0, len(args))
+       nargs := make([]string, 0, len(args)+len(extraArgs))
        for _, arg := range args {
                if !strings.HasPrefix(arg, "-O") {
                        nargs = append(nargs, arg)
                }
        }
 
-       // Force -O0 optimization but keep the trailing "-" at the end.
-       nargs = append(nargs, "-O0")
-       nl := len(nargs)
-       nargs[nl-2], nargs[nl-1] = nargs[nl-1], nargs[nl-2]
+       // Force -O0 optimization and append extra arguments, but keep the
+       // trailing "-" at the end.
+       li := len(nargs) - 1
+       last := nargs[li]
+       nargs[li] = "-O0"
+       nargs = append(nargs, extraArgs...)
+       nargs = append(nargs, last)
 
        if *debugGcc {
                fmt.Fprintf(os.Stderr, "$ %s <<EOF\n", strings.Join(nargs, " "))
index a83ae352936654db002f3dd809ad389476680a91..5ea5c81656def7f707afd8ea9f0dbeedd075a5c5 100644 (file)
@@ -1106,8 +1106,9 @@ func (t *tester) cgoTest(dt *distTest) error {
 
                cmd := t.addCmd(dt, "misc/cgo/test", t.goTest())
                cmd.Env = append(os.Environ(), "GOFLAGS=-ldflags=-linkmode=external")
-               // A -g argument in CGO_CFLAGS should not affect how the test runs.
-               cmd.Env = append(cmd.Env, "CGO_CFLAGS=-g0")
+               // cgo should be able to cope with both -g arguments and colored
+               // diagnostics.
+               cmd.Env = append(cmd.Env, "CGO_CFLAGS=-g0 -fdiagnostics-color")
 
                t.addCmd(dt, "misc/cgo/testtls", t.goTest(), "-ldflags", "-linkmode=auto")
                t.addCmd(dt, "misc/cgo/testtls", t.goTest(), "-ldflags", "-linkmode=external")