Skip to content

Commit

Permalink
build: Allow forcing assertions (#1182)
Browse files Browse the repository at this point in the history
In the CI, we currently run the examples in Release only, which adds the `-DNDEBUG` flag and consequently disables `assert`. We can't run these in the CI with Debug, since that implies `-O0` and runs forever. 

This PR adds a new CMake option `ACTS_FORCE_ASSERTIONS` which defaults to `OFF`. If enabled, this will prepend a system include directory `cmake/assert_include` to all compilation units, which contains a wrapper around `cassert`, `Eigen/{Core,Dense,Geometry}` which enables `assert` (and `eigen_assert`) regardless of `NDEBUG`.

I verified that this would have triggered the assertion from #1180 for example.

The CI jobs are configured to run with `ACTS_FORCE_ASSERTIONS=ON`.
  • Loading branch information
paulgessinger committed Mar 3, 2022
1 parent 988e2ca commit 40d6e79
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
-DACTS_BUILD_EVERYTHING=ON
-DACTS_BUILD_EXAMPLES_PYTHON_BINDINGS=ON
-DACTS_LOG_FAILURE_THRESHOLD=WARNING
-DACTS_BUILD_ASSERTIONS=ON
- name: Build
run: ${SETUP} && cmake --build build --
- name: Unit tests
Expand Down Expand Up @@ -129,6 +130,7 @@ jobs:
-DACTS_BUILD_UNITTESTS=ON
-DACTS_BUILD_INTEGRATIONTESTS=ON
-DACTS_LOG_FAILURE_THRESHOLD=WARNING
-DACTS_BUILD_ASSERTIONS=ON
-DACTS_USE_SYSTEM_BOOST=OFF
-DACTS_USE_SYSTEM_EIGEN3=OFF
-DACTS_BUILD_PLUGIN_JSON=ON
Expand All @@ -152,6 +154,7 @@ jobs:
-DCMAKE_CXX_FLAGS=-Werror
-DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}"
-DACTS_LOG_FAILURE_THRESHOLD=WARNING
-DACTS_BUILD_ASSERTIONS=ON
-DACTS_USE_SYSTEM_BOOST=OFF
-DACTS_USE_SYSTEM_EIGEN3=OFF
-DACTS_BUILD_EXAMPLES=ON
Expand Down Expand Up @@ -204,6 +207,7 @@ jobs:
-DCMAKE_PREFIX_PATH=/usr/local/acts
-DACTS_BUILD_EVERYTHING=ON
-DACTS_LOG_FAILURE_THRESHOLD=WARNING
-DACTS_BUILD_ASSERTIONS=ON
- name: Build
run: cmake --build build --
- name: Unit tests
Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ option(ACTS_BUILD_EVERYTHING "Build with most options enabled (except HepMC3 and
# core related options
set(ACTS_PARAMETER_DEFINITIONS_HEADER "" CACHE FILEPATH "Use a different (track) parameter definitions header")
set(ACTS_LOG_FAILURE_THRESHOLD "" CACHE STRING "Log level above which an exception should be automatically thrown")
option(ACTS_FORCE_ASSERTIONS "Force assertions regardless of build type" OFF)
# plugins related options
option(ACTS_BUILD_PLUGIN_AUTODIFF "Build the autodiff plugin" OFF)
option(ACTS_USE_SYSTEM_AUTODIFF "Use autodiff provided by the system instead of the bundled version" OFF)
Expand Down Expand Up @@ -126,6 +127,12 @@ include(ActsComponentsHelpers) # handle components via add_..._if commands
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")

# This needs to happen before we set up any targets
if(ACTS_FORCE_ASSERTIONS)
message(STATUS "Injecting headers to force assertions. This can have side-effects, USE WITH CAUTION!")
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/cmake/assert_include)
endif()

# minimal dependency versions. they are defined here in a single place so
# they can be easily upgraded, although they might not be used if the
# dependency is included via `add_subdirectory(...)`.
Expand Down
2 changes: 2 additions & 0 deletions cmake/assert_include/Eigen/Core
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define eigen_assert(x) assert(x)
#include_next "Eigen/Core"
2 changes: 2 additions & 0 deletions cmake/assert_include/Eigen/Dense
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define eigen_assert(x) assert(x)
#include_next "Eigen/Dense"
2 changes: 2 additions & 0 deletions cmake/assert_include/Eigen/Geometry
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define eigen_assert(x) assert(x)
#include_next "Eigen/Geometry"
9 changes: 9 additions & 0 deletions cmake/assert_include/cassert
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#if defined(NDEBUG)
#define _HAD_NDEBUG
#undef NDEBUG
#endif
#include_next <cassert>
#if defined(_HAD_NDEBUG)
#undef _HAD_NDEBUG
#define NDEBUG
#endif
1 change: 1 addition & 0 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ components.
| ACTS_BUILD_DOCS | Build documentation |
| ACTS_BUILD_ANALYSIS_APPS | Build root based stand-alone analysis applications (defaults is OFF) |
| ACTS_LOG_FAILURE_THRESHOLD | Automatically fail when a log above the specified debug level is emitted (useful for automated tests) |
| ACTS_FORCE_ASSERTIONS | Try to force keeping `assert` even in Release builds. (useful for automated tests) |
| ACTS_PARAMETER_DEFINITIONS_HEADER | Use a different (track) parameter definitions header |
| ACTS_USE_SYSTEM_AUTODIFF | Use autodiff provided by the system instead of the bundled version |
| ACTS_USE_SYSTEM_NLOHMANN_JSON | Use nlohmann::json provided by the system instead of the bundled version |
Expand Down

0 comments on commit 40d6e79

Please sign in to comment.