[build] Support clang-cl for Windows builds (best effort) (#36831)

Usage: `bazel build --config=clang-cl --build_tag_filters=-no_windows :all`

The highlight, for me anyway: thread safety annotation checking can now be done on Windows-only code.

clang-cl's interpretation of `-Wall` differs from that of clang on linux. This PR uses the set of warnings enabled on linux, and whittles down the list until all builds pass. I left comments in `copts.bzl` describing the warnings, many of which can be removed with targeted code cleanups.

Closes #36831

PiperOrigin-RevId: 641009783
This commit is contained in:
AJ Heller 2024-06-06 13:50:54 -07:00 committed by Copybara-Service
parent 5d586d3ae3
commit da05edc802
7 changed files with 164 additions and 0 deletions

8
BUILD
View File

@ -19,6 +19,7 @@ load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load(
"//bazel:grpc_build_system.bzl",
"grpc_cc_library",
"grpc_clang_cl_settings",
"grpc_filegroup",
"grpc_generate_one_off_targets",
"grpc_upb_proto_library",
@ -66,6 +67,8 @@ bool_flag(
build_setting_default = False,
)
grpc_clang_cl_settings()
config_setting(
name = "grpc_no_rls_flag",
flag_values = {":disable_grpc_rls": "true"},
@ -216,6 +219,11 @@ config_setting(
values = {"define": "use_strict_warning=true"},
)
config_setting(
name = "use_strict_warning_windows",
values = {"define": "use_strict_warning_windows=true"},
)
python_config_settings()
# This should be updated along with build_handwritten.yaml

View File

@ -57,7 +57,104 @@ GRPC_LLVM_WARNING_FLAGS = [
"-Wno-unused-function",
]
GRPC_LLVM_WINDOWS_WARNING_FLAGS = GRPC_LLVM_WARNING_FLAGS + [
### Some checks that are missing with clang-cl
"-Wthread-safety",
"-Wreorder-ctor",
### Avoid some checks that are default on clang-cl
"-Wno-c++98-compat-pedantic",
"-Wno-missing-prototypes",
"-Wno-thread-safety-precise", # too many aliases
# abseil offenses
"-Wno-comma",
"-Wno-deprecated-redundant-constexpr-static-def",
"-Wno-deprecated", # remove when the above works in all clang versions we test with
"-Wno-float-equal",
"-Wno-gcc-compat",
"-Wno-reserved-identifier",
"-Wno-thread-safety-negative",
"-Wno-sign-compare",
# re2 offenses
"-Wno-zero-as-null-pointer-constant",
# ares offenses
"-Wno-macro-redefined",
# protobuf offenses
"-Wno-cast-align",
"-Wno-inconsistent-missing-destructor-override",
# xxhash offenses
"-Wno-disabled-macro-expansion",
# benchmark offenses
"-Wno-shift-sign-overflow",
# Evidently nodiscard is inappropriately pinned as a C++17 feature
"-Wno-c++17-attribute-extensions",
# declarations are not used in many places
"-Wno-missing-variable-declarations",
# TODO: delete iomgr
"-Wno-old-style-cast",
"-Wno-cast-qual",
"-Wno-unused-member-function",
"-Wno-unused-template",
# TODO(hork): see if the TraceFlag offense can be removed
"-Wno-global-constructors",
# TODO(hork): clean up EE offenses
"-Wno-missing-field-initializers",
"-Wno-non-virtual-dtor",
# TODO(ctiller): offense: dump_args. signed to unsigned
"-Wno-sign-conversion",
"-Wno-shorten-64-to-32",
# TODO: general cleanup required. Maybe new developer or rainy day projects.
"-Wno-unreachable-code-break",
"-Wno-unreachable-code-return",
"-Wno-unreachable-code",
"-Wno-used-but-marked-unused",
"-Wno-newline-eof",
"-Wno-unused-const-variable",
"-Wno-extra-semi",
"-Wno-extra-semi-stmt",
"-Wno-suggest-destructor-override",
"-Wno-shadow",
"-Wno-missing-noreturn",
"-Wno-nested-anon-types",
"-Wno-gnu-anonymous-struct",
"-Wno-nonportable-system-include-path",
"-Wno-microsoft-cast",
"-Wno-exit-time-destructors",
"-Wno-undef", # #if <MACRO_NAME> to #ifdef <MACRO_NAME>
"-Wno-unused-macros",
"-Wno-redundant-parens",
"-Wno-undefined-func-template",
"-Wno-gnu-zero-variadic-macro-arguments",
"-Wno-double-promotion",
"-Wno-implicit-float-conversion",
"-Wno-implicit-int-conversion",
"-Wno-float-conversion",
"-Wno-unused-parameter",
"-Wno-suggest-override",
"-Wno-documentation",
"-Wno-documentation-unknown-command",
### possibly bad warnings for this codebase
"-Wno-covered-switch-default",
"-Wno-switch-enum",
"-Wno-c99-extensions",
"-Wno-unused-private-field", # GRPC_UNUSED does not appear to work for private fields
]
GRPC_DEFAULT_COPTS = select({
"//:use_strict_warning": GRPC_LLVM_WARNING_FLAGS + ["-DUSE_STRICT_WARNING=1"],
"//:use_strict_warning_windows": GRPC_LLVM_WINDOWS_WARNING_FLAGS + ["-DUSE_STRICT_WARNING=1"],
"//conditions:default": [],
})

View File

@ -45,6 +45,7 @@ def if_not_windows(a):
return select({
"//:windows": [],
"//:windows_msvc": [],
"//:windows_clang": [],
"//conditions:default": a,
})
@ -52,6 +53,7 @@ def if_windows(a):
return select({
"//:windows": a,
"//:windows_msvc": a,
"//:windows_clang": a,
"//conditions:default": [],
})
@ -802,3 +804,22 @@ def python_config_settings():
name = "python3",
flag_values = {"@bazel_tools//tools/python:python_version": "PY3"},
)
# buildifier: disable=unnamed-macro
def grpc_clang_cl_settings():
native.platform(
name = "x64_windows-clang-cl",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
"@bazel_tools//tools/cpp:clang-cl",
],
)
native.config_setting(
name = "windows_clang",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
"@bazel_tools//tools/cpp:clang-cl",
],
)

