Skip to content

Commit

Permalink
CMake: Compile with -ftrivial-auto-var-init=zero
Browse files Browse the repository at this point in the history
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 nmeum#133

Co-authored-by: munix9 <44939650+munix9@users.noreply.github.com>
  • Loading branch information
lweiss-fairphone and munix9 committed Mar 20, 2024
1 parent 3f3b2d8 commit 6b3d9ff
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions vendor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ if(APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_DARWIN_C_SOURCE -D__DARWIN_C_LEVEL=__DARWIN_C_FULL")
endif()

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.
Expand Down

0 comments on commit 6b3d9ff

Please sign in to comment.