[CMake] embed version numbers in libraries (.so, a) and executables

Vandenbroucke Sander Sander.Vandenbroucke at vandewiele.com
Thu Mar 20 03:27:14 EDT 2008


I also struggled with getting a SVN revision into my source code. 
By the looks of this my solution can be improved :-)

> CMakeLists.txt:
> =============
> CONFIGURE_FILE(dump_svnversion.sh.in dump_svnversion.sh)
> ADD_CUSTOM_TARGET(OutputSvnVersion ALL
>         ${CMAKE_CURRENT_BINARY_DIR}/dump_svnversion.sh)
>
> # allow including the current_svnversion.h file which in this example is generated in the binary directory
> INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
> SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/current_svnversion.h
>    PROPERTIES GENERATED true)
> ADD_EXECUTABLE(foo
>    foo.cc
>    ${CMAKE_CURRENT_BINARY_DIR}/current_svnversion.h)
>
> dump_svnversion.sh.in:
> =================
> #!/bin/sh
> FILE=@CMAKE_CURRENT_BINARY_DIR@/svnversion_tmp
>
> echo "#ifndef @CMAKE_PROJECT_NAME at _CURRENT_SVNVERSION_H" >  $FILE
> echo "#define @CMAKE_PROJECT_NAME at _CURRENT_SVNVERSION_H" >> $FILE
> echo "#define @CMAKE_PROJECT_NAME at _SUBVERSION_REV \"`svnversion @CMAKE_CURRENT_SOURCE_DIR@`\"" >> $FILE
> echo "#endif" >> $FILE
>
> @CMAKE_COMMAND@ -E copy_if_different @CMAKE_CURRENT_BINARY_DIR@/svnversion_tmp @CMAKE_CURRENT_BINARY_DIR@/current_svnversion.h

This is verry unix like solution. I used a Python script with the PySvn plug-in. For me it works on both XP and Linux.

First I tried a CMake macro as in:
ADD_CUSTOM_TARGET(OutputSvnVersion ALL
         ${MY_SVN_MACRO})

Which obviously doesn't work. It should be a nice feature to have, in my opinion. That way there is no/less need for using external scripts.

Anyway: Could CMake provide some standard macro's to generate such files on build time? It seems to me that a lot of people would benefit from it. I mean this isn't the first post on the subject, is it?

Kind regards,
Sander.


More information about the CMake mailing list