View File

@ -222,6 +222,7 @@ def grpc_deps():
],
patches = [
"@com_github_grpc_grpc//third_party:protobuf.patch",
"@com_github_grpc_grpc//third_party:protobuf.10007.patch",
],
patch_args = ["-p1"],
)

View File

@ -2733,6 +2733,7 @@ grpc_cc_library(
{
"//:windows": ["windows_event_engine"],
"//:windows_msvc": ["windows_event_engine"],
"//:windows_clang": ["windows_event_engine"],
"//:windows_other": ["windows_event_engine"],
"//:mac": [
"posix_event_engine",

30
third_party/protobuf.10007.patch vendored Normal file
View File

@ -0,0 +1,30 @@
diff --git a/build_defs/BUILD.bazel b/build_defs/BUILD.bazel
index 1c72eba68..176ab5a91 100644
--- a/build_defs/BUILD.bazel
+++ b/build_defs/BUILD.bazel
@@ -14,10 +14,24 @@ package(
)
create_compiler_config_setting(
- name = "config_msvc",
+ name = "config_msvc_cl",
value = "msvc-cl",
)
+# Caveat: clang-cl support in protobuf is only best-effort / untested for now.
+create_compiler_config_setting(
+ name = "config_clang_cl",
+ value = "clang-cl",
+)
+
+selects.config_setting_group(
+ name = "config_msvc",
+ match_any = [
+ ":config_clang_cl",
+ ":config_msvc_cl",
+ ],
+)
+
config_setting(
name = "aarch64",
values = {"cpu": "linux-aarch_64"},

View File

@ -44,6 +44,12 @@ build:macos --dynamic_mode=off
build:windows_opt --compilation_mode=opt
build:windows_dbg --compilation_mode=dbg
build:clang-cl --extra_toolchains=@local_config_cc//:cc-toolchain-x64_windows-clang-cl
build:clang-cl --extra_execution_platforms=//:x64_windows-clang-cl
build:clang-cl --incompatible_enable_cc_toolchain_resolution
build:clang-cl --define=use_strict_warning_windows=true
build:clang-cl --compiler=clang-cl
build:asan --strip=never
build:asan --copt=-fsanitize=address
build:asan --copt=-O0