Add option to force asserts in release builds

This commit is contained in:
Jean Pierre Cimalando 2020-05-19 23:42:10 +02:00
parent 2e78e5a55d
commit ad6e45f9ab
4 changed files with 37 additions and 33 deletions

View file

@ -37,6 +37,7 @@ option (SFIZZ_DEVTOOLS "Enable developer tools build [default: OFF]" OF
option (SFIZZ_SHARED "Enable shared library build [default: ON]" ON)
option (SFIZZ_USE_VCPKG "Assume that sfizz is build using vcpkg [default: OFF]" OFF)
option (SFIZZ_STATIC_LIBSNDFILE "Link libsndfile statically [default: OFF]" OFF)
option (SFIZZ_RELEASE_ASSERTS "Forced assertions in release builds [default: OFF]" OFF)
# Don't use IPO in non Release builds
include (CheckIPO)

View file

@ -112,6 +112,7 @@ Use vcpkg: ${SFIZZ_USE_VCPKG}
Statically link libsndfile: ${SFIZZ_STATIC_LIBSNDFILE}
Link libatomic: ${SFIZZ_LINK_LIBATOMIC}
Use clang libc++: ${USE_LIBCPP}
Release asserts: ${SFIZZ_RELEASE_ASSERTS}
Install prefix: ${CMAKE_INSTALL_PREFIX}
LV2 destination directory: ${LV2PLUGIN_INSTALL_DIR}

View file

@ -62,6 +62,9 @@ set_target_properties (sfizz_static PROPERTIES OUTPUT_NAME sfizz PUBLIC_HEADER "
if (WIN32)
target_compile_definitions (sfizz_static PRIVATE _USE_MATH_DEFINES)
endif()
if (SFIZZ_RELEASE_ASSERTS)
target_compile_definitions (sfizz_static PRIVATE "SFIZZ_ENABLE_RELEASE_ASSERT=1")
endif()
if (NOT MSVC)
install (TARGETS sfizz_static
@ -95,6 +98,9 @@ if (SFIZZ_SHARED)
if (WIN32)
target_compile_definitions (sfizz_shared PRIVATE _USE_MATH_DEFINES)
endif()
if (SFIZZ_RELEASE_ASSERTS)
target_compile_definitions (sfizz_shared PRIVATE "SFIZZ_ENABLE_RELEASE_ASSERT=1")
endif()
target_compile_definitions(sfizz_shared PRIVATE SFIZZ_EXPORT_SYMBOLS)
set_target_properties (sfizz_shared PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR} OUTPUT_NAME sfizz)
sfizz_enable_lto_if_needed(sfizz_shared)

View file

@ -6,54 +6,50 @@
#pragma once
#ifndef NDEBUG
#if !defined(NDEBUG) || defined(SFIZZ_ENABLE_RELEASE_ASSERT)
#include <iostream>
#if (__linux__ || __unix__)
// Break in source code
#if (__x86_64__ || __i386__)
#define ASSERTFALSE \
do { \
std::cerr << "Assert failed at " << __FILE__ << ":" << __LINE__ << '\n'; \
__asm__("int3"); \
} while (0)
#elif (__arm__ || __aarch64__)
#define ASSERTFALSE \
do { \
std::cerr << "Assert failed at " << __FILE__ << ":" << __LINE__ << '\n'; \
__builtin_trap(); \
} while (0)
#endif
#elif (_WIN32 || _WIN64)
#ifdef _MSC_VER
#if defined(_WIN32) && defined(_MSC_VER)
#pragma intrinsic(__debugbreak)
#endif
#define ASSERTFALSE \
do { \
std::cerr << "Assert failed at " << __FILE__ << ":" << __LINE__ << '\n'; \
__debugbreak(); \
} while (0)
#define debugBreak() __debugbreak()
#elif defined(_WIN32)
#define debugBreak() __debugbreak()
#elif defined(__x86_64__) || defined(__i386__)
#define debugBreak() asm volatile("int3")
#elif defined(__clang__) && ((__clang_major__ > 3) || (__clang_major__ == 3 && __clang_minor__ >= 2))
#define debugBreak() __builtin_debugtrap()
#elif defined(__GNUC__)
#define debugBreak() __builtin_trap()
#else
#define ASSERTFALSE do {} while (0)
#include <csignal>
#define debugBreak() ::raise(SIGTRAP)
#endif
// Assert stuff
#define ASSERTFALSE \
do { \
std::cerr << "Assert failed at " << __FILE__ << ":" << __LINE__ << '\n'; \
debugBreak(); \
} while (0)
#define ASSERT(expression) \
do { \
if (!(expression)) \
ASSERTFALSE; \
} \
while (0)
// Debug message
#define DBG(ostream) do { std::cerr << ostream << '\n'; } while (0)
ASSERTFALSE; \
} while (0)
#else // NDEBUG
#define ASSERTFALSE do {} while (0)
#define ASSERT(expression) do {} while (0)
#define DBG(ostream) do {} while (0)
#endif
// Debug message
#if !defined(NDEBUG) || defined(SFIZZ_ENABLE_RELEASE_DBG)
#include <iostream>
#define DBG(ostream) do { std::cerr << ostream << '\n'; } while (0)
#else
#define DBG(ostream) do {} while (0)
#endif