CMake set_property command with generator expressions using multiple values

走远了吗. 提交于 2020-12-12 06:04:07

问题


I am using CMake v3.13.4 with the Visual Studio 2017 Win64 generator and I need to modify the command line options for the Visual Studio Librarian (for a CMake object library).

To achieve that CMake offers the target property STATIC_LIBRARY_OPTIONS that can be set by the set_property and set_target_properties command.

The documentation states that STATIC_LIBRARY_OPTIONS supports generator expressions:

Contents of STATIC_LIBRARY_OPTIONS may use “generator expressions” with the syntax $<...>. See the cmake-generator-expressions(7) manual for available expressions. See the cmake-buildsystem(7) manual for more on defining buildsystem properties.

But: No matter what I try, the values are not properly escaped´, e.g.

set_property(TARGET object_library PROPERTY STATIC_LIBRARY_OPTIONS $<$<CONFIG:Release>:/WX /NOLOGO /LTCG /ERRORREPORT:NONE>)

leads to the following in Visual Studio:

All Options: `/OUT:"..." /LTCG /MACHINE:X64 /NOLOGO 
Additional Options: %(AdditionalOptions) /machine:x64 ""$"<1:/WX" "/ERRORREPORT:NONE>"

I've tried the following, all of them seem to fail.

set_property(TARGET object_library PROPERTY
  STATIC_LIBRARY_OPTIONS $<$<CONFIG:Release>:/WX /NOLOGO /LTCG /ERRORREPORT:NONE>
  #STATIC_LIBRARY_OPTIONS "$<$<CONFIG:Release>:/WX /NOLOGO /LTCG /ERRORREPORT:NONE>"
  #STATIC_LIBRARY_OPTIONS $<$<CONFIG:Release>:"/WX /NOLOGO /LTCG /ERRORREPORT:NONE">
  #STATIC_LIBRARY_OPTIONS $<$<CONFIG:Release>:/WX /NOLOGO /LTCG /ERRORREPORT:NONE>
  #STATIC_LIBRARY_OPTIONS $<$<CONFIG:Release>:/WX;/NOLOGO;/LTCG;/ERRORREPORT:NONE>
  #STATIC_LIBRARY_OPTIONS $<$<CONFIG:Release>:"/WX;/NOLOGO;/LTCG;/ERRORREPORT:NONE">
  #STATIC_LIBRARY_OPTIONS "$<$<CONFIG:Release>:/WX;/NOLOGO;/LTCG;/ERRORREPORT:NONE>"
  )

So my question is: How can I pass multiple values using generator expressions to STATIC_LIBRARY_OPTIONS with the set_property or set_target_properties command?


回答1:


Thanks to @Tsyvarev, I've been able to solve this problem. The solution is to wrap each value into its own generator expression:

set_property(TARGET object_library PROPERTY
    STATIC_LIBRARY_OPTIONS
      $<$<CXX_COMPILER_ID:MSVC>:/WX>
      $<$<CXX_COMPILER_ID:MSVC>:/NOLOGO>
      $<$<CXX_COMPILER_ID:MSVC>:/ERRORREPORT:NONE>
      $<$<CXX_COMPILER_ID:MSVC>:$<$<CONFIG:Release>:/LTCG>>
  )



回答2:


Assuming you have a list of options:

set(option_list /WX /NOLOGO /LTCG /ERRORREPORT:NONE)

you may create a list of generator expressions for them by a single command:

list(TRANSFORM option_list
  REPLACE ".+" "$<$<CXX_COMPILER_ID:MSVC>:\\0>"
  OUTPUT_VARIABLE option_list_msvc
)

Resulted list can be used directly:

set_property(TARGET object_library PROPERTY STATIC_LIBRARY_OPTIONS ${option_list_msvc})

Command flow list(TRANSFORM) is available since CMake 3.12.




回答3:


This answer is for future readers with newer cmake: As detailed in this answer by @firmament, depending on the cmake version, it may also be possible to put all values into a list via

set(my_option_list /WX /NOLOGO /LTCG /ERRORREPORT:NONE)

and pass this list to the generator expression:

"<$<CONFIG:Release>:${my_option_list}>"

This works on cmake 3.18.4 with clang and gcc generator expressions.



来源:https://stackoverflow.com/questions/55262774/cmake-set-property-command-with-generator-expressions-using-multiple-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!