linux - cmake 2.8 自定义目标复制多个文件
问题描述
我不得不在 Linux 环境中使用较旧的 cmake 版本 2.8.12。
作为预构建步骤,我必须将多个头文件从源目录复制到目标目录。我决定使用add_custom_target
从句。如果这本身就是个坏主意,请告诉我。例如:
add_custom_target( prebuild
COMMENT "Prebuild step: copy other headers"
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/../other/include/alpha.h ${CMAKE_SOURCE_DIR}/include/other
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/../other/include/bravo.h ${CMAKE_SOURCE_DIR}/include/other
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_SOURCE_DIR}/../other/include/charlie.h ${CMAKE_SOURCE_DIR}/include/other
)
add_executable( myapp main.cxx )
# My application depends on the pre-build step.
add_dependencies( myapp prebuild )
set_target_properties( myapp PROPERTIES COMPILE_FLAGS "-g" )
install( TARGETS myapp DESTINATION ${BIN_INSTALL_DIR} )
列出每个头文件会很乏味。我知道如何搜索所有头文件并将它们放入列表变量中。例如。
file( GLOB other_headers "${CMAKE_SOURCE_DIR}/../other/include/*.h" )
但是,如何将该列表变量放在add_custom_target
子句中使用?
有没有办法在add_custom_target
子句中复制多个文件?
有没有更好的方法来复制多个文件作为构建我的应用程序的依赖项的预构建步骤?
受限于旧版本的 cmake 限制了我的选择。以下是我尝试过但没有成功的事情。
- 如果我有 cmake 3.5 版,那么该
copy_if_different
命令可以采用多个源路径。 - 添加
COMMAND_EXPAND_LISTS
到add_custom_target
子句没有效果。直到更新的 cmake 版本才可以使用。 - 在子句中使用
foreach
循环add_custom_target
不起作用。在我看到的其他示例中,它们总是使用foreach
循环,并且add_custom_target
在该循环内。但是,如果我这样做,那么我不知道如何将其作为构建我的应用程序的依赖项。
解决方案
在子句中使用
foreach
循环add_custom_target
不起作用。
但是使用foreach
您可以创建一个包含所有必需命令的变量。然后在 add_custom_target 中使用该变量:
set(commands)
# Assume 'other_headers' contain list of files
foreach(header ${other_headers})
list(APPEND commands
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${header} ${CMAKE_SOURCE_DIR}/include/other)
endforeach()
add_custom_target( prebuild
COMMENT "Prebuild step: copy other headers"
${commands}
)
推荐阅读
- unity3d - 如何从 Blender 中导出 .dae 动画?
- ios - 创建类似于 iOS 联系人的 UI 编辑 UI
- python - 如何创建一组 tkinter 小部件(for 循环)并单独引用它们中的每一个?
- netlogo - 协助 Netlogo 中的节点/链接导航
- tensorflow - 自定义层输出 Keras 的尺寸
- jenkins - 如何在 Jenkins Job DSL 插件的上下文中使用共享/常见的 groovy 方法
- javascript - 多个源浏览器选项卡,单个目标选项卡
- oracle - 如何在没有按钮的情况下将项目值从一个页面传递到另一个页面?
- drools - 优化 Redis 中 Drools kieBase 的存储
- php - 访客如何也可以从 TorrentTrade v3 下载 Torrent