Skip to content

Commit

Permalink
SERVER-80623 Support --disable-warnings-as-error on Linux/Windows in …
Browse files Browse the repository at this point in the history
…Bazel (#23330)

GitOrigin-RevId: d0787b7e4d6c6d76582897eddd0616a72faa7483
  • Loading branch information
zackwintermdb authored and MongoDB Bot committed Jun 11, 2024
1 parent 7723ea8 commit ddc5c64
Show file tree
Hide file tree
Showing 23 changed files with 302 additions and 23 deletions.
8 changes: 8 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ build --experimental_cc_shared_library
# toolchain_suite elsewhere.
build:linux --crosstool_top=@mongo_toolchain//:toolchain_suite

# Disable the default compiler flags to avoid certain flags that conflict with flags we setup.
build:windows --features=-smaller_binary
build:windows --features=-frame_pointer
build:windows --features=-opt
build:windows --features=-dbg
build:windows --features=-fastbuild
build:windows --features=-dynamic_link_msvcrt

# Dynamic linking on Windows (DLL generation) is currently not supported.
build:windows --//bazel/config:linkstatic=True

Expand Down
Empty file added abc.cc
Empty file.
56 changes: 56 additions & 0 deletions bazel/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ load(
"compiler_type",
"dbg",
"detect_odr_violations",
"disable_warnings_as_errors",
"dwarf_version",
"fsan",
"http_client",
Expand Down Expand Up @@ -1618,3 +1619,58 @@ selects.config_setting_group(
":dwarf_version_5",
],
)

# --------------------------------------
# disable_warnings_as_errors
# --------------------------------------

disable_warnings_as_errors(
name = "disable_warnings_as_errors",
build_setting_default = False,
)

config_setting(
name = "warnings_as_errors_disabled",
flag_values = {
"//bazel/config:disable_warnings_as_errors": "True",
},
)

config_setting(
name = "warnings_as_errors_enabled",
flag_values = {
"//bazel/config:disable_warnings_as_errors": "False",
},
)

selects.config_setting_group(
name = "disable_warnings_as_errors_linux",
match_all = [
"@platforms//os:linux",
":warnings_as_errors_enabled",
],
)

selects.config_setting_group(
name = "disable_warnings_as_errors_posix",
match_all = [
":not_windows",
":warnings_as_errors_enabled",
],
)

selects.config_setting_group(
name = "disable_warnings_as_errors_macos",
match_all = [
"@platforms//os:macos",
":warnings_as_errors_enabled",
],
)

selects.config_setting_group(
name = "disable_warnings_as_errors_windows",
match_all = [
"@platforms//os:windows",
":warnings_as_errors_enabled",
],
)
14 changes: 14 additions & 0 deletions bazel/config/configs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,17 @@ dwarf_version = rule(
implementation = dwarf_version_impl,
build_setting = config.string(flag = True),
)

# =========
# disable-warnings-as-errors
# =========

disable_warnings_as_errors_provider = provider(
doc = """Don't add a warnings-as-errors flag to compiler command lines""",
fields = ["enabled"],
)

disable_warnings_as_errors = rule(
implementation = lambda ctx: disable_warnings_as_errors_provider(enabled = ctx.build_setting_value),
build_setting = config.bool(flag = True),
)
48 changes: 38 additions & 10 deletions bazel/mongo_src_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,6 @@ GCC_OR_CLANG_WARNINGS_COPTS = select({
# harmful to capture unused variables we are suppressing for now with a plan to fix later.
"-Wno-unused-lambda-capture",

# Enable sized deallocation support.
"-fsized-deallocation",

# This warning was added in Apple clang version 11 and flags many explicitly defaulted move
# constructors and assignment operators for being implicitly deleted, which is not useful.
"-Wno-defaulted-function-deleted",
Expand Down Expand Up @@ -1003,6 +1000,34 @@ DEDUPE_SYMBOL_LINKFLAGS = select({
"//conditions:default": [],
})

DISABLE_SOURCE_WARNING_AS_ERRORS_COPTS = select({
"//bazel/config:disable_warnings_as_errors_linux": ["-Werror"],
# TODO(SERVER-90183): Enable once MacOS has a custom Bazel toolchain config.
# "//bazel/config:disable_warnings_as_errors_macos": ["-Werror"],
"//bazel/config:disable_warnings_as_errors_windows": ["/WX"],
"//bazel/config:warnings_as_errors_disabled": [],
"//conditions:default": [],
})

# Enable sized deallocation support.
# Bazel doesn't allow for defining C++-only flags without a custom toolchain config. This is setup
# in the Linux toolchain, but currently there is no custom MacOS toolchain. Enabling warnings-as-errors will fail
# the build if this flag is passed to the compiler when building C code.
# Define it here on MacOS only to allow us to configure warnings-as-errors on Linux.
# TODO(SERVER-90183): Remove this once custom toolchain configuration is implemented on MacOS.
FSIZED_DEALLOCATION_COPT = select({
"@platforms//os:macos": ["-fsized-deallocation"],
"//conditions:default": [],
})

DISABLE_SOURCE_WARNING_AS_ERRORS_LINKFLAGS = select({
"//bazel/config:disable_warnings_as_errors_linux": ["-Wl,--fatal-warnings"],
# TODO(SERVER-90183): Enable once MacOS has a custom Bazel toolchain config.
# "//bazel/config:disable_warnings_as_errors_macos": ["-Wl,-fatal_warnings"],
"//bazel/config:warnings_as_errors_disabled": [],
"//conditions:default": [],
})

MTUNE_MARCH_COPTS = select({
# If we are enabling vectorization in sandybridge mode, we'd
# rather not hit the 256 wide vector instructions because the
Expand Down Expand Up @@ -1045,15 +1070,16 @@ MONGO_GLOBAL_COPTS = MONGO_GLOBAL_INCLUDE_DIRECTORIES + WINDOWS_COPTS + LIBCXX_C
GCC_OR_CLANG_WARNINGS_COPTS + GCC_OR_CLANG_GENERAL_COPTS + \
FLOATING_POINT_COPTS + MACOS_WARNINGS_COPTS + CLANG_WARNINGS_COPTS + \
CLANG_FNO_LIMIT_DEBUG_INFO + COMPRESS_DEBUG_COPTS + DEBUG_TYPES_SECTION_FLAGS + \
IMPLICIT_FALLTHROUGH_COPTS + MTUNE_MARCH_COPTS
IMPLICIT_FALLTHROUGH_COPTS + MTUNE_MARCH_COPTS + DISABLE_SOURCE_WARNING_AS_ERRORS_COPTS + \
FSIZED_DEALLOCATION_COPT

MONGO_GLOBAL_LINKFLAGS = MEMORY_SANITIZER_LINKFLAGS + ADDRESS_SANITIZER_LINKFLAGS + FUZZER_SANITIZER_LINKFLAGS + \
UNDEFINED_SANITIZER_LINKFLAGS + THREAD_SANITIZER_LINKFLAGS + \
LIBCXX_LINKFLAGS + LINKER_LINKFLAGS + DETECT_ODR_VIOLATIONS_LINKFLAGS + WINDOWS_LINKFLAGS + \
BIND_AT_LOAD_LINKFLAGS + RDYNAMIC_LINKFLAG + LINUX_PTHREAD_LINKFLAG + \
EXTRA_GLOBAL_LIBS_LINKFLAGS + ANY_SANITIZER_AVAILABLE_LINKFLAGS + ANY_SANITIZER_GCC_LINKFLAGS + \
GCC_OR_CLANG_LINKFLAGS + COMPRESS_DEBUG_LINKFLAGS + DEDUPE_SYMBOL_LINKFLAGS + \
DEBUG_TYPES_SECTION_FLAGS
DEBUG_TYPES_SECTION_FLAGS + DISABLE_SOURCE_WARNING_AS_ERRORS_LINKFLAGS

MONGO_GLOBAL_ACCESSIBLE_HEADERS = ["//src/third_party/boost:headers", "//src/third_party/immer:headers"]

Expand Down Expand Up @@ -1158,7 +1184,8 @@ def mongo_cc_library(
skip_global_deps = [],
non_transitive_dyn_linkopts = [],
defines = [],
additional_linker_inputs = []):
additional_linker_inputs = [],
features = []):
"""Wrapper around cc_library.
Args:
Expand Down Expand Up @@ -1250,7 +1277,7 @@ def mongo_cc_library(
local_defines = MONGO_GLOBAL_DEFINES + visibility_support_defines + local_defines,
defines = defines,
includes = includes,
features = MONGO_GLOBAL_FEATURES + ["supports_pic", "pic"],
features = MONGO_GLOBAL_FEATURES + ["supports_pic", "pic"] + features,
target_compatible_with = select({
"//bazel/config:shared_archive_enabled": [],
"//conditions:default": ["@platforms//:incompatible"],
Expand All @@ -1277,7 +1304,7 @@ def mongo_cc_library(
"//bazel/config:linkstatic_disabled": ["supports_pic", "pic"],
"//bazel/config:shared_archive_enabled": ["supports_pic", "pic"],
"//conditions:default": ["pie"],
}),
}) + features,
target_compatible_with = target_compatible_with,
additional_linker_inputs = additional_linker_inputs,
)
Expand Down Expand Up @@ -1330,7 +1357,8 @@ def mongo_cc_binary(
local_defines = [],
target_compatible_with = [],
defines = [],
additional_linker_inputs = []):
additional_linker_inputs = [],
features = []):
"""Wrapper around cc_binary.
Args:
Expand Down Expand Up @@ -1392,7 +1420,7 @@ def mongo_cc_binary(
local_defines = MONGO_GLOBAL_DEFINES + LIBUNWIND_DEFINES + local_defines,
defines = defines,
includes = includes,
features = MONGO_GLOBAL_FEATURES + ["pie"],
features = MONGO_GLOBAL_FEATURES + ["pie"] + features,
dynamic_deps = select({
"//bazel/config:linkstatic_disabled": deps,
"//conditions:default": [],
Expand Down
50 changes: 48 additions & 2 deletions bazel/toolchains/mongo_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,6 @@ def _impl(ctx):
enabled = False,
flag_sets = [
flag_set(
# This needs to only be set in the cpp compile actions to avoid generating debug info when
# building assembly files since the assembler doesn't support gdwarf64.
actions = all_cpp_compile_actions,
flag_groups = [flag_group(flags = ["-gdwarf64"])],
),
Expand All @@ -546,6 +544,50 @@ def _impl(ctx):
],
)

no_warn_non_virtual_detour_feature = feature(
name = "no_warn_non_virtual_detour",
enabled = False,
flag_sets = [
flag_set(
actions = all_cpp_compile_actions,
flag_groups = [flag_group(flags = ["-Wno-non-virtual-dtor"])],
),
],
)

no_deprecated_enum_enum_conversion_feature = feature(
name = "no_deprecated_enum_enum_conversion",
enabled = False,
flag_sets = [
flag_set(
actions = all_cpp_compile_actions,
flag_groups = [flag_group(flags = ["-Wno-deprecated-enum-enum-conversion"])],
),
],
)

no_volatile_feature = feature(
name = "no_volatile",
enabled = False,
flag_sets = [
flag_set(
actions = all_cpp_compile_actions,
flag_groups = [flag_group(flags = ["-Wno-volatile"])],
),
],
)

fsized_deallocation_feature = feature(
name = "fsized_deallocation",
enabled = True,
flag_sets = [
flag_set(
actions = all_cpp_compile_actions,
flag_groups = [flag_group(flags = ["-fsized-deallocation"])],
),
],
)

features = [
bin_dirs_feature,
default_compile_flags_feature,
Expand Down Expand Up @@ -575,6 +617,10 @@ def _impl(ctx):
dwarf5_feature,
dwarf32_feature,
dwarf64_feature,
no_warn_non_virtual_detour_feature,
no_deprecated_enum_enum_conversion_feature,
no_volatile_feature,
fsized_deallocation_feature,
]

return [
Expand Down
1 change: 1 addition & 0 deletions site_scons/site_tools/integrate_bazel.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ def bazel_debug_func(msg: str):
f'--//bazel/config:streams_release_build={env.GetOption("streams-release-build") is not None}',
f'--//bazel/config:build_enterprise={env.GetOption("modules") == "enterprise"}',
f'--//bazel/config:visibility_support={env.GetOption("visibility-support")}',
f'--//bazel/config:disable_warnings_as_errors={env.GetOption("disable-warnings-as-errors") == "source"}',
f"--platforms=//bazel/platforms:{distro_or_os}_{normalized_arch}_{env.ToolchainName()}",
f"--host_platform=//bazel/platforms:{distro_or_os}_{normalized_arch}_{env.ToolchainName()}",
"--compilation_mode=dbg", # always build this compilation mode as we always build with -g
Expand Down
11 changes: 11 additions & 0 deletions src/third_party/abseil-cpp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,10 @@ mongo_cc_library(
"dist/absl/crc/internal/crc_non_temporal_memcpy.cc",
],
hdrs = ABSEIL_HEADERS,
copts = select({
"//bazel/config:gcc_or_clang": ["-Wno-ignored-attributes"],
"//conditions:default": [],
}),
includes = ["dist"],
skip_global_deps = ABSEIL_SKIP_GLOBAL_DEPS,
deps = [
Expand Down Expand Up @@ -1029,6 +1033,9 @@ mongo_cc_library(
"dist/absl/flags/reflection.cc",
],
hdrs = ABSEIL_HEADERS,
features = [
"no_warn_non_virtual_detour",
],
includes = ["dist"],
skip_global_deps = ABSEIL_SKIP_GLOBAL_DEPS,
deps = [
Expand Down Expand Up @@ -1986,6 +1993,10 @@ mongo_cc_library(
"dist/absl/random/internal/randen_hwaes.cc",
],
hdrs = ABSEIL_HEADERS,
copts = select({
"//bazel/config:gcc_or_clang": ["-Wno-pass-failed"],
"//conditions:default": [],
}),
includes = ["dist"],
skip_global_deps = ABSEIL_SKIP_GLOBAL_DEPS,
deps = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ cc_library(
"randen_hwaes.h",
],
copts = ABSL_DEFAULT_COPTS + ABSL_RANDOM_RANDEN_COPTS + select({
"//bazel/config:gcc_or_clang" ["-Wno-pass-failed"],
"//absl:msvc_compiler": [],
"//absl:clang-cl_compiler": [],
"//conditions:default": ["-Wno-pass-failed"],
Expand Down
18 changes: 18 additions & 0 deletions src/third_party/boost/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ mongo_cc_library(
],
hdrs = BOOST_LIB_HEADERS,
copts = BOOST_COPTS,
features = [
"no_warn_non_virtual_detour",
],
local_defines = BOOST_DEFINES,
deps = [
"//src/mongo/util:boost_assert_shim",
Expand All @@ -59,6 +62,9 @@ mongo_cc_library(
],
hdrs = BOOST_LIB_HEADERS,
copts = BOOST_COPTS,
features = [
"no_warn_non_virtual_detour",
],
local_defines = BOOST_DEFINES,
deps = [
":boost_system",
Expand All @@ -83,6 +89,9 @@ mongo_cc_library(
],
hdrs = BOOST_LIB_HEADERS,
copts = BOOST_COPTS,
features = [
"no_warn_non_virtual_detour",
],
local_defines = BOOST_DEFINES,
deps = [
"//src/mongo/util:boost_assert_shim",
Expand All @@ -97,6 +106,9 @@ mongo_cc_library(
],
hdrs = BOOST_LIB_HEADERS,
copts = BOOST_COPTS,
features = [
"no_warn_non_virtual_detour",
],
local_defines = BOOST_DEFINES,
deps = [
":boost_system",
Expand Down Expand Up @@ -134,6 +146,9 @@ mongo_cc_library(
"//conditions:default": [],
}),
copts = BOOST_COPTS,
features = [
"no_warn_non_virtual_detour",
],
local_defines = BOOST_DEFINES + ["BOOST_THREAD_BUILD_LIB"] + select({
"@platforms//os:linux": [
"BOOST_THREAD_PTHREAD",
Expand Down Expand Up @@ -197,6 +212,9 @@ mongo_cc_library(
],
"//conditions:default": [],
}),
features = [
"no_warn_non_virtual_detour",
],
linkopts = select({
"@platforms//os:windows": ["synchronization.lib"],
"//conditions:default": [],
Expand Down
Loading

0 comments on commit ddc5c64

Please sign in to comment.