Use CMake to get build-time Subversion revision

Viewed 17131

With CMake I can get the Subversion revision using Subversion_WC_INFO. However, this only happens at configure time -- every subsequent make will use this cached revision.

I'd like to get the svn revision at build time (ie, every time Make is run). How can I do this?

6 Answers

Ideally this would be a comment on richq's answer, but I don't have enough reputation yet. If an admin could convert it to one, that would be great.

One issue I found (apart from the dependency one that was resolved by Mark) is that the SVN revision support in CMake only includes the "base revision", not any modifiers (in particular whether it is an "M" (modified) build).

So instead of the suggested getsvn.cmake, I used this:

# Set current SVN version into a variable
execute_process(COMMAND svnversion ${SOURCE_ROOT} OUTPUT_VARIABLE MY_WC_REVISION OUTPUT_STRIP_TRAILING_WHITESPACE)
# write a file with the SVNVERSION define
file(WRITE svnversion.h.txt "#define SVN_REV \"${MY_WC_REVISION}\"\n")
# copy the file to the final header only if the version changes
# reduces needless rebuilds
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
                    svnversion.h.txt svnversion.h)

And because I don't just want the revision of the current directory, I am passing SOURCE_ROOT (the directory for the top of the SVN tree) in to the custom command, instead of SOURCE_DIR, and I work out the correct value for that elsewhere in my CMakeLists.txt tree

Related