Rename CPack Output

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

What I want to do is run "(n) make package" and create an installer file without any additional commands. Two possible approaches that I know of are manipulating the CPack file name variables during build and renaming the final CPack output.

If you use "include (CPack)" in the CMakeLists.txt file, then it seems that CPack always works last and you cannot have a post-build command. This mailing list message allows you to indicate that a custom target can be written to run CPack, but I could not figure out how to do this without creating an infinite recursion.

How can I do that?

+5
source share
2 answers

With a little help from the CMake mailing list, I figured out how to do this 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}")
+5
source

Why not extract build-info from VCS in cmake-time? Then you can easily change CPACK_PACKAGE_FILE_NAME to indicate the version number.

: CMake-time, , . "Readme.txt" git -info, CMake configure_file . , , "config.h", .

: CMake, Git . Git , ...

# First try to find the git-executable
find_program( Git_EXECUTABLE NAMES git git.cmd PATHS
    ${Git_DIR}
    ENV PATHS
    $ENV{Git_DIR}
)
# Run "git log -n 1 --pretty="%h" for the current commit-hash
execute_process( COMMAND ${Git_EXECUTABLE} "log" "-n" "1" "--pretty=\"%h\"" 
                 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 
                 OUTPUT_VARIABLE Git_Commit_Hash 
                 OUTPUT_STRIP_TRAILING_WHITESPACE
                 )
# and use a regex to strip quotes.
string( REGEX REPLACE "^\"(.*)\"$" "\\1" Git_Commit_Hash ${Git_Commit_Hash} )

Git_Commit_Hash 7- char, CPack:

set( CPACK_PACKAGE_NAME "MyProject" )
message( STATUS "    CPack options: " ${CPACK_PACKAGE_NAME} )
message( STATUS "    Preparing CPACK: " )
message( STATUS "      and hash: ${Git_Commit_Hash}" )

set( CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}_${Git_Build_Version}_${CPACK_PACKAGE_VERSION}" )
+4

All Articles