Search code examples
gitcmakecpack

CPack: only pack files tracked by git


I'm trying to understand how to use CPack. I want to produce a binary and a source package. The binary one is perfectly fine, including only what's necessary. In the source distribution, though, there are basically all files present in the project's root directory - including my CMake build directory from where I ran the CPack command.

Since one might have several build directories, I can't see how I can exclude them all from being put into the source package.

set(CPACK_SOURCE_IGNORE_FILES "*.git*") works fine but ideally I would like to restrict the set of included files only to those that are tracked by version control, e.g. git, in the first place. And, no, I don't consider making sure that my project directory is completely clean before a cpack a viable option.

(Packing an entire directory into an archive is something I don't need CPack for.)


Solution

  • As far as I know, you can only configure CPack source package files by making a great big ignore file list. You could get inspiration from your own .gitignore files or some other sources. For example:

    list(APPEND CPACK_SOURCE_IGNORE_FILES "/\.git/")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/CMakeFiles/")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/_CPack_Packages/")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/bin(ary)?/")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/build/")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/dist/")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/libs?/")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/logs?/")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/objs?/")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/[tT]e?mp/")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/___pycache__")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/\.cache")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/\.gitignore.*")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "/CMakeCache.txt")
    list(APPEND CPACK_SOURCE_IGNORE_FILES "\.pyc$")
    

    etc.