[libc++] Automatically detect the libc++ hardening mode from the test suite (#172505)

This prevents hardcoding the hardening mode via compiler flags, and
allows testing what the default hardening mode is on platforms that set
it to something that isn't `none`. Otherwise, a platform setting a
default (which is done via -DLIBCXX_HARDENING_MODE=mode at CMake
configuration time) would end up passing `-D_LIBCPP_HARDENING_MODE=mode`
to the compiler, which does not allow checking what the default mode is.
This commit is contained in:
Louis Dionne 2026-01-23 17:37:36 -05:00 committed by GitHub
parent f45c8ba0cd
commit 5faf48d413
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 4 deletions

View File

@ -41,8 +41,6 @@ if (NOT LIBCXX_ENABLE_RTTI)
serialize_lit_param(SERIALIZED_LIT_PARAMS enable_rtti False)
endif()
serialize_lit_string_param(SERIALIZED_LIT_PARAMS hardening_mode "${LIBCXX_HARDENING_MODE}")
if (CMAKE_CXX_COMPILER_TARGET)
serialize_lit_string_param(SERIALIZED_LIT_PARAMS target_triple "${CMAKE_CXX_COMPILER_TARGET}")
else()

View File

@ -6,7 +6,7 @@
#
# ===----------------------------------------------------------------------===##
from libcxx.test.dsl import Feature, compilerMacros
from libcxx.test.dsl import Feature, compilerMacros, programSucceeds
features = []
@ -74,3 +74,22 @@ for macro, feature in inverted_macros.items():
and compilerMacros(cfg)[m] == "0",
)
)
for mode in ("none", "fast", "extensive", "debug"):
check_program = f"""
#include <stddef.h> // any header to get the definitions
int main(int, char**) {{
#if defined(_LIBCPP_VERSION) && \\
defined(_LIBCPP_HARDENING_MODE) && _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_{mode.upper()}
return 0;
#else
return 1;
#endif
}}
"""
features.append(
Feature(
name=f"libcpp-hardening-mode={mode}",
when=lambda cfg, prog=check_program: programSucceeds(cfg, prog)
)
)

View File

@ -420,7 +420,6 @@ DEFAULT_PARAMETERS = [
AddCompileFlag("-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST") if hardening_mode == "fast" else None,
AddCompileFlag("-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE") if hardening_mode == "extensive" else None,
AddCompileFlag("-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG") if hardening_mode == "debug" else None,
AddFeature("libcpp-hardening-mode={}".format(hardening_mode)) if hardening_mode != "undefined" else None,
],
),
),