Index: tools/autogen2.awk
===================================================================
--- tools/autogen2.awk	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
+++ tools/autogen2.awk	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -0,0 +1,23 @@
+#!/usr/bin/awk -f
+
+/}.*;/ {
+	if (($0 != "};") && ($0 != "} " struct_name "_t;")) {
+		print("Bad struct ending: " $0) > "/dev/stderr"
+		exit 1
+	}
+	print "DEFINE_STRUCT(" struct_name ", " toupper(struct_name) ")"
+	struct_name = ""
+}
+
+/;$/ {
+	if (struct_name != "") {
+		# Remove array subscript, if any.
+		sub("(\\[.*\\])?;", "", $0)
+		member = $NF
+		print "DEFINE_MEMBER(" struct_name ", " toupper(struct_name) ", " member ", " toupper(member) ")"
+	}
+}
+
+/^typedef struct .* \{/ {
+	struct_name = $3
+}
Index: tools/autogen2.sh
===================================================================
--- tools/autogen2.sh	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ tools/autogen2.sh	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -1,35 +1,57 @@
 #!/bin/sh
 
-filename="$1"
-struct_name="$2"
+# There's a bunch of restriction. The script assumes that the structure definition begins with "typedef struct struct_name {",
+# ends with "struct_name_t;", and that every word in between that ends with ";" is a struct member name with optional
+# array subscript. Error handling is mostly omitted for simplicity, so any input that does not follow these rules will result
+# in cryptic errors.
 
-members=`cat $filename | sed -n -e "1,/typedef struct $struct_name {/d" -e "/} ${struct_name}_t;/,$$d" -e "s/.* \([^ ]\+\);/\1/p"`
+echo $PWD
 
-ustruct_name=`echo "$struct_name" | awk '{print toupper($0)}'`
-dmembers=`echo "$members" | awk '{print "DEFINE_MEMBER(" $0 ", " toupper($0) ")" }'`
+input="$1"
+output="$2"
+toolsdir="$(readlink -f $(dirname "$0"))"
+awkscript="$toolsdir/autogen2.awk"
 
+# Set default value for $CC.
 if [ -z "${CC}" ]; then
 	CC=cc
 fi
 
+# If $CC is clang, we need to make sure integrated assembler is not used.
 if ( ${CC} --version | grep clang > /dev/null ) ; then
 	CFLAGS="${CFLAGS} -no-integrated-as"
 fi
 
-echo "/* Autogenerated file, do not modify. */"
-echo "#pragma once"
+# Tell the compiler to generate makefile dependencies.
+CFLAGS="${CFLAGS} -MD -MP -MT $output -MF $output.d"
 
-( ${CC} ${CFLAGS} -w -S -x c - -o - | sed -n 's/^.* #define \([^ ]\+\) [^0-9]*\([0-9]\+\) .*/#define \1 \2/p' ) <<- EOF
+# Generate defines
+defs=`$awkscript -- $input || exit 1`
 
-#include "$filename"
+# Generate C file.
+cat > $output.c <<- EOF
 
-#define DEFINE_MEMBER(lc, uc) \
-	asm volatile ("/* #define ${ustruct_name}_OFFSET_"#uc" %0 */" :: "i" (__builtin_offsetof(struct $struct_name, lc))); \
-	asm volatile ("/* #define ${ustruct_name}_SIZE_"#uc" %0 */" :: "i" (sizeof((struct $struct_name){}.lc)));
+#include "`basename $input`"
+
+#define DEFINE_MEMBER(ls, us, lm, um) \
+	asm volatile ("/* #define "#us"_OFFSET_"#um" %0 */" :: "i" (__builtin_offsetof(struct ls, lm))); \
+	asm volatile ("/* #define "#us"_SIZE_"#um" %0 */" :: "i" (sizeof((struct ls){}.lm)));
+
+#define DEFINE_STRUCT(ls, us) \\
+	asm volatile ("/* #define "#us"_SIZE %0 */" :: "i" (sizeof(struct ls)));
 
 void autogen()
 {
-	$dmembers
+	$defs
 }
 
 EOF
+
+# Turn the C file into assembly.
+${CC} ${CFLAGS} -w -S -o $output.s $output.c || exit 1
+
+# Process the output.
+
+echo "/* Autogenerated file, do not modify. */" > $output
+echo "#pragma once" >> $output
+sed -n 's/^.* #define \([^ ]\+\) [^0-9]*\([0-9]\+\) .*/#define \1 \2/p' < $output.s > $output || exit 1
Index: tools/autotool.py
===================================================================
--- tools/autotool.py	(revision d5e5fd1214ee9617b9a936d6f09efec4c9a91b48)
+++ tools/autotool.py	(revision 5af6cf3d6b44b525ad694f194a5101ab4bd5e115)
@@ -681,6 +681,4 @@
 		sandbox_leave(owd)
 
-	common['AUTOGEN'] = "%s/autogen.py" % os.path.dirname(os.path.abspath(sys.argv[0]))
-
 	create_makefile(MAKEFILE, common)
 	create_header(HEADER, macros)
