生成器表达式cmake:复制在调试但不是释放模式下工作 [英] Generator expressions cmake: copying works in debug but not release mode

查看:239
本文介绍了生成器表达式cmake:复制在调试但不是释放模式下工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何复制一些libs取决于配置在cmake。



我试过这个:



<$ c $ p> add_custom_command(TARGET Myapp
POST_BUILD
COMMAND $ {CMAKE_COMMAND} -E copy_if_different
$< $< CONFIG:Debug> ;: $ {_ LIBS_DEBUG}>
$< $< CONFIG:Release>:$ {_ LIBS_RELEASE}>
$< TARGET_FILE_DIR:MyApp>)



它在Debug中复制libs但不在发行版中:



  1. 如果不合法(我没有收到错误),我该如何达到同样的效果?


解决方案

将我的评论转换为答案



我通常做的调试这些情况是在实际的线路之前添加另一个 COMMAND 只是回荡的命令行。在您的情况下:

  COMMAND $ {CMAKE_COMMAND} -E echo 
$< $< CONFIG:Debug> :$ {_ LIBS_DEBUG}>
$< $< CONFIG:Release>:$ {_ LIBS_RELEASE}>

我运行了几个测试,你会看到 $ < 1:...> $< 0:...> b
$ b

所以看到这是我正在搜索CMake的bug跟踪数据库,这是一个已知的问题,但是(对于CMake 3.5.2)未解决: 0009974:CMake应该支持可以根据配置变化的自定义命令。



在这张票中提出了几种方法,可以使用现有版本的CMake。






在你的情况下 - 直到这个问题解决,如果你想让它独立于shell - 我会做旧方式并调用CMake脚本:



CopyLibsByConfig.cmake.in

  if(_CONFIG STREQUALDebug)
文件(COPY @ _LIBS_DEBUG @ DESTINATION$ {_ DEST_PATH})
else COPY @ _LIBS_RELEASE @ DESTINATION$ {_ DEST_PATH})
endif()



< > CMakeLists.txt

  ... 
configure_file(CopyLibsByConfig.cmake.in CopyLibsByConfig.cmake @ ONLY)
add_custom_command(TARGET MyApp
POST_BUILD
COMMAND $ {CMAKE_COMMAND}
-D _CONFIG = $< CONFIG>
-D _DEST_PATH =$< TARGET_FILE_DIR :MyApp>
-P$ {CMAKE_CURRENT_BINARY_DIR} /CopyLibsByConfig.cmake


$ b b




但是解决方案可以非常依赖于要复制到二进制输出文件夹的文件。有很多方法可以做到,例如使用 安装()

  install(FILES $ {_ LIBS_DEBUG} CONFIGURATIONS Debug DESTINATION $& ; TARGET_FILE_DIR:MyApp>)
install(FILES $ {_ LIBS_RELEASE} CONFIGURATIONS个发布DESTINATION $< TARGET_FILE_DIR:MyApp>)

set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1)

显然不是 install()的使用方式,正确分配您的应用程序及其所有依赖项的 INSTALL PACKAGE



如果我们谈论Visual Studio运行时DLL,你最有可能想看看 InstallRequiredSystemLibraries CMake模块。


I am trying to figure out how to copy some libs depending on the config in cmake.

I tried this:

add_custom_command(TARGET Myapp
  POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_if_different
  $<$<CONFIG:Debug>:${_LIBS_DEBUG}> 
  $<$<CONFIG:Release>:${_LIBS_RELEASE}> 
  $<TARGET_FILE_DIR:MyApp>)

It copies libs in Debug but not in release:

  1. Is this supposed to be legal and should work?
  2. If it is not legal (I do not get error), how can I achieve the same effect?

解决方案

Turning my comments into an answer

What I normally do to debug those case is to add another COMMAND before the actual line in question that just echos the command line. In your case:

COMMAND ${CMAKE_COMMAND} -E echo 
    $<$<CONFIG:Debug>:${_LIBS_DEBUG}>
    $<$<CONFIG:Release>:${_LIBS_RELEASE}> 

I've run this a few tests and you will see that the $<1:...> and $<0:...> expressions are not evaluated.

So seeing this I was searching CMake's bug tracker database and this is a known issue and yet (as for CMake 3.5.2) unresolved: 0009974: CMake should support custom commands that can vary by configuration.

There are several ways proposed in this ticket that do work with existing versions of CMake.


In your case - until this issue is resolved and if you want to have it shell independent - I would do it the "old way" and call a CMake script:

CopyLibsByConfig.cmake.in

if (_CONFIG STREQUAL "Debug")
    file(COPY @_LIBS_DEBUG@ DESTINATION "${_DEST_PATH}")
else()
    file(COPY @_LIBS_RELEASE@ DESTINATION "${_DEST_PATH}")
endif()

CMakeLists.txt

...
configure_file(CopyLibsByConfig.cmake.in CopyLibsByConfig.cmake @ONLY)
add_custom_command(TARGET MyApp
  POST_BUILD
  COMMAND ${CMAKE_COMMAND} 
    -D _CONFIG=$<CONFIG> 
    -D _DEST_PATH="$<TARGET_FILE_DIR:MyApp>"
    -P "${CMAKE_CURRENT_BINARY_DIR}/CopyLibsByConfig.cmake"
)


But the solution can very much depend on the files you want to copy to your binary output folder. And there are a lot of way doing it, like using install():

install(FILES ${_LIBS_DEBUG} CONFIGURATIONS Debug DESTINATION $<TARGET_FILE_DIR:MyApp>)
install(FILES ${_LIBS_RELEASE} CONFIGURATIONS Release DESTINATION $<TARGET_FILE_DIR:MyApp>)

set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1)

Obviously that's not the way install() is meant to be used, so consider using the INSTALL or PACKAGE targets properly to distribute your application and all its dependencies.

And if we are talking about Visual Studio runtime DLLs you most likely want to take a look at the InstallRequiredSystemLibraries CMake module.

这篇关于生成器表达式cmake:复制在调试但不是释放模式下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