[CMake] Addition to FindGit

Eric Noulard eric.noulard at gmail.com
Tue Jul 12 09:50:28 EDT 2011


2011/7/12 Michael Hertling <mhertling at online.de>:
> On 07/12/2011 09:35 AM, Eric Noulard wrote:
>> 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.
>
> For the custom command, use a CMake script which
>
> (1) invokes the desired git command via EXECUTE_PROCESS()
> (2) uses the results to convert a version.h.in template to
>    the final version.h header via CONFIGURE_FILE(... @ONLY).
>
> The CONFIGURE_FILE() command does not touch its output file if it
> already exists and wouldn't change, so there won't be unneccesary
> recompilations.
>
>> 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.
>
> This doesn't work for Makefiles if the version.h header is to be
> incorporated in the versiondTarget because PRE_BUILD == PRE_LINK,
> i.e. all of versiondTarget's compilations have already taken place
> when the custom command is run and regenerates the version.h header.

Ok then I didn't remember that point,
thank you Michael for fixing my mistake.


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


More information about the CMake mailing list