Trouble with conditional generator expression inside target_link_libraries

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Trouble with conditional generator expression inside target_link_libraries

Björn Blissing

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}>

)

 

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

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Eric Noulard


Le jeu. 4 oct. 2018 à 16:06, Björn Blissing <[hidden email]> 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

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

--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Björn Blissing

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 <[hidden email]>
Sent: Thursday, October 4, 2018 4:37 PM
To: Björn Blissing <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

 

Le jeu. 4 oct. 2018 à 16:06, Björn Blissing <[hidden email]> 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

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


--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Eric Noulard


Le jeu. 4 oct. 2018 à 16:53, Björn Blissing <[hidden email]> 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

--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Björn Blissing

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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:10 PM
To: Björn Blissing <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

 

Le jeu. 4 oct. 2018 à 16:53, Björn Blissing <[hidden email]> 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


--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Andrew Fuller-2

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 <[hidden email]> on behalf of Björn Blissing <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:10 PM
To: Björn Blissing <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

 

Le jeu. 4 oct. 2018 à 16:53, Björn Blissing <[hidden email]> 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


--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Björn Blissing

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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:54 PM
To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
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 <[hidden email]> on behalf of Björn Blissing <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:10 PM
To: Björn Blissing <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

 

Le jeu. 4 oct. 2018 à 16:53, Björn Blissing <[hidden email]> 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


--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Andrew Fuller-2

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 <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:54 PM
To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
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 <[hidden email]> on behalf of Björn Blissing <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:10 PM
To: Björn Blissing <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

 

Le jeu. 4 oct. 2018 à 16:53, Björn Blissing <[hidden email]> 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


--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Björn Blissing

Hi Andrew,

 

That works, but as previously said. The third party find module I am using do not differentiate between debug and release libraries. To make matters even worse the keywords “optimized” and “debug” is already in the variable list, trying to split them will be painful.

 

The workaround I am using right now is to have an IF-statement for the list option:

if(${USE_FOOLIB})

    target_link_libraries(my_exe

    PUBLIC

        ${FOO_LIBRARIES}

    )

endif()

 

 

target_link_libraries(my_exe

    PUBLIC

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

)

 

But that breaks the pattern with using generator expressions, as I do for the rest of my options.

 

I don’t know if this should be considered a bug, but it seems really strange that generator expressions should break down for only this special case, i.e. the combination using lists with the conditional operator BOOL and using it inside target_link_libraries.

 

I don’t know if other CMake functions will react similarly bad to the list/bool operator combo.

 

Regards,

Björn

 

 

From: Andrew Fuller <[hidden email]>
Sent: Thursday, October 4, 2018 6:16 PM
To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

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 <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:54 PM
To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
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 <[hidden email]> on behalf of Björn Blissing <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:10 PM
To: Björn Blissing <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

 

Le jeu. 4 oct. 2018 à 16:53, Björn Blissing <[hidden email]> 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


--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Marc CHEVRIER
I am afraid that you cannot mix "optimized" or "debug" keywords with "generator expressions" because keywords handling is done during evaluation of command "target_link_libraries" and "generator expressions" are evaluated during generation.

And target_link_libraries expect following pattern: [<keyword>] <library> [<library> ...], so specifying a generator expression wrapping this breaks the parsing of the arguments: keyword is no longer at the beginning of the sequence so it is no longer  recognized as is...

So, The most efficient way to work-around this problem is to transform your list of libraries in valid generator expressions:
  • INITIAL: optimized foo debug food_d
  • RESULT: $<$<CONFIG:RELEASE>:foo> $<$<CONFIG:DEBUG>:foo_d>
For that purpose, an helper function can do the trick...

Le jeu. 4 oct. 2018 à 18:34, Björn Blissing <[hidden email]> a écrit :

Hi Andrew,

 

That works, but as previously said. The third party find module I am using do not differentiate between debug and release libraries. To make matters even worse the keywords “optimized” and “debug” is already in the variable list, trying to split them will be painful.

 

The workaround I am using right now is to have an IF-statement for the list option:

if(${USE_FOOLIB})

    target_link_libraries(my_exe

    PUBLIC

        ${FOO_LIBRARIES}

    )

endif()

 

 

target_link_libraries(my_exe

    PUBLIC

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

)

 

But that breaks the pattern with using generator expressions, as I do for the rest of my options.

 

I don’t know if this should be considered a bug, but it seems really strange that generator expressions should break down for only this special case, i.e. the combination using lists with the conditional operator BOOL and using it inside target_link_libraries.

 

I don’t know if other CMake functions will react similarly bad to the list/bool operator combo.

 

Regards,

Björn

 

 

From: Andrew Fuller <[hidden email]>
Sent: Thursday, October 4, 2018 6:16 PM


To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

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 <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:54 PM
To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
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 <[hidden email]> on behalf of Björn Blissing <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:10 PM
To: Björn Blissing <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

 

Le jeu. 4 oct. 2018 à 16:53, Björn Blissing <[hidden email]> 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

--

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

--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Eric Noulard
In reply to this post by Björn Blissing


