Skip to content

Commit

Permalink
Merge pull request OpenEnroth#1820 from captainurist/macos_fixes_586
Browse files Browse the repository at this point in the history
Enable assertions in release builds
  • Loading branch information
pskelton authored Oct 17, 2024
2 parents 954716e + 4728041 commit 6dc0330
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ include(CppLint)
include(LlsLint)
include(Dependencies)
include(CheckLinkerFlag)
include(Assertions)

# Defaults.
if (CMAKE_BUILD_TYPE)
Expand Down
36 changes: 36 additions & 0 deletions CMakeModules/Assertions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Enable assertions in release builds.
if(MSVC)
set(NDEBUG_FLAG "/DNDEBUG")
else()
set(NDEBUG_FLAG "-DNDEBUG")
endif()
foreach(BUILD_TYPE RELEASE RELWITHDEBINFO MINSIZEREL)
string(REPLACE "${NDEBUG_FLAG}" "" CMAKE_CXX_FLAGS_${BUILD_TYPE} "${CMAKE_CXX_FLAGS_${BUILD_TYPE}}")
string(REPLACE "${NDEBUG_FLAG}" "" CMAKE_C_FLAGS_${BUILD_TYPE} "${CMAKE_C_FLAGS_${BUILD_TYPE}}")
endforeach()

function(generate_assertion_test_file PATH EXPR)
file(WRITE "${PATH}" "
#include <assert.h>
#include <stdlib.h>
int main() {
#ifdef _WIN32
_set_abort_behavior(0, _WRITE_ABORT_MSG);
#endif
assert(${EXPR});
return 0;
}")
endfunction()

# We don't run tests when cross-compiling b/c it's a mess...
if(NOT CMAKE_CROSSCOMPILING)
generate_assertion_test_file("${CMAKE_BINARY_DIR}/test_assert_false.cpp" false)
generate_assertion_test_file("${CMAKE_BINARY_DIR}/test_assert_true.cpp" true)
try_run(TEST_ASSERT_FALSE_RUN_RESULT TEST_ASSERT_FALSE_COMPILE_RESULT
SOURCES "${CMAKE_BINARY_DIR}/test_assert_false.cpp")
try_run(TEST_ASSERT_TRUE_RUN_RESULT TEST_ASSERT_TRUE_COMPILE_RESULT
SOURCES "${CMAKE_BINARY_DIR}/test_assert_true.cpp")
if((TEST_ASSERT_FALSE_RUN_RESULT EQUAL 0) OR (NOT TEST_ASSERT_FALSE_COMPILE_RESULT) OR (NOT TEST_ASSERT_TRUE_RUN_RESULT EQUAL 0) OR (NOT TEST_ASSERT_TRUE_COMPILE_RESULT))
message(FATAL_ERROR "Could not enable assertions for this build.")
endif()
endif()

0 comments on commit 6dc0330

Please sign in to comment.