5

Our project is written in C++ and uses gRPC as a dependency. We are using clang as compiler. We set up the C++ toolchain file with -Wall -Werror, but this is causing issues with the warnings raised by gRPC itself.

Is there a way to prevent Bazel from applying the Werror flag to the gRPC files, but still apply it elsewhere in the project?

The files look like this:

WORKSPACE:
git_repository(
  name = "com_github_grpc_grpc",
  remote = "https://github.com/grpc/grpc",
  ...
)
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()
load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
grpc_extra_deps()
...


BUILD:
cc_binary(
  name = "one_of_many_binaries",
  srcs = ["source_1.cc"],
  deps = ["@com_github_grpc_grpc//:grpc++", 
         ...],
)
...


cc_toolchain_config.bzl:
default_compile_flags_feature = feature(
        name = "default_compile_flags",
        enabled = True,
        flag_sets = [
            flag_set(
                actions = all_compile_actions,
                flag_groups = [
                    flag_group(
                        flags = ["-Wall", "-Werror", ...]
....


UPDATE 9/2/2020 Based on Ondrej's very help solution, I've solved this issue in the following way.

  • Remove the -Werror flag from the feature in which I had it (along with the other flags) and into a new feature, which is disabled by default, like this:
compile_flags_with_werror = feature(
        name = "compile_flags_with_werror",
        enabled = False, #this is important
        flag_sets = [
            flag_set(
                actions = all_compile_actions,
                flag_groups = [
                    flag_group(
                        flags = ["-Werror"]

Then, at the top of each of the BUILD files in my own project, add this line:

package(features = ["compile_flags_with_werror"])

This has the effect of applying -Werror when compiling files in my project, but not when compiling any external dependencies.

user3856970
  • 319
  • 1
  • 3
  • 11

1 Answers1

1

You can define toolchain feature such as:

warning_flags_feature = feature(
    name = "warning_flags",
    enabled = True,
    flag_sets = [
        flag_set(
            actions = all_compile_actions,
            flag_groups = [
                flag_group(
                    flags = [
                        "-Wall",
                        "-Werror",
                    ],
                ),
            ],
        ),
    ],
)        

Which is enabled by default and add it to features of create_cc_toolchain_config_info() to add the desired flags (removing them from your default_compile_flags_feature).

Then for the misbehaving external dependencies, you can disable the feature for an entire package in its BUILD file:

package(features = ["-warning_flags"])

Or do so on per target basis:

cc_library(
    name = "external_lib",
    ...
    features = ["-warning_flags"],
)
Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
  • Thank you very much! Do I need to do anything in the BUILD file so that it's aware of the feature name? I've added it to create_cc_toolchain_config_info() as suggested, but it seems the BUILD file doesn't know the feature names, or at least putting things here has no effect. I can even put non-existing feature names in package() and no errors are raised. – user3856970 Aug 27 '20 at 13:50
  • No, you should not need to do anything special, but note the string used in the `features` list is the `name` used for the `feature`. If `enabled = True` and included in the list of `features` of your cc toolchain it's enabled when that toolchain is used and `-` prefix to the name means to not use the feature for given package/target. Try cross-checking these points and if it still gives you trouble, I could give you some minimal config example (I've actually tried and checked the above lines to work). – Ondrej K. Aug 27 '20 at 15:07
  • If you don't mind, I keep having trouble with this. It looks like we're on the right track, but it seems that these features do not 'recurse' into the `deps` for a library or binary. So the flags get applied at the time library/binary is compiled, but NOT when its dependencies are compiled. Do you have any suggestions for that? Thank you very much. – user3856970 Sep 02 '20 at 17:58
  • To clarify, I can't add the feature to the BUILD file of the stuff in `deps` because those dependencies are an external library (grpc, protobuf, etc.) – user3856970 Sep 02 '20 at 18:01
  • I see what you're after. That is correct, explicit enabling / disabling of features is effective locally (for given package or target) and passed along with dependencies. I can imagine your own code compiling against third party (headers) breaking your desired warnings config, but I am afraid affected targets need to (also) be explicitly configured for greater leniency. I am afraid I do not have a more convenient idea at the moment. – Ondrej K. Sep 02 '20 at 21:00
  • No problem and thank you very much. This has been very helpful. I'll use a workaround based on your solution. – user3856970 Sep 02 '20 at 22:35
  • If I understand this correctly, when working with a "bazlified" dependency, like grpc, there's no straightforward way to turn off a feature ("-Werror", or "-D_FORTIFY_SOURCE=1") for that dependency. Has anyone tried supplying a passthrough "BUILD" file with a `package(...)` at the top? Is that too complicated? Does it even work? – Zendel Sep 14 '22 at 01:18