From a279a1866e39c193a42655379219d7cb5fbfa2b6 Mon Sep 17 00:00:00 2001 From: Austin Schuh Date: Mon, 24 Jun 2019 12:03:42 -0700 Subject: [PATCH] Fix cgo build failures with specific GCC flags (#2097) cgo runs the compiler over a test file as part of the build process. It then parses the compiler errors. This requires the compiler to be configured well enough to work for cgo. -fdiagnostics-color is another way to enable color support on GCC. This adds colors unconditionally to the output (bazel doesn't present a TTY, so to get colors with bazel, you need to force them on). -fmax-errors=... is a way to prevent GCC from completely flooding your terminal when something goes wrong. A missed semicolon or misplaced {} can cause thousands of lines of errors. This can stop showing errors before cgo sees all the errors it is looking for. We want to strip it out too. --- go/private/context.bzl | 20 +++++++++++++++++++- go/private/rules/cgo.bzl | 5 ++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/go/private/context.bzl b/go/private/context.bzl index 46ff54b8bb..e9ab9d56cc 100644 --- a/go/private/context.bzl +++ b/go/private/context.bzl @@ -69,7 +69,15 @@ GoContext = provider() _GoContextData = provider() _COMPILER_OPTIONS_BLACKLIST = { + # cgo parses the error messages from the compiler. It can't handle colors. + # Ignore both variants of the diagnostics color flag. "-fcolor-diagnostics": None, + "-fdiagnostics-color": None, + + # cgo also wants to see all the errors when it is testing the compiler. + # fmax-errors limits that and causes build failures. + "-fmax-errors=": None, + "-Wall": None, # Symbols are needed by Go, so keep them @@ -86,8 +94,18 @@ _LINKER_OPTIONS_BLACKLIST = { "-Wl,--gc-sections": None, } +def _match_option(option, pattern): + if pattern.endswith("="): + return option.startswith(pattern) + else: + return option == pattern + def _filter_options(options, blacklist): - return [option for option in options if option not in blacklist] + return [ + option + for option in options + if not any([_match_option(option, pattern) for pattern in blacklist]) + ] def _child_name(go, path, ext, name): childname = mode_string(go.mode) + "/" diff --git a/go/private/rules/cgo.bzl b/go/private/rules/cgo.bzl index c4370909a3..abe91c3fc5 100644 --- a/go/private/rules/cgo.bzl +++ b/go/private/rules/cgo.bzl @@ -795,7 +795,10 @@ def setup_cgo_library_for_mode(name, srcs, cdeps, copts, cxxopts, cppopts, clink name = cgo_o_name, srcs = [select_main_c], deps = cdeps + cgo_o_deps, - copts = copts + cppopts, + copts = copts + cppopts + [ + # The generated thunks often contain unused variables. + "-Wno-unused-variable", + ], linkopts = clinkopts, visibility = ["//visibility:private"], **common_attrs