如何解决在Bazel中,如何防止某些C ++编译器标志传递给外部依赖项?
我们的项目是用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",...]
....
UPDATE 9/2/2020 基于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
是哪个features
,并将其添加到create_cc_toolchain_config_info()
的{{1}}中以添加所需的标志(将其从default_compile_flags_feature
中删除)。
然后,由于外部依赖项异常,您可以在其BUILD
文件中为整个软件包禁用该功能:
package(features = ["-warning_flags"])
或者按目标进行:
cc_library(
name = "external_lib",...
features = ["-warning_flags"],)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。