[CMake] Trouble with conditional generator expression inside target_link_libraries

Andrew Fuller afuller at teradici.com
Thu Oct 4 12:15:41 EDT 2018


What about this:


list(APPEND FOO_LIBRARIES_OPT foo)
list(APPEND FOO_LIBRARIES_DBG foo_d)

target_link_libraries(my_exe
   PUBLIC
        debug "$<$<BOOL:${USE_FOOLIB}>:${FOO_LIBRARIES_DBG}>"
        optimized "$<$<BOOL:${USE_FOOLIB}>:${FOO_LIBRARIES_OPT}>"
        "$<$<BOOL:${USE_BARLIB}>:bar>"
)

A little more verbose.

________________________________
From: Björn Blissing <bjorn.blissing at vti.se>
Sent: October 4, 2018 9:00:28 AM
To: Andrew Fuller; Eric Noulard
Cc: CMake Mailinglist
Subject: RE: [CMake] Trouble with conditional generator expression inside target_link_libraries


Hi Andrew,



When I put the genex inside double quotes I get:


optimized.lib;foo.lib;debug.lib;foo_d.lib;bar.lib; --- for both debug and release builds

Regards,

Björn





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



What happens if you put the genex inside double quotes?



target_link_libraries(my_exe

    PUBLIC

         "$<$<BOOL:${USE_FOOLIB}>:${FOO_LIBRARIES}>"

         "$<$<BOOL:${USE_BARLIB}>:bar>"

)



________________________________

From: CMake <cmake-bounces at cmake.org<mailto:cmake-bounces at cmake.org>> on behalf of Björn Blissing <bjorn.blissing at vti.se<mailto:bjorn.blissing at vti.se>>
Sent: October 4, 2018 8:49:19 AM
To: Eric Noulard
Cc: CMake Mailinglist
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries



Hi Eric,



I tried to do a self contained minimal example:



cmake_minimum_required(VERSION 3.12)

project(expension_error LANGUAGES CXX)



add_executable(my_exe main.cpp)



option(USE_FOOLIB "Use foo.lib" ON)

option(USE_BARLIB "Use bar.lib" ON)



list(APPEND FOO_LIBRARIES optimized foo)

list(APPEND FOO_LIBRARIES debug foo_d)





target_link_libraries(my_exe

    PUBLIC

         $<$<BOOL:${USE_FOOLIB}>:${FOO_LIBRARIES}>

         $<$<BOOL:${USE_BARLIB}>:bar>

)



But when I run this script using CMake 3.12.2, it expands to something even worse:



$<1:optimized;foo.lib;foo_d>.lib;bar.lib --- for debug builds

$<1:optimized;foo.lib;>.lib;bar.lib-- for release builds



So something goes really wrong when I try to use a list inside a conditional generator expression inside target_link_libraries().



Regards,

Björn







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





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

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.



Sometimes the devil is hiding in the details.

Do ou manage to reproduce the genex expansion error on a toy example?



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.



You can perfectly write a CMake helper function which takes MYLIB_LIBRARIES as input and spit out MYLIBS_DEBUG, MYLIBS_OPTIMIZED as an output.

This way you don't have to rewrite 3rd party code and keep your code clean.



--

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


More information about the CMake mailing list