CMake add_subdirectory导致CPack仅构建源目录

时间:2019-01-14 16:17:41

标签: cmake

我正在为我的团队试用cmake 3.11。我希望演示的内容包括用于管理内部/外部依赖关系的原型。我还必须将我们的可执行文件和库套件分发给可能具有或没有Internet连接并且可能具有或没有能力访问我们的存储库的客户。

目前,我已经开发了可用于以下情况的所有内部项目的模板:

从存储库中提取内容以获取本地网络依赖性

FetchContent_Declare(
    internaldependency
    GIT_REPOSITORY git@repo.net:ProductFamily/Project.git
)
FetchContent_GetProperties(internaldependency)
if(NOT internaldependency_POPULATED)
    FetchContent_Populate(internaldependency)
    add_subdirectory(${internalproject_SOURCE_DIR} ${internalproject_BINARY_DIR} )

这是我为所有要导出为install / cpack的项目开发的模板,理想情况下,这样客户可以将我们开发的任何内容包括在我们产品系列中的库:

include(CPack)
set(CPACK_GENERATOR "TGZ")
include(GNUInstallDirs)
install(TARGETS ourlibrary EXPORT ourlibrary_export
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT  ourlibrary_export
    FILE ourlibraryTargets.cmake
    NAMESPACE productfamily
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ourlibrary
)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    ${CMAKE_CURRENT_BINARY_DIR}/ourlibraryConfigVersion.cmake
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY AnyNewerVersion
)
configure_package_config_file(
    ${CMAKE_CURRENT_LIST_DIR}/CMake/ourlibraryConfig.cmake.in # It's a package init and an inclusion to ourlibraryTargets.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/ourlibraryConfig.cmake
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ourlibrary
)
install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/ourlibraryConfigVersion.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/ourlibraryConfig.cmake
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ourlibrary
)
install(DIRECTORY
    ${CMAKE_CURRENT_BINARY_DIR}/etc/ourlibrary
    DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}
)

考虑到我有MainLibraryDependencyLibrary的情况,它们都在其构建脚本中包含上述安装目标模板:如果MainLibrary通过以下方式包括DependencyLibrary FetchContent + add_subdirectory,当我只希望它根据安装目标生成发布发行版时,cpack始终将源目录归档为源发行版。安装的行为符合预期,如果我使用find_packages来获取DependencyLibrary包含在main中,cpack的行为也会符合预期,但是add_subdirectories导致CPack只想归档源目录。 / p>

为什么会这样,我该如何预防?

1 个答案:

答案 0 :(得分:0)

结果是,问题出在include(CPack)的位置。由于该语句本身负责生成CPack配置,因此在定义这些必需的一切之前,要早于生成这些配置就调用它。

用于设置安装目标的模板已更改如下:

set(CPACK_GENERATOR "ZIP")
include(GNUInstallDirs)
install(TARGETS ourlibrary EXPORT ourlibrary_export
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT  ourlibrary_export
    FILE ourlibraryTargets.cmake
    NAMESPACE productfamily
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ourlibrary
)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    ${CMAKE_CURRENT_BINARY_DIR}/ourlibraryConfigVersion.cmake
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY AnyNewerVersion
)
configure_package_config_file(
    ${CMAKE_CURRENT_LIST_DIR}/CMake/ourlibraryConfig.cmake.in # It's a package init and an inclusion to ourlibraryTargets.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/ourlibraryConfig.cmake
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ourlibrary
)
install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/ourlibraryConfigVersion.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/ourlibraryConfig.cmake
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ourlibrary
)
install(DIRECTORY
    ${CMAKE_CURRENT_BINARY_DIR}/etc/ourlibrary
    DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}
)
include(CPack)

通过移动包含直到定义了安装目标后,我的问题才得以解决。