Add abstraction for optimization level
Currently CMake hard-codes optimization flags for each default configuration (Debug
, Release
, RelWithDebInfo
, MinSizeRel
) using compiler-specific flags, such as gcc's -O3
for Release
. Projects can change them only by replacing CMAKE_<LANG>_FLAGS_<CONFIG>
altogether (including non-optimization flags), or by knowing CMake's existing default and editing CMAKE_<LANG>_FLAGS_<CONFIG>
with string operations. That means we cannot even change our defaults without a policy.
Instead, we could introduce a first-class abstraction for choosing the optimization level used by each configuration. The setting would be independent of others and could be changed individually without knowing its default or affecting other kinds of flags. The choice would map to compiler-specific flags underneath (though would probably need a way for the user to provide compiler-specific flags directly too).
This issue is similar to #19085 (for warning levels), #19108 (closed) (for MSVC runtime library), and #20610 (for MSVC exceptions/rtti).