<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Nov 19, 2019 at 10:36 PM Eric Doenges <<a href="mailto:doenges@mvtec.com">doenges@mvtec.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF">
    <div>Am 19.11.19 um 12:09 schrieb Stéphane
      Ancelot:<br>
    </div>
    <blockquote type="cite">
      
      <p>Hi,</p>
      <p>I have a particular target (using swig / jni) that must not
        have -O3 -NDEBUG flags from Relase build type</p>
      <p>How can I overload this target flags ?</p>
    </blockquote>
    Since CMAKE_<lang>_FLAGS_RELEASE does not map to a
    user-visible target property, you cannot override it directly
    (anything added to the COMPILE_FLAGS property is appended to the
    command line and does not replace any of the compiler flags CMake
    sets by itself). However, you can redefine this variable before you
    create your special target(s), and then restore it so that other
    targets are not affected:<br>
    <p>set(_CMAKE_CXX_FLAGS_RELEASE_SAVE ${CMAKE_CXX_FLAGS_RELEASE})<br>
      set(CMAKE_CXX_FLAGS_RELEASE)<br>
      < add your target(s) here ><br>
      set(CMAKE_CXX_FLAGS_RELEASE ${_CMAKE_CXX_FLAGS_RELEASE_SAVE})<br>
    </p>
    <p>This assumes your target is C++; if it is C, simply replace the
      _CXX_ with _C_.</p></div></blockquote></div><div><br></div><div>Actually, no, that's not how it works. This is actually a great example of why projects shouldn't generally try to avoid manipulating CMAKE_CXX_FLAGS and should instead prefer to modify target or directory properties instead (not possible here for the original problem, but worth highlighting nonetheless). The (often surprising) behavior at play in the proposed example above is that it is not the value of the CMAKE_CXX_FLAGS_RELEASE variable at the time the target is created that matters, it's the variable's value <i>at the end of the directory scope</i>. You can change the variable's value as much as you like along the way before or after creating targets, but only the final value at the end of the scope will actually be used in the build command lines for targets created in that directory scope. This is rarely what developers expect, but that's how it works, for better or worse. When you start adding in calls to add_subdirectory(), it can get really confusing what value is getting used where, so take great care if your project really must modify these variables.</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Craig Scott<br><div>Melbourne, Australia</div><div><a href="https://crascit.com" target="_blank">https://crascit.com</a><br></div><div><br></div><div>Get the hand-book for every CMake user: <a href="https://crascit.com/professional-cmake/" target="_blank">Professional CMake: A Practical Guide</a><br></div><div>Consulting services (CMake, C++, build/release processes): <a href="https://crascit.com/services" target="_blank">https://crascit.com/services</a></div></div></div></div></div></div></div></div></div></div></div></div>