forked from TileDB-Inc/TileDB
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support compile-time assert configuration in release build (TileDB-In…
…c#2962) * Add support for compile-time assertion configuration. This commit adds support for configuring the behavior of `assert` at compile time. In particular, assertions may now be enabled even if building in release mode. CMake unconditionally adds the `-DNDEBUG` to compile definitions in a release build, so a CMake snippet is added from LLVM which strips the definition from CMake default flags. Modifying this paramater after the first cmake initialization is unlikely to have any effect. Also add `bootstrap` support for --enable-assertions --- TYPE: IMPROVEMENT DESC: Add support for compile-time assertion configuration * Add test for assertions build. The test strategy here is: - auxiliary executable try_assert which should assert out if TILEDB_ASSERTIONS=ON - test executable test_assert which runs try_assert and checks the error code. Test failure is conditional on compile-time configuration: - if assertions enabled, we expect the try_assert process to exit to SIGABRT. --- TYPE: NO_HISTORY * Address review comments * DEBUG -> Debug
- Loading branch information
Showing
9 changed files
with
160 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Lightly modified from LLVM, used under "Apache 2.0 License with LLVM exceptions” | ||
# https://github.com/llvm/llvm-project/blob/93d1a623cecb6f732db7900baf230a13e6ac6c6a/llvm/cmake/modules/HandleLLVMOptions.cmake#L60-L85 | ||
|
||
if(TILEDB_ASSERTIONS) | ||
# MSVC doesn't like _DEBUG on release builds. See LLVM PR 4379. | ||
if( NOT MSVC ) | ||
add_definitions( -D_DEBUG ) | ||
endif() | ||
# On non-Debug builds cmake automatically defines NDEBUG, so we | ||
# explicitly undefine it: | ||
if( NOT CMAKE_BUILD_TYPE STREQUAL "Debug" ) | ||
# NOTE: use `add_compile_options` rather than `add_definitions` since | ||
# `add_definitions` does not support generator expressions. | ||
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() | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
include(TileDBAssertions) | ||
|
||
find_package(Catch_EP REQUIRED) | ||
|
||
add_executable( | ||
test_assert | ||
test_assert.cc | ||
) | ||
|
||
target_link_libraries(test_assert PUBLIC Catch2::Catch2) | ||
|
||
if (TILEDB_ASSERTIONS) | ||
target_compile_definitions( | ||
test_assert | ||
PRIVATE | ||
-DTILEDB_ASSERTIONS | ||
) | ||
endif() | ||
|
||
add_executable( | ||
try_assert | ||
try_assert.cc | ||
) | ||
|
||
add_dependencies(test_assert try_assert) | ||
|
||
add_test( | ||
NAME "test_ci_asserts" | ||
COMMAND test_assert | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @file test/ci/test_assert | ||
* | ||
* @section LICENSE | ||
* | ||
* The MIT License | ||
* | ||
* @copyright Copyright (c) 2022 TileDB, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* @section DESCRIPTION | ||
* | ||
* This file defines a test which calls executable `try_assert` to determine | ||
* whether assertions are correctly enabled in build tree. | ||
*/ | ||
|
||
#define CATCH_CONFIG_MAIN | ||
#include <catch.hpp> | ||
|
||
#include <cassert> | ||
#include <iostream> | ||
|
||
#ifdef _WIN32 | ||
constexpr int assert_exit_code = 3; | ||
#else | ||
constexpr int assert_exit_code = 6; | ||
#endif | ||
|
||
TEST_CASE("CI: Test assertions configuration", "[ci][assertions]") { | ||
int retval = system("./try_assert"); | ||
|
||
#ifdef TILEDB_ASSERTIONS | ||
REQUIRE(retval == assert_exit_code); | ||
#else | ||
(void)assert_exit_code; | ||
REQUIRE(retval == 0); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <cassert> | ||
#include <iostream> | ||
|
||
int main(int, char**) { | ||
assert(false); | ||
|
||
std::cout << "Assert did not exit!" << std::endl; | ||
return 0; | ||
} |