Feb 25, 2011

Putting the Current SVN Revision in a Header File with Eclipse

I've never been a big fan of cluttering up the top of my source code with a bunch of comments like the author name, company name, copyright, file revision, reasons for changes etc. All this information strikes me as data that belongs in my source control management system, not my source code.
I can see the value of knowing for a particular executable, which SVN revision it was built from. I can imagine others might like to see dates and other information. However, I don't see why it would be needed in every file. I develop embedded firmware with Eclipse and C++. I like to have an ID? command in all my instruments that returns the version of the firmware currently running. In the past I have used various "artificial" numbering schemes, but using the actual revision number from the SCM is less work and more useful. If you use TortoiseSVN it's pretty easy too.  Open the Windows->Preferences and add a prebuild step that runs the SubWCRev command included with TortoiseSVN. You can learn a lot about the command by just issuing it from a command prompt. The usage line looks likes this:
SubWCRev WorkingCopyPath [SrcVersionFile DstVersionFile] [-nmdf]
Below is an example put into the Pre-build step of eclipse. I'm using ${workspace_loc} to get the absolute path to my workspace and my project is called SyncorLibrary. You can just hard code the entire path if you prefer.  Here's the entire prebuild line where version.txt has my source file and I want the destination file to be version.h:

SubWCRev  ${workspace_loc:/SyncorLibrary} ${workspace_loc:/SyncorLibrary/version.txt}  ${workspace_loc}\SyncorLibrary\version.h
The version.txt file can contain anything you want as long as it has the placeholders you want SubWCRev to replace. Here's one example:
#ifndef VERSION_H_
#define VERSION_H_
    static char GTT_TOUCHSCREEN_TESTS_VERSION[] = "1.0.$WCREV$.$WCMODS?1:0$";

The placeholder $WCREV$ will get replaced with the highest revision in svn for this project. The $WCMOD?1:0$ will get replaced with a 0 if the the repository is up to date or with a 1 if the current code hasn't been committed yet. It's probably a good idea to leave both version.h and version.txt out of the repository. Version.h will get completely overwritten with the output of SubWCRev.

After building the project version.h will contain this for the above example:

Special NetBurner addendum

If you want to use this version information from a web page in your NetBurner project, take the following steps:

Create an hmtlvar.h file inside your html folder and add a reference to your version.h as shown in the image below: Since my version.h sits one level up from src folder I have a ../version path for my include. Then add the html folder to your C++ include directory path (very important step).  Finally inside any html page where you want to show the version you can add
and the version number will show up.

About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.