diff --git a/CMakeLists.txt b/CMakeLists.txt index f24f68c0..0bf9700c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/cmake/SfizzConfig.cmake b/cmake/SfizzConfig.cmake index 6b5818fc..e651bc18 100644 --- a/cmake/SfizzConfig.cmake +++ b/cmake/SfizzConfig.cmake @@ -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} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 03f976d2..01473869 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) diff --git a/src/sfizz/Debug.h b/src/sfizz/Debug.h index a9a1d097..b8705552 100644 --- a/src/sfizz/Debug.h +++ b/src/sfizz/Debug.h @@ -6,54 +6,50 @@ #pragma once -#ifndef NDEBUG +#if !defined(NDEBUG) || defined(SFIZZ_ENABLE_RELEASE_ASSERT) #include -#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 +#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 +#define DBG(ostream) do { std::cerr << ostream << '\n'; } while (0) +#else +#define DBG(ostream) do {} while (0) +#endif