[CMake] Automatically add a revision number to the CPack installer name

Glenn Ramsey gr at componic.co.nz
Thu Mar 8 20:35:48 EST 2012


On 09/03/12 12:00, Eric Noulard wrote:
> 2012/3/8 Glenn Ramsey<gr at componic.co.nz>:
>> Yes, you can use add_custom_command(...) to run a script to get the revision
>> number, but I don't think you can use that to alter CPACK_PACKAGE_FILE_NAME,
>> which has been set at cmake time.
>
> This true but how wouldn't a revision update trigger a CMake re-run?
>
> Whatever the reason if you want to change/alter/set a CPACK_xxx value
> at CPack time ,
> independently of CMakre re-run, you can use  a CPACK_PROJECT_CONFIG_FILE:
>
> CPACK_PROJECT_CONFIG_FILE
>         CPack-time project CPack configuration file.
>
>         This file included at cpack time, once per generator after CPack has
>         set CPACK_GENERATOR to the actual generator being used.  It allows
>         per-generator setting of CPACK_* variables at cpack time.
>
> In your CMakeLists.txt you have to:
>
> set(CPACK_PROJECT_CONFIG_FILE ${CMAKE_CURRENT_SOURCE_DIR}/MyCPackConf.cmake)
>
> then write the logic you want in MyCPackConf.cmake.
>
> see:
> http://www.cmake.org/Wiki/CMake:CPackPackageGenerators#Overall_usage_.28common_to_all_generators.29
>
> This CPACK_PROJECT_CONFIG_FILE is a CMake script file loaded by CPack
> (which includes
> a CMake script interpreter) you cannot use non scriptable CMake command in there
> (and no add_xxx to make it simple) but you can perfectly use
> execute_process in order to run
> some SCM commands.
>
> You should be able to override CPACK_PACKAGE_FILE_NAME dynamically in there.
> You have access to all CPACK_xxx variable value.
>
> However, in this script file you do not have access to CMAKE_xxx
> variables you can usually access in CMakeLists.txt
> (no CMAKE_PROJECT_NAME, nor orther variables YOU defined in the CMakeLists.txt
>   besides CPACK_xxx ones)
> If you need some of these values you'll have to create a
> MyCPackConf.cmake.in
> which will be configured **at CMake-time**
> into
> MyCPackConf.cmake
>
> CMake itself is using this with the "CMakeCPackOptions.cmake.in" file
> at the root of CMake
> sources.

Thanks Eric, this lead me to a solution that works for me, where the revision 
number is updated at build time. For future reference here is what I did:

-- 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 at -${CPACK_PACKAGE_VERSION}r at revision@-${CPACK_SYSTEM_NAME}")






More information about the CMake mailing list