title: Correct way to handle compiler flags when using CMake
body: <p>I am struggling to find a proper way to propagate correct compiler flags for all targets.</p>
  
  <p>Let's imagine that there is a project which contains a library and unit tests.</p>
  
  <pre><code>ProjectFolder
  |-WorkerLibFolder
    |-Worker.cpp
    |-Worker.hpp
    |-CMakeLists.txt  (2)
  |-main.cpp
  |-CMakeLists.txt (1)
  |-TestsFolder
    |-UnitTests.cpp
    |-CMakeLists.txt  (3)
  </code></pre>
  
  <p>In CMakeLists.txt (1) I'd like to set compile options globally because I assume that optimization level and other flags should be the same for all libraries of the project. 
  <code>add_compile_options(-Wall -Werror -Wno-error=maybe-uninitialized)</code> is used to achieve it.</p>
  
  <p>Also I use <code>CMAKE_BUILD_TYPE</code> feature which automatically sets needed optimization level with help of <code>CMAKE_CXX_FLAGS_RELEASE</code> or <code>CMAKE_CXX_FLAGS_DEBUG</code> flags.</p>
  
  <p>Also the fact that afterwards one of these variables is implicitly passed to the compiler seems to be annoying, becaues I anyway have to set these variables in advance for needed optimization flags <code>set(CMAKE_CXX_FLAGS_RELEASE "-Ofast -DNDEBUG -DBOOST_DISABLE_ASSERTS")</code> only because <code>-O3</code> is by defaut set with Release configuration.</p>
  
  <p>Anyway everything is ok up to the moment when I want to alway have <code>-O0</code> being set when compiling test environment (UniTests.cpp). CMakeLists.txt (3) produces project_ut executable. I want WorkerLib to be compiled with configured optimization level (taked from <code>CMAKE_BUILD_TYPE</code>), but it automatically means that <code>CMAKE_CXX_FLAGS_RELEASE</code> with <code>-Ofast</code> is propagated to UnitTest.cpp</p>
  
  <p>I guess that I might be doing something strange here and there is a better way to deal with an issue.
  One option here is to pass optimization flags without help of <code>CMAKE_BUILD_TYPE</code> feature but it seems to be a wrong (as I do not want to maintain lists of flags for every target).</p>
  
  <p>EDIT:</p>
  
  <pre><code>#CMakeLists.txt(1):
  cmake_minimum_required(VERSION 3.14.1)
  project(TestProject LANGUAGES CXX C)
  
  #####  common comp flags #####
  
  add_compile_options(-Wall -Werror -Wno-error=maybe-uninitialized)
  
  ##############################
  
  set(RELEASE_FLAGS "-Ofast -DNDEBUG -DBOOST_DISABLE_ASSERTS")
  set(DEBUG_FLAGS "-O0 -ggdb3")
  
  set(CMAKE_CXX_FLAGS_RELEASE ${RELEASE_FLAGS})
  set(CMAKE_C_FLAGS_RELEASE ${RELEASE_FLAGS})
  
  set(CMAKE_CXX_FLAGS_DEBUG ${DEBUG_FLAGS})
  set(CMAKE_C_FLAGS_DEBUG ${DEBUG_FLAGS})
  
  add_subdirectory(WorkerLibFolder)
  add_subdirectory(TestsFolder)
  
  
  add_executable(mainExec main.cpp)
  target_link_libraries(mainExec PRIVATE worker)
  
  </code></pre>
  
  <pre><code>#CMakeLists.txt(3):
  add_executable(tests UnitTests.cpp)
  target_link_libraries(tests PRIVATE worker)
  #I want sommehow fix optimization flags for that particular target, while for worker library left them as they were set
  </code></pre>
  
  <pre><code>#CMakeLists.txt(2):
  add_library(worker Worker.cpp)
  target_include_directories(worker PUBLIC ${CMAKE_CURRENT_LIST_DIR})
  </code></pre>