How to override compiler optimisation level?

What is the modern CMake way of overriding (replacing) the C++ optimisation level (for all targets) ?

For Release builds using armclang I want to replace -O3 with -Ofast.

CMake doesn’t provide an abstraction for optimization flags. Replacing or appending to CMAKE_<LANG>_FLAGS_RELEASE is the best available today.

Thanks. Can I do a string replace on CMAKE_<LANG>_FLAGS_RELEASE?

Yes, that will work (unless the user changes it themselves). I think it’d be better to just string(APPEND) to it since most compilers use whatever the last -O flag is as the “winner” (some with warnings though).

Ok, thanks.

I have a common repository of shared CMake modules that many projects use, and one of these modules defines a “default target” as an interface library. Then, any target you create can just link against this default target.

Of course, this doesn’t help if you’re wanting to mess with options for targets in projects you don’t control.