"make dist"相当于CMake [英] "make dist" equivalent in CMake

查看:520
本文介绍了"make dist"相当于CMake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

另一方面,自动工具生成的make dist仅捆绑其知道的文件,主要是编译所需的源.

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

解决方案

我已经考虑了好一阵子,并且我不假装我可以模拟make dist而无需CMake本身直接支持. /p>

问题是,您可以使用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)
 

注意:使用的 BUILDSYSTEM_TARGETS 属性至少需要CMake 3.7版

我将以上代码视为起点并证明了概念.您可以根据需要添加库,标头等,但是您可能应该只调整进行出价.

作为起点,请参见例如在评论中提供的链接 @ usr1234567.

参考

According to FAQ, CMake doesn't create a make dist target and source package can be created using CPack. But CPack just makes a tarball of the source directory with all files that don't match patterns in CPACK_SOURCE_IGNORE_FILES.

On the other hand, make dist generated by autotools bundles only files it knows about, mostly sources needed for compilation.

Anyone has a smart way of making a source package with only files that are specified in CMakeLists.txt (and its dependencies)?

解决方案

I've been thinking about this for a while and I won't pretend I can simulate a make dist without having this directly supported by CMake itself.

The problem is that you can add a lot of file dependencies with CMake on the one side (e.g. to pre-build libraries) and on the other side CMake does not know about dependencies directly checked by the generated build environment itself (e.g. any header dependencies).

So here is a code that just collects all CMakeList.txt and source files given with any build targets:

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)

Note: The used BUILDSYSTEM_TARGETS property needs at least CMake version 3.7

I see the code above as an starting point and prove of concept. You could add libraries, headers, etc. on a need-by basis, but you should probably just tweak to do your bidding.

As a starting point see e.g. the link @usr1234567 provided in the comments.

References

这篇关于"make dist"相当于CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