问题如下:
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 ...
据我所知:
include(CPack)
)两次(例如,无法在src
级别和SecondApp
级别的CMakeLists.txt中调用它; 在ProjectDir/tools/SomeTool/CMakeLists.txt
我(它的pseado代码):
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)
这样我就可以致电make SomeTool-package
,然后创建内容为SomeTool.rpm
的包(/usr/bin/SomeTool
)。太好了!
我可以在CMakeLists.txt
目录中使用非常相似的ProjectDir/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
也与 FirstApp 和 SecondApp 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 可执行文件名,我也必须在这里更改它。安装路径也是如此。