build: Rework version generation; don't allow make version override

Convert the script to generate the build version from a shell script
to a python script.

Remove the ability to override the version at build time via "make
VERSION=xyz".  Replace it with ability to add extra version
information at build time via "make EXTRAVERSION=xyz".

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
diff --git a/Makefile b/Makefile
index e5f28d4..6e36104 100644
--- a/Makefile
+++ b/Makefile
@@ -51,6 +51,8 @@
 cc-option=$(shell if test -z "`$(1) $(2) -S -o /dev/null -xc /dev/null 2>&1`" \
     ; then echo "$(2)"; else echo "$(3)"; fi ;)
 
+EXTRAVERSION=
+
 CPPFLAGS = -P -MD -MT $@
 
 COMMONCFLAGS := -I$(OUT) -Isrc -Os -MD -g \
@@ -154,10 +156,10 @@
 	@echo "  Compiling (16bit) $@"
 	$(Q)$(CC) $(CFLAGS16) -c -D__ASSEMBLY__ $< -o $@
 
-$(OUT)romlayout16.lds: $(OUT)ccode32flat.o $(OUT)code32seg.o $(OUT)ccode16.o $(OUT)romlayout.o scripts/layoutrom.py scripts/buildversion.sh
+$(OUT)romlayout16.lds: $(OUT)ccode32flat.o $(OUT)code32seg.o $(OUT)ccode16.o $(OUT)romlayout.o src/version.c scripts/layoutrom.py scripts/buildversion.py
 	@echo "  Building ld scripts"
-	$(Q)BUILD_VERSION="$(VERSION)" ./scripts/buildversion.sh $(OUT)version.c
-	$(Q)$(CC) $(CFLAGS32FLAT) -c $(OUT)version.c -o $(OUT)version.o
+	$(Q)$(PYTHON) ./scripts/buildversion.py -e "$(EXTRAVERSION)" $(OUT)autoversion.h
+	$(Q)$(CC) $(CFLAGS32FLAT) -c src/version.c -o $(OUT)version.o
 	$(Q)$(LD) $(LD32BIT_FLAG) -r $(OUT)ccode32flat.o $(OUT)version.o -o $(OUT)code32flat.o
 	$(Q)$(LD) $(LD32BIT_FLAG) -r $(OUT)ccode16.o $(OUT)romlayout.o -o $(OUT)code16.o
 	$(Q)$(OBJDUMP) -thr $(OUT)code32flat.o > $(OUT)code32flat.o.objdump
@@ -226,10 +228,10 @@
 	@echo "  Compiling (16bit) $@"
 	$(Q)$(CC) $(CFLAGS16) -c -D__ASSEMBLY__ $< -o $@
 
-$(OUT)vgarom.o: $(OUT)vgaccode16.o $(OUT)vgaentry.o $(OUT)vgasrc/vgalayout.lds scripts/buildversion.sh
+$(OUT)vgarom.o: $(OUT)vgaccode16.o $(OUT)vgaentry.o $(OUT)vgasrc/vgalayout.lds vgasrc/vgaversion.c scripts/buildversion.py
 	@echo "  Linking $@"
-	$(Q)BUILD_VERSION="$(VERSION)" ./scripts/buildversion.sh $(OUT)vgaversion.c VAR16
-	$(Q)$(CC) $(CFLAGS16) -c $(OUT)vgaversion.c -o $(OUT)vgaversion.o
+	$(Q)$(PYTHON) ./scripts/buildversion.py -e "$(EXTRAVERSION)" $(OUT)autovgaversion.h
+	$(Q)$(CC) $(CFLAGS16) -c vgasrc/vgaversion.c -o $(OUT)vgaversion.o
 	$(Q)$(LD) --gc-sections -T $(OUT)vgasrc/vgalayout.lds $(OUT)vgaccode16.o $(OUT)vgaentry.o $(OUT)vgaversion.o -o $@
 
 $(OUT)vgabios.bin.raw: $(OUT)vgarom.o
