Specify default opt/debug options for different CMAKE_BUILD_TYPEs#409
Specify default opt/debug options for different CMAKE_BUILD_TYPEs#409greenc-FNAL wants to merge 2 commits intomainfrom
CMAKE_BUILD_TYPEs#409Conversation
greenc-FNAL
commented
Mar 11, 2026
- May be overridden from CMake command line, environment, etc.
- May be overridden from CMake command line, environment, etc.
|
@phlexbot cmake-fix |
|
Automatic cmake-format fixes pushed (commit ff40cdd). |
There was a problem hiding this comment.
Pull request overview
Adds explicit default C++ compiler flags for common CMake build types at the top level, aiming to standardize optimization/debug settings across builds.
Changes:
- Introduce cached defaults for
CMAKE_CXX_FLAGS_DEBUG,CMAKE_CXX_FLAGS_RELEASE, andCMAKE_CXX_FLAGS_RELWITHDEBINFO.
| if(NOT CMAKE_CXX_FLAGS_DEBUG) | ||
| set(CMAKE_CXX_FLAGS_DEBUG "-Og -g" CACHE STRING "" FORCE) | ||
| endif() | ||
|
|
||
| if(NOT CMAKE_CXX_FLAGS_RELEASE) | ||
| set(CMAKE_CXX_FLAGS_RELEASE "-O3 -g0 -DNDEBUG" CACHE STRING "" FORCE) | ||
| endif() | ||
|
|
||
| if(NOT CMAKE_CXX_FLAGS_RELWITHDEBINFO) | ||
| set( | ||
| CMAKE_CXX_FLAGS_RELWITHDEBINFO | ||
| "-O3 -fno-omit-frame-pointer -g -DNDEBUG" | ||
| CACHE STRING | ||
| "" | ||
| FORCE | ||
| ) |
There was a problem hiding this comment.
These set(CMAKE_CXX_FLAGS_<CONFIG> ... CACHE ... FORCE) calls run before project()/cet_cmake_env(), so the variables are typically still unset and will be force-initialized here. That overrides the toolchain/compiler defaults CMake (and cetmodules) would normally populate, and can break or unexpectedly change flags for non-GNU/Clang toolchains. Prefer setting CMAKE_CXX_FLAGS_<CONFIG>_INIT before project(), or move this after project() and append per-config flags via add_compile_options($<$<CONFIG:...>:...>)/target options, and avoid FORCE unless you intend to override user-provided cache values.
| if(NOT CMAKE_CXX_FLAGS_DEBUG) | |
| set(CMAKE_CXX_FLAGS_DEBUG "-Og -g" CACHE STRING "" FORCE) | |
| endif() | |
| if(NOT CMAKE_CXX_FLAGS_RELEASE) | |
| set(CMAKE_CXX_FLAGS_RELEASE "-O3 -g0 -DNDEBUG" CACHE STRING "" FORCE) | |
| endif() | |
| if(NOT CMAKE_CXX_FLAGS_RELWITHDEBINFO) | |
| set( | |
| CMAKE_CXX_FLAGS_RELWITHDEBINFO | |
| "-O3 -fno-omit-frame-pointer -g -DNDEBUG" | |
| CACHE STRING | |
| "" | |
| FORCE | |
| ) | |
| if(NOT DEFINED CMAKE_CXX_FLAGS_DEBUG_INIT) | |
| set(CMAKE_CXX_FLAGS_DEBUG_INIT "-Og -g") | |
| endif() | |
| if(NOT DEFINED CMAKE_CXX_FLAGS_RELEASE_INIT) | |
| set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -g0 -DNDEBUG") | |
| endif() | |
| if(NOT DEFINED CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT) | |
| set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-O3 -fno-omit-frame-pointer -g -DNDEBUG") |
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@
## main #409 +/- ##
==========================================
+ Coverage 83.71% 83.83% +0.12%
==========================================
Files 126 126
Lines 3291 3291
Branches 583 583
==========================================
+ Hits 2755 2759 +4
+ Misses 325 324 -1
+ Partials 211 208 -3
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|