根据FAQ,CMake 不会创建make dist
目标,并且可以使用 CPack 创建源包。但是 CPack 只是制作了一个源目录的 tarball,其中包含所有与CPACK_SOURCE_IGNORE_FILES
.
另一方面,make dist
由 autotools 生成的仅捆绑它知道的文件,主要是编译所需的源。
任何人都有一种聪明的方法来制作只包含 CMakeLists.txt (及其依赖项)中指定的文件的源包?
我一直在考虑这个问题,我不会假装我可以在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)
注意:usedBUILDSYSTEM_TARGETS
属性至少需要 CMake 版本 3.7
我将上面的代码视为起点和概念证明。您可以根据需要添加库、标头等,但您可能应该只调整cpack以进行投标。
作为起点,请参见评论中提供的链接@usr1234567。
参考
西蒙在上面是正确的,但没有给出完整的答案。使用 git,您可以使用 git archive 命令生成兼容的 tar 球存档。
这个例子与过去的版本是兼容make dist
的。
git archive --format=tar.gz -o my-repo-0.01.tar.gz --prefix=my-repo-0.01/ master
见:https ://gist.github.com/simonw/a44af92b4b255981161eacc304417368