1

问题如下:

项目结构:

ProjectDir
- CMakeLists.txt
- src
-- CMakeLists.txt
-- FirstApp
--- CMakeLists.txt
--- ... sources ...
-- SecondApp
--- CMakeLists.txt
--- ... sources ...
- tools
-- CMakeLists.txt
-- SomeTool
--- CMakeLists.txt
--- ... sources ...
-- SomeOtherTool
--- CMakeLists.txt
--- ... sources ...

我想要实现的是:

  • 带有SomeTool二进制文件的包(让它成为rpmdeb) ;
  • 包含FirstAppSecondAppSomeOtherTool二进制文件;

我所知道的:

  • CPack 将安装命令中定义的内容放入包内容中;
  • CPack 不能include(CPack)在同一棵树中被调用 ( ) 两次(例如,它不能在 CMakeLists.txtsrc级别 ANDSecondApp级别调用);

我有什么

ProjectDir/tools/SomeTool/CMakeLists.txt我(这是伪代码)中:

PROJECT( SomeTool )

SET(TARGET SomeTool)

SET(CPACK_GENERATOR "RPM")
SET(CPACK_PACKAGE_NAME "SomeTool")
# ...
# all the other CPACK_* stuff
# ...

include(CPack)
add_custom_target(${TARGET}-package 
                  COMMAND cpack --config "${CMAKE_CURRENT_BINARY_DIR}/CPack${PROJECT_NAME}Config.cmake")

SET(SOURCES ...)
SET(HEADERS ...)
add_executable(${TARGET} ${SOURCES} ${HEADERS})
install(TARGETS ${TARGET}
        RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/usr/bin)

这样我就可以调用它,它会创建带有 content的make SomeTool-packagepackage ( ) 。伟大的!SomeTool.rpm/usr/bin/SomeTool


我可以CMakeLists.txtProjectDir/src/目录中有非常相似的:

# src/CMakeLists.txt
PROJECT( MainProject )

SET(CPACK_GENERATOR "RPM")
SET(CPACK_PACKAGE_NAME "MainProject")
# ...
# all the other CPACK_* stuff
# ...

include(CPack)
add_custom_target(MainProject-package 
                  COMMAND cpack --config "${CMAKE_CURRENT_BINARY_DIR}/CPack${PROJECT_NAME}Config.cmake")

add_subdirectory(FirstApp)
add_subdirectory(SecondApp)

# src/FirstApp/CMakeLists.txt
SET(TARGET FirstApp)
SET(SOURCES ...)
SET(HEADERS ...)
add_executable(${TARGET} ${SOURCES} ${HEADERS})
install(TARGETS ${TARGET}
        RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/usr/bin)

# src/SecondApp/CMakeLists.txt
SET(TARGET SecondApp)
SET(SOURCES ...)
SET(HEADERS ...)
add_executable(${TARGET} ${SOURCES} ${HEADERS})
install(TARGETS ${TARGET}
        RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/usr/bin)

这样我可以调用make MainProject-package,它会创建MainProject.rpm包含内容的包 ()/usr/bin/FirstApp/usr/bin/SecondApp. 伟大的!

SomeOtherTool也有一个CMakeLists.txt看起来与FirstAppSecondApp CMakeLists.txt相同的东西,我可以调用make SomeOtherTool它,它会生成SomeOtherTool二进制文件。

问题

由于 CPack 正在将连接到install命令的东西放入包中,我如何将SomeOtherTool与我的MainProject-package连接?

我的解决方案是(是)install在末尾添加另一个子句,src/CMakeLists.txt如下所示:

add_dependency(MainProject SomeOtherTool)
install(FILES ${CMAKE_BINARY_DIR}/SomeOtherTool DESTINATION ${CMAKE_INSTALL_PREFIX}/usr/bin )

还有其他方法吗?这个解决方案看起来不干净。如果我更改SomeOtherTool可执行文件名称,我也必须在这里更改它。安装路径也是如此。

4

0 回答 0