在 Bazel 中,如何防止某些 C++ 编译器标志传递给外部依赖项?
In Bazel, how to prevent some C++ compiler flags from passing to external dependencies?
我们的项目是用 C++ 编写的,并使用 gRPC 作为依赖项。我们使用 clang 作为编译器。我们使用 -Wall -Werror
设置了 C++ 工具链文件,但这导致了 gRPC 本身引发的警告问题。
有没有办法阻止 Bazel 将 Werror
标志应用到 gRPC 文件,但仍将其应用到项目的其他地方?
文件如下所示:
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", ...]
....
2020 年 9 月 2 日更新
基于Ondrej的非常帮助解决方案,我通过以下方式解决了这个问题。
- 从我拥有它的功能中删除
-Werror
标志(连同其他标志)并添加到默认情况下禁用的新功能中,如下所示:
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"]
然后,在我自己项目中的每个 BUILD 文件的顶部,添加这一行:
package(features = ["compile_flags_with_werror"])
这会在我的项目中编译文件时应用 -Werror
,但在编译任何外部依赖项时不会。
您可以定义工具链功能,例如:
warning_flags_feature = feature(
name = "warning_flags",
enabled = True,
flag_sets = [
flag_set(
actions = all_compile_actions,
flag_groups = [
flag_group(
flags = [
"-Wall",
"-Werror",
],
),
],
),
],
)
默认情况下是 enabled
并将其添加到 create_cc_toolchain_config_info()
的 features
以添加所需的标志(将它们从 default_compile_flags_feature
中删除)。
然后对于行为不当的外部依赖项,您可以在其 BUILD
文件中为整个包禁用该功能:
package(features = ["-warning_flags"])
或者在每个目标的基础上这样做:
cc_library(
name = "external_lib",
...
features = ["-warning_flags"],
)
我们的项目是用 C++ 编写的,并使用 gRPC 作为依赖项。我们使用 clang 作为编译器。我们使用 -Wall -Werror
设置了 C++ 工具链文件,但这导致了 gRPC 本身引发的警告问题。
有没有办法阻止 Bazel 将 Werror
标志应用到 gRPC 文件,但仍将其应用到项目的其他地方?
文件如下所示:
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", ...]
....
2020 年 9 月 2 日更新 基于Ondrej的非常帮助解决方案,我通过以下方式解决了这个问题。
- 从我拥有它的功能中删除
-Werror
标志(连同其他标志)并添加到默认情况下禁用的新功能中,如下所示:
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"]
然后,在我自己项目中的每个 BUILD 文件的顶部,添加这一行:
package(features = ["compile_flags_with_werror"])
这会在我的项目中编译文件时应用 -Werror
,但在编译任何外部依赖项时不会。
您可以定义工具链功能,例如:
warning_flags_feature = feature(
name = "warning_flags",
enabled = True,
flag_sets = [
flag_set(
actions = all_compile_actions,
flag_groups = [
flag_group(
flags = [
"-Wall",
"-Werror",
],
),
],
),
],
)
默认情况下是 enabled
并将其添加到 create_cc_toolchain_config_info()
的 features
以添加所需的标志(将它们从 default_compile_flags_feature
中删除)。
然后对于行为不当的外部依赖项,您可以在其 BUILD
文件中为整个包禁用该功能:
package(features = ["-warning_flags"])
或者在每个目标的基础上这样做:
cc_library(
name = "external_lib",
...
features = ["-warning_flags"],
)