如何为CPack TGZ生成器指定不同的文件夹结构?

时间:2016-04-12 22:25:21

标签: cmake archive cpack

我有一个CMake项目,根据install命令将内容安装到系统中,如下所示:

install (
    TARGETS myTarget
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
)

make install完美无缺。然后我想要一个二进制存档:

set(CPACK_GENERATOR "TGZ")

make package生成一个tar.gz文件,其文件夹结构与指定的install命令相同。但是,我希望有一个扁平结构,即将所有内容(包括可执行文件和库)放在“前缀”中,而不是“bin”和“lib”目录。

这可能吗?可以巧妙地使用组件系统,构建类型系统或CPACK_PROJECT_CONFIG_FILE

1 个答案:

答案 0 :(得分:0)

最后,我添加了一个自定义安装脚本,通过查看CMAKE_INSTALL_PREFIX来检测它是否由CPack运行,并在必要时重新构建安装树。

这是我的解决方案:

在CMakeLists.txt中,在所有install()命令之后,添加

install(SCRIPT "${CMAKE_SOURCE_DIR}/cmake/flatten.cmake")

添加文件" cmake / flatten.cmake",内容如下

# Detect if the install is run by CPack.
if (${CMAKE_INSTALL_PREFIX} MATCHES "/_CPack_Packages/.*/(TGZ|ZIP)/")
    # Flatten the directory structure such that everything except the header files is placed in root.
    file(GLOB bin_files LIST_DIRECTORIES FALSE ${CMAKE_INSTALL_PREFIX}/bin/*)
    file(GLOB lib_files LIST_DIRECTORIES FALSE ${CMAKE_INSTALL_PREFIX}/lib/*)
    foreach(file ${bin_files} ${lib_files})
        get_filename_component(file_name ${file} NAME)
        execute_process(
            COMMAND ${CMAKE_COMMAND} -E rename
            ${file}
            ${CMAKE_INSTALL_PREFIX}/${file_name}
        )
    endforeach()
    execute_process( COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_INSTALL_PREFIX}/bin)
    execute_process( COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_INSTALL_PREFIX}/lib)
endif()