[CMake] Trouble with conditional generator expression inside target_link_libraries

Björn Blissing bjorn.blissing at vti.se
Thu Oct 4 10:53:51 EDT 2018


Hello Eric,

The minimal example was just to display the expansion error. In real life the code uses a Boolean variable (and the rest of the CMake code is much larger as well). It was just to show the expansion error you get if you try to use a conditional generator expression inside a target_link_libraries function.

I do agree that using it would be simpler if I could use:
     $<$<CONFIG:Debug>:${MYLIBS_DEBUG}>
     $<$<CONFIG:Release>:${MYLIBS_OPTIMIZED}>

But since I use a third party find module the MYLIB_LIBRARIES variable is not separated into these categories. I was hoping to avoid rewriting this external find module.

Regards,
Björn

From: Eric Noulard <eric.noulard at gmail.com>
Sent: Thursday, October 4, 2018 4:37 PM
To: Björn Blissing <bjorn.blissing at vti.se>
Cc: CMake Mailinglist <cmake at cmake.org>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries


Le jeu. 4 oct. 2018 à 16:06, Björn Blissing <bjorn.blissing at vti.se<mailto:bjorn.blissing at vti.se>> a écrit :
Hello,

I am having trouble with a conditional generator expression inside target link libraries, which seems to expand incorrectly (or at least not the way I expect).

In short, I have a list of libraries that is found by a CMake module. The list is composed of a number of elements each preceded by “optimized” or “debug”, depending on if there are built for release or debug. These libraries should only be included if a boolean is set to true. I have tried to write a generator expression which only includes the list of libraries if this variable is set. But it seems that this expression gets expanded wrong.

If I try to condense the problem to a minimum example, it would be something like this:

LIST(APPEND MYLIB_LIBRARIES optimized foo)
LIST(APPEND MYLIB_LIBRARIES debug foo_d)

TARGET_LINK_LIBRARIES(target_name
    PUBLIC
        $<$<BOOL:TRUE>:${MYLIB_LIBRARIES}>
)

I don't quite understand your usage here. The genex $<BOOL:TRUE> is always true?
You must have something like:

$<$<BOOL:${CONDVAR}>:${MYLIB_LIBRARIES}>

with CONDVAR computed before that to HOLD TRUE/1 or FALSE/0
or something similar?

I would try to build a proper MYLIBS_DEBUG and MYLIBS_OPTIMIZED list out of your MYLIB_LIBRARIES var

and then:

target_link_libraries(target_name
   PUBLIC
     $<$<CONFIG:Debug>:${MYLIBS_DEBUG}>
     $<$<CONFIG:Release>:${MYLIBS_OPTIMIZED}>

which may be easier to read.

Or may be I missed something in your explanation.


This generator expression makes my target depend of the following libraries for the release build:
foo.lib & optimized.lib

And for the debug build:
foo_d.lib, foo.lib & optimized.lib

Which is obviously incorrect.

Replacing the generator expression with an IF-statement instead works. But I would like to stick to generator expressions if possible to keep my CMake code consistent.

How should I rewrite my generator expression to get the correct expansion?

Regards,
Björn

--

Powered by www.kitware.com<http://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


--
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20181004/3d7481bd/attachment-0001.html>


More information about the CMake mailing list