From 1c10f730a12818ac51663ddcf3dff52365a70486 Mon Sep 17 00:00:00 2001 From: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com> Date: Thu, 18 Dec 2025 16:07:32 +0000 Subject: [PATCH] moved env. detection to compilationenv.cmake Signed-off-by: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com> --- CMakeLists.txt | 8 +++-- cmake/CompilationEnv.cmake | 68 ++++++++++++++++++++++++++++++++++++++ cmake/XrplCompiler.cmake | 2 ++ cmake/XrplInterface.cmake | 2 ++ cmake/XrplSanitizers.cmake | 47 +++++++------------------- cmake/XrplSanity.cmake | 10 +++--- cmake/XrplSettings.cmake | 6 +--- 7 files changed, 95 insertions(+), 48 deletions(-) create mode 100644 cmake/CompilationEnv.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 697c1bf311..cb2fe05daf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,14 +16,16 @@ set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") +include(CompilationEnv) + +if(is_gcc) # GCC-specific fixes add_compile_options(-Wno-unknown-pragmas -Wno-subobject-linkage) # -Wno-subobject-linkage can be removed when we upgrade GCC version to at least 13.3 -elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") +elseif(is_clang) # Clang-specific fixes add_compile_options(-Wno-unknown-warning-option) # Ignore unknown warning options -elseif(MSVC) +elseif(is_msvc) # MSVC-specific fixes add_compile_options(/wd4068) # Ignore unknown pragmas endif() diff --git a/cmake/CompilationEnv.cmake b/cmake/CompilationEnv.cmake new file mode 100644 index 0000000000..4c84f26de6 --- /dev/null +++ b/cmake/CompilationEnv.cmake @@ -0,0 +1,68 @@ + # Shared detection of compiler, operating system, and architecture. + # + # This module centralizes environment detection so that other + # CMake modules can use the same variables instead of repeating + # checks on CMAKE_* and built-in platform variables. + +# Only run once per configure step. +if(DEFINED XRPL_COMPILATION_ENV_INCLUDED) + return() +endif() +set(XRPL_COMPILATION_ENV_INCLUDED TRUE) + +# -------------------------------------------------------------------- +# Compiler detection (C++) +# -------------------------------------------------------------------- +set(is_clang FALSE) +set(is_gcc FALSE) +set(is_msvc FALSE) + +if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") # Clang or AppleClang + set(is_clang TRUE) +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set(is_gcc TRUE) +elseif(MSVC) + set(is_msvc TRUE) +endif() + +set(xrpl_cxx_compiler_id "${CMAKE_CXX_COMPILER_ID}") +set(xrpl_cxx_compiler_version "${CMAKE_CXX_COMPILER_VERSION}") + +# Backwards-compat aliases used in some modules +set(IS_CLANG ${is_clang}) +set(IS_GCC ${is_gcc}) + +# -------------------------------------------------------------------- +# Operating system detection +# -------------------------------------------------------------------- +set(is_linux FALSE) +set(is_windows FALSE) +set(is_macos FALSE) + +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + set(is_linux TRUE) +elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows") + set(is_windows TRUE) +elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + set(is_macos TRUE) +endif() + +# -------------------------------------------------------------------- +# Architecture / pointer size +# -------------------------------------------------------------------- +set(is_64bit FALSE) +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(is_64bit TRUE) +endif() +set(pointer_size "${CMAKE_SIZEOF_VOID_P}") + +set(is_amd64 FALSE) +set(is_arm64 FALSE) +if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") + set(is_amd64 TRUE) +elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64") + set(is_arm64 TRUE) +endif() + +# Keep legacy uppercase alias for existing code +set(IS_AMD64 ${is_amd64}) diff --git a/cmake/XrplCompiler.cmake b/cmake/XrplCompiler.cmake index 954c335ec2..923c6f4cae 100644 --- a/cmake/XrplCompiler.cmake +++ b/cmake/XrplCompiler.cmake @@ -2,6 +2,8 @@ setup project-wide compiler settings #]===================================================================] +include(CompilationEnv) + #[=========================================================[ TODO some/most of these common settings belong in a toolchain file, especially the ABI-impacting ones diff --git a/cmake/XrplInterface.cmake b/cmake/XrplInterface.cmake index 9847f39fba..0a2f67ddf6 100644 --- a/cmake/XrplInterface.cmake +++ b/cmake/XrplInterface.cmake @@ -2,6 +2,8 @@ xrpld compile options/settings via an interface library #]===================================================================] +include(CompilationEnv) + add_library (opts INTERFACE) add_library (Xrpl::opts ALIAS opts) target_compile_definitions (opts diff --git a/cmake/XrplSanitizers.cmake b/cmake/XrplSanitizers.cmake index 2c56db4fab..426d096ea9 100644 --- a/cmake/XrplSanitizers.cmake +++ b/cmake/XrplSanitizers.cmake @@ -9,12 +9,14 @@ - "Thread,UndefinedBehavior" - "UndefinedBehavior" - The compiler type and platform are detected automatically by CMake. + The compiler type and platform are detected in CompilationEnv.cmake. The sanitizer compile options are applied to the 'common' interface library which is linked to all targets in the project. #]===================================================================] -# Read environment variable +include(CompilationEnv) + + # Read environment variable set(SANITIZERS $ENV{SANITIZERS}) if(NOT SANITIZERS) @@ -33,7 +35,6 @@ set(_san_list "${SANITIZERS}") string(REPLACE "," ";" _san_list "${_san_list}") separate_arguments(_san_list) -set(REMAINING_SANITIZERS "") foreach(_san IN LISTS _san_list) if(_san STREQUAL "Address") set(ENABLE_ASAN TRUE) @@ -42,35 +43,11 @@ foreach(_san IN LISTS _san_list) elseif(_san STREQUAL "UndefinedBehavior") set(ENABLE_UBSAN TRUE) else() - list(APPEND REMAINING_SANITIZERS "${_san}") + message(FATAL_ERROR "Unsupported sanitizer type: ${_san}" + "Supported: Address, Thread, UndefinedBehavior and their combinations.") endif() endforeach() -if(REMAINING_SANITIZERS) - message(FATAL_ERROR - "Unknown SANITIZERS value(s): ${REMAINING_SANITIZERS}. " - "Supported: Address, Thread, UndefinedBehavior and their combinations.") -endif() - -# Detect compiler type -set(IS_GCC FALSE) -set(IS_CLANG FALSE) -if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - set(IS_GCC TRUE) - message(STATUS " Compiler: GCC ${CMAKE_CXX_COMPILER_VERSION}") -elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(IS_CLANG TRUE) - message(STATUS " Compiler: Clang ${CMAKE_CXX_COMPILER_VERSION}") -endif() - -# Detect platform (amd64/x86_64 vs arm64/aarch64) -set(IS_AMD64 FALSE) -if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") - set(IS_AMD64 TRUE) - message(STATUS " Platform: amd64") -else() - message(STATUS " Platform: ${CMAKE_SYSTEM_PROCESSOR}") -endif() # Frame pointer is required for meaningful stack traces. Sanitizers recommend minimum of -O1 for reasonable performance set(SANITIZERS_COMPILE_FLAGS "-fno-omit-frame-pointer" "-O1") @@ -86,7 +63,7 @@ endif() if(ENABLE_UBSAN) # UB sanitizer flags - if(IS_CLANG) + if(is_clang) # Clang supports additional UB checks list(APPEND SANITIZERS_FLAGS "undefined" "float-divide-by-zero" "unsigned-integer-overflow") else() @@ -98,7 +75,7 @@ endif() # Use large code model for ASAN to avoid relocation errors # Use medium code model for TSAN (large is not compatible with TSAN) set(SANITIZERS_RELOCATION_FLAGS) -if(IS_GCC AND IS_AMD64) +if(is_gcc AND is_amd64) if(ENABLE_ASAN) message(STATUS " Using large code model (-mcmodel=large)") list(APPEND SANITIZERS_COMPILE_FLAGS "-mcmodel=large") @@ -110,8 +87,8 @@ if(IS_GCC AND IS_AMD64) endif() endif() -# Compiler-specific configuration -if(IS_GCC) + # Compiler-specific configuration +if(is_gcc) # Disable mold, gold and lld linkers for GCC with sanitizers # Use default linker (bfd/ld) which is more lenient with mixed code models set(use_mold OFF CACHE BOOL "Use mold linker" FORCE) @@ -132,9 +109,9 @@ if(IS_GCC) # Add sanitizer to compile and link flags list(APPEND SANITIZERS_COMPILE_FLAGS "-fsanitize=${SANITIZERS_FLAGS_STR}") - set(SANITIZERS_LINK_FLAGS "${SANITIZERS_RELOCATION_FLAGS}" "-fsanitize=${SANITIZERS_FLAGS_STR}") + set(SANITIZERS_LINK_FLAGS "${SANITIZERS_RELOCATION_FLAGS}" "-fsanitize=${SANITIZERS_FLAGS_STR}") -elseif(IS_CLANG) +elseif(is_clang) # Add ignorelist for Clang (GCC doesn't support this) # Use CMAKE_SOURCE_DIR to get the path to the ignorelist set(IGNORELIST_PATH "${CMAKE_SOURCE_DIR}/sanitizers/suppressions/sanitizer-ignorelist.txt") diff --git a/cmake/XrplSanity.cmake b/cmake/XrplSanity.cmake index db983da73d..594aadbf55 100644 --- a/cmake/XrplSanity.cmake +++ b/cmake/XrplSanity.cmake @@ -2,6 +2,8 @@ sanity checks #]===================================================================] +include(CompilationEnv) + get_property(is_multiconfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) set (CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE) @@ -16,14 +18,12 @@ if (NOT is_multiconfig) endif () endif () -if ("${CMAKE_CXX_COMPILER_ID}" MATCHES ".*Clang") # both Clang and AppleClang - set (is_clang TRUE) +if (is_clang) # both Clang and AppleClang if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 16.0) message (FATAL_ERROR "This project requires clang 16 or later") endif () -elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - set (is_gcc TRUE) +elseif (is_gcc) if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 12.0) message (FATAL_ERROR "This project requires GCC 12 or later") endif () @@ -40,7 +40,7 @@ if (MSVC AND CMAKE_GENERATOR_PLATFORM STREQUAL "Win32") message (FATAL_ERROR "Visual Studio 32-bit build is not supported.") endif () -if (NOT CMAKE_SIZEOF_VOID_P EQUAL 8) +if (NOT is_64bit) message (FATAL_ERROR "Xrpld requires a 64 bit target architecture.\n" "The most likely cause of this warning is trying to build xrpld with a 32-bit OS.") endif () diff --git a/cmake/XrplSettings.cmake b/cmake/XrplSettings.cmake index a272ddcef8..6e30703fb7 100644 --- a/cmake/XrplSettings.cmake +++ b/cmake/XrplSettings.cmake @@ -2,11 +2,7 @@ declare options and variables #]===================================================================] -if(CMAKE_SYSTEM_NAME STREQUAL "Linux") - set (is_linux TRUE) -else() - set(is_linux FALSE) -endif() +include(CompilationEnv) if("$ENV{CI}" STREQUAL "true" OR "$ENV{CONTINUOUS_INTEGRATION}" STREQUAL "true") set(is_ci TRUE)