Sunday, August 22, 2010

Automatic Serial Number for Test Driven Development. Makefile Tip #0

With the pending release of James Grenning's new book Test Driven Development for Embedded C there has been a lot interest in TDD in the embedded space.

Jack Ganssle did a two part interview with Mr. Grenning this summer [2010], on the subject of TDD in Embedded Development:

Something that I have found very useful is an automatic build or serial number generated with each invocation of Make. The number can be accessed from C for use in version messages, or in the output from the invocation of Make directly.

This technique came out of a discussion by Matt VanDeWerk and Dave Hylands on the AVR-GCC list in 2005.

Create a file called 'buildnumber' that says this in the same directory as your Makefile:

#-------------------Cut Line -----------------------------------#
# This is the current build number of the latest build
# DO NOT EDIT THIS FILE - it is updated automagically
# by the build process.

#-------------------Cut Line -----------------------------------#

In the Makefile add these lines:

#-------------------Cut Line -----------------------------------#
# Add before any 'all:', or other target:
# Auto-incrementing number, which may be used in version numbers:
include buildnumber
NEW_BUILDNUMBER = $(shell echo $$(( $(BUILDNUMBER) + 1 )))


%.elf: $(OBJ)
 @echo Test run: $(NEW_BUILDNUMBER)
# Do normal stuff here, append these two lines, which rewrite the
# buildnumber file for the next Make invocation:
 @echo "# Automatically generated file - do not edit" > buildnumber
 @echo "BUILDNUMBER=$(NEW_BUILDNUMBER)" >> buildnumber
#-------------------Cut Line -----------------------------------#

/*-------------------Cut Line -----------------------------------*/
/* C Example usage:
static uint8_t const BuildNumber_str[] = NEW_BUILDNUMBER_TEXT; /* Build number */
/*-------------------Cut Line -----------------------------------*/

Make does not get enough credit for being the powerful tool that it is.