From c2a061068a5372bb1099de9e15c2e296d13c5b2f Mon Sep 17 00:00:00 2001 From: Luca Weiss Date: Wed, 20 Mar 2024 08:25:10 +0100 Subject: [PATCH] CMake: Compile with -ftrivial-auto-var-init=zero As per upstream guidance [0] we should compile the sources with this flag in order to not hit issues with using uninitialized variables. [0] https://android-review.googlesource.com/c/platform/system/core/+/2963911/1#message-01ebff378bb51f7815b6ed8b035a57fbce0418ab Fixes #133 Co-authored-by: munix9 <44939650+munix9@users.noreply.github.com> --- vendor/CMakeLists.txt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/vendor/CMakeLists.txt b/vendor/CMakeLists.txt index bcabe7e..ca4f759 100644 --- a/vendor/CMakeLists.txt +++ b/vendor/CMakeLists.txt @@ -16,6 +16,22 @@ if(APPLE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_DARWIN_C_SOURCE -D__DARWIN_C_LEVEL=__DARWIN_C_FULL") endif() +# Different versions of clang require a different set of flags for -ftrivial-auto-var-init +# Simplify this contruct once old clang version support is dropped +include(CheckCXXCompilerFlag) +check_cxx_compiler_flag("-ftrivial-auto-var-init=zero" COMPILER_SUPPORTS_TRIVIAL_ZERO_INIT) +if(COMPILER_SUPPORTS_TRIVIAL_ZERO_INIT) + set(VAR_ZERO_INIT_FLAGS "-ftrivial-auto-var-init=zero") +else() + check_cxx_compiler_flag("-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang" + COMPILER_REQUIRES_ENABLE_TRIVIAL_ZERO_INIT) + if(COMPILER_REQUIRES_ENABLE_TRIVIAL_ZERO_INIT) + set(VAR_ZERO_INIT_FLAGS "-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang") + endif() +endif() +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${VAR_ZERO_INIT_FLAGS}") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VAR_ZERO_INIT_FLAGS}") + # Android seems to use various attributes supported by clang but not by # GCC which causes it to emit lots of warnings. Since these attributes # don't seem to effect runtime behaviour simply disable the warnings.