Skip to content

Commit f802525

Browse files
jheaff1copybara-github
authored andcommitted
Add warnings as errors cc feature
This PR adds a `feature` to cc toolchains to treat warnings as errors. The `feature` is disabled by default. Closes #11550 Closes #13927. PiperOrigin-RevId: 423364251
1 parent cfaecc5 commit f802525

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

tools/cpp/unix_cc_configure.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
532532
# We need to undef it as some distributions now have it enabled by default.
533533
"-U_FORTIFY_SOURCE",
534534
"-fstack-protector",
535-
# All warnings are enabled. Maybe enable -Werror as well?
535+
# All warnings are enabled.
536536
"-Wall",
537537
# Enable a few more warnings that aren't part of -Wall.
538538
] + ((

tools/cpp/unix_cc_toolchain_config.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,20 @@ def _impl(ctx):
11821182
],
11831183
)
11841184

1185+
treat_warnings_as_errors_feature = feature(
1186+
name = "treat_warnings_as_errors",
1187+
flag_sets = [
1188+
flag_set(
1189+
actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
1190+
flag_groups = [flag_group(flags = ["-Werror"])],
1191+
),
1192+
flag_set(
1193+
actions = all_link_actions,
1194+
flag_groups = [flag_group(flags = ["-Wl,-fatal-warnings"])],
1195+
),
1196+
],
1197+
)
1198+
11851199
is_linux = ctx.attr.target_libc != "macosx"
11861200

11871201
# TODO(#8303): Mac crosstool should also declare every feature.
@@ -1234,6 +1248,7 @@ def _impl(ctx):
12341248
user_compile_flags_feature,
12351249
sysroot_feature,
12361250
unfiltered_compile_flags_feature,
1251+
treat_warnings_as_errors_feature,
12371252
] + layering_check_features(ctx.attr.compiler)
12381253
else:
12391254
# macOS artifact name patterns differ from the defaults only for dynamic
@@ -1263,6 +1278,7 @@ def _impl(ctx):
12631278
user_compile_flags_feature,
12641279
sysroot_feature,
12651280
unfiltered_compile_flags_feature,
1281+
treat_warnings_as_errors_feature,
12661282
] + layering_check_features(ctx.attr.compiler)
12671283

12681284
return cc_common.create_cc_toolchain_config_info(

tools/cpp/windows_cc_toolchain_config.bzl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ def _impl(ctx):
786786
name = "treat_warnings_as_errors",
787787
flag_sets = [
788788
flag_set(
789-
actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
789+
actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile] + all_link_actions,
790790
flag_groups = [flag_group(flags = ["/WX"])],
791791
),
792792
],
@@ -1271,6 +1271,20 @@ def _impl(ctx):
12711271
provides = ["profile"],
12721272
)
12731273

1274+
treat_warnings_as_errors_feature = feature(
1275+
name = "treat_warnings_as_errors",
1276+
flag_sets = [
1277+
flag_set(
1278+
actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
1279+
flag_groups = [flag_group(flags = ["-Werror"])],
1280+
),
1281+
flag_set(
1282+
actions = all_link_actions,
1283+
flag_groups = [flag_group(flags = ["-Wl,-fatal-warnings"])],
1284+
),
1285+
],
1286+
)
1287+
12741288
user_compile_flags_feature = feature(
12751289
name = "user_compile_flags",
12761290
enabled = True,
@@ -1311,6 +1325,7 @@ def _impl(ctx):
13111325
dbg_feature,
13121326
opt_feature,
13131327
user_compile_flags_feature,
1328+
treat_warnings_as_errors_feature,
13141329
sysroot_feature,
13151330
]
13161331

0 commit comments

Comments
 (0)