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

How to specify different folder structure for CPack TGZ generator?

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

install (
    TARGETS myTarget
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
)

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

set(CPACK_GENERATOR "TGZ")

make package 生成一个 tar.gz 文件,其文件夹结构与指定的 install 命令相同。但是,我想要一个平面结构,即将所有内容(包括可执行文件和库)都放在 "prefix" 中,而没有 "bin" 和 "lib" 目录。

这可能吗?可能是对组件系统、构建类型系统或 CPACK_PROJECT_CONFIG_FILE?

的巧妙使用

最后我添加了一个自定义安装脚本,它通过查看 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()