在CMake中"make dist"相当于

mar*_*cin 10 packaging cmake cpack

根据FAQ,CMake不会创建make dist目标,可以使用CPack创建源包.但是CPack只是使用与模式不匹配的所有文件制作源目录的tarball CPACK_SOURCE_IGNORE_FILES.

另一方面,make dist由autotools生成的文件只包含它所知道的文件,主要是编译所需的文件.

任何人都有一种聪明的方法来制作一个只包含在CMakeLists.txt(及其依赖项)中指定的文件的源包?

Flo*_*ian 3

我已经考虑这个问题有一段时间了,我不会假装我可以在make dist没有 CMake 本身直接支持的情况下模拟 a。

问题是,一方面您可以使用 CMake 添加大量文件依赖项(例如预构建库),另一方面 CMake 不知道生成的构建环境本身直接检查的依赖项(例如任何标头依赖项) )。

因此,这里的代码仅收集CMakeList.txt任何构建目标给出的所有文件和源文件:

function(make_dist_creator _variable _access _value _current_list_file _stack)
    if (_access STREQUAL "MODIFIED_ACCESS")
        # Check if we are finished (end of main CMakeLists.txt)
        if (NOT _current_list_file)
            get_property(_subdirs GLOBAL PROPERTY MAKE_DIST_DIRECTORIES)
            list(REMOVE_DUPLICATES _subdirs)
            foreach(_subdir IN LISTS _subdirs)
                list(APPEND _make_dist_sources "${_subdir}/CMakeLists.txt")
                get_property(_targets DIRECTORY "${_subdir}" PROPERTY BUILDSYSTEM_TARGETS)
                foreach(_target IN LISTS _targets)
                    get_property(_sources TARGET "${_target}" PROPERTY SOURCES)
                    foreach(_source IN LISTS _sources)
                        list(APPEND _make_dist_sources "${_subdir}/${_source}")
                    endforeach()
                endforeach()
            endforeach()

            add_custom_target(
                dist
                COMMAND "${CMAKE_COMMAND}" -E tar zcvf "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.tar.gz" -- ${_make_dist_sources}
                COMMENT "Make distribution ${PROJECT_NAME}.tar.gz"
                WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
            )
            message("_make_dist_sources = ${_make_dist_sources}")
        else()
            # else collect subdirectories in my source dir
            file(RELATIVE_PATH _dir_rel "${CMAKE_SOURCE_DIR}" "${_value}")
            if (NOT _dir_rel MATCHES "\.\.")
                set_property(GLOBAL APPEND PROPERTY MAKE_DIST_DIRECTORIES "${_value}")
            endif()
        endif()
    endif()
endfunction()

variable_watch("CMAKE_CURRENT_LIST_DIR" make_dist_creator)
Run Code Online (Sandbox Code Playgroud)

:所用BUILDSYSTEM_TARGETS属性至少需要 CMake 3.7 版本

我将上面的代码视为一个起点和概念证明。您可以根据需要添加库、标头等,但您可能应该调整来执行您的命令。

作为起点,请参阅评论中提供的链接@usr1234567。

参考