Skip to content

Commit

Permalink
Reapply "build: add enable-assertions option"
Browse files Browse the repository at this point in the history
This reverts commit 531ff0d.
  • Loading branch information
Chilledheart committed Oct 21, 2024
1 parent 531ff0d commit 69eeba0
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 33 deletions.
83 changes: 56 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ option(OFFICIAL_BUILD "Build with official build" OFF)

if (OFFICIAL_BUILD)
list(APPEND YASS_APP_FEATURES "official-build")
add_definitions(-DOFFICIAL_BUILD)
add_compile_definitions(-DOFFICIAL_BUILD)
endif()

if(MSVC)
Expand Down Expand Up @@ -559,7 +559,7 @@ cmake_dependent_option(

if (FLATPAK_BUILD)
list(APPEND YASS_APP_FEATURES "flatpak-build")
add_definitions(-DFLATPAK_BUILD)
add_compile_definitions(-DFLATPAK_BUILD)
endif()

cmake_dependent_option(
Expand All @@ -568,7 +568,7 @@ cmake_dependent_option(

if (FLATHUB_BUILD)
list(APPEND YASS_APP_FEATURES "flathub-build")
add_definitions(-DFLATHUB_BUILD)
add_compile_definitions(-DFLATHUB_BUILD)
endif()

# Dynamic users are supported from version 235
Expand Down Expand Up @@ -643,6 +643,44 @@ else()
message(STATUS "Debug Build")
endif()

if (upper_CMAKE_BUILD_TYPE MATCHES "RELWITHDEBINFO|DEBUG" OR NOT OFFICIAL_BUILD)
option(ENABLE_ASSERTIONS "Enable assertions" ON)
else()
option(ENABLE_ASSERTIONS "Enable assertions" OFF)
endif()

if (ENABLE_ASSERTIONS)
message(STATUS "Enabled Assertions")
list(APPEND YASS_APP_FEATURES "assertions")
add_compile_definitions(-DDCHECK_ALWAYS_ON=1)

# MSVC doesn't like _DEBUG on release builds. See PR 4379.
if(NOT MSVC)
add_compile_definitions(_DEBUG)
endif()
# On non-Debug builds cmake automatically defines NDEBUG, so we
# explicitly undefine it:
if(NOT upper_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-UNDEBUG>)
if (MSVC)
# Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
foreach(flags_var_to_scrub
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS_MINSIZEREL)
string(REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
"${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
endforeach()
endif()
endif()
else()
## Optimization flags
add_compile_options($<$<OR:$<CONFIG:RELEASE>,$<CONFIG:MINSIZEREL>>:-DNDEBUG>)
endif()

if (ENABLE_LTO AND IS_RELEASE_BUILD)
if (COMPILER_CLANG)
if (USE_MOLD AND UNIX AND NOT APPLE)
Expand Down Expand Up @@ -884,7 +922,7 @@ if(MSAN)
add_compile_options(-fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer)
add_link_options(-fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer)
endif()
add_definitions(-DMEMORY_SANITIZER)
add_compile_definitions(-DMEMORY_SANITIZER)
list(APPEND YASS_APP_FEATURES "msan")
endif()

Expand All @@ -902,9 +940,9 @@ if(ASAN)
add_compile_options(-fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer)
add_link_options(-fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer)
endif()
add_definitions(-DADDRESS_SANITIZER)
add_compile_definitions(-DADDRESS_SANITIZER)
if (NOT APPLE AND USE_LIBCXX)
add_definitions(-D_LIBCPP_INSTRUMENTED_WITH_ASAN)
add_compile_definitions(-D_LIBCPP_INSTRUMENTED_WITH_ASAN)
endif()
list(APPEND YASS_APP_FEATURES "asan")
endif()
Expand Down Expand Up @@ -937,7 +975,7 @@ if(TSAN)
add_compile_options(-fsanitize=thread)
add_link_options(-fsanitize=thread)
endif()
add_definitions(-DTHREAD_SANITIZER)
add_compile_definitions(-DTHREAD_SANITIZER)
list(APPEND YASS_APP_FEATURES "tsan")
endif()

Expand All @@ -955,7 +993,7 @@ if(UBSAN)
add_compile_options(-fsanitize=undefined)
add_link_options(-fsanitize=undefined)
endif()
add_definitions(-DUNDEFINED_SANITIZER)
add_compile_definitions(-DUNDEFINED_SANITIZER)
list(APPEND YASS_APP_FEATURES "ubsan")
endif()

Expand Down Expand Up @@ -1002,14 +1040,6 @@ if (NOT MSVC)
endif()
endif()

## optimization flags
## TODO Changed in version 3.19: Multiple configurations can be specified for cfgs
## see https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#genex:CONFIG
add_compile_options(
"$<$<CONFIG:RELEASE>:-DNDEBUG>"
"$<$<CONFIG:MINSIZEREL>:-DNDEBUG>"
)

## can gc eliminate any unused functions and data items
if (NOT APPLE)
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-fdata-sections>)
Expand Down Expand Up @@ -1141,7 +1171,6 @@ else()

## Optimization flags
add_compile_options($<$<AND:$<CONFIG:RELEASE>,$<COMPILE_LANGUAGE:C,CXX>>:/Zc:inline>)
add_compile_options($<$<AND:$<CONFIG:RELEASE>,$<COMPILE_LANGUAGE:C,CXX>>:/DNDEBUG>)

## Disable LTCG for static libraries because this setting introduces ABI incompatibility between minor compiler versions
if (NOT MSVC_CRT_LINKAGE STREQUAL "static" AND NOT COMPILER_CLANG)
Expand Down Expand Up @@ -1906,7 +1935,7 @@ endif()
# *****************************************************************************************

if (USE_LIBCXX)
add_definitions(-DHAVE_LIBCXX)
add_compile_definitions(-DHAVE_LIBCXX)
add_subdirectory(third_party/libc++ EXCLUDE_FROM_ALL)
link_libraries(cxx ${libcxx_PUBLIC_LIBRARIES})
set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES} ${libcxx_PUBLIC_LIBRARIES}")
Expand All @@ -1918,15 +1947,15 @@ if (USE_LIBCXX)
if (MINGW_WORKAROUND)
list(APPEND YASS_APP_FEATURES "mingw atexit workaround")
endif()
elseif(NOT MSVC)
elseif(LINUX AND ENABLE_ASSERTIONS)
# Enable assertions on safety checks, also in libstdc++
#
# In case the C++ standard library implementation used is libstdc++, then
# enable its own hardening checks.
#
# Enable libstdc++ hardening lightweight assertions. Those have a low
# performance penalty but are considered a bare minimum for security.
add_definitions(-D_GLIBCXX_ASSERTIONS=1)
add_compile_definitions(-D_GLIBCXX_ASSERTIONS=1)
endif()

# *****************************************************************************************
Expand Down Expand Up @@ -1954,7 +1983,7 @@ int main() {
}
" HAVE_FLOCK)
if (HAVE_FLOCK)
add_definitions(-DHAVE_FLOCK)
add_compile_definitions(-DHAVE_FLOCK)
endif()

if (NOT WIN32 AND NOT APPLE)
Expand All @@ -1966,16 +1995,16 @@ if (NOT WIN32 AND NOT APPLE)
check_symbol_exists(mallinfo2 malloc.h HAVE_MALLINFO2)
endif()
if (HAVE_PIPE2)
add_definitions(-DHAVE_PIPE2)
add_compile_definitions(-DHAVE_PIPE2)
endif()
if (HAVE_DUP3)
add_definitions(-DHAVE_DUP3)
add_compile_definitions(-DHAVE_DUP3)
endif()
if (HAVE_MALLINFO)
add_definitions(-DHAVE_MALLINFO)
add_compile_definitions(-DHAVE_MALLINFO)
endif()
if (HAVE_MALLINFO2)
add_definitions(-DHAVE_MALLINFO2)
add_compile_definitions(-DHAVE_MALLINFO2)
endif()
if (MINGW)
# see https://github.com/mingw-w64/mingw-w64/blob/master/mingw-w64-headers/crt/_mingw.h.in#L215
Expand Down Expand Up @@ -2354,9 +2383,9 @@ include_directories(third_party/abseil-cpp)
# force win32 thread model for libc++ usage
if (MINGW AND USE_LIBCXX)
# force non-inline emulated-tls
add_definitions(-DABSL_CONSUME_DLL)
add_compile_definitions(-DABSL_CONSUME_DLL)
# force c++11 thread model
add_definitions(-DABSL_FORCE_THREAD_IDENTITY_MODE=ABSL_THREAD_IDENTITY_MODE_USE_CPP11)
add_compile_definitions(-DABSL_FORCE_THREAD_IDENTITY_MODE=ABSL_THREAD_IDENTITY_MODE_USE_CPP11)
endif()
if (ABSL_BUILD_TESTING)
set(ABSL_USE_EXTERNAL_GOOGLETEST "ON" CACHE BOOL "")
Expand Down
13 changes: 11 additions & 2 deletions src/config/config_version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ static void ParseConfigFileOption(int argc, const char** argv) {
#endif
#ifndef NDEBUG
std::cout << "Debug build (NDEBUG not #defined)" << std::endl;
#endif
#ifdef DCHECK_ALWAYS_ON
std::cout << "Assertions build (DCHECK_ALWAYS_ON #defined)" << std::endl;
#endif
argv[pos] = "";
pos += 1;
Expand All @@ -100,8 +103,11 @@ static void ParseConfigFileOption(int argc, const char** argv) {
#ifdef HAVE_MIMALLOC
std::cerr << "MIMALLOC: " << mi_version() << std::endl;
#endif
#ifdef DCHECK_ALWAYS_ON
std::cerr << "Assertions build (DCHECK_ALWAYS_ON #defined)" << std::endl;
#endif
#ifndef NDEBUG
std::cerr << "Debug build (NDEBUG not #defined)\n" << std::endl;
std::cerr << "Debug build (NDEBUG not #defined)" << std::endl;
#endif
}

Expand Down Expand Up @@ -130,8 +136,11 @@ void ReadConfigFileAndArguments(int argc, const char** argv) {
#ifdef HAVE_MIMALLOC
LOG(WARNING) << "MIMALLOC: " << mi_version();
#endif
#ifdef DCHECK_ALWAYS_ON
LOG(WARNING) << "Assertions build (DCHECK_ALWAYS_ON #defined)";
#endif
#ifndef NDEBUG
LOG(WARNING) << "Debug build (NDEBUG not #defined)\n";
LOG(WARNING) << "Debug build (NDEBUG not #defined)";
#endif
}

Expand Down
5 changes: 4 additions & 1 deletion src/ss_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,11 @@ int main(int argc, char** argv) {
LOG(WARNING) << "Application starting: " << YASS_APP_TAG << " type: " << ProgramTypeToStr(pType);
LOG(WARNING) << "Last Change: " << YASS_APP_LAST_CHANGE;
LOG(WARNING) << "Features: " << YASS_APP_FEATURES;
#ifdef DCHECK_ALWAYS_ON
LOG(WARNING) << "Assertions build (DCHECK_ALWAYS_ON #defined)";
#endif
#ifndef NDEBUG
LOG(WARNING) << "Debug build (NDEBUG not #defined)\n";
LOG(WARNING) << "Debug build (NDEBUG not #defined)";
#endif

#ifdef _WIN32
Expand Down
5 changes: 4 additions & 1 deletion src/ss_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,11 @@ int main(int argc, char** argv) {
LOG(WARNING) << "Application starting: " << YASS_APP_TAG << " type: " << ProgramTypeToStr(pType);
LOG(WARNING) << "Last Change: " << YASS_APP_LAST_CHANGE;
LOG(WARNING) << "Features: " << YASS_APP_FEATURES;
#ifdef DCHECK_ALWAYS_ON
LOG(WARNING) << "Assertions build (DCHECK_ALWAYS_ON #defined)";
#endif
#ifndef NDEBUG
LOG(WARNING) << "Debug build (NDEBUG not #defined)\n";
LOG(WARNING) << "Debug build (NDEBUG not #defined)";
#endif

#ifdef _WIN32
Expand Down
6 changes: 5 additions & 1 deletion third_party/libc++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,14 @@ if (${CMAKE_BUILD_TYPE} MATCHES Debug)
set(libcxx_PUBLIC_DEFINITIONS
${libcxx_PUBLIC_DEFINITIONS}
"_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG")
else()
elseif(ENABLE_ASSERTIONS)
set(libcxx_PUBLIC_DEFINITIONS
${libcxx_PUBLIC_DEFINITIONS}
"_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE")
else()
set(libcxx_PUBLIC_DEFINITIONS
${libcxx_PUBLIC_DEFINITIONS}
"_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_NONE")
endif()

set(libcxx_PUBLIC_DEFINITIONS
Expand Down
2 changes: 1 addition & 1 deletion third_party/libc++/__assertion_handler
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#endif

// Based on CHECK_WILL_STREAM() defined in base/check.h
#if defined(NDEBUG) && defined(OFFICIAL_BUILD)
#if defined(OFFICIAL_BUILD) && !defined(DCHECK_ALWAYS_ON)

_LIBCPP_BEGIN_NAMESPACE_STD

Expand Down

0 comments on commit 69eeba0

Please sign in to comment.