You could always write a simple program in Python or something to create an include file that has simple #define
statements with a build number, time, and date. You would then need to run this program before doing a build.
If you like I'll write one and post source here.
If you are lucky, your build tool (IDE or whatever) might have the ability to run an external command, and then you could have the external tool rewrite the include file automatically with each build.
EDIT: Here's a Python program. This writes a file called build_num.h
and has an integer build number that starts at 1 and increments each time this program is run; it also writes #define
values for the year, month, date, hours, minutes and seconds of the time this program is run. It also has a #define
for major and minor parts of the version number, plus the full VERSION
and COMPLETE_VERSION
that you wanted. (I wasn't sure what you wanted for the date and time numbers, so I went for just concatenated digits from the date and time. You can change this easily.)
Each time you run it, it reads in the build_num.h
file, and parses it for the build number; if the build_num.h
file does not exist, it starts the build number at 1. Likewise it parses out major and minor version numbers, and if the file does not exist defaults those to version 0.1.
import time
FNAME = "build_num.h"
build_num = None
version_major = None
version_minor = None
DEF_BUILD_NUM = "#define BUILD_NUM "
DEF_VERSION_MAJOR = "#define VERSION_MAJOR "
DEF_VERSION_MINOR = "#define VERSION_MINOR "
def get_int(s_marker, line):
_, _, s = line.partition(s_marker) # we want the part after the marker
return int(s)
try:
with open(FNAME) as f:
for line in f:
if DEF_BUILD_NUM in line:
build_num = get_int(DEF_BUILD_NUM, line)
build_num += 1
elif DEF_VERSION_MAJOR in line:
version_major = get_int(DEF_VERSION_MAJOR, line)
elif DEF_VERSION_MINOR in line:
version_minor = get_int(DEF_VERSION_MINOR, line)
except IOError:
build_num = 1
version_major = 0
version_minor = 1
assert None not in (build_num, version_major, version_minor)
with open(FNAME, 'w') as f:
f.write("#ifndef BUILD_NUM_H\n")
f.write("#define BUILD_NUM_H\n")
f.write("\n")
f.write(DEF_BUILD_NUM + "%d\n" % build_num)
f.write("\n")
t = time.localtime()
f.write("#define BUILD_YEAR %d\n" % t.tm_year)
f.write("#define BUILD_MONTH %d\n" % t.tm_mon)
f.write("#define BUILD_DATE %d\n" % t.tm_mday)
f.write("#define BUILD_HOUR %d\n" % t.tm_hour)
f.write("#define BUILD_MIN %d\n" % t.tm_min)
f.write("#define BUILD_SEC %d\n" % t.tm_sec)
f.write("\n")
f.write("#define VERSION_MAJOR %d\n" % version_major)
f.write("#define VERSION_MINOR %d\n" % version_minor)
f.write("\n")
f.write("#define VERSION \"%d.%d\"\n" % (version_major, version_minor))
s = "%d.%d.%04d%02d%02d.%02d%02d%02d" % (version_major, version_minor,
t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
f.write("#define COMPLETE_VERSION \"%s\"\n" % s)
f.write("\n")
f.write("#endif // BUILD_NUM_H\n")
I made all the defines just be integers, but since they are simple integers you can use the standard stringizing tricks to build a string out of them if you like. Also you can trivially extend it to build additional pre-defined strings.
This program should run fine under Python 2.6 or later, including any Python 3.x version. You could run it under an old Python with a few changes, like not using .partition()
to parse the string.