title: Change default value of CMAKE_CXX_FLAGS_DEBUG and friends in CMake
body: <p>I would like to change the default values for <code>CMAKE_CXX_FLAGS_RELEASE</code> or <code>CMAKE_CXX_FLAGS_DEBUG</code> in CMake. Basically, I have some project defaults that differ just slightly from CMake's (for release, for instance), and I shouldn't have to ask myself "Oh, does their -O3 or our -O2 take precedence when added with add_compile_options."</p>
  
  <p>Now, I know how to set these values, but I don't know how to make them user editable in the two usual ways: by using -<code>DCMAKE_CXX_FLAGS_DEBUG=yourflags</code> on the command line or by configuring it with ccmake or CMakeSetup.</p>
  
  <p>The problem being that CMAKE sets and caches its own defaults for these, and if you try to overwrite the variables without using FORCE, the "defaults" are never changed. If I use FORCE in my set command: <code>set(CMAKE_CXX_FLAGS_DEBUG blah CACHE STRING "" FORCE)</code>, it will overwrite it every time the script is run, eliminating the possibility for the user to change it if he wishes.</p>
  
  <p>I managed to hack it to work with CCMAKE by doing the following, but this still doesn't work with <code>cmake -DCMAKE_CXX_FLAGS_DEBUG</code> as it overwrites the user change AFTER he's done it:</p>
  
  <pre><code>set(DEFAULTS_SET FALSE CACHE BOOL "")
  set(CMAKE_CXX_FLAGS_DEBUG "-this -that" CACHE STRING "" FORCE)
  set(DEFAULTS_SET TRUE CACHE BOOL "" FORCE)
  </code></pre>
  
  <p>Obviously, this is a nasty hack and doesn't completely work(in the case of cmake -Dwhatever=thisorthat). I could add other build types as well, but I don't really see why that should be necessary just to change a few simple things.</p>
  
  <p><b>Edit March 1st, 2015:</b></p>
  
  <p>I've created a solution that works, though I still am not super thrilled about what I have to do. I'd seen other comments that solve the problem of setting <code>CMAKE_CXX_FLAGS_DEBUG</code> and friends without having them get clobbered, but this originally didn't work for me because I was trying to also select them based on the compiler that was in use. The compiler isn't determined, however, until it has already filled the variables for me. The trick I used is as follows. You must set the flags variables to something 'special' before the project command.</p>
  
  <pre><code>set(CMAKE_CXX_FLAGS_DEBUG "_UNSET" CACHE STRING "")
  project(your_project C CXX)
  
  if(${CMAKE_CXX_FLAGS_DEBUG} STREQUAL "_UNSET")
      # Do some compiler switching here and then set your flags with FORCE.
      set(CMAKE_CXX_FLAGS_DEBUG "-ggdb3 -O0" CACHE STRING "" FORCE)
  endif()
  </code></pre>
  
  <p>This now allows me to choose defaults that are completely override-able via command line with -D or in cmake-gui.</p>