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?
This is more of a pain to do than it needs to be. You can run a program at build time to extract the version information from subversion. CMake itself can be used as this program using its scripting language. You can run any CMake script using the -P command line flag. The piece of code to do this would be (To be placed in file
getsvn.cmake):This extracts the subversion revision information and writes it to a header file. This header file is only updated when needed to avoid recompiling all the time. In your program, you would include that header file to get at the SVNVERSION define.
The
CMakeLists.txtfile that you would need to run the script would be something like this:You need to use
add_custom_targetandadd_custom_commandsince there are no inputs to the svnheader.h file, it “appears from nowhere” to cmake. I already dealt with the issue of needless rebuilds in the getsvn.cmake file though, so rebuilding the “svnheader” target is basically a no-op if the subversion revision has not changed.Finally, an example
main.cfile: