Have just worked through this and I think in short there is no *good*
way.

The method I decided to live with is caching the CFLAGS in the
toolchain file.

If you don't have a custom toolchain file for that project, create one.
Assuming you're using a GNU-derived toolchain try this:

    set(CMAKE_C_FLAGS_RELEASE "-O2" CACHE STRING "Some description")
    include(Compiler/GNU)

Pass the toolchain file to cmake:

cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/my/toolchain-file.cmake ...

The downside is you can no longer change the C_FLAGS from a cmake UI
(like ccmake) though you *can* change them from the command line.
 ( cmake -DCMAKE_C_FLAGS_RELEASE=... )

What doesn't work and why:

 * Setting CMAKE_CFLAGS_RELEASE in a CMakeLists.txt
   -> Obviously. It's too late, we're past toolchain
   determination and try_compile

 * Setting CMAKE_CFLAGS_RELEASE_INIT in a toolchain file
   -> GNU compiler determination appends -O3 and -NDEBUG

ciao
  Mark

On Mon, 10 Sep 2018 14:34:26 +0200, Stéphane Ancelot wrote:
> Hi,
> 
> In one project I need to compile with -O2 option , how to set it and remove
> -O3 -DNDEBUG flags for this project ?
> 
> Regards,
> 
> Steph
> 
> -- 
> 
> Powered by www.kitware.com
> 
> Please keep messages on-topic and check the CMake FAQ at: 
> http://www.cmake.org/Wiki/CMake_FAQ
> 
> Kitware offers various services to support the CMake community. For more 
> information on each offering, please visit:
> 
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
> 
> Visit other Kitware open-source projects at 
> http://www.kitware.com/opensource/opensource.html
> 
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake

-- 
Cognomen Ltd
http://cognomen.co.uk
+44 7855 790184

Attachment: signature.asc
Description: PGP signature

-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

Reply via email to