From c64fa93373c11c5a48eaaced789e4a475e7dd154 Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 24 Nov 2026 02:34:11 +0300 Subject: [PATCH] build: add compiler, reflection, and sanitizer capability probes (X-BASE-04) --- CMakeLists.txt | 4 ++ cmake/Capabilities.cmake | 107 +++++++++++++++++++++++++++++++++++++++ cmake/Module.cmake | 9 ++++ 3 files changed, 120 insertions(+) create mode 100644 cmake/Capabilities.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f3b37b..77bbddb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ option(VOID_ENABLE_TESTING "Build the test suite" ON) option(VOID_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" OFF) option(VOID_ENABLE_ASAN "Enable AddressSanitizer (Clang/GCC)" OFF) option(VOID_ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer (Clang/GCC)" OFF) +option(VOID_ENABLE_TSAN "Enable ThreadSanitizer (Clang/GCC)" OFF) option(VOID_ENABLE_COVERAGE "Enable code coverage instrumentation" OFF) set(CMAKE_CXX_STANDARD 26) @@ -28,6 +29,8 @@ include(CMakePackageConfigHelpers) set(EXPORT_SET_NAME ${PROJECT_NAME}Targets) +include(cmake/Capabilities.cmake) +void_probe_capabilities() include(cmake/Module.cmake) # Modules to configure, with the modules they need (INTERNAL_DEPS, followed @@ -80,6 +83,7 @@ foreach(module_path ${children}) endif() endforeach() void_finalize_modules() +void_report_capabilities() if(VOID_ENABLE_TESTING) enable_testing() diff --git a/cmake/Capabilities.cmake b/cmake/Capabilities.cmake new file mode 100644 index 0000000..c56c254 --- /dev/null +++ b/cmake/Capabilities.cmake @@ -0,0 +1,107 @@ +include(CheckCXXSourceCompiles) +include(CMakePushCheckState) + +function(_void_probe_sanitizer option_name compile_flags link_flag cache_var) + if(NOT ${option_name}) + return() + endif() + if(MSVC) + message(FATAL_ERROR + "[Void] ${option_name}=ON is not supported with MSVC via this probe") + endif() + + cmake_push_check_state(RESET) + set(CMAKE_REQUIRED_FLAGS "${compile_flags}") + set(CMAKE_REQUIRED_LINK_OPTIONS "${link_flag}") + check_cxx_source_compiles( + "int main() { int x = 0; return x; }\n" + ${cache_var} + ) + cmake_pop_check_state() + + if(NOT ${cache_var}) + message(FATAL_ERROR + "[Void] ${option_name}=ON requested, but ${CMAKE_CXX_COMPILER_ID} " + "${CMAKE_CXX_COMPILER_VERSION} failed to compile/link with '${link_flag}'.") + endif() +endfunction() + +function(void_probe_capabilities) + if(VOID_ENABLE_ASAN AND VOID_ENABLE_TSAN) + message(FATAL_ERROR + "[Void] VOID_ENABLE_ASAN and VOID_ENABLE_TSAN are mutually exclusive") + endif() + + cmake_push_check_state(RESET) + set(CMAKE_REQUIRED_FLAGS "-std=c++26") + check_cxx_source_compiles( + "#include \nint main() { std::expected e{0}; return *e; }\n" + VOID_HAS_STD_EXPECTED + ) + cmake_pop_check_state() + + if(NOT VOID_HAS_STD_EXPECTED) + message(FATAL_ERROR + "[Void] Compiler/stdlib lacks C++23/26 support required by Void") + endif() + + cmake_push_check_state(RESET) + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16) + set(CMAKE_REQUIRED_FLAGS "-std=c++26 -freflection") + else() + set(CMAKE_REQUIRED_FLAGS "-std=c++26") + endif() + check_cxx_source_compiles( + "#include \nconsteval bool probe() { constexpr auto r = ^^int; return std::meta::is_type(r); }\nstatic_assert(probe());\nint main() { return 0; }\n" + VOID_HAS_CXX26_REFLECTION + ) + cmake_pop_check_state() + + _void_probe_sanitizer(VOID_ENABLE_ASAN + "-fsanitize=address -fno-omit-frame-pointer" "-fsanitize=address" + VOID_COMPILER_SUPPORTS_ASAN) + _void_probe_sanitizer(VOID_ENABLE_UBSAN + "-fsanitize=undefined" "-fsanitize=undefined" + VOID_COMPILER_SUPPORTS_UBSAN) + _void_probe_sanitizer(VOID_ENABLE_TSAN + "-fsanitize=thread" "-fsanitize=thread" + VOID_COMPILER_SUPPORTS_TSAN) +endfunction() + +function(void_report_capabilities) + get_property(_mods GLOBAL PROPERTY VOID_MODULES) + list(LENGTH _mods _mod_count) + + if(VOID_HAS_CXX26_REFLECTION) + set(_refl "yes") + else() + set(_refl "no") + endif() + + if(VOID_HAS_STD_EXPECTED) + set(_exp "yes") + else() + set(_exp "no") + endif() + + set(_sans "") + if(VOID_ENABLE_ASAN) + list(APPEND _sans "ASan") + endif() + if(VOID_ENABLE_UBSAN) + list(APPEND _sans "UBSan") + endif() + if(VOID_ENABLE_TSAN) + list(APPEND _sans "TSan") + endif() + if(NOT _sans) + set(_sans_str "none") + else() + string(REPLACE ";" "+" _sans_str "${_sans}") + endif() + + message(STATUS + "[Void] Capabilities: compiler=${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}, " + "reflection=${_refl}, expected=${_exp}, shared=${BUILD_SHARED_LIBS}, " + "werror=${VOID_WARNINGS_AS_ERRORS}, sanitizers=${_sans_str}, modules=${_mod_count}") +endfunction() diff --git a/cmake/Module.cmake b/cmake/Module.cmake index fc2c9e2..7742198 100644 --- a/cmake/Module.cmake +++ b/cmake/Module.cmake @@ -192,6 +192,15 @@ function(void_add_module MODULE_NAME) ) endif() + if(VOID_ENABLE_TSAN) + target_compile_options(${TGT} PUBLIC + "$<$:-fsanitize=thread>" + ) + target_link_options(${TGT} PUBLIC + "$<$:-fsanitize=thread>" + ) + endif() + if(VOID_ENABLE_COVERAGE) target_compile_options(${TGT} PUBLIC "$<$:--coverage;-fprofile-update=atomic>"