How I can add a changelog in deb with CMAKE?

Viewed 1105

I am building a debian package with CMAKE and CPACK. Everything is smooth with the build except that I can't find how to add a changelog file in the deb package. Is there any way to do it?

1 Answers

Since at least CMake 3, the Debian CPack generator provides the variable CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA, which is a list of arbitrary files to add to the control section of the package.

You can write/generate your changelog file, and add it to this variable:

set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_SOURCE_DIR}/debian/changelog")

See the CMake docs for more.

===== EDIT =====

Lintian (quite rightly) doesn't like the above solution. The changelog should be compressed and installed in /usr/share/doc/package-name/changelog.gz. The following code works on Linux:

include(GNUInstallDirs)

add_custom_command(
    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz
    COMMAND gzip -cn9 "${CMAKE_CURRENT_SOURCE_DIR}/changelog" > "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz"
    WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
    DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/changelog"
    COMMENT "Compressing changelog"
)

add_custom_target(changelog ALL DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz")

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz"
    DESTINATION "${CMAKE_INSTALL_DOCDIR}"
)

add_custom_command adds a hook to re-generate changelog.gz when its dependencies (changelog) change. add_custom_target adds a target to generate changelog.gz at build time. install installs the compressed changelog to the correct location.

The code should go in a CMakeLists.txt file in the same directory as the changelog. Unfortunately, CMake doesn't seem to have a cross-platform way to compress single files yet.

Related