diff --git a/scripts/buildversion.py b/scripts/buildversion.py
new file mode 100755
index 0000000..b948134
--- /dev/null
+++ b/scripts/buildversion.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+# Generate version information for a program
+#
+# Copyright (C) 2015  Kevin O'Connor <kevin@koconnor.net>
+#
+# This file may be distributed under the terms of the GNU GPLv3 license.
+import sys, os, subprocess, time, socket, optparse
+
+VERSION_FORMAT = """
+/* DO NOT EDIT!  This is an autogenerated file.  See scripts/buildversion.py. */
+#define BUILD_VERSION "%s"
+"""
+
+# Obtain version info from "git" program
+def git_version():
+    if not os.path.exists('.git'):
+        return ""
+    params = "git describe --tags --long --dirty".split()
+    try:
+        ver = subprocess.check_output(params).decode().strip()
+    except:
+        return ""
+    return ver
+
+# Look for version in a ".version" file
+def file_version():
+    if not os.path.isfile('.version'):
+        return ""
+    try:
+        f = open('.version', 'r')
+        ver = f.readline().strip()
+        f.close()
+    except:
+        return ""
+    return ver
+
+# Generate an output file with the version information
+def write_version(outfile, version):
+    sys.stdout.write("Version: %s\n" % (version,))
+    f = open(outfile, 'w')
+    f.write(VERSION_FORMAT % (version,))
+    f.close()
+
+def main():
+    usage = "%prog [options] <outputheader.h>"
+    opts = optparse.OptionParser(usage)
+    opts.add_option("-e", "--extra", dest="extra", default="",
+                    help="extra version string to append to version")
+
+    options, args = opts.parse_args()
+    if len(args) != 1:
+        opts.error("Incorrect arguments")
+    outfile = args[0]
+
+    ver = git_version()
+    if not ver:
+        ver = file_version()
+        if not ver:
+            ver = "?"
+    btime = time.strftime("%Y%m%d_%H%M%S")
+    hostname = socket.gethostname()
+    ver = "%s-%s-%s%s" % (ver, btime, hostname, options.extra)
+    write_version(outfile, ver)
+
+if __name__ == '__main__':
+    main()
diff --git a/scripts/buildversion.sh b/scripts/buildversion.sh
deleted file mode 100755
index 516aff5..0000000
--- a/scripts/buildversion.sh
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/bin/sh
-# Script to generate a C file with version information.
-OUTFILE="$1"
-VAR16MODE="$2"
-
-# Extract version info
-if [ -z "$BUILD_VERSION" ]; then
-    if [ -d .git -o -f .git ]; then
-        VERSION="`git describe --tags --long --dirty`"
-    elif [ -f .version ]; then
-        VERSION="`cat .version`"
-    else
-        VERSION="?"
-    fi
-    VERSION="${VERSION}-`date +"%Y%m%d_%H%M%S"`-`hostname`"
-else
-    VERSION="$BUILD_VERSION"
-fi
-echo "Version: ${VERSION}"
-
-# Build header file
-if [ "$VAR16MODE" = "VAR16" ]; then
-    cat > ${OUTFILE} <<EOF
-#include "types.h"
-char VERSION[] VAR16 = "${VERSION}";
-EOF
-else
-    cat > ${OUTFILE} <<EOF
-char VERSION[] = "${VERSION}";
-EOF
-fi
diff --git a/src/version.c b/src/version.c
new file mode 100644
index 0000000..d8c266f
--- /dev/null
+++ b/src/version.c
@@ -0,0 +1,4 @@
+// Place build generated version into a C variable
+#include "autoversion.h"
+
+char VERSION[] = BUILD_VERSION;
diff --git a/vgasrc/vgaversion.c b/vgasrc/vgaversion.c
new file mode 100644
index 0000000..02c8ea3
--- /dev/null
+++ b/vgasrc/vgaversion.c
@@ -0,0 +1,5 @@
+// Place build generated version into a C variable
+#include "autovgaversion.h"
+#include "types.h"
+
+char VERSION[] VAR16 = BUILD_VERSION;