<div dir="ltr"><br><div class="gmail_extra"><div class="gmail_quote">On Mon, Mar 19, 2018 at 8:25 AM, Saad Khattak <span dir="ltr"><<a href="mailto:saadrustam@gmail.com" target="_blank">saadrustam@gmail.com</a>></span> wrote:<br><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 dir="ltr">Thank you for the clarification Craig.<span class="gmail-"><div><br></div><div>>> <span style="color:rgb(33,33,33)">If you made your<span class="gmail-m_-6642983747251034497inbox-inbox-Apple-converted-space"> </span></span><font face="monospace, monospace" style="color:rgb(33,33,33)">main</font><span style="color:rgb(33,33,33)"><span class="gmail-m_-6642983747251034497inbox-inbox-Apple-converted-space"> </span>target link against<span class="gmail-m_-6642983747251034497inbox-inbox-Apple-converted-space"> </span></span><font face="monospace, monospace" style="color:rgb(33,33,33)">LibA</font><span style="color:rgb(33,33,33)">, you'd see that CMake automatically expands out the generator expressions when it constructs the link command line for<span class="gmail-m_-6642983747251034497inbox-inbox-Apple-converted-space"> </span></span><font face="monospace, monospace" style="color:rgb(33,33,33)">main</font><span style="color:rgb(33,33,33)">, so the example as it stands doesn't actually have any error.</span></div><div><span style="color:rgb(33,33,33)"><br></span></div></span><div><font color="#212121">In my actual project, a library like LibA has .cmake files that can be included by dependent projects to reuse. Of course, CMake `include` does not get affected by target_link_libraries. </font></div><div><font color="#212121"><br></font></div><div><font color="#212121">I would like dependent projects to have the ability to use an existing clone of LibA, if found (e.g. using find_package), otherwise clone locally for using ExternalProject_Add which I am now trying to refactor to FetchContent. Another option open to dependent projects is to force the FetchContent on all repositories regardless of what find_package returns (this is great for testing locally to ensure I have no uncommitted, unpushed changes).</font></div><div><font color="#212121"><br></font></div><div><font color="#212121">Using existing clones is a way to ensure that every project dependent on LibA doesn't clone it's own copy (unless forced to do so).</font></div><div><font color="#212121"><br></font></div><div><font color="#212121">To allow the possibility for any dependent project to include the .cmake files properly, I get the INTERFACE_INCLUDE_DIRECTORIES target property (using get_target_property) for LibA and then include the .cmake utility files like this:</font></div><div><font color="#212121"><br></font></div><div><font color="#212121">include(${LibA_INCLUDE_<wbr>DIRECTORY}/my_utility.cmake)</font></div><div><font color="#212121"><br></font></div><div><font color="#212121">Perhaps I am approaching the problem incorrectly? Essentially, I would like a way to reliably include .cmake files found in LibA's include folder regardless of how LibA was acquired (e.g. cloned and built and/or installed separately or acquired using FetchContent). In my case, I could only think of the above as a reliable way to include the files.</font></div></div></blockquote><div><br></div><div>Rather than focusing on including files, I would choose to base things around targets. If the rest of your project simply links against the required target name, the target itself should bring with it any transitive dependencies such as required header search paths, dependent libraries that must also be linked, etc. These are the INTERFACE_... properties that can be set on a target, usually by commands such as target_include_directories(foo INTERFACE ...), target_link_libraries(foo INTERFACE ...), etc. When building directly from your source tree, the targets will be directly available. When using find_package(), the package should provide import targets. The rest of your project shouldn't really need to care which one of the two it is dealing with. This is the recommended way to structure a situation like this. You shouldn't need to also pull in a secondary .cmake file in most circumstances, but maybe I'm missing something about your particular situation.<br></div><div><br></div><div> </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 dir="ltr"><div><br></div></div><div class="gmail-HOEnZb"><div class="gmail-h5"><div class="gmail_quote"><div dir="ltr">On Sun, Mar 18, 2018 at 4:22 PM Craig Scott <<a href="mailto:craig.scott@crascit.com" target="_blank">craig.scott@crascit.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 dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Mar 19, 2018 at 3:44 AM, Saad Khattak <span dir="ltr"><<a href="mailto:saadrustam@gmail.com" target="_blank">saadrustam@gmail.com</a>></span> wrote:<br><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 dir="ltr">Absolutely. Please find the example project here: <a href="https://github.com/samaursa/cmake_fetch_content_and_generator_expressions" target="_blank">https://github.com/<wbr>samaursa/cmake_fetch_content_<wbr>and_generator_expressions</a> <div><br></div><div>The repository README also includes the output from running `./setup.sh`. </div></div></blockquote><div><br></div><div><br></div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Okay that's much clearer, thanks. The example is doing what I'd expect and the generator expressions are also expected to be visible at the point your example is printing them. If you made your <font face="monospace, monospace">main</font> target link against <font face="monospace, monospace">LibA</font>, you'd see that CMake automatically expands out the generator expressions when it constructs the link command line for <font face="monospace, monospace">main</font>, so the example as it stands doesn't actually have any error.</div><div><br></div><div>Generator expressions are not expanded when CMake is processing the files (called the <i>configure</i> stage), they are expanded only when writing out the Makefiles during the <i>generation</i> stage. When running cmake from the command line, one doesn't tend to think of these two phases as being distinct, but you can see it at the end of the cmake log with these two messages:</div><div><br></div><div><div>-- Configuring done</div><div>-- Generating done</div></div><div><br></div><div>It is clearer when using the CMake GUI application because you get two different buttons, one for Configure and another for Generate, so you have to trigger both phases manually. So if you look at the contents of various properties and variables with CMake commands like <font face="monospace, monospace">message(...)</font>, you are doing that during the configure stage and therefore will see unexpanded generator expressions.</div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div><br></div><div> </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 class="gmail-m_-6642983747251034497m_536526476433205802gmail-HOEnZb"><div class="gmail-m_-6642983747251034497m_536526476433205802gmail-h5"><br><div class="gmail_quote"><div dir="ltr">On Sat, Mar 17, 2018 at 6:47 PM Craig Scott <<a href="mailto:craig.scott@crascit.com" target="_blank">craig.scott@crascit.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 dir="ltr">Can you provide a small project example that can be used to demonstrate your problem? The specifics of how you are doing things may be important.<br><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote"></div></div><div class="gmail_extra"><div class="gmail_quote">On Sun, Mar 18, 2018 at 8:12 AM, Saad Khattak <span dir="ltr"><<a href="mailto:saadrustam@gmail.com" target="_blank">saadrustam@gmail.com</a>></span> wrote:<br></div></div><div class="gmail_extra"><div class="gmail_quote"><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 dir="ltr">Hi,<div><br></div><div>ExternalProject_Add builds, generates and installs and thus any generator expressions used will be expanded by the time another library uses it. </div><div><br></div><div>For example, if I add a library LibA using ExternalProject_Add, I can then query the target property INTERFACE_INCLUDE_DIRECTORIES and get the include directory for the library:</div><div><br></div><div>get_target_property(LibA_<wbr>INCLUDE_DIRECTORIES LibA</div><div>  INTERFACE_INCLUDE_DIRECTORIES</div><div>  )</div><div><br></div><div>This allows me to then use the variable LibA_INCLUDE_DIRECTORIES in my CMake include(...) statements. However, with FetchContent, this is no longer possible.</div><div><br></div><div>The reason I would like to query a variable is because at project generation time, I invoke FetchContent only if LibA is not found by find_package(LibA). </div><div><br></div><div>Thus, the include directory for LibA may be in the current build directory (through FetchContent) OR it may be found in the folder where LibA is cloned by the user and generated/built OR an INSTALL of LibA.</div><div><br></div><div>Perhaps I should not be using `get_target_property` to get the a library's include directory? Either way, would like some guidance in solving this issue.</div><div><br></div><div>Thank you,</div><div>Saad</div><div></div></div></blockquote></div></div></blockquote></div></div></div></blockquote></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail-m_-6642983747251034497m_536526476433205802gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><br></div></div></div></div></div></div></div>
</div></div></blockquote></div>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><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></div></div></div></div></div>
</div></div>