title: How do I pass gcc optimization level to cmake targets?
body: <p>I have a C project which uses CMake to build with three targets for each library type (static, dynamic and shared):</p>
  
  <pre><code>add_library(contact-static STATIC EXCLUDE_FROM_ALL lib/contact.c)
  add_library(contact-shared SHARED EXCLUDE_FROM_ALL lib/contact.c)
  
  add_executable(contactbook src/main.c)
  target_link_libraries(contactbook contact-static)
  
  add_executable(contactbook-static EXCLUDE_FROM_ALL src/main.c)
  target_link_libraries(contactbook-static contact-static)
  add_dependencies(contactbook-static contact-static)
  
  add_executable(contactbook-shared EXCLUDE_FROM_ALL src/main.c)
  add_dependencies(contactbook-shared contact-shared)
  target_link_libraries(contactbook-shared contact-shared)
  
  add_executable(contactbook-dynamic EXCLUDE_FROM_ALL src/main.c)
  target_link_libraries(contactbook-dynamic contact-shared)
  target_compile_definitions(contactbook-dynamic PRIVATE DYNLIB=true)
  target_compile_options(contactbook-dynamic PRIVATE -rdynamic)
  target_link_libraries(contactbook-dynamic dl)
  </code></pre>
  
  <p>The problem is that I have to provide optimization level passing to the <code>make</code> command.</p>
  
  <p>I have already tried (but in both I've got stuck):</p>
  
  <ul>
  <li>passing a variable through make comman like <code>make -Dopt:STRING=0</code> but i have no idea how to use this as compiler flag</li>
  <li>creating targets for each optimization level, but I can't add definitions to <code>add_custom_target</code> targets</li>
  </ul>
  
  <p>So how can I do that and what is the best practice for passing optimization level to cmake target?</p>