From a4f8aa623fe2fc20218644ce7a847fa70746b3b3 Mon Sep 17 00:00:00 2001 From: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com> Date: Thu, 22 Jan 2026 16:16:18 +0000 Subject: [PATCH] chore: Detect uninitialized variables in CMake files (#6247) There were a few uninitialized variables in CMake files. This change will make sure we always check if a variable has been initialized before using them, or in come cases initialize them by default. This change will raise an error on CI if a developer introduced an uninitialized variable in CMake files. --- CMakeLists.txt | 4 +++- cmake/CompilationEnv.cmake | 6 ++++++ cmake/XrplCore.cmake | 8 ++++---- cmake/XrplInstall.cmake | 6 ++++++ cmake/XrplInterface.cmake | 13 +++++++++---- cmake/XrplSanitizers.cmake | 5 ++++- cmake/XrplSettings.cmake | 9 +++++---- 7 files changed, 37 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ee0484e79d..c24b27adb2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,9 @@ if(POLICY CMP0077) endif() # Fix "unrecognized escape" issues when passing CMAKE_MODULE_PATH on Windows. -file(TO_CMAKE_PATH "${CMAKE_MODULE_PATH}" CMAKE_MODULE_PATH) +if(DEFINED CMAKE_MODULE_PATH) + file(TO_CMAKE_PATH "${CMAKE_MODULE_PATH}" CMAKE_MODULE_PATH) +endif() list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") project(xrpl) diff --git a/cmake/CompilationEnv.cmake b/cmake/CompilationEnv.cmake index 345e4cdd62..2d97f94615 100644 --- a/cmake/CompilationEnv.cmake +++ b/cmake/CompilationEnv.cmake @@ -13,6 +13,7 @@ include_guard(GLOBAL) set(is_clang FALSE) set(is_gcc FALSE) set(is_msvc FALSE) +set(is_xcode FALSE) if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") # Clang or AppleClang set(is_clang TRUE) @@ -24,6 +25,11 @@ else() message(FATAL_ERROR "Unsupported C++ compiler: ${CMAKE_CXX_COMPILER_ID}") endif() +# Xcode generator detection +if(CMAKE_GENERATOR STREQUAL "Xcode") + set(is_xcode TRUE) +endif() + # -------------------------------------------------------------------- # Operating system detection diff --git a/cmake/XrplCore.cmake b/cmake/XrplCore.cmake index 0689fbe7b6..2e50cd2f7a 100644 --- a/cmake/XrplCore.cmake +++ b/cmake/XrplCore.cmake @@ -32,14 +32,14 @@ target_protobuf_sources(xrpl.libpb xrpl/proto target_compile_options(xrpl.libpb PUBLIC - $<$:-wd4996> - $<$: + $<$:-wd4996> + $<$: --system-header-prefix="google/protobuf" -Wno-deprecated-dynamic-exception-spec > PRIVATE - $<$:-wd4065> - $<$>:-Wno-deprecated-declarations> + $<$:-wd4065> + $<$>:-Wno-deprecated-declarations> ) target_link_libraries(xrpl.libpb diff --git a/cmake/XrplInstall.cmake b/cmake/XrplInstall.cmake index 310436998d..0599a8268c 100644 --- a/cmake/XrplInstall.cmake +++ b/cmake/XrplInstall.cmake @@ -4,6 +4,12 @@ include(create_symbolic_link) +# If no suffix is defined for executables (e.g. Windows uses .exe but Linux +# and macOS use none), then explicitly set it to the empty string. +if(NOT DEFINED suffix) + set(suffix "") +endif() + install ( TARGETS common diff --git a/cmake/XrplInterface.cmake b/cmake/XrplInterface.cmake index 6e0203c099..f53b2dac26 100644 --- a/cmake/XrplInterface.cmake +++ b/cmake/XrplInterface.cmake @@ -4,6 +4,11 @@ include(CompilationEnv) +# Set defaults for optional variables to avoid uninitialized variable warnings +if(NOT DEFINED voidstar) + set(voidstar OFF) +endif() + add_library (opts INTERFACE) add_library (Xrpl::opts ALIAS opts) target_compile_definitions (opts @@ -52,7 +57,7 @@ add_library (xrpl_syslibs INTERFACE) add_library (Xrpl::syslibs ALIAS xrpl_syslibs) target_link_libraries (xrpl_syslibs INTERFACE - $<$: + $<$: legacy_stdio_definitions.lib Shlwapi kernel32 @@ -69,10 +74,10 @@ target_link_libraries (xrpl_syslibs odbccp32 crypt32 > - $<$>:dl> - $<$,$>>:rt>) + $<$>:dl> + $<$,$>>:rt>) -if (NOT MSVC) +if (NOT is_msvc) set (THREADS_PREFER_PTHREAD_FLAG ON) find_package (Threads) target_link_libraries (xrpl_syslibs INTERFACE Threads::Threads) diff --git a/cmake/XrplSanitizers.cmake b/cmake/XrplSanitizers.cmake index 050a5ef6f0..fc31e4a3ec 100644 --- a/cmake/XrplSanitizers.cmake +++ b/cmake/XrplSanitizers.cmake @@ -43,7 +43,10 @@ include(CompilationEnv) # Read environment variable -set(SANITIZERS $ENV{SANITIZERS}) +set(SANITIZERS "") +if(DEFINED ENV{SANITIZERS}) + set(SANITIZERS "$ENV{SANITIZERS}") +endif() # Set SANITIZERS_ENABLED flag for use in other modules if(SANITIZERS MATCHES "address|thread|undefinedbehavior") diff --git a/cmake/XrplSettings.cmake b/cmake/XrplSettings.cmake index 647e95837d..3724ea2b4f 100644 --- a/cmake/XrplSettings.cmake +++ b/cmake/XrplSettings.cmake @@ -4,10 +4,11 @@ include(CompilationEnv) -if("$ENV{CI}" STREQUAL "true" OR "$ENV{CONTINUOUS_INTEGRATION}" STREQUAL "true") - set(is_ci TRUE) -else() - set(is_ci FALSE) +set(is_ci FALSE) +if(DEFINED ENV{CI}) + if("$ENV{CI}" STREQUAL "true") + set(is_ci TRUE) + endif() endif() get_directory_property(has_parent PARENT_DIRECTORY)