29 Apr

Automatic build numbers

I needed a program that would keep track of a single number, increase it on demand and output the number to the standard output. This is because I don’t really like using any IDEs (at least anything for MinGW) and so I don’t have all the modern luxuries like automatic build numbers (a sequential number that increases every time the program gets built). I have only an editor, a compiler, and make. I have included the packaging of the releases in the makefile (i.e. make & make installer). It’s eventually confusing to name all of your installers with the same filename, so that’s what I need the build number for.

The program I wrote, called bn, stores the build number in a single-line file. bn filename increases the number in filename and outputs the result. If you add noinc after the filename only the current number is output. In the makefile, add bn buildnumber in the rule that builds your executable:

program.exe: program.c
  gcc program.c -o program.exe
  bn buildnumber

Every time program.exe gets built, the number in the file buildnumber increases by one. I use the limited command line interface in Windows but the make I use has the more capable shell sh built-in, so I can have a rule like this (cp -f is the same as copy /y in sh):

installer: program.exe
  makensis program.nsi   # outputs the installer as installer.exe
  cp -f installer.exe installer-build-`bn buildnumber noinc`.exe

In sh, everything that is between two grave accents (`) gets treated as a separate command as you would type it in. However, this makes sh substitute bn buildnumber noinc with whatever the command prints in stdout. So, effectively the line in the makefile is seen as (assuming the build number is 1000):

  cp -f installer.exe installer-build-1000.exe

Note: You have to manually create a file named buildnumber. Just save a text file with a single line with the number that you want to start counting from. This is so that you don’t accidentally overwrite something.

Scratch one tedious manual task per release! You can also use this to inject numbers in the actual executable or keep track of times a program is run (by using a batch file).

Download the program with the source code (in C)