[CMake] Generator Expressions and FetchContent

Craig Scott craig.scott at crascit.com
Sun Mar 18 16:22:10 EDT 2018


On Mon, Mar 19, 2018 at 3:44 AM, Saad Khattak <saadrustam at gmail.com> wrote:

> Absolutely. Please find the example project here: https://github.com/
> samaursa/cmake_fetch_content_and_generator_expressions
>
> The repository README also includes the output from running `./setup.sh`.
>


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 main target link against LibA,
you'd see that CMake automatically expands out the generator expressions
when it constructs the link command line for main, so the example as it
stands doesn't actually have any error.

Generator expressions are not expanded when CMake is processing the files
(called the *configure* stage), they are expanded only when writing out the
Makefiles during the *generation* 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:

-- Configuring done
-- Generating done

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 message(...), you are
doing that during the configure stage and therefore will see unexpanded
generator expressions.




>
> On Sat, Mar 17, 2018 at 6:47 PM Craig Scott <craig.scott at crascit.com>
> wrote:
>
>> 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.
>>
>>
>> On Sun, Mar 18, 2018 at 8:12 AM, Saad Khattak <saadrustam at gmail.com>
>> wrote:
>>
>>> Hi,
>>>
>>> ExternalProject_Add builds, generates and installs and thus any
>>> generator expressions used will be expanded by the time another library
>>> uses it.
>>>
>>> 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:
>>>
>>> get_target_property(LibA_INCLUDE_DIRECTORIES LibA
>>>   INTERFACE_INCLUDE_DIRECTORIES
>>>   )
>>>
>>> This allows me to then use the variable LibA_INCLUDE_DIRECTORIES in my
>>> CMake include(...) statements. However, with FetchContent, this is no
>>> longer possible.
>>>
>>> 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).
>>>
>>> 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.
>>>
>>> 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.
>>>
>>> Thank you,
>>> Saad
>>>
>>>
>>> --
>>>
>>> Powered by www.kitware.com
>>>
>>> Please keep messages on-topic and check the CMake FAQ at:
>>> http://www.cmake.org/Wiki/CMake_FAQ
>>>
>>> Kitware offers various services to support the CMake community. For more
>>> information on each offering, please visit:
>>>
>>> CMake Support: http://cmake.org/cmake/help/support.html
>>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>>
>>> Visit other Kitware open-source projects at http://www.kitware.com/
>>> opensource/opensource.html
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> https://cmake.org/mailman/listinfo/cmake
>>>
>>>
>>
>>
>> --
>> Craig Scott
>> Melbourne, Australia
>> https://crascit.com
>>
>


-- 
Craig Scott
Melbourne, Australia
https://crascit.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180319/11d08add/attachment.html>


More information about the CMake mailing list