Tuesday, March 29, 2016

Autoincrementing build number in Atmel Studio 7 (and Visual Studio too).

This approach uses Windows batch file which reads current version from major_version.txt and current build number from build_number.txt, then increments the latter and composes header file build_number.h with different representations of version numbers. It's used as pre-built event in Atmel Studio 7 (which uses Visual Studio 2015 engine).

First thing you need is to manually create build_number.txt with the number 1 and major_version.txt with any number, for example, "1.0" (no quotation marks) in it before calling the batch. So both files are just one-liners with numbers.

Visual Studio pre- and post-build events are independent for Debug and Release configurations. You can choose to use different version numbers for each one. In this case you put separate files used for version numbers (including the batch file) in corresponding Debug and Release folders and edit my examples so the Studio finds this files not in project folder but in Debug or Release ones.

I chose to use one copy of version and build number for both Debug and Release configurations, that's why I place the batch file and both major_version.txt and build_number.txt in project directory, not Debug or Release ones. Also, that's why there's "..\\" before filenames in batch - otherwise pre-build event will try to find files in Debug or Release directory and complain there's no such files.

Here's my batch file:

set /p version=<..\\major_version.txt
@echo %version%
set /p old=<..\\build_number.txt
set /a new=%old%+1
@echo New build number: %new%
@echo Version: %version%.%new%
@echo %new% > ..\\build_number.txt
(
  echo #ifndef BUILD_NUMBER
  echo #define BUILD_NUMBER %new%U
  echo #endif
  echo #ifndef BUILD_NUMBER_STR
  echo #define BUILD_NUMBER_STR "%new%"
  echo #endif
  echo #ifndef VERSION_FULL_STR
  echo #define VERSION_FULL_STR "%version%.%new%"
  echo #endif
) > ..\\build_number.h

And in Pre-Build event command line you add  "$(MSBuildProjectDirectory)\make_buildnum.bat" (no quotation marks).



On each build there's the output in Studio showing new build number and full version:


Here's the resulting header file in project folder after calling the exact BAT file I showed earlier:
#ifndef BUILD_NUMBER
#define BUILD_NUMBER 24U
#endif
#ifndef BUILD_NUMBER_STR
#define BUILD_NUMBER_STR "24"
#endif
#ifndef VERSION_FULL_STR
#define VERSION_FULL_STR "0.9.24"
#endif


Hope this helps anyone.