Le jeu. 4 oct. 2018 à 18:34, Björn Blissing <[hidden email]> a écrit :

Hi Andrew,

 

That works, but as previously said. The third party find module I am using do not differentiate between debug and release libraries. To make matters even worse the keywords “optimized” and “debug” is already in the variable list, trying to split them will be painful.

 

The workaround I am using right now is to have an IF-statement for the list option:

if(${USE_FOOLIB})

    target_link_libraries(my_exe

    PUBLIC

        ${FOO_LIBRARIES}

    )

endif()

 

 

target_link_libraries(my_exe

    PUBLIC

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

)

 

But that breaks the pattern with using generator expressions, as I do for the rest of my options.

 

I don’t know if this should be considered a bug, but it seems really strange that generator expressions should break down for only this special case, i.e. the combination using lists with the conditional operator BOOL and using it inside target_link_libraries.


I think you are right there is a bug.
When you put a list on the right hand-side of $<BOOL:, one gets unevaluated output.

If you try

>>>>> cut here <<<<
cmake_minimum_required(VERSION 3.12)

project(expansion_error LANGUAGES CXX)
add_executable(my_exe main.cpp)

option(USE_ANYLIB "Use foo.lib" ON)
option(USE_BARLIB "Use bar.lib" ON)

list(APPEND ANY_LIBRARIES any1 any2)

target_link_libraries(my_exe
    PUBLIC
         $<$<BOOL:${USE_ANYLIB}>:"${ANY_LIBRARIES}">
         $<$<BOOL:${USE_BARLIB}>:bar>
)

add_custom_target(ShowMe
    COMMAND ${CMAKE_COMMAND} -E echo $<$<BOOL:${USE_ANYLIB}>:${ANY_LIBRARIES}>
    COMMAND ${CMAKE_COMMAND} -E echo $<$<BOOL:${USE_BARLIB}>:bar>
    VERBATIM
    )
>>>>>>>>> cut here <<<<<<<<<<<<<

then:
$ make ShowMe

gives:
$<1:any1 any2>
bar
which seems wrong too.

In the TLL you get "$<1:any1 -lany2>" in either Makefile or ninja generator
whereas you get proper "-lbar" 
      
I don't know why this happen but it really looks like a bug.
 

 

I don’t know if other CMake functions will react similarly bad to the list/bool operator combo.

 

Regards,

Björn

 

 

From: Andrew Fuller <[hidden email]>
Sent: Thursday, October 4, 2018 6:16 PM
To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

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 <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:54 PM
To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
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 <[hidden email]> on behalf of Björn Blissing <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:10 PM
To: Björn Blissing <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

 

Le jeu. 4 oct. 2018 à 16:53, Björn Blissing <[hidden email]> 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



--
Eric

--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Björn Blissing
In reply to this post by Marc CHEVRIER

Hi Marc,

 

If that is the case that mixing of “optimized” and “debug” keywords with generator expressions is impossible, I think this should be mentioned in the official documentation. It took us the better part of an afternoon to track down why we suddenly had a dependency to a library called “optimized.lib”.

 

Other people seem to struggle with similar issues as well:
https://www.google.com/search?q=optimized.lib+cmake

 

Regards,

Björn

 

 

From: Marc CHEVRIER <[hidden email]>
Sent: Thursday, October 4, 2018 6:59 PM
To: Björn Blissing <[hidden email]>
Cc: Andrew Fuller <[hidden email]>; Eric Noulard <[hidden email]>; CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

I am afraid that you cannot mix "optimized" or "debug" keywords with "generator expressions" because keywords handling is done during evaluation of command "target_link_libraries" and "generator expressions" are evaluated during generation.

 

And target_link_libraries expect following pattern: [<keyword>] <library> [<library> ...], so specifying a generator expression wrapping this breaks the parsing of the arguments: keyword is no longer at the beginning of the sequence so it is no longer  recognized as is...

 

So, The most efficient way to work-around this problem is to transform your list of libraries in valid generator expressions:

  • INITIAL: optimized foo debug food_d
  • RESULT: $<$<CONFIG:RELEASE>:foo> $<$<CONFIG:DEBUG>:foo_d>

For that purpose, an helper function can do the trick...

 

Le jeu. 4 oct. 2018 à 18:34, Björn Blissing <[hidden email]> a écrit :

Hi Andrew,

 

That works, but as previously said. The third party find module I am using do not differentiate between debug and release libraries. To make matters even worse the keywords “optimized” and “debug” is already in the variable list, trying to split them will be painful.

 

The workaround I am using right now is to have an IF-statement for the list option:

if(${USE_FOOLIB})

    target_link_libraries(my_exe

    PUBLIC

        ${FOO_LIBRARIES}

    )

endif()

 

 

target_link_libraries(my_exe

    PUBLIC

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

)

 

But that breaks the pattern with using generator expressions, as I do for the rest of my options.

 

I don’t know if this should be considered a bug, but it seems really strange that generator expressions should break down for only this special case, i.e. the combination using lists with the conditional operator BOOL and using it inside target_link_libraries.

 

I don’t know if other CMake functions will react similarly bad to the list/bool operator combo.

 

