Equivalent to "make dist" in CMake

According to the FAQ , CMake does not create a make dist target, and the source package can be created using CPack. But CPack simply creates a tarball of the source directory with all files that do not match the patterns in CPACK_SOURCE_IGNORE_FILES .

On the other hand, the make dist generated by autotools only links the files it knows about, mostly the sources needed to compile.

Does anyone have a smart way to make the source package only with the files specified in CMakeLists.txt (and its dependencies)?

+8
cmake packaging cpack
source share
1 answer

I thought about this for a while, and I will not pretend that I can simulate 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 hand (for example, to pre-build libraries), and on the other hand CMake is not aware of the dependencies directly checked by the generated build environment itself (for example, any header dependencies).

So, here is the code that just collects all the CMakeList.txt and source files given for any build purpose:

 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 BUILDSYSTEM_TARGETS property BUILDSYSTEM_TARGETS requires at least CMake version 3.7.

I see the code above as a starting point and prove the concept. You can add libraries, headers, etc. Of necessity, but you probably should just configure cpack to complete your bets.

As a starting point, see, for example, the link @ usr1234567 in the comments.

References

  • Get all source files the target depends on in CMake
0
source share

All Articles