[CMake] Addition to FindGit

Eric Noulard eric.noulard at gmail.com
Tue Jul 12 03:35:22 EDT 2011


2011/7/12 Thomas Petazzoni <thomas.petazzoni at free-electrons.com>:
> Hello,
>
> Le Sun, 05 Jun 2011 11:02:00 +0200,
> Quintus <sutniuq at gmx.net> a écrit :
>
>> I'm working on a git-versioned project that I'd like to display it's
>> version number for development versions like this:
>>
>> 1.2.3-dev (commit abc1234 on devel, 12/4/10)
>
> For  similar need I did something like :
>
> execute_process(COMMAND sh ../getlocalversion OUTPUT_VARIABLE MYPROG_VERSION)
> add_definitions(-DMYPROG_VERSION="${MYPROG_VERSION}")
>
> where getlocalversion is a shell script that does :
>
> printf "%s%s" $(git rev-parse --verify --short HEAD) $(if test `git
> diff-index --name-only HEAD | wc -l` -ne 0 ; then echo "-dirty"; fi)
>
> However, I am not very happy with this, because the version is
> determined at configuration time, and not at compile time. Therefore,
> if I make some changes and commit them to the Git repository without
> re-doing the CMake configuration step, the program version isn't
> changed. Any idea on how to make sure that the version of the program
> is updated at every compilation ?

If you want to execute **build time** scripts you should use

add_custom_command/add_custom_target

You'll have to make your script create a header that will be included
in the concerned source file(s), something like:

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/version.h
                                   COMMAND  getlocalversion --output
${CMAKE_CURRENT_BINARY_DIR}/version.h)

If the script is smart enough not to re-create the file if the version
did not change
then it should work without too much overhead. If not this will trigger
a undue recompilation.

if the version.h concerns a single target you could use:

add_custom_command(TARGET versionedTarget
                                   PRE_BUILD
                                   COMMAND  getlocalversion --output
${CMAKE_CURRENT_BINARY_DIR}/version.h)

this build of the "versionedTarget" should trigger the command.



-- 
Erk
Membre de l'April - « promouvoir et défendre le logiciel libre » -
http://www.april.org


More information about the CMake mailing list