Regards,

Björn

 

 

From: Andrew Fuller <[hidden email]>
Sent: Thursday, October 4, 2018 6:16 PM


To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

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 <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:54 PM
To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
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 <[hidden email]> on behalf of Björn Blissing <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:10 PM
To: Björn Blissing <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

 

Le jeu. 4 oct. 2018 à 16:53, Björn Blissing <[hidden email]> 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

--

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


--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Eric Noulard
In reply to this post by Marc CHEVRIER


Le jeu. 4 oct. 2018 à 18:59, Marc CHEVRIER <[hidden email]> a écrit :
I am afraid that you cannot mix "optimized" or "debug" keywords with "generator expressions" because keywords handling is done during evaluation of command "target_link_libraries" and "generator expressions" are evaluated during generation.

And target_link_libraries expect following pattern: [<keyword>] <library> [<library> ...], so specifying a generator expression wrapping this breaks the parsing of the arguments: keyword is no longer at the beginning of the sequence so it is no longer  recognized as is...

So, The most efficient way to work-around this problem is to transform your list of libraries in valid generator expressions:
  • INITIAL: optimized foo debug food_d
  • RESULT: $<$<CONFIG:RELEASE>:foo> $<$<CONFIG:DEBUG>:foo_d>
For that purpose, an helper function can do the trick...

I agree with you Marc but I nevertheless think the handling of list in genex has something unexpected.

See my previous example with custom target.

--
Eric

--

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
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with conditional generator expression inside target_link_libraries

Marc CHEVRIER
In reply to this post by Björn Blissing
For now, clearly it is not supported. Introducing a generator expression breaks the parsing of arguments of command target_link_libraries.

@Björn I suggest to you to log an issue (see https://gitlab.kitware.com/cmake/cmake/issues) about that (don't forget to put a short example demonstrating the problem) to enable discussion about this and to identify a solution.

Le jeu. 4 oct. 2018 à 19:09, Björn Blissing <[hidden email]> a écrit :

Hi Marc,

 

If that is the case that mixing of “optimized” and “debug” keywords with generator expressions is impossible, I think this should be mentioned in the official documentation. It took us the better part of an afternoon to track down why we suddenly had a dependency to a library called “optimized.lib”.

 

Other people seem to struggle with similar issues as well:
https://www.google.com/search?q=optimized.lib+cmake

 

Regards,

Björn

 

 

From: Marc CHEVRIER <[hidden email]>
Sent: Thursday, October 4, 2018 6:59 PM
To: Björn Blissing <[hidden email]>
Cc: Andrew Fuller <[hidden email]>; Eric Noulard <[hidden email]>; CMake Mailinglist <[hidden email]>


Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

I am afraid that you cannot mix "optimized" or "debug" keywords with "generator expressions" because keywords handling is done during evaluation of command "target_link_libraries" and "generator expressions" are evaluated during generation.

 

And target_link_libraries expect following pattern: [<keyword>] <library> [<library> ...], so specifying a generator expression wrapping this breaks the parsing of the arguments: keyword is no longer at the beginning of the sequence so it is no longer  recognized as is...

 

So, The most efficient way to work-around this problem is to transform your list of libraries in valid generator expressions:

  • INITIAL: optimized foo debug food_d
  • RESULT: $<$<CONFIG:RELEASE>:foo> $<$<CONFIG:DEBUG>:foo_d>

For that purpose, an helper function can do the trick...

 

Le jeu. 4 oct. 2018 à 18:34, Björn Blissing <[hidden email]> a écrit :

Hi Andrew,

 

That works, but as previously said. The third party find module I am using do not differentiate between debug and release libraries. To make matters even worse the keywords “optimized” and “debug” is already in the variable list, trying to split them will be painful.

 

The workaround I am using right now is to have an IF-statement for the list option:

if(${USE_FOOLIB})

    target_link_libraries(my_exe

    PUBLIC

        ${FOO_LIBRARIES}

    )

endif()

 

 

target_link_libraries(my_exe

    PUBLIC

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

)

 

But that breaks the pattern with using generator expressions, as I do for the rest of my options.

 

I don’t know if this should be considered a bug, but it seems really strange that generator expressions should break down for only this special case, i.e. the combination using lists with the conditional operator BOOL and using it inside target_link_libraries.

 

I don’t know if other CMake functions will react similarly bad to the list/bool operator combo.

 

Regards,

Björn

 

 

From: Andrew Fuller <[hidden email]>
Sent: Thursday, October 4, 2018 6:16 PM


To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

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 <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:54 PM
To: Björn Blissing <[hidden email]>; Eric Noulard <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
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 <[hidden email]> on behalf of Björn Blissing <[hidden email]>
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 <[hidden email]>
Sent: Thursday, October 4, 2018 5:10 PM
To: Björn Blissing <[hidden email]>
Cc: CMake Mailinglist <[hidden email]>
Subject: Re: [CMake] Trouble with conditional generator expression inside target_link_libraries

 

 

Le jeu. 4 oct. 2018 à 16:53, Björn Blissing <[hidden email]> 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

--

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


--

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