use svn revision number in application version

c++, continuous-integration, svn, visual-studio-2010

Solution

the best guess i came up with so far is to generate a header file in the pre-build event that contains the svnversion.

e.g. i create a batch-script `version.bat`:

@echo off
FOR /F "tokens=*" %%i IN ('call svnversion') DO echo #define SVNVERSION "%%i" > svnversion.h

and wherever i want to get the svnversion, i simply `#include "svnversion.h"`

Problem

In a VS2010 solution (not .NET), I'm looking to include the svn revision number as part of the application version. we do not use makefiles at the moment, only the VS solution/project settings. I'd like to get the working copy revision number at compile time, store it into a variable, so it can be used later on in the code to display the version number. So far, I've successfully used `svnversion` to get the latest version of the working copy as a pre-built event. ``` "C:\Program Files\CollabNet\Subversion Client\svnversion.exe" -n $(SolutionDir) ``` At build time, I can see the correct revision number being returned into the `output` console. Now, the question is, how can store this value into a variable that can be used within the code? I've tried defining a pre compiler variable (`_SVNREV`) and using it to save the result from the above cmd, directly from the `pre-build` event box, but that doesn't work. ``` _SVNREV="C:\Program Files\CollabNet\Subversion Client\svnversion.exe" -n $(SolutionDir) %_SVNREV%="C:\Program Files\CollabNet\Subversion Client\svnversion.exe" -n $(SolutionDir) %_SVNREV="C:\Program Files\CollabNet\Subversion Client\svnversion.exe" -n $(SolutionDir) $(_SVNREV)="C:\Program Files\CollabNet\Subversion Client\svnversion.exe" -n $(SolutionDir) ``` none of these actually work. RESOLUTION: I was heading nowhere trying to update a variable from within the VS env. so I took another route, calling a script as a pre-build step, that gets the svn revision of the working copy and then creates a header file with that info. Here's the `svnrev.bat` for anyone interested: ``` @echo off set cmd="C:\"Program Files\CollabNet\Subversion Client"\svnversion.exe -n %1 " set versionfile=%1/version.h FOR /F %%i IN ('%cmd%') DO SET SVNVER=%%i echo Detected program revision %SVNVER% in %1 echo #pragma once > %versionfile% echo #define _SVNVER "%SVNVER%" >> %versionfile% ```

Original source

Related problems