title: Optimize in CMake by default
body: <p>I have a C++ project which uses CMake as its build system. I'd like the following behavior:</p>
  
  <p>If cmake is invoked as <code>cmake ..</code>, then <code>CMAKE_CXX_FLAGS</code> is <code>-O3 -Wall -Wextra</code></p>
  
  <p>If cmake is invoked as <code>cmake .. -DCMAKE_BUILD_TYPE=Debug</code>, then <code>CMAKE_CXX_FLAGS</code> is <code>-g -Wall -Wextra</code></p>
  
  <p>I tried the following</p>
  
  <pre><code>message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
  
  set(CMAKE_CXX_FLAGS "-O3 -Wall -Wextra")
  set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wextra")
  </code></pre>
  
  <p>But this has a big problem. First of all, if the second invocation is used, then both <code>-O3</code> and <code>-g</code> flags are passed to the compiler. Besides, if I use the second invocation and the first thereafter, <code>CMAKE_BUILD_TYPE</code> stays <code>Debug</code> although not explicitly ordered so - so I get a Debug build although I want an optimized build.</p>
  
  <p>Why? What can I do to get the desired behavior?</p>