[CMake] default release build flags

Marcus D. Hanwell marcus.hanwell at kitware.com
Mon May 9 15:13:38 EDT 2016


On Thu, Apr 28, 2016 at 2:21 PM, Burlen Loring <burlen.loring at gmail.com> wrote:
> Hi Guys,
>
> I'd like to discuss changing the defaults of CMAKE_C/CXX_FLAGS_RELEASE on
> gcc, and potentially gcc like compilers such as clang and intel.
>
> Currently the default is "-O3 -DNDEBUG". I would like to discuss changing
> this to "-O3 -march=native -mtune=native -DNDEBUG". This change will enable
> numerous optimizations and produce faster code targeted for the cpu in use
> during the compilation.
>
As someone who was bitten by a project enabling -march=native in their
CMake-based build system, and the days of debugging as this was a
nested dependency built in a superbuild to create a package I hope we
never make that a default.

As a former Gentoo developer who has seen that -O3 is often not the
best default flag for optimized builds I would actually suggest
changing the default to "-O2 -DNDEBUG". The binaries are normally
smaller, compile times faster, and the resulting code faster. Quoting
from the wiki (https://wiki.gentoo.org/wiki/GCC_optimization):

'-O3: the highest level of optimization possible. It enables
optimizations that are expensive in terms of compile time and memory
usage. Compiling with -O3 is not a guaranteed way to improve
performance, and in fact, in many cases, can slow down a system due to
larger binaries and increased memory usage. -O3 is also known to break
several packages. Using -O3 is not recommended.'

I think adding a RelWithNative or RelAgressiveOpt might be an option,
but there are a number of things like distcc that makes applying
-march=native undesirable. For those that know what they are doing
certainly, but there are side effects and debugging can be tricky for
the poor packagers too (who often have limited resources, and do not
expect this).

My $0.03 cents as someone who may have lost a little hair over this ;-)

Marcus


More information about the CMake mailing list