svnversion and Kdevelop

8 June, 2007 - 14:26

Subversion keyword substitution is a nice concept, but the revision number refers to the revision the file was last changed, not the current version of the file. If you have for example a project with several files (e.g. main.c, pol.c, mol.s) and in main.c you have code that should render the current version of the program (like printf("Revision: $Revision$");). That revision keyword in main.c will not be expanded if you change and commit pol.c or mol.c, so the rendered revision will be wrong in that case.

With the Subversion tool svnversion it is possible to get more usable version information about your working copy. With this trick involving some Makefile magick it is possible to use it in your source code.

It got even messier when I tried to do this within Kdevelop. It involved a lot of trial and error, but I obtained a solution.

  1. Add the following line to the file Makefile.am of each directory where you want to use the svn version string:
    SVNREV = $(shell svnversion -n $(srcdir))
    This will create a variable $SVNREV with as value the current revision of the src directory at build (make) time.
  2. This variable has to be available to the compile command as compiler flag -DSVNREV="$SVNREV". Go to Menu "Project" > "Project options" > "Configure options", under the "C" and "C++" tab enter/add in/to the "Compiler flags" field:
    -DSVNREV=\\\"\$(SVNREV)\\\"
    (Those extra escape "\" thingies are needed to counter string parsing/expanding during the configure stage.)
    Screenshot:
  3. Use the svn version string in your code. e.g.:
    #ifdef SVNREV
    cout << "Version: " << SVNREV << endl;
    #endif

Fhew.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed
  • Lines and paragraphs break automatically.

More information about formatting options