Incrementing Version Numbers

Dealing with software version numbers is a pain. If you try to do it manually you will invariably end up forgetting to update the version information in your source code or forget to commit the changes. Having programatic access to version information is helpful too.

Today I wrote a simple header file containing version information and a very simple awk script that increments the build number. In MS Visual Studio 2005 I set a pre-build event that runs the awk script so I never forget to increment version information. The header file contains a rather hacky but workable trick to convert the version numbers into a conventional dot separated string. Having the information both as numeric values and as a string is very helpful.

The script:

awk '{if ($2 ~ /BUILD/) print $1 " " $2 " " ++$3; else print}'

The version header file:

#ifndef _VERSION_H
#define _VERSION_H

/**
* Short simple description of why this version exists.
*/
#define VERSION_DESC "added evidence type names to output"

/** major release number  */
#define MAJOR 0
/** minor release number  */
#define MINOR 7
/** build number (increment for every single build) */
#define BUILD 2

/**
* Converts the argument to a string constant.  This is used by
* VERSION_STRING to create a version string from version numbers
*/
#define STRINGIFY(s) #s

/**
* Returns a dot separated version string.
*/
#define VERSION_STRING(major, minor, build) 
STRINGIFY(major) "." STRINGIFY(minor) "." STRINGIFY(build)

#endif

The interesting part of the header file is the two-level macro usage. In the VERSION_STRING macro we want to create a character constant. If we used ‘#’ to turn the arguments into character constants we would get “major.minor.build” which is not desired. To correct that we use the STRINGIFY macro that creates a character constant of the value of the macro argument. Thus we get a string of “0.7.2” when we call VERSION_STRING.

Advertisements
This entry was posted in Code, Geek, Tips. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s