Search code examples
cmakecpack

Rename the output of CPack


I would like to rename the installer file that CPack (v2.8.7) produces to include a version number that is obtained at build time from the version control system. It appears this cannot be be done by setting the CPACK_* variables because that happens at "cmake" time.

What I want to be able to do is to run "(n)make package" and have the installer file be created with no further commands required. The two possible approaches that I am aware of are manipulating the CPack filename variables at build time and renaming the final output of CPack.

If using "include(CPack)" in a CMakeLists.txt file then it appears that CPack always runs last and you can't have a post-build command. This mailing list message suggests that a custom target can be written to run CPack, but I was unable to figure out how to do that without creating infinite recursion.

How can this be done?


Solution

  • With a bit of help from the CMake mailing list I figured out how to do it, using subversion.

    CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)
    project(myapp)
    
    add_executable(main main.cpp)
    install(TARGETS main DESTINATION .)
    
    add_custom_target(first ALL
        # update the working copy
        COMMAND ${Subversion_SVN_EXECUTABLE} update ${CMAKE_SOURCE_DIR}
    
        # generate cpackoptions.cmake at build time so we get the
        # most recent revision number
        COMMAND ${CMAKE_COMMAND}
        -DSOURCE_DIR=${CMAKE_SOURCE_DIR}
        -DBINARY_DIR=${CMAKE_BINARY_DIR}
        -Dproj_name=${CMAKE_PROJECT_NAME}
        -P ${CMAKE_SOURCE_DIR}/create-cpackoptions.cmake
        )
    
    add_dependencies(main first)
    
    set(CPACK_PROJECT_CONFIG_FILE ${CMAKE_BINARY_DIR}/CPackOptions.cmake)
    
    include(CPack)
    

    create-cpackoptions.cmake

    include(FindSubversion)
    Subversion_WC_INFO(${SOURCE_DIR} ${proj_name})
    
    set(revision ${${proj_name}_WC_REVISION})
    
    configure_file(${SOURCE_DIR}/CPackOptions.cmake.in
        ${BINARY_DIR}/CPackOptions.cmake
        @ONLY)
    

    cpackOptions.cmake.in

    set(CPACK_PACKAGE_FILE_NAME "@proj_name@-${CPACK_PACKAGE_VERSION}r@revision@-${CPACK_SYSTEM_NAME}")