[cmake-developers] conditionals in generator expressions

Brad King brad.king at kitware.com
Tue Aug 21 10:10:29 EDT 2012


On 08/20/2012 04:25 PM, Stephen Kelly wrote:
> I've implemented it like that now. That will also work for 
> compile_definitions, but I think it won't work for link_libraries. I'm also 
> not sure about link_directories, compiler_flags and linker_flags. For 
> link_libraries it is quite unfortunate, because it would make things like 
> 
>   $<${USE_FOO}:${FOO_LIBRARIES}>
> 
> not work as expected:
> 
> /usr/lib/icecc/bin/c++       CMakeFiles/fooex.dir/newex.cpp.o  -o fooex -
> rdynamic libfoo.so libfoo.so $<1:Qt5::Widgets -lQt5::Sql> -Wl,-
> rpath,/home/stephen/dev/src/playground/cmake/build 
> g++: error: $<1:Qt5::Widgets: No such file or directory
> 
> I think this needs to be fixed, but I get the feeling it might require 
> deeper redesign of the link libraries logic?

Quoting should work:

 "$<${USE_FOO}:${FOO_LIBRARIES}>"

to ensure it is one complete expression.  It would be nice if we
didn't need to ask the user to do that though.  More below.

>> It should be in cmTarget::ComputeLinkImplementation at this line:

If it is not that early then we need to consider the interaction
of at least these pieces:

 cmTarget::GetLinkerLanguage
 cmTarget::GetLinkClosure
 cmTarget::ComputeLinkClosure
 cmTarget::GetLinkImplementation
 cmTarget::ComputeLinkImplementation
 cmComputeLinkInformation
 cmComputeLinkDepends

They all deal with the link libraries and need the processed
output of generator expressions.

> I tried this, but it made (5) below not work anymore. The generator 
> expression was evaluated too early it seems for Qt5::Core, and at that point 
> TYPE == SHARED_LIBRARY.

Way back when we discussed the $<TARGET_PROPERTY_...> expressions
we concluded that the target could be implicit because they are
always evaluated in the context of a target.  However, in the case
of linking there are multiple targets involved so we do not know
which one the user may mean.

In (5) you need to delay evaluation of generator expressions as
long as possible, essentially until the computation of the final
link line for a specific target.  The same expressions may be
evaluated multiple times in different contexts and yield different
results e.g. link Qt5::Core into a GUI and a non-GUI executable.

Delayed evaluation means we need to handle list expansion cleanly.
One approach is to try to re-work all the above-named methods to
keep the original link information in pure-string form instead of
performing ExpandListArgument early.  The target_link_libraries
command would set a string-valued LINK_LIBRARIES target property
instead of recording some internal C++ structure, just like the
LINK_INTERFACE_LIBRARIES property.  This conversion would be
similar to the INCLUDE_DIRECTORIES target property behavior we
recently introduced.  Add an interface in cmGeneratorTarget to
evaluate generator expressions in the LINK_LIBRARIES and
LINK_INTERFACE_LIBRARIES properties given the context of some
target for which the final link library list is being computed.

-Brad



More information about the cmake-developers mailing list