<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">Am 01.11.19 um 11:02 schrieb Stephen
      Morris:<br>
    </div>
    <blockquote type="cite"
cite="mid:VE1PR06MB6350867023D11505C22C0CBFC9620@VE1PR06MB6350.eurprd06.prod.outlook.com">
      <pre class="moz-quote-pre" wrap="">I'm setting up a custom build step to build a precompiled header in gcc (yes I know that native support is coming with CMake 3.16, but I need to get something working on an older version).

My approach is basically to set up a custom command thus:

set_target_properties(mytarget PRIVATE CXX_STANDARD 17)
set_target_properties(mytarget PRIVATE  POSITION_INDEPENDENT CODE_1)
set(CMAKE_VERBOSE_MAKEFILE TRUE)
...
...
set(CXX_FLAGS ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG})   .. or whatever, depending on the current configuration..
get_target_property(compile_options, mytarget, COMPILE_OPTIONS)
add_custom_command(OUTPUT myheader.h.gch
                                           COMMAND ${CMAKE_CXX_COMPILER} ${CXX_FLAGS} ${compile_options} -fPIC -std=gnu++17 -c myheader.h -o myheader.h.gch
                                           DEPENDS myheader.h)
add_custom_target(BuildMyPCH
                                     DEPENDS myheader.h.gch)
add_dependencies(mytarget, BuildMyPCH)

You'll note that in my custom command I've had to specify -fPIC and -std-gnu++17 explicitly, because they aren't included among either CXX_FLAGS or COMPILE_OPTIONS. I've looked for them among the  COMPILE_DEFINITIONS, COMPILE_FEATURES and COMPILE_FLAGS properties of my target as well, but there's no sign of them. And yet if I look at the verbose output when gcc builds other files using the CMake-generated makefile, I see that they are being included automatically.

So, my question: where is CMake storing these settings, and how can I apply them in my custom command without having to do so manually?</pre>
    </blockquote>
    <p>As far as I am aware, there is no way of querying the entirety of
      the command line arguments applied by CMake - you will need to
      recreate CMake's behavior manually. In case of -fPIC, this could
      look like this (not tested, so there may be syntax errors, but the
      principle should work):</p>
    <p><tt>...</tt><tt><br>
      </tt><tt>get_target_property(_tmp mytarget
        POSITION_INDEPENDENT_CODE)</tt><tt><br>
      </tt><tt>if(_tmp AND CMAKE_CXX_COMPILE_OPTIONS_PIC)</tt><tt><br>
      </tt><tt>  string(APPEND CXX_FLAGS "
        ${CMAKE_CXX_COMPILE_OPTIONS_PIC}")</tt><tt><br>
      </tt><tt>endif()</tt><tt><br>
      </tt><tt>...</tt><br>
    </p>
    <p>If you have multiple targets that may have different compile
      options you will probably want to put this in a function that
      takes a target as input and returns the compiler flags as output.</p>
    <p>To figure out the names of the variables CMake uses to hold flags
      to pass to the compiler, search the
      <...>/share/cmake-3.xx/Modules/Compiler directory.</p>
    <p>
    </p>
    <blockquote type="cite"
cite="mid:VE1PR06MB6350867023D11505C22C0CBFC9620@VE1PR06MB6350.eurprd06.prod.outlook.com"></blockquote>
    <div class="moz-signature">-- <br>
      <div style="width:480px; text-align: left; font-family: Arial,
        Helvetica, sans-serif;">
        <p style="color: #062d64; font-size: 14px; text-align: left;
          font-family: Arial, Helvetica, sans-serif;">
          <b>Dr. Eric Dönges</b>
          <br>
          Senior Software Engineer
        </p>
        <p style="color: #062d64; font-size: 12px; text-align: left;
          font-family: Arial, Helvetica, sans-serif;">MVTec Software
          GmbH | Arnulfstr. 205 | 80634 Munich | Germany
          <br>
          <a style="font-size: 12px; font-family: Arial, Helvetica,
            sans-serif; color: #062d64;"
            href="mailto:mustermann@mvtec.com">doenges@mvtec.com</a> |
          Tel: +49 89 457 695-0 | <a style="font-size: 12px;
            font-family: Arial, Helvetica, sans-serif; color: #062d64;"
            href="http://www.mvtec.com">www.mvtec.com</a>
          <br>
        </p>
        <p>
          <span style="font-size: 12px; font-family: Arial, Helvetica,
            sans-serif; color: #062d64;">Find our privacy policy <a
              style="font-size: 12px; font-family: Arial, Helvetica,
              sans-serif; color: #062d64;"
              href="https://www.mvtec.com/imprint">here</a>.</span> </p>
        <p style="color: #062d64; font-size: 12px; text-align: left;
          font-family: Arial, Helvetica, sans-serif;"><img
src="https://mvtec.com/fileadmin/Redaktion/newsletter/mail-signature/newsletter-icon.png"
            valign="bottom" width="16" height="16"> <a style="font-size:
            12px; font-family: Arial, Helvetica, sans-serif; color:
            #062d64; font-weight: bold;"
            href="https://www.mvtec.com/newsletter">Sign up</a> for our
          MVTec Newsletter!</p>
        <p style="margin: 0px; color: #666; font-size: 12px; text-align:
          left; font-family: Arial, Helvetica, sans-serif;">Geschäftsführer:
          Dr. Wolfgang Eckstein, Dr. Olaf Munkelt<br>
          Amtsgericht München HRB 114695</p>
        <p style="margin: 0px; color: #666; font-size: 12px; text-align:
          left; font-family: Arial, Helvetica, sans-serif;"> </p>
        <img
src="https://www.mvtec.com/fileadmin/Redaktion/newsletter/mail-signature/mvtec-logo-line.png"
          alt="MVTec Software GmbH Logo"></div>
    </div>
  </body>
</